Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / shape_infer / built-in / ie_gemm_shape_infer.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <description_buffer.hpp>
8 #include "ie_built_in_impl.hpp"
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <vector>
13 #include <debug.h>
14 #include <cmath>
15 #include <algorithm>
16
17 namespace InferenceEngine {
18 namespace ShapeInfer {
19
20 /**
21  *@brief Implementation of Shape inference for Gemm layer
22  */
23 class GemmShapeProp : public BuiltInShapeInferImpl {
24 public:
25     explicit GemmShapeProp(const std::string& type) : BuiltInShapeInferImpl(type) {}
26
27     void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs,
28                          const std::map<std::string, std::string>& params,
29                          const std::map<std::string, Blob::Ptr>& blobs,
30                          std::vector<SizeVector>& outShapes) override {
31         // TODO: primitive does not support 5D tensor yet
32         LayerParams lp{};
33         GemmLayer gemmLayer(lp);
34         gemmLayer.params = params;
35         gemmLayer.type = _type;
36         validate(&gemmLayer, inBlobs, params, blobs);
37
38         auto dims0 = inShapes[0];
39         auto dims1 = inShapes[1];
40
41         SizeVector shapes;
42         for (int idx = 0; idx < dims0.size() - 2; idx++) {
43             unsigned long max_dim = dims0[idx] > dims1[idx] ? dims0[idx] : dims1[idx];
44
45             if (inShapes.size() == 3) {
46                 auto dims2 = inShapes[2];
47                 max_dim = max_dim > dims2[idx] ? max_dim : dims2[idx];
48             }
49
50             shapes.push_back(max_dim);
51         }
52
53         unsigned long xAxis = dims0.size() - 1;
54         unsigned long yAxis = dims0.size() - 2;
55
56         shapes.push_back(dims0[yAxis]);
57         shapes.push_back(dims1[xAxis]);
58         outShapes.push_back(shapes);
59     }
60 };
61
62 }  // namespace ShapeInfer
63 }  // namespace InferenceEngine