26 #ifndef NVNEURAL_BASELAYER_H
27 #define NVNEURAL_BASELAYER_H
77 const char*
name()
const noexcept
override;
184 const void*
weightsData(
const char* pWeightsName)
const;
198 template<
typename... Indices>
201 const std::size_t arraySize =
sizeof...(Indices);
202 const std::array<size_t, arraySize> checklist = { (size_t)indices... };
204 size_t checklistIndex;
206 for (checklistIndex = 0; checklistIndex < arraySize; ++checklistIndex)
217 std::stringstream errorStream;
218 errorStream <<
name() <<
": These mandatory inputs are not connected: " << checklist[checklistIndex];
219 for (checklistIndex += 1; checklistIndex < arraySize; ++checklistIndex)
221 const std::size_t inputIndex = checklist[checklistIndex];
224 errorStream <<
", " << inputIndex;
258 static LoadResult load2ElementParameter(
const std::vector<std::string>& inArray,
size_t& outElementX,
size_t& outElementY,
size_t inFill,
bool inFillFirstElement =
false)
268 size_t calcFill = inFill;
270 size_t firstElement = inFill;
271 if (isdigit(inArray[0][0]))
275 firstElement =
static_cast<size_t>(std::stoul(inArray[0]));
277 catch (
const std::exception&)
287 if (inFillFirstElement)
293 calcFill = firstElement;
297 size_t elementsOut[2] = { calcFill, calcFill };
302 elementsOut[0] = firstElement;
306 size_t secondElement = calcFill;
307 if (inArray.size() >= 2)
309 if (isdigit(inArray[1].
data()[0]))
313 secondElement =
static_cast<size_t>(std::stoul(inArray[1]));
315 catch (
const std::exception&)
326 elementsOut[1] = secondElement;
329 outElementX = elementsOut[0];
330 outElementY = elementsOut[1];
339 std::vector<float> m_activationCoefficients;
343 std::string m_weightsName;
347 std::vector<ILayer*> m_inputLayers;
349 struct InternalImplementation
355 std::vector<InternalImplementation> m_implementations;
356 mutable const InternalImplementation* m_pCurrentImplementation;
359 void updateImplementation()
const;
Common helper classes and template function implementations.
ILogger * DefaultLogger()
Returns a pointer to the default logger for this module.
Definition: Logging.cpp:38
Fundamental NvNeural data types are declared here.
TensorDataType
Enumeration describing common tensor element types.
Definition: CoreTypes.h:59
TensorDataLayout
Enumeration describing common tensor data layouts.
Definition: CoreTypes.h:75
NetworkBackendId
Enumeration describing common network backends.
Definition: CoreTypes.h:237
ActivationFunctionId
Enumeration describing common activation functions.
Definition: CoreTypes.h:257
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:273
Interface types needed by layer objects.
WeightsQuery
WeightsQuery describes the different types of queries for weights data.
Definition: LayerTypes.h:39
BaseLayer provides common implementations for most of ILayer.
Definition: BaseLayer.h:74
void * data(TensorFormat format)
Returns a pointer to the output tensor memory in the provided format.
Definition: BaseLayer.cpp:287
NeuralResult setWeightsName(const char *pWeightsName) noexcept
Sets the name used to identify this layer's weights.
Definition: BaseLayer.cpp:317
NeuralResult loadFromParameters(const IParameterNode *pParameters) noexcept
Loads layer parameters from a serialized key-value representation.
Definition: BaseLayer.cpp:126
const char * weightsName() const noexcept
Retrieves the name used to identify this layer's weights.
Definition: BaseLayer.cpp:312
NeuralResult setActivationCoefficient(std::size_t coefficientIndex, float value) noexcept
Sets an activation coefficient.
Definition: BaseLayer.cpp:142
INetworkRuntime * networkRuntime() const
Returns the INetworkRuntime object most recently set with setNetworkRuntime.
Definition: BaseLayer.cpp:88
NeuralResult setName(const char *pName) noexcept
Sets the layer name.
Definition: BaseLayer.cpp:46
TensorDimension stepping() const noexcept
Returns the internal storage stride consumed by this layer implementation.
Definition: BaseLayer.cpp:98
NeuralResult setActivationFunction(ActivationFunctionId activationFunction) noexcept
Sets the activation function attached to the layer.
Definition: BaseLayer.cpp:131
TensorDimension internalDimensions() const noexcept
Retrieves the dimensions of the layer's output tensor as allocated internally.
Definition: BaseLayer.cpp:103
size_t tensorInternalBufferSize() const noexcept
Retrieves the dimensions of the layer's output tensor as allocated internally.
Definition: BaseLayer.cpp:118
bool isPermanent() const noexcept
Returns the current status of the "permanent" flag.
Definition: BaseLayer.cpp:181
NetworkBackendId backendId() const noexcept
Returns the backend ID associated with this layer implementation.
Definition: BaseLayer.cpp:435
virtual void onImplementationChanged() const
Callback for derived classes to know that the "preferred implementation" has changed.
Definition: BaseLayer.cpp:364
const void * weightsData(const char *pWeightsName) const
Returns a pointer to device-side memory containing the indicated weights data.
Definition: BaseLayer.cpp:332
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: BaseLayer.cpp:247
ActivationFunctionId activationFunction() const noexcept
Retrieves the activation function attached to this layer.
Definition: BaseLayer.cpp:137
NeuralResult setNetworkRuntime(INetworkRuntime *pNetworkRuntime) noexcept
Informs the layer it has been attached to a new network.
Definition: BaseLayer.cpp:67
size_t tensorBufferSize() const noexcept
Retrieve the size of the layer's output tensor buffer in bytes.
Definition: BaseLayer.cpp:110
const char * name() const noexcept
Retrieves the layer name.
Definition: BaseLayer.cpp:62
NeuralResult getConstData(const void **ppOut, TensorFormat format, const ILayer *pRequestingLayer) const noexcept
Retrieves read-only device-side memory for the layer's output.
Definition: BaseLayer.cpp:222
static LoadResult load2ElementParameter(const std::vector< std::string > &inArray, size_t &outElementX, size_t &outElementY, size_t inFill, bool inFillFirstElement=false)
Helper class that takes a vector of strings and loads 2 size_t elements.
Definition: BaseLayer.h:258
float activationCoefficient(std::size_t coefficientIndex) const noexcept
Retrieves the activation coefficient for the specified index.
Definition: BaseLayer.cpp:161
TensorFormat tensorFormat() const noexcept
Returns the tensor format consumed by this layer implementation.
Definition: BaseLayer.cpp:448
NeuralResult getInputLayers(ILayerList **ppInputLayers) const noexcept
Retrieves the inputs for this layer.
Definition: BaseLayer.cpp:197
LoadResult
This enum describes a result from the load2ElementParameter function.
Definition: BaseLayer.h:242
@ NonNumericInput
An input component contained a nondigit.
@ Success
Parsing succeeded.
@ CouldNotTranslate
An input component could not be parsed as a number.
NeuralResult setAffected(bool affected) noexcept
Sets or clears the "affected" flag on a layer's output tensor.
Definition: BaseLayer.cpp:186
NeuralResult setInputLayer(std::size_t index, ILayer *pLayer) noexcept
Sets an input layer by index.
Definition: BaseLayer.cpp:207
const ILayer * inputLayer(size_t index) const
Returns the Nth input layer attached to this layer.
Definition: BaseLayer.cpp:292
bool verifyInputConnected(Indices... indices) const noexcept
Test if inputs represented by indices are connected, and log errors if not.
Definition: BaseLayer.h:199
NeuralResult setPermanent(bool permanent) noexcept
Sets or clears the "permanent" flag on a layer's output tensor.
Definition: BaseLayer.cpp:175
TensorDimension loadedWeightsSize(const ILayer *pWeightsLayer, const char *pWeightsName) const
Returns the size of already-loaded weights data, whether using a layer or IWeightsLoader.
Definition: BaseLayer.cpp:341
size_t inputLayerCount() const
Returns the number of input layers assigned to this object.
Definition: BaseLayer.cpp:307
void registerImplementation(NetworkBackendId backendId, TensorDataType elementType, TensorDataLayout layout, int penalty)
Registers an implementation for multi-format layers.
Definition: BaseLayer.cpp:357
RefPtr< INetworkBackend > networkBackend() const
Returns the INetworkBackend object associated with the implementation that most closely matches the c...
Definition: BaseLayer.cpp:93
NeuralResult getData(void **ppOut, TensorFormat format, const ILayer *pRequestingLayer) noexcept
Retrieves device-side memory for the layer's output.
Definition: BaseLayer.cpp:238
TensorDimension weightsDimensions(const char *pWeightsName, WeightsQuery queryType) const noexcept
Retrieves the tensor dimension of a layer's named weight input.
Definition: BaseLayer.cpp:327
bool isAffected() const noexcept
Returns the current status of the "affected" flag.
Definition: BaseLayer.cpp:192
ILayer is the base class for neural network layers.
Definition: LayerTypes.h:59
ILayerList represents an immutable collection of ILayer pointers.
Definition: CoreTypes.h:927
virtual NeuralResult logError(VerbosityLevel verbosity, const char *format,...) noexcept=0
Logs an error message.
INetworkBackend is a runtime-specific interface for CUDA, DirectX, or other system- specific operatio...
Definition: CoreTypes.h:641
INetworkRuntime is a subset of the basic network interface that is accessible from layer classes duri...
Definition: CoreTypes.h:996
Represents a serialized parameter block in a model definition.
Definition: CoreTypes.h:1697
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:134