Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / shape_infer / built-in / ie_built_in_impl.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <string>
8 #include <vector>
9 #include <list>
10 #include <map>
11 #include <memory>
12
13 #include <ie_layers.h>
14 #include <ie_iextension.h>
15 #include "details/caseless.hpp"
16 #include <description_buffer.hpp>
17 #include <ie_layer_validators.hpp>
18
19 namespace InferenceEngine {
20 namespace ShapeInfer {
21
22 /**
23  *@brief Base class for all built-in shape infer implementations. Contains common logic with validators and errors handling
24  */
25 class BuiltInShapeInferImpl : public IShapeInferImpl {
26 public:
27     explicit BuiltInShapeInferImpl(const std::string& type) : _type(type) {
28         _validator = details::LayerValidators::getInstance()->getValidator(_type);
29         if (!_validator)
30             THROW_IE_EXCEPTION << "Internal error: failed to find validator for layer with type: " << _type;
31     }
32
33     void validate(CNNLayer* layer, const std::vector<Blob::CPtr>& inBlobs,
34                   const std::map<std::string, std::string>& params,
35                   const std::map<std::string, Blob::Ptr>& blobs) {
36         _validator->parseParams(layer);
37         _validator->checkParams(layer);
38         _validator->checkShapes(layer, inShapes);
39         _validator->checkCorrespondence(layer, blobs, inShapes);
40     }
41
42     virtual void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs,
43                                  const std::map<std::string, std::string>& params,
44                                  const std::map<std::string, Blob::Ptr>& blobs,
45                                  std::vector<SizeVector>& outShapes) = 0;
46
47     StatusCode inferShapes(const std::vector<SizeVector>& inShapes,
48                            const std::map<std::string, std::string>& params,
49                            const std::map<std::string, Blob::Ptr>& blobs,
50                            std::vector<SizeVector>& outShapes,
51                            ResponseDesc* resp) noexcept override {
52         return DescriptionBuffer(GENERAL_ERROR, resp)
53                 << "Unexpected call of deprecated Shape Infer function with input shapes";
54     }
55
56     StatusCode inferShapes(const std::vector<Blob::CPtr>& inBlobs,
57                            const std::map<std::string, std::string>& params,
58                            const std::map<std::string, Blob::Ptr>& blobs,
59                            std::vector<SizeVector>& outShapes,
60                            ResponseDesc* resp) noexcept override {
61         inShapes.clear();
62         for (const auto& blob : inBlobs) {
63             inShapes.push_back(blob->getTensorDesc().getDims());
64         }
65         outShapes.clear();
66         try {
67             inferShapesImpl(inBlobs, params, blobs, outShapes);
68             return OK;
69         } catch (const std::exception& ex) {
70             return InferenceEngine::DescriptionBuffer(GENERAL_ERROR, resp) << ex.what();
71         } catch (...) {
72             return InferenceEngine::DescriptionBuffer(UNEXPECTED) << "Unknown error";
73         }
74     }
75
76 protected:
77     std::string _type;
78     details::LayerValidator::Ptr _validator;
79     std::vector<SizeVector> inShapes;
80 };
81
82 }  // namespace ShapeInfer
83 }  // namespace InferenceEngine