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.
Merge pull request openvinotoolkit#56 from bszmelcz/add_aten_roll
Add support for aten::roll
- Loading branch information
Showing
3 changed files
with
80 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,37 @@ | ||
// Copyright (C) 2018-2022 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "openvino/frontend/pytorch/node_context.hpp" | ||
#include "openvino/opsets/opset8.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace op { | ||
|
||
OutputVector translate_roll(NodeContext& context) { | ||
const auto data = context.get_input(0); | ||
const auto shifts = context.get_input(1); | ||
const auto axes = context.get_input(2); | ||
const auto shifts_pshape = shifts.get_partial_shape(); | ||
const auto axes_pshape = axes.get_partial_shape(); | ||
const auto match_dims = axes_pshape.compatible(shifts_pshape); | ||
if (!match_dims) { | ||
const auto const_minus_1 = opset8::Constant::create(element::i32, Shape{1}, {-1}); | ||
const auto axis_0 = opset8::Constant::create(element::i32, Shape{1}, {0}); | ||
const auto flat = std::make_shared<opset8::Reshape>(data, const_minus_1, false); | ||
const auto roll = std::make_shared<opset8::Roll>(flat, shifts, axis_0); | ||
const auto shape_of_data = std::make_shared<opset8::ShapeOf>(data); | ||
const auto reshape = std::make_shared<opset8::Reshape>(roll, shape_of_data, false); | ||
context.mark_nodes({const_minus_1, flat, roll, shape_of_data, reshape}); | ||
return {reshape}; | ||
} | ||
return {context.mark_node(std::make_shared<opset8::Roll>(data, shifts, axes))}; | ||
}; | ||
|
||
} // 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,41 @@ | ||
# Copyright (C) 2018-2022 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import numpy as np | ||
from pytorch_layer_test_class import PytorchLayerTest | ||
|
||
|
||
class TestRoll(PytorchLayerTest): | ||
def _prepare_input(self): | ||
return (np.random.uniform(0, 50, (2, 3, 4)).astype(np.float32),) | ||
|
||
def create_model(self, shifts, dim): | ||
|
||
import torch | ||
|
||
class aten_roll(torch.nn.Module): | ||
def __init__(self, shifts, dim=None): | ||
super(aten_roll, self).__init__() | ||
self.dim = dim | ||
self.shits = shifts | ||
|
||
def forward(self, x): | ||
if self.dim is not None: | ||
return torch.roll(x, self.shits, self.dim) | ||
return torch.roll(x, self.shits) | ||
|
||
ref_net = None | ||
|
||
return aten_roll(shifts, dim), ref_net, "aten::roll" | ||
|
||
@pytest.mark.parametrize(("shifts", "dim"), [ | ||
[(2, 1), (0, 1)], | ||
[1, 0], | ||
[-1, 0], | ||
[1, None], | ||
]) | ||
@pytest.mark.nightly | ||
def test_roll(self, shifts, dim, ie_device, precision, ir_version): | ||
self._test(*self.create_model(shifts, dim), ie_device, precision, ir_version) | ||
|