Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
74 changes: 40 additions & 34 deletions modules/dnn/src/layers/gemm_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,48 +289,54 @@ class GemmLayerImpl CV_FINAL : public GemmLayer {
virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inputs,
const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE
{
auto ieInpNode = nodes[0].dynamicCast<InfEngineNgraphNode>()->node;
std::shared_ptr<ov::Node> matmul;
ov::Output<ov::Node> nodeA = nodes[0].dynamicCast<InfEngineNgraphNode>()->node;
ov::Output<ov::Node> nodeB;
if (const_B)
nodeB = std::make_shared<ov::op::v0::Constant>(ov::element::f32, getShape(blobs[0]), blobs[0].data);
else
nodeB = nodes[1].dynamicCast<InfEngineNgraphNode>()->node;

int flatten_axis = nodeA.get_shape().size() - nodeB.get_shape().size();
if (flatten_axis > 0) {
std::vector<int> shape(1 + flatten_axis, 0);
shape[shape.size() - 1] = -1;
nodeA = std::make_shared<ov::op::v1::Reshape>(
nodeA,
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{shape.size()}, shape.data()),
true);
}

if (nodes.size() == 2)
std::shared_ptr<ov::Node> nodeAB = std::make_shared<ov::op::v0::MatMul>(nodeA, nodeB, trans_a, trans_b);
if (alpha != 1.0f)
{
auto& inp2 = nodes[1].dynamicCast<InfEngineNgraphNode>()->node;
matmul = std::make_shared<ov::op::v0::MatMul>(ieInpNode, inp2, trans_a, trans_b);
nodeAB = std::make_shared<ov::op::v1::Multiply>(
nodeAB,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &alpha));
}
else

if (!have_bias)
return Ptr<BackendNode>(new InfEngineNgraphNode(nodeAB));

ov::Output<ov::Node> nodeC;
if (const_C)
{
std::shared_ptr<ov::Node> ieWeights = std::make_shared<ov::op::v0::Constant>(ov::element::f32, getShape(blobs[0]), blobs[0].data);

int flatten_axis = ieInpNode.get_shape().size() - ieWeights->get_shape().size();
if (flatten_axis > 0) {
std::vector<int> shape(1 + flatten_axis, 0);
shape[shape.size() - 1] = -1;
ieInpNode = std::make_shared<ov::op::v1::Reshape>(
ieInpNode,
std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{shape.size()}, shape.data()),
true
);
}
matmul = std::make_shared<ov::op::v0::MatMul>(ieInpNode, ieWeights, trans_a, trans_b);
auto shape_C = blobs.back().total() == blobs.back().size[0] ? ov::Shape{blobs.back().total()} : getShape(blobs.back());
nodeC = std::make_shared<ov::op::v0::Constant>(ov::element::f32, shape_C, blobs.back().data);
}
if (alpha != 1.0f) {
matmul = std::make_shared<ov::op::v1::Multiply>(matmul,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &alpha)
);
else
{
nodeC = nodes.back().dynamicCast<InfEngineNgraphNode>()->node;
}

if (have_bias && const_C) {
Mat bias = blobs.back();
auto shape = bias.total() == bias.size[0] ? ov::Shape{bias.total()} : getShape(bias);
std::shared_ptr<ov::Node> bias_node = std::make_shared<ov::op::v0::Constant>(ov::element::f32, shape, bias.data);
if (beta != 1.0f) {
bias_node = std::make_shared<ov::op::v1::Multiply>(bias_node,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &beta)
);
}
matmul = std::make_shared<ov::op::v1::Add>(matmul, bias_node, ov::op::AutoBroadcastType::NUMPY);
if (beta != 1.0f)
{
nodeC = std::make_shared<ov::op::v1::Multiply>(
nodeC,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &beta));
}
return Ptr<BackendNode>(new InfEngineNgraphNode(matmul));

auto nodeGemm = std::make_shared<ov::op::v1::Add>(nodeAB, nodeC, ov::op::AutoBroadcastType::NUMPY);
return Ptr<BackendNode>(new InfEngineNgraphNode(nodeGemm));
}
#endif // HAVE_DNN_NGRAPH

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@
"test_gemm_all_attributes",
"test_gemm_alpha",
"test_gemm_beta",
"test_gemm_default_matrix_bias",
"test_gemm_default_no_bias",
Comment on lines -124 to -125
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove.

"test_gemm_default_scalar_bias",
"test_gemm_default_single_elem_vector_bias",
"test_gemm_default_vector_bias",
Expand Down