Publishing 2019 R3 content
[platform/upstream/dldt.git] / inference-engine / src / vpu / graph_transformer / include / vpu / custom_layer.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <memory>
8 #include <string>
9 #include <sstream>
10 #include <vector>
11 #include <map>
12 #include <functional>
13
14 #include <details/caseless.hpp>
15
16 #include <pugixml.hpp>
17
18 #include <vpu/utils/enums.hpp>
19 #include <vpu/utils/containers.hpp>
20
21 namespace vpu {
22
23 namespace ie = InferenceEngine;
24
25 VPU_DECLARE_ENUM(CustomDataFormat,
26     BYXF = 0,  // NHWC used in most software layers
27     BFYX = 1,  // NCHW used if HW module is enabled
28     YXF  = 2,  // HWC used in most software layers
29     FYX  = 3,  // CHW used if HW module is enabled
30     Any  = 4,  // doesn't really matter
31     None = 5
32 )
33
34 VPU_DECLARE_ENUM(CustomParamType,
35     Input,
36     Output,
37     Data,
38     InputBuffer,
39     OutputBuffer,
40     Int,
41     Float
42 )
43
44 VPU_DECLARE_ENUM(CustomDimSource,
45     Input,
46     Output
47 )
48
49 class CustomLayer final {
50 public:
51     using Ptr = std::shared_ptr<CustomLayer>;
52
53     struct KernelParam final {
54         CustomParamType type = CustomParamType::Input;
55         CustomDataFormat format = CustomDataFormat::Any;
56         std::string argName;
57         int portIndex = -1;
58         std::string irSource;
59         SmallVector<std::string> bufferSizeRules;
60         CustomDimSource dimSource;
61         int dimIdx = -1;
62     };
63
64     static ie::details::caseless_map<std::string, std::vector<CustomLayer::Ptr>> loadFromFile(
65                 const std::string& configFile,
66                 bool canBeMissed = false);
67
68     const std::string& kernelBinary() const { return _kernelBinary; }
69
70     void setStageNumInputs(int id);
71     int stageNumInputs() const;
72     int kernelAddress(int idx = 1) const;
73     int maxShaves() const;
74     const std::map<std::string, std::string>& whereParams() const { return _whereParams; }
75
76     const SmallVector<KernelParam>& bindings() const { return _kernelParams; }
77     const SmallVector<std::string>& parameters() const { return _parameters; }
78
79     const SmallVector<std::string>& globalSizeRules() const { return _globalSizeRules; }
80     const SmallVector<std::string>& localSizeRules() const { return _localSizeRules; }
81
82     CustomDimSource dimSource() const { return _wgDimSource; }
83     int dimSourceIndex() const { return _wgDimIdx; }
84
85 private:
86     explicit CustomLayer(const std::string& dirname) : _configDir(dirname) {}
87
88     void loadSingleLayer(const pugi::xml_node& node);
89     void processWhere(const pugi::xml_node& node);
90     void processKernelNode(const pugi::xml_node& node);
91     void processParametersNode(const pugi::xml_node& node);
92     void processWorkSizesNode(const pugi::xml_node& node);
93
94     static bool isLegalSizeRule(const std::string& rule);
95     static CustomDataFormat formatFromString(const std::string& str);
96
97 private:
98     std::string _configDir;
99     std::string _layerName;
100     std::string _kernelEntry;
101     std::string _kernelBinary;
102     std::map<std::string, std::string> _whereParams;
103
104     int _maxShaves = 0;
105     int _stageNumInputs = -1;
106
107     SmallVector<KernelParam> _kernelParams;
108     SmallVector<std::string> _globalSizeRules;
109     SmallVector<std::string> _localSizeRules;
110     SmallVector<std::string> _parameters;
111
112     std::map<uint32_t, uint32_t, std::greater<uint32_t>> _kernelAddress;
113
114     CustomDimSource _wgDimSource = CustomDimSource::Input;
115     int _wgDimIdx = -1;
116 };
117
118 };  // namespace vpu