[FIX] Support constexpr .to() in interpreter mode#310
Open
[FIX] Support constexpr .to() in interpreter mode#310
Conversation
2 tasks
Sanitizer Performance Benchmark
Iterations: 1 warmup + 20 measured |
6368485 to
357479a
Compare
In compiled Triton, constexpr.to(dtype) works via the compiler pipeline, but in interpreter mode constexpr has no .to() method, and raw Python scalars passed as constexprs are not wrapped. Three coupled fixes: - Monkey-patch tl.constexpr.to() for interpreter mode - Wrap scalar constexprs in tl.constexpr in _grid_executor_call - Unwrap tl.constexpr in SymbolicExpr.from_value to prevent type errors
357479a to
0524caf
Compare
# Conflicts: # tests/end_to_end/test_sanitizer.py
Jokeren
reviewed
Mar 9, 2026
triton_viz/core/patch.py
Outdated
| call_args[name] = arg | ||
| ret = arg | ||
| call_args[name] = ( | ||
| tl.constexpr(arg) if isinstance(arg, (int, float, bool)) else arg |
Member
There was a problem hiding this comment.
Why should you make it constexpr only for int, float, and bool?
…g, universal normalization, and bool support - Replace no-op _implicit_cvt shim with numpy-based cast that handles truncation (float→int) and bitcast (reinterpret bits) - Move constexpr monkey-patch from module-level to scoped _patch_constexpr() via _LangPatchScope, with hasattr guards for upstream compatibility - Add __getattr__ proxy on constexpr for attribute access (e.g. DTYPE.is_int()) - Universally wrap all constexpr args via _normalize_constexpr_arg(), not just int/float/bool, enabling dtype/tuple/custom-type meta-params - Add bool to symbolic engine's builtin_scala_types with int1 dtype inference - Add E2E tests for real cast, bitcast, dtype meta-param, tuple meta-param, grid lambda, and bool cast - Add unit tests for bool symbolic inference and constexpr patch lifecycle
Triton's semantic.cast() raises ValueError when source and destination primitive bitwidths differ for bitcast. Our interpreter-mode _constexpr_to was silently truncating via a bytes slice instead. Add an explicit size check and remove the unnecessary slice.
Unit tests assert that _constexpr_to returns a tensor with the exact destination dtype (uint8, int16, uint32, float64). E2E test verifies unsigned integer division with a constexpr denominator.
Replace _implicit_cvt return path with _typed_scalar_tensor helper that wraps the cast result using the exact target dtype. This fixes dtype demotion for uint8, int16, uint32, float64, and bool/int1 casts.
… values to grid lambda - Reject invalid fp_downcast_rounding values (e.g. "RTZ", "foo") with ValueError instead of silently falling back to rtne. - Raise OverflowError for integer literals outside [-2**63, 2**64) in _src_np_dtype, matching upstream Triton's representable range. - Pass raw host values (not tl.constexpr wrappers) to grid lambdas so isinstance checks in grid/heuristic functions behave as in real Triton.
…dth validation - Replace dst_np.type(value) with np.array(src).astype(dst) to get proper wrap/truncate semantics matching upstream Triton's cast_impl (e.g. 300->uint8=44, -1->uint32=4294967295). - Validate bitcast using Triton primitive_bitwidth instead of numpy storage itemsize, so bool(int1)->int8 bitcast correctly rejects (1 bit != 8 bits) instead of passing on storage size equality. - Extract _src_triton_dtype() mirroring upstream to_tensor type rules; _src_np_dtype() now delegates to it.
Jokeren
requested changes
Mar 21, 2026
| def _patch_constexpr(scope: _LangPatchScope) -> None: | ||
| """Patch tl.constexpr with .to() and __getattr__ for interpreter mode.""" | ||
| if not hasattr(tl.constexpr, "to"): | ||
| scope.set_attr(tl.constexpr, "to", _constexpr_to) |
Member
There was a problem hiding this comment.
There isn't a constexpr.to method. This is tensor.to
@builtin
def to(self, dtype: dtype, fp_downcast_rounding: Optional[str] = None, bitcast: bool = False, _semantic=None):
"""
Alias for :py:func:`tensor.cast`.
"""
return cast(self, dtype, fp_downcast_rounding, bitcast, _semantic=_semantic)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
constexpr.to(dtype)works via the compiler pipeline, but in interpreter modeconstexprhas no.to()methodconstexprsare not wrapped intl.constexpr.to(), wrap scalars intl.constexpr, unwrap inSymbolicExpr.from_valueTest plan
test_float_no_attr_to—eps.to(dtype)where eps is a constexpr float