-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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::lerp and aten::lerp_ (#27272)
### Details: - *Support `aten::lerp` and `aten::lerp_`* ### Tickets: - *CVS-156191*
- Loading branch information
Showing
3 changed files
with
94 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,36 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "openvino/frontend/pytorch/node_context.hpp" | ||
#include "openvino/op/add.hpp" | ||
#include "openvino/op/convert_like.hpp" | ||
#include "openvino/op/multiply.hpp" | ||
#include "openvino/op/subtract.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace op { | ||
|
||
using namespace ov::op; | ||
|
||
OutputVector translate_lerp(const NodeContext& context) { | ||
// Tensor = aten::lerp(%lhs.1, %rhs.1, %self.weight) | ||
num_inputs_check(context, 3, 3); | ||
Output<Node> start; | ||
Output<Node> end; | ||
std::tie(start, end) = get_inputs_with_promoted_types(context, 0, 1); | ||
|
||
Output<Node> weight = context.get_input(2); | ||
auto scale = context.mark_node(std::make_shared<v1::Subtract>(end, start)); | ||
weight = context.mark_node(std::make_shared<v1::ConvertLike>(weight, scale)); | ||
auto delta = context.mark_node(std::make_shared<v1::Multiply>(scale, weight)); | ||
return {context.mark_node(std::make_shared<v1::Add>(start, delta))}; | ||
}; | ||
|
||
} // 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,56 @@ | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import numpy as np | ||
import pytest | ||
import torch | ||
|
||
from pytorch_layer_test_class import PytorchLayerTest, skip_if_export | ||
|
||
|
||
class TestLerp(PytorchLayerTest): | ||
def _prepare_input(self): | ||
return (np.random.randn(2, 5, 3, 4).astype(np.float32), self.input_rhs) | ||
|
||
def create_model(self, weight, op_type): | ||
class aten_lerp(torch.nn.Module): | ||
def __init__(self, weight, op) -> None: | ||
super().__init__() | ||
self.weight = weight | ||
self.forward = self.forward1 if op == "lerp" else self.forward2 | ||
|
||
def forward1(self, lhs, rhs): | ||
return torch.lerp(lhs, rhs, weight=self.weight) | ||
|
||
def forward2(self, lhs, rhs): | ||
return lhs.lerp_(rhs, weight=self.weight) | ||
|
||
return aten_lerp(weight, op_type), None, f"aten::{op_type}" | ||
|
||
@pytest.mark.parametrize("weight", (-0.5, | ||
0, | ||
0.5, | ||
1, | ||
2, | ||
skip_if_export([1, 5, 3, 4])) | ||
) | ||
@pytest.mark.parametrize("input_shape_rhs", [[2, 5, 3, 4], | ||
[1, 5, 3, 4], | ||
[1]]) | ||
@pytest.mark.parametrize("op_type", ["lerp", "lerp_"]) | ||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
@pytest.mark.precommit_torch_export | ||
@pytest.mark.precommit_fx_backend | ||
def test_lerp(self, ie_device, precision, ir_version, | ||
weight, input_shape_rhs, op_type): | ||
self.input_rhs = np.random.randn(*input_shape_rhs).astype(np.float32) | ||
if isinstance(weight, list): | ||
weight = torch.rand(weight) | ||
self._test( | ||
*self.create_model(weight, op_type), | ||
ie_device, | ||
precision, | ||
ir_version, | ||
use_convert_model=True, | ||
) |