Skip to content

fix(fx): FuseBiasInLinear must preserve input dtype for float16/bfloat16 - #2548

Closed
davrens101 wants to merge 1 commit into
huggingface:mainfrom
davrens101:fix/fuse-bias-in-linear-half-dtype
Closed

davrens101 wants to merge 1 commit into
huggingface:mainfrom
davrens101:fix/fuse-bias-in-linear-half-dtype

Conversation

@davrens101

Copy link
Copy Markdown

Fixes #2532

Summary

The fused constant ones tensor was created with torch.ones(shape, device=...)
using the default float32 dtype. Concatenating it with a float16/bfloat16
linear input raises RuntimeError: expected m1 and m2 to have the same dtype
(or silently corrupts the result).

Pass dtype=linear_input.dtype so the fused constant matches the input,
keeping the transformation dtype-transparent and bit-exact.

Verification

  • Reproduced on CPU: both float16 and bfloat16 raised before; bit-exact after.
  • Added test_fuse_bias_in_linear_half_precision (float16 + bfloat16,
    bit-exact vs original).
  • ruff clean.

The fused constant ones tensor was created with torch.ones(shape, device=...)
using the default float32 dtype. Concatenating it with a float16/bfloat16
linear input upcasts the input and raises
RuntimeError: expected m1 and m2 to have the same dtype (or silently
corrupts the result).

Pass dtype=linear_input.dtype so the fused constant matches the input,
keeping the transformation dtype-transparent and bit-exact.

Fixes huggingface#2532

Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com>
@davrens101

Copy link
Copy Markdown
Author

Closing this PR. gorkadabo1 opened #2533 on October 17 to fix the same issue with regression tests, and I missed it before opening this one. Apologies for the duplicate work.

@davrens101 davrens101 closed this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FuseBiasInLinear fails with float16 and bfloat16 inputs

1 participant