[Bugfix] FusedMoE: coerce shape-(1,) per-tensor scales to 0-D scalar … (#43362)

Signed-off-by: Varshith <kvarshithgowda@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Michael Goin <mgoin64@gmail.com>
This commit is contained in:
Varshith
2026-06-22 13:26:53 -07:00
committed by GitHub
co-authored by Claude Michael Goin
parent d1a38c2762
commit c0b2d8f471
2 changed files with 33 additions and 4 deletions
@@ -325,3 +325,24 @@ class TestWeightLoadingWithPaddedHiddenSize:
shard_id="w2",
expert_id=0,
)
class TestPerTensorScaleCoercion:
"""Regression test for shape-(1,) per-tensor scales (issue #43297).
llm-compressor NVFP4 emits per-tensor weight and input scales as
shape-(1,) tensors. `_to_scalar` collapses them to a 0-D scalar so the
scalar-slot assignments in the weight loader neither broadcast nor raise.
"""
def test_collapses_to_scalar(self):
# shape-(1,) and 0-D both reduce to a 0-D scalar.
for loaded_weight in (torch.tensor([0.5]), torch.tensor(0.5)):
scalar = RoutedExperts._to_scalar(loaded_weight)
assert scalar.shape == ()
assert scalar.item() == pytest.approx(0.5)
def test_rejects_non_scalar(self):
# numel > 1 must fail loudly instead of silently picking an element.
with pytest.raises(RuntimeError):
RoutedExperts._to_scalar(torch.tensor([0.1, 0.2]))