Repository navigation
fbgemm-xpu: add all_to_one_device for XPU #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c61bdaa
fbgemm-xpu: add all_to_one_device for XPU
mkrze b2499f8
fbgemm-xpu: test all_to_one_device with inputs spread over devices
mkrze e467fe3
fbgemm-xpu: mark asserts in the all_to_one_device test for bandit
mkrze 31831e0
fbgemm-xpu: accept any torch 2.14.x in the int-nbit fixture
mkrze 0506b9a
fbgemm-xpu: copy synchronously in all_to_one_device without peer access
mkrze a0cf8d5
fbgemm-xpu: return contiguous copies from all_to_one_device
mkrze 44e4fa9
fbgemm-xpu: drop the lifetime claim from the cross-device stream test
mkrze 993e8bb
fbgemm-xpu: test all_to_one_device with FBGEMM's upstream test
mkrze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. | ||
| * Copyright (c) 2026 Intel Corporation. All Rights Reserved. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| #include <ATen/core/Tensor.h> | ||
| #include <ATen/xpu/PeerToPeerAccess.h> | ||
| #include <c10/core/DeviceGuard.h> | ||
| #include <torch/library.h> | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "fbgemm_utils/tensor_utils.h" | ||
|
|
||
| namespace fbgemm_xpu { | ||
| namespace { | ||
|
|
||
| // XPU counterpart of fbgemm_gpu::all_to_one_device (see fbgemm_gpu/src/ | ||
| // merge_pooled_embedding_ops/merge_pooled_embedding_ops_gpu.cpp). | ||
| // It keeps the CUDA contract: the target needs a device index, tensors already | ||
| // on the target are returned as they are, and copies are contiguous. CUDA | ||
| // copies the rest through its own peer-to-peer path and requires peer access | ||
| // between all devices; here Tensor.to lets PyTorch's XPU cross-device copy | ||
| // order the source and target streams, and devices without peer access fall | ||
| // back to a synchronous copy. | ||
| std::vector<at::Tensor> all_to_one_device_xpu( | ||
| std::vector<at::Tensor> input_tensors, | ||
| at::Device target_device) { | ||
| TORCH_CHECK( | ||
| target_device.is_xpu(), "all_to_one_device: target_device must be XPU"); | ||
| TORCH_CHECK( | ||
| target_device.has_index(), | ||
| "target_device.index() is -1. Please pass target_device with device " | ||
| "index, e.g., torch.device(\"xpu:0\")"); | ||
| for (const auto& tensor : input_tensors) { | ||
| TENSOR_ON_SYCL_XPU(tensor); | ||
| } | ||
|
|
||
| const c10::DeviceGuard guard(target_device); | ||
| std::vector<at::Tensor> output_tensors; | ||
| output_tensors.reserve(input_tensors.size()); | ||
| for (const auto& tensor : input_tensors) { | ||
| if (tensor.device() == target_device) { | ||
| output_tensors.push_back(tensor); | ||
| continue; | ||
| } | ||
| // Without peer access PyTorch stages the copy through pageable host memory | ||
| // and does not wait for the device-to-host half when non_blocking is set. | ||
| const bool non_blocking = at::xpu::get_p2p_access( | ||
| tensor.device().index(), target_device.index()); | ||
| output_tensors.push_back(tensor.to( | ||
| target_device, | ||
| tensor.scalar_type(), | ||
| non_blocking, | ||
| /*copy=*/false, | ||
| at::MemoryFormat::Contiguous)); | ||
| } | ||
| return output_tensors; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TORCH_LIBRARY_IMPL(fbgemm, XPU, m) { | ||
| m.impl("all_to_one_device", TORCH_FN(all_to_one_device_xpu)); | ||
| } | ||
|
|
||
| } // namespace fbgemm_xpu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Copyright (c) 2026 Intel Corporation. All Rights Reserved. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| """``all_to_one_device`` on XPU. | ||
|
|
||
| TorchRec's ``SeqEmbeddingsAllToOne`` gathers per-rank sequence embedding | ||
| outputs onto one device with this operator. FBGEMM's patched | ||
| ``test_all_to_one_device`` checks the gathered values for inputs spread over | ||
| devices. These tests cover what it does not check: tensors already on the | ||
| target come back as the same views, copies are contiguous, invalid devices are | ||
| rejected, and copies work on non-default streams. The cross-device tests need | ||
| two XPU devices. | ||
| """ | ||
|
|
||
| import fbgemm_xpu # noqa: F401 - registers the fbgemm XPU operators | ||
| import pytest | ||
| import torch | ||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| not torch.xpu.is_available(), reason="requires an XPU device" | ||
| ) | ||
|
|
||
| requires_two_xpus = pytest.mark.skipif( | ||
| torch.xpu.is_available() and torch.xpu.device_count() < 2, | ||
| reason="requires two XPU devices", | ||
| ) | ||
|
|
||
|
|
||
| def test_same_device_preserves_views_and_empty_tensors(): | ||
| xpu = torch.device("xpu:0") | ||
| torch.xpu.set_device(xpu) | ||
| base = torch.arange(24, device=xpu).reshape(4, 6) | ||
| tensors = [ | ||
| base, | ||
| base[:, ::2], | ||
| torch.empty((0, 5), device=xpu), | ||
| torch.ones((), device=xpu), | ||
| ] | ||
| outputs = torch.ops.fbgemm.all_to_one_device(tensors, xpu) | ||
| assert len(outputs) == len(tensors) # nosec B101 | ||
| for actual, expected in zip(outputs, tensors): | ||
| torch.testing.assert_close(actual, expected) | ||
| assert actual.data_ptr() == expected.data_ptr() # nosec B101 | ||
| assert actual.stride() == expected.stride() # nosec B101 | ||
|
|
||
|
|
||
| def test_rejects_unsupported_device_inputs(): | ||
| xpu = torch.device("xpu:0") | ||
| tensor = torch.ones(2, device=xpu) | ||
| with pytest.raises(RuntimeError, match="target_device must be XPU"): | ||
| torch.ops.fbgemm.all_to_one_device([tensor], torch.device("cpu")) | ||
| with pytest.raises(RuntimeError, match="Please pass target_device with device index"): | ||
| torch.ops.fbgemm.all_to_one_device([tensor], torch.device("xpu")) | ||
| with pytest.raises(RuntimeError, match="must be a SYCL XPU tensor"): | ||
| torch.ops.fbgemm.all_to_one_device([tensor, torch.ones(2)], xpu) | ||
|
|
||
|
|
||
| @requires_two_xpus | ||
| def test_mixed_devices_keep_target_views_and_copy_contiguous(): | ||
| """In one call, a view already on the target comes back as the same view, | ||
| and copies are contiguous whatever the input layout, as on CUDA.""" | ||
| source = torch.device("xpu:1") | ||
| target = torch.device("xpu:0") | ||
| on_target = torch.randn((10, 64), device=target)[:, :20] | ||
| copied = [ | ||
| torch.arange(24, device=source).reshape(4, 6).t(), | ||
| torch.randn((2, 3, 4, 5), device=source).to(memory_format=torch.channels_last), | ||
| torch.randn((10, 64), device=source)[:, :20], | ||
| ] | ||
| outputs = torch.ops.fbgemm.all_to_one_device([on_target, *copied], target) | ||
| assert outputs[0].data_ptr() == on_target.data_ptr() # nosec B101 | ||
| assert outputs[0].stride() == on_target.stride() # nosec B101 | ||
| for source_tensor, actual in zip(copied, outputs[1:]): | ||
| assert actual.device == target # nosec B101 | ||
| assert actual.is_contiguous() # nosec B101 | ||
| torch.testing.assert_close(actual.cpu(), source_tensor.cpu()) | ||
|
|
||
|
|
||
| @requires_two_xpus | ||
| def test_cross_device_stream_copy(): | ||
| """Copies in both directions on non-default streams.""" | ||
| for source_index, target_index in ((0, 1), (1, 0)): | ||
| source = torch.device(f"xpu:{source_index}") | ||
| target = torch.device(f"xpu:{target_index}") | ||
| producer = torch.xpu.Stream(device=source) | ||
| consumer = torch.xpu.Stream(device=target) | ||
| with torch.xpu.stream(producer): | ||
| inputs = [ | ||
| torch.arange(48, device=source).reshape(6, 8)[:, ::2], | ||
| torch.full((3, 7), 2.5, dtype=torch.float16, device=source), | ||
| torch.empty((0, 4), device=source), | ||
| ] | ||
| torch.xpu.current_stream(source).wait_stream(producer) | ||
| with torch.xpu.stream(consumer): | ||
| outputs = torch.ops.fbgemm.all_to_one_device(inputs, target) | ||
| consumed = [tensor.clone() for tensor in outputs] | ||
| consumer.synchronize() | ||
| expected = [ | ||
| torch.arange(48).reshape(6, 8)[:, ::2], | ||
| torch.full((3, 7), 2.5, dtype=torch.float16), | ||
| torch.empty((0, 4)), | ||
| ] | ||
| for actual, reference in zip(consumed, expected): | ||
| assert actual.device == target # nosec B101 | ||
| torch.testing.assert_close(actual.cpu(), reference) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any tests we can reuse from fbgemm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, FBGEMM has a test for it.
Addressed in 993e8bb with a hybrid approach: we now run the upstream test on XPU through the shared patch and dropped our copy of it, and keep a few local tests for what upstream doesn't check (no copy for tensors already on the target, contiguous copies, invalid devices, non-default streams).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. One thing I really care about is reusing fbgemm tests as much as possible and keeping our own tests to the bare minimum. This way we increase our chances to be really compatible with the upstream and minimize our debt for potential upstreaming.