26 #ifndef NVNEURAL_MODELPREPROCESSOR_H
27 #define NVNEURAL_MODELPREPROCESSOR_H
30 #include <Pugixml/pugixml.hpp>
32 #include <unordered_map>
33 #include <unordered_set>
36 namespace nvneural {
namespace detail {
44 class XmlModelPreprocessor
47 explicit XmlModelPreprocessor(XmlModelPreprocessor* pParent =
nullptr,
bool inlineModels =
false);
48 ~XmlModelPreprocessor();
51 void addCustomLayer(
const std::string& layer);
53 enum class FusingRuleResult : std::uint16_t
59 FusingRuleResult addFusingScript(
const std::string& script);
62 FusingRuleResult addScopedFusingScript(
const TensorFormat& currentScope,
const std::string& script);
63 void setScriptDebug(
bool enable);
65 void addCutLayer(
const std::string name);
67 bool loadModelText(
const std::string& text,
bool autoUpdateModelVersion =
true);
68 bool loadModelFile(
const std::string& fileName,
bool autoUpdateModelVersion =
true);
69 bool generateLinked(pugi::xml_document& doc);
71 bool supportedByEditor()
const;
73 static void pushIncludeDirectory(
const std::string& path);
82 const std::vector<ioDesc>& getOutputs()
const;
83 const std::vector<ioDesc>& getInputs()
const;
85 int searchOutputDescIndex(
const std::string& layerName)
const;
88 static void cloneXmlNode(pugi::xml_node& dest,
const pugi::xml_node& src);
89 static bool isCustomScripted(
const pugi::xml_node& layer);
90 static bool isInputTypeLayer(
const std::string& type);
92 const static std::vector<std::string> m_prototypeCode;
94 int currentModelVersion()
const;
95 static int actualModelVersion();
96 void updateModelVersion();
98 std::string getLastError()
const;
100 std::unordered_map<std::string, std::vector<std::string>> getInnerInlinedTemplates()
const;
101 std::unordered_map<std::string, std::string> getSynonyms()
const {
return m_synonyms; }
103 std::unordered_map<std::string, std::shared_ptr<XmlModelPreprocessor>> getInnerTemplates()
const;
110 enum TypeCode : uint8_t
121 TypeCode type = FATAL_ERROR;
124 std::vector<rulex> lexParser(
const std::string& text);
126 struct ruleSyntaxTreeNode
143 TypeCode type = TypeCode::FATAL_ERROR;
146 std::vector<ruleSyntaxTreeNode> params;
148 ruleSyntaxTreeNode syntaxParser(std::vector<rulex>& lexemes);
149 ruleSyntaxTreeNode syntaxParseLayer(std::vector<rulex>& lexemes,
size_t& offset);
150 ruleSyntaxTreeNode syntaxParseConditionOr(std::vector<rulex>& lexemes,
size_t& offset);
151 ruleSyntaxTreeNode syntaxParseInputs(std::vector<rulex>& lexemes,
size_t& offset);
152 ruleSyntaxTreeNode syntaxParseSelection(std::vector<rulex>& lexemes,
size_t& offset);
153 ruleSyntaxTreeNode syntaxParseConditionAnd(std::vector<rulex>& lexemes,
size_t& offset);
154 ruleSyntaxTreeNode syntaxParseComparison(std::vector<rulex>& lexemes,
size_t& offset);
155 ruleSyntaxTreeNode syntaxParseOperand(std::vector<rulex>& lexemes,
size_t& offset);
156 ruleSyntaxTreeNode syntaxParseRange(std::vector<rulex>& lexemes,
size_t& offset);
157 void syntaxError(
const ruleSyntaxTreeNode& errorResult,
size_t offset)
const;
159 bool m_debugFusingScript =
false;
160 void printSyntaxTree(
const ruleSyntaxTreeNode& node,
int indent)
const;
162 std::vector<ruleSyntaxTreeNode> m_fusingScripts;
164 static std::vector<std::string> s_includeDirs;
165 static std::string findFile(
const std::string& fileName);
167 XmlModelPreprocessor* m_pParent;
169 std::string m_fileName;
170 bool checkExternalCollision(
const std::string& fileName)
const;
172 std::string m_lastError;
173 void setError(
const std::string& text);
174 void setError(
const std::string& text,
const pugi::xml_node& node);
175 void addError(
const std::string& text);
177 struct fusedTemplateLayerNode
180 pugi::xml_node templateNode;
181 std::string templateInstanceScope;
184 std::size_t fuseIndex;
187 struct modelLayerNode
189 std::string finalName;
190 std::vector<std::string> inputList;
191 pugi::xml_node xmlNode;
192 pugi::xml_node templateNode;
196 std::string templateInstanceScope;
197 bool isFused =
false;
199 std::vector<fusedTemplateLayerNode> fusedTemplateLayerNodes;
202 std::string inputLayerName(std::string input);
203 std::string inputLayerName(modelLayerNode &node,
size_t index);
205 std::vector<std::string> m_inputs;
206 std::vector<ioDesc> m_inputDescriptors;
207 std::unordered_map<std::string, modelLayerNode> m_table;
208 std::vector<std::string> m_outputs;
209 std::unordered_map<std::string, std::string> m_outputLink;
210 std::vector<ioDesc> m_outputDescriptors;
211 std::unordered_map<std::string, std::string> m_customLayers;
212 std::unordered_set<std::string> m_cutLayers;
213 std::unordered_map<std::string, std::vector<std::string>> m_innerInlinedTemplateLayers;
214 std::unordered_map<std::string, std::string> m_synonyms;
216 std::vector<pugi::xml_node> m_extraNodes;
218 bool tryFusingLayer(modelLayerNode& layer, ruleSyntaxTreeNode& rule);
219 std::string collectFusingInputs(modelLayerNode& layer, std::vector<std::string>& fusingInputs);
220 bool interpretLayer(modelLayerNode& layer, ruleSyntaxTreeNode& rule, std::vector<std::string>& fused);
221 bool interpretCondition(modelLayerNode& layer, ruleSyntaxTreeNode& rule);
222 bool interpretInputs(modelLayerNode& layer, ruleSyntaxTreeNode& rule, std::vector<std::string>& fused,
size_t& in_index);
223 bool interpretSelection(modelLayerNode& layer, ruleSyntaxTreeNode& rule, std::vector<std::string>& fused,
size_t& in_index);
224 bool interpretParameter(modelLayerNode& layer, ruleSyntaxTreeNode& rule, rulex& op);
225 bool interpretCheckBreak(ruleSyntaxTreeNode& rule,
int count);
227 XmlModelPreprocessor* getSubgraphAsModel(
228 const std::vector<std::string>& inputs,
229 const std::vector<std::string>& outputs);
232 pugi::xml_node& network,
233 const XmlModelPreprocessor* pTempModel,
234 std::unordered_map<std::string, std::string> &synonym,
235 const std::string& name,
236 pugi::xml_node& layer,
237 const std::string& input,
238 bool isInnerTemplate);
240 pugi::xml_document m_model;
242 bool parseModel(pugi::xml_node networkModel,
int version);
243 bool backWayCollector(
244 const std::string& node,
245 std::unordered_set<std::string>& collection,
246 std::vector<std::string>& controlStack,
247 const std::vector<std::string>* pStopList);
249 std::unordered_map<std::string, std::shared_ptr<XmlModelPreprocessor>> m_innerTemplates;
250 std::unordered_map<std::string, std::unique_ptr<XmlModelPreprocessor>> m_externTemplates;
251 std::unordered_map<std::string, std::unique_ptr<XmlModelPreprocessor>> m_patternTemplates;
253 enum class FusingRuleLayoutScope
260 enum class FusingRuleDataTypeScope
267 bool preprocessFusingScriptScope(std::vector<rulex>& lexemes, FusingRuleLayoutScope& layoutScope, FusingRuleDataTypeScope& dataTypeScope)
const;
268 bool checkAndAddFusingScript(
const std::vector<rulex> lexemes);
269 bool checkFusingScript(
const std::vector<rulex>& lexemes);
Fundamental NvNeural data types are declared here.
@ Float
32-bit floating point elements (float)
@ Half
16-bit floating point elements (__half)
@ Success
Operation succeeded. Generic result.
@ Failure
Operation failed. Generic result.