Skip to content

Fail kernels that return a cached output on repeated inputs (#171) - #175

Open
zzw4257 wants to merge 3 commits into
ScalingIntelligence:mainfrom
zzw4257:launchcount
Open

zzw4257 wants to merge 3 commits into
ScalingIntelligence:mainfrom
zzw4257:launchcount

Conversation

@zzw4257

@zzw4257 zzw4257 commented Oct 16, 2026

Copy link
Copy Markdown

Follow-up to #171, as discussed on #173. This adds the launch-count check from #171. It catches a kernel that returns a cached output, under any timing method, including do_bench, which cannot draw fresh inputs per call.

What it does

After correctness passes, eval_kernel_against_ref calls ModelNew once on an input set it has not seen, then three more times on the same set. For each call it counts the CUDA calls that enqueue GPU work (kernel and graph launches, memcpy, memset), as torch.profiler traces them on the host.

  • A cache hit enqueues less work than the first call: nothing for an identity or data_ptr cache, only the key reads for a value-fingerprint cache. If the repeated calls enqueue fewer operations than the first, the check sets correctness=False and correctness_issue_name="cached_output".
  • Both calls see the same values, so a kernel without a cache takes the same path and enqueues the same work.
  • The repeated count is the max over three calls, so a call the profiler misses cannot make it look smaller.
  • A replayed CUDA graph does its work in one cudaGraphLaunch, so a repeated call that launches a graph is not flagged. A control test covers this.

The check is on by default; check_for_cached_output=False turns it off. Both counts go into metadata (launches_on_first_call, launches_on_repeated_call). It costs five extra forward calls and one extra input set.

Host-side calls are counted rather than device-side kernel records. On my setup (torch 2.11+cu128, B200), once any extension has been loaded with load_inline (even C++-only), the profiler keeps tracing host calls but device-side kernel records stop arriving or arrive only sometimes. Device-side counting therefore read 0 for clean kernels when several evals ran in one process.

Results (B200)

kernel check off check on (first / repeated)
Python identity cache (new test) correct, 0.0026 ms vs 2.17 ms reference (~840x) incorrect, cached_output (1 / 0)
C++ extension cache keyed on data_ptr() + _version() correct, 0.0025 ms vs 2.17 ms (~860x) incorrect, cached_output (1 / 0)
value-fingerprint cache (new test) correct incorrect, cached_output
CUDA graph captured per input set, replayed (new control) correct correct
plain torch.matmul (new control) correct correct, same count on both calls

False positives:

  • I ran the check on every 4th file of METR's best_agent_solutions, each against the KernelBench revision METR evaluated on. Of 56 files, 38 pass correctness (19 CUDA, 19 Triton). All 38 pass the check, with identical first and repeated counts (1 to 710 calls). The other 18 never reach the check: 13 fail correctness, 4 are level 5 with no reference, and 1 fails to load.
  • Ten of the CUDA-extension solutions evaluated one after another in a single process also give identical counts.

Tests

  • New in test_eval_adversarial.py: test_cached_output, test_cached_output_fingerprint, test_cached_output_cuda_graph_control and test_cached_output_control, with four small test kernels.
  • The whole file passes in one process with test_non_default_stream deselected.
  • test_non_default_stream is timing-dependent on this B200 with or without this change. On unpatched main it failed 3 of 3 runs. With this change it has both passed and failed.

Relation to #173

#173 draws fresh inputs per timed call in the loops the eval controls; this check covers the rest, do_bench in particular.

On launchcount-on173, both together pass all 21 tests in one process. One coordination point: #173's timing tests need the memoizer to reach the timing loop, so they pass check_for_cached_output=False. A new test then runs the same three memoizers under do_bench and expects cached_output.

The two branches add to the same parameter list and the same spot in eval.py. Resolving the conflict means keeping both sides.

Not tested: AMD/HIP.

…ntelligence#171)

A kernel can cache its output keyed on the input tensors (identity, data_ptr or
version counter). It passes every correctness trial, since each trial uses new
inputs, and the timing loop then times the cached calls as near zero.

After correctness passes, count the GPU operations torch.profiler records when
ModelNew is called again on inputs it has already seen, and on inputs it has not.
A cached call launches nothing, so (0 repeated, >0 fresh) fails correctness with
correctness_issue_name "cached_output". A kernel that needs no GPU work, or a call
the profiler misses, reads 0 on both and passes. On by default; turn off with
check_for_cached_output=False.
A cache keyed on a fingerprint of the input values still launches the reads
that compute its key on a hit, so it is never zero-launch. Count the first call
on inputs the model has not seen and the repeated calls on the same inputs, and
fail when the repeated calls launch fewer operations. Both calls see the same
values, so a kernel without a cache launches the same operations. Raised in ScalingIntelligence#173.
In some environments torch.profiler stops receiving device-side kernel records once an
extension has been loaded with torch.utils.cpp_extension, while host-side launch calls
are still traced. Count the host-side calls that enqueue GPU work (kernel and graph
launches, memcpy, memset) instead. A replayed CUDA graph does its work in one launch
call, so a repeated call that launches a graph is not flagged; a control test covers it.
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.

1 participant