Skip to content

Commit d91f231

Browse files
shawnwang18tensorflower-gardener
authored andcommitted
PR #41808: [XLA:GPU] Migrate CollectivePermute command buffer support to thunk
Imported from GitHub PR openxla/xla#41808 PR description: 📝 Summary of Changes Migrates CollectivePermute command-buffer support from `CollectivePermuteCmd` to `CollectivePermuteThunk` by appending the thunk directly as a command. Removes the old `CollectivePermuteCmd` implementation and command type. Adds command-buffer create/update coverage and real NCCL multi-GPU execution tests for `CollectivePermuteThunk`. 🎯 Justification This continues the migration of GPU collective command-buffer handling into thunks, reducing duplicated command wrapper logic and aligning CollectivePermute with the thunk-owned command-buffer path. Workloads using CollectivePermute under GPU command buffers benefit from exercising the same thunk implementation for eager execution and command-buffer recording. 🚀 Kind of Contribution 🧪 Tests ♻️ Cleanup 📊 Benchmark (for Performance Improvements) N/A. This PR is not a performance improvement. 🧪 Unit Tests: Updated `//xla/backends/gpu/runtime:collective_permute_thunk_test` to cover CollectivePermute thunk command-buffer create/update behavior and source-target remapping logic. 🧪 Execution Tests: Added `//xla/backends/gpu/runtime:collective_permute_thunk_multigpu_test`, covering real NCCL CollectivePermute execution on 2 GPUs through: - `ExecuteOnStream` - command-buffer create mode with warm-up - command-buffer update mode Verified with: `bazel test //xla/backends/gpu/runtime:collective_permute_thunk_test //xla/backends/gpu/runtime:collective_permute_thunk_multigpu_test` Copybara import of the project: -- 7e5aa8f1d0ba7764836dbf8aaf6a811e9a8baaf5 by Shawn Wang <[email protected]>: [xla:gpu] Migrate CollectivePermuteCmd to thunk command -- 2a81fcd0df75baaf6b855e4700a1afb4bbb9799d by Shawn Wang <[email protected]>: [xla:gpu] Add CollectivePermuteThunk multigpu tests -- 3091a945c2cdc7001f6fd082c03643d3c41d072e by Shawn Wang <[email protected]>: Fix clang-format for collective permute test -- 2d16ad745025e77b116716c6129b8bfc84dccfb4 by Shawn Wang <[email protected]>: Refactor CollectivePermuteThunk multi-GPU test -- e49a9476cf4eee52eafc0f444b95c2cf2f7ceac5 by Shawn Wang <[email protected]>: Fix clang-format -- 97e7f54d3d75a572d802e2e6cefd82f6264dc6c1 by Shawn Wang <[email protected]>: Fix clang-tidy issues in collective-permute tests -- 51a14bd549ab8282e78d11881615f2b83af6c989 by Shawn Wang <[email protected]>: Fix ASSERT_OK include-cleaner issue in multigpu test -- 546b99468761decd0d4c1408d9cc15690fa617ed by Shawn Wang <[email protected]>: Fix collective permute multigpu lambda return types Merging this change closes #41808 PiperOrigin-RevId: 917613648
1 parent 7963804 commit d91f231

8 files changed

Lines changed: 553 additions & 138 deletions

File tree

