Skip to content

Commit 595997e

Browse files
[KMCompiler][TLERaw] Adjust ag-gemm implementation for multi-node execution (#918)
Co-authored-by: iwanna-lxy <37344393+i3wanna2@users.noreply.github.com>
1 parent df824d7 commit 595997e

6 files changed

Lines changed: 938 additions & 269 deletions

File tree

python/triton/experimental/tle/raw/cuda/runtime.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def __init__(self, fn: Any, file: Path, *args, **kwargs) -> None:
234234
self.lowered_region_dialect: Final[str] = "llvm"
235235
self.arg_dialect: Final[str] = "llvm"
236236
self.source_file: Final[str] = str(file)
237+
self.opt_level: Final[str] = kwargs.get("opt_level", "-O3")
237238

238239
if self.library == "nvshmem":
239240
from triton.experimental.tle.raw.nvshmem.utils import enable_nvshmem_device_bc
@@ -259,7 +260,7 @@ def register_pending_source(self, *, hint: str = "") -> str:
259260
extern_func_name=self.extern_func_name,
260261
source=self.code,
261262
hint=hint,
262-
extra={"source_file": self.source_file},
263+
extra={"source_file": self.source_file, "opt_level": self.opt_level},
263264
)
264265

265266
def create_region_by_llvm(self, builder, llvm: str, handles, alias_indices, hint: str = "",
@@ -285,7 +286,7 @@ def make_llvm(self, mlir_context) -> str:
285286
"--cuda-device-only",
286287
_get_cuda_gpu_arch(),
287288
"-emit-llvm",
288-
"-O2",
289+
self.opt_level,
289290
"-S",
290291
"-",
291292
"-o",
@@ -314,5 +315,6 @@ def read_text(self):
314315
file=_CudaSourceFile(),
315316
extern_func_name=entry.get("extern_func_name"),
316317
deferred=True,
318+
opt_level=entry.get("opt_level"),
317319
)
318320
return cuda_fn.make_llvm(context)

python/triton/experimental/tle/raw/nvshmem/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,18 @@ def set_signal_cuda_ptr(signal_ptr, signal, stream):
336336
CUDA_CHECK(err)
337337

338338

339+
def copy_on_stream(dst_ptr, src_ptr, nbytes, stream):
340+
_, cudart = _get_cuda_modules()
341+
(err, ) = cudart.cudaMemcpyAsync(
342+
dst_ptr,
343+
src_ptr,
344+
nbytes,
345+
cudart.cudaMemcpyKind.cudaMemcpyDefault,
346+
stream.cuda_stream,
347+
)
348+
CUDA_CHECK(err)
349+
350+
339351
def print_perf(
340352
name: str,
341353
value: float,

python/tutorials/tle/raw/nvshmem/02-allgather-gemm/ag-gemm-device.cu

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
#include <cuda_fp16.h>
21
#include <stddef.h>
32
#include <stdint.h>
43

54
extern "C" __device__ uint64_t nvshmem_signal_wait_until(uint64_t *sig_addr,
65
int cmp,
76
uint64_t cmp_value);
7+
extern "C" __device__ void
8+
nvshmemx_putmem_signal_block(void *dest, const void *source, size_t bytes,
9+
uint64_t *sig_addr, uint64_t signal, int sig_op,
10+
int pe);
811

912
enum {
1013
NVSHMEM_CMP_GE = 5,
@@ -16,7 +19,7 @@ ag_mark_local_ready(__attribute__((address_space(1))) uint64_t *ready,
1619
int rank) {
1720
if (threadIdx.x == 0) {
1821
__threadfence_system();
19-
ready[(size_t)rank] = 1;
22+
ready[rank] = 1;
2023
}
2124
__syncthreads();
2225
}
@@ -29,3 +32,13 @@ ag_wait_ready(__attribute__((address_space(1))) uint64_t *ready,
2932
}
3033
__syncthreads();
3134
}
35+
36+
extern "C" __device__ __attribute__((always_inline)) void
37+
ag_putmem_signal_block(__attribute__((address_space(1))) void *dest,
38+
__attribute__((address_space(1))) const void *source,
39+
size_t bytes,
40+
__attribute__((address_space(1))) uint64_t *sig_addr,
41+
uint64_t signal, int peer) {
42+
nvshmemx_putmem_signal_block(dest, source, bytes, sig_addr, signal,
43+
NVSHMEM_SIGNAL_SET, peer);
44+
}

python/tutorials/tle/raw/nvshmem/02-allgather-gemm/ag-gemm-host.cu

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#include <cuda_fp16.h>
21
#include <cuda_runtime.h>
32
#include <nvshmem.h>
43
#include <nvshmemx.h>
54
#include <stdint.h>
65
#include <stdio.h>
7-
#include <stdlib.h>
86

97
#define CUDA_CHECK(stmt) \
108
do { \
@@ -16,10 +14,12 @@
1614
} \
1715
} while (0)
1816

19-
extern "C" int ag_gemm_workspace_create(int elements_per_rank, void **workspace,
20-
uint64_t **ready, int *mype, int *npes,
21-
int *mype_in_node, int *npes_in_node) {
22-
if (elements_per_rank <= 0 || workspace == nullptr || ready == nullptr) {
17+
extern "C" int ag_gemm_workspace_create(int elements_per_rank, int element_size,
18+
void **workspace, uint64_t **ready,
19+
int *mype, int *npes, int *mype_in_node,
20+
int *npes_in_node) {
21+
if (elements_per_rank <= 0 || element_size <= 0 || workspace == nullptr ||
22+
ready == nullptr) {
2323
return -1;
2424
}
2525

@@ -29,7 +29,7 @@ extern "C" int ag_gemm_workspace_create(int elements_per_rank, void **workspace,
2929
*npes_in_node = nvshmem_team_n_pes(NVSHMEMX_TEAM_NODE);
3030
CUDA_CHECK(cudaSetDevice(*mype_in_node));
3131

32-
size_t workspace_bytes = (size_t)(*npes) * elements_per_rank * sizeof(__half);
32+
size_t workspace_bytes = (size_t)(*npes) * elements_per_rank * element_size;
3333
*workspace = nvshmem_malloc(workspace_bytes);
3434
*ready = (uint64_t *)nvshmem_calloc((size_t)(*npes), sizeof(uint64_t));
3535
if (*workspace == nullptr || *ready == nullptr) {
@@ -61,3 +61,13 @@ extern "C" void *ag_gemm_peer_workspace_ptr(void *workspace, int peer) {
6161
extern "C" uint64_t *ag_gemm_peer_ready_ptr(uint64_t *ready, int peer) {
6262
return (uint64_t *)nvshmem_ptr(ready, peer);
6363
}
64+
65+
extern "C" void ag_gemm_barrier_all_on_stream(cudaStream_t stream) {
66+
nvshmemx_barrier_all_on_stream(stream);
67+
}
68+
69+
extern "C" void ag_gemm_signal_wait_until_on_stream(uint64_t *signal,
70+
uint64_t value,
71+
cudaStream_t stream) {
72+
nvshmemx_signal_wait_until_on_stream(signal, NVSHMEM_CMP_GE, value, stream);
73+
}

0 commit comments

Comments
 (0)