Merged
[refactor] reuse upstream interfaces for DeepSeek-V4.1 #1143
1Fire4Add on Oct 31
[refactor] reuse upstream interfaces for DeepSeek-V4.1 #1143
Merged
Total: 3 files changed+23-11
| @@ -67,6 +67,28 @@ def _merged_hf_dict() -> dict[str, torch.Tensor]: | |||
| 67 | return _base_hf_dict() | _vision_hf_dict() | 67 | return _base_hf_dict() | _vision_hf_dict() |
| 68 | 68 | ||
| 69 | 69 | ||
| 70 | +@pytest.mark.parametrize("flavor", ["deepseek_v4_1_debugmodel_text", "deepseek_v4_1_debugmodel"]) | ||
| 71 | +def test_layer_weights_round_trip_for_text_and_multimodal(flavor): | ||
| 72 | + config = model_registry(flavor) | ||
| 73 | + adapter = DeepSeekV41StateDictAdapter(model_config=config, hf_assets_path=None) | ||
| 74 | + hf_weights = { | ||
| 75 | + "layers.3.attn.wq_a.weight": torch.arange(12, dtype=torch.float32).reshape(3, 4), | ||
| 76 | + "layers.3.ffn.gate.weight": torch.arange(8, dtype=torch.float32).reshape(2, 4), | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + local = adapter.from_hf(hf_weights) | ||
| 80 | + torch.testing.assert_close( | ||
| 81 | + local["layers.3.attention.wq_a.weight"], hf_weights["layers.3.attn.wq_a.weight"], rtol=0, atol=0 | ||
| 82 | + ) | ||
| 83 | + torch.testing.assert_close( | ||
| 84 | + local["layers.3.moe.router.gate.weight"], hf_weights["layers.3.ffn.gate.weight"], rtol=0, atol=0 | ||
| 85 | + ) | ||
| 86 | + restored = adapter.to_hf(local) | ||
| 87 | + assert restored.keys() == hf_weights.keys() | ||
| 88 | + for key, value in hf_weights.items(): | ||
| 89 | + torch.testing.assert_close(restored[key], value, rtol=0, atol=0) | ||
| 90 | + | ||
| 91 | + | ||
| 70 | class TestVisionAdapterOwnership: | 92 | class TestVisionAdapterOwnership: |
| 71 | """Unit tests for DeepSeekV41VisionStateDictAdapter ownership helpers.""" | 93 | """Unit tests for DeepSeekV41VisionStateDictAdapter ownership helpers.""" |
| 72 | 94 | ||
| @@ -40,9 +40,7 @@ def _shard_engram_tables( | |||
| 40 | ignored_params.add(module.weight) | 40 | ignored_params.add(module.weight) |
| 41 | # A table FSDP does not manage still has replicas along the sparse | 41 | # A table FSDP does not manage still has replicas along the sparse |
| 42 | # data-parallel axes, and their gradients have to be summed. | 42 | # data-parallel axes, and their gradients have to be summed. |
| 43 | - wire_replicas = getattr(module, "wire_sparse_grad_replicas", None) | 43 | + module.wire_sparse_grad_replicas(edp_mesh=edp_mesh, edp_mesh_dims=edp_mesh_dims) |
| 44 | - if wire_replicas is not None: | ||
| 45 | - wire_replicas(edp_mesh=edp_mesh, edp_mesh_dims=edp_mesh_dims) | ||
| 46 | init_buffer = getattr(module, "init_elastic_buffer", None) | 44 | init_buffer = getattr(module, "init_elastic_buffer", None) |
| 47 | if init_buffer is not None: | 45 | if init_buffer is not None: |
| 48 | init_buffer(param_dtype=TORCH_DTYPE_MAP[training.mixed_precision_param]) | 46 | init_buffer(param_dtype=TORCH_DTYPE_MAP[training.mixed_precision_param]) |
| @@ -314,11 +314,3 @@ class DeepSeekV41StateDictAdapter(DeepSeekPEFTAdapter, DeepSeekV3StateDictAdapte | |||
| 314 | hf_state_dict[key] = value | 314 | hf_state_dict[key] = value |
| 315 | 315 | ||
| 316 | return hf_state_dict | 316 | return hf_state_dict |
| 317 | - | ||
| 318 | - def _map_from_hf_layer_key(self, abstract_key: str, layer_num: str) -> tuple[str, str]: | ||
| 319 | - return self.from_hf_map[abstract_key], layer_num | ||
| 320 | - | ||
| 321 | - def _map_to_hf_layer_key(self, key: str, to_hf_map: dict[str, str]) -> tuple[str, str]: | ||
| 322 | - abstract_key = re.sub(r"(\d+)", "{}", key, count=1) | ||
| 323 | - layer_num = re.search(r"\d+", key).group(0) # pyrefly: ignore [missing-attribute] | ||
| 324 | - return to_hf_map[abstract_key], layer_num | ||