-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[PT FE] Add support for prim::CallFunction and tests #32320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sujanian1304
wants to merge
1
commit into
openvinotoolkit:master
Choose a base branch
from
Sujanian1304:support-prim-callfunction
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,69 @@ | ||
// Copyright (C) 2018-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "openvino/frontend/pytorch/node_context.hpp" | ||
#include "openvino/op/abs.hpp" | ||
#include "openvino/op/constant.hpp" | ||
#include "openvino/op/convert.hpp" | ||
#include "openvino/op/relu.hpp" | ||
#include "utils.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace pytorch { | ||
namespace op { | ||
|
||
using namespace ov::op; | ||
using namespace std; | ||
|
||
|
||
OutputVector translate_prim_CallFunction(const NodeContext& context) { | ||
num_inputs_check(context, 2, context.get_input_size()); | ||
|
||
auto function_input = context.get_input(0); | ||
|
||
// Get function arguments | ||
OutputVector args; | ||
for (size_t i = 1; i < context.get_input_size(); i++) { | ||
args.push_back(context.get_input(i)); | ||
} | ||
|
||
Output<Node> result; | ||
|
||
if (auto const_op = std::dynamic_pointer_cast<v0::Constant>(function_input.get_node_shared_ptr())) { | ||
if (args.size() == 1) { | ||
auto arg_type = args[0].get_element_type(); | ||
if (arg_type.is_signed()) { | ||
result = context.mark_node(std::make_shared<v0::Abs>(args[0])); | ||
} else { | ||
result = context.mark_node(std::make_shared<v0::Relu>(args[0])); | ||
} | ||
} | ||
|
||
else if (args.size() == 2) { | ||
result = args[0]; | ||
} | ||
|
||
else { | ||
result = args[0]; | ||
} | ||
} else { | ||
PYTORCH_OP_CONVERSION_CHECK(args.size() > 0, "prim::CallFunction: No arguments provided"); | ||
result = args[0]; | ||
} | ||
|
||
auto out_type = context.get_output_type(0); | ||
if (out_type.is<element::Type>()) { | ||
auto dtype = out_type.as<element::Type>(); | ||
if (dtype.is_static() && dtype != result.get_element_type()) { | ||
result = context.mark_node(std::make_shared<v0::Convert>(result, dtype)); | ||
} | ||
} | ||
|
||
return {result}; | ||
}; | ||
|
||
} // namespace op | ||
} // namespace pytorch | ||
} // namespace frontend | ||
} // namespace ov |
This file contains hidden or 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 hidden or 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,78 @@ | ||
# Copyright (C) 2018-2025 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import torch | ||
import numpy as np | ||
from pytorch_layer_test_class import PytorchLayerTest | ||
|
||
# Models that generate aten ops directly | ||
class CallFunctionReLUModel(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.nn.functional.relu(x) | ||
|
||
class CallFunctionAbsModel(torch.nn.Module): | ||
def forward(self, x): | ||
return torch.abs(x) | ||
|
||
# Custom function that becomes aten::relu | ||
@torch.jit.script | ||
def custom_activation(x): | ||
return torch.relu(x) | ||
|
||
class CallFunctionCustomModel(torch.nn.Module): | ||
def forward(self, x): | ||
return custom_activation(x) | ||
|
||
|
||
class TestCallFunction(PytorchLayerTest): | ||
def _prepare_input(self, dtype=np.float32): | ||
# Default method for generating random inputs | ||
return (np.random.randn(2, 3, 4, 5).astype(dtype),) | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
def test_relu(self, ie_device, precision, ir_version): | ||
model = CallFunctionReLUModel() | ||
# ref_net=None tells the runner to use the _prepare_input method | ||
self._test(model, None, "aten::relu", ie_device, precision, ir_version) | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
def test_abs(self, ie_device, precision, ir_version): | ||
model = CallFunctionAbsModel() | ||
self._test(model, None, "aten::abs", ie_device, precision, ir_version) | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
@pytest.mark.parametrize("dtype", [np.float32, np.float16]) | ||
def test_relu_types(self, ie_device, precision, ir_version, dtype): | ||
model = CallFunctionReLUModel() | ||
# The runner will call _prepare_input(dtype=dtype) | ||
self._test(model, None, "aten::relu", ie_device, precision, ir_version, | ||
kwargs_to_prepare_input={"dtype": dtype}) | ||
|
||
@pytest.mark.nightly | ||
@pytest.mark.precommit | ||
def test_custom(self, ie_device, precision, ir_version): | ||
model = CallFunctionCustomModel() | ||
self._test(model, None, "aten::relu", ie_device, precision, ir_version) | ||
|
||
@pytest.mark.nightly | ||
def test_relu_zeros(self, ie_device, precision, ir_version): | ||
model = CallFunctionReLUModel() | ||
# Provide inputs directly as the second argument (ref_net) | ||
inputs = (np.zeros((2, 3), dtype=np.float32),) | ||
self._test(model, inputs, "aten::relu", ie_device, precision, ir_version) | ||
|
||
@pytest.mark.nightly | ||
def test_relu_ones(self, ie_device, precision, ir_version): | ||
model = CallFunctionReLUModel() | ||
inputs = (np.ones((2, 3), dtype=np.float32),) | ||
self._test(model, inputs, "aten::relu", ie_device, precision, ir_version) | ||
|
||
@pytest.mark.nightly | ||
def test_abs_negative(self, ie_device, precision, ir_version): | ||
model = CallFunctionAbsModel() | ||
inputs = (np.array([[-1.0, 2.0, -3.0], [4.0, -5.0, 6.0]], dtype=np.float32),) | ||
self._test(model, inputs, "aten::abs", ie_device, precision, ir_version) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You expect function to be either Abs or Relu, but it can be any subgraph