Skip to content

Commit fbc1b47

Browse files
Group div-family tests into a TestDiv class
Combines test_div, test_div_integer_promotes_to_float, test_div_scalar_integer_promotes_to_float, test_true_divide_integer_promotes_to_float, test_div_tensor_mode_none_integer_promotes_to_float, test_div_tensor_mode, test_true_divide, and test_true_divide_scalar into a single TestDiv class, matching the per-op class convention used elsewhere in the file (e.g. TestCopy).
1 parent 25082e4 commit fbc1b47

1 file changed

Lines changed: 134 additions & 135 deletions

File tree

tests/ops/test_ops.py

Lines changed: 134 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,166 +1519,165 @@ def forward(self, dest: Tensor, src: Tensor) -> Tensor:
15191519
)
15201520

15211521

1522-
@pytest.mark.parametrize("dynamic", [False, True])
1523-
@pytest.mark.parametrize("x", [torch.rand(2, 2)])
1524-
@pytest.mark.parametrize("y", [torch.rand(2, 2)])
1525-
async def test_div(x: Tensor, y: Tensor, dynamic: bool) -> None:
1526-
class DivModel(nn.Module):
1527-
def __init__(self) -> None:
1528-
super().__init__()
1529-
1530-
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1531-
return x / y
1532-
1533-
model = DivModel().eval()
1534-
if dynamic:
1535-
dims = _all_dims_dynamic(x)
1536-
dynamic_shapes = {"x": dims, "y": dims}
1537-
else:
1538-
dynamic_shapes = None
1539-
await validate_numerical_output(
1540-
model=model, x=x, y=y, dynamic_shapes=dynamic_shapes
1541-
)
1542-
1522+
class TestDiv:
1523+
"""Test suite for aten.div.Tensor / div.Scalar / div.Tensor_mode /
1524+
true_divide.Tensor → coreai.broadcasting_divide conversion."""
15431525

1544-
@pytest.mark.parametrize(
1545-
"x,y",
1546-
[
1547-
(
1548-
torch.tensor([7, -7, 3, 1], dtype=torch.int32),
1549-
torch.tensor([2, 2, 2, 4], dtype=torch.int32),
1550-
),
1551-
(
1552-
torch.tensor([1, 2, 3, 4], dtype=torch.int64),
1553-
torch.tensor([3, 3, 3, 3], dtype=torch.int64),
1554-
),
1555-
],
1556-
)
1557-
async def test_div_integer_promotes_to_float(x: Tensor, y: Tensor) -> None:
1558-
"""aten.div.Tensor on integer operands must promote to float before dividing."""
1559-
1560-
class DivModel(nn.Module):
1561-
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1562-
return x / y
1526+
@pytest.mark.parametrize("dynamic", [False, True])
1527+
@pytest.mark.parametrize("x", [torch.rand(2, 2)])
1528+
@pytest.mark.parametrize("y", [torch.rand(2, 2)])
1529+
async def test_div(self, x: Tensor, y: Tensor, dynamic: bool) -> None:
1530+
class DivModel(nn.Module):
1531+
def __init__(self) -> None:
1532+
super().__init__()
15631533

1564-
model = DivModel().eval()
1565-
await validate_numerical_output(model=model, x=x, y=y)
1534+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1535+
return x / y
15661536

1537+
model = DivModel().eval()
1538+
if dynamic:
1539+
dims = _all_dims_dynamic(x)
1540+
dynamic_shapes = {"x": dims, "y": dims}
1541+
else:
1542+
dynamic_shapes = None
1543+
await validate_numerical_output(
1544+
model=model, x=x, y=y, dynamic_shapes=dynamic_shapes
1545+
)
15671546

1568-
async def test_div_scalar_integer_promotes_to_float() -> None:
1569-
"""aten.div.Scalar on an integer tensor must promote to float before dividing."""
1570-
x = torch.tensor([7, -7, 3, 1], dtype=torch.int32)
1547+
@pytest.mark.parametrize(
1548+
"x,y",
1549+
[
1550+
(
1551+
torch.tensor([7, -7, 3, 1], dtype=torch.int32),
1552+
torch.tensor([2, 2, 2, 4], dtype=torch.int32),
1553+
),
1554+
(
1555+
torch.tensor([1, 2, 3, 4], dtype=torch.int64),
1556+
torch.tensor([3, 3, 3, 3], dtype=torch.int64),
1557+
),
1558+
],
1559+
)
1560+
async def test_div_integer_promotes_to_float(self, x: Tensor, y: Tensor) -> None:
1561+
"""aten.div.Tensor on integer operands must promote to float before dividing."""
15711562

1572-
class DivScalarModel(nn.Module):
1573-
def forward(self, x: Tensor) -> Tensor:
1574-
return x / 4
1563+
class DivModel(nn.Module):
1564+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1565+
return x / y
15751566

