NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
NetworkLayer.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_NETWORKLAYER_H
27 #define NVNEURAL_NETWORKLAYER_H
28 
29 #include <nvneural/CoreTypes.h>
30 #include <nvneural/LayerTypes.h>
31 #include <BaseLayer.h>
32 #include <nvneural/NetworkTypes.h>
33 #include <map>
34 
35 namespace nvneural {
36 
41 class INetworkLayer : public IRefObject
42 {
43 public:
45  static const IRefObject::TypeId typeID = 0x90a0da9f3d52f363ul;
46 
54  virtual NeuralResult getFlattenSubNetworkLayerList(ILayerList** ppLayerList, size_t* pDepth) const noexcept = 0;
55 
61  virtual NeuralResult addFusingRuleToSubNetwork(const char* pFusingRule) noexcept = 0;
62 
67  virtual NeuralResult getOutputs(ILayerList** ppLayerList) const noexcept = 0;
68 };
69 
70 namespace detail {
71 
73 class NetworkLayer
74  : public refobj::RefObjectBase<
75  refobj::Implements<BaseLayer>
76  , refobj::IndirectlyImplements<ILayer, BaseLayer>
77  , refobj::Implements<INetworkLayer>
78  , refobj::Implements<IRuntimeOptionsLayer>>
79 {
80 public:
81  static const char* const ObjectClass;
82 
83  ~NetworkLayer();
84 
85  const char* serializedType() const noexcept override;
86 
87  NetworkBackendId backendId() const noexcept override;
88  TensorFormat tensorFormat() const noexcept override;
89  NeuralResult setNetworkRuntime(INetworkRuntime* pNetworkRuntime) noexcept override;
90 
91  NeuralResult loadFromParameters(const IParameterNode* pParameters) noexcept override;
92 
93  NeuralResult reshape() noexcept override;
94  NeuralResult evaluateForward() noexcept override;
95 
96  NeuralResult setPermanent(bool permanent) noexcept override;
97 
98  NeuralResult setAffected(bool affected) noexcept override;
99 
100  TensorDimension dimensions() const noexcept override;
101 
102  NeuralResult setInputLayer(size_t index, ILayer* pLayer) noexcept override;
103 
104  NeuralResult getConstData(const void** ppOut, TensorFormat format, const ILayer* pRequestingLayer) const noexcept override;
105  NeuralResult getCpuConstData(void* pOutBuffer, size_t bufferByteCount, size_t* pBytesCopied, TensorFormat format) const noexcept override;
106 
107  // INetworkLayer virtuals
108  NeuralResult getFlattenSubNetworkLayerList(ILayerList** ppLayerList, size_t* pDepth) const noexcept override;
109  NeuralResult addFusingRuleToSubNetwork(const char* pFusingRule) noexcept override;
110  NeuralResult getOutputs(ILayerList** ppLayerList) const noexcept override;
111 
112  // IRuntimeOptionsLayer virtuals
113  std::size_t runtimeOptionCount() const noexcept override;
114  const char* runtimeOptionByIndex(size_t index) const noexcept override;
115  NeuralResult setRuntimeOptionsHost(IRuntimeOptionsHost* pRuntimeOptionsHost) noexcept override;
116  NeuralResult setRuntimeOptionValue(const char* pName, const char* pValue) noexcept override;
117  const char* getRuntimeOptionValue(const char* pName) const noexcept override;
118 
119 private:
120  struct InputConnection
121  {
122  InputConnection(int index, ILayer* pLayer)
123  : inputIndex(index)
124  , pToLayer(pLayer)
125  {
126  }
127 
128  int inputIndex;
129  ILayer* pToLayer;
130  };
131 
132  NeuralResult setSubNetworkInputLayer(size_t index, ILayer* pLayer) noexcept;
133 
134  std::string m_weightPath;
135  RefPtr<INetwork> m_pSubNetwork;
136  std::map<ILayer*, std::vector<InputConnection>> m_inputConnectionMap; //< Mapping between sub network inputs and sub networks layers with their input index
137  INetworkRuntime* m_pNetworkRuntime = nullptr;
138  std::vector<ILayer*> m_inputs; //< Vector of inputs of the NetworkLayer
139  std::vector<ILayer*> m_outputs; //< Vector of outputs from the sub network (also represents the ouput of the NetworkLayer)
140  std::vector<ILayer*> m_reshapeLayer; //< Vector of layer from the sub network that needs to be reshaped when calling NetworkLayer::reshape. They corresponds to all the accpetor layer of the sub network inputs.
141  IRuntimeOptionsHost* m_pRuntimeOptionsHost = nullptr;
142  std::vector<std::string> m_fusingRules;
143 };
144 
145 } // namespace detail
146 
147 } // namespace nvneural
148 
149 #endif // !NVNEURAL_NETWORKLAYER_H
Generic helper class to aid implementation of new ILayer objects.
Fundamental NvNeural data types are declared here.
NetworkBackendId
Enumeration describing common network backends.
Definition: CoreTypes.h:239
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
Interface types needed by layer objects.
Host application types for network construction and manipulation.
ILayer is the base class for neural network layers.
Definition: LayerTypes.h:59
ILayerList represents an immutable collection of ILayer pointers.
Definition: CoreTypes.h:1060
INetworkLayer objects are ILayers that encapsulate an externally defined network graph.
Definition: NetworkLayer.h:42
virtual NeuralResult getFlattenSubNetworkLayerList(ILayerList **ppLayerList, size_t *pDepth) const noexcept=0
Returns a recursively flattened list of layers contained in the subgraph.
virtual NeuralResult addFusingRuleToSubNetwork(const char *pFusingRule) noexcept=0
Adds a v2 fusing rule to the subgraph's network builder object.
static const IRefObject::TypeId typeID
Interface TypeId for InterfaceOf purposes.
Definition: NetworkLayer.h:45
virtual NeuralResult getOutputs(ILayerList **ppLayerList) const noexcept=0
Returns a list of output layers defined in the subgraph.
INetworkRuntime is a subset of the basic network interface that is accessible from layer classes duri...
Definition: CoreTypes.h:1129
Represents a serialized parameter block in a model definition.
Definition: CoreTypes.h:1889
Base class for all objects, similar to COM's IUnknown.
Definition: CoreTypes.h:343
std::uint64_t TypeId
Every interface must define a unique TypeId. This should be randomized.
Definition: CoreTypes.h:349
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
TensorDimension describes the dimensions of a four-dimensional image tensor.
Definition: CoreTypes.h:136
TensorFormat describes a specific tensor shape (element type and layout).
Definition: CoreTypes.h:88
Parameterized base class implementing common IRefObject operations.
Definition: RefObject.h:336