-
Notifications
You must be signed in to change notification settings - Fork 29
ck_tile grouped gemm: more padding #574
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
Open
matthiasdiener
wants to merge
10
commits into
dev
Choose a base branch
from
mdiener/cktile-grouped-gemm-padding
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
95f984c
ck_tile grouped gemm: more padding
matthiasdiener cfbc537
Merge branch 'dev' into mdiener/cktile-grouped-gemm-padding
matthiasdiener 225c3dc
address review comments
matthiasdiener 2939017
NT workaround, split, address review comments
matthiasdiener fa87ccc
Merge remote-tracking branch 'origin/dev' into mdiener/cktile-grouped…
matthiasdiener 01f62d0
factor out templating
matthiasdiener aee2c4c
address review comments, capture fallbacks
matthiasdiener f830b89
Merge remote-tracking branch 'origin/dev' into mdiener/cktile-grouped…
matthiasdiener 2751b2a
Merge remote-tracking branch 'origin' into mdiener/cktile-grouped-gem…
matthiasdiener a59a4ae
fix env reset
matthiasdiener 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3075,6 +3075,144 @@ def test_grouped_gemm(shape, dtype, layout, accumulate, use_cutlass): | |
| os.environ.pop("NVTE_USE_CUTLASS_GROUPED_GEMM", None) | ||
|
|
||
|
|
||
| if IS_HIP_EXTENSION: | ||
| @pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16], ids=str) | ||
| @pytest.mark.parametrize("layout", ["TN", "NN", "NT", "TT"]) | ||
| @pytest.mark.parametrize("accumulate", [False, True]) | ||
| @pytest.mark.parametrize( | ||
| "pad_dim", | ||
| ["K", "M", "N", "MK", "MKN"], | ||
| ids=lambda d: f"pad{d}", | ||
| ) | ||
| def test_grouped_gemm_unaligned(dtype, layout, accumulate, pad_dim, capfd): | ||
| """Test CK grouped GEMM with M, N, or K not aligned to CK tile size. | ||
|
|
||
| CK constraints for bf16/fp16: | ||
| - Contiguous dim of A/B must be dword-aligned (even for 2-byte types). | ||
| RowMajor: contiguous dim is cols (K for A, N for B). | ||
| ColMajor: contiguous dim is rows (M for A, K for B). | ||
| - K tile: 64, M tile: 256, N tile: 128/256 | ||
| """ | ||
| torch.manual_seed(0) | ||
| z = 8 | ||
|
|
||
| # Unaligned values per dimension (all satisfy CK vector-load constraints). | ||
| # K: even but not multiple of tile (64). Same for all groups. | ||
| # M: not multiples of tile (256), varies per group. | ||
| # N: multiple of 16 but not multiple of tile (128). | ||
| unaligned_k = 2016 | ||
| unaligned_m = [100, 300, 150, 200, 50, 350, 250, 180] | ||
| unaligned_n = 2032 | ||
|
|
||
| # Aligned defaults. | ||
| k_aligned = 2048 | ||
| m_aligned = 256 | ||
| n_aligned = 2048 | ||
|
|
||
| # Select (un)aligned values based on pad_dim. | ||
| k_val = unaligned_k if "K" in pad_dim else k_aligned | ||
| m_vals = unaligned_m if "M" in pad_dim else [m_aligned] * z | ||
| n_val = unaligned_n if "N" in pad_dim else n_aligned | ||
|
|
||
| total_m = sum(m_vals) | ||
| os.environ["NVTE_USE_CUTLASS_GROUPED_GEMM"] = "1" | ||
|
Collaborator
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: better use monkeypath to make sure the envs are cleared if tests fails |
||
| os.environ["NVTE_CUTLASS_GROUPED_GEMM_WARN_FALLBACK"] = "1" | ||
|
|
||
| if layout == "TN": | ||
| A = [torch.randn(n_val, k_val, dtype=dtype, device="cuda") for _ in range(z)] | ||
| B = [torch.randn(m, k_val, dtype=dtype, device="cuda") for m in m_vals] | ||
| out = [torch.randn(total_m, n_val, dtype=dtype, device="cuda")] | ||
| out_ref = [o.clone() for o in torch.split(out[0], m_vals)] | ||
| m_splits = m_vals | ||
| grad = False | ||
| single_output = True | ||
| elif layout == "NN": | ||
| A = [torch.randn(k_val, n_val, dtype=dtype, device="cuda") for _ in range(z)] | ||
| B = [torch.randn(m, k_val, dtype=dtype, device="cuda") for m in m_vals] | ||
| out = [torch.randn(total_m, n_val, dtype=dtype, device="cuda")] | ||
| out_ref = [o.clone() for o in torch.split(out[0], m_vals)] | ||
| m_splits = m_vals | ||
| grad = True | ||
| single_output = True | ||
| elif layout == "NT": | ||
| A = list(torch.split( | ||
| torch.randn(total_m, k_val, dtype=dtype, device="cuda"), m_vals | ||
| )) | ||
| B = list(torch.split( | ||
| torch.randn(total_m, n_val, dtype=dtype, device="cuda"), m_vals | ||
| )) | ||
| out = [torch.randn(n_val, k_val, dtype=dtype, device="cuda") for _ in range(z)] | ||
| out_ref = [o.clone() for o in out] | ||
| m_splits = m_vals | ||
| grad = True | ||
| single_output = False | ||
| else: # TT | ||
| A = [torch.randn(n_val, k_val, dtype=dtype, device="cuda") for _ in range(z)] | ||
| B = [torch.randn(k_val, m, dtype=dtype, device="cuda") for m in m_vals] | ||
| out = [torch.randn(total_m, n_val, dtype=dtype, device="cuda")] | ||
| out_ref = [o.clone() for o in torch.split(out[0], m_vals)] | ||
| m_splits = m_vals | ||
| grad = False | ||
| single_output = True | ||
|
|
||
| # Reference: individual GEMMs | ||
| for i in range(z): | ||
| if layout == "TT": | ||
| # general_gemm doesn't support TT; compute reference manually. | ||
| ref = B[i].T.to(torch.float32) @ A[i].T.to(torch.float32) | ||
| if accumulate: | ||
| out_ref[i] = (out_ref[i].to(torch.float32) + ref).to(dtype) | ||
| else: | ||
| out_ref[i] = ref.to(dtype) | ||
| else: | ||
| general_gemm( | ||
| A[i], | ||
| B[i], | ||
| dtype, | ||
| grad=grad, | ||
| accumulate=accumulate, | ||
| layout=layout, | ||
| out=out_ref[i], | ||
| ) | ||
|
|
||
| if single_output: | ||
| out_ref = [torch.cat(out_ref)] | ||
|
|
||
| general_grouped_gemm( | ||
| A, | ||
| B, | ||
| out, | ||
| [None] * z, | ||
| dtype, | ||
| m_splits=m_splits, | ||
| grad=grad, | ||
| accumulate=accumulate, | ||
| layout=layout, | ||
| single_output=single_output, | ||
| ) | ||
|
|
||
| for o, o_ref in zip(out, out_ref): | ||
| if IS_HIP_EXTENSION and accumulate and dtype == torch.bfloat16 and get_device_compute_capability() == (9, 4): | ||
|
Collaborator
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. The test itself is IS_HIP_EXTENSION only |
||
| torch.testing.assert_close(o, o_ref, rtol=4e-2, atol=4e-2) | ||
| else: | ||
| torch.testing.assert_close(o, o_ref, rtol=1.5e-2, atol=1.5e-2) | ||
|
|
||
| os.environ.pop("NVTE_USE_CUTLASS_GROUPED_GEMM", None) | ||
| os.environ.pop("NVTE_CUTLASS_GROUPED_GEMM_WARN_FALLBACK", None) | ||
|
|
||
| # Check for CK fallback warnings from C++ (NVTE_WARN writes to std::cerr). | ||
| # capfd captures file-descriptor-level output, including C/C++ stderr. | ||
| captured = capfd.readouterr() | ||
| if "Falling back" in captured.err or "Fallback" in captured.err: | ||
| if "K" in pad_dim and layout != "NN": | ||
| pytest.xfail( | ||
| "Known CK_Tile limitation: K-padding with non-NN layouts may fall back to cuBLAS " | ||
| "(kPadK + ColMajor B bug, or CK_Tile stride alignment requirements)" | ||
| ) | ||
| else: | ||
| pytest.fail(f"CK_Tile grouped GEMM fell back to cuBLAS:\n{captured.err}") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("N", [32]) | ||
| @pytest.mark.parametrize("datatype", [torch.float16, torch.bfloat16]) | ||
| @pytest.mark.parametrize( | ||
|
|
||
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
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.
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.
I think z should be derived as len of unaligned_m, or it should be asserted that they are equal