fix(arange): preserve static output shape for float-dtype arange with integer bounds#25
Merged
gokulkrishna98 merged 2 commits intoJun 29, 2026
Conversation
… dtypes Cast range_ operands to si32 instead of the output dtype, so the compiler can infer a static element count from constant int bounds. Cast the result to the requested dtype afterward. Previously casting operands to float caused range_ to return tensor<?xT> even for compile-time constants, breaking composite signatures that expected a static dimension.
Float operands (e.g. arange(0.5, 5.0, 0.5)) must not be cast to si32 as that truncates the values. Only apply the integer-path optimisation when start has an integer element type; fall back to target_type for float operands where a dynamic shape is correct anyway.
DawerG
approved these changes
Jun 29, 2026
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.
Problem
torch.arange(start, end, step, dtype=torch.float32)was producing a dynamic-shape output (tensor<?xf32>) even when all three bounds were compile-time integer constants.Root cause:
replace_arange_start_stepwas casting each operand to the FX node's output dtype (e.g.f32) before callingcoreai.range_. Whenrange_receives float operands it cannot statically determine the element count, so it returnstensor<?xf32>regardless of whether the values are constants.This propagated through composites that use
arangeto build position indices (e.g. RoPE), corrupting their output type signature from a static to a dynamic dimension and breaking downstream composites that expected a static shape.Fix
For integer operands (the common case — e.g.
arange(0, head_dim, 2, dtype=float32)): keep all three operands assi32for therange_call. The compiler can statically count integer steps, so the result istensor<NxSI32>with a knownN. Cast the result to the requested output dtype afterward.For float operands (e.g.
arange(0.5, 5.0, 0.5)): keep the original behaviour — cast totarget_typebeforerange_. A dynamic shape is correct here since floating-point step sizes don't have a statically determinable count, and truncating values like0.5 → 0viasi32would produce wrong results.Tests
TestArangeIR::test_dynamicFileCheck patterns to reflect the new IR (si32 operands →range_→ si32 result → cast to f32).TestArangeIR::test_static_float_dtype_preserves_shapeas a direct regression test:arange(0, 8, 2, dtype=float32)must lower to a statictensor<4xf32>, nottensor<?xf32>.arange(0.0, 5.0, 0.5),arange(0.5, 5.0, 0.5)).