Skip to content
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

AddOp(upsample_bicubic2d) | feat(torchlib) #1208

Merged
merged 21 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
81 changes: 75 additions & 6 deletions onnxscript/function_libs/torch_lib/ops/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2197,16 +2197,85 @@
raise NotImplementedError()


@torch_op(("aten::upsample_bicubic2d", "aten::upsample_bicubic2d.vec"), trace_only=True)
def aten_upsample_bicubic2d(
self: TensorType,
self: TReal,
output_size: INT64,
align_corners: bool,
scales_h: Optional[float] = None,
scales_w: Optional[float] = None,
) -> TensorType:
"""upsample_bicubic2d(Tensor self, SymInt[2] output_size, bool align_corners, float? scales_h=None, float? scales_w=None) -> Tensor"""
scales_factors: Optional[TFloat] = None,
) -> TReal:
"""upsample_bicubic2d.vec(Tensor input, SymInt[]? output_size, bool align_corners, float[]? scale_factors) -> Tensor
Fixed Show fixed Hide fixed
upsample_bicubic2d(Tensor self, SymInt[2] output_size, bool align_corners, float? scales_h=None, float? scales_w=None) -> Tensor"""
Fixed Show fixed Hide fixed

raise NotImplementedError()
if output_size is not None:
result = _aten_upsample_output_size(self, output_size, align_corners, "cubic")
Fixed Show fixed Hide fixed
else:
Fixed Show fixed Hide fixed
result = _aten_upsample_scales(self, scales, align_corners, "cubic")
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
return result


@torch_op("aten::upsample_bicubic2d", private=True)
def _aten_upsample_output_size(
self: TReal,
output_size: INT64,
align_corners: bool,
str_mode: str,
) -> TReal:
self_shape = op.Shape(self)
starts = op.Constant(value_ints=[0])
ends = op.Constant(value_ints=[2])
batch_channel = op.Slice(self_shape, starts, ends)
output_size = op.Concat(batch_channel, output_size, axis=0)

Check warning on line 2228 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2224-L2228

Added lines #L2224 - L2228 were not covered by tests
if align_corners:
result = op.Resize(

Check warning on line 2230 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2230

Added line #L2230 was not covered by tests
self,
None,
None,
output_size,
mode=str_mode,
coordinate_transformation_mode="align_corners",
)
else:
result = op.Resize(

Check warning on line 2239 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2239

Added line #L2239 was not covered by tests
self,
None,
None,
output_size,
mode=str_mode,
coordinate_transformation_mode="pytorch_half_pixel",
)

return result

Check warning on line 2248 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2248

Added line #L2248 was not covered by tests


@torch_op("aten::upsample_bicubic2d", private=True)
def _aten_upsample_scales(
self: TReal,
scales: TFloat,
align_corners: bool,
str_mode: str,
) -> TReal:
scales = op.Cast(scales, to=FLOAT.dtype)
scales = op.Concat(op.Constant(value_floats=[1.0, 1.0]), scales, axis=0)

Check warning on line 2259 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2258-L2259

Added lines #L2258 - L2259 were not covered by tests
if align_corners:
result = op.Resize(

Check warning on line 2261 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2261

Added line #L2261 was not covered by tests
self,
None,
scales, # format should be: [1.0, 1.0, scale_h, scale_w]
None,
mode=str_mode,
coordinate_transformation_mode="align_corners",
)
else:
result = op.Resize(

Check warning on line 2270 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2270

Added line #L2270 was not covered by tests
self,
None,
scales, # format should be: [1.0, 1.0, scale_h, scale_w]
None,
mode=str_mode,
coordinate_transformation_mode="pytorch_half_pixel",
)
return result

Check warning on line 2278 in onnxscript/function_libs/torch_lib/ops/nn.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/function_libs/torch_lib/ops/nn.py#L2278

Added line #L2278 was not covered by tests


def aten_upsample_bicubic2d_backward(
Expand Down
58 changes: 58 additions & 0 deletions onnxscript/tests/function_libs/torch_lib/extra_opinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,57 @@ def sample_inputs_unfold(op_info, device, dtype, requires_grad, **kwargs):
yield opinfo_core.SampleInput(t, args=(dimension, size, step))


def sample_inputs_upsample_bicubic2d(op_info, device, dtype, requires_grad, **kwargs):
del op_info
del kwargs

N, C = 2, 3
D = 4
SS = 3
L = 5

align_corners_options = (True, False)
rank = 2

def shape(size, rank, with_batch_channel=True):
if with_batch_channel:
return tuple([N, C] + ([size] * rank))
return tuple([size] * rank)

make_arg = functools.partial(
torch_testing.make_tensor,
device=device,
dtype=dtype,
requires_grad=requires_grad,
low=-1,
high=1,
)

yield opinfo_core.SampleInput(make_arg(shape(D, rank)), shape(SS, rank, False), True)

for align_corners in align_corners_options:
yield opinfo_core.SampleInput(
make_arg(shape(D, rank)), shape(S, rank, False), align_corners
)
yield opinfo_core.SampleInput(
make_arg(shape(D, rank)),
shape(L, rank, False),
align_corners,
)
yield opinfo_core.SampleInput(
make_arg(shape(D, rank)),
None, # output_size
align_corners,
(1.7, 1.7), # scaler
)
yield opinfo_core.SampleInput(
make_arg(shape(D, rank)),
None, # if this is None, the scalar must be list
align_corners,
(0.6, 0.6),
)


class _TestParamsMaxPoolEmptyStrideBase:
# Adapted from https://github.com/pytorch/pytorch/blob/d6d55f8590eab05d2536756fb4efcfb2d07eb81a/torch/testing/_internal/common_methods_invocations.py#L3203
def __init__(self):
Expand Down Expand Up @@ -1874,6 +1925,13 @@ def __init__(self):
sample_inputs_func=sample_inputs_unfold,
supports_out=False,
),
opinfo_core.OpInfo(
"ops.aten.upsample_bicubic2d",
aten_name="upsample_bicubic2d",
dtypes=common_dtype.floating_types_and(torch.bfloat16),
sample_inputs_func=sample_inputs_upsample_bicubic2d,
supports_out=False,
),
opinfo_core.OpInfo(
"nn.functional.max_pool1d_with_indices",
aten_name="max_pool1d_with_indices",
Expand Down
5 changes: 5 additions & 0 deletions onnxscript/tests/function_libs/torch_lib/ops_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,11 @@ def _where_input_wrangler(
input_wrangler=_upsample_bilinear2d_input_wrangler,
trace_only=True,
),
TorchLibOpInfo(
"ops.aten.upsample_bicubic2d",
nn_ops.aten_upsample_bicubic2d,
trace_only=True,
),
TorchLibOpInfo(
"nn.functional.upsample_nearest2d",
nn_ops.aten_upsample_nearest2d,
Expand Down
Loading