Skip to content
Open
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
8 changes: 8 additions & 0 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8533,6 +8533,14 @@ def aten_trunc(self: TFloat) -> TFloat:
return op.Floor(op.Abs(self)) * op.Sign(self)


@torch_op("math::trunc", trace_only=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to use a single cast to do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single cast to INT64 should do the truncation

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying that the discussion referenced below (onnx issue 4588) is not relevant now?

Surprisingly, the onnx Cast op documentation does not seem to say what it does for float to int conversion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we don't expect overflows for symfloats, I think this is ok. onnx#4588 is more rigorous and the impl is used for the aten op.

Cast op: there was a spec clarification issue I remember

def python_math_trunc(self: TFloat) -> TInt:
"""trunc(Tensor self) -> Tensor"""
# Reference https://github.com/onnx/onnx/issues/4588#issuecomment-2658170591
result = op.Floor(op.Abs(self)) * op.Sign(self)
return op.Cast(result, to=INT64.dtype)


@torch_op("aten::type_as", trace_only=True)
def aten_type_as(self: TTensor, other: TTensor2) -> TTensor2:
"""type_as(Tensor self, Tensor other) -> Tensor"""
Expand Down
Loading