forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4914541
commit 837a357
Showing
3 changed files
with
87 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,53 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "openvino/frontend/pytorch/node_context.hpp" | ||
#include "openvino/op/constant.hpp" | ||
#include "openvino/op/convert.hpp" | ||
#include "openvino/op/equal.hpp" | ||
#include "openvino/op/logical_and.hpp" | ||
#include "openvino/op/logical_or.hpp" | ||
#include "openvino/op/not_equal.hpp" | ||
#include "openvino/op/shape_of.hpp" | ||
#include "pt_framework_node.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace op { | ||
|
||
using namespace ov::op; | ||
|
||
OutputVector translate_is_nonzero(const NodeContext& context) { | ||
num_inputs_check(context, 1, 1); | ||
auto const_0 = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {0})); | ||
auto const_1 = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {1})); | ||
auto input = context.get_input(0); | ||
|
||
// check if length input is 1 | ||
auto input_shape = context.mark_node(std::make_shared<v3::ShapeOf>(input, element::i32)); | ||
auto is_length_one = context.mark_node(std::make_shared<v1::Equal>(input_shape, const_1)); | ||
|
||
// check if element is not equal to 0 or false | ||
auto zero_tensor = context.mark_node(v0::Constant::create(element::f32, Shape{1}, {0.0})); | ||
auto false_tensor = context.mark_node(v0::Constant::create(element::boolean, Shape{1}, {false})); | ||
|
||
// perform type conversion | ||
auto converted_input = context.mark_node(std::make_shared<v0::Convert>(input, element::f32)); | ||
|
||
auto is_nonzero_numeric = context.mark_node(std::make_shared<v1::NotEqual>(converted_input, zero_tensor)); | ||
auto is_nonzero_boolean = context.mark_node(std::make_shared<v1::NotEqual>(input, false_tensor)); | ||
|
||
auto final_result = context.mark_node(std::make_shared<v1::LogicalAnd>( | ||
is_length_one, | ||
context.mark_node(std::make_shared<v1::LogicalOr>(is_nonzero_numeric, is_nonzero_boolean)))); | ||
|
||
return {final_result}; | ||
}; | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 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('input_tensor', (np.array([0.]), np.array([1.5]), np.array([False]), np.array([3]), np.array([1, 3, 5]))) | ||
class TestIsNonZero(PytorchLayerTest): | ||
|
||
def _prepare_input(self): | ||
input_tensor = self.input_tensor | ||
return (input_tensor.astype(np.int64),) | ||
|
||
def create_model(self): | ||
class aten_is_nonzero(torch.nn.Module): | ||
|
||
def forward(self, input_tensor): | ||
return torch.is_nonzero(input_tensor) | ||
|
||
ref_net = None | ||
|
||
return aten_is_nonzero(), ref_net, "aten::is_nonzero" | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
def test_is_nonzero(self, ie_device, precision, ir_version, input_tensor): | ||
self.input_tensor = input_tensor | ||
self._test(*self.create_model(), ie_device, precision, ir_version) |