Skip to content

Commit dcc5d37

Browse files
committed
fixed coding style with clang-format-9 and added the suggested changes
1 parent c8dac15 commit dcc5d37

File tree

4 files changed

+94
-100
lines changed

4 files changed

+94
-100
lines changed
+9-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
#include "openvino/op/cos.hpp"
2-
#include "openvino/op/sin.hpp"
3-
#include "openvino/op/multiply.hpp"
4-
#include "openvino/op/concat.hpp"
51
#include "openvino/frontend/complex_type_mark.hpp"
62
#include "openvino/op/convert.hpp"
3+
#include "openvino/op/cos.hpp"
4+
#include "openvino/op/multiply.hpp"
5+
#include "openvino/op/sin.hpp"
76
#include "utils.hpp"
87

98
namespace ov {
@@ -17,15 +16,15 @@ OutputVector translate_polar(const NodeContext& context) {
1716
num_inputs_check(context, 2, 3);
1817
auto abs = context.get_input(0);
1918
auto angle = context.get_input(1);
20-
auto real = context.mark_node(std::make_shared<v1::Multiply>(abs,context.mark_node(std::make_shared<v0::Cos>(angle))));
21-
auto imag = context.mark_node(std::make_shared<v1::Multiply>(abs,context.mark_node(std::make_shared<v0::Sin>(angle))));
22-
auto complex_concat = context.mark_node(std::make_shared<v0::Concat>(OutputVector{real, imag}, -1));
23-
// wrap the tensor with ComplexTypeMark to flag it as complex for later operations.
24-
auto complex_tensor = context.mark_node(std::make_shared<ComplexTypeMark>(complex_concat));
19+
auto real =
20+
context.mark_node(std::make_shared<v1::Multiply>(abs, context.mark_node(std::make_shared<v0::Cos>(angle))));
21+
auto imag =
22+
context.mark_node(std::make_shared<v1::Multiply>(abs, context.mark_node(std::make_shared<v0::Sin>(angle))));
23+
auto complex_tensor = context.mark_node(std::make_shared<ComplexTypeMark>(real, imag));
2524
return {complex_tensor};
2625
}
2726

2827
} // namespace op
2928
} // namespace pytorch
3029
} // namespace frontend
31-
} // namespace ov
30+
} // namespace ov

src/frontends/pytorch/src/op/randperm.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#include "openvino/op/topk.hpp"
2-
#include "openvino/op/random_uniform.hpp"
31
#include "openvino/frontend/pytorch/node_context.hpp"
42
#include "openvino/op/constant.hpp"
5-
#include "utils.hpp"
3+
#include "openvino/op/random_uniform.hpp"
64
#include "openvino/op/shape_of.hpp"
5+
#include "openvino/op/topk.hpp"
6+
#include "utils.hpp"
77

88
namespace ov {
99
namespace frontend {
@@ -20,25 +20,37 @@ OutputVector translate_randperm(const NodeContext& context) {
2020
} else if (num_inputs == 2) {
2121
if (!context.input_is_none(1)) {
2222
dtype_value = context.const_input<int>(1);
23-
OPENVINO_ASSERT(dtype_value == 4, "Only dtype value 4 (int64) is supported for aten::randperm, got: ", dtype_value);
23+
OPENVINO_ASSERT(dtype_value == 4,
24+
"Only dtype value 4 (int64) is supported for aten::randperm, got: ",
25+
dtype_value);
2426
}
2527
} else if (num_inputs == 5) {
2628
if (!context.input_is_none(1)) {
2729
dtype_value = context.const_input<int>(1);
28-
OPENVINO_ASSERT(dtype_value == 4, "Only dtype value 4 (int64) is supported for aten::randperm, got: ", dtype_value);
30+
OPENVINO_ASSERT(dtype_value == 4,
31+
"Only dtype value 4 (int64) is supported for aten::randperm, got: ",
32+
dtype_value);
2933
}
3034
} else {
31-
PYTORCH_OP_CONVERSION_CHECK(false, "Unexpected number of inputs for aten::randperm: ", num_inputs);
35+
PYTORCH_OP_CONVERSION_CHECK(false, "Unexpected number of inputs for aten::randperm: ", num_inputs);
3236
}
3337
if (n == 0) {
34-
return {context.mark_node(v0::Constant::create(element::i64, Shape{0},std::vector<int64_t>{}))};}
38+
auto const_empty = std::make_shared<v0::Constant>(element::i64, Shape{0}, std::vector<int64_t>{});
39+
return {context.mark_node(const_empty)};
40+
}
3541
auto shape = v0::Constant::create(element::i64, Shape{1}, {n});
3642
auto min_val = v0::Constant::create(element::f32, Shape{}, {0.0f});
3743
auto max_val = v0::Constant::create(element::f32, Shape{}, {1.0f});
3844
auto random_tensor = context.mark_node(std::make_shared<v8::RandomUniform>(shape, min_val, max_val, element::f32));
3945
const int64_t axis = 0;
4046
auto k = v0::Constant::create(element::i64, Shape{}, {n});
41-
auto topk = context.mark_node(std::make_shared<v11::TopK>(random_tensor, k, axis, ov::op::TopKMode::MIN, ov::op::TopKSortType::SORT_VALUES, element::i64, false));
47+
auto topk = context.mark_node(std::make_shared<v11::TopK>(random_tensor,
48+
k,
49+
axis,
50+
ov::op::TopKMode::MIN,
51+
ov::op::TopKSortType::SORT_VALUES,
52+
element::i64,
53+
false));
4254
return {topk->output(1)};
4355
}
4456

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
# Copyright (C) 2018-2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import numpy as np
45
import pytest
56
import torch
6-
import numpy as np
7+
78
from pytorch_layer_test_class import PytorchLayerTest
89

