NVIDIA NvNeural SDK  2021.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:
172  virtual RefPtr<ILayer> createLayerObject(const std::string& name, const std::string& objectClass) const;
173 
182  virtual RefPtr<IParameterNode> createParameterNode(ILayer* pLayer, pugi::xml_node node) const;
183 
197  virtual NeuralResult setLayerActivation(ILayer* pLayer, ActivationFunctionId activationId, const std::string& activationName) const;
198 
208  virtual NeuralResult setLayerAlpha(ILayer* pLayer, size_t alphaIndex, float alphaValue) const;
209 
219  virtual NeuralResult setLayerInput(ILayer* pLayer, size_t inputIndex, ILayer* pInput) const;
220 
221  private:
222  struct PrototypeInfo
223  {
224  std::map<std::string, unsigned int> activePrototypeVersionToId;
225  pugi::xml_node xmlNode;
226  };
227 
228  RefPtr<const IClassRegistry> m_pRegistry;
229  bool m_enableDefaultFusing;
230  std::map<std::string, std::string> m_layerOverrides;
231  std::vector<std::string> m_manualFusingRules;
232  mutable std::vector<std::string> m_validFusingRules;
233  std::string m_fallbackClass;
234  LayerScopeCallbackFunc m_scopeCallback;
235  bool m_enableInlining;
236  RefPtr<IRuntimeOptionsHost> m_pRuntimeOptionsHost;
237 
238  static int scoreLayerCandidate(NetworkBackendId objectBackend, const TensorFormat& objectTensorFormat, const INetworkRuntime* pNetwork);
239  static int scorePrototypeCandidate(const PrototypeDesc& prototypeDesc, const INetworkRuntime* pNetwork);
240 
241  NeuralResult buildNetworkFromPreprocessor(INetwork* pNetwork, detail::XmlModelPreprocessor& preprocessor) const;
242  NeuralResult initPreprocessor(INetwork* pNetwork, detail::XmlModelPreprocessor& preprocessor) const;
243  };
244 
245 } // namespace nvneural
246 
247 #endif // NVNEURAL_XMLNETWORKBUILDER_H
Fundamental NvNeural data types are declared here.
NetworkBackendId
Enumeration describing common network backends.
Definition: CoreTypes.h:238
ActivationFunctionId
Enumeration describing common activation functions.
Definition: CoreTypes.h:258
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:274
Smart pointer class for tracking IRefObject instances.
Common class registry for instantiating objects and collecting plugin-provided descriptors.
Definition: CoreTypes.h:1750
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:1128
IRuntimeOptionsHost is an interface that provides runtime option communication from the layer to the ...
Definition: CoreTypes.h:543
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:44
NeuralResult setRuntimeOptionsHost(IRuntimeOptionsHost *pRuntimeOptionsHost)
Set the runtime options host pointer to pass to layers during building.
Definition: XmlNetworkBuilder.cpp:754
NeuralResult buildNetworkFromFile(INetwork *pNetwork, const char *pFilename) const
Builds a network from a file on disk.
Definition: XmlNetworkBuilder.cpp:175
NeuralResult setInliningEnabled(bool enableInlining)
Enable or disable network inlining during preprocessing.
Definition: XmlNetworkBuilder.cpp:748
NeuralResult setScopeCallbackFunc(LayerScopeCallbackFunc scopeCallback)
Sets a function that will be called around layer-specific operations during buildNetwork calls.
Definition: XmlNetworkBuilder.cpp:93
virtual NeuralResult setLayerActivation(ILayer *pLayer, ActivationFunctionId activationId, const std::string &activationName) const
Sets an activation function on the indicated layer.
Definition: XmlNetworkBuilder.cpp:777
virtual RefPtr< IParameterNode > createParameterNode(ILayer *pLayer, pugi::xml_node node) const
Creates an appropriate parameter node for a given <Parameters> block.
Definition: XmlNetworkBuilder.cpp:772
NeuralResult clearLayerOverrides()
Removes all layer overrides.
Definition: XmlNetworkBuilder.cpp:51
NeuralResult setLoadDefaultFusingRules(bool enableDefaultFusing)
Turns ClassRegistry-based fusing rules on or off.
Definition: XmlNetworkBuilder.cpp:225
virtual NeuralResult setLayerInput(ILayer *pLayer, size_t inputIndex, ILayer *pInput) const
Sets an input on the indicated layer.
Definition: XmlNetworkBuilder.cpp:787
virtual RefPtr< ILayer > createLayerObject(const std::string &name, const std::string &objectClass) const
Creates a layer object for a given object class.
Definition: XmlNetworkBuilder.cpp:760
NeuralResult setLayerOverride(const char *pLayerName, const char *pObjectClass)
Overrides construction of a layer.
Definition: XmlNetworkBuilder.cpp:57
bool loadDefaultFusingRules() const
Returns true if ClassRegistry-based fusing rules are enabled.
Definition: XmlNetworkBuilder.cpp:231
NeuralResult buildNetworkFromXmlLiteral(INetwork *pNetwork, const char *pXmlLiteral) const
Builds a network from a provided XML literal.
Definition: XmlNetworkBuilder.cpp:200
NeuralResult setFallbackLayerType(const char *pObjectClass)
Set an emergency fallback object class when no candidates match the layer type.
Definition: XmlNetworkBuilder.cpp:80
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:782
NeuralResult addFusingRule(const char *pFusingRule)
Adds a fusing rule to network construction.
Definition: XmlNetworkBuilder.cpp:236
This structure represent a prototype layer version (backend, tensor format and kernel type),...
Definition: CoreTypes.h:1646
TensorFormat describes a specific tensor shape (element type and layout).
Definition: CoreTypes.h:87