Skip to content

Commit

Permalink
[torchlib] Fix implementation for clamp_max / clamp_min (#1765)
Browse files Browse the repository at this point in the history
Update clamp_max and clamp_min. Remove support for size-0 inputs to
simplify the implementations. Fixed registration to make the operators
discoverable.
  • Loading branch information
justinchuby authored Jul 30, 2024
1 parent 19f1126 commit a72f048
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
36 changes: 14 additions & 22 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,40 +1660,32 @@ def aten_clamp(self: TReal, min: Optional[TReal] = None, max: Optional[TReal] =
return clamped


@torch_op("aten::clamp_max", traceable=True)
@torch_op(("aten::clamp_max", "aten::clamp_max.Tensor"), traceable=True)
def aten_clamp_max(self: TReal, max_: TReal) -> TReal:
"""clamp_max(Tensor self, Tensor max) -> Tensor"""

self_size = op.Size(self)
max_shape = op.Shape(max_)
max_rank = op.Size(max_shape)
if self_size == 0:
result = op.Expand(self, max_shape)
# This implementation does not intent to handle when self is an empty tensor
max_rank = Rank(max_)
if max_rank == 0:
max_ = op.CastLike(max_, self)
result = op.Clip(self, None, max_)
else:
if max_rank == 0:
max_ = op.CastLike(max_, self)
result = op.Clip(self, None, max_)
else:
result = op.Min(self, max_)
result = op.Min(self, max_)

return result


@torch_op("aten::clamp_min", traceable=True)
@torch_op(("aten::clamp_min", "aten::clamp_min.Tensor"), traceable=True)
def aten_clamp_min(self: TReal, min_: TReal) -> TReal:
"""clamp_min(Tensor self, Tensor min) -> Tensor"""

self_size = op.Size(self)
min_shape = op.Shape(min_)
min_rank = op.Size(min_shape)
if self_size == 0:
result = op.Expand(self, min_shape)
# This implementation does not intent to handle when self is an empty tensor
min_rank = Rank(min_)
if min_rank == 0:
min_ = op.CastLike(min_, self)
result = op.Clip(self, min_, None)
else:
if min_rank == 0:
min_ = op.CastLike(min_, self)
result = op.Clip(self, min_, None)
else:
result = op.Max(self, min_)
result = op.Max(self, min_)

return result

Expand Down
19 changes: 12 additions & 7 deletions tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import copy
import dataclasses
import functools
import sys
from typing import Any, Callable, Collection, Optional

import numpy as np
Expand Down Expand Up @@ -713,19 +712,25 @@ def _where_input_wrangler(
dtypes=(torch.bool,),
reason="fixme: ORT does not implement SplitToSequence for bool inputs: https://github.com/microsoft/onnxruntime/issues/16905",
),
TorchLibOpInfo("clamp_max", core_ops.aten_clamp).skip(
enabled_if=sys.version_info[:2] >= (3, 9) or sys.platform != "win32",
reason="fails in this particular case",
),
TorchLibOpInfo("clamp_max", core_ops.aten_clamp_max).skip(
TorchLibOpInfo("clamp_max", core_ops.aten_clamp_max)
.skip(
matcher=lambda sample: len(sample.input.shape) == 0,
enabled_if=version_utils.onnxruntime_older_than("1.16"),
reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492",
)
.skip(
reason="Size 0 inputs are not handled by design",
matcher=lambda sample: sample.input.numel() == 0,
),
TorchLibOpInfo("clamp_min", core_ops.aten_clamp_min).skip(
TorchLibOpInfo("clamp_min", core_ops.aten_clamp_min)
.skip(
matcher=lambda sample: len(sample.input.shape) == 0,
enabled_if=version_utils.onnxruntime_older_than("1.16"),
reason="fixme (core dump): ORT aborts on scalar inputs to Reduce*-18. https://github.com/microsoft/onnxruntime/issues/16492",
)
.skip(
reason="Size 0 inputs are not handled by design",
matcher=lambda sample: sample.input.numel() == 0,
),
TorchLibOpInfo("clone", core_ops.aten_clone),
TorchLibOpInfo("complex", core_ops.aten_complex),
Expand Down

0 comments on commit a72f048

Please sign in to comment.