Skip to content
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

Fix the aten::mv for pytorch models #22073 #22677

Merged
merged 24 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/frontends/pytorch/src/op/matmul.cpp
mvafin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "openvino/frontend/pytorch/node_context.hpp"

Check warning on line 1 in src/frontends/pytorch/src/op/matmul.cpp

View workflow job for this annotation

GitHub Actions / clang-format

[reviewdog-suggester] reported by reviewdog 🐶 Raw Output: src/frontends/pytorch/src/op/matmul.cpp:1:- #include "openvino/frontend/pytorch/node_context.hpp" src/frontends/pytorch/src/op/matmul.cpp:1:+#include "openvino/frontend/pytorch/node_context.hpp"
#include "pt_framework_node.hpp"
#include "utils.hpp"


Check warning on line 5 in src/frontends/pytorch/src/op/matmul.cpp

View workflow job for this annotation

GitHub Actions / clang-format

[reviewdog-suggester] reported by reviewdog 🐶 Raw Output: src/frontends/pytorch/src/op/matmul.cpp:5:-
namespace ov {
namespace frontend {
namespace pytorch {
namespace op {
using namespace ov::op;
OutputVector translate_mv(const NodeContext& context) {
num_inputs_check(context, 3, 7);

Check warning on line 12 in src/frontends/pytorch/src/op/matmul.cpp

View workflow job for this annotation

GitHub Actions / clang-format

[reviewdog-suggester] reported by reviewdog 🐶 Raw Output: src/frontends/pytorch/src/op/matmul.cpp:12:- num_inputs_check(context, 3, 7); src/frontends/pytorch/src/op/matmul.cpp:11:+ num_inputs_check(context, 3, 7);
mvafin marked this conversation as resolved.
Show resolved Hide resolved
mvafin marked this conversation as resolved.
Show resolved Hide resolved
auto matrix = context.mark_node(std::make_shared<v0::Convert>(context.get_input(0), element::f32));
eaidova marked this conversation as resolved.
Show resolved Hide resolved
auto vector = context.mark_node(std::make_shared<v0::Convert>(context.get_input(1), element::f32));
mvafin marked this conversation as resolved.
Show resolved Hide resolved
auto result = context.mark_node(std::make_shared<v1::Multiply>(matrix, vector)); // Custom operation: element-wise multiplication

Check warning on line 15 in src/frontends/pytorch/src/op/matmul.cpp

View workflow job for this annotation

GitHub Actions / clang-format

[reviewdog-suggester] reported by reviewdog 🐶 Raw Output: src/frontends/pytorch/src/op/matmul.cpp:15:- auto result = context.mark_node(std::make_shared<v1::Multiply>(matrix, vector)); // Custom operation: element-wise multiplication src/frontends/pytorch/src/op/matmul.cpp:14:+ auto result = context.mark_node( src/frontends/pytorch/src/op/matmul.cpp:15:+ std::make_shared<v1::Multiply>(matrix, vector)); // Custom operation: element-wise multiplication
mvafin marked this conversation as resolved.
Show resolved Hide resolved
return {result};
}
} // namespace op
} // namespace pytorch
} // namespace frontend
} // namespace ov
mvafin marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions src/frontends/pytorch/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ OP_CONVERTER(translate_linalg_matrix_norm);
OP_CONVERTER(translate_linalg_vector_norm);
OP_CONVERTER(translate_linear);
OP_CONVERTER(translate_linspace);
OP_CONVERTER(translate_mv);
mvafin marked this conversation as resolved.
Show resolved Hide resolved
OP_CONVERTER(translate_list_construct);
OP_CONVERTER(translate_list_unpack);
OP_CONVERTER(translate_log);
Expand Down Expand Up @@ -412,6 +413,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::linalg_vector_norm", op::translate_linalg_vector_norm},
{"aten::linear", op::translate_linear},
{"aten::linspace", op::translate_linspace},
{"aten::mv", op::translate_mv},
mvafin marked this conversation as resolved.
Show resolved Hide resolved
{"aten::log", op::translate_log},
{"aten::log_", op::inplace_op<op::translate_log>},
{"aten::logical_and", op::translate_and},
Expand Down
35 changes: 35 additions & 0 deletions tests/layer_tests/pytorch_tests/test_matmul.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import pytest
import torch
from pytorch_layer_test_class import PytorchLayerTest

class TestMVOperation(PytorchLayerTest):
def _prepare_input(self, matrix, vector):
matrix_input = np.array(matrix).astype(np.float32)
vector_input = np.array(vector).astype(np.float32)
return [matrix_input, vector_input]

def create_model(self, matrix, vector):
class CustomMVOperation(torch.nn.Module):
def forward(self, matrix, vector):
return matrix * vector # Element-wise multiplication as per the provided code
mvafin marked this conversation as resolved.
Show resolved Hide resolved

model_class = CustomMVOperation()
ref_net = None
return model_class, ref_net, "aten::mv"

@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.parametrize("matrix, vector", [
(np.array([[1, 2], [3, 4]]), np.array([5, 6])),
(np.array([[0, 0], [0, 0]]), np.array([1, 2])),
# Add more test cases as needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add maybe 1 or 2 more test cases for non-square matrix and 3x3 matrix

])
def test_mv_operation(self, matrix, vector, ie_device, precision, ir_version):
self._test(
*self.create_model(matrix, vector),
ie_device,
precision,
ir_version,
kwargs_to_prepare_input={"matrix": matrix, "vector": vector}
)
Loading