Skip to content
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
11 changes: 8 additions & 3 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,16 +925,21 @@ def aten_atan(self: TFloat) -> TFloat:
return op.Atan(self)


@torch_op("aten::atan2")
@torch_op("aten::atan2", trace_only=True)
def aten_atan2(self: TFloat, other: TFloat) -> TFloat:
"""atan2(Tensor self, Tensor other) -> Tensor"""

# self is y, and other is x on coordinate
slope = op.Div(self, other)
atan = op.Atan(slope)
zero = common_ops.constant(0.0, dtype=self.dtype)
pi = common_ops.constant(_MATH_PI, dtype=self.dtype)

second_third_quadrant = op.Where(self > 0.0, atan + _MATH_PI, atan - _MATH_PI)
result = op.Where(other < 0.0, second_third_quadrant, atan)
second_third_quadrant = op.Where(op.Greater(self, zero), atan + pi, atan - pi)
result = op.Where(op.Less(other, zero), second_third_quadrant, atan)

# Map NaN to 0 to match PyTorch behavior
result = op.Where(op.IsNaN(result), zero, result)

return result

Expand Down
2 changes: 1 addition & 1 deletion tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def _where_input_wrangler(
TorchLibOpInfo("asin", core_ops.aten_asin),
TorchLibOpInfo("asinh", core_ops.aten_asinh),
TorchLibOpInfo("atan", core_ops.aten_atan),
TorchLibOpInfo("atan2", core_ops.aten_atan2, tolerance={torch.float16: (1e-3, 1e-3)}),
TorchLibOpInfo("atan2", core_ops.aten_atan2),
TorchLibOpInfo("atanh", core_ops.aten_atanh),
TorchLibOpInfo("atleast_1d", core_ops.aten_atleast_1d).skip(
matcher=lambda sample: isinstance(sample.input, (list, tuple)),
Expand Down
Loading