Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / builders / ie_layer_builder.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <builders/ie_layer_builder.hpp>
6 #include <details/caseless.hpp>
7
8 #include <limits>
9 #include <memory>
10 #include <vector>
11 #include <string>
12 #include <map>
13
14 using namespace InferenceEngine;
15
16 Builder::Layer::Layer(const std::string& type, const std::string& name):
17         name(name), type(type), id((std::numeric_limits<idx_t>::max)()) {}
18
19 Builder::Layer::Layer(const ILayer::CPtr& layer) {
20     id = layer->getId();
21     name = layer->getName();
22     type = layer->getType();
23     inPorts = layer->getInputPorts();
24     outPorts = layer->getOutputPorts();
25     params = layer->getParameters();
26 }
27
28 Builder::Layer::Layer(idx_t id, const Builder::Layer& layer): Layer(layer) {
29     this->id = id;
30 }
31
32 idx_t Builder::Layer::getId() const noexcept {
33     return id;
34 }
35
36 const std::string& Builder::Layer::getType() const noexcept {
37     return type;
38 }
39 Builder::Layer& Builder::Layer::setType(const std::string& type) {
40     this->type = type;
41     return *this;
42 }
43
44 const std::string& Builder::Layer::getName() const noexcept {
45     return name;
46 }
47 Builder::Layer& Builder::Layer::setName(const std::string& name) {
48     this->name = name;
49     return *this;
50 }
51
52 const std::map<std::string, Parameter>& Builder::Layer::getParameters() const noexcept {
53     return params;
54 }
55 std::map<std::string, Parameter>& Builder::Layer::getParameters() {
56     return params;
57 }
58 Builder::Layer& Builder::Layer::setParameters(const std::map<std::string, Parameter>& params) {
59     getParameters() = params;
60     return *this;
61 }
62
63 std::vector<Port>& Builder::Layer::getInputPorts() {
64     return inPorts;
65 }
66 const std::vector<Port>& Builder::Layer::getInputPorts() const noexcept {
67     return inPorts;
68 }
69 Builder::Layer& Builder::Layer::setInputPorts(const std::vector<Port> &ports) {
70     getInputPorts() = ports;
71     return *this;
72 }
73
74 std::vector<Port>& Builder::Layer::getOutputPorts() {
75     return outPorts;
76 }
77 const std::vector<Port>& Builder::Layer::getOutputPorts() const noexcept {
78     return outPorts;
79 }
80 Builder::Layer& Builder::Layer::setOutputPorts(const std::vector<Port> &ports) {
81     getOutputPorts() = ports;
82     return *this;
83 }
84
85 const ILayer::CPtr Builder::Layer::build() const {
86     validate(true);
87     return std::static_pointer_cast<const ILayer>(shared_from_this());
88 }
89
90 void Builder::Layer::addValidator(const std::string &type, const std::function<void(const Layer::CPtr&, bool)>& validator) {
91     auto holder = getValidatorsHolder();
92     if (holder->validators.find(type) == holder->validators.end())
93         holder->validators[type] = validator;
94 }
95
96 void Builder::Layer::validate(bool partial) const {
97     if (getValidatorsHolder()->validators.find(type) != getValidatorsHolder()->validators.end())
98         getValidatorsHolder()->validators[type](shared_from_this(), partial);
99 }
100
101 std::shared_ptr<Builder::ValidatorsHolder> Builder::Layer::getValidatorsHolder() {
102     static std::shared_ptr<ValidatorsHolder> localHolder;
103     if (localHolder == nullptr) {
104         localHolder = std::make_shared<ValidatorsHolder>();
105     }
106     return localHolder;
107 }