Repository navigation
Conversation
Triton's interpreter runs `and`/`or` in kernel code with Python semantics and treats any tensor with more than one element as true, so `(offs >= lo) and (offs < hi)` returned `offs < hi`, and `a or b` returned `a`. Every client inherited this: the sanitizer reported out-of-bounds accesses that the compiled kernel masks off, and the tracer computed different values. Compiled Triton short-circuits only on compile-time values and combines runtime operands with logical_and and logical_or. After Triton's own AST rewrite, rewrite each BoolOp of a traced function into a native comparison chain over `_BoolOpChain` objects, as Triton's interpreter does for this in triton-lang/triton#12150: operands are still evaluated once, left to right and in their own scope, and the chain stops only at a compile-time value that decides the result. A Triton whose interpreter rewrites BoolOps itself keeps its own rewrite. The helper is bound like the loop wrapper, as a hidden keyword-only default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Performance Benchmark
Iterations: 1 warmup + 20 measured |
This branch has not been deployed
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
Under every client,
and/oron tensors follow Python semantics instead of Triton's:Triton's interpreter, which TileLens builds on, runs kernel code as Python and treats any tensor with more than one element as true, so
a and breturnsbanda or breturnsa. Compiled Triton short-circuits only on compile-time values and combines runtime operands withlogical_and/logical_or. In the kernel above, the sanitizer reports the load of lanesoffs < loas out of bounds, though the compiled kernel masks them off, and the tracer computes different values. Many kernels in the wild write masks this way; on TritonBench, with the other open sanitizer fixes applied, it causes all the remaining false positives of the sanitizer.triton-lang/triton#12150 fixes this in the interpreter; this PR applies the same rewrite under TileLens for Tritons without it (3.8.0 included):
_visit_triton_function_defruns_BoolOpRewriterafter Triton's own AST rewrite. It turns eacha and binto the native chainop(True) < op(True, a) < op(True, b)over_BoolOpChainobjects, so operands are still evaluated once, left to right and in their own scope, and the chain stops only at a compile-time value that decides the result; runtime tensors are combined withlogical_and/logical_or.visit_Assigndoes not visit assigned values (so avisit_BoolOpon Triton's transformer never seesx = a and b), and so that a Tritonvisit_Comparecannot rewrite the chains again. If Triton'sASTTransformerdefinesvisit_BoolOp, TileLens leaves BoolOps to it._tilelens_bool_opis bound like_tilelens_loop_iter_wrapper, as a hidden keyword-only default, so no name is added to the user's globals;_hidden_kernel_argslists both for the function-def rewrite,_compile_and_exec, and_prepare_grid_launch.As for loops, a device function that is called as a plain
JITFunction(not traced, not interpreted) is not rewritten.Tests
tests/end_to_end/test_tracer.py:test_and_or_on_tensors_follow_compiled_triton, 14 cases (and,or, chains, nesting, and constexpr operands, on vectors and scalars) that store the result under the tracer and compare it with the logical operators. The 5 vector cases fail on main. Native runs of the same kernel on an RTX 4090 give the expected values with Triton 3.8.0 and 3.7.1.tests/end_to_end/test_sanitizer.py:test_sanitizer_and_or_on_tensors_follow_compiled_triton, the kernel above. It fails on main with a spurious out-of-bounds report onx_ptr.pytest tests/: 366 passed, 6 skipped on main; 381 passed, 6 skipped with this PR (Python 3.12, Triton 3.8.0).ruff checkandruff format --check(v0.1.9) pass on the changed files.🤖 Generated with Claude Code