‎third_party/xla/xla/backends/gpu/runtime/BUILD‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,10 @@ cc_library(
221221
hdrs = ["command_buffer_cmd.h"],
222222
deps = [
223223
":collective_execution",
224-
":collective_permute_thunk",
225224
":collective_thunk",
226225
":command",
227226
":command_executor",
228227
":command_state",
229-
":p2p_thunk_common",
230228
":thunk",
231229
":while_loop",
232230
"//xla:executable_run_options",
@@ -236,8 +234,6 @@ cc_library(
236234
"//xla:util",
237235
"//xla:xla_data_proto_cc",
238236
"//xla/backends/gpu/collectives:gpu_clique_key",
239-
"//xla/core/collectives:communicator",
240-
"//xla/core/collectives:rank_id",
241237
"//xla/runtime:buffer_use",
242238
"//xla/runtime:device_id",
243239
"//xla/service:buffer_assignment",
@@ -2691,9 +2687,12 @@ xla_test(
26912687
deps = [
26922688
":async_thunk",
26932689
":collective_permute_thunk",
2690+
":collective_thunk",
2691+
":command",
26942692
":command_buffer_cmd_emitter",
26952693
":command_buffer_thunk",
26962694
":command_executor",
2695+
":command_state",
26972696
":execution_stream_id",
26982697
":p2p_thunk_common",
26992698
":thunk",
@@ -2702,6 +2701,7 @@ xla_test(
27022701
"//xla:shape_util",
27032702
"//xla:util",
27042703
"//xla:xla_data_proto_cc",
2704+
"//xla:xla_proto_cc",
27052705
"//xla/backends/gpu/collectives:gpu_clique_key",
27062706
"//xla/core/collectives:rank_id",
27072707
"//xla/hlo/ir:collective_op_group_mode",
@@ -2713,23 +2713,66 @@ xla_test(
27132713
"//xla/service:computation_placer",
27142714
"//xla/service:executable",
27152715
"//xla/service:hlo_module_config",
2716+
"//xla/service:platform_util",
2717+
"//xla/service:service_executable_run_options",
2718+
"//xla/service:shaped_slice",
2719+
"//xla/service/gpu:buffer_allocations",
27162720
"//xla/service/gpu:gpu_constants",
27172721
"//xla/service/gpu:gpu_executable",
2722+
"//xla/stream_executor:command_buffer",
2723+
"//xla/stream_executor:device_address",
27182724
"//xla/stream_executor:platform",
2725+
"//xla/stream_executor:platform_manager",
2726+
"//xla/stream_executor:semantic_version",
27192727
"//xla/stream_executor:stream",
2728+
"//xla/stream_executor:stream_executor_address_allocator",
27202729
"//xla/stream_executor:stream_executor_h",
2730+
"//xla/stream_executor:trace_command_buffer_factory",
27212731
"//xla/tests/restricted:hlo_test_base_legacy",
2722-
"//xla/tsl/platform:statusor",
2732+
"//xla/tsl/platform:status_macros",
27232733
"//xla/tsl/platform:test",
27242734
"//xla/tsl/util/proto:parse_text_proto",
27252735
"//xla/tsl/util/proto:proto_matchers",
27262736
"@com_google_absl//absl/algorithm:container",
27272737
"@com_google_absl//absl/container:flat_hash_map",
2738+
"@com_google_absl//absl/status",
2739+
"@com_google_absl//absl/status:statusor",
2740+
"@com_google_absl//absl/strings",
27282741
"@com_google_googletest//:gtest_main",
27292742
"@tsl//tsl/platform:casts",
27302743
],
27312744
)
27322745

2746+
xla_test(
2747+
name = "collective_permute_thunk_multigpu_test",
2748+
srcs = ["collective_permute_thunk_multigpu_test.cc"],
2749+
backend_tags = {
2750+
"gpu": ["multi_gpu"],
2751+
},
2752+
backends = ["gpu"],
2753+
deps = [
2754+
":collective_permute_thunk",
2755+
":collective_thunk",
2756+
":collective_thunk_multigpu_test_utils",
2757+
":p2p_thunk_common",
2758+
":thunk",
2759+
"//xla:shape_util",
2760+
"//xla:xla_data_proto_cc",
2761+
"//xla:xla_proto_cc",
2762+
"//xla/service:buffer_assignment",
2763+
"//xla/service:computation_placer_hdr",
2764+
"//xla/service:shaped_slice",
2765+
"//xla/service/gpu:buffer_allocations",
2766+
"//xla/stream_executor:device_address",
2767+
"//xla/stream_executor:stream",
2768+
"//xla/tsl/platform:status_macros",
2769+
"@com_google_absl//absl/status",
2770+
"@com_google_absl//absl/strings:str_format",
2771+
"@com_google_absl//absl/types:span",
2772+
"@com_google_googletest//:gtest_main",
2773+
],
2774+
)
2775+
27332776
cc_library(
27342777
name = "collective_clique_requests",
27352778
srcs = ["collective_clique_requests.cc"],
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
/* Copyright 2026 The OpenXLA Authors.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
// Multi-GPU integration tests for CollectivePermuteThunk through NCCL. Requires
17+
// at least kNumDevices GPUs. Command-buffer tests additionally require CUDA
18+
// 12.9+ driver/toolkit for CreateChildCommand / UpdateChildCommand support.
19+
20+
#include <cstdint>
21+
#include <vector>
22+
23+
#include <gmock/gmock.h>
24+
#include <gtest/gtest.h>
25+
#include "absl/status/status.h"
26+
#include "absl/strings/str_format.h"
27+
#include "absl/types/span.h"
28+
#include "xla/tsl/platform/status_macros.h"
29+
#include "xla/backends/gpu/runtime/collective_permute_thunk.h"
30+
#include "xla/backends/gpu/runtime/collective_thunk.h"
31+
#include "xla/backends/gpu/runtime/collective_thunk_multigpu_test_utils.h"
32+
#include "xla/backends/gpu/runtime/p2p_thunk_common.h"
33+
#include "xla/backends/gpu/runtime/thunk.h"
34+
#include "xla/service/buffer_assignment.h"
35+
#include "xla/service/computation_placer.h"
36+
#include "xla/service/gpu/buffer_allocations.h"
37+
#include "xla/service/shaped_slice.h"
38+
#include "xla/shape_util.h"
39+
#include "xla/stream_executor/device_address.h"
40+
#include "xla/stream_executor/stream.h"
41+
#include "xla/xla.pb.h"
42+
#include "xla/xla_data.pb.h"
43+
44+
namespace xla::gpu {
45+
namespace {
46+
47+
static constexpr int kNumDevices = 2;
48+
static constexpr int64_t kLength = 4;
49+
static constexpr int64_t kByteLength = sizeof(float) * kLength;
50+
51+
static P2PConfig MakeCollectivePermuteConfig() {
52+
P2PConfig config;
53+
config.config.operand_element_type = {F32};
54+
config.config.group_mode = COLLECTIVE_OP_GROUP_MODE_CROSS_REPLICA;
55+
56+
ReplicaGroup replica_group;
57+
for (int i = 0; i < kNumDevices; ++i) {
58+
replica_group.add_replica_ids(i);
59+
}
60+
config.config.replica_groups = {replica_group};
61+
62+
// Exchange values between the two ranks: 0 -> 1 and 1 -> 0.
63+
config.id_to_source_target[0].source = 1;
64+
config.id_to_source_target[0].target = 1;
65+
config.id_to_source_target[1].source = 0;
66+
config.id_to_source_target[1].target = 0;
67+
return config;
68+
}
69+
70+
static CollectivePermuteThunk MakeThunk(const BufferAllocation& alloc_src,
71+
const BufferAllocation& alloc_dst) {
72+
ShapedSlice src_slice{BufferAllocation::Slice(&alloc_src, 0, kByteLength),
73+
ShapeUtil::MakeShape(F32, {kLength})};
74+
ShapedSlice dst_slice{BufferAllocation::Slice(&alloc_dst, 0, kByteLength),
75+
ShapeUtil::MakeShape(F32, {kLength})};
76+
CollectiveThunk::Buffer buffer{.element_count = kLength,
77+
.source_buffer = src_slice,
78+
.destination_buffer = dst_slice,
79+
.source_memory_space = 0,
80+
.destination_memory_space = 0};
81+
return CollectivePermuteThunk(
82+
Thunk::ThunkInfo(), MakeCollectivePermuteConfig(), {buffer},
83+
/*collectives_mode=*/DebugOptions::COLLECTIVES_PRIVATE_MEMORY,
84+
/*connected_components_enabled=*/false);
85+
}
86+
87+
using DeviceTestSlot = CollectiveThunkMultiGpuTestState;
88+
89+
static std::vector<int64_t> DeviceBufferSizes() {
90+
return {kByteLength, kByteLength};
91+
}
92+
93+
static std::vector<float> SourceValues(int source_rank, int phase) {
94+
std::vector<float> values(kLength);
95+
for (int i = 0; i < kLength; ++i) {
96+
values[i] = static_cast<float>(phase * 100 + source_rank * 10 + i);
97+
}
98+
return values;
99+
}
100+
101+
static absl::Status FillSourceBuffer(se::Stream& stream,
102+
se::DeviceAddressBase buffer,
103+
int device_ordinal, int phase) {
104+
return FillDeviceBuffer(stream, buffer, SourceValues(device_ordinal, phase));
105+
}
106+
107+
static absl::Status FillDestinationBuffer(se::Stream& stream,
108+
se::DeviceAddressBase buffer,
109+
float value) {
110+
return FillDeviceBuffer(stream, buffer, std::vector<float>(kLength, value));
111+
}
112+
113+
static absl::Status PrepareInputs(
114+
se::Stream& stream, absl::Span<const se::DeviceAddressBase> buffers,
115+
int device_ordinal, int phase) {
116+
RETURN_IF_ERROR(FillSourceBuffer(stream, buffers[0], device_ordinal, phase));
117+
return FillDestinationBuffer(stream, buffers[1], -1.0f);
118+
}
119+
120+
static absl::Status VerifyOutput(se::Stream& stream, se::DeviceAddressBase dst,
121+
int device_ordinal, int phase) {
122+
ASSIGN_OR_RETURN(std::vector<float> output,
123+
ReadDeviceBuffer(stream, dst, kLength));
124+
int expected_source_rank = 1 - device_ordinal;
125+
std::vector<float> expected = SourceValues(expected_source_rank, phase);
126+
for (int i = 0; i < kLength; ++i) {
127+
if (output[i] != expected[i]) {
128+
return absl::InternalError(
129+
absl::StrFormat("device %d output[%d] = %g, expected %g",
130+
device_ordinal, i, output[i], expected[i]));
131+
}
132+
}
133+
return absl::OkStatus();
134+
}
135+
136+
static absl::Status SetupDeviceSlot(int device_ordinal, DeviceTestSlot& slot,
137+
CollectivePermuteThunk& thunk,
138+
const DeviceAssignment& device_assignment) {
139+
std::vector<int64_t> buffer_sizes = DeviceBufferSizes();
140+
return SetupCollectiveThunkDevice(device_ordinal, kNumDevices, buffer_sizes,
141+
thunk, device_assignment, slot);
142+
}
143+
144+
static absl::Status RunExecuteOnStreamPhase(DeviceTestSlot& slot,
145+
CollectivePermuteThunk& thunk,
146+
int device_ordinal, int phase) {
147+
RETURN_IF_ERROR(
148+
PrepareInputs(*slot.stream, slot.create_buffers, device_ordinal, phase));
149+
150+
BufferAllocations allocations =
151+
MakeBufferAllocations(slot, slot.create_buffers);
152+
Thunk::ExecuteParams execute_params = MakeExecuteParams(slot, allocations);
153+
154+
RETURN_IF_ERROR(ExecuteOnStreamAndBlock(thunk, execute_params));
155+
return VerifyOutput(*slot.stream, slot.create_buffers[1], device_ordinal,
156+
phase);
157+
}
158+
159+
static absl::Status RunCreatePhase(DeviceTestSlot& slot,
160+
CollectivePermuteThunk& thunk,
161+
int device_ordinal, int phase) {
162+
RETURN_IF_ERROR(
163+
PrepareInputs(*slot.stream, slot.create_buffers, device_ordinal, phase));
164+
165+
BufferAllocations allocations =
166+
MakeBufferAllocations(slot, slot.create_buffers);
167+
Thunk::ExecuteParams execute_params = MakeExecuteParams(slot, allocations);
168+
169+
// Warm up NCCL outside stream capture. Reset destination buffers afterward so
170+
// correctness is verified from command-buffer execution, not from warm-up.
171+
RETURN_IF_ERROR(ExecuteOnStreamAndBlock(thunk, execute_params));
172+
RETURN_IF_ERROR(
173+
FillDestinationBuffer(*slot.stream, slot.create_buffers[1], -1.0f));
174+
175+
RETURN_IF_ERROR(RecordCommandBufferCreate(slot, thunk, execute_params));
176+
RETURN_IF_ERROR(SubmitCommandBuffer(slot));
177+
return VerifyOutput(*slot.stream, slot.create_buffers[1], device_ordinal,
178+
phase);
179+
}
180+
181+
static absl::Status RunUpdatePhase(DeviceTestSlot& slot,
182+
CollectivePermuteThunk& thunk,
183+
int device_ordinal, int phase) {
184+
RETURN_IF_ERROR(
185+
PrepareInputs(*slot.stream, slot.update_buffers, device_ordinal, phase));
186+
187+
BufferAllocations allocations =
188+
MakeBufferAllocations(slot, slot.update_buffers);
189+
Thunk::ExecuteParams execute_params = MakeExecuteParams(slot, allocations);
190+
191+
RETURN_IF_ERROR(
192+
RecordCommandBufferUpdate(slot, thunk, execute_params, {0, 1}));
193+
RETURN_IF_ERROR(SubmitCommandBuffer(slot));
194+
return VerifyOutput(*slot.stream, slot.update_buffers[1], device_ordinal,
195+
phase);
196+
}
197+
198+
TEST(CollectivePermuteThunkMultiGpuTest, ExecuteOnStream) {
199+
if (!HasEnoughGpus(kNumDevices)) {
200+
GTEST_SKIP() << "Test requires at least " << kNumDevices << " GPUs";
201+
}
202+
203+
DeviceAssignment device_assignment = MakeDeviceAssignment(kNumDevices);
204+
BufferAllocation alloc_src(/*index=*/0, kByteLength, /*color=*/0);
205+
BufferAllocation alloc_dst(/*index=*/1, kByteLength, /*color=*/0);
206+
CollectivePermuteThunk thunk = MakeThunk(alloc_src, alloc_dst);
207+
std::vector<DeviceTestSlot> slots(kNumDevices);
208+
209+
ASSERT_OK(RunOnDevices(
210+
kNumDevices, "collective_permute_execute", [&](int d) -> absl::Status {
211+
RETURN_IF_ERROR(SetupDeviceSlot(d, slots[d], thunk, device_assignment));
212+
return RunExecuteOnStreamPhase(slots[d], thunk, d, /*phase=*/1);
213+
}));
214+
}
215+
216+
TEST(CollectivePermuteThunkMultiGpuTest, RecordCommandBufferCreate) {
217+
if (!HasEnoughGpus(kNumDevices)) {
218+
GTEST_SKIP() << "Test requires at least " << kNumDevices << " GPUs";
219+
}
220+
if (!IsAtLeastCuda12900(GetGpuExecutor(0))) {
221+
GTEST_SKIP() << "Child command nodes require CUDA 12.9+";
222+
}
223+
224+
DeviceAssignment device_assignment = MakeDeviceAssignment(kNumDevices);
225+
BufferAllocation alloc_src(/*index=*/0, kByteLength, /*color=*/0);
226+
BufferAllocation alloc_dst(/*index=*/1, kByteLength, /*color=*/0);
227+
CollectivePermuteThunk thunk = MakeThunk(alloc_src, alloc_dst);
228+
std::vector<DeviceTestSlot> slots(kNumDevices);
229+
230+
ASSERT_OK(RunOnDevices(
231+
kNumDevices, "collective_permute_create", [&](int d) -> absl::Status {
232+
RETURN_IF_ERROR(SetupDeviceSlot(d, slots[d], thunk, device_assignment));
233+
return RunCreatePhase(slots[d], thunk, d, /*phase=*/2);
234+
}));
235+
}
236+
237+
TEST(CollectivePermuteThunkMultiGpuTest, RecordCommandBufferUpdate) {
238+
if (!HasEnoughGpus(kNumDevices)) {
239+
GTEST_SKIP() << "Test requires at least " << kNumDevices << " GPUs";
240+
}
241+
if (!IsAtLeastCuda12900(GetGpuExecutor(0))) {
242+
GTEST_SKIP() << "Child command nodes require CUDA 12.9+";
243+
}
244+
245+
DeviceAssignment device_assignment = MakeDeviceAssignment(kNumDevices);
246+
BufferAllocation alloc_src(/*index=*/0, kByteLength, /*color=*/0);
247+
BufferAllocation alloc_dst(/*index=*/1, kByteLength, /*color=*/0);
248+
CollectivePermuteThunk thunk = MakeThunk(alloc_src, alloc_dst);
249+
std::vector<DeviceTestSlot> slots(kNumDevices);
250+
251+
ASSERT_OK(RunOnDevices(
252+
kNumDevices, "collective_permute_create", [&](int d) -> absl::Status {
253+
RETURN_IF_ERROR(SetupDeviceSlot(d, slots[d], thunk, device_assignment));
254+
return RunCreatePhase(slots[d], thunk, d, /*phase=*/2);
255+
}));
256+
257+
ASSERT_OK(RunOnDevices(kNumDevices, "collective_permute_update", [&](int d) {
258+
return RunUpdatePhase(slots[d], thunk, d, /*phase=*/3);
259+
}));
260+
}
261+
262+
} // namespace
263+
} // namespace xla::gpu

0 commit comments

Comments
 (0)