Skip to content

Draw new inputs for every timed call to stop input memoization (#171) - #173

Open
utsav1033 wants to merge 2 commits into
ScalingIntelligence:mainfrom
utsav1033:fix/rotate-perf-inputs
Open

utsav1033 wants to merge 2 commits into
ScalingIntelligence:mainfrom
utsav1033:fix/rotate-perf-inputs

Conversation

@utsav1033

@utsav1033 utsav1033 commented Oct 14, 2026 •

Copy link
Copy Markdown

Fixes #171.

Problem

eval_kernel_against_ref built inputs once and every timing method called kernel_fn(*args) with the same tensors on every timed trial. A ModelNew that memoizes on its inputs (if x is self._last_x: return self._last_out) is correct on every correctness trial, but after the first call every timed trial is a cache hit, so it reports an arbitrarily large speedup. Tolerances and input distributions can't catch this, because the output really is correct.

Fix

  • eval.make_fresh_inputs(get_inputs, seed, device, ...) returns fresh_args(call), which draws a new input set for each timed call with its own seed (seed + 1 + call). Inputs are new tensors with new values and stay reproducible. They're drawn directly on the device, falling back to the CPU if get_inputs can't run there, so drawing per call stays cheap.
  • The loops the eval controls (cuda_event, do_bench_impl, host_time) take an optional fresh_args and draw before each timed call, outside the timed region. The previous set is freed first, so only one extra set is live whatever the input size. Warmup still uses the original inputs.
  • do_bench and nsight_python own their loops, so drawing per call would land inside the timed region. They keep reusing the inputs and print a warning when fresh_args is passed.
  • eval_kernel_against_ref gets fresh_perf_inputs=True (False gives the old behaviour). The reference timing in the excessive-speedup check uses the same per-call inputs, so the comparison is like for like.
  • measure_ref_program_time (baseline generation) is unchanged, so published baselines stay comparable.

New values on every call matter: copies of the same inputs would still lose to a memoizer keyed on a fingerprint of a few input values. It passes correctness, since those trials use different values, then hits on every timed call.

The first version of this PR rotated through a pre-built pool capped at 25% of free memory. As the review showed, the pool cycles once it's smaller than the number of timed calls (2 sets for level1/19's 6.4 GB inputs), so a memoizer keeping the last 4 inputs still won. The pool and the cap are gone now.

Tests

test_eval_adversarial.py:

  • memoizers keyed on the last input, the last 4 inputs and a value fingerprint, each across cuda_event, do_bench_impl and host_time
  • the review's ReLU memoizer (last 4 inputs) on a level1/19-shaped problem scaled to 1/8 so it fits a 16 GB GPU
  • peak memory with fresh inputs doesn't grow with the number of trials
  • a control showing the hack is flagged with fresh_perf_inputs=False

test_fresh_perf_inputs.py (CPU only): drawn inputs differ per call, are reproducible and differ from warmup, precision and non-tensor handling, CPU fallback, and a real host_time loop (CUDA stubbed) giving warmup the original inputs and every timed call new values.

Results (Tesla T4, cuda_event unless noted, 100 trials, 19 passed in 1m51s)

case runtime reference
last-input memoizer, 3 timing methods 29.8 / 30.2 / 31.7 ms 29.9 / 30.4 / 31.8 ms honest
last-4 memoizer, 3 timing methods 30.7 / 31.8 / 32.9 ms 31.1 / 32.0 / 33.2 ms honest
fingerprint memoizer, 3 timing methods 32.5 / 33.6 / 34.6 ms 32.7 / 33.8 / 34.7 ms honest
ReLU last-4 memoizer (level1/19 shape, 1/8 size) 6.96 ms 6.92 ms honest
control, fresh_perf_inputs=False 0.00246 ms 34.2 ms 13902x, flagged

Peak extra memory while timing a 1 GiB input: 3.00 GiB for 10 trials and 3.00 GiB for 50 trials.

