NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
XmlNetworkBuilder.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_XMLNETWORKBUILDER_H
27 #define NVNEURAL_XMLNETWORKBUILDER_H
28 
29 #include <functional>
30 #include <nvneural/CoreTypes.h>
31 #include <nvneural/RefPtr.h>
32 #include <map>
33 #include <set>
34 #include <string>
35 #include <Pugixml/pugixml.hpp>
36 
37 namespace nvneural {
38 
39  class ClassRegistry;
40  class INetwork;
41  namespace detail {
42  class XmlModelPreprocessor;
43  }
44 
57  {
58  public:
62  explicit XmlNetworkBuilder(const IClassRegistry* pRegistry);
63 
68  NeuralResult buildNetworkFromFile(INetwork* pNetwork, const char* pFilename) const;
69 
74  NeuralResult buildNetworkFromXmlLiteral(INetwork* pNetwork, const char* pXmlLiteral) const;
75 
92  NeuralResult setLayerOverride(const char* pLayerName, const char* pObjectClass);
93 
96 
107  NeuralResult setFallbackLayerType(const char* pObjectClass);
108 
115  using LayerScopeCallbackFunc = std::function<void(const char* pLayerName)>;
116 
133 
139  NeuralResult setLoadDefaultFusingRules(bool enableDefaultFusing);
140 
144  bool loadDefaultFusingRules() const;
145 
151  NeuralResult addFusingRule(const char* pFusingRule);
152 
159  NeuralResult setInliningEnabled(bool enableInlining);
160 
163 
164  protected:
173  virtual RefPtr<ILayer> createLayerObject(const std::string& name, const std::string& layerType, const std::string& objectClass) const;
174 
183  virtual RefPtr<IParameterNode> createParameterNode(ILayer* pLayer, pugi::xml_node node) const;
184 
198  virtual NeuralResult setLayerActivation(ILayer* pLayer, ActivationFunctionId activationId, const std::string& activationName) const;
199 
209  virtual NeuralResult setLayerAlpha(ILayer* pLayer, size_t alphaIndex, float alphaValue) const;
210 
220  virtual NeuralResult setLayerInput(ILayer* pLayer, size_t inputIndex, ILayer* pInput) const;
221 
222  private:
223  struct PrototypeInfo
224  {
225  std::map<std::string, unsigned int> activePrototypeVersionToId;
226  pugi::xml_node xmlNode;
227  };
228 
229  RefPtr<const IClassRegistry> m_pRegistry;
230  bool m_enableDefaultFusing;
231  std::map<std::string, std::string> m_layerOverrides;
232  std::vector<std::string> m_manualFusingRules;
233  mutable std::vector<std::string> m_validFusingRules;
234  std::string m_fallbackClass;
235  LayerScopeCallbackFunc m_scopeCallback;
236  bool m_enableInlining;
237  RefPtr<IRuntimeOptionsHost> m_pRuntimeOptionsHost;
238 
239  static int scoreLayerCandidate(NetworkBackendId objectBackend, const TensorFormat& objectTensorFormat, const INetworkRuntime* pNetwork);
240  static int scorePrototypeCandidate(const PrototypeDesc& prototypeDesc, const INetworkRuntime* pNetwork);
241 
242  NeuralResult buildNetworkFromPreprocessor(INetwork* pNetwork, detail::XmlModelPreprocessor& preprocessor) const;
243  NeuralResult initPreprocessor(INetwork* pNetwork, detail::XmlModelPreprocessor& preprocessor) const;
244  };
245 
246 } // namespace nvneural
247 
248 #endif // NVNEURAL_XMLNETWORKBUILDER_H
Fundamental NvNeural data types are declared here.
NetworkBackendId
Enumeration describing common network backends.
Definition: CoreTypes.h:239
ActivationFunctionId
Enumeration describing common activation functions.
Definition: CoreTypes.h:259
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
Smart pointer class for tracking IRefObject instances.
Common class registry for instantiating objects and collecting plugin-provided descriptors.
Definition: CoreTypes.h:1801
ILayer is the base class for neural network layers.
Definition: LayerTypes.h:59
INetwork is the public interface to Network.
Definition: NetworkTypes.h:119
INetworkRuntime is a subset of the basic network interface that is accessible from layer classes duri...
Definition: CoreTypes.h:1129
IRuntimeOptionsHost is an interface that provides runtime option communication from the layer to the ...
Definition: CoreTypes.h:544
Intrusive pointer using IRefObject's reference counting system.
Definition: RefPtr.h:46
XML-based network builder.
Definition: XmlNetworkBuilder.h:57
XmlNetworkBuilder(const IClassRegistry *pRegistry)
Create an XmlNetworkBuilder.
Definition: XmlNetworkBuilder.cpp:77
NeuralResult setRuntimeOptionsHost(IRuntimeOptionsHost *pRuntimeOptionsHost)
Set the runtime options host pointer to pass to layers during building.
Definition: XmlNetworkBuilder.cpp:799
NeuralResult buildNetworkFromFile(INetwork *pNetwork, const char *pFilename) const
Builds a network from a file on disk.
Definition: XmlNetworkBuilder.cpp:208
virtual RefPtr< ILayer > createLayerObject(const std::string &name, const std::string &layerType, const std::string &objectClass) const
Creates a layer object for a given object class.
Definition: XmlNetworkBuilder.cpp:805
NeuralResult setInliningEnabled(bool enableInlining)
Enable or disable network inlining during preprocessing.
Definition: XmlNetworkBuilder.cpp:793
NeuralResult setScopeCallbackFunc(LayerScopeCallbackFunc scopeCallback)
Sets a function that will be called around layer-specific operations during buildNetwork calls.
Definition: XmlNetworkBuilder.cpp:126
virtual NeuralResult setLayerActivation(ILayer *pLayer, ActivationFunctionId activationId, const std::string &activationName) const
Sets an activation function on the indicated layer.
Definition: XmlNetworkBuilder.cpp:822
virtual RefPtr< IParameterNode > createParameterNode(ILayer *pLayer, pugi::xml_node node) const
Creates an appropriate parameter node for a given <Parameters> block.
Definition: XmlNetworkBuilder.cpp:817
NeuralResult clearLayerOverrides()
Removes all layer overrides.
Definition: XmlNetworkBuilder.cpp:84
NeuralResult setLoadDefaultFusingRules(bool enableDefaultFusing)
Turns ClassRegistry-based fusing rules on or off.
Definition: XmlNetworkBuilder.cpp:258
virtual NeuralResult setLayerInput(ILayer *pLayer, size_t inputIndex, ILayer *pInput) const
Sets an input on the indicated layer.
Definition: XmlNetworkBuilder.cpp:832
NeuralResult setLayerOverride(const char *pLayerName, const char *pObjectClass)
Overrides construction of a layer.
Definition: XmlNetworkBuilder.cpp:90
bool loadDefaultFusingRules() const
Returns true if ClassRegistry-based fusing rules are enabled.
Definition: XmlNetworkBuilder.cpp:264
NeuralResult buildNetworkFromXmlLiteral(INetwork *pNetwork, const char *pXmlLiteral) const
Builds a network from a provided XML literal.
Definition: XmlNetworkBuilder.cpp:233
NeuralResult setFallbackLayerType(const char *pObjectClass)
Set an emergency fallback object class when no candidates match the layer type.
Definition: XmlNetworkBuilder.cpp:113
std::function< void(const char *pLayerName)> LayerScopeCallbackFunc
Function type for setScopeCallbackFunc.
Definition: XmlNetworkBuilder.h:115
virtual NeuralResult setLayerAlpha(ILayer *pLayer, size_t alphaIndex, float alphaValue) const
Sets an activation coefficient on the indicated layer.
Definition: XmlNetworkBuilder.cpp:827
NeuralResult addFusingRule(const char *pFusingRule)
Adds a fusing rule to network construction.
Definition: XmlNetworkBuilder.cpp:269
This structure represent a prototype layer version (backend, tensor format and kernel type),...
Definition: CoreTypes.h:1697
TensorFormat describes a specific tensor shape (element type and layout).
Definition: CoreTypes.h:88