1576-
await validate_numerical_output(model=DivScalarModel().eval(), x=x)
1567+
model = DivModel().eval()
1568+
await validate_numerical_output(model=model, x=x, y=y)
15771569

1570+
async def test_div_scalar_integer_promotes_to_float(self) -> None:
1571+
"""aten.div.Scalar on an integer tensor must promote to float before dividing."""
1572+
x = torch.tensor([7, -7, 3, 1], dtype=torch.int32)
15781573

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)
1574+
class DivScalarModel(nn.Module):
1575+
def forward(self, x: Tensor) -> Tensor:
1576+
return x / 4
15831577

1584-
class TrueDivideModel(nn.Module):
1585-
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1586-
return torch.true_divide(x, y)
1578+
await validate_numerical_output(model=DivScalarModel().eval(), x=x)
15871579

1588-
await validate_numerical_output(model=TrueDivideModel().eval(), x=x, y=y)
1580+
async def test_true_divide_integer_promotes_to_float(self) -> None:
1581+
"""aten.true_divide.Tensor on integer operands must promote to float before dividing."""
1582+
x = torch.tensor([7, -7, 3, 1], dtype=torch.int32)
1583+
y = torch.tensor([2, 2, 2, 4], dtype=torch.int32)
15891584

1585+
class TrueDivideModel(nn.Module):
1586+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1587+
return torch.true_divide(x, y)
15901588

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)
1589+
await validate_numerical_output(model=TrueDivideModel().eval(), x=x, y=y)
15961590

1597-
class DivTensorModeModel(nn.Module):
1598-
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1599-
return torch.div(x, y, rounding_mode=None)
1591+
async def test_div_tensor_mode_none_integer_promotes_to_float(self) -> 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)
16001596

1601-
await validate_numerical_output(model=DivTensorModeModel().eval(), x=x, y=y)
1597+
class DivTensorModeModel(nn.Module):
1598+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1599+
return torch.div(x, y, rounding_mode=None)
16021600

1601+
await validate_numerical_output(model=DivTensorModeModel().eval(), x=x, y=y)
16031602

1604-
@pytest.mark.parametrize(
1605-
"x,y",
1606-
[
1607-
# Float tensors - mixed positive/negative values
1608-
(
1609-
torch.tensor([[3.5, -7.2], [-2.8, 9.1]]),
1610-
torch.tensor([[2.0, 3.0], [2.0, -4.0]]),
1611-
),
1612-
# Larger tensors
1613-
(
1614-
torch.rand(3, 4) * 10 - 5,
1615-
torch.rand(3, 4) * 4 + 0.5,
1616-
), # Avoid division by values near zero
1617-
# Broadcasting case
1618-
(torch.rand(2, 3, 4) * 10 - 5, torch.rand(1, 3, 1) * 4 + 0.5),
1619-
],
1620-
)
1621-
@pytest.mark.parametrize("rounding_mode", [None, "floor", "trunc"])
1622-
async def test_div_tensor_mode(x: Tensor, y: Tensor, rounding_mode: str | None) -> None:
1623-
"""Test division with different rounding modes.
1624-
1625-
aten.div.Tensor_mode(input, other, rounding_mode) supports:
1626-
- None: True division (standard floating-point division)
1627-
- "floor": Floor division (rounds toward negative infinity)
1628-
- "trunc": Truncated division (rounds toward zero)
1629-
"""
1630-
1631-
class DivTensorModeModel(nn.Module):
1632-
def __init__(self) -> None:
1633-
super().__init__()
1603+
@pytest.mark.parametrize(
1604+
"x,y",
1605+
[
1606+
# Float tensors - mixed positive/negative values
1607+
(
1608+
torch.tensor([[3.5, -7.2], [-2.8, 9.1]]),
1609+
torch.tensor([[2.0, 3.0], [2.0, -4.0]]),
1610+
),
1611+
# Larger tensors
1612+
(
1613+
torch.rand(3, 4) * 10 - 5,
1614+
torch.rand(3, 4) * 4 + 0.5,
1615+
), # Avoid division by values near zero
1616+
# Broadcasting case
1617+
(torch.rand(2, 3, 4) * 10 - 5, torch.rand(1, 3, 1) * 4 + 0.5),
1618+
],
1619+
)
1620+
@pytest.mark.parametrize("rounding_mode", [None, "floor", "trunc"])
1621+
async def test_div_tensor_mode(
1622+
self, x: Tensor, y: Tensor, rounding_mode: str | None
1623+
) -> None:
1624+
"""Test division with different rounding modes.
16341625
1635-
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1636-
return torch.div(x, y, rounding_mode=rounding_mode)
1626+
aten.div.Tensor_mode(input, other, rounding_mode) supports:
1627+
- None: True division (standard floating-point division)
1628+
- "floor": Floor division (rounds toward negative infinity)
1629+
- "trunc": Truncated division (rounds toward zero)
1630+
"""
16371631

