NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
BasePrototypeLayer.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 #ifndef NVNEURAL_BASEPROTOTYPELAYER_H
26 #define NVNEURAL_BASEPROTOTYPELAYER_H
27 
28 #include <nvneural/CodeGenTypes.h>
29 #include <nvneural/CoreTypes.h>
30 #include <nvneural/LayerTypes.h>
31 #include <nvneural/RefObject.h>
33 #include "ScriptEngine.h"
34 #include <Pugixml/pugixml.hpp>
35 
36 #include <map>
37 #include <string>
38 #include <vector>
39 
40 namespace nvneural {
41 
44  : public refobj::RefObjectBase<
45  refobj::Implements<ILayer>
46  , refobj::Implements<IPrototypeLayer>
47  , refobj::Implements<ICppCodeGenerationLayer>>
48  , detail::ScriptInterpretInterface<detail::AnyOperand>
49 {
50 public:
51  virtual ~BasePrototypeLayer();
52 
55 
57  const char* serializedType() const noexcept override;
58 
60  NeuralResult setName(const char* pName) noexcept override;
62  const char* name() const noexcept override;
63 
65  NeuralResult setNetworkRuntime(INetworkRuntime* pNetworkRuntime) noexcept override;
66 
68  NetworkBackendId backendId() const noexcept override;
70  TensorFormat tensorFormat() const noexcept override;
72  TensorDimension stepping() const noexcept override;
73 
75  TensorDimension dimensions() const noexcept override;
77  TensorDimension internalDimensions() const noexcept override;
79  size_t tensorBufferSize() const noexcept override;
81  size_t tensorInternalBufferSize() const noexcept override;
82 
84  NeuralResult loadFromParameters(const IParameterNode* pParameters) noexcept override;
85 
87  NeuralResult getInputLayers(ILayerList** ppInputLayers) const noexcept override;
89  NeuralResult setInputLayer(size_t index, ILayer* pLayer) noexcept override;
90 
92  NeuralResult setPermanent(bool permanent) noexcept override;
94  bool isPermanent() const noexcept override;
95 
97  NeuralResult setAffected(bool affected) noexcept override;
99  bool isAffected() const noexcept override;
100 
104  ActivationFunctionId activationFunction() const noexcept override;
105 
107  NeuralResult setActivationCoefficient(std::size_t coefficientIndex, float value) noexcept override;
109  float activationCoefficient(std::size_t coefficientIndex) const noexcept override;
110 
112  const char* weightsName() const noexcept override;
114  NeuralResult setWeightsName(const char* pWeightsName) noexcept override;
115 
117  TensorDimension weightsDimensions(const char* pWeightsName, WeightsQuery queryType) const noexcept override;
118 
120  NeuralResult reshape() noexcept override;
122  NeuralResult evaluateForward() noexcept override;
123 
125  NeuralResult getData(void** ppOut, TensorFormat format, const ILayer* pRequestingLayer) noexcept override;
127  NeuralResult getConstData(const void** ppOut, TensorFormat format, const ILayer* pRequestingLayer) const noexcept override;
129  NeuralResult getCpuConstData(void* pOutBuffer, size_t bufferByteCount, size_t* pBytesCopied, TensorFormat format) const noexcept override;
130 
132 
135 
137  NeuralResult addPrototypeCode(const char* pName, const char* pCode) noexcept override final;
139  NeuralResult addForwardEvalCall(const char* pName, const char* pCall) noexcept override final;
141  NeuralResult addParameterKey(const char* pKey) noexcept override final;
142 
144 
147 
149  NeuralResult generateLayerCpp(ICppCodeGenerationLayerHost* pHost) noexcept override final;
151  NeuralResult networkGenerationComplete() noexcept override final;
152 
154 
155 protected:
158 
161 
163  virtual NeuralResult implementationReshape() noexcept = 0;
164 
166  virtual NeuralResult implementationForward() noexcept = 0;
167 
169 
189  const TensorFormat& format,
190  CompilationLevel compilationLevel,
191  const std::string& currentImpl,
192  const IParameterNode* pParameters);
193 
202  NeuralResult setBasePrototypeInfo(std::size_t inputsCount,
203  const std::string& type,
204  NetworkBackendId backend,
205  const TensorFormat& format,
206  const std::string& dim,
207  const std::string& impl) noexcept;
208 
210  detail::SizeValue m_block;
211  detail::SizeValue m_grid;
212 
213  std::vector<detail::AnyOperand> m_ops;
214  std::string m_selected_code;
215  std::string m_entry;
216 
217 private:
218  struct BasePrototypeLayerParameters
219  {
220  std::string type; // layer type for after prototyping
223  std::map<std::string, std::string> code; // tensor type as a key
224  // dim example: 1,in[0].c+in[1].c,in.h,in.w;... (without index mean in[0].h == in[1].h == in[2].h ...)
225  std::string dimNCHWCalculation;
226  // fwd example: calling_kernel_name(in[0],in[1],out.w,out.h,in[0].c,in[1].c,out,1+5*4,act,act.alpha,@option,parameter)
227  std::map<std::string, std::string> fwdEvalCall;
228  std::vector<std::string> parametersKey;
229  std::string currentImpl;
230  };
231 
233  void setReal(detail::AnyOperand& v, const std::string& str) override;
234  void setInteger(detail::AnyOperand& v, const std::string& str) override;
235  void setBufferPointer(detail::AnyOperand& v, const std::string& str, int arg_index) override;
236  void neg(detail::AnyOperand& v) override;
237  void add(detail::AnyOperand& v, detail::AnyOperand& op) override;
238  void sub(detail::AnyOperand& v, detail::AnyOperand& op) override;
239  void mul(detail::AnyOperand& v, detail::AnyOperand& op) override;
240  void div(detail::AnyOperand& v, detail::AnyOperand& op) override;
241  void setRealOption(detail::AnyOperand& v, const std::string& str) override;
242  void setIntegerOption(detail::AnyOperand& v, const std::string& str) override;
243  void transform(detail::AnyOperand& v, const std::string& str) override;
244  void setActType(detail::AnyOperand& v) override;
245  void setActAlhpaLeft(detail::AnyOperand& v) override;
246  void setActAlhpaRight(detail::AnyOperand& v) override;
247  void setWeightsPointer(detail::AnyOperand& v, const std::string& str) override;
248  void setInputPointer(detail::AnyOperand& v, int index) override;
249  void setInputDimValue(detail::AnyOperand& v, int index, const std::string& str) override;
250  void setOutputPointer(detail::AnyOperand& v, const std::string& name) override;
251  void setOutputDimValue(detail::AnyOperand& v, const std::string& name, const std::string& str) override;
252 
253  void createScriptEngine();
254  std::vector<detail::AnyOperand> interpretFinalize(const detail::SynParser::Node& node, bool onReshape = false);
255 
256  BasePrototypeLayerParameters m_basePrototypeParameters;
257  std::string m_name;
258  INetworkBackend* m_pBackend;
259  TensorDimension m_dim;
260  std::map<std::string, std::string> m_parameters;
261  std::vector<ILayer*> m_inputLayers;
262  bool m_permanent;
263  bool m_affected;
264  ActivationFunctionId m_activationFunction;
265  std::vector<float> m_activationCoefficients;
266 
267  detail::ScriptEngine* m_pEngine = nullptr;
268 
269  std::vector<ILayer*> m_skip;
270 
271  detail::SynParser::Node m_fwdSyntree;
272  size_t m_smemSize = 0;
273  std::string m_type;
274 
275  TensorDimension m_steppingValue = TensorDimension(1, 1, 1, 1);
276  std::size_t m_expectedInputCount = 0;
277 
278  bool m_reshapeFailed = true;
279 };
280 
281 } // namespace nvneural
282 
283 #endif // !NVNEURAL_BASEPROTOTYPELAYER_H
Definitions of C++ code generation interfaces exported by tools and plugins.
Fundamental NvNeural data types are declared here.
NetworkBackendId
Enumeration describing common network backends.
Definition: CoreTypes.h:239
CompilationLevel
This enum represent kernel type contained by a prototype layer.
Definition: CoreTypes.h:1688
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
Common interfaces for prototype layers.
Interface types needed by layer objects.
WeightsQuery
WeightsQuery describes the different types of queries for weights data.
Definition: LayerTypes.h:39
Standard implementation for IRefObject-derived objects.
Internal helper classes for working with prototype layer scripts.
Base class for the CUDA prototype layers shipped with NvNeural.
Definition: BasePrototypeLayer.h:49
NeuralResult getInputLayers(ILayerList **ppInputLayers) const noexcept
Retrieves the inputs for this layer.
Definition: BasePrototypeLayer.cpp:467
detail::SizeValue m_grid
Preferred grid size in blocks.
Definition: BasePrototypeLayer.h:211
NeuralResult setWeightsName(const char *pWeightsName) noexcept
Sets the name used to identify this layer's weights.
Definition: BasePrototypeLayer.cpp:576
INetworkRuntime * m_pNetwork
Pointer to owning network.
Definition: BasePrototypeLayer.h:209
float activationCoefficient(std::size_t coefficientIndex) const noexcept
Retrieves the activation coefficient for the specified index.
Definition: BasePrototypeLayer.cpp:555
NeuralResult setNetworkRuntime(INetworkRuntime *pNetworkRuntime) noexcept
Informs the layer it has been attached to a new network.
Definition: BasePrototypeLayer.cpp:385
NeuralResult setBasePrototypeInfo(std::size_t inputsCount, const std::string &type, NetworkBackendId backend, const TensorFormat &format, const std::string &dim, const std::string &impl) noexcept
Stores implementation-agnostic details of the prototype layer.
Definition: BasePrototypeLayer.cpp:188
NeuralResult setActivationFunction(ActivationFunctionId activationFunction) noexcept
Sets the activation function attached to the layer.
Definition: BasePrototypeLayer.cpp:525
NetworkBackendId backendId() const noexcept
Returns the backend ID associated with this layer implementation.
Definition: BasePrototypeLayer.cpp:410
NeuralResult addPrototypeCode(const char *pName, const char *pCode) noexcept final
Adds a kernel definition for a particular tensor format and backend.
Definition: BasePrototypeLayer.cpp:207
TensorDimension stepping() const noexcept
Returns the internal storage stride consumed by this layer implementation.
Definition: BasePrototypeLayer.cpp:405
const char * weightsName() const noexcept
Retrieves the name used to identify this layer's weights.
Definition: BasePrototypeLayer.cpp:569
NeuralResult getCpuConstData(void *pOutBuffer, size_t bufferByteCount, size_t *pBytesCopied, TensorFormat format) const noexcept
Retrieves read-only CPU-side memory for the layer's output.
Definition: BasePrototypeLayer.cpp:615
size_t tensorBufferSize() const noexcept
Retrieve the size of the layer's output tensor buffer in bytes.
Definition: BasePrototypeLayer.cpp:432
NeuralResult generateLayerCpp(ICppCodeGenerationLayerHost *pHost) noexcept final
Generates C++ code to configure the layer.
Definition: BasePrototypeLayer.cpp:241
NeuralResult loadBasePrototypeInfo(NetworkBackendId backendId, const TensorFormat &format, CompilationLevel compilationLevel, const std::string &currentImpl, const IParameterNode *pParameters)
Loads prototype information from a parameter node.
Definition: BasePrototypeLayer.cpp:68
TensorDimension weightsDimensions(const char *pWeightsName, WeightsQuery queryType) const noexcept
Retrieves the tensor dimension of a layer's named weight input.
Definition: BasePrototypeLayer.cpp:583
virtual NeuralResult implementationForward() noexcept=0
Implementation-specific logic for ILayer::evaluateForward.
detail::SizeValue m_block
Preferred block size in threads.
Definition: BasePrototypeLayer.h:210
size_t tensorInternalBufferSize() const noexcept
Retrieves the dimensions of the layer's output tensor as allocated internally.
Definition: BasePrototypeLayer.cpp:440
NeuralResult setAffected(bool affected) noexcept
Sets or clears the "affected" flag on a layer's output tensor.
Definition: BasePrototypeLayer.cpp:514
NeuralResult addForwardEvalCall(const char *pName, const char *pCall) noexcept final
Adds a call script for a particular tensor format and backend.
Definition: BasePrototypeLayer.cpp:218
NeuralResult setPermanent(bool permanent) noexcept
Sets or clears the "permanent" flag on a layer's output tensor.
Definition: BasePrototypeLayer.cpp:503
NeuralResult getData(void **ppOut, TensorFormat format, const ILayer *pRequestingLayer) noexcept
Retrieves device-side memory for the layer's output.
Definition: BasePrototypeLayer.cpp:590
const char * serializedType() const noexcept
Retrieves the layer type.
Definition: BasePrototypeLayer.cpp:337
NeuralResult setInputLayer(size_t index, ILayer *pLayer) noexcept
Sets an input layer by index.
Definition: BasePrototypeLayer.cpp:477
TensorFormat tensorFormat() const noexcept
Returns the tensor format consumed by this layer implementation.
Definition: BasePrototypeLayer.cpp:415
TensorDimension internalDimensions() const noexcept
Retrieves the dimensions of the layer's output tensor as allocated internally.
Definition: BasePrototypeLayer.cpp:425
std::vector< detail::AnyOperand > m_ops
Arguments to pass to the compiled entry point.
Definition: BasePrototypeLayer.h:213
bool isAffected() const noexcept
Returns the current status of the "affected" flag.
Definition: BasePrototypeLayer.cpp:520
NeuralResult reshape() noexcept
Initializes (or reinitializes) the layer implementation with the current set of parameters.
Definition: BasePrototypeLayer.cpp:655
bool isPermanent() const noexcept
Returns the current status of the "permanent" flag.
Definition: BasePrototypeLayer.cpp:509
ActivationFunctionId activationFunction() const noexcept
Retrieves the activation function attached to this layer.
Definition: BasePrototypeLayer.cpp:531
NeuralResult loadFromParameters(const IParameterNode *pParameters) noexcept
Loads layer parameters from a serialized key-value representation.
Definition: BasePrototypeLayer.cpp:448
NeuralResult evaluateForward() noexcept
Performs forward evaluation for this layer.
Definition: BasePrototypeLayer.cpp:758
NeuralResult addParameterKey(const char *pKey) noexcept final
Adds a named parameter to the layer.
Definition: BasePrototypeLayer.cpp:229
std::string m_selected_code
Source code for the currently selected implementation.
Definition: BasePrototypeLayer.h:214
NeuralResult setName(const char *pName) noexcept
Sets the layer name.
Definition: BasePrototypeLayer.cpp:368
virtual NeuralResult implementationReshape() noexcept=0
Implementation-specific logic for ILayer::reshape.
std::string m_entry
Name of compiled entry point (e.g., CUDA function name)
Definition: BasePrototypeLayer.h:215
const char * name() const noexcept
Retrieves the layer name.
Definition: BasePrototypeLayer.cpp:380
NeuralResult getConstData(const void **ppOut, TensorFormat format, const ILayer *pRequestingLayer) const noexcept
Retrieves read-only device-side memory for the layer's output.
Definition: BasePrototypeLayer.cpp:599
NeuralResult networkGenerationComplete() noexcept final
Indicates the entire network has been generated.
Definition: BasePrototypeLayer.cpp:332
TensorDimension dimensions() const noexcept
Retrieves the dimensions of the layer's output tensor.
Definition: BasePrototypeLayer.cpp:420
NeuralResult setActivationCoefficient(std::size_t coefficientIndex, float value) noexcept
Sets an activation coefficient.
Definition: BasePrototypeLayer.cpp:536
BasePrototypeLayer()
Creates a new BasePrototypeLayer object.
Definition: BasePrototypeLayer.cpp:53
Tool-supplied interface for C++ code generation.
Definition: CodeGenTypes.h:130
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
INetworkBackend is a runtime-specific interface for CUDA, DirectX, or other system- specific operatio...
Definition: CoreTypes.h:643
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
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