2021-09-30 00:03:40 +08:00
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
# Also available under a BSD-style license. See LICENSE.
|
2021-06-26 08:25:09 +08:00
|
|
|
|
|
|
|
import torch
|
|
|
|
|
2021-09-28 02:36:44 +08:00
|
|
|
from torch_mlir_e2e_test.torchscript.framework import TestUtils
|
|
|
|
from torch_mlir_e2e_test.torchscript.registry import register_test_case
|
|
|
|
from torch_mlir_e2e_test.torchscript.annotations import annotate_args, export
|
2021-06-26 08:25:09 +08:00
|
|
|
|
|
|
|
# TODO: Support scalar !torch.int/!torch.float variants. Add support to
|
|
|
|
# ReduceOpVariants to implement them in terms of the tensor-only variants +
|
|
|
|
# torch.prim.NumToTensor.
|
|
|
|
|
|
|
|
# TODO: This is pretty verbose. Can we have a helper to reduce
|
|
|
|
# the boilerplate?
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
class ElementwiseUnaryModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.tanh(a)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseUnaryModule())
|
|
|
|
def ElementwiseUnaryModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
class ElementwiseBinaryModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return a * b
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseBinaryModule())
|
|
|
|
def ElementwiseBinaryModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4), tu.rand(4))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-12-29 10:11:07 +08:00
|
|
|
class ElementwiseBinaryStaticShapeModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([5, 4, 3, 3, 1], torch.float32, True),
|
|
|
|
([4, 3, 1, 2], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return a * b
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-29 10:11:07 +08:00
|
|
|
@register_test_case(
|
|
|
|
module_factory=lambda: ElementwiseBinaryStaticShapeModule())
|
|
|
|
def ElementwiseBinaryStaticShapeModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(5, 4, 3, 3, 1), tu.rand(4, 3, 1, 2))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-06-26 08:25:09 +08:00
|
|
|
class ElementwiseTernaryModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1, -1], torch.float32, True),
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b, c):
|
|
|
|
return torch.lerp(a, b, c)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseTernaryModule())
|
|
|
|
def ElementwiseTernaryModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4, 5), tu.rand(4, 5), tu.rand(5))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-12-09 04:28:50 +08:00
|
|
|
class ElementwiseWhereSelfModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1, -1], torch.float32, True),
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b, c):
|
|
|
|
return torch.where(a > 0.5, b, c)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseWhereSelfModule())
|
|
|
|
def ElementwiseWhereSelfModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4, 5), tu.rand(4, 5), tu.rand(5))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-06-26 08:25:09 +08:00
|
|
|
# Addition is an interesting special case of a binary op, because under the hood
|
|
|
|
# it carries a third scalar "alpha" parameter, which needs special handling.
|
|
|
|
class ElementwiseAddModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
([], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return a + b
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseAddModule())
|
|
|
|
def ElementwiseAddModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(4), tu.rand())
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
class ElementwiseUnsqueezeBroadcastModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
([], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return a * b.unsqueeze(0)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(
|
|
|
|
module_factory=lambda: ElementwiseUnsqueezeBroadcastModule())
|
|
|
|
def ElementwiseUnsqueezeBroadcastModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(4), tu.rand())
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-10-26 02:52:51 +08:00
|
|
|
class ElementwiseUnsqueezeNegDimsModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a):
|
|
|
|
# As mentioned in `unsqueeze` docstring,
|
|
|
|
# valid dim values are [-input.dim()-1, input.dim()+1).
|
|
|
|
# This tests the lower bound
|
|
|
|
return torch.unsqueeze(a, -3)
|
|
|
|
|
|
|
|
|
2021-12-29 10:11:07 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseUnsqueezeNegDimsModule())
|
2021-10-26 02:52:51 +08:00
|
|
|
def ElementwiseUnsqueezeNegDimsModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(4, 3))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-07-08 04:59:47 +08:00
|
|
|
class ElementwiseFlattenBroadcastModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
([], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return a * b.flatten(-1, -1)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseFlattenBroadcastModule())
|
|
|
|
def ElementwiseFlattenBroadcastModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(6), tu.rand())
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-06-26 08:25:09 +08:00
|
|
|
class ElementwiseReluModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.relu(x)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseReluModule())
|
|
|
|
def ElementwiseReluModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(4, 2) - 0.5)
|
2021-08-30 10:03:38 +08:00
|
|
|
|
|
|
|
# ==============================================================================
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-24 21:38:59 +08:00
|
|
|
class ElementwiseLeakyReluModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.ops.aten.leaky_relu(x, negative_slope=0.1)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseLeakyReluModule())
|
|
|
|
def ElementwiseLeakyReluModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(4, 2) - 0.5)
|
|
|
|
|
|
|
|
# ==============================================================================
|
2021-08-30 10:03:38 +08:00
|
|
|
|
2021-10-26 07:16:01 +08:00
|
|
|
class ElementwiseGeluModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.gelu = torch.nn.GELU()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return self.gelu(x)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseGeluModule())
|
|
|
|
def ElementwiseGeluModule_basic(module, tu: TestUtils):
|
2021-12-29 10:11:07 +08:00
|
|
|
module.forward(2 * tu.rand(5, 3) - 0.5)
|
2021-10-26 07:16:01 +08:00
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-08-30 10:03:38 +08:00
|
|
|
class ElementwiseSigmoidModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.sigmoid(x)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseSigmoidModule())
|
|
|
|
def ElementwiseSigmoidModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 5))
|
|
|
|
|
2021-09-02 02:58:31 +08:00
|
|
|
# ==============================================================================
|
2021-10-27 11:44:01 +08:00
|
|
|
|
|
|
|
class ElementwiseMinimumModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x, y):
|
|
|
|
return torch.minimum(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMinimumModule())
|
|
|
|
def ElementwiseMinimumModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 5), tu.rand(3, 5))
|
|
|
|
module.forward(tu.nans(3, 5), tu.rand(3, 5))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
class ElementwiseMaximumModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x, y):
|
|
|
|
return torch.maximum(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMaximumModule())
|
|
|
|
def ElementwiseMaximumModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 5), tu.rand(3, 5))
|
|
|
|
module.forward(tu.nans(3, 5), tu.rand(3, 5))
|
|
|
|
|
|
|
|
# ==============================================================================
|
2021-12-09 04:28:50 +08:00
|
|
|
|
2021-10-27 11:44:01 +08:00
|
|
|
class ElementwiseClampModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
# TODO: It would be great to return all of these, so they get checked
|
|
|
|
# individually, but RefBackend doesn't support multiple returns.
|
|
|
|
# Instead, multiply them together, which has some chance of propagating
|
|
|
|
# all the values.
|
|
|
|
float_min = torch.clamp(x, min=-2.0)
|
|
|
|
int_min = torch.clamp(x, min=-3)
|
|
|
|
float_max = torch.clamp(x, max=2.0)
|
|
|
|
int_max = torch.clamp(x, max=3)
|
|
|
|
both = torch.clamp(x, min=-5, max=5)
|
|
|
|
return float_min * int_min * float_max * int_max * both
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseClampModule())
|
|
|
|
def ElementwiseClampModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 5, low=-10, high=10))
|
2021-11-02 15:14:23 +08:00
|
|
|
|
2021-11-01 19:46:46 +08:00
|
|
|
# ==============================================================================
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-02 15:14:23 +08:00
|
|
|
class RsubModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.rsub(x, 3.0, alpha=1.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-02 15:14:23 +08:00
|
|
|
@register_test_case(module_factory=lambda: RsubModule())
|
|
|
|
def RsubModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-11-02 15:14:23 +08:00
|
|
|
class RsubModule_noalpha(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.rsub(x, 2.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-02 15:14:23 +08:00
|
|
|
@register_test_case(module_factory=lambda: RsubModule_noalpha())
|
|
|
|
def RsubModule_noalpha_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-12-29 10:11:07 +08:00
|
|
|
|
2021-11-11 17:02:13 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-12-10 22:55:47 +08:00
|
|
|
class ElementwiseMulScalarIntModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-12-10 22:55:47 +08:00
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.int64, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.mul(x, 4)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-10 22:55:47 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMulScalarIntModule())
|
|
|
|
def ElementwiseMulScalarModule_int(module, tu: TestUtils):
|
|
|
|
module.forward(torch.randint(10, (3, 4)))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-10 22:55:47 +08:00
|
|
|
|
|
|
|
class ElementwiseMulScalarFloatModule(torch.nn.Module):
|
2021-11-11 17:02:13 +08:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.mul(x, 100.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-10 22:55:47 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMulScalarFloatModule())
|
|
|
|
def ElementwiseMulScalarModule_float(module, tu: TestUtils):
|
2021-11-11 17:02:13 +08:00
|
|
|
module.forward(tu.rand(3, 4))
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-12-10 22:55:47 +08:00
|
|
|
class ElementwiseMulScalarModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
2022-02-12 04:30:02 +08:00
|
|
|
([-1, -1], torch.int32, True),
|
2021-12-10 22:55:47 +08:00
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.mul(x, 8.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-10 22:55:47 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMulScalarModule())
|
|
|
|
def ElementwiseMulScalarModule_basic(module, tu: TestUtils):
|
2022-02-12 04:30:02 +08:00
|
|
|
module.forward(torch.randint(10, (3, 4), dtype=torch.int32))
|
2021-12-10 22:55:47 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
|
|
|
class ElementwiseMulTensorFloatModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
([-1], torch.float64, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return torch.mul(a, b)
|
|
|
|
|
|
|
|
|
2021-12-29 10:11:07 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMulTensorFloatModule())
|
2021-12-04 03:51:25 +08:00
|
|
|
def ElementwiseMulTensorFloatModule_basic(module, tu: TestUtils):
|
2021-12-29 10:11:07 +08:00
|
|
|
module.forward(tu.rand(4), tu.rand(4).type(torch.float64))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
|
|
|
class ElementwiseMulTensorIntModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.int32, True),
|
|
|
|
([-1], torch.int64, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return torch.mul(a, b)
|
|
|
|
|
|
|
|
|
2021-12-29 10:11:07 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseMulTensorIntModule())
|
2021-12-04 03:51:25 +08:00
|
|
|
def ElementwiseMulTensorIntModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(
|
2021-12-29 10:11:07 +08:00
|
|
|
torch.randint(10, [4]).type(torch.int32), torch.randint(10, [4]))
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-11-01 19:46:46 +08:00
|
|
|
# ==============================================================================
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-02 23:38:13 +08:00
|
|
|
class ElementwiseLogModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.log(a)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseLogModule())
|
|
|
|
def ElementwiseLogModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-05 07:53:06 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-11-05 07:53:06 +08:00
|
|
|
|
|
|
|
class ElementwiseSqrtModule(torch.nn.Module):
|
2021-12-29 10:11:07 +08:00
|
|
|
def __init__(self):
|
2021-11-05 07:53:06 +08:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.sqrt(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-05 07:53:06 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseSqrtModule())
|
|
|
|
def ElementwiseSqrtModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-06 22:19:01 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-11-06 22:19:01 +08:00
|
|
|
class ElementwiseFloorModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.floor(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-06 22:19:01 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseFloorModule())
|
|
|
|
def ElementwiseFloorModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-08 16:24:12 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-12-12 01:20:24 +08:00
|
|
|
class ElementwiseCeilModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.ceil(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-12 01:20:24 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseCeilModule())
|
|
|
|
def ElementwiseCeilModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-11-08 16:24:12 +08:00
|
|
|
class ElementwisePowModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.pow(a, 2.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-08 16:24:12 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwisePowModule())
|
|
|
|
def ElementwisePowModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-01 19:46:46 +08:00
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
class ElementwiseToDtypeF32ToI64Module(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True)
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return x.to(torch.int64)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-01 19:46:46 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseToDtypeF32ToI64Module())
|
|
|
|
def ElementwiseToDtypeF32ToI64Module_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 5))
|
2021-11-08 16:03:22 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-12-23 20:04:29 +08:00
|
|
|
class ElementwiseToDtypeIdentityModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True)
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return x.to(torch.float32, False, False)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-23 20:04:29 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseToDtypeIdentityModule())
|
|
|
|
def ElementwiseToDtypeIdentityModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 5))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-11-08 16:03:22 +08:00
|
|
|
class ElementwiseLog2Module(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.log2(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-08 16:03:22 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseLog2Module())
|
|
|
|
def ElementwiseLog2Module_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-09 16:34:23 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2021-11-09 16:34:23 +08:00
|
|
|
class ElementwiseRsqrtModule(torch.nn.Module):
|
2021-12-29 10:11:07 +08:00
|
|
|
def __init__(self):
|
2021-11-09 16:34:23 +08:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.rsqrt(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-09 16:34:23 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseRsqrtModule())
|
|
|
|
def ElementwiseRsqrtModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-23 14:54:31 +08:00
|
|
|
|
|
|
|
# ==============================================================================
|
2021-11-30 14:46:51 +08:00
|
|
|
|
|
|
|
class ElementwiseAbsModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.abs(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-30 14:46:51 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseAbsModule())
|
|
|
|
def ElementwiseAbsModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4, 5, low=-1.0, high=1.0))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
class ElementwiseReciprocalModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
])
|
|
|
|
|
|
|
|
def forward(self, a):
|
|
|
|
return torch.reciprocal(a)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-11-30 14:46:51 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseReciprocalModule())
|
|
|
|
def ElementwiseReciprocalModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(4))
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
2021-11-23 14:54:31 +08:00
|
|
|
class ElementwiseDivScalarModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.div(x, 10.0)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseDivScalarModule())
|
|
|
|
def ElementwiseDivScalarModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2021-11-24 00:24:47 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
|
|
|
class ElementwiseDivTensorFloatModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1], torch.float32, True),
|
|
|
|
([-1], torch.float64, True),
|
|
|
|
])
|
|
|
|
def forward(self, a, b):
|
|
|
|
return torch.div(a, b)
|
|
|
|
|
|
|
|
|
2021-12-29 10:11:07 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseDivTensorFloatModule())
|
2021-12-04 03:51:25 +08:00
|
|
|
def ElementwiseDivTensorFloatModule_basic(module, tu: TestUtils):
|
2021-12-29 10:11:07 +08:00
|
|
|
module.forward(tu.rand(4), tu.rand(4).type(torch.float64))
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-11-24 00:24:47 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-11-24 00:24:47 +08:00
|
|
|
class ElementwiseAndIntegerModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.int32, True),
|
|
|
|
([-1, -1], torch.int64, True),
|
|
|
|
])
|
|
|
|
def forward(self, x, y):
|
|
|
|
return torch.bitwise_and(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseAndIntegerModule())
|
|
|
|
def ElementwiseAndIntegerModule_basic(module, tu: TestUtils):
|
2021-12-29 10:11:07 +08:00
|
|
|
module.forward(
|
|
|
|
torch.randint(-10, 10, (3, 4)).to(torch.int32),
|
|
|
|
torch.randint(-10, 10, (3, 4)))
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-04 03:51:25 +08:00
|
|
|
|
2021-12-22 22:52:20 +08:00
|
|
|
class ElementwiseSubScalarIntModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
2022-02-12 04:30:02 +08:00
|
|
|
([-1, -1], torch.int32, True),
|
2021-12-22 22:52:20 +08:00
|
|
|
])
|
|
|
|
def forward(self, x):
|
2021-12-29 10:11:07 +08:00
|
|
|
return torch.sub(x, 2.1, alpha=2)
|
|
|
|
|
2021-12-22 22:52:20 +08:00
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseSubScalarIntModule())
|
|
|
|
def ElementwiseSubScalarIntModule_basic(module, tu: TestUtils):
|
2022-02-12 04:30:02 +08:00
|
|
|
module.forward(torch.randint(10, (3, 4), dtype=torch.int32))
|
2021-12-22 22:52:20 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-22 22:52:20 +08:00
|
|
|
|
|
|
|
class ElementwiseSubScalarFloatModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.sub(x, 2.1)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-22 22:52:20 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseSubScalarFloatModule())
|
|
|
|
def ElementwiseSubScalarFloatModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
|
|
|
|
2022-02-12 04:30:02 +08:00
|
|
|
class ElementwiseAddScalarInt64Module(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.int64, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.add(x, 3.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2022-02-12 04:30:02 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseAddScalarInt64Module())
|
|
|
|
def ElementwiseAddScalarInt64Module_basic(module, tu: TestUtils):
|
|
|
|
module.forward(torch.randint(10, (3, 4)))
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-22 22:52:20 +08:00
|
|
|
|
|
|
|
class ElementwiseAddScalarIntModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
2022-02-12 04:30:02 +08:00
|
|
|
([-1, -1], torch.int32, True),
|
2021-12-22 22:52:20 +08:00
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.add(x, 3.0)
|
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
|
2021-12-22 22:52:20 +08:00
|
|
|
@register_test_case(module_factory=lambda: ElementwiseAddScalarIntModule())
|
|
|
|
def ElementwiseAddScalarIntModule_basic(module, tu: TestUtils):
|
2022-02-12 04:30:02 +08:00
|
|
|
module.forward(torch.randint(10, (2, 3), dtype=torch.int32))
|
2021-12-22 22:52:20 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2021-12-22 22:52:20 +08:00
|
|
|
|
|
|
|
class ElementwiseAddScalarFloatModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
2021-12-29 10:11:07 +08:00
|
|
|
return torch.add(x, 3.0, alpha=2)
|
|
|
|
|
2021-12-22 22:52:20 +08:00
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseAddScalarFloatModule())
|
|
|
|
def ElementwiseAddScalarFloatModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(3, 4))
|
2022-02-10 11:31:03 +08:00
|
|
|
|
2022-02-15 03:46:38 +08:00
|
|
|
# ==============================================================================
|
2022-02-10 11:31:03 +08:00
|
|
|
|
|
|
|
class ElementwiseCloneModule(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@export
|
|
|
|
@annotate_args([
|
|
|
|
None,
|
|
|
|
([-1, -1, -1], torch.float32, True),
|
|
|
|
])
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.clone(x)
|
|
|
|
|
|
|
|
|
|
|
|
@register_test_case(module_factory=lambda: ElementwiseCloneModule())
|
|
|
|
def ElementwiseCloneModule_basic(module, tu: TestUtils):
|
|
|
|
module.forward(tu.rand(2, 3, 4))
|