Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/gpu/nvidia/native/models/flash-next/check.py # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / gpu / nvidia / native / models / flash-next / check.py` 186 lines ยท 8377 bytes Raw Blame History 1 """Compare the seeded lookup with the installed Transformers model and real FP8 rows.""" 2 import argparse 3 import bisect 4 import contextlib 5 import hashlib 6 import json 7 import random 8 import struct 9 import subprocess 10 import tempfile 11 from pathlib import Path 12 13 import torch 14 from safetensors import safe_open 15 from transformers import AutoConfig 16 from transformers.models.qwen4_exp.modeling_qwen4_exp import ( 17 Qwen4ExpTextModel, Qwen4ExpTextNGramEmbedding, Qwen4ExpTextRotaryEmbedding, 18 ) 19 20 21 PREFIX = "model.language_model.layers.1.ple.ple_embedding." 22 INDEX_HASH = "`0419e2c2dfbb925257d7409405433a793cf7ff7d96f3eba882a815ec6d9fe7a6`" 23 CHAT = [248045, 846, 198, 20206, 440, 799, 15262, 1132, 13, 220, 17, 10, 24 17, 30, 248046, 198, 248045, 74455, 198, 248068, 271, 248069, 271, 19, 248046] 25 26 27 class ReferenceTable(torch.nn.Module): 28 def __init__(self, tables, scale): 29 super().__init__() 30 self.register_buffer("weight", torch.empty(0)) 31 self.tables = tables 32 self.scale = scale 33 self.ends = [] 34 self.used = set() 35 rows = 0 36 for table in tables: 37 count, width = table.get_shape() 38 assert width == 160 39 rows += count 40 self.ends.append(rows) 41 42 def forward(self, indices): 43 rows = [] 44 for row in indices.flatten().tolist(): 45 shard = bisect.bisect_right(self.ends, row) 46 self.used.add(shard) 47 local = row - (self.ends[shard - 1] if shard else 0) 48 rows.append(self.tables[shard][local:local + 1]) 49 values = torch.cat(rows).float() * self.scale.float() 50 return values.bfloat16().reshape(*indices.shape, 160) 51 52 53 def decoder_input(cfg, weight, ids): 54 """Capture the real input to the first Transformers decoder block without loading later blocks.""" 55 class ReachedDecoder(Exception): 56 pass 57 58 def capture(_module, args): 59 raise ReachedDecoder(args[0].view(torch.uint16).numpy().tobytes()) 60 61 with torch.device("meta"): 62 model = Qwen4ExpTextModel(cfg) 63 model.embed_tokens = torch.nn.Embedding.from_pretrained(weight) 64 model.rotary_emb = Qwen4ExpTextRotaryEmbedding(cfg) 65 model.layers[0].register_forward_pre_hook(capture) 66 masks = {"linear_attention": None, "qwen_sparse_attention": None} 67 try: 68 with torch.inference_mode(): 69 model(torch.tensor([ids]), attention_mask=masks, use_cache=False) 70 except ReachedDecoder as result: 71 return result.args[0] 72 raise AssertionError("Transformers did not enter the first decoder block") 73 74 75 def check(exe, model): 76 index = (model / "model.safetensors.index.json").read_bytes() 77 assert hashlib.sha256(index).hexdigest() == INDEX_HASH 78 locations = json.loads(index)["weight_map"] 79 cfg = AutoConfig.from_pretrained(model, local_files_only=True).text_config 80 rng = random.Random(1331) 81 ids = CHAT + [cfg.eos_token_id, 0, cfg.eos_token_id, cfg.eos_token_id, 82 cfg.vocab_size - 1] + [rng.randrange(cfg.vocab_size) for _ in range(240)] 83 data = struct.pack(f"<{len(ids)}I", *ids) 84 with safe_open(model / locations["model.language_model.embed_tokens.weight"], framework="pt") as shard: 85 weight = shard.get_tensor("model.language_model.embed_tokens.weight") 86 assert weight.shape == (cfg.vocab_size, cfg.hidden_size) 87 residual = decoder_input(cfg, weight, ids) 88 89 def input_run(payload, cwd=model, mode="--decoder-input"): 90 return subprocess.run([exe, mode], input=payload, stdout=subprocess.PIPE, cwd=cwd, timeout=60) 91 92 actual = input_run(data) 93 assert actual.returncode == 0 and actual.stdout == residual 94 assert len(residual) == len(ids) * 4 * 5120 95 assert input_run(b"").returncode == 0 96 for bad in (b"x", struct.pack("