Skip to content

fbgemm_fp8:Keep the current device aligned with the input tensor#46403

Open
kaixuanliu wants to merge 3 commits into
huggingface:mainfrom
kaixuanliu:fbgemm_fp8_xpu
Open

fbgemm_fp8:Keep the current device aligned with the input tensor#46403
kaixuanliu wants to merge 3 commits into
huggingface:mainfrom
kaixuanliu:fbgemm_fp8_xpu

Conversation

@kaixuanliu
Copy link
Copy Markdown
Contributor

@kaixuanliu kaixuanliu commented Jun 4, 2026

As the comment in the code: x_quantized and x_scale are not necessarily on the same device as x in L119, on xpu, although we use kernels-community/fp8-fbgemm, we still meet this problem. Even move the output can still produce incorrect output(nan output in middle layer). In this PR we use a context manager to fix the bug and optimize the code like what we did in mxfp4 .
Cases to re-produce:

RUN_SLOW=1 python -m pytest tests/quantization/fbgemm_fp8/test_fbgemm_fp8.py::FbgemmFp8Test::test_quantized_model_multi_gpu

RUN_SLOW=1 python -m pytest tests/quantization/fbgemm_fp8/test_fbgemm_fp8.py::FbgemmFp8Test::test_save_pretrained_multi_gpu

Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>
Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>
@kaixuanliu
Copy link
Copy Markdown
Contributor Author

@SunMarc ,pls help review, thx!

@kaixuanliu kaixuanliu changed the title Fbgemm fp8 xpu fbgemm_fp8:Keep the current device aligned with the input tensor Jun 4, 2026
Copy link
Copy Markdown
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just a nit

Comment on lines +45 to +62
@contextmanager
def on_device(tensor):
"""Force the global current device to match ``tensor``'s device.

This keeps quantization kernel launches aligned with the input tensor device when the
process current device differs from the module placement.
"""
device = getattr(tensor, "device", None)
device_type = getattr(device, "type", None)
if device_type == "cuda":
with torch.cuda.device(device):
yield
elif _is_torch_xpu_available and device_type == "xpu":
with torch.xpu.device(device):
yield
else:
yield

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put that in another file so that we can reuse them across quants integration ?

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.

2 participants