910
class TestPolar(PytorchLayerTest):
1011
def _prepare_input(self):
1112
return (
12-
np.array([1.0, 2.0, 3.0], dtype=np.float32),
13-
np.array([0.1, 0.2, 0.3], dtype=np.float32)
13+
np.random.uniform(0, 10, (1, 1000)).astype(np.float32),
14+
np.random.uniform(-np.pi, np.pi, (1, 1000)).astype(np.float32)
1415
)
1516

1617
def create_model(self):
1718
class PolarModel(torch.nn.Module):
1819
def forward(self, abs, angle):
19-
real = abs * torch.cos(angle)
20-
imag = abs * torch.sin(angle)
21-
return torch.stack([real, imag], dim=-1)
22-
return PolarModel(), None, None
20+
complex_tensor = torch.polar(abs, angle)
21+
return torch.view_as_real(complex_tensor)
2322

23+
return PolarModel(), None, "aten::polar"
24+
25+
@pytest.mark.parametrize("input_case", [
26+
(1, 1000),
27+
(2, 500),
28+
(5, 200),
29+
(10, 100),
30+
])
31+
@pytest.mark.parametrize("dtype", [
32+
np.float32,
33+
np.float64
34+
])
2435
@pytest.mark.nightly
2536
@pytest.mark.precommit
26-
@pytest.mark.parametrize("input_variant", ["static", "dynamic"])
27-
def test_polar(self, ie_device, precision, ir_version, input_variant):
28-
atol = 1e-4 if precision == "FP32" else 1e-3
29-
rtol = 1e-4
30-
if input_variant == "static":
31-
input_data = self._prepare_input()
32-
else:
33-
static_input = self._prepare_input()
34-
input_data = (
35-
np.expand_dims(static_input[0], axis=0),
36-
np.expand_dims(static_input[1], axis=0)
37-
)
38-
self._test(*self.create_model(), ie_device, precision, ir_version,
39-
input_data=input_data, model_trace=True, atol=atol, rtol=rtol)
37+
def test_polar(self, input_case, dtype, ie_device, precision, ir_version):
38+
self.input_shape = input_case
39+
self._prepare_input = lambda: (
40+
np.random.uniform(0, 10, input_case).astype(dtype),
41+
np.random.uniform(-np.pi, np.pi, input_case).astype(dtype)
42+
)
43+
self._test(*self.create_model(), ie_device, precision, ir_version, trace_model=True,
44+
use_convert_model=True, kwargs_to_prepare_input={})
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,57 @@
1-
# Copyright (C) 2018-2025 Intel Corporation
2-
# SPDX-License-Identifier: Apache-2.0
3-
41
import pytest
52
import torch
63
import numpy as np
74
from pytorch_layer_test_class import PytorchLayerTest, flattenize_inputs
8-
from copy import deepcopy
95

106
class TestRandperm(PytorchLayerTest):
117
def _prepare_input(self):
12-
return ()
8+
return (np.array([self.n], dtype=np.int64),)
139

