|
12 | 12 | merge_lora_weights, |
13 | 13 | preprocess_awq_weights, |
14 | 14 | preprocess_gptq_weights, |
| 15 | + rename_weight_keys, |
15 | 16 | split_codegen_qkv, |
16 | 17 | split_fused_qkv, |
17 | 18 | split_gate_up_proj, |
|
20 | 21 | tie_word_embeddings, |
21 | 22 | vlm_decoder_weights, |
22 | 23 | vlm_embedding_weights, |
| 24 | + vlm_vision_weights, |
23 | 25 | ) |
24 | 26 |
|
25 | 27 |
|
@@ -169,6 +171,71 @@ def test_preserves_tensor_values(self): |
169 | 171 | assert result["key"].data_ptr() == t.data_ptr() |
170 | 172 |
|
171 | 173 |
|
| 174 | +class TestRenameWeightKeys: |
| 175 | + """Tests for rename_weight_keys.""" |
| 176 | + |
| 177 | + def test_applies_replacements(self): |
| 178 | + """Each (old, new) pair is applied to every key.""" |
| 179 | + state_dict = { |
| 180 | + "model.layers.0.attention_layernorm.weight": torch.tensor(1.0), |
| 181 | + "model.layers.0.feedforward_layernorm.weight": torch.tensor(2.0), |
| 182 | + "model.embed.weight": torch.tensor(3.0), |
| 183 | + } |
| 184 | + result = rename_weight_keys( |
| 185 | + state_dict, |
| 186 | + [ |
| 187 | + (".attention_layernorm.", ".input_layernorm."), |
| 188 | + (".feedforward_layernorm.", ".post_attention_layernorm."), |
| 189 | + ], |
| 190 | + ) |
| 191 | + assert set(result.keys()) == { |
| 192 | + "model.layers.0.input_layernorm.weight", |
| 193 | + "model.layers.0.post_attention_layernorm.weight", |
| 194 | + "model.embed.weight", |
| 195 | + } |
| 196 | + |
| 197 | + def test_replacements_are_ordered_and_cascade(self): |
| 198 | + """Replacements apply to the progressively updated key, in order.""" |
| 199 | + state_dict = {"a.weight": torch.tensor(1.0)} |
| 200 | + result = rename_weight_keys(state_dict, [("a.", "b."), ("b.", "c.")]) |
| 201 | + assert list(result.keys()) == ["c.weight"] |
| 202 | + |
| 203 | + def test_no_replacements_is_identity(self): |
| 204 | + """An empty replacement list returns a key-equivalent copy.""" |
| 205 | + state_dict = {"x.weight": torch.tensor(1.0)} |
| 206 | + result = rename_weight_keys(state_dict, []) |
| 207 | + assert list(result.keys()) == ["x.weight"] |
| 208 | + assert result is not state_dict |
| 209 | + |
| 210 | + def test_shares_tensor_values(self): |
| 211 | + """Tensor values are shared, not cloned.""" |
| 212 | + t = torch.randn(2, 3) |
| 213 | + result = rename_weight_keys({"old.k": t}, [("old.", "new.")]) |
| 214 | + assert result["new.k"].data_ptr() == t.data_ptr() |
| 215 | + |
| 216 | + def test_collision_raises(self): |
| 217 | + """Two source keys mapping to the same renamed key raises. |
| 218 | +
|
| 219 | + The error message must name both the colliding key and the original |
| 220 | + producer key to aid debugging in large checkpoints. |
| 221 | + """ |
| 222 | + state_dict = { |
| 223 | + "a.weight": torch.tensor(1.0), |
| 224 | + "b.weight": torch.tensor(2.0), |
| 225 | + } |
| 226 | + with pytest.raises(ValueError, match="collision") as exc_info: |
| 227 | + rename_weight_keys(state_dict, [("a.", "x."), ("b.", "x.")]) |
| 228 | + message = str(exc_info.value) |
| 229 | + # The producer ("a.weight", processed first) and the colliding key |
| 230 | + # ("b.weight") must both appear. |
| 231 | + assert "a.weight" in message |
| 232 | + assert "b.weight" in message |
| 233 | + |
| 234 | + def test_empty_state_dict(self): |
| 235 | + """Empty input returns empty output.""" |
| 236 | + assert rename_weight_keys({}, [("a", "b")]) == {} |
| 237 | + |
| 238 | + |
172 | 239 | class TestSplitFusedQkvValidation: |
173 | 240 | """Tests for QKV dimension mismatch detection.""" |
174 | 241 |
|
@@ -330,6 +397,44 @@ def test_custom_keyword(self): |
330 | 397 | assert list(result.keys()) == ["word_embedding.weight"] |
331 | 398 |
|
332 | 399 |
|
| 400 | +class TestVlmVisionWeights: |
| 401 | + """Tests for vlm_vision_weights.""" |
| 402 | + |
| 403 | + def test_filters_and_renames(self): |
| 404 | + """Keeps prefixed keys and renames fc1/fc2.""" |
| 405 | + fc1 = torch.randn(4, 8) |
| 406 | + fc2 = torch.randn(8, 4) |
| 407 | + sd = { |
| 408 | + "vision_tower.encoder.layers.0.mlp.fc1.weight": fc1, |
| 409 | + "vision_tower.encoder.layers.0.mlp.fc2.weight": fc2, |
| 410 | + "multi_modal_projector.linear.weight": torch.randn(4), |
| 411 | + "language_model.model.layers.0.weight": torch.randn(4), |
| 412 | + } |
| 413 | + result = vlm_vision_weights(sd, ("vision_tower.", "multi_modal_projector.")) |
| 414 | + assert set(result.keys()) == { |
| 415 | + "vision_tower.encoder.layers.0.mlp.up_proj.weight", |
| 416 | + "vision_tower.encoder.layers.0.mlp.down_proj.weight", |
| 417 | + "multi_modal_projector.linear.weight", |
| 418 | + } |
| 419 | + assert result["vision_tower.encoder.layers.0.mlp.up_proj.weight"].data_ptr() == ( |
| 420 | + fc1.data_ptr() |
| 421 | + ) |
| 422 | + |
| 423 | + def test_single_prefix(self): |
| 424 | + """Works with a single-element prefix tuple.""" |
| 425 | + sd = { |
| 426 | + "vision_model.layers.0.mlp.fc1.weight": torch.randn(2), |
| 427 | + "other.weight": torch.randn(2), |
| 428 | + } |
| 429 | + result = vlm_vision_weights(sd, ("vision_model.",)) |
| 430 | + assert list(result.keys()) == ["vision_model.layers.0.mlp.up_proj.weight"] |
| 431 | + |
| 432 | + def test_empty_when_no_match(self): |
| 433 | + """Returns empty dict when no key matches the prefixes.""" |
| 434 | + sd = {"language_model.layers.0.weight": torch.randn(2)} |
| 435 | + assert vlm_vision_weights(sd, ("vision_tower.",)) == {} |
| 436 | + |
| 437 | + |
333 | 438 | class TestPreprocessGptqWeights: |
334 | 439 | """Tests for GPTQ weight preprocessing. |
335 | 440 |
|
|
0 commit comments