Unverified Commit 21313e09 authored by Michael Goin's avatar Michael Goin Committed by GitHub
Browse files

[Bugfix] Fix default weight loading for scalars (#7534)

parent f4da5f7b
...@@ -516,11 +516,17 @@ def default_weight_loader(param: torch.Tensor, ...@@ -516,11 +516,17 @@ def default_weight_loader(param: torch.Tensor,
loaded_weight: torch.Tensor) -> None: loaded_weight: torch.Tensor) -> None:
"""Default weight loader.""" """Default weight loader."""
try: try:
assert param.size() == loaded_weight.size(), ( if param.numel() == 1 and loaded_weight.numel() == 1:
f"Attempted to load weight ({loaded_weight.size()}) " # Sometimes scalar values aren't considered tensors with shapes
f"into parameter ({param.size()})") # so if both param and loaded_weight are a scalar,
# "broadcast" instead of copy
param.data.copy_(loaded_weight) param.data.fill_(loaded_weight.item())
else:
assert param.size() == loaded_weight.size(), (
f"Attempted to load weight ({loaded_weight.size()}) "
f"into parameter ({param.size()})")
param.data.copy_(loaded_weight)
except Exception: except Exception:
# NOTE: This exception is added for the purpose of setting breakpoint to # NOTE: This exception is added for the purpose of setting breakpoint to
# debug weight loading issues. # debug weight loading issues.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment