Skip to content

[SOT][Exception] Fix zerodiv not raising #73030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from ...utils.exceptions import InnerError
from ...utils.magic_methods import (
BINARY_OPS,
NEED_GUARD_ZERO_DIVISION_ERROR_OPS,
UNARY_OPS,
magic_method_builtin_dispatch,
non_inplace_op_to_inplace_op,
Expand Down Expand Up @@ -85,6 +86,7 @@
)

if TYPE_CHECKING:
from ...utils.magic_methods import BinaryOp
from .variables import DataVariable, TensorVariable


Expand Down Expand Up @@ -1036,6 +1038,23 @@ def is_not_func(var: VariableBase, other: VariableBase):
)


def apply_op_with_zero_division_check(
op: BinaryOp, lhs: VariableBase, rhs: VariableBase
):

graph = lhs.graph
if op in NEED_GUARD_ZERO_DIVISION_ERROR_OPS:
call_eq = BuiltinVariable(operator.eq, graph, DanglingTracker())
zero = ConstantVariable.wrap_literal(0, graph)
rhs_eq_to_zero = call_eq(rhs, zero)
add_guard(rhs_eq_to_zero)
return VariableFactory.from_value(
op(lhs.get_py_value(), rhs.get_py_value()),
graph,
DummyTracker([lhs, rhs]),
)


# Constant
for unary_fn in UNARY_OPS:
for magic_method in magic_method_builtin_dispatch(unary_fn):
Expand All @@ -1060,11 +1079,7 @@ def is_not_func(var: VariableBase, other: VariableBase):
"ConstantVariable | NumPyNumberVariable",
),
partial(
lambda fn, var, other: VariableFactory.from_value(
fn(var.get_py_value(), other.get_py_value()),
var.graph,
tracker=DummyTracker([var, other]),
),
apply_op_with_zero_division_check,
binary_fn,
),
)
Expand Down
1 change: 0 additions & 1 deletion python/paddle/jit/sot/symbolic_shape/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:

from ..utils.magic_methods import BinaryOp, UnaryOp


Expand Down
12 changes: 12 additions & 0 deletions python/paddle/jit/sot/utils/magic_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
UNARY_OPS = set(UNARY_OPS_TO_MAGIC_NAMES.keys())


# NOTE: Both operator.pow and operator.ipow should be considered for inclusion in this list,
# as they raise ZeroDivisionError when evaluating 0^n where n < 0 (division by zero).
NEED_GUARD_ZERO_DIVISION_ERROR_OPS: list[BinaryOp] = [
operator.floordiv,
operator.truediv,
operator.mod,
operator.ifloordiv,
operator.itruediv,
operator.imod,
]


@dataclass
class MagicMethod:
name: str
Expand Down
24 changes: 24 additions & 0 deletions test/sot/test_24_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,5 +944,29 @@ def fn():
self.assert_results(fn)


class TestBuiltinFunctionRaiseExceptionGuard(TestCaseBase):
def test_guard_run(self):
def foo_floordiv(x):
1 / x

def foo_mod(x):
2 % x

self.assert_results(foo_floordiv, 1)
self.assert_exceptions(
ZeroDivisionError,
"division by zero",
foo_floordiv,
0,
)
self.assert_results(foo_mod, 10)
self.assert_exceptions(
ZeroDivisionError,
"integer (.)*modulo by zero",
foo_mod,
0,
)


if __name__ == "__main__":
unittest.main()
Loading