Skip to content

Commit

Permalink
Fixed OpenVINO gemm layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Lyulkov committed May 1, 2024
1 parent 94f4678 commit e00f234
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions modules/dnn/src/layers/gemm_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,48 +289,63 @@ 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;

if (nodes.size() == 2)
{
auto& inp2 = nodes[1].dynamicCast<InfEngineNgraphNode>()->node;
matmul = std::make_shared<ov::op::v0::MatMul>(ieInpNode, inp2, trans_a, trans_b);
}
auto nodeA = nodes[0].dynamicCast<InfEngineNgraphNode>()->node;
std::shared_ptr<ov::Node> nodeAB;

if (const_B)
nodeAB = std::make_shared<ov::op::v0::MatMul>(
nodeA,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, getShape(blobs[0]), blobs[0].data),
trans_a,
trans_b);
else
nodeAB = std::make_shared<ov::op::v0::MatMul>(
nodeA,
nodes[1].dynamicCast<InfEngineNgraphNode>()->node,
trans_a,
trans_b);

if (alpha != 1.0f)
{
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);
}
if (alpha != 1.0f) {
matmul = std::make_shared<ov::op::v1::Multiply>(matmul,
nodeAB = std::make_shared<ov::op::v1::Multiply>(
nodeAB,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &alpha)
);
}

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 (!have_bias)
return Ptr<BackendNode>(new InfEngineNgraphNode(nodeAB));

std::shared_ptr<ov::Node> nodeGemm;
if (beta != 1.0f)
{
std::shared_ptr<ov::Node> nodeC;
if (const_C)
nodeC = std::make_shared<ov::op::v1::Multiply>(
std::make_shared<ov::op::v0::Constant>(ov::element::f32, getShape(blobs.back()), blobs.back().data),
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &beta));
else
nodeC = std::make_shared<ov::op::v1::Multiply>(
nodes.back().dynamicCast<InfEngineNgraphNode>()->node,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &beta));

nodeGemm = std::make_shared<ov::op::v1::Add>(nodeAB, nodeC, ov::op::AutoBroadcastType::NUMPY);
}
else
{
if (const_C)
nodeGemm = std::make_shared<ov::op::v1::Add>(
nodeAB,
std::make_shared<ov::op::v0::Constant>(ov::element::f32, getShape(blobs.back()), blobs.back().data),
ov::op::AutoBroadcastType::NUMPY);
else
nodeGemm = std::make_shared<ov::op::v1::Add>(
nodeAB,
nodes.back().dynamicCast<InfEngineNgraphNode>()->node,
ov::op::AutoBroadcastType::NUMPY);
}
return Ptr<BackendNode>(new InfEngineNgraphNode(matmul));

return Ptr<BackendNode>(new InfEngineNgraphNode(nodeGemm));
}
#endif // HAVE_DNN_NGRAPH

Expand Down

0 comments on commit e00f234

Please sign in to comment.