-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathgemm.py
More file actions
308 lines (263 loc) · 9.69 KB
/
gemm.py
File metadata and controls
308 lines (263 loc) · 9.69 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
# Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# See LICENSE for license information.
"""Python interface for GEMM extensions"""
from typing import Iterable, Optional, Tuple, Union, List
import os
import functools
import torch
import transformer_engine_torch as tex
from ..constants import TE_DType
from ..utils import get_sm_count, _empty_tensor
from ..quantized_tensor import Quantizer
from ..tensor.storage.float8_blockwise_tensor_storage import Float8BlockwiseQTensorStorage
from ..tensor.utils import is_custom
from ..custom_recipes.gemm import custom_gemm
from ...debug.pytorch.debug_quantization import DebugQuantizer
__all__ = [
"general_gemm",
"general_grouped_gemm",
]
_NUM_MAX_UB_STREAMS = 3
def get_cublas_workspace_size_bytes() -> None:
"""Return 32 MiB if using hopper, 4 MiB for all other architectures."""
if torch.cuda.get_device_properties(torch.cuda.current_device()).major >= 9:
# 32 MiB for NVFP4 GEMM, plus additional 1024 B for alignment and misc scales
return 32 * 1024 * 1024 + 1024
return 4_194_304
@functools.lru_cache(maxsize=None)
def get_cublas_workspace(device: int, ub: bool, grouped_gemm: bool) -> torch.Tensor:
"""Returns workspace for cublas GEMM."""
assert not (ub and grouped_gemm), "UB is unsupported for grouped GEMM."
if ub:
return torch.empty(
get_cublas_workspace_size_bytes() * _NUM_MAX_UB_STREAMS,
dtype=torch.uint8,
device=device,
)
if grouped_gemm:
_multi_stream_cublas_workspace = []
for _ in range(tex.get_num_cublas_streams()):
_multi_stream_cublas_workspace.append(
torch.empty(get_cublas_workspace_size_bytes(), dtype=torch.uint8, device=device)
)
return _multi_stream_cublas_workspace
return torch.empty(get_cublas_workspace_size_bytes(), dtype=torch.uint8, device=device)
def validate_gemm_scale(scale: Optional[float], required: bool) -> float:
"""Validate whether a GEMM scaling factor is consistent with its usage"""
if required:
return scale if scale is not None else 1.0
if scale not in (0.0, None):
raise ValueError("scale must be zero")
return 0.0
def get_tensor_device(tensor: torch.Tensor) -> int:
"""
Returns tensor device as an integer.
This method is used because checking instances of
QuantizedTensor or Storage incurs more CPU overhead.
The order of attributes checked is important to also
minimize overhead.
"""
if hasattr(tensor, "_rowwise_data") and tensor._rowwise_data is not None:
return tensor._rowwise_data.device.index
if hasattr(tensor, "_columnwise_data") and tensor._columnwise_data is not None:
return tensor._columnwise_data.device.index
if hasattr(tensor, "_data") and tensor._data is not None:
return tensor._data.device.index
if hasattr(tensor, "_transpose") and tensor._transpose is not None:
return tensor._transpose.device.index
if hasattr(tensor, "device"):
return tensor.device.index
return torch.cuda.current_device()
def general_gemm(
A: torch.Tensor,
B: torch.Tensor,
out_dtype: Optional[torch.dtype] = None,
quantization_params: Optional[Quantizer] = None,
gelu: bool = False,
gelu_in: torch.Tensor = None,
alpha: float = 1.0,
beta: Optional[float] = None,
accumulate: bool = False,
layout: str = "TN",
out: Optional[torch.Tensor] = None,
bias: Optional[torch.Tensor] = None,
use_split_accumulator: bool = False,
grad: bool = False,
ub: Union[tex.CommOverlap, tex.CommOverlapP2P] = None,
ub_type: tex.CommOverlapType = None,
extra_output: Optional[torch.Tensor] = None,
bulk_overlap: bool = False,
) -> Iterable[Optional[torch.Tensor]]:
"""GEMM supporting fp8 inputs."""
assert layout in ("TN", "NN", "NT"), f"GEMM layout {layout} not supported."
transa = layout[0] == "T"
transb = layout[1] == "T"
alpha = validate_gemm_scale(alpha, True)
beta = validate_gemm_scale(beta, accumulate)
workspace = get_cublas_workspace(A.device, ub is not None, False)
if ub_type is not None:
assert ub is not None, (
f"{'AG+GEMM' if ub_type == tex.CommOverlapType.AG else 'GEMM+RS'} overlap requires"
+ "a valid `ub` communicator object."
)
if ub is not None:
assert ub_type is not None, "Comm+GEMM overlap requires a valid `comm_type` argument."
if ub_type == tex.CommOverlapType.RS:
if not (bulk_overlap and not ub.is_fp8_ubuf()):
assert extra_output is not None, "GEMM+RS overlap requires extra output tensor."
if out is not None:
if not out.is_contiguous():
raise ValueError("Output tensor is not contiguous.")
# If A or B are custom tensors -> dispatch to quantizers's qgemm implementation
if is_custom(A) or is_custom(B):
return custom_gemm(
A,
B,
workspace,
out_dtype,
quantization_params,
gelu,
gelu_in,
accumulate,
layout,
out,
bias,
use_split_accumulator,
grad,
)
debug_quantizer = None
if isinstance(quantization_params, DebugQuantizer):
debug_quantizer = quantization_params
quantization_params = quantization_params.parent_quantizer
A = A.get_tensor(not transa)
B = B.get_tensor(transb)
# Use bfloat16 as default bias_dtype
bias_dtype = TE_DType[torch.bfloat16 if bias is None else bias.dtype]
if isinstance(A, Float8BlockwiseQTensorStorage) or isinstance(B, Float8BlockwiseQTensorStorage):
# FP8 block-scaling requires split accumulator
use_split_accumulator = True
args = (
A,
transa, # transa
B,
transb, # transb
out,
quantization_params,
TE_DType[out_dtype] if out_dtype is not None else None,
bias,
bias_dtype,
gelu,
gelu_in,
grad, # grad
workspace,
workspace.shape[0],
accumulate,
use_split_accumulator,
)
kwargs = {
"comm_overlap": ub,
"comm_type": ub_type,
"extra_output": extra_output,
"bulk_overlap": bulk_overlap,
"alpha": alpha,
"beta": beta,
}
out, bias_grad, gelu_input, extra_output = tex.generic_gemm(*args, **kwargs)
if debug_quantizer is not None:
out = debug_quantizer.process_gemm_output(out)
return out, bias_grad, gelu_input, extra_output
def general_grouped_gemm(
A: List[torch.Tensor],
B: List[torch.Tensor],
out: List[torch.Tensor],
quantization_params: List[Optional[Quantizer]],
out_dtype: torch.dtype,
layout: str = "TN",
m_splits: Optional[List[int]] = None,
gelu: bool = False,
grad=False,
accumulate: bool = False,
bias: Optional[List[torch.Tensor]] = None,
use_bias: bool = False,
use_split_accumulator: bool = False,
D_dtype: Optional[tex.DType] = None,
single_output=False,
) -> Tuple[List[torch.Tensor], ...]:
"""
TN layout Grouped GEMM with fp8 inputs.
"""
num_gemms = len(A)
transa = layout[0] == "T"
transb = layout[1] == "T"
empty_tensor = _empty_tensor()
empty_tensors = [empty_tensor] * num_gemms
# Use bfloat16 as default bias_dtype
gelu_input = empty_tensors
out_dtype = TE_DType[out[0].dtype] if D_dtype is None else D_dtype
sm_count = get_sm_count()
workspaces = get_cublas_workspace(get_tensor_device(A[0]), False, True)
if grad and use_bias:
grad_bias = [
torch.empty(B[i].size(1), dtype=out[0].dtype, device="cuda") for i in range(num_gemms)
]
else:
grad_bias = empty_tensors
bias = bias if use_bias else empty_tensors
if use_bias:
bias_dtype = TE_DType[grad_bias[0].dtype] if grad else TE_DType[bias[0].dtype]
else:
bias_dtype = TE_DType[torch.bfloat16]
if isinstance(quantization_params[0], DebugQuantizer):
assert not gelu, "GELU not supported in debug mode"
if single_output:
out_init = out[0]
start_idx = 0
out = [None] * num_gemms
for i in range(num_gemms):
size = m_splits[i]
out[i] = out_init[start_idx : start_idx + size]
start_idx += size
for i in range(num_gemms):
_, bias_or_grad, _, _ = general_gemm(
A[i],
B[i],
quantization_params=quantization_params[i],
out_dtype=out[0].dtype,
layout=layout,
accumulate=accumulate,
out=out[i],
bias=bias[i] if use_bias else None,
use_split_accumulator=use_split_accumulator,
grad=grad,
)
if grad and use_bias:
grad_bias[i] = bias_or_grad
if single_output:
out = out_init
return out, grad_bias if grad else bias, None
if gelu:
gelu_input = [
torch.empty_like(o, dtype=bias_dtype, memory_format=torch.contiguous_format)
for o in out
] # this should differ with respect to single output
bias = tex.te_general_grouped_gemm(
A,
transa,
B,
transb,
out,
out_dtype,
m_splits,
grad_bias if grad else bias,
bias_dtype,
single_output,
gelu_input, # this is pre_gelu_out
grad, # grad
workspaces,
workspaces[0].shape[0],
accumulate,
use_split_accumulator,
sm_count - int(os.getenv("NVTE_EXT_MARGIN_SM", str(sm_count))),
)
return out, bias, gelu_input