Skip to content

Commit 25082e4

Browse files
address PR review feedback
Fix adjacent latent same-class bug: replace_div_tensor_mode with rounding_mode=None used the generic integer-stays-integer promotion rule instead of promoting to the node output float type before dividing. Add torch.true_divide(int, int) test to directly cover the true_divide.Tensor resolver rewiring. Add torch.div(int, int, rounding_mode=None) test to cover the div_tensor_mode fix.
1 parent 629cce3 commit 25082e4

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

coreai_torch/_aten_to_core.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,20 @@ def replace_div_tensor_mode(
874874
else (node.args[2] if len(node.args) > 2 else None)
875875
)
876876

877+
if rounding_mode is None:
878+
# True divide: integer operands must promote to the node's float
879+
# output type before dividing, not the generic same-kind-stays-integer
880+
# promotion rule used by "floor"/"trunc" below.
881+
result_type = get_output_element_type_from_node(node)
882+
return coreai.broadcasting_divide(
883+
coreai.cast(x, result_type), coreai.cast(y, result_type)
884+
)
885+
877886
promoted_type = get_promoted_type(x.type, y.type)
878887
casted_x = coreai.cast(x, promoted_type)
879888
casted_y = coreai.cast(y, promoted_type)
880889

881-
if rounding_mode is None:
882-
return coreai.broadcasting_divide(casted_x, casted_y)
883-
elif rounding_mode == "floor":
890+
if rounding_mode == "floor":
884891
return coreai.broadcasting_floor_divide(casted_x, casted_y)
885892
elif rounding_mode == "trunc":
886893
# Integer division already truncates toward zero, so a plain divide

tests/ops/test_ops.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,31 @@ def forward(self, x: Tensor) -> Tensor:
15761576
await validate_numerical_output(model=DivScalarModel().eval(), x=x)
15771577

15781578

1579+
async def test_true_divide_integer_promotes_to_float() -> None:
1580+
"""aten.true_divide.Tensor on integer operands must promote to float before dividing."""
1581+
x = torch.tensor([7, -7, 3, 1], dtype=torch.int32)
1582+
y = torch.tensor([2, 2, 2, 4], dtype=torch.int32)
1583+
1584+
class TrueDivideModel(nn.Module):
1585+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1586+
return torch.true_divide(x, y)
1587+
1588+
await validate_numerical_output(model=TrueDivideModel().eval(), x=x, y=y)
1589+
1590+
1591+
async def test_div_tensor_mode_none_integer_promotes_to_float() -> None:
1592+
"""aten.div.Tensor_mode with rounding_mode=None on integer operands must
1593+
promote to float before dividing, matching aten.div.Tensor semantics."""
1594+
x = torch.tensor([7, -7, 3, 1], dtype=torch.int32)
1595+
y = torch.tensor([2, 2, 2, 4], dtype=torch.int32)
1596+
1597+
class DivTensorModeModel(nn.Module):
1598+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1599+
return torch.div(x, y, rounding_mode=None)
1600+
1601+
await validate_numerical_output(model=DivTensorModeModel().eval(), x=x, y=y)
1602+
1603+
15791604
@pytest.mark.parametrize(
15801605
"x,y",
15811606
[

0 commit comments

Comments
 (0)