7d1375194785827aed03789b5594e1596f6c0e31
[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,  // HWC used in most software layers
27     BFYX = 1,  // CHW used if HW module is enabled
28     Any  = 2,  // doesn't really matter
29     None = 3
30 )
31
32 VPU_DECLARE_ENUM(CustomParamType,
33     Input,
34     Output,
35     Data,
36     InputBuffer,
37     OutputBuffer,
38     Int,
39     Float
40 )
41
42 VPU_DECLARE_ENUM(CustomDimSource,
43     Input,
44     Output
45 )
46
47 class CustomLayer final {
48 public:
49     using Ptr = std::shared_ptr<CustomLayer>;
50
51     struct KernelParam final {
52         CustomParamType type = CustomParamType::Input;
53         CustomDataFormat format = CustomDataFormat::Any;
54         std::string argName;
55         int portIndex = -1;
56         std::string irSource;
57         SmallVector<std::string> bufferSizeRules;
58         CustomDimSource dimSource;
59         int dimIdx = -1;
60     };
61
62     static ie::details::caseless_map<std::string, std::vector<CustomLayer::Ptr>> loadFromFile(
63                 const std::string& configFile,
64                 bool canBeMissed = false);
65
66     const std::string& kernelBinary() const { return _kernelBinary; }
67
68     void setStageNumInputs(int id);
69     int stageNumInputs() const;
70     int kernelAddress(int idx = 1) const;
71     int maxShaves() const;
72     const std::map<std::string, std::string>& whereParams() const { return _whereParams; }
73
74     const SmallVector<KernelParam>& bindings() const { return _kernelParams; }
75     const SmallVector<std::string>& parameters() const { return _parameters; }
76
77     const SmallVector<std::string>& globalSizeRules() const { return _globalSizeRules; }
78     const SmallVector<std::string>& localSizeRules() const { return _localSizeRules; }
79
80     CustomDimSource dimSource() const { return _wgDimSource; }
81     int dimSourceIndex() const { return _wgDimIdx; }
82
83 private:
84     explicit CustomLayer(const std::string& dirname) : _configDir(dirname) {}
85
86     void loadSingleLayer(const pugi::xml_node& node);
87     void processWhere(const pugi::xml_node& node);
88     void processKernelNode(const pugi::xml_node& node);
89     void processParametersNode(const pugi::xml_node& node);
90     void processWorkSizesNode(const pugi::xml_node& node);
91
92     static bool isLegalSizeRule(const std::string& rule);
93     static CustomDataFormat formatFromString(const std::string& str);
94
95 private:
96     std::string _configDir;
97     std::string _layerName;
98     std::string _kernelEntry;
99     std::string _kernelBinary;
100     std::map<std::string, std::string> _whereParams;
101
102     int _maxShaves = 0;
103     int _stageNumInputs = -1;
104
105     SmallVector<KernelParam> _kernelParams;
106     SmallVector<std::string> _globalSizeRules;
107     SmallVector<std::string> _localSizeRules;
108     SmallVector<std::string> _parameters;
109
110     std::map<uint32_t, uint32_t, std::greater<uint32_t>> _kernelAddress;
111
112     CustomDimSource _wgDimSource = CustomDimSource::Input;
113     int _wgDimIdx = -1;
114 };
115
116 };  // namespace vpu