Skip to content

GPU JIT: cap fused kernels at the CUDA parameter limit - #38

Open
AdvaitTahilyani wants to merge 11 commits into
scale/22-lu-followupsfrom
scale/23-jit-param-limit
Open

AdvaitTahilyani wants to merge 11 commits into
scale/22-lu-followupsfrom
scale/23-jit-param-limit

Conversation

@AdvaitTahilyani

Copy link
Copy Markdown

A long unsynced elementwise chain could produce a fused kernel with more than 4 KB of parameters. ptxas rejected it and the server aborted. Now:

  • Fusion stops before a kernel would exceed the parameter budget.
  • Output copies are moved up so that split kernels don't keep every intermediate alive.
  • If ptxas still rejects a kernel, the JIT falls back to one kernel per op instead of aborting.
  • The budget is part of the cache key.

No change in stencil or black_scholes timings. Adds test_jit_params.

🤖 Generated with Claude Code

AdvaitTahilyani and others added 3 commits November 4, 2026 23:33
ptxas rejects kernels with more than 4352 bytes of parameters below PTX
ISA 8.1 (we target 8.0 on sm_80), and a long unsynchronized elementwise
chain fused into one loop passed every array (a (3 + 2 * rank) * 8 byte
memref descriptor each) and scalar as a parameter of a single launch. The
loop fuser now stops merging when the fused loop would capture more than
the limit less 128 bytes (32764 for PTX >= 8.1 on sm_70+;
CNUM_JIT_MAX_PARAM_BYTES overrides it); temporaries used only inside the
merged loop are free. Copies of temporaries into outputs move up next to
their producer first, so a split kernel keeps the outputs thread-local
instead of materializing every intermediate. The budget is part of the
kernel cache key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit fafda827f1fbf3712558dd217252653e9873bb52)
A PTX image that did not assemble used to be embedded as PTX, and
loading it at engine initialization hit report_fatal_error (LLVM ERROR,
server abort). generateNVIDIAHost now returns nullptr with the ptxas log,
and compileKernel retries the AST as one kernel per op.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 7a2a5be0c2717153c532f45bf27ad204dfc10518)
Long unsynchronized chains (lat.py's batch loop, also with no fused-op
cap), 120 live outputs in 1-D/2-D/3-D, 110 distinct inputs of mixed
dtypes and 150 views of one array, vs NumPy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 180f30ec741b9f07748899be3e6f61c6ea3dc77c)

@adityapb adityapb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict: approve with nits.

This is a static review. Line numbers are at this PR's head.

These hold up:

  • paramBytes: it matches the lowering. Each memref costs (3+2·rank)·8 bytes with the default kernel ABI, scalars count as at most 8 bytes, and sunk values are free.
  • The budget is really exercised: on A100/H100 it is effectively 4224 bytes, because PTX is capped at 8.0, so test_jit_params does exercise splitting.
  • hoistOutputCopies: safe, because outputs are fresh arrays.
  • Cache key: the budget is part of it.
  1. [minor] The ptxas fallback can itself abort.
    • The fallback recompiles the whole node unfused (jit.cpp:2771-2781), and the temporaries are freed only at the end of the host function (:283-285), so it needs device memory for all the intermediates at once.
    • On a long chain over a large tile (e.g. 200 ops on a 400 MB tile), cn_mgpuMemAlloc hits report_fatal_error("JIT temporary allocation failed").
    • This is rare: it only happens when the budgeted kernel still fails ptxas.
    • Fix: split at the rejected kernel, or free temporaries after their last use.

Temporaries were freed at the end of the host function, so the unfused fallback (and
any split kernel chain) needed device memory for every intermediate at once. The
dealloc now follows the temporary's last user; launches and frees are ordered on the
chare's stream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author
  1. [minor] the ptxas fallback can abort: fixed in 7ea597f (each temporary's dealloc moves to just after its last user, and the stream-ordered cuMemFreeAsync lets an unfused chain hold only its live temporaries). I did not add a split at the rejected kernel, because with early frees it isn't needed.

Validated: GPU build compiles; CPU regress +p1/+p2 (orig 15/15, ports 25/25, ops 33/33, test_jit_params 21 checks). An A100 check is queued (budget 0, a 240-op chain on a 400 MB array, which would need about 96 GB with end-of-function frees).

AdvaitTahilyani and others added 2 commits November 10, 2026 16:19
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author

The queued GPU job covered #35/#36 only and did not include the JIT long-chain memory check, so the temporary-freeing change is validated on CPU only so far.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author

Correction to my earlier comment: the GPU job did include the JIT long-chain check, and it failed (JIT temporary allocation failed, device out of memory, 240-op chain on a 400 MB array with the fallback forced). Cause: allocations are still hoisted above the first loop, so freeing each temporary after its last use does not lower the peak. A fix that sinks each allocation to its first use is in progress; I'll post GPU results with it.

fuseElementwiseLoops() hoists every allocation above the first loop so that loops become
adjacent, so 7ea597f's early frees did not lower the peak: an unfused chain (the ptxas
fallback, or a kernel split at the parameter budget) still allocated all of its temporaries
before the first launch and ran out of device memory. Each remaining alloc now moves to just
before its first user, so the chain holds only its live temporaries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

AdvaitTahilyani commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

The ptxas fallback could still run out of device memory. This is now fixed in 664bde9.

Validated on 1 A100-40GB: one 1-D float64 tile, 1 PE, CNUM_ODF=1. The chain is x = x*s_i; x = x + t_i with distinct scalars, checked against NumPy at the head, middle and tail (rtol 1e-12). Peak is nvidia-smi memory.used, sampled every 20 ms.

build tile ops CNUM_JIT_MAX_PARAM_BYTES result peak
#37 (scale/22) 400 MB 600 n/a abort: ptxas parameter space 0x1310 > 0x1100 -
#37 400 MB 240 n/a pass (one fused kernel) 1.25 GB
#38 at 79c896a 400 MB 600 1e6 (ptxas fallback) abort: JIT temporary allocation failed: 2 40.2 GB
#38 + 664bde9 400 MB 600 1e6 (ptxas fallback) pass 2.0 GB
#38 at 79c896a 40 MB 600 1e6 pass 23.5 GB
#38 + 664bde9 40 MB 600 1e6 pass 0.67 GB
#38 at 79c896a 400 MB 240 0 (unfused) abort: out of memory 40.2 GB
#38 + 664bde9 400 MB 240 0 (unfused) pass 2.0 GB
#38 + 664bde9 400 MB 600 default (split at budget) pass 1.6 GB

The GPU build of the #38 head compiles. The merged-up heads are validated on #42 and #44.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants