OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
# Copyright (C) 2018-2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
import torch
import torch.nn.functional as F
from pytorch_layer_test_class import PytorchLayerTest
OPS = {
"aten::abs": torch.abs,
"aten::abs_": torch.abs_,
"aten::rsqrt": torch.rsqrt,
"aten::rsqrt_": torch.rsqrt_,
"aten::sqrt": torch.sqrt,
"aten::sqrt_": torch.sqrt_,
"aten::erf": torch.erf,
"aten::erf_": torch.erf_,
"aten::erfc": torch.erfc,
"aten::erfc_": torch.erfc_,
"aten::erfinv": torch.erfinv,
"aten::exp": torch.exp,
"aten::exp_": torch.exp_,
"aten::expm1": torch.expm1,
"aten::expm1_": torch.expm1_,
"aten::relu": torch.relu,
"aten::relu_": torch.relu_,
"aten::ceil": torch.ceil,
"aten::ceil_": torch.ceil_,
"aten::floor": torch.floor,
"aten::floor_": torch.floor_,
"aten::trunc": torch.trunc,
"aten::trunc_": torch.trunc_,
"aten::sigmoid": torch.sigmoid,
"aten::sigmoid_": torch.sigmoid_,
"aten::reciprocal": torch.reciprocal,
"aten::reciprocal_": torch.reciprocal_,
"aten::relu6": F.relu6,
"aten::selu": F.selu,
"aten::silu": F.silu,
"aten::log": torch.log,
"aten::log_": torch.log_,
"aten::log2": torch.log2,
"aten::log2_": torch.log2_,
"aten::log10": torch.log10,
"aten::log10_": torch.log10_,
"aten::log1p": torch.log1p,
"aten::log1p_": torch.log1p_,
"aten::log_sigmoid": F.logsigmoid,
"aten::mish": F.mish,
"aten::cos": torch.cos,
"aten::cos_": torch.cos_,
"aten::sin": torch.sin,
"aten::sin_": torch.sin_,
"aten::tan": torch.tan,
"aten::tan_": torch.tan_,
"aten::cosh": torch.cosh,
"aten::cosh_": torch.cosh_,
"aten::sinh": torch.sinh,
"aten::sinh_": torch.sinh_,
"aten::tanh": torch.tanh,
"aten::tanh_": torch.tanh_,
"aten::acos": torch.acos,
"aten::acos_": torch.acos_,
"aten::asin": torch.asin,
"aten::asin_": torch.asin_,
"aten::atan": torch.atan,
"aten::atan_": torch.atan_,
"aten::acosh": torch.acosh,
"aten::acosh_": torch.acosh_,
"aten::asinh": torch.asinh,
"aten::asinh_": torch.asinh_,
"aten::atanh": torch.atanh,
"aten::atanh_": torch.atanh_,
"aten::hardswish": F.hardswish
}
class unary_op_net(torch.nn.Module):
def __init__(self, op, dtype):
super().__init__()
self.dtype = dtype
self.op = op
def forward(self, x):
x1 = x.to(self.dtype)
y = self.op(x1)
return y, x1
class unary_op_out_net(torch.nn.Module):
def __init__(self, op, dtype):
super().__init__()
self.dtype = dtype
self.op = op
self.out_op = PytorchLayerTest.out_variant(op, torch.ops.aten.relu.out) if op == torch.relu else op
def forward(self, x):
x1 = x.to(self.dtype)
y = self.op(x1)
z = torch.empty_like(y)
y1 = self.out_op(x1, out=z)
return y1, z
class unary_func_op_inplace_net(torch.nn.Module):
def __init__(self, op, dtype):
super().__init__()
self.dtype = dtype
self.op = op
def forward(self, x):
x1 = x.to(self.dtype)
y = self.op(x1, inplace=True)
return y, x1
class prim_abs_net(torch.nn.Module):
def __init__(self, dtype):
super().__init__()
self.dtype = dtype
def forward(self, x):
x1 = x.to(self.dtype)
y = abs(x1)
return y, x1
class unary_op_complex_net(torch.nn.Module):
def __init__(self, op, dtype):
super().__init__()
self.dtype = dtype
self.op = op
def forward(self, x):
x1 = x.to(self.dtype) * 1j
y = self.op(x1)
y = torch.view_as_real(y.to(torch.complex64))
return y, torch.view_as_real(x1)
class TestUnaryOp(PytorchLayerTest):
def _prepare_input(self, unit_range=False):
if unit_range:
# atanh requires inputs in (-1, 1); use uniform random in that range.
# Integer dtypes will truncate to 0, giving atanh(0)=0 — still exercises the op.
x = self.random.torch_rand(2, 10) * 1.99 - 0.995
else:
# random number in range [1, 11)
x = self.random.torch_rand(2, 10) * 10 + 1
return (x.to(self.dtype).numpy(),)
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64, torch.int8, torch.uint8, torch.int32, torch.int64])
@pytest.mark.parametrize("op_type",
[
"aten::abs",
"aten::rsqrt",
"aten::sqrt",
"aten::erf",
"aten::erfc",
"aten::erfinv",
"aten::exp",
"aten::expm1",
"aten::relu",
"aten::relu_",
"aten::ceil",
"aten::ceil_",
"aten::floor",
"aten::floor_",
"aten::trunc",
"aten::trunc_",
"aten::sigmoid",
"aten::reciprocal",
"aten::log",
"aten::log2",
"aten::log10",
"aten::log1p",
# trigonometry
"aten::cos",
"aten::sin",
"aten::tan",
"aten::cosh",
"aten::sinh",
"aten::tanh",
"aten::acos",
"aten::asin",
"aten::atan",
"aten::acosh",
"aten::asinh",
"aten::atanh"
])
def test_unary_op(self, op_type, dtype, ie_device, precision, ir_version):
self.dtype = dtype
if self.use_torch_export() and op_type == "aten::atanh" and dtype in [torch.int8, torch.int32, torch.int64]:
pytest.xfail(reason="torch.export after 2.4.0 doesn't support unsigned int types for atanh in some configurations")
if ie_device == "GPU" and op_type == "aten::erfinv":
pytest.xfail(reason="erfinv is not supported on GPU")
self._test(unary_op_net(OPS[op_type], dtype), op_type,
ie_device, precision, ir_version,
kwargs_to_prepare_input={"unit_range": op_type in ("aten::atanh", "aten::erfinv")})
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
@pytest.mark.parametrize("op_type",
[
# some pytorch inplace ops do not support int
"aten::abs_",
"aten::erf_",
"aten::erfc_",
"aten::exp_",
"aten::rsqrt_",
"aten::sqrt_",
"aten::expm1_",
"aten::sigmoid_",
"aten::reciprocal_",
"aten::relu6",
"aten::selu",
"aten::silu",
"aten::log_sigmoid",
"aten::log_",
"aten::log2_",
"aten::log10_",
"aten::log1p_",
"aten::mish",
# trigonometry
"aten::cos_",
"aten::sin_",
"aten::tan_",
"aten::cosh_",
"aten::sinh_",
"aten::tanh_",
"aten::acos_",
"aten::asin_",
"aten::atan_",
"aten::acosh_",
"aten::asinh_",
"aten::atanh_",
"aten::hardswish"
])
def test_unary_op_float(self, op_type, dtype, ie_device, precision, ir_version):
self.dtype = dtype
if self.use_torch_compile_backend() and op_type == "aten::sigmoid_":
pytest.xfail(reason="Accuracy issue, one or two values are off sometimes")
self._test(unary_op_net(OPS[op_type], dtype), op_type,
ie_device, precision, ir_version)
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64, torch.int8, torch.uint8, torch.int32, torch.int64])
@pytest.mark.parametrize("op_type",
[
"aten::abs",
"aten::rsqrt",
"aten::sqrt",
"aten::erf",
"aten::erfc",
"aten::erfinv",
"aten::exp",
"aten::expm1",
"aten::relu",
"aten::ceil",
"aten::floor",
"aten::trunc",
"aten::sigmoid",
"aten::reciprocal",
"aten::log",
"aten::log2",
"aten::log10",
"aten::log1p",
# trigonometry
"aten::cos",
"aten::sin",
"aten::tan",
"aten::cosh",
"aten::sinh",
"aten::tanh",
"aten::acos",
"aten::asin",
"aten::atan",
"aten::acosh",
"aten::asinh",
"aten::atanh"
])
@pytest.mark.precommit_torch_export
def test_unary_op_out(self, op_type, dtype, ie_device, precision, ir_version):
self.dtype = dtype
self._test(unary_op_out_net(OPS[op_type], dtype), op_type,
ie_device, precision, ir_version,
kwargs_to_prepare_input={"unit_range": op_type in ("aten::atanh", "aten::erfinv")})
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
@pytest.mark.parametrize("op_type",
[
"aten::relu6",
"aten::selu",
"aten::silu",
"aten::hardswish",
"aten::mish",
])
@pytest.mark.precommit_torch_export
def test_unary_func_op_inplace(self, op_type, dtype, ie_device, precision, ir_version):
self.dtype = dtype
self._test(unary_func_op_inplace_net(OPS[op_type], dtype), op_type + "_",
ie_device, precision, ir_version)
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64, torch.int8, torch.uint8, torch.int32, torch.int64])
def test_prim_abs(self, dtype, ie_device, precision, ir_version):
self.dtype = dtype
self._test(prim_abs_net(dtype), "prim::abs",
ie_device, precision, ir_version, fx_kind="aten.abs")
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64, torch.int8, torch.uint8, torch.int32, torch.int64])
@pytest.mark.parametrize("op_type",
[
"aten::abs",
"aten::exp",
])
@pytest.mark.precommit_torch_export
def test_complex_unary_op(self, op_type, dtype, ie_device, precision, ir_version):
self.dtype = dtype
self._test(unary_op_complex_net(OPS[op_type], dtype), op_type,
ie_device, precision, ir_version)
class TestLog1pExpm1Accuracy(PytorchLayerTest):
# log1p and expm1 exist to stay accurate for small inputs, where 1 + x and exp(x) round to 1.
# The result is divided by x so that it is ~1 for small x and the absolute tolerance of the
# comparison cannot hide a result that collapsed to 0 or lost most of its digits.
def _prepare_input(self):
return (np.array([1e-30, -1e-30, 1e-10, -1e-10, 1e-7, -1e-7, 1e-4, -1e-4, 0.5, -0.5], dtype=np.float32),)
def create_model(self, op_name, dtype):
class aten_op_relative(torch.nn.Module):
def __init__(self, op, dtype):
super().__init__()
self.op = op
self.dtype = dtype
def forward(self, x):
x = x.to(self.dtype)
return self.op(x) / x
return aten_op_relative(getattr(torch, op_name), dtype), "aten::" + op_name
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize("op_name", ["log1p", "expm1"])
@pytest.mark.parametrize("dtype", [torch.float32, torch.float64])
def test_small_values(self, op_name, dtype, ie_device, precision, ir_version):
if precision == "FP16":
pytest.skip("Values exceed the f16 range")
self._test(*self.create_model(op_name, dtype), ie_device, precision, ir_version)
class TestLog1pExpm1SpecialValues(PytorchLayerTest):
def _prepare_input(self):
return (np.array(self.values, dtype=np.float32),)
def create_model(self, op_name):
class aten_op(torch.nn.Module):
def __init__(self, op):
super().__init__()
self.op = op
def forward(self, x):
return self.op(x)
return aten_op(getattr(torch, op_name)), "aten::" + op_name
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize(("op_name", "values"), [
("log1p", [np.inf, -1.0, -1.5, np.nan, 3e38]),
# -87.5, -100 and -103 make exp(x) subnormal, where expm1 must still be exactly -1
("expm1", [np.inf, -np.inf, -87.5, -100.0, -103.0, 80.0]),
])
def test_special_values(self, op_name, values, ie_device, precision, ir_version):
if precision == "FP16":
pytest.skip("Values exceed the f16 range")
self.values = values
self._test(*self.create_model(op_name), ie_device, precision, ir_version)