14-
def create_model(self, n):
15-
class AtenRandperm(torch.nn.Module):
16-
def __init__(self, n):
10+
def create_model(self, n, num_inputs, dtype_value=None):
11+
class aten_randperm(torch.nn.Module):
12+
def __init__(self, n, num_inputs, dtype_value):
1713
super().__init__()
18-
self.n = n
19-
20-
def forward(self):
21-
return torch.randperm(self.n, dtype=torch.int64)
22-
23-
return AtenRandperm(n), None, "aten::randperm"
24-
25-
def is_valid_permutation(self, output, n):
26-
if hasattr(output, 'detach'):
27-
arr = output.detach().cpu().numpy().astype(np.int64)
28-
else:
29-
arr = np.array(output, dtype=np.int64)
30-
sorted_arr = np.sort(arr.flatten())
31-
expected = np.arange(n, dtype=np.int64)
32-
return np.array_equal(sorted_arr, expected)
33-
34-
@pytest.mark.parametrize("n", [1, 5, 10])
14+
self.n = torch.tensor(n, dtype=torch.int64)
15+
self.num_inputs = num_inputs
16+
self.dtype = torch.int64 if dtype_value == 4 else None
17+
18+
def forward(self, x):
19+
if self.num_inputs == 1:
20+
return torch.randperm(self.n)
21+
elif self.num_inputs == 2:
22+
return torch.randperm(self.n, dtype=self.dtype)
23+
elif self.num_inputs == 5:
24+
return torch.randperm(self.n, dtype=self.dtype, layout=torch.strided,
25+
device=x.device, pin_memory=False)
26+
raise ValueError("Invalid num_inputs")
27+
28+
return aten_randperm(n, num_inputs, dtype_value), None, "aten::randperm"
29+
30+
@pytest.mark.parametrize(("n", "num_inputs", "dtype_value"), [
31+
(0, 1, None),
32+
(1, 1, None),
33+
(5, 1, None),
34+
(5, 2, 4),
35+
(5, 5, 4),
36+
])
3537
@pytest.mark.nightly
3638
@pytest.mark.precommit
37-
def test_randperm_custom(self, n, ie_device, precision, ir_version):
38-
model, ref_net, op = self.create_model(n)
39+
def test_randperm(self, n, num_inputs, dtype_value, ie_device, precision, ir_version):
40+
self.n = n
41+
model, ref_net, op = self.create_model(n, num_inputs, dtype_value)
3942
inputs = self._prepare_input()
4043
torch_inputs = [torch.from_numpy(x) if isinstance(x, np.ndarray) else x for x in inputs]
4144
ov_inputs = flattenize_inputs(inputs)
42-
trace_model = True
43-
dynamic_shapes = True
44-
freeze_model = True
45-
46-
with torch.no_grad():
47-
smodel, converted_model = self.convert_directly_via_frontend(
48-
model, torch_inputs, trace_model, dynamic_shapes, ov_inputs, freeze_model
49-
)
50-
51-
from openvino import Core
52-
core = Core()
53-
compiled_model = core.compile_model(converted_model, ie_device).
54-
ov_output_dict = compiled_model(())
55-
ov_output_tensor = list(ov_output_dict.values())[0]
56-
57-
assert ov_output_tensor.shape[0] == n, f"Output shape {ov_output_tensor.shape} does not match expected ({n},)"
58-
assert self.is_valid_permutation(ov_output_tensor, n), (
59-
f"Output {ov_output_tensor} is not a valid permutation of [0, 1, ..., {n-1}]"
45+
smodel, converted_model = self.convert_directly_via_frontend(
46+
model, torch_inputs, trace_model=True, dynamic_shapes=False, ov_inputs=ov_inputs, freeze_model=True
6047
)
61-
62-
@pytest.mark.xfail(reason="OpenVINO doesn't support empty tensors for randperm")
63-
def test_randperm_zero(self, ie_device, precision, ir_version):
64-
model, ref_net, op = self.create_model(0)
65-
inputs = self._prepare_input()
66-
torch_inputs = [torch.from_numpy(x) if isinstance(x, np.ndarray) else x for x in inputs]
67-
ov_inputs = flattenize_inputs(inputs)
68-
trace_model = True
69-
dynamic_shapes = True
70-
freeze_model = True
71-
72-
with torch.no_grad():
73-
smodel, converted_model = self.convert_directly_via_frontend(
74-
model, torch_inputs, trace_model, dynamic_shapes, ov_inputs, freeze_model
75-
)
7648
from openvino import Core
7749
core = Core()
7850
compiled_model = core.compile_model(converted_model, ie_device)
79-
_ = compiled_model(())
51+
52+
ov_output = compiled_model(ov_inputs)[0]
53+
if n > 0:
54+
assert ov_output.shape[0] == n, f"Output shape {ov_output.shape} does not match expected ({n},)"
55+
assert np.array_equal(np.sort(ov_output), np.arange(n)), f"Output is not a valid permutation of [0, ..., {n-1}]"
56+
else:
57+
assert ov_output.shape[0] == 0, f"Output shape for n=0 should be (0,), got {ov_output.shape}"

0 commit comments

Comments
 (0)