26 #ifndef NVNEURAL_BASELAYER_H
27 #define NVNEURAL_BASELAYER_H
77 const char*
name()
const noexcept
override;
199 const void*
weightsData(
const char* pWeightsName)
const;
213 template<
typename... Indices>
216 const std::size_t arraySize =
sizeof...(Indices);
217 const std::array<size_t, arraySize> checklist = { (size_t)indices... };
219 size_t checklistIndex;
221 for (checklistIndex = 0; checklistIndex < arraySize; ++checklistIndex)
232 std::stringstream errorStream;
233 errorStream <<
name() <<
": These mandatory inputs are not connected: " << checklist[checklistIndex];
234 for (checklistIndex += 1; checklistIndex < arraySize; ++checklistIndex)
236 const std::size_t inputIndex = checklist[checklistIndex];
239 errorStream <<
", " << inputIndex;
266 CouldNotTranslate = 3,
278 static LoadResult load2ElementParameter(
const std::vector<std::string>& inArray,
size_t& outElementX,
size_t& outElementY,
size_t inFill,
bool inFillFirstElement =
false)
288 size_t calcFill = inFill;
290 size_t firstElement = inFill;
291 if (isdigit(inArray[0][0]))
295 firstElement =
static_cast<size_t>(std::stoul(inArray[0]));
297 catch (
const std::exception&)
307 if (inFillFirstElement)
313 calcFill = firstElement;
317 size_t elementsOut[2] = { calcFill, calcFill };
322 elementsOut[0] = firstElement;
326 size_t secondElement = calcFill;
327 if (inArray.size() >= 2)
329 if (isdigit(inArray[1].
data()[0]))
333 secondElement =
static_cast<size_t>(std::stoul(inArray[1]));
335 catch (
const std::exception&)
346 elementsOut[1] = secondElement;
349 outElementX = elementsOut[0];
350 outElementY = elementsOut[1];
359 std::vector<float> m_activationCoefficients;
363 std::string m_weightsName;
367 std::vector<ILayer*> m_inputLayers;
369 struct InternalImplementation
375 std::vector<InternalImplementation> m_implementations;
376 mutable const InternalImplementation* m_pCurrentImplementation;
379 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:60
TensorDataLayout
Enumeration describing common tensor data layouts.
Definition: CoreTypes.h:77
MemoryHandle__type * MemoryHandle
Opaque typedef used to represent INetworkBackend memory handles.
Definition: CoreTypes.h:626
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
@ Success
Operation succeeded. Generic result.
@ Failure
Operation failed. Generic result.
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:310
NeuralResult setWeightsName(const char *pWeightsName) noexcept
Sets the name used to identify this layer's weights.
Definition: BaseLayer.cpp:340
NeuralResult loadFromParameters(const IParameterNode *pParameters) noexcept
Loads layer parameters from a serialized key-value representation.
Definition: BaseLayer.cpp:147
const char * weightsName() const noexcept
Retrieves the name used to identify this layer's weights.
Definition: BaseLayer.cpp:335
NeuralResult setActivationCoefficient(std::size_t coefficientIndex, float value) noexcept
Sets an activation coefficient.
Definition: BaseLayer.cpp:163
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:119
NeuralResult setActivationFunction(ActivationFunctionId activationFunction) noexcept
Sets the activation function attached to the layer.
Definition: BaseLayer.cpp:152
TensorDimension internalDimensions() const noexcept
Retrieves the dimensions of the layer's output tensor as allocated internally.
Definition: BaseLayer.cpp:124
size_t tensorInternalBufferSize() const noexcept
Retrieves the dimensions of the layer's output tensor as allocated internally.
Definition: BaseLayer.cpp:139
NeuralResult allocateMemoryBlock(MemoryHandle *pHandle, size_t byteCount) noexcept
Allocates a memory block of the requested size.
Definition: BaseLayer.cpp:98
bool isPermanent() const noexcept
Returns the current status of the "permanent" flag.
Definition: BaseLayer.cpp:204
NetworkBackendId backendId() const noexcept
Returns the backend ID associated with this layer implementation.
Definition: BaseLayer.cpp:458
virtual void onImplementationChanged() const
Callback for derived classes to know that the "preferred implementation" has changed.
Definition: BaseLayer.cpp:387
const void * weightsData(const char *pWeightsName) const
Returns a pointer to device-side memory containing the indicated weights data.
Definition: BaseLayer.cpp:355
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:270
ActivationFunctionId activationFunction() const noexcept
Retrieves the activation function attached to this layer.
Definition: BaseLayer.cpp:158
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:131
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:245
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:278
float activationCoefficient(std::size_t coefficientIndex) const noexcept
Retrieves the activation coefficient for the specified index.
Definition: BaseLayer.cpp:183
TensorFormat tensorFormat() const noexcept
Returns the tensor format consumed by this layer implementation.
Definition: BaseLayer.cpp:471
NeuralResult getInputLayers(ILayerList **ppInputLayers) const noexcept
Retrieves the inputs for this layer.
Definition: BaseLayer.cpp:220
LoadResult
This enum describes a result from the load2ElementParameter function.
Definition: BaseLayer.h:262
@ 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:209
NeuralResult setInputLayer(std::size_t index, ILayer *pLayer) noexcept
Sets an input layer by index.
Definition: BaseLayer.cpp:230
const ILayer * inputLayer(size_t index) const
Returns the Nth input layer attached to this layer.
Definition: BaseLayer.cpp:315
bool verifyInputConnected(Indices... indices) const noexcept
Test if inputs represented by indices are connected, and log errors if not.
Definition: BaseLayer.h:214
NeuralResult setPermanent(bool permanent) noexcept
Sets or clears the "permanent" flag on a layer's output tensor.
Definition: BaseLayer.cpp:198
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:364
size_t inputLayerCount() const
Returns the number of input layers assigned to this object.
Definition: BaseLayer.cpp:330
void registerImplementation(NetworkBackendId backendId, TensorDataType elementType, TensorDataLayout layout, int penalty)
Registers an implementation for multi-format layers.
Definition: BaseLayer.cpp:380
RefPtr< INetworkBackend > networkBackend() const
Returns the INetworkBackend object associated with the implementation that most closely matches the c...
Definition: BaseLayer.cpp:93
void requestReshape() noexcept
Marks this layer as in need of reshape.
Definition: BaseLayer.cpp:484
NeuralResult getData(void **ppOut, TensorFormat format, const ILayer *pRequestingLayer) noexcept
Retrieves device-side memory for the layer's output.
Definition: BaseLayer.cpp:261
TensorDimension weightsDimensions(const char *pWeightsName, WeightsQuery queryType) const noexcept
Retrieves the tensor dimension of a layer's named weight input.
Definition: BaseLayer.cpp:350
bool isAffected() const noexcept
Returns the current status of the "affected" flag.
Definition: BaseLayer.cpp:215
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
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: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
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