Skip to content

[inductor] Render user-defined Triton kernel constexprs as importable source - #195687

Closed
bobrenjc93 wants to merge 9 commits into
gh/bobrenjc93/1106/basefrom
gh/bobrenjc93/1106/head
Closed

bobrenjc93 wants to merge 9 commits into
gh/bobrenjc93/1106/basefrom
gh/bobrenjc93/1106/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

A user-defined Triton kernel can take constexpr arguments whose values are
arbitrary Python objects (dataclasses, namedtuples, enums, nested containers).
When Inductor emits a standalone kernel-launch module it must reproduce those
constexpr values as source. Previously non-trivial values could not be rendered
faithfully, so autotune config matching could silently bake a degraded value.

This renders each constexpr value as an importable source expression: the type
is imported from its defining module and reconstructed via its repr protocol,
with the rendered expression verified to eval back to a value equal to the
original before it is used. Values whose type cannot be imported by the
generated module are rejected with an actionable error that names the offending
definition scope (a function local or main) and how to fix it.

Autotune cache matching now returns the real candidate config (preserving tuple
and Enum kwarg values) when the matching configs hold the same typed values --
a unique match or interchangeable duplicates. Distinct candidates that
serialize alike (subset kwargs, int/float or bool/int splits, which are == but
distinct Triton specializations) fall through to reconstruction from the cached
value, as before, with any tuple/Enum kwarg the candidates agree on restored;
the load re-autotunes only when the candidates disagree on the typed value
behind the cached one (MODE=Mode.A vs MODE=1). This fixes duplicate tuple-kwarg
configs re-autotuning on every warm
start.

Behavior note: this depends on the constexpr Enum-unwrapping introduced by
#189494, which exists only on main; there is no released behavior to deprecate.
IntEnum and str-mixin enum members compare equal to their unwrapped values, so
they are not degraded by JSON round-tripping and match directly; only plain
Enum members require the reconstruction path.

BC note: emitting a standalone kernel-launch module for a kernel whose
constexpr value has a type defined in main or a function local now raises
an actionable error instead of proceeding. On prior releases such a value was
rendered by whatever fallback was in effect, so a workload that relied on that
fallback and only enables standalone-module emission will now surface this as a
hard error. The fix is to move the offending type to an importable module; the
error message names the type and its scope.

Suggested review order: torch/_inductor/utils.py (the importable-type and
repr-children helpers), torch/_inductor/codegen/wrapper.py (rendering and the
decline message), then autotune_cache.py and async_compile.py.

Test Plan:

python test/inductor/test_codegen_triton.py

123 tests, 33 skipped (Triton-gated tests skip without a GPU/Triton install).

Authored with an AI assistant.

Compatibility and design notes. Rendering is structural (dataclass, attrs,
repr_args, exact builtin containers, enums, torch/tl dtypes) with a
fallback for importable types whose repr is a constructor call
(fractions.Fraction, decimal.Decimal): the argument text is kept verbatim and
accepted only if evaluating it at codegen rebuilds an equal value, so every
value #189692 accepted still renders; the one narrowing is that a
constructor-style repr whose arguments reference names the generated module
cannot bind now declines loudly at codegen instead of failing with NameError
when the generated module is loaded, and a value whose repr is a bare literal
of another type (a sympy.Integer, a numpy scalar) declines where the old path
emitted the literal; Dynamo specializes constexpr SymInts to Python ints, so
that only affects such objects passed directly as constexprs. repr_args
types are verified field-wise and do not need an eq; when they define one
it is required too, since it sees state the repr hides. The decline error names the actual
cause (unrenderable; rendered but evaluation raised, with the exception; or
rendered but not rebuilt equal) plus a specific hint
for container subclasses, init=False repr fields, NaN, and unimportable scopes.

Object-valued constexpr defaults are not rendered; the user's def-time
expression is spliced verbatim, so the only validation on defaults is that the
type is importable and its root name is bound. The expression must reference
the type by its bare root name; that contract is documented at the call site.

The async-compile worker fallback attributes a missing module to a constexpr
import when it is the import, an ancestor of it, or (for user modules only) a
descendant of it; torch and triton roots are excluded from descendant matching
since a missing submodule under them is a real error. The autotune cache
reconstructs coordesc and dynamically added winners with the typed kwarg values
their candidates hold (tuple, Enum) and only re-autotunes when no candidate
serializes to the cached value; interchangeable duplicate candidates compare
on every Config attribute, not just kwargs, by type as well as value.
LambdaFuture.result(timeout) waits on the worker future without consuming its
failure, so the in-process fallback also runs when
compile_worker_wait_timeout is set.

Co-Authored-By: Claude Fable 5.1 [email protected]

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195687

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 2 Cancelled Jobs, 20 Pending

As of commit 9d69e96 with merge base 3d3d3f4 (image):

NEW FAILURE - The following job has failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: inductor"

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Closing in favor of the canonical stack #195353-#195362 (now also #195911-#195929). Note for future readers: this change has no counterpart there yet; the code remains in this PR's history if it is wanted later.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1106/head branch November 14, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant