Skip to content

[torchlib] Implement floor_divide for int inputs #2343

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 1 commit into from
May 27, 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
13 changes: 13 additions & 0 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,19 @@
return op.Floor(op.Div(self, other))


@torch_op("aten::floor_divide", trace_only=True)
def aten_floor_divide_int(self: TInt, other: TInt) -> TInt:
"""floor_divide(Tensor self, Tensor other) -> Tensor"""

# TODO(justinchuby): This can be simplified if we can constrain the
# inputs to be positive integers. Consider how we can embed constraints in the model.
dtype = self.dtype
self = op.Cast(self, to=FLOAT.dtype)
other = op.Cast(other, to=FLOAT.dtype)
result = op.Floor(op.Div(self, other))
return op.Cast(result, to=dtype)

Check warning on line 3667 in onnxscript/function_libs/torch_lib/ops/core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/core.py#L3663-L3667

Added lines #L3663 - L3667 were not covered by tests


@torch_op("_operator::floordiv", trace_only=True)
def operator_floordiv(self: INT64, other: INT64) -> INT64:
# We implement floor_divide only for positive inputs (using integer division)
Expand Down
1 change: 1 addition & 0 deletions tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ def _where_input_wrangler(
TorchLibOpInfo("flatten", core_ops.aten_flatten),
TorchLibOpInfo("floor", core_ops.aten_floor),
TorchLibOpInfo("ops.aten.floor_divide", core_ops.aten_floor_divide),
TorchLibOpInfo("ops.aten.floor_divide.int", core_ops.aten_floor_divide_int),
TorchLibOpInfo("fmod", core_ops.aten_fmod),
TorchLibOpInfo("frac", core_ops.aten_frac),
TorchLibOpInfo("full", core_ops.aten_full),
Expand Down
Loading