Repository navigation
1D Convolution: avoid 11.4 GiB windows matrix in reference_impl (JAX T4 OOM) - #391
kunal-mansukhani wants to merge 1 commit into
Conversation
… unfold+einsum The perf case (1.5M x 2047) made einsum copy the strided windows view into an 11.42 GiB tensor. On a 16 GB T4 with JAX's default 75% GPU preallocation only ~3.4 GiB is free, so every JAX submit on T4 OOMed in the reference.
shxjames
left a comment
There was a problem hiding this comment.
The reference_impl swap from unfold+einsum to F.conv1d is the same valid-padding, stride-1 cross-correlation as the HTML (output[i] = sum_j input[i+j] * kernel[j]), the old path, and the unchanged reference_impl_jax. No kernel flip. Only this function changed.
Correctness (independent fp64 CPU windowed dots in /workspace/kr; same tensors for the unseeded random functional cases; seed 0 on perf):
- Examples 1 and 2 match the HTML (
[-2, -2, -2],[1.8, 3.2, 4.6]). - Example + all 13 functional cases: old vs new max abs 4.77e-6 (N=1000, K=15); new vs fp64 also 4.77e-6; worst share of the 1e-4 budget 0.00106 (~940x headroom). Every test dict includes the output buffer; shapes are
N-K+1. - Perf N=1,500,000 K=2047: old algorithm run chunked so as not to allocate the 11.4 GiB windows matrix. new vs chunked-old max abs 1.41e-4; new vs fp64 1.37e-4; max|y| 77.13; budget 7.81e-3; share 0.0176 (~57x headroom).
- Unchanged JAX ref vs new: functional worst 5.72e-6 / share 0.00156; mid 200k x 2047 max abs 1.26e-4 / share 0.0185.
Memory / T4 OOM (no CUDA on this box; method: analytic size + Linux ru_maxrss in a fresh process; mid-size einsum used to confirm the windows matrix is materialized):
- Old windows at perf size:
(N-K+1)*K*4 = 12,265,247,352bytes = 11.42 GiB.unfolditself is a view (storage = input only);einsum/.contiguous()allocates the full matrix. Mid-size: 20k x 2047 analytic 0.137 GiB, einsum dHWM 144 MiB; 40k x 2047 analytic 0.289 GiB, dHWM 300 MiB — tracks the analytic size. On a 16 GB T4, JAX's default 75% preallocation leaves ~4 GiB, which cannot hold 11.42 GiB. That is the OOM. - New
conv1dat the real perf size: peak extra vs the post-input baseline is ~60 MiB (HWM 289 MiB from a 229 MiB post-tensor baseline; output itself is 5.7 MiB). Inputs+output ~12 MB. Fits comfortably in 16 GB even next to JAX preallocation. (CPU RSS, nottorch.cuda.max_memory_allocated; a T4 cuDNN workspace should still be orders of magnitude under the leftover 4 GiB.) - JAX path was already
lax.conv_general_dilatedwith VALID padding (no windows matrix) and is unchanged. CPU JAX at the full perf size peaked ~61 MiB above the jnp arrays, not 11.4 GiB.
Spec / tests / starters / low-prec
- HTML, constraints, and sizes already match; this PR does not touch them. Coverage includes K=1, K==N (10x10), 1x1, and odd K (3, 7, 15, 2047).
- atol=rtol=1e-4: correct fp32 has >=57x headroom on perf. TF32 / FP16 / BF16 shortcuts are rejected (perf shares vs fp32: 6.58 / 4.56 / 3850; worst functional 11.4 / 5.87 / 47).
- All 6 starters unchanged and consistent (
solve(input, kernel, output, input_size, kernel_size); JAX returns the output). - Perf is 6.13 GFLOP; T4 fp32 floor ~0.76 ms. Pre-existing sizing, still fine.
CI on a0937ce2aea5bd9c836b227aff13292c0569acf1 is green (review, checks, Lint Python Files, Check Mojo Files, Lint C++/CUDA Files, check-index). Merge is blocked only on a code-owner approval. LGTM, no blockers.
|
Reviewed the
LGTM. 🤖 Generated with Claude Code |
Problem
JAX submissions to 1D Convolution on T4 almost never pass (7/76 submits all-time; 0 from Jan to Jul). 54 of the 66 failed JAX T4 submits are
CUDA out of memory ... Tried to allocate 11.42 GiB ... of which ~3.4 GiB is free ... 37 MiB is allocated by PyTorch.11.42 GiB is exactly
(1,500,000 - 2047 + 1) * 2047 * 4bytes, the size of theinput.unfold(0, kernel_size, 1)windows matrix at the performance size.torch.einsum("ij,j->i", windows, kernel)copies that strided view into a contiguous tensor. On a 16 GB T4, JAX's default GPU preallocation (75%) holds most of the memory once the functional tests have initialized JAX, so the reference OOMs on the first perf warmup no matter what the user's solution does. CUDA/Triton/PyTorch solutions usually get away with it because nothing else holds the memory, though they hit the same OOM during an Apr 12-19 burst (132 CUDA, 47 Triton).Fix
Compute the reference with
torch.nn.functional.conv1d(cross-correlation, valid padding, stride 1, the same semantics). No windows matrix is materialized.reference_impl_jax, tests, sizes and tolerances are unchanged.Testing (local, CPU only)
reference_impl_jax: all pass the checker'scompare()at atol/rtol 1e-4 (max diff 4.8e-6). On a scaled perf case (400k x 2047): max diff 1.6e-4 vs max |out| 70.8, which is well inside the scaled tolerance.tried to allocate 12265247352 bytes; the new one passes.core.run_test_case/benchmark) on CPU JAX:reference_impl_jax): passes (unchanged).scripts/validate_challenges.pyreports 0 errors.Not run on a real GPU/TPU.