Skip to content

Commit 3bf0bef

Browse files
justinchubyCopilot
andcommitted
Name the producer key in rename_weight_keys collision error
Address PR review: when two source keys rename to the same target, the ValueError now identifies the first producer key (not just "another key"), which makes the collision actionable when it fires in a large checkpoint. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <11205048+justinchuby@users.noreply.github.com>
1 parent e7c9926 commit 3bf0bef

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/mobius/_weight_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,18 @@ def rename_weight_keys(
303303
(a collision that would otherwise silently drop a tensor).
304304
"""
305305
result: dict[str, torch.Tensor] = {}
306+
producers: dict[str, str] = {}
306307
for name, tensor in state_dict.items():
307308
new_name = name
308309
for old, new in replacements:
309310
new_name = new_name.replace(old, new)
310311
if new_name in result:
311312
raise ValueError(
312313
f"Weight key collision after rename: {name!r} -> {new_name!r} "
313-
f"(already produced by another key)"
314+
f"(already produced by {producers[new_name]!r})"
314315
)
315316
result[new_name] = tensor
317+
producers[new_name] = name
316318
return result
317319

318320

src/mobius/_weight_utils_test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,22 @@ def test_shares_tensor_values(self):
213213
assert result["new.k"].data_ptr() == t.data_ptr()
214214

215215
def test_collision_raises(self):
216-
"""Two source keys mapping to the same renamed key raises."""
216+
"""Two source keys mapping to the same renamed key raises.
217+
218+
The error message must name both the colliding key and the original
219+
producer key to aid debugging in large checkpoints.
220+
"""
217221
state_dict = {
218222
"a.weight": torch.tensor(1.0),
219223
"b.weight": torch.tensor(2.0),
220224
}
221-
with pytest.raises(ValueError, match="collision"):
225+
with pytest.raises(ValueError, match="collision") as exc_info:
222226
rename_weight_keys(state_dict, [("a.", "x."), ("b.", "x.")])
227+
message = str(exc_info.value)
228+
# The producer ("a.weight", processed first) and the colliding key
229+
# ("b.weight") must both appear.
230+
assert "a.weight" in message
231+
assert "b.weight" in message
223232

224233
def test_empty_state_dict(self):
225234
"""Empty input returns empty output."""

0 commit comments

Comments
 (0)