-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PT FE] Support aten::masked_select for pytorch models (#26162)
### Details: - support `aten::masked_select` operator ### Tickets: - [None](#23325)
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "openvino/frontend/pytorch/node_context.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace op { | ||
|
||
using namespace ov::op; | ||
|
||
OutputVector translate_masked_select(const NodeContext& context) { | ||
// aten::masked_select(Tensor self, Tensor mask, Tensor source) -> Tensor | ||
num_inputs_check(context, 2, 2); | ||
auto data = context.get_input(0); | ||
auto mask = context.get_input(1); | ||
auto res = masked_select(context, data, mask); | ||
return {res}; | ||
}; | ||
|
||
} // namespace op | ||
} // namespace pytorch | ||
} // namespace frontend | ||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import numpy as np | ||
import torch | ||
from packaging.version import parse as parse_version | ||
import pytest | ||
|
||
from pytorch_layer_test_class import PytorchLayerTest | ||
|
||
|
||
class TestMaskedSelect(PytorchLayerTest): | ||
def _prepare_input(self, mask_select='ones', mask_dtype=bool, input_dtype=float): | ||
input_shape = [1, 10] | ||
mask = np.zeros(input_shape).astype(mask_dtype) | ||
if mask_select == 'ones': | ||
mask = np.ones(input_shape).astype(mask_dtype) | ||
if mask_select == 'random': | ||
idx = np.random.choice(10, 5) | ||
mask[:, idx] = 1 | ||
return (np.random.randn(1, 10).astype(input_dtype), mask) | ||
|
||
def create_model(self): | ||
import torch | ||
|
||
class aten_masked_select(torch.nn.Module): | ||
def __init__(self): | ||
super(aten_masked_select, self).__init__() | ||
|
||
def forward(self, x, mask): | ||
return x.masked_select(mask) | ||
|
||
ref_net = None | ||
|
||
return aten_masked_select(), ref_net, "aten::masked_select" | ||
|
||
@pytest.mark.parametrize( | ||
"mask_select", ['zeros', 'ones', 'random']) | ||
@pytest.mark.parametrize("input_dtype", [np.float32, np.float64, int, np.int32]) | ||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
def test_masked_select(self, mask_select, input_dtype, ie_device, precision, ir_version): | ||
self._test(*self.create_model(), | ||
ie_device, precision, ir_version, | ||
dynamic_shapes=False, | ||
trace_model=True, | ||
kwargs_to_prepare_input={'mask_select': mask_select, 'mask_dtype': bool, "input_dtype": input_dtype}) | ||
|
||
@pytest.mark.skipif(parse_version(torch.__version__) >= parse_version("2.1.0"), reason="pytorch 2.1 and above does not support nonboolean mask") | ||
@pytest.mark.parametrize( | ||
"mask_select", ['zeros', 'ones', 'random']) | ||
@pytest.mark.parametrize("input_dtype", [np.float32, np.float64, int, np.int32]) | ||
@pytest.mark.parametrize("mask_dtype", [np.uint8, np.int32, np.float32]) | ||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
def test_masked_select_non_bool_mask(self, mask_select, mask_dtype, input_dtype, ie_device, precision, ir_version): | ||
self._test(*self.create_model(), | ||
ie_device, precision, ir_version, | ||
dynamic_shapes=False, | ||
trace_model=True, | ||
kwargs_to_prepare_input={'mask_select': mask_select, 'mask_dtype': mask_dtype, "input_dtype": input_dtype}) | ||
|