-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Feature] use pytest for sgl-kernel #4896
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e650066
Parameterize
adarshxs d2e4ddd
Merge branch 'sgl-project:main' into pytest
adarshxs a47cc35
use pytest
adarshxs f57d44d
Merge branch 'sgl-project:main' into pytest
adarshxs 15790f8
fix all_reduce_test
adarshxs fbf82db
fix trt_allreduce
adarshxs be2dc2f
Merge branch 'main' into pytest
adarshxs b63fde9
fix lint
adarshxs d6767f8
fix
adarshxs f6d9931
fix
adarshxs 7e53a51
pass CI
adarshxs c1de340
upd
zhyncs 11d0d4b
pass CI
adarshxs 3c69014
add top level func
adarshxs 23b799b
add top level func
adarshxs 7548600
Merge branch 'main' into pytest
FlamingoPg ac2bed2
Merge branch 'main' into pytest
zhyncs 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,49 +1,49 @@ | ||
| import unittest | ||
|
|
||
| import pytest | ||
| import torch | ||
| from sgl_kernel import cublas_grouped_gemm | ||
|
|
||
|
|
||
| def torch_grouped_gemm(a_array, b_array, out_dtype): | ||
| c_array = [] | ||
| for a, b in zip(a_array, b_array): | ||
| c_array.append(torch.matmul(a, b.t()).to(out_dtype)) | ||
| return c_array | ||
|
|
||
|
|
||
| class TestGroupedGemm(unittest.TestCase): | ||
| def _test_accuracy(self, Ms, Ns, Ks, out_dtype): | ||
| group_count = len(Ms) | ||
| a_array = [] | ||
| b_array = [] | ||
| c_array_cublas = [] | ||
| for i in range(group_count): | ||
| M, N, K = Ms[i], Ns[i], Ks[i] | ||
| a_array.append(torch.randn((M, K), device="cuda", dtype=out_dtype) * 5) | ||
| b_array.append(torch.randn((N, K), device="cuda", dtype=out_dtype) * 5) | ||
| c_array_cublas.append(torch.empty((M, N), device="cuda", dtype=out_dtype)) | ||
|
|
||
| c_array_torch = torch_grouped_gemm(a_array, b_array, out_dtype) | ||
| cublas_grouped_gemm(a_array, b_array, c_array_cublas, out_dtype) | ||
|
|
||
| for i in range(group_count): | ||
| M, N, K = Ms[i], Ns[i], Ks[i] | ||
| torch.testing.assert_close(c_array_torch[i], c_array_cublas[i]) | ||
| print(f"M={M}, N={N}, K={K}, out_dtype={out_dtype}: OK") | ||
|
|
||
| def test_accuracy(self): | ||
| Ms = [1, 16, 32, 256, 1024] | ||
| Ns = [2, 16, 128, 256, 4096] | ||
| Ks = [3, 16, 32, 512, 8192] | ||
| out_dtypes = [torch.float16, torch.bfloat16] | ||
| for out_dtype in out_dtypes: | ||
| self._test_accuracy(Ms, Ns, Ks, out_dtype) | ||
| return [torch.matmul(a, b.t()).to(out_dtype) for a, b in zip(a_array, b_array)] | ||
|
|
||
|
|
||
| skip_condition = not torch.cuda.is_available() or ( | ||
| torch.version.cuda is None | ||
| or tuple(map(int, torch.version.cuda.split("."))) < (12, 5) | ||
| ) | ||
|
|
||
|
|
||
| m_values = [1, 16, 32, 256, 1024] | ||
| n_values = [2, 16, 128, 256, 4096] | ||
| k_values = [3, 16, 32, 512, 8192] | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| skip_condition, reason="CUDA not available or CUDA version lower than 12.5" | ||
| ) | ||
| @pytest.mark.parametrize("out_dtype", [torch.float16, torch.bfloat16]) | ||
| @pytest.mark.parametrize("M", m_values) | ||
| @pytest.mark.parametrize("N", n_values) | ||
| @pytest.mark.parametrize("K", k_values) | ||
| def test_grouped_gemm_accuracy(out_dtype, M, N, K): | ||
| try: | ||
| a = torch.randn((M, K), device="cuda", dtype=out_dtype) * 5 | ||
| b = torch.randn((N, K), device="cuda", dtype=out_dtype) * 5 | ||
| expected = torch.matmul(a, b.t()).to(out_dtype) | ||
|
|
||
| a_array = [a] | ||
| b_array = [b] | ||
| c_array = [torch.empty((M, N), device="cuda", dtype=out_dtype)] | ||
|
|
||
| result_torch = torch_grouped_gemm(a_array, b_array, out_dtype)[0] | ||
| cublas_grouped_gemm(a_array, b_array, c_array, out_dtype) | ||
|
|
||
| torch.testing.assert_close(result_torch, expected) | ||
| torch.testing.assert_close(c_array[0], expected) | ||
|
|
||
| except torch.cuda.OutOfMemoryError: | ||
| pytest.skip(f"Skipping M={M}, N={N}, K={K} due to OOM") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if torch.cuda.is_available(): | ||
| cuda_version = tuple(map(int, torch.version.cuda.split("."))) | ||
| if cuda_version >= (12, 5): | ||
| unittest.main() | ||
| else: | ||
| print(f"Cuda version {cuda_version} lower than 12.5, not executing tests.") | ||
| pytest.main([__file__]) | ||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,67 +1,49 @@ | ||
| import unittest | ||
|
|
||
| import pytest | ||
| import torch | ||
| from sgl_kernel import fp8_scaled_mm | ||
|
|
||
|
|
||
| def torch_scaled_mm(a, b, scale_a, scale_b, out_dtype, bias): | ||
| o = torch.matmul(a.to(torch.float32), b.to(torch.float32)) | ||
|
|
||
| o = o.to(torch.float32) | ||
| temp1 = o * scale_a.view(-1, 1) | ||
| temp2 = temp1 * scale_b.view(1, -1) | ||
| final = temp2.to(out_dtype) | ||
| if bias is not None: | ||
| final = final + bias.view(1, -1) | ||
|
|
||
| return final | ||
|
|
||
|
|
||
| class TestFp8Gemm(unittest.TestCase): | ||
| def _test_accuracy_once(self, M, N, K, with_bias, out_dtype, device): | ||
| fp8_info = torch.finfo(torch.float8_e4m3fn) | ||
| fp8_max, fp8_min = fp8_info.max, fp8_info.min | ||
|
|
||
| a_fp32 = ( | ||
| (torch.rand(M, K, dtype=torch.float32, device=device) - 0.5) * 2 * fp8_max | ||
| ) | ||
| a_fp8 = a_fp32.clamp(min=fp8_min, max=fp8_max).to(torch.float8_e4m3fn) | ||
|
|
||
| b_fp32 = ( | ||
| (torch.rand(N, K, dtype=torch.float32, device=device) - 0.5) * 2 * fp8_max | ||
| ) | ||
| b_fp8 = b_fp32.clamp(min=fp8_min, max=fp8_max).to(torch.float8_e4m3fn) | ||
|
|
||
| scale_a = torch.randn((M,), device=device, dtype=torch.float32) * 0.001 | ||
| scale_b = torch.randn((N,), device=device, dtype=torch.float32) * 0.001 | ||
| if with_bias: | ||
| bias = torch.randn((N,), device=device, dtype=out_dtype) | ||
| else: | ||
| bias = None | ||
| o1 = torch.empty((M, N), device=device, dtype=torch.bfloat16) | ||
| b_fp8 = b_fp8.t() | ||
| o = torch_scaled_mm(a_fp8, b_fp8, scale_a, scale_b, out_dtype, bias) | ||
| o1 = fp8_scaled_mm(a_fp8, b_fp8, scale_a, scale_b, out_dtype, bias) | ||
| rtol = 0.02 | ||
| atol = 1 | ||
| torch.testing.assert_close(o, o1, rtol=rtol, atol=atol) | ||
| print(f"M={M}, N={N}, K={K}, with_bias={with_bias}, out_dtype={out_dtype}: OK") | ||
|
|
||
| def test_accuracy(self): | ||
| Ms = [1, 128, 512, 1024, 4096] | ||
| Ns = [16, 128, 512, 1024, 4096] | ||
| Ks = [512, 1024, 4096, 8192, 16384] | ||
| bias_opts = [True, False] | ||
| out_dtypes = [torch.bfloat16, torch.float16] | ||
| for M in Ms: | ||
| for N in Ns: | ||
| for K in Ks: | ||
| for with_bias in bias_opts: | ||
| for out_dtype in out_dtypes: | ||
| self._test_accuracy_once( | ||
| M, N, K, with_bias, out_dtype, "cuda" | ||
| ) | ||
| def _test_accuracy_once(M, N, K, with_bias, out_dtype, device): | ||
| fp8_info = torch.finfo(torch.float8_e4m3fn) | ||
| fp8_max, fp8_min = fp8_info.max, fp8_info.min | ||
| a_fp32 = (torch.rand(M, K, dtype=torch.float32, device=device) - 0.5) * 2 * fp8_max | ||
| a_fp8 = a_fp32.clamp(min=fp8_min, max=fp8_max).to(torch.float8_e4m3fn) | ||
| b_fp32 = (torch.rand(N, K, dtype=torch.float32, device=device) - 0.5) * 2 * fp8_max | ||
| b_fp8 = b_fp32.clamp(min=fp8_min, max=fp8_max).to(torch.float8_e4m3fn) | ||
| scale_a = torch.randn((M,), device=device, dtype=torch.float32) * 0.001 | ||
| scale_b = torch.randn((N,), device=device, dtype=torch.float32) * 0.001 | ||
| if with_bias: | ||
| bias = torch.randn((N,), device=device, dtype=out_dtype) | ||
| else: | ||
| bias = None | ||
| b_fp8 = b_fp8.t() | ||
| o = torch_scaled_mm(a_fp8, b_fp8, scale_a, scale_b, out_dtype, bias) | ||
| o1 = fp8_scaled_mm(a_fp8, b_fp8, scale_a, scale_b, out_dtype, bias) | ||
| rtol = 0.02 | ||
| atol = 1 | ||
| torch.testing.assert_close(o, o1, rtol=rtol, atol=atol) | ||
| print(f"M={M}, N={N}, K={K}, with_bias={with_bias}, out_dtype={out_dtype}: OK") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("M", [1, 128, 512, 1024, 4096]) | ||
| @pytest.mark.parametrize("N", [16, 128, 512, 1024, 4096]) | ||
| @pytest.mark.parametrize("K", [512, 1024, 4096, 8192, 16384]) | ||
| @pytest.mark.parametrize("with_bias", [True, False]) | ||
| @pytest.mark.parametrize("out_dtype", [torch.bfloat16, torch.float16]) | ||
| def test_accuracy(M, N, K, with_bias, out_dtype): | ||
| _test_accuracy_once(M, N, K, with_bias, out_dtype, "cuda") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| pytest.main([__file__]) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.