Not run: level1/19 at full size (doesn't fit a T4), other precisions and backends, AMD.

Follow-up

The launch-count check from #171 (zero CUDA launches on a call means it did no work) would also cover do_bench, nsight_python and memoization paths fresh inputs can't reach. The issue author offered to send it.

🤖 Generated with Claude Code

…emoization

The timing loop passed the same input tensors to every timed trial, so a
ModelNew that caches its output on input identity (e.g. `if x is
self._last_x: return self._last_out`) passes every correctness trial but is
timed from its cache, giving an arbitrarily large fake speedup.

eval_kernel_against_ref now builds a pool of distinct input sets (own seed
each, up to one per timed call, capped at 25% of free device memory by
default) and every timing method rotates through it. Warmup still uses the
original inputs, so pool entries are unseen before the first timed call. The
reference timing in the excessive-speedup check uses the same pool.
Rotation can be turned off with rotate_perf_inputs=False.

Fixes ScalingIntelligence#171

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

zzw4257 commented Oct 16, 2026

Copy link
Copy Markdown

Thanks for the quick fix! I ran it at ea770f5 on a B200 and found one remaining gap: the pool cycles once it holds fewer sets than there are timed calls, and the cap makes that common.

The test kernel only remembers the last input, so any 2-set rotation beats it. A memoizer that keeps a few entries survives once the pool cycles:

class ModelNew(nn.Module):
    def __init__(self):
        super().__init__()
        self._cache = []                      # [(x, out)], last 4 inputs
    def forward(self, x):
        for seen, out in self._cache:
            if seen is x:
                return out
        out = torch.relu(x)
        self._cache = (self._cache + [(x, out)])[-4:]
        return out

Results with run_test_kernel's settings (cuda_event, 100 timed trials, threshold 10):

problem memoizer pool speedup flagged
level1/1 matmul last input (PR test) 101 1.0x no
level1/1 matmul one entry per input set 101 1.0x no
level1/1 matmul one entry per input set, T4-sized cap 30 3.4x no
level1/19 ReLU last 4 inputs, default cap 2 89x yes
  • Large inputs: each level1/19 input set is 6.4 GB (4096 x 393216 fp32), so the default cap (eval.py:653-654) left 2 sets on the B200. Problems 20-22 (LeakyReLU, Sigmoid, Tanh) use the same input shape.
  • Small GPUs: with the cap the default gives on a T4 (25% of ~15 GB free), the matmul pool holds 30 sets. The speedup stays under the 10x threshold, so nothing is flagged.
  • excessive_speedup only sets metadata. It does not change correctness or runtime.

Two suggestions:

  1. In the loops the eval controls (cuda_event, do_bench_impl, host_time), draw a fresh input set before each timed call, outside the timed region, instead of cycling a pool (timing.py:169). Then the pool size and the memory cap no longer matter, and only one or two sets need to be live at a time.
  2. do_bench owns its loop, so it can only cycle (timing.py:346). Cap its call count at the pool size, or note that this path is not covered.

It would also help to add the per-set memoizer and a capped pool to test_eval_adversarial.py, since the current test passes with any pool of 2 or more.

The launch-count check from #171 catches this regardless of pool size: a cached call launches zero kernels. Happy to send the launch-count check as a follow-up PR.

utsav1033 pushed a commit to utsav1033/KernelBench that referenced this pull request Oct 16, 2026
Review on ScalingIntelligence#173 showed the pool cycles once it holds fewer sets than timed
calls, and the memory cap makes that common: a memoizer that keeps the last 4
inputs got 89x on level1/19 (6.4 GB input sets, pool of 2) and 3.4x on matmul
with a T4-sized cap, under the excessive-speedup threshold.

eval.make_fresh_inputs draws a new input set (own seed, seed + 1 + call)
before each timed call, outside the timed region, in the loops the eval
controls (cuda_event, do_bench_impl, host_time). The previous set is freed
first, so only one set is live and there is no pool or memory cap. New values
on every call also defeat memoizers keyed on a fingerprint of the input
values, which copies of the same inputs would not. do_bench and
nsight_python own their loops, so they keep reusing the inputs and print a
warning.

Tests: memoizers keyed on the last input, the last 4 inputs and a value
fingerprint, each across cuda_event, do_bench_impl and host_time, plus CPU
tests for make_fresh_inputs and the timing loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
utsav1033 pushed a commit to utsav1033/KernelBench that referenced this pull request Oct 16, 2026
Review on ScalingIntelligence#173 showed the pool cycles once it holds fewer sets than timed
calls, and the memory cap makes that common: a memoizer that keeps the last 4
inputs got 89x on level1/19 (6.4 GB input sets, pool of 2) and 3.4x on matmul
with a T4-sized cap, under the excessive-speedup threshold.

eval.make_fresh_inputs draws a new input set (own seed, seed + 1 + call)
before each timed call, outside the timed region, in the loops the eval
controls (cuda_event, do_bench_impl, host_time). The previous set is freed
first, so only one set is live and there is no pool or memory cap. New values
on every call also defeat memoizers keyed on a fingerprint of the input
values, which copies of the same inputs would not. Inputs are drawn on the
device (falling back to the CPU if get_inputs cannot run there), so drawing
them per call stays cheap. do_bench and
nsight_python own their loops, so they keep reusing the inputs and print a
warning.

Tests: memoizers keyed on the last input, the last 4 inputs and a value
fingerprint, each across cuda_event, do_bench_impl and host_time, plus CPU
tests for make_fresh_inputs and the timing loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review on ScalingIntelligence#173 showed the pool cycles once it holds fewer sets than timed
calls, and the memory cap makes that common: a memoizer that keeps the last 4
inputs got 89x on level1/19 (6.4 GB input sets, pool of 2) and 3.4x on matmul
with a T4-sized cap, under the excessive-speedup threshold.

eval.make_fresh_inputs draws a new input set (own seed, seed + 1 + call)
before each timed call, outside the timed region, in the loops the eval
controls (cuda_event, do_bench_impl, host_time). The previous set is freed
first, so only one set is live and there is no pool or memory cap. New values
on every call also defeat memoizers keyed on a fingerprint of the input
values, which copies of the same inputs would not. Inputs are drawn on the
device (falling back to the CPU if get_inputs cannot run there), so drawing
them per call stays cheap. do_bench and
nsight_python own their loops, so they keep reusing the inputs and print a
warning.

Tests: memoizers keyed on the last input, the last 4 inputs and a value
fingerprint, each across cuda_event, do_bench_impl and host_time, plus CPU
tests for make_fresh_inputs and the timing loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@utsav1033 utsav1033 changed the title Rotate timed trials over distinct input sets to stop input-identity memoization (#171) Draw new inputs for every timed call to stop input memoization (#171) Oct 16, 2026
@utsav1033

Copy link
Copy Markdown
Author

Thanks for testing this on a B200 and for the thorough breakdown. You're right that cycling a capped pool doesn't hold up once a memoizer keeps more entries than the pool has sets.

I've pushed an update (e6103a0) that takes your first suggestion. The pool and the memory cap are gone. In the loops the eval controls (cuda_event, do_bench_impl, host_time), each timed call now gets a newly drawn input set (seed seed + 1 + call), drawn outside the timed region. The previous set is freed first, so only one extra set is live whatever the input size.

One change from your suggestion: I draw new inputs rather than copying the same ones. Copies would still lose to a memoizer keyed on a fingerprint of a few input values. It passes correctness, since those trials use different values, then hits on every timed call, and the launch-count check wouldn't catch it because the fingerprint read launches a kernel. Inputs are drawn directly on the device (falling back to the CPU if get_inputs can't run there), so drawing per call stays cheap: the full test run takes under 2 minutes on a T4.

do_bench and nsight_python own their loops, so I left them reusing the inputs and they print a warning when fresh inputs are requested. I went with documenting it rather than capping the call count.

Tests in test_eval_adversarial.py now cover memoizers keyed on the last input, the last 4 inputs and a value fingerprint, each across cuda_event, do_bench_impl and host_time. There's also your ReLU memoizer on a level1/19-shaped problem scaled to 1/8, and a check that peak memory doesn't grow with the number of trials.

On a T4:

case runtime reference
last-input / last-4 / fingerprint memoizers, 3 timing methods each 29.8 to 34.6 ms 29.9 to 34.7 ms
your ReLU last-4 memoizer (1/8 size) 6.96 ms 6.92 ms
control, fresh_perf_inputs=False 0.00246 ms 34.2 ms (13902x, flagged)

Peak extra memory with a 1 GiB input is 3.00 GiB for both 10 and 50 trials. I couldn't run level1/19 at full size, since a 6.4 GB input set doesn't fit on a T4 alongside its output. If you can rerun your B200 table on this version, that would be great.

And yes, please send the launch-count check! It covers what fresh inputs can't, like do_bench.

zzw4257 added a commit to zzw4257/KernelBench that referenced this pull request Oct 16, 2026
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.
@zzw4257

zzw4257 commented Oct 16, 2026

Copy link
Copy Markdown

Thanks, the per-call inputs fix it. I reran e6103a0 on a B200, including the full-size level1/19 case that didn't fit on the T4.

Your tests: test_fresh_perf_inputs.py 7/7 and test_eval_adversarial.py 15/15 pass.

The review table on this version (100 timed trials, threshold 10; columns are timing methods, values are speedup over the reference):

problem memoizer cuda_event do_bench_impl host_time do_bench
level1/1 matmul last input 1.0x 1.0x 1.0x 851x
level1/1 matmul last 4 inputs 1.0x 1.0x 1.0x 854x
level1/1 matmul value fingerprint 0.97x 0.99x 0.97x 49x
level1/19 ReLU, full size (6.4 GB per input set) last 4 inputs 1.0x 1.0x 0.99x 738x

So the eval-controlled loops are covered, and do_bench is the gap you documented.

Good catch on the fingerprint memoizer. A zero-launch rule misses it because the key read launches a D2H copy on every hit. I changed the check to compare the first call on unseen inputs with repeated calls on the same inputs and to fail when the repeated calls enqueue less GPU work. It catches all three of your memoizers under do_bench. The PR is #175.

@utsav1033

Copy link
Copy Markdown
Author

Thanks a lot for rerunning everything on the B200, especially the full-size level1/19 case I couldn't fit on a T4. Your review made this PR much better. Glad the fingerprint case helped shape #175; together the two should cover #171, including do_bench.

@utsav1033

Copy link
Copy Markdown
Author

@simonguozirui when you have a moment, this one is ready for review. It fixes the timing-loop memoization from #171 (a memoizing kernel got a 13,000x+ fake speedup) and has been verified on a B200 by the issue's author, including the full-size level1/19 case. It pairs with #175, which covers do_bench. Happy to rebase whichever lands second.

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.

The timing loop reuses one input tensor object, so a kernel that caches on input identity is timed from its cache

2 participants