1638-
model = DivTensorModeModel().eval()
1639-
await validate_numerical_output(model=model, x=x, y=y)
1632+
class DivTensorModeModel(nn.Module):
1633+
def __init__(self) -> None:
1634+
super().__init__()
16401635

1636+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1637+
return torch.div(x, y, rounding_mode=rounding_mode)
16411638

1642-
@pytest.mark.parametrize("dynamic", [False, True])
1643-
@pytest.mark.parametrize(
1644-
"x,y",
1645-
[
1646-
(torch.rand(2, 3) + 0.1, torch.rand(2, 3) + 0.1),
1647-
(torch.rand(3, 4, 5) + 0.1, torch.rand(3, 4, 5) + 0.1),
1648-
(torch.rand(4) + 0.1, torch.rand(4) + 0.1),
1649-
# FP16
1650-
(
1651-
torch.rand(2, 3, dtype=torch.float16) + 0.1,
1652-
torch.rand(2, 3, dtype=torch.float16) + 0.1,
1653-
),
1654-
],
1655-
)
1656-
async def test_true_divide(x: Tensor, y: Tensor, dynamic: bool) -> None:
1657-
class TrueDivideModel(nn.Module):
1658-
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1659-
return torch.true_divide(x, y)
1639+
model = DivTensorModeModel().eval()
1640+
await validate_numerical_output(model=model, x=x, y=y)
16601641

1661-
model = TrueDivideModel().eval()
1662-
if dynamic:
1663-
dims = _all_dims_dynamic(x)
1664-
dynamic_shapes = {"x": dims, "y": dims}
1665-
else:
1666-
dynamic_shapes = None
1667-
await validate_numerical_output(
1668-
model=model, x=x, y=y, dynamic_shapes=dynamic_shapes
1642+
@pytest.mark.parametrize("dynamic", [False, True])
1643+
@pytest.mark.parametrize(
1644+
"x,y",
1645+
[
1646+
(torch.rand(2, 3) + 0.1, torch.rand(2, 3) + 0.1),
1647+
(torch.rand(3, 4, 5) + 0.1, torch.rand(3, 4, 5) + 0.1),
1648+
(torch.rand(4) + 0.1, torch.rand(4) + 0.1),
1649+
# FP16
1650+
(
1651+
torch.rand(2, 3, dtype=torch.float16) + 0.1,
1652+
torch.rand(2, 3, dtype=torch.float16) + 0.1,
1653+
),
1654+
],
16691655
)
1656+
async def test_true_divide(self, x: Tensor, y: Tensor, dynamic: bool) -> None:
1657+
class TrueDivideModel(nn.Module):
1658+
def forward(self, x: Tensor, y: Tensor) -> Tensor:
1659+
return torch.true_divide(x, y)
16701660

1661+
model = TrueDivideModel().eval()
1662+
if dynamic:
1663+
dims = _all_dims_dynamic(x)
1664+
dynamic_shapes = {"x": dims, "y": dims}
1665+
else:
1666+
dynamic_shapes = None
1667+
await validate_numerical_output(
1668+
model=model, x=x, y=y, dynamic_shapes=dynamic_shapes
1669+
)
16711670

1672-
@pytest.mark.parametrize("dynamic", [False, True])
1673-
@pytest.mark.parametrize("x", [torch.rand(2, 3) + 0.1, torch.rand(3, 4, 5) + 0.1])
1674-
async def test_true_divide_scalar(x: Tensor, dynamic: bool) -> None:
1675-
class TrueDivideScalarModel(nn.Module):
1676-
def forward(self, x: Tensor) -> Tensor:
1677-
return torch.true_divide(x, 2.0)
1671+
@pytest.mark.parametrize("dynamic", [False, True])
1672+
@pytest.mark.parametrize("x", [torch.rand(2, 3) + 0.1, torch.rand(3, 4, 5) + 0.1])
1673+
async def test_true_divide_scalar(self, x: Tensor, dynamic: bool) -> None:
1674+
class TrueDivideScalarModel(nn.Module):
1675+
def forward(self, x: Tensor) -> Tensor:
1676+
return torch.true_divide(x, 2.0)
16781677

1679-
model = TrueDivideScalarModel().eval()
1680-
dynamic_shapes = {"x": _all_dims_dynamic(x)} if dynamic else None
1681-
await validate_numerical_output(model=model, x=x, dynamic_shapes=dynamic_shapes)
1678+
model = TrueDivideScalarModel().eval()
1679+
dynamic_shapes = {"x": _all_dims_dynamic(x)} if dynamic else None
1680+
await validate_numerical_output(model=model, x=x, dynamic_shapes=dynamic_shapes)
16821681

16831682

16841683
@pytest.mark.parametrize("dynamic", [False, True])

0 commit comments

Comments
 (0)