Skip to content

[fix] Autotuner: propagate the tuned function's return value#899

Open
aryaman-gupta wants to merge 2 commits into
mainfrom
aryaman/fix-autotune-return-value
Open

[fix] Autotuner: propagate the tuned function's return value#899
aryaman-gupta wants to merge 2 commits into
mainfrom
aryaman/fix-autotune-return-value

Conversation

@aryaman-gupta

@aryaman-gupta aryaman-gupta commented Jul 24, 2026

Copy link
Copy Markdown

Summary

Autotuner._run_with_hints calls self.fn(*args, **kwargs) on both branches (with and without compiler hints) but does not return it. As a result Autotuner.__call__ always yields None, even though _run_config does return self._run_with_hints(...) and __call__ returns _run_config(...) — the value is threaded all the way up to this one method, which drops it.

Any autotuned function that produces its result as a return value (rather than writing into a caller-owned output buffer) therefore silently gets None. Callers currently work around this by allocating an output tensor themselves, passing it in, and returning that buffer instead of trusting the autotuner's return.

Fix

Add the missing return on both branches of _run_with_hints.

Backward compatible:

  • Callers that ignore the return value (in-place kernels writing into out) are unaffected — the fn still returns None, so the tuner still returns None.
  • Callers whose fn returns its result now receive it.

Test

Adds two cases to tests/unit/test_autotune.py (GPU-free):

  • test_call_returns_tuned_fn_value — the tuned fn's return value propagates through __call__ on both the search path and the cache-hit path. Fails without the fix (assert None == ('result', 64)).
  • test_call_returns_none_when_fn_returns_none — an in-place fn that returns nothing still yields None, confirming the fix only forwards the value and doesn't fabricate one.

Run:

pytest tests/unit/test_autotune.py -k return -v

_run_with_hints called self.fn(*args, **kwargs) on both branches without
returning it, so Autotuner.__call__ always yielded None even though
_run_config already returns _run_with_hints(...). Any autotuned function that
produces its result as a return value (rather than writing into a caller-owned
output buffer) silently got None. Add the missing return on both branches;
backward compatible for callers that ignore the return value.
Add a regression test that the tuned function's return value propagates through
__call__ on both the search and cache-hit paths, plus a guard that in-place
kernels (which return None) still return None. The first test fails without the
_run_with_hints fix.
@aryaman-gupta
aryaman-gupta requested a review from coderfeli July 24, 2026 15:07
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