* Added Torchscript Backend * First commit for backend with Torch FX Decoder * Merging changes from Torch FX branch * Torch FX initial fixes (Temporary) * Fixed type/shape issues in Torch FX decoder * Added translation for built-in getitem * MaxPool update & Output shape fix (Torch FX) * Torch FX graph outputs fix * Torch FX support for sigmoid and slu_ * Torch FX graph module caching * Torch Fx partitioner cache removed * Torch FX initial getitem replacer added * Index check for torch fx getitem replacer * Debug print removed from partitioner * Added environment variables for pytorch tracing mode and openvino device * FX translation fix for getitem & getitem replacer removed * Added checks for PyTorch tracing mode environment variable * Adding compile mode for fallback * Added more ops for resnet18 * Added a check for environment variable * Generalized addmm to work with torchscript and torchfx * Added the missing batch_norm.default translation * fx_backend: include get_attr ops to the partitions * AddeTODO note t to improvget_attr algorithm * created function for adding get_attr nodes * fx_backend: added aten.mul.Tensor, re-enabled aten.empty.memory_format * fx_backend: Additional op support/improvement for Inception V3 * Added comment for fix 64-bit to 32-bit max int conversion * fx_backend: Update for avg_poolnd to support 3 inputs * Fixed erorr in decoder.py * TorchFX caching fix * Torch backend, op support for Stable Diff. & BERT * Arranged ops in order and added torch tensor mapping * Added support for more ops for super glue * TorchFX: Initial permanent fallback * TorchFX: New ops for improved TorchVision support * TorchFX backend optimizations for partitioning and tmp fallback * working operator updates for superglue * Updates to operators for superglue * Removed max.dim and stack * Cleanup * Cleanup * Fixed a couple of syntax issues * Fixed a couple of syntax issues * Added missing method to TorchFX Decoder * Added missing method to TorchFX Decoder * Removed redundant code for transpose * TorchFX: Initial StableDiffusion support * PyTorch decoder ovtype to ctype fix for int64 * Added ops for distilbert * Fixed few unnecessary include statements * Seperated TorchFX and TorchScript decoders * Modified import statements to reflect two decoders * f64 fix for TorchFX * Import fix for PyTorch backend modules * TorchFX serialize graph for debugging (Temporary) * Serialize and load back feature enabled for TorchFX * Temporary optimization to remove Broadcast * Temporary SoftmaxRehapeElimination pass is added * TorchFX custom model cache directory * PyTorch bitwise translation, conversion checks enabled * Naming fix in make_list_construct * TorchFX: Added comments to Softmax and Slice translations * translate_chunk temporarily removed for TS backend * Fixed linter issues * Addressed clang formatting issues * Fixed few more clang and linter issues * Fixed tests to use ts_decoder * Fixed naming convention issues * Added missing import * Added inlined_inputs to TorchScriptDecoder * Added tests for torch fx backend * Removed magic numbers in PyTorch decoder utils * TorchFX decoder data type fix * Added cast from size_t to int * TorchFX output handling code cleanup * TorchFX: Use detached input tensor * Added missing cast from size_t to int * Added static cast in group_norm * Fixed casting issue in split --------- Co-authored-by: ynimmaga <yamini.nimmagadda@intel.com> Co-authored-by: Cavus Mustafa <mustafa.cavus@intel.com>
131 lines
5.1 KiB
Python
131 lines
5.1 KiB
Python
# Copyright (C) 2018-2023 Intel Corporation
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import numpy as np
|
|
import pytest
|
|
import torch
|
|
|
|
from pytorch_layer_test_class import PytorchLayerTest
|
|
|
|
|
|
@pytest.mark.parametrize('alpha', (-0.5, 0, 0.5, 1, 2))
|
|
@pytest.mark.parametrize('input_rhs', (np.random.randn(2, 5, 3, 4).astype(np.float32),
|
|
np.random.randn(
|
|
1, 5, 3, 4).astype(np.float32),
|
|
np.random.randn(1).astype(np.float32)))
|
|
class TestAdd(PytorchLayerTest):
|
|
|
|
def _prepare_input(self):
|
|
return (np.random.randn(2, 5, 3, 4).astype(np.float32), self.input_rhs)
|
|
|
|
def create_model(self, alpha, op_type):
|
|
class aten_add(torch.nn.Module):
|
|
|
|
def __init__(self, alpha, op) -> None:
|
|
super().__init__()
|
|
self.alpha = alpha
|
|
self.forward = self.forward1 if op == "add" else self.forward2
|
|
|
|
def forward1(self, lhs, rhs):
|
|
return torch.add(lhs, rhs, alpha=self.alpha)
|
|
|
|
def forward2(self, lhs, rhs):
|
|
return lhs.add_(rhs, alpha=self.alpha)
|
|
|
|
ref_net = None
|
|
|
|
return aten_add(alpha, op_type), ref_net, f"aten::{op_type}"
|
|
|
|
@pytest.mark.nightly
|
|
@pytest.mark.precommit
|
|
@pytest.mark.precommit_ts_backend
|
|
@pytest.mark.precommit_fx_backend
|
|
@pytest.mark.parametrize("op_type", ["add", "add_"])
|
|
def test_add(self, ie_device, precision, ir_version, alpha, input_rhs, op_type):
|
|
self.input_rhs = input_rhs
|
|
self._test(*self.create_model(alpha, op_type), ie_device, precision, ir_version)
|
|
|
|
|
|
class TestAddTypes(PytorchLayerTest):
|
|
|
|
def _prepare_input(self):
|
|
if len(self.lhs_shape) == 0:
|
|
return (torch.randn(self.rhs_shape).to(self.rhs_type).numpy(),)
|
|
elif len(self.rhs_shape) == 0:
|
|
return (torch.randn(self.lhs_shape).to(self.lhs_type).numpy(),)
|
|
return (torch.randn(self.lhs_shape).to(self.lhs_type).numpy(),
|
|
torch.randn(self.rhs_shape).to(self.rhs_type).numpy())
|
|
|
|
def create_model(self, lhs_type, lhs_shape, rhs_type, rhs_shape):
|
|
|
|
class aten_add(torch.nn.Module):
|
|
def __init__(self, lhs_type, lhs_shape, rhs_type, rhs_shape):
|
|
super().__init__()
|
|
self.lhs_type = lhs_type
|
|
self.rhs_type = rhs_type
|
|
if len(lhs_shape) == 0:
|
|
self.forward = self.forward1
|
|
elif len(rhs_shape) == 0:
|
|
self.forward = self.forward2
|
|
else:
|
|
self.forward = self.forward3
|
|
|
|
def forward1(self, rhs):
|
|
return torch.add(torch.tensor(3).to(self.lhs_type), rhs.to(self.rhs_type), alpha=2)
|
|
|
|
def forward2(self, lhs):
|
|
return torch.add(lhs.to(self.lhs_type), torch.tensor(3).to(self.rhs_type), alpha=2)
|
|
|
|
def forward3(self, lhs, rhs):
|
|
return torch.add(lhs.to(self.lhs_type), rhs.to(self.rhs_type), alpha=2)
|
|
|
|
ref_net = None
|
|
|
|
return aten_add(lhs_type, lhs_shape, rhs_type, rhs_shape), ref_net, "aten::add"
|
|
|
|
@pytest.mark.parametrize(("lhs_type", "rhs_type"),
|
|
[[torch.int32, torch.int64],
|
|
[torch.int32, torch.float32],
|
|
[torch.int32, torch.float64],
|
|
[torch.int64, torch.int32],
|
|
[torch.int64, torch.float32],
|
|
[torch.int64, torch.float64],
|
|
[torch.float32, torch.int32],
|
|
[torch.float32, torch.int64],
|
|
[torch.float32, torch.float64],
|
|
])
|
|
@pytest.mark.parametrize(("lhs_shape", "rhs_shape"), [([2, 3], [2, 3]),
|
|
([2, 3], []),
|
|
([], [2, 3]),
|
|
])
|
|
@pytest.mark.nightly
|
|
@pytest.mark.precommit
|
|
@pytest.mark.precommit_ts_backend
|
|
@pytest.mark.precommit_fx_backend
|
|
def test_add_types(self, ie_device, precision, ir_version, lhs_type, lhs_shape, rhs_type, rhs_shape):
|
|
self.lhs_type = lhs_type
|
|
self.lhs_shape = lhs_shape
|
|
self.rhs_type = rhs_type
|
|
self.rhs_shape = rhs_shape
|
|
self._test(*self.create_model(lhs_type, lhs_shape, rhs_type, rhs_shape),
|
|
ie_device, precision, ir_version)
|
|
|
|
class TestAddLists(PytorchLayerTest):
|
|
|
|
def _prepare_input(self):
|
|
return (np.random.randn(2, 5, 3, 4).astype(np.float32),)
|
|
|
|
def create_model(self):
|
|
class aten_add(torch.nn.Module):
|
|
def forward(self, x):
|
|
return x.reshape(x.shape[:-1] + (-1,))
|
|
|
|
return aten_add(), None, "aten::add"
|
|
|
|
@pytest.mark.nightly
|
|
@pytest.mark.precommit
|
|
@pytest.mark.precommit_ts_backend
|
|
@pytest.mark.precommit_fx_backend
|
|
def test_add(self, ie_device, precision, ir_version):
|
|
self._test(*self.create_model(), ie_device, precision, ir_version)
|