"""The plain PyTorch pieces against the originals: transformers' Qwen 3.5 backbone on gepard's own weights (fp32), the audio interface, heads and compressor against the code as written upstream (reimplemented here from the published files).""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) import torch from safetensors.torch import load_file from transformers.models.qwen3_5.configuration_qwen3_5 import Qwen3_5TextConfig from transformers.models.qwen3_5.modeling_qwen3_5 import Qwen3_5TextModel import gepard_model as gm cfg = dict(vocab_size=248320, hidden_size=1024, intermediate_size=3584, num_hidden_layers=14, num_attention_heads=8, num_key_value_heads=2, head_dim=256, hidden_act="silu", rms_norm_eps=1e-6, max_position_embeddings=262144, attention_bias=False, attn_output_gate=True, layer_types=["full_attention"] * 14, rope_parameters={"mrope_interleaved": True, "mrope_section": [11, 11, 10], "rope_type": "default", "rope_theta": 10000000, "partial_rotary_factor": 1.0}, partial_rotary_factor=1.0, use_cache=False, tie_word_embeddings=True) hf = Qwen3_5TextModel(Qwen3_5TextConfig(**cfg, attn_implementation="eager")).eval().float() sd = {k[len("model."):]: v.float() for k, v in load_file(str(gm.WEIGHTS)).items() if k.startswith("model.")} print("load", hf.load_state_dict(sd, strict=False)) g, comp, emb = gm.load() torch.manual_seed(0) ids = torch.randint(0, 248000, (1, 40)); x = torch.nn.functional.embedding(ids, emb) with torch.no_grad(): ref = hf(inputs_embeds=x, attention_mask=torch.ones(1, 40, dtype=torch.long)).last_hidden_state P = 25; mask = torch.zeros(P, P).masked_fill(torch.triu(torch.ones(P, P), 1).bool(), float("-inf")) cos, sin = gm.rope(0, P); e = torch.zeros(14, 2, 0, 256) h, k, v = g.decoder(x[:, :P], e, e, cos, sin, mask) print("prefill: max diff", float((h - ref[:, :P]).abs().max()), "of", float(ref.abs().max())) outs = [h] for t in range(P, 40): c, s = gm.rope(t, 1); hh, k1, v1 = g.decoder(x[:, t:t + 1], k, v, c, s, torch.zeros(1, t + 1)); k, v = torch.cat((k, k1), 2), torch.cat((v, v1), 2); outs.append(hh) print("with memory, step by step: max diff", float((torch.cat(outs, 1) - ref).abs().max()))