Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[Torch Export][FX] Add Support for aten.index_copy (#29778)

Aamir Nazir75697f181159
diff --git a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py
index 10801cbe9c8..e429388c252 100644
--- a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py
+++ b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py
@@ -143,6 +143,7 @@ class OperatorSupport(OpSupport):
"torch.ops.aten.index.Tensor": None,
"torch.ops.aten._unsafe_index.Tensor": None,
"torch.ops.aten.index_select.default": None,
+ "torch.ops.aten.index_copy.default": None,
"torch.ops.aten.isfinite.default": None,
"torch.ops.aten.isinf.default": None,
"torch.ops.aten.isnan.default": None,
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index f71535fb178..6fb3eca4e9b 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -548,6 +548,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::imag", common_translators::translate_imag},
// aten::index - Supported in limited set of patterns
{"aten::index_copy_", op::inplace_op<op::translate_index_copy_>},
+ {"aten::index_copy", op::translate_index_copy_},
{"aten::index_fill_", op::inplace_op<op::translate_index_fill_>},
{"aten::index_put", op::translate_index_put},
{"aten::index_add", op::translate_index_add},
@@ -939,6 +940,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {
{"aten.index.Tensor", op::translate_index_fx},
{"aten._unsafe_index.Tensor", op::translate_index_fx},
{"aten.index_select.default", op::translate_index_select},
+ {"aten.index_copy.default", op::translate_index_copy_},
{"aten.isfinite.default", op::translate_1to1_match_1_inputs<opset10::IsFinite>},
{"aten.isinf.default", op::translate_1to1_match_1_inputs<opset10::IsInf>},
{"aten.isnan.default", op::translate_1to1_match_1_inputs<opset10::IsNaN>},
diff --git a/tests/layer_tests/pytorch_tests/test_index_copy_.py b/tests/layer_tests/pytorch_tests/test_index_copy_.py
index bd9f26814e1..764d482bf89 100644
--- a/tests/layer_tests/pytorch_tests/test_index_copy_.py
+++ b/tests/layer_tests/pytorch_tests/test_index_copy_.py
@@ -13,20 +13,23 @@ class TestIndexCopy(PytorchLayerTest):
def _prepare_input(self):
return (self.input_tensor, self.values)
- def create_model(self, dim, index):
+ def create_model(self, dim, index, inplace):
class aten_index_copy_(torch.nn.Module):
- def __init__(self, dim, index):
+ def __init__(self, dim, index, inplace):
super().__init__()
self.dim = dim
self.index = index
+ self.inplace = inplace
def forward(self, input_tensor, values):
+ if not self.inplace:
+ return input_tensor.index_copy(self.dim, self.index, values)
input_tensor.index_copy_(self.dim, self.index, values)
return input_tensor
ref_net = None
-
- return aten_index_copy_(dim, index), ref_net, "aten::index_copy_"
+ op_name = "aten::index_copy_" if inplace else "aten::index_copy"
+ return aten_index_copy_(dim, index, inplace), ref_net, op_name
@pytest.mark.parametrize(
"input_data",
@@ -77,9 +80,10 @@ class TestIndexCopy(PytorchLayerTest):
)
@pytest.mark.nightly
@pytest.mark.precommit
- def test_index_copy_single_index(self, ie_device, precision, ir_version, input_data):
+ @pytest.mark.parametrize('inplace', [True, False])
+ def test_index_copy_single_index(self, inplace, ie_device, precision, ir_version, input_data):
self.input_tensor = np.random.randn(*input_data["input_shape"]).astype(np.float32)
self.values = np.random.randn(*input_data["values_shape"]).astype(np.float32)
index = input_data["index"]
dim = input_data["dim"]
- self._test(*self.create_model(dim, index), ie_device, precision, ir_version)
+ self._test(*self.create_model(dim, index, inplace), ie_device, precision, ir_version)