Skip to content

1D Convolution: avoid 11.4 GiB windows matrix in reference_impl (JAX T4 OOM) - #391

Open
kunal-mansukhani wants to merge 1 commit into
mainfrom
fix/jax-low-success-nov22
Open

kunal-mansukhani wants to merge 1 commit into
mainfrom
fix/jax-low-success-nov22

Conversation

@kunal-mansukhani

Copy link
Copy Markdown
Contributor

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 * 4 bytes, the size of the input.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)

  • New vs old reference on the example + all 14 functional cases, and against reference_impl_jax: all pass the checker's compare() 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.
  • Peak extra memory at the real perf size (1.5M x 2047): conv1d 0.05 GiB vs einsum 11.42 GiB. With a 7 GB address-space cap, the old reference fails with tried to allocate 12265247352 bytes; the new one passes.
  • Full submit (example + functional + 2 warmup + 15 perf runs) of a correct JAX solution through the infra runner (core.run_test_case / benchmark) on CPU JAX:
    • GPU-backend path (torch reference): passes with the new reference. With main's reference under the 7 GB cap, it fails at the 12.27 GB allocation.
    • TPU-backend path (reference_impl_jax): passes (unchanged).
  • black 24.1.1 / isort 5.13.2 / flake8 7.0.0 clean; scripts/validate_challenges.py reports 0 errors.

Not run on a real GPU/TPU.

… 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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Review bot: needs a human reviewer.

  • edits existing challenge grader/tests challenges/easy/9_1d_convolution/challenge.py

@github-actions github-actions Bot added the needs-human-review Review bot: needs a human reviewer label Nov 22, 2026

@shxjames shxjames left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,352 bytes = 11.42 GiB. unfold itself 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 conv1d at 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, not torch.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_dilated with 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.

@claude

claude Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Reviewed the reference_impl change (unfold+einsum → F.conv1d).

  • Semantics match: F.conv1d is cross-correlation with no kernel flip, valid padding and stride 1. That is the same as the HTML spec, the old path and the unchanged reference_impl_jax. view(1, 1, -1) is safe because the inputs are contiguous 1D tensors.
  • Only standard PyTorch ops are used, so it lowers on XLA. Asserts and tests are unchanged. F.conv* references are already used elsewhere in the repo (2D Convolution, Gaussian Blur, Causal Depthwise Conv1d).
  • Live T4 validation: a shared-memory tiled CUDA solution (it chunks the kernel so K=2047 works) passed --action run and --action submit (example + all functional + perf: "All tests passed"). The solution file was not committed.
  • Non-blocking note: on Ampere and newer GPUs, cuDNN convs default to TF32 (torch.backends.cudnn.allow_tf32=True). At K=2047 that could push the reference past 1e-4. This doesn't affect T4, which has no TF32, and the repo's other F.conv* challenges already have the same exposure. It's worth keeping in mind if this challenge is ever run on A100 or H100.

LGTM.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-human-review Review bot: needs a human reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants