-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathsymmetric_tensor.cpp
More file actions
604 lines (505 loc) · 19.2 KB
/
symmetric_tensor.cpp
File metadata and controls
604 lines (505 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// clang-format off
/*
* SPDX-FileCopyrightText: Copyright (c) 2025-present NVIDIA CORPORATION & AFFILIATES.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
// clang-format on
#include "multidevice/symmetric_tensor.h"
#include <numeric>
#include "cuda_utils.h"
#include "driver_api.h"
#include "multidevice/communicator.h"
#include "multidevice/ipc_utils.h"
#include "multidevice/utils.h"
namespace nvfuser {
namespace {
// Returns the allocation granularity for symmetric memory.
// - query_mcast_granularity: if true, considers multicast granularity
// - query_mcast_recommended_granularity: if true, uses recommended (larger)
// multicast granularity
int64_t getGranularityForSymmetricMemory(
const CUmemAllocationProp& prop,
size_t requested_size_bytes,
bool query_mcast_granularity = true,
bool query_mcast_recommended_granularity = false) {
size_t alloc_granularity = 0;
NVFUSER_CUDA_SAFE_CALL(cuMemGetAllocationGranularity(
&alloc_granularity, &prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM));
#if (CUDA_VERSION >= NVF_MIN_CUDA_FOR_MCAST)
if (!query_mcast_granularity) {
return alloc_granularity;
}
// Check if device supports multicast before querying multicast granularity
int is_multicast_supported = 0;
NVFUSER_CUDA_SAFE_CALL(cuDeviceGetAttribute(
&is_multicast_supported,
CU_DEVICE_ATTRIBUTE_MULTICAST_SUPPORTED,
prop.location.id));
if (is_multicast_supported == 0) {
return alloc_granularity;
}
// Device supports multicast, query multicast granularity
CUmulticastObjectProp mcast_prop{};
mcast_prop.flags = 0;
mcast_prop.handleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
mcast_prop.numDevices = Communicator::getInstance().size();
// Align requested size to allocation granularity as a baseline
mcast_prop.size =
((requested_size_bytes + alloc_granularity - 1) / alloc_granularity) *
alloc_granularity;
size_t mcast_min_granularity = 0;
NVFUSER_CUDA_SAFE_CALL(cuMulticastGetGranularity(
&mcast_min_granularity, &mcast_prop, CU_MULTICAST_GRANULARITY_MINIMUM));
size_t granularity = mcast_min_granularity;
if (query_mcast_recommended_granularity) {
size_t mcast_rec_granularity = 0;
NVFUSER_CUDA_SAFE_CALL(cuMulticastGetGranularity(
&mcast_rec_granularity,
&mcast_prop,
CU_MULTICAST_GRANULARITY_RECOMMENDED));
granularity = mcast_rec_granularity;
}
return std::max(alloc_granularity, granularity);
#else
(void)requested_size_bytes;
(void)query_mcast_granularity;
(void)query_mcast_recommended_granularity;
return alloc_granularity;
#endif
}
} // namespace
at::Tensor SymmetricTensor::allocate(
at::IntArrayRef sizes,
at::ScalarType dtype,
at::Device device) {
int is_vmm_supported;
NVFUSER_CUDA_SAFE_CALL(cuDeviceGetAttribute(
&is_vmm_supported,
CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED,
device.index()));
NVF_ERROR(is_vmm_supported, "Device does not support VMM");
const int64_t numel = std::accumulate(
sizes.begin(), sizes.end(), 1, std::multiplies<int64_t>());
const int64_t element_size = c10::elementSize(dtype);
const int64_t alloc_size = numel * element_size;
CUmemAllocationProp prop{};
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
prop.location.id = static_cast<int>(device.index());
prop.requestedHandleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
size_t granularity =
getGranularityForSymmetricMemory(prop, static_cast<size_t>(alloc_size));
int64_t rounded_size =
((alloc_size + granularity - 1) / granularity) * granularity;
CUmemGenericAllocationHandle handle = 0;
NVFUSER_CUDA_SAFE_CALL(cuMemCreate(&handle, rounded_size, &prop, 0));
CUdeviceptr ptr = 0;
NVFUSER_CUDA_SAFE_CALL(
cuMemAddressReserve(&ptr, rounded_size, granularity, 0, 0));
NVFUSER_CUDA_SAFE_CALL(cuMemMap(ptr, rounded_size, 0, handle, 0));
CUmemAccessDesc access{};
access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
access.location.id = static_cast<int>(device.index());
access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
NVFUSER_CUDA_SAFE_CALL(cuMemSetAccess(ptr, rounded_size, &access, 1));
std::vector<int64_t> strides(sizes.size());
strides.back() = 1;
for (int64_t i = strides.size() - 2; i >= 0; --i) {
strides[i] = strides[i + 1] * sizes[i + 1];
}
return at::from_blob(
(void*)ptr,
sizes,
std::move(strides),
[=](void* ptr) {
cuMemUnmap((CUdeviceptr)(ptr), rounded_size);
cuMemAddressFree((CUdeviceptr)(ptr), rounded_size);
cuMemRelease(handle);
},
at::TensorOptions().dtype(dtype).device(device));
}
std::string SymmetricTensor::validate(at::Tensor tensor) {
int is_vmm_supported;
NVFUSER_CUDA_SAFE_CALL(cuDeviceGetAttribute(
&is_vmm_supported,
CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED,
tensor.device().index()));
if (!is_vmm_supported) {
return "Device does not support VMM";
}
auto ptr = (CUdeviceptr)tensor.data_ptr();
CUmemLocation location{};
location.type = CU_MEM_LOCATION_TYPE_DEVICE;
location.id = Communicator::getInstance().local_rank();
unsigned long long flags = 0;
NVFUSER_CUDA_SAFE_CALL(cuMemGetAccess(&flags, &location, ptr));
if (flags != CU_MEM_ACCESS_FLAGS_PROT_READWRITE) {
return "Invalid access flags: " + std::to_string(flags);
}
CUmemGenericAllocationHandle alloc_handle = 0;
NVFUSER_CUDA_SAFE_CALL(
cuMemRetainAllocationHandle(&alloc_handle, (void*)ptr));
CUmemAllocationProp prop{};
NVFUSER_CUDA_SAFE_CALL(
cuMemGetAllocationPropertiesFromHandle(&prop, alloc_handle));
if (prop.type != CU_MEM_ALLOCATION_TYPE_PINNED) {
return "Not pinned allocation";
}
if (prop.location.type != CU_MEM_LOCATION_TYPE_DEVICE) {
return "Not device memory";
}
if (prop.location.id != Communicator::getInstance().local_rank()) {
return "Wrong device: " + std::to_string(prop.location.id);
}
if (prop.requestedHandleTypes != CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR) {
return "Wrong handle type";
}
size_t size_bytes = tensor.numel() * tensor.element_size();
const size_t granularity = getGranularityForSymmetricMemory(prop, size_bytes);
CUdeviceptr base_ptr = 0;
size_t va_size = 0;
NVFUSER_CUDA_SAFE_CALL(cuMemGetAddressRange(&base_ptr, &va_size, ptr));
if (va_size < size_bytes) {
return "VA range smaller than tensor size";
}
if ((static_cast<size_t>(base_ptr) % granularity) != 0 ||
(va_size % granularity) != 0) {
return "Misaligned to granularity";
}
cuMemRelease(alloc_handle);
return "";
}
SymmetricTensor::SymmetricTensor(const at::Tensor& local_tensor)
: local_tensor_(local_tensor) {
NVF_ERROR(
local_tensor.is_cuda(),
"Expected CUDA tensor, got: ",
local_tensor.device());
std::string error = SymmetricTensor::validate(local_tensor);
NVF_CHECK(error.empty(), "Invalid symmetric allocation: ", error);
Communicator& comm = Communicator::getInstance();
world_size_ = comm.size();
my_device_id_ = comm.deviceId();
CUmemAllocationProp prop{};
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
prop.location.id = static_cast<int>(comm.local_rank());
prop.requestedHandleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
requested_size_ = local_tensor.numel() * local_tensor.element_size();
granularity_ = getGranularityForSymmetricMemory(prop, requested_size_);
CUdeviceptr local_ptr =
reinterpret_cast<CUdeviceptr>(local_tensor_.data_ptr());
CUdeviceptr base_ptr = 0;
size_t base_size = 0;
NVFUSER_CUDA_SAFE_CALL(
cuMemGetAddressRange(&base_ptr, &base_size, local_ptr));
size_t mem_offset = static_cast<size_t>(local_ptr - base_ptr);
size_t offset_diff = mem_offset % granularity_;
aligned_size_ =
((requested_size_ + offset_diff + granularity_ - 1) / granularity_) *
granularity_;
alloc_handles_.resize(world_size_);
remote_ptrs_.resize(world_size_);
CUmemGenericAllocationHandle local_handle;
NVFUSER_CUDA_SAFE_CALL(cuMemRetainAllocationHandle(
&local_handle, reinterpret_cast<void*>(local_ptr)));
alloc_handles_[my_device_id_] = local_handle;
remote_ptrs_[my_device_id_] = local_ptr;
}
SymmetricTensor::~SymmetricTensor() {
if (device_peer_ptrs_ != nullptr) {
cudaFree(device_peer_ptrs_);
device_peer_ptrs_ = nullptr;
}
#if (CUDA_VERSION >= 13000)
if (is_multicast_setup_) {
if (mc_base_ptr_) {
cuMemUnmap(mc_base_ptr_, aligned_size_);
cuMemAddressFree(mc_base_ptr_, aligned_size_);
}
if (mcast_handle_) {
// On some driver versions, cuMulticastUnbind is sometimes failing with
// CUDA_ERROR_INVALID_VALUE, as seen in CI (but not on my system)
// According to docs, call to cuMulticastUnbind is not required, and
// destroying the object unbinds it. Therefore, we simply skip the call
// for now. cuMulticastUnbind(mcast_handle_, cu_dev_, 0, aligned_size_);
cuMemRelease(mcast_handle_);
}
if (peer_fd_ >= 0)
close(peer_fd_);
}
#endif
if (are_remote_tensors_setup_ == true) {
CUdeviceptr local_ptr =
reinterpret_cast<CUdeviceptr>(local_tensor_.data_ptr());
CUdeviceptr base_ptr = 0;
size_t va_size = 0;
NVFUSER_CUDA_SAFE_CALL(
cuMemGetAddressRange(&base_ptr, &va_size, local_ptr));
size_t offset = local_ptr - base_ptr;
for (int64_t rank = 0; rank < world_size_; ++rank) {
if (rank != my_device_id_ && remote_ptrs_[rank]) {
cuMemUnmap(remote_ptrs_[rank] - offset, va_size);
cuMemAddressFree(remote_ptrs_[rank] - offset, va_size);
}
if (rank != my_device_id_ && alloc_handles_[rank]) {
cuMemRelease(alloc_handles_[rank]);
}
}
}
if (alloc_handles_.size() > static_cast<size_t>(my_device_id_) &&
alloc_handles_[my_device_id_]) {
cuMemRelease(alloc_handles_[my_device_id_]);
}
}
void SymmetricTensor::setupRemoteHandles(const std::string& tag) {
if (are_remote_tensors_setup_ == true) {
return;
}
Communicator& comm = Communicator::getInstance();
CUmemGenericAllocationHandle local_handle = alloc_handles_[my_device_id_];
CUdeviceptr local_ptr = remote_ptrs_[my_device_id_];
CUdeviceptr base_ptr = 0;
size_t va_size = 0;
NVFUSER_CUDA_SAFE_CALL(cuMemGetAddressRange(&base_ptr, &va_size, local_ptr));
size_t offset = local_ptr - base_ptr;
int shared_fd;
NVFUSER_CUDA_SAFE_CALL(cuMemExportToShareableHandle(
&shared_fd,
local_handle,
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR,
/*flags=*/0));
std::string my_socket_path =
"@nvfuser_sym_p2p_" + std::to_string(my_device_id_) + "_" + tag;
int listener_fd = createIpcSocket(my_socket_path);
comm.barrier();
for (int64_t peer = 0; peer < world_size_; ++peer) {
if (peer == my_device_id_) {
continue;
}
std::string peer_path =
"@nvfuser_sym_p2p_" + std::to_string(peer) + "_" + tag;
int my_rank_data = (int)my_device_id_;
sendFd(peer_path, shared_fd, &my_rank_data, sizeof(my_rank_data));
}
for (int64_t i = 0; i < world_size_ - 1; ++i) {
int sender_rank = -1;
int local_fd = recvFd(listener_fd, &sender_rank, sizeof(sender_rank));
NVF_CHECK(
sender_rank >= 0 && sender_rank < world_size_, "Invalid sender rank");
CUmemGenericAllocationHandle peer_handle;
NVFUSER_CUDA_SAFE_CALL(cuMemImportFromShareableHandle(
&peer_handle,
reinterpret_cast<void*>(static_cast<uint64_t>(local_fd)),
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR));
alloc_handles_[sender_rank] = peer_handle;
CUdeviceptr peer_ptr = 0;
NVFUSER_CUDA_SAFE_CALL(
cuMemAddressReserve(&peer_ptr, va_size, granularity_, 0, 0));
// cuMemMap does not support for now mapping a subregion of an allocation,
// so we map the full allocation but store the offseted peer pointer.
NVFUSER_CUDA_SAFE_CALL(cuMemMap(peer_ptr, va_size, 0, peer_handle, 0));
CUmemAccessDesc access{};
access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
access.location.id = static_cast<int>(comm.local_rank());
access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
NVFUSER_CUDA_SAFE_CALL(cuMemSetAccess(peer_ptr, va_size, &access, 1));
remote_ptrs_[sender_rank] = peer_ptr + offset;
close(local_fd);
}
close(listener_fd);
close(shared_fd);
comm.barrier();
are_remote_tensors_setup_ = true;
}
at::Tensor SymmetricTensor::remoteTensor(int64_t rank) const {
NVF_CHECK(rank >= 0 && rank < world_size_, "Rank out of range");
if (rank == my_device_id_) {
return local_tensor_;
}
NVF_CHECK(are_remote_tensors_setup_ == true, "Remote tensors not setup");
return at::from_blob(
reinterpret_cast<void*>(remote_ptrs_[rank]),
local_tensor_.sizes(),
local_tensor_.strides(),
at::TensorOptions()
.dtype(local_tensor_.scalar_type())
.device(at::kCUDA, rank));
}
void** SymmetricTensor::devicePeerPointers() const {
NVF_CHECK(are_remote_tensors_setup_ == true, "Remote tensors not setup");
if (device_peer_ptrs_ == nullptr) {
std::vector<void*> host_peer_ptrs(world_size_);
for (int64_t rank = 0; rank < world_size_; ++rank) {
host_peer_ptrs[rank] = reinterpret_cast<void*>(remote_ptrs_[rank]);
}
NVFUSER_CUDA_RT_SAFE_CALL(
cudaMalloc(&device_peer_ptrs_, world_size_ * sizeof(void*)));
NVFUSER_CUDA_RT_SAFE_CALL(cudaMemcpy(
device_peer_ptrs_,
host_peer_ptrs.data(),
world_size_ * sizeof(void*),
cudaMemcpyHostToDevice));
}
return device_peer_ptrs_;
}
void* SymmetricTensor::multicastPtr() const {
NVF_CHECK(is_multicast_setup_, "Multicast not setup");
return mc_ptr_;
}
void SymmetricTensor::setupContiguousView(const std::string& tag) {
if (is_contiguous_view_setup_) {
return;
}
NVF_CHECK(
are_remote_tensors_setup_ == true,
"Remote tensors must be setup before setupContiguousView");
Communicator& comm = Communicator::getInstance();
const int64_t local_rank = comm.local_rank();
const int64_t world_size = comm.size();
size_t total_size = aligned_size_ * world_size;
CUdeviceptr base;
NVFUSER_CUDA_SAFE_CALL(
cuMemAddressReserve(&base, total_size, granularity_, 0, 0));
for (int64_t rank = 0; rank < world_size; ++rank) {
CUdeviceptr region = base + (rank * aligned_size_);
NVFUSER_CUDA_SAFE_CALL(
cuMemMap(region, aligned_size_, 0, alloc_handles_[rank], 0));
CUmemAccessDesc access{};
access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
access.location.id = static_cast<int>(local_rank);
access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
NVFUSER_CUDA_SAFE_CALL(cuMemSetAccess(region, aligned_size_, &access, 1));
}
std::vector<int64_t> sizes = {world_size};
for (int64_t s : local_tensor_.sizes()) {
sizes.push_back(s);
}
std::vector<int64_t> strides;
strides.reserve(sizes.size());
NVF_CHECK(
aligned_size_ % local_tensor_.element_size() == 0,
"Aligned size must be divisible by element size");
strides.push_back(aligned_size_ / local_tensor_.element_size());
for (int64_t s : local_tensor_.strides()) {
strides.push_back(s);
}
size_t map_size = aligned_size_;
contiguous_view_ = at::from_blob(
reinterpret_cast<void*>(base),
sizes,
strides,
[=](void* ptr) {
for (int64_t rank = 0; rank < world_size; ++rank) {
cuMemUnmap(
reinterpret_cast<CUdeviceptr>(ptr) + (rank * map_size), map_size);
}
cuMemAddressFree(reinterpret_cast<CUdeviceptr>(ptr), total_size);
},
at::TensorOptions()
.dtype(local_tensor_.scalar_type())
.device(at::kCUDA, 0));
// Remove the old trivial DIDx dimension
contiguous_view_ = contiguous_view_.squeeze(1);
is_contiguous_view_setup_ = true;
}
at::Tensor SymmetricTensor::getContiguousView() const {
NVF_CHECK(is_contiguous_view_setup_, "Contiguous view not setup");
return contiguous_view_;
}
void SymmetricTensor::setupMulticast(
int64_t exporter_rank,
const std::string& tag) {
#if (CUDA_VERSION >= 13000)
if (is_multicast_setup_) {
return;
}
Communicator& comm = Communicator::getInstance();
const int64_t my_rank = comm.deviceId();
const int64_t local_rank = comm.local_rank();
int is_multicast_supported;
NVFUSER_CUDA_SAFE_CALL(cuDeviceGetAttribute(
&is_multicast_supported,
CU_DEVICE_ATTRIBUTE_MULTICAST_SUPPORTED,
local_rank));
NVF_CHECK(is_multicast_supported, "Multicast not supported");
exporter_rank_ = exporter_rank;
CUmulticastObjectProp mcast_prop{};
mcast_prop.handleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
mcast_prop.numDevices = world_size_;
mcast_prop.size = aligned_size_;
int shared_handle_fd = -1;
int listener_fd = -1;
if (my_rank == exporter_rank) {
NVFUSER_CUDA_SAFE_CALL(cuMulticastCreate(&mcast_handle_, &mcast_prop));
NVFUSER_CUDA_SAFE_CALL(cuMemExportToShareableHandle(
&shared_handle_fd,
mcast_handle_,
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR,
0));
} else {
std::string my_path =
"@nvfuser_sym_mcast_" + std::to_string(my_rank) + "_" + tag;
listener_fd = createIpcSocket(my_path);
}
comm.barrier();
if (my_rank != exporter_rank) {
peer_fd_ = recvFd(listener_fd);
close(listener_fd);
NVFUSER_CUDA_SAFE_CALL(cuMemImportFromShareableHandle(
&mcast_handle_,
reinterpret_cast<void*>(static_cast<uint64_t>(peer_fd_)),
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR));
} else {
for (int i = 0; i < world_size_; ++i) {
if (i == my_rank) {
continue;
}
std::string peer_path =
"@nvfuser_sym_mcast_" + std::to_string(i) + "_" + tag;
sendFd(peer_path, shared_handle_fd);
}
close(shared_handle_fd);
}
NVFUSER_CUDA_SAFE_CALL(cuDeviceGet(&cu_dev_, static_cast<int>(local_rank)));
NVFUSER_CUDA_SAFE_CALL(cuMulticastAddDevice(mcast_handle_, cu_dev_));
CUdeviceptr local_ptr = remote_ptrs_[my_device_id_];
CUdeviceptr base_ptr;
size_t base_size;
NVFUSER_CUDA_SAFE_CALL(
cuMemGetAddressRange(&base_ptr, &base_size, local_ptr));
size_t mem_offset = static_cast<size_t>(local_ptr - base_ptr);
size_t aligned_mem_offset = (mem_offset / granularity_) * granularity_;
size_t offset_diff = mem_offset - aligned_mem_offset;
NVF_CHECK(
aligned_mem_offset + aligned_size_ <= base_size,
"SymmetricTensor: Physical allocation too small for aligned multicast "
"binding");
NVFUSER_CUDA_SAFE_CALL(cuMulticastBindMem(
mcast_handle_,
0,
alloc_handles_[my_device_id_],
aligned_mem_offset,
aligned_size_,
0));
CUdeviceptr mc_ptr;
NVFUSER_CUDA_SAFE_CALL(
cuMemAddressReserve(&mc_ptr, aligned_size_, granularity_, 0, 0));
NVFUSER_CUDA_SAFE_CALL(cuMemMap(mc_ptr, aligned_size_, 0, mcast_handle_, 0));
CUmemAccessDesc access{};
access.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
access.location.id = static_cast<int>(local_rank);
access.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
NVFUSER_CUDA_SAFE_CALL(cuMemSetAccess(mc_ptr, aligned_size_, &access, 1));
mc_ptr_ = reinterpret_cast<void*>(mc_ptr + offset_diff);
mc_base_ptr_ = mc_ptr;
is_multicast_setup_ = true;
comm.barrier();
#else
(void)exporter_rank;
(void)tag;
NVF_ERROR("Multicast requires CUDA 13.0+");
#endif
}
} // namespace nvfuser