-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmatmul_wrapper.py
More file actions
180 lines (163 loc) · 4.81 KB
/
Copy pathmatmul_wrapper.py
File metadata and controls
180 lines (163 loc) · 4.81 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
# SPDX-License-Identifier: MIT
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
import torch
import triton
# from streamk_kernel import streamk_gemm
from gemm_all_scatter_wg_specialization import (
persistent_gemm_all_scatter_wg_specialization,
)
from examples.common.utils import is_triton_interpret_set
import iris
gemm_kernel = persistent_gemm_all_scatter_wg_specialization
class matmul(torch.autograd.Function):
_debug = False
_registers = None
_spills = None
_num_xcds = iris.hip.get_num_xcc()
@staticmethod
def set_debug(debug: bool):
matmul._debug = debug
@staticmethod
def get_matmul_registers():
if matmul._debug:
return matmul._registers
else:
raise RuntimeError("Debug mode is not enabled. Call set_debug(True) first.")
@staticmethod
def get_matmul_spills():
if matmul._debug:
return matmul._spills
else:
raise RuntimeError("Debug mode is not enabled. Call set_debug(True) first.")
@staticmethod
def _call(
a: torch.Tensor,
b: torch.Tensor,
c: torch.Tensor,
c_global: torch.Tensor,
bias: torch.Tensor,
locks: torch.Tensor,
rank: int,
world_size: int,
gemm_sms: int,
num_sms: int,
BLK_M: int,
BLK_N: int,
BLK_K: int,
gsize_m: int,
num_stages: int,
heap_bases_ptr: torch.Tensor = None,
arch: str = "gfx942",
COLLECT_TIMESTAMPS: bool = False,
mm_begin_timestamp: torch.Tensor = None,
mm_end_timestamp: torch.Tensor = None,
):
# checks constraints
assert a.shape[1] == b.shape[0], "incompatible dimensions"
M, K = a.shape
_, N = b.shape
num_xcds = matmul._num_xcds
# TODO: Use arch-specific values.
num_warps = 8
waves_per_eu = 0
mfma = 16
kpack = 1
total_blocks_M = triton.cdiv(M, BLK_M)
total_blocks_N = triton.cdiv(N, BLK_N)
iters_per_tile = triton.cdiv(K, BLK_K)
total_tiles = total_blocks_M * total_blocks_N
even_k = K % BLK_K == 0
use_bias = False
# compute grid (work to do per SM on the first wave)
stride_bias = bias.stride(0) if use_bias else 0
kk = gemm_kernel[(num_sms,)](
a,
b,
c,
c_global,
bias,
locks,
M,
N,
K,
a.stride(0),
a.stride(1),
b.stride(0),
b.stride(1),
c.stride(0),
c.stride(1),
c_global.stride(0),
c_global.stride(1),
stride_bias,
BLOCK_SIZE_M=BLK_M,
BLOCK_SIZE_N=BLK_N,
BLOCK_SIZE_K=BLK_K,
GROUP_SIZE_M=gsize_m,
GEMM_SMS=gemm_sms,
NUM_SMS=num_sms,
NUM_XCDS=num_xcds,
BIAS=use_bias,
EVEN_K=even_k,
num_stages=num_stages,
num_warps=num_warps,
waves_per_eu=waves_per_eu,
matrix_instr_nonkdim=mfma,
kpack=kpack,
heap_bases=heap_bases_ptr,
cur_rank=rank,
world_size=world_size,
COLLECT_TIMESTAMPS=COLLECT_TIMESTAMPS,
mm_begin_timestamp_ptr=mm_begin_timestamp,
mm_end_timestamp_ptr=mm_end_timestamp,
)
if matmul._debug and not is_triton_interpret_set():
matmul._registers = kk.n_regs
matmul._spills = kk.n_spills
return c
@staticmethod
def forward(
ctx,
a: torch.Tensor,
b: torch.Tensor,
c: torch.Tensor,
c_global: torch.Tensor,
bias: torch.Tensor,
locks: torch.Tensor,
rank: int,
world_size: int,
gemm_sms: int,
num_sms: int,
BLK_M: int,
BLK_N: int,
BLK_K: int,
gsize_m: int,
num_stages: int,
heap_bases_ptr: torch.Tensor = None,
arch: str = "gfx942",
COLLECT_TIMESTAMPS: bool = False,
mm_begin_timestamp: torch.Tensor = None,
mm_end_timestamp: torch.Tensor = None,
):
matmul._call(
a=a,
b=b,
c=c,
c_global=c_global,
bias=bias,
locks=locks,
rank=rank,
world_size=world_size,
gemm_sms=gemm_sms,
num_sms=num_sms,
BLK_M=BLK_M,
BLK_N=BLK_N,
BLK_K=BLK_K,
gsize_m=gsize_m,
num_stages=num_stages,
heap_bases_ptr=heap_bases_ptr,
arch=arch,
COLLECT_TIMESTAMPS=COLLECT_TIMESTAMPS,
mm_begin_timestamp=mm_begin_timestamp,
mm_end_timestamp=mm_end_timestamp,
)
return c