-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Kernel][Triton] Add Triton implementation for scaled_mm_triton to support fp8 and int8 SmoothQuant, symmetric case #9857
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4624680
add implementation
rasmith 242e6d1
add implementation
rasmith fa38282
Fix type in test case
rasmith 7cb6c6e
just assert tensor strides good instead of fixing them, simplify tens…
rasmith a875da8
8-bit support for testing
rasmith 0fb4836
Check for cuda
rasmith 7c8865a
yapf
rasmith 5abafe4
yapf
rasmith d5e390d
prune tests, add skipif, has_good_tensor->is_weak_contiguous
rasmith f003676
only add fp8 if has_device_capability(89)
rasmith 4f1e62e
add supports_fp8 to git_8bit_types
rasmith fa6b1cc
rename scaled_mm_triton->triton_scaled_mm
rasmith 5a04f41
Merge branch 'vllm-project:main' into ransmith_int8_triton
rasmith 69cd7a3
Merge branch 'vllm-project:main' into ransmith_int8_triton
rasmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"""Tests for the scaled_mm_triton kernel | ||
Run `pytest tests/kernels/test_scaled_mm_triton.py`. | ||
""" | ||
import importlib | ||
from typing import Optional, Type | ||
|
||
import pytest | ||
import torch | ||
|
||
from vllm.platforms import current_platform | ||
|
||
device = "cuda" | ||
|
||
|
||
def scaled_mm_torch(a: torch.Tensor, | ||
b: torch.Tensor, | ||
scale_a: torch.Tensor, | ||
scale_b: torch.Tensor, | ||
out_dtype: Type[torch.dtype], | ||
bias: Optional[torch.Tensor] = None) -> torch.Tensor: | ||
out = torch.mm(a.to(torch.float32), b.to(torch.float32)) | ||
out = scale_a * out | ||
out = scale_b.T * out | ||
out = out.to(out_dtype) | ||
if bias is not None: | ||
out = out + bias | ||
|
||
return out | ||
|
||
|
||
def get_8bit_types(): | ||
types = [torch.int8] | ||
if current_platform.is_rocm(): | ||
types.append(torch.float8_e4m3fnuz) | ||
elif current_platform.is_cuda(): | ||
types.append(torch.float8_e4m3fn) | ||
return types | ||
|
||
|
||
@pytest.mark.parametrize("M", [1, 33, 64, 512]) | ||
@pytest.mark.parametrize("N", [256, 971, 20486]) | ||
@pytest.mark.parametrize("K", [128, 496, 1024]) | ||
@pytest.mark.parametrize("out_dtype", [torch.float16, torch.bfloat16]) | ||
@pytest.mark.parametrize("in_dtype", get_8bit_types()) | ||
@pytest.mark.parametrize("use_scalar_scale_a", [True, False]) | ||
@pytest.mark.parametrize("use_scalar_scale_b", [True, False]) | ||
@pytest.mark.parametrize("use_bias", [True, False]) | ||
@pytest.mark.skipif(not current_platform.has_device_capability(89), | ||
reason="FP8 is not supported on this GPU type.") | ||
def test_scaled_mm(M, N, K, in_dtype, out_dtype, use_scalar_scale_a, | ||
use_scalar_scale_b, use_bias): | ||
is_floating_point_type = lambda t: torch.tensor([1, 1], dtype=t | ||
).is_floating_point() | ||
|
||
current_platform.seed_everything(0) | ||
|
||
# NOTE: There are cases, where if the matrix is large enough, an output | ||
# like 65504.4 can be produced, and can easily turn into inf when | ||
# multiplied when using float16/bfloat16. This means one function, e.g., | ||
# testing function, and another function, e.g. golden function, can | ||
# produce a non-inf value while the other produces an inf value, and | ||
# will cause assert_close/allclose to fail, even though if overflow | ||
# wouldn't have occurred, the values would have been "close." | ||
# | ||
# So, the values here are kept small enough to avoid this situation. | ||
if is_floating_point_type(in_dtype): | ||
a = (0.25 * torch.rand( | ||
(M, K), dtype=torch.float32, device=device)).to(in_dtype) | ||
b = (0.25 * torch.rand( | ||
(K, N), dtype=torch.float32, device=device)).to(in_dtype) | ||
else: | ||
a = torch.randint(-32, 32, (M, K), dtype=in_dtype, device=device) | ||
b = torch.randint(-32, 32, (K, N), dtype=in_dtype, device=device) | ||
|
||
if use_scalar_scale_a: | ||
scale_a = torch.rand((1, 1), device=device) | ||
else: | ||
scale_a = 0.25 * torch.rand((M, 1), device=device) | ||
|
||
if use_scalar_scale_b: | ||
scale_b = torch.rand((1, 1), device=device) | ||
else: | ||
scale_b = 0.25 * torch.rand((N, 1), device=device) | ||
|
||
bias = None | ||
if use_bias: | ||
bias = torch.rand((N, ), device=device, dtype=out_dtype) | ||
|
||
scaled_mm_triton_module = importlib.import_module( | ||
"vllm.model_executor.layers.quantization.compressed_tensors." | ||
"scaled_mm_triton") | ||
scaled_mm_triton = scaled_mm_triton_module.scaled_mm_triton | ||
|
||
c_check = scaled_mm_triton(a, b, scale_a, scale_b, out_dtype, bias) | ||
|
||
a_cpu = a.cpu() | ||
b_cpu = b.cpu() | ||
scale_a_cpu = scale_a.cpu() | ||
scale_b_cpu = scale_b.cpu() | ||
bias_cpu = None if bias is None else bias.cpu() | ||
|
||
c_actual = scaled_mm_torch(a_cpu, b_cpu, scale_a_cpu, scale_b_cpu, | ||
out_dtype, bias_cpu) | ||
|
||
c_check_cpu = c_check.cpu() | ||
torch.testing.assert_close(c_check_cpu, c_actual, rtol=1e-1, atol=1e-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
vllm/model_executor/layers/quantization/compressed_tensors/scaled_mm_triton.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
from typing import Optional, Type | ||
|
||
import torch | ||
import triton | ||
import triton.language as tl | ||
|
||
|
||
def is_weak_contiguous(x: torch.Tensor): | ||
strides = x.stride() | ||
sizes = x.shape | ||
is_not_transpose = strides[0] == 1 and (strides[1] >= max(1, sizes[0])) | ||
is_transpose = strides[1] == 1 and (strides[0] >= max(1, sizes[1])) | ||
return is_transpose or is_not_transpose | ||
|
||
|
||
@triton.jit | ||
def scaled_mm_kernel(a_ptr, b_ptr, scale_a_ptr, scale_b_ptr, c_ptr, bias_ptr, | ||
M, N, K, stride_am, stride_ak, stride_bk, stride_bn, | ||
stride_cm, stride_cn, ACCUMULATOR_DTYPE: tl.constexpr, | ||
BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, | ||
BLOCK_SIZE_K: tl.constexpr, | ||
BLOCK_SIZE_SCALE_A: tl.constexpr, | ||
BLOCK_SIZE_SCALE_B: tl.constexpr): | ||
pid = tl.program_id(axis=0) | ||
|
||
num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) | ||
|
||
pid_m = pid // num_pid_n | ||
pid_n = pid % num_pid_n | ||
|
||
accumulator_dtype = ACCUMULATOR_DTYPE | ||
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), | ||
dtype=accumulator_dtype) | ||
|
||
# NOTE: Some tensor inputs are so large, they will cause int32 overflow | ||
# so it is necessary to use tl.int64 for all the offsets, else SEGV will | ||
# eventually occur. | ||
|
||
# Offsets and masks. | ||
offsets_am = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M).to(tl.int64) | ||
masks_am = offsets_am < M | ||
|
||
offsets_bn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N).to(tl.int64) | ||
masks_bn = offsets_bn < N | ||
|
||
offsets_k = tl.arange(0, BLOCK_SIZE_K).to(tl.int64) | ||
offsets_a = (stride_am * offsets_am[:, None] + | ||
stride_ak * offsets_k[None, :]) | ||
offsets_b = (stride_bk * offsets_k[:, None] + | ||
stride_bn * offsets_bn[None, :]) | ||
|
||
# NOTE: BLOCK_SIZE_SCALE_A could be 1 or BLOCK_SIZE_M, so need to create | ||
# appropriate offsets and masks for each case. Same goes for | ||
# BLOCK_SIZE_SCALE_B. | ||
offsets_scale_am = (tl.arange(0, BLOCK_SIZE_SCALE_A) + | ||
(BLOCK_SIZE_SCALE_A > 1) * pid_m * BLOCK_SIZE_M) | ||
masks_scale_am = offsets_scale_am < M | ||
|
||
offsets_scale_bn = (tl.arange(0, BLOCK_SIZE_SCALE_B) + | ||
(BLOCK_SIZE_SCALE_B > 1) * pid_n * BLOCK_SIZE_N) | ||
masks_scale_bn = offsets_scale_bn < N | ||
|
||
a_ptrs = a_ptr + offsets_a | ||
b_ptrs = b_ptr + offsets_b | ||
|
||
scale_a_ptrs = scale_a_ptr + offsets_scale_am | ||
scale_b_ptrs = scale_b_ptr + offsets_scale_bn | ||
|
||
for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): | ||
masks_k = offsets_k < K | ||
masks_a = masks_am[:, None] & masks_k[None, :] | ||
a = tl.load(a_ptrs, mask=masks_a) | ||
|
||
masks_b = masks_k[:, None] & masks_bn[None, :] | ||
b = tl.load(b_ptrs, mask=masks_b) | ||
|
||
# Accumulate results. | ||
accumulator = tl.dot(a, b, accumulator, out_dtype=accumulator_dtype) | ||
|
||
offsets_k += BLOCK_SIZE_K | ||
a_ptrs += BLOCK_SIZE_K * stride_ak | ||
b_ptrs += BLOCK_SIZE_K * stride_bk | ||
|
||
# Apply scale at end. | ||
masks_scale_a = masks_scale_am[:, None] & (tl.arange(0, 1) < 1)[:, None] | ||
scale_a = tl.load(scale_a_ptrs[:, None], masks_scale_a) | ||
# Need to broadcast to the appropriate size, if scale_a is already | ||
# (BLOCK_SIZE_M, 1) then it will broadcast to its own shape. Same goes | ||
# for scale_b below. | ||
scale_a = scale_a.broadcast_to((BLOCK_SIZE_M, 1)) | ||
accumulator = scale_a * accumulator.to(tl.float32) | ||
|
||
masks_scale_b = masks_scale_bn[:, None] & (tl.arange(0, 1) < 1)[None, :] | ||
scale_b = tl.load(scale_b_ptrs[:, None], masks_scale_b) | ||
scale_b = scale_b.broadcast_to((BLOCK_SIZE_N, 1)) | ||
accumulator = scale_b.T * accumulator.to(tl.float32) | ||
|
||
# Convert to output format. | ||
c = accumulator.to(c_ptr.type.element_ty) | ||
|
||
# Add bias, it's already in output format, so add it after conversion. | ||
if bias_ptr: | ||
offsets_bias = offsets_bn | ||
bias_ptrs = bias_ptr + offsets_bias | ||
bias_mask = offsets_bias < N | ||
bias = tl.load(bias_ptrs, bias_mask) | ||
c += bias | ||
|
||
# Save output | ||
offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M).to(tl.int64) | ||
offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N).to(tl.int64) | ||
offs_cm = offs_cm.to(tl.int64) | ||
offs_cn = offs_cn.to(tl.int64) | ||
c_ptrs = (c_ptr + stride_cm * offs_cm[:, None] + | ||
stride_cn * offs_cn[None, :]) | ||
c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N) | ||
|
||
tl.store(c_ptrs, c, mask=c_mask) | ||
|
||
|
||
# input - [M, K] | ||
# weight - [K, N] | ||
def scaled_mm_triton(input: torch.Tensor, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
weight: torch.Tensor, | ||
scale_a: torch.Tensor, | ||
scale_b: torch.Tensor, | ||
out_dtype: Type[torch.dtype], | ||
bias: Optional[torch.Tensor] = None, | ||
block_size_m: int = 32, | ||
block_size_n: int = 32, | ||
block_size_k: int = 32) -> torch.Tensor: | ||
M, K = input.shape | ||
N = weight.shape[1] | ||
|
||
assert N > 0 and K > 0 and M > 0 | ||
assert weight.shape[0] == K | ||
assert input.dtype == weight.dtype | ||
assert scale_a.dtype == scale_b.dtype and scale_a.is_floating_point() | ||
assert scale_a.shape == torch.Size([1, 1]) or scale_a.shape == torch.Size( | ||
[M, 1]) | ||
assert scale_b.shape == torch.Size([1, 1]) or scale_b.shape == torch.Size( | ||
[N, 1]) | ||
assert out_dtype.is_floating_point | ||
assert bias is None or bias.is_floating_point() | ||
assert is_weak_contiguous(input) | ||
assert is_weak_contiguous(weight) | ||
|
||
grid = lambda META: (triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv( | ||
N, META['BLOCK_SIZE_N']), ) | ||
|
||
result = torch.empty((M, N), dtype=out_dtype, device=input.device) | ||
|
||
has_scalar = lambda x: x.shape[0] == 1 and x.shape[1] == 1 | ||
|
||
block_size_sa = 1 if has_scalar(scale_a) else block_size_m | ||
block_size_sb = 1 if has_scalar(scale_b) else block_size_n | ||
|
||
accumulator_dtype = tl.float32 if input.is_floating_point() else tl.int32 | ||
|
||
# A = input, B = weight, C = result | ||
# A = M x K, B = K x N, C = M x N | ||
scaled_mm_kernel[grid](input, | ||
weight, | ||
scale_a, | ||
scale_b, | ||
result, | ||
bias, | ||
M, | ||
N, | ||
K, | ||
input.stride(0), | ||
input.stride(1), | ||
weight.stride(0), | ||
weight.stride(1), | ||
result.stride(0), | ||
result.stride(1), | ||
accumulator_dtype, | ||
BLOCK_SIZE_M=block_size_m, | ||
BLOCK_SIZE_N=block_size_n, | ||
BLOCK_SIZE_K=block_size_k, | ||
BLOCK_SIZE_SCALE_A=block_size_sa, | ||
BLOCK_SIZE_SCALE_B=block_size_sb) | ||
|
||
return result.to(out_dtype) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should detect if
in_dtype
is fp8 as well, otherwise we're skipping the int8 tests when we don't need toThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tlrmchlsmth I just changed get_8bit_types() to only include fp8 if current_platform.has_device_capability(89) returns true. Will this work for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep!