Repository navigation
Avoid zero-initializing JAX outputs the kernel already writes #631
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,3 +189,58 @@ def test_grouped_gemm_jax_errors(): | |
| b_dtype="bfloat16", | ||
| bias_tensor=bias_j, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.L0 | ||
| def test_grouped_gemm_jax_addressed_rows_survive_a_dirty_allocator(): | ||
| """Rows before ``padded_offsets[-1]`` are correct whatever the output buffer held. | ||
|
|
||
| The outputs are no longer zero-initialized, so the rows the kernel does not | ||
| address carry whatever XLA last left in the buffer. A test that runs on a clean | ||
| allocator passes either way -- fresh device memory reads as zeros -- so this one | ||
| dirties the allocator first and checks only the contract that still holds. | ||
| """ | ||
| skip_unless_sm100() | ||
| from cudnn import grouped_gemm_jax_sm100, grouped_gemm_wrapper_sm100 | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| m, n, k, experts = 1024, 256, 128, 2 | ||
| a_np, b_storage_np, _, alpha_np, prob_np = make_problem(m, n, k, experts) | ||
| # 256-aligned, and the last offset stops short of m, so the tail is never addressed | ||
| offsets_np = np.array([256, 512], dtype=np.int32) | ||
|
|
||
| to_torch = lambda x: torch.from_numpy(x.view(np.uint8)).view(torch.bfloat16).cuda() | ||
|
Contributor
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. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Replace the assigned lambda with a function. Ruff reports E731 for 🧰 Tools🪛 Ruff (0.16.1)[error] 211-211: Do not assign a Rewrite (E731) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||
| a_t = to_torch(a_np).reshape(m, k, 1) | ||
| b_t = to_torch(b_storage_np).reshape(experts, n, k) | ||
| b_ptrs_t = torch.tensor([b_t[i].data_ptr() for i in range(experts)], dtype=torch.int64, device="cuda") | ||
| expected = grouped_gemm_wrapper_sm100( | ||
| a_tensor=a_t, | ||
| padded_offsets=torch.from_numpy(offsets_np).cuda(), | ||
| alpha_tensor=torch.from_numpy(alpha_np).cuda(), | ||
| prob_tensor=torch.from_numpy(prob_np).cuda(), | ||
| b_ptrs=b_ptrs_t, | ||
| n=n, | ||
| b_dtype=torch.bfloat16, | ||
| c_dtype=torch.bfloat16, | ||
| d_dtype=torch.bfloat16, | ||
| )["d_tensor"] | ||
| device_sync() | ||
|
|
||
| a_j = jnp.asarray(a_np) | ||
| weights = [jnp.asarray(b_storage_np[i]) for i in range(experts)] | ||
| offsets_j, alpha_j, prob_j = (jnp.asarray(x) for x in (offsets_np, alpha_np, prob_np)) | ||
| jax.block_until_ready((a_j, offsets_j, alpha_j, prob_j, *weights)) | ||
| ptr_values = np.array([w.unsafe_buffer_pointer() for w in weights], dtype=np.int64) | ||
| b_ptrs_j = jax.block_until_ready(jnp.asarray(ptr_values.view(np.uint8))) | ||
| jitted = jax.jit(lambda a, off, alpha, ptrs, prob: grouped_gemm_jax_sm100(a, off, alpha, ptrs, n, prob)[0]) | ||
|
|
||
| # Poison the allocator so a reused output buffer cannot read back as zeros. | ||
| for _ in range(8): | ||
| junk = jax.block_until_ready(jnp.full((m, n, 1), -7.5, dtype=jnp.bfloat16)) | ||
| del junk | ||
|
|
||
| actual = jax.block_until_ready(jitted(a_j, offsets_j, alpha_j, b_ptrs_j, prob_j)) | ||
| valid = int(offsets_np[-1]) | ||
| np.testing.assert_array_equal( | ||
| np.asarray(actual)[:valid], | ||
| expected[:valid].float().cpu().numpy().astype(np.asarray(actual).dtype), | ||
| ) | ||
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Replace the named lambda with a function.
Ruff reports E731 on Line 240. This lint error can fail the configured Ruff check. Define
to_torchwithdef.Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.16.1)
[error] 240-240: Do not assign a
lambdaexpression, use adefRewrite
to_torchas adef(E731)
🤖 Prompt for AI Agents
Source: Linters/SAST tools