|
30 | 30 | ) |
31 | 31 |
|
32 | 32 |
|
| 33 | +def write_original_dflash_gguf(path: Path) -> None: |
| 34 | + from gguf import GGUFWriter |
| 35 | + |
| 36 | + metadata = { |
| 37 | + "embedding_length": 32, |
| 38 | + "block_count": 1, |
| 39 | + "attention.head_count": 4, |
| 40 | + "attention.head_count_kv": 2, |
| 41 | + "attention.key_length": 8, |
| 42 | + "feed_forward_length": 64, |
| 43 | + "block_size": 4, |
| 44 | + "context_length": 64, |
| 45 | + } |
| 46 | + config = DFlashConfig.from_gguf_metadata( |
| 47 | + {"dflash." + key: value for key, value in metadata.items()} |
| 48 | + | {"dflash.target_layers": [0, 1], "tokenizer.ggml.mask_token_id": 0} |
| 49 | + ) |
| 50 | + weights = dict(DFlashDraftModel(config).named_parameters()) |
| 51 | + names = { |
| 52 | + "fc.weight": "fc.weight", |
| 53 | + "enc.output_norm.weight": "hidden_norm.weight", |
| 54 | + "output_norm.weight": "norm.weight", |
| 55 | + "blk.0.attn_q.weight": "layers.0.self_attn.q_proj.weight", |
| 56 | + "blk.0.attn_k.weight": "layers.0.self_attn.k_proj.weight", |
| 57 | + "blk.0.attn_v.weight": "layers.0.self_attn.v_proj.weight", |
| 58 | + "blk.0.attn_output.weight": "layers.0.self_attn.o_proj.weight", |
| 59 | + "blk.0.attn_q_norm.weight": "layers.0.self_attn.q_norm.weight", |
| 60 | + "blk.0.attn_k_norm.weight": "layers.0.self_attn.k_norm.weight", |
| 61 | + "blk.0.attn_norm.weight": "layers.0.input_layernorm.weight", |
| 62 | + "blk.0.ffn_norm.weight": "layers.0.post_attention_layernorm.weight", |
| 63 | + "blk.0.ffn_gate.weight": "layers.0.mlp.gate_proj.weight", |
| 64 | + "blk.0.ffn_up.weight": "layers.0.mlp.up_proj.weight", |
| 65 | + "blk.0.ffn_down.weight": "layers.0.mlp.down_proj.weight", |
| 66 | + } |
| 67 | + writer = GGUFWriter(str(path), "dflash") |
| 68 | + for key, value in metadata.items(): |
| 69 | + writer.add_uint32("dflash." + key, value) |
| 70 | + writer.add_array("dflash.target_layers", [0, 1]) |
| 71 | + writer.add_uint32("tokenizer.ggml.mask_token_id", 0) |
| 72 | + for name, parameter in names.items(): |
| 73 | + writer.add_tensor(name, weights[parameter].detach().numpy()) |
| 74 | + writer.write_header_to_file() |
| 75 | + writer.write_kv_data_to_file() |
| 76 | + writer.write_tensors_to_file() |
| 77 | + writer.close() |
| 78 | + |
| 79 | + |
33 | 80 | class ReferenceTarget(torch.nn.Module): |
34 | 81 | def __init__(self): |
35 | 82 | super().__init__() |
@@ -214,6 +261,26 @@ def old_uniform_mask(total_context, block, capacity, window): |
214 | 261 |
|
215 | 262 |
|
216 | 263 | class DFlash2OperationsTest(TestCase): |
| 264 | + def test_gguf_preserves_checkpoint_capacity(self): |
| 265 | + from executorch.examples.models.muse_glimmer.loaders.dflash_loader import ( |
| 266 | + load_dflash_gguf, |
| 267 | + ) |
| 268 | + |
| 269 | + with tempfile.TemporaryDirectory() as directory: |
| 270 | + path = Path(directory) / "draft.gguf" |
| 271 | + write_original_dflash_gguf(path) |
| 272 | + for backend in ("cuda", "mlx"): |
| 273 | + for max_seq_len in (32, 128): |
| 274 | + with self.subTest(backend=backend, max_seq_len=max_seq_len): |
| 275 | + draft, config = load_dflash_gguf( |
| 276 | + str(path), backend=backend, max_seq_len=max_seq_len |
| 277 | + ) |
| 278 | + self.assertEqual(config.max_seq_len, 64) |
| 279 | + self.assertEqual( |
| 280 | + draft.layers[0].self_attn.kv_cache.k_cache.shape[2], |
| 281 | + max_seq_len, |
| 282 | + ) |
| 283 | + |
217 | 284 | def test_invalid_metadata(self): |
218 | 285 | for metadata in ( |
219 | 286 | {"conv_kernel_size": 2}, |
|
0 commit comments