[Bugfix] Fix layerwise reload dropping params after a composed weight loader (#44814)

Signed-off-by: hallerite <git@hallerite.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Kyle Sayers <kylesayrs@gmail.com>
This commit is contained in:
hallerite
2026-06-10 06:42:05 -07:00
committed by GitHub
co-authored by Claude Opus 4.8 Kyle Sayers
parent 6850839c6f
commit c9e5bf8135
2 changed files with 96 additions and 1 deletions
@@ -185,4 +185,18 @@ def get_numel_loaded(
"""
with CopyCounter() as counter:
return_value = weight_loader(*args.args, **args.kwargs)
return counter.copied_numel, return_value
# A weight loader fills a single destination parameter, so the number of
# loaded elements is at most that parameter's size. Some loaders copy into
# the parameter more than once -- e.g. ``composed_weight_loader`` runs an
# in-place post-load transform (``param.copy_(fn(param))``) on top of the
# initial copy -- which would make CopyCounter report twice the parameter
# size. Over-counting inflates the layer's loaded-element total and can
# finalize the layer before every parameter is loaded, silently dropping
# the trailing parameter(s) (e.g. Mamba ``mixer.D``). Cap the count at the
# destination size to keep the per-layer accounting correct.
numel = counter.copied_numel
param = args.arguments.get("param", None)
if isinstance(param, torch.Tensor):
numel = min(numel, param.numel())
return numel, return_value