Skip to content

Commit 0d1b595

Browse files
_aten_to_core: fix arange lowering — use si32 only for integer operands
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.
1 parent be19b31 commit 0d1b595

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

coreai_torch/_aten_to_core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,16 +644,21 @@ def replace_arange_start_step(
644644
else coreai.constant(1, dtype=start.type.element_type)
645645
)
646646

647-
# Keep operands as integers so coreai.range_ can infer a static shape;
648-
# cast the result to the requested dtype afterward.
647+
# When operands are integer-typed, keep them as si32 so coreai.range_ can
648+
# infer a static output shape, then cast the result to the requested dtype.
649+
# For float operands the count isn't statically determinable anyway, so
650+
# cast everything to target_type and let range_ return a dynamic shape.
649651
target_type = get_output_element_type_from_node(node)
650652
si32 = IntegerType.get_signed(32)
653+
range_type = (
654+
si32 if isinstance(start.type.element_type, IntegerType) else target_type
655+
)
651656

652657
def to_scalar(v: Value) -> Value:
653658
if v.type.rank > 0:
654659
v = coreai.shrink_dims(v, list(range(v.type.rank)))
655-
if v.type.element_type != si32:
656-
v = coreai.cast(v, si32)
660+
if v.type.element_type != range_type:
661+
v = coreai.cast(v, range_type)
657662
return v
658663

659664
result = coreai.range_(to_scalar(start), to_scalar(end), to_scalar(step))

0 commit comments

Comments
 (0)