Beatmup
|
2D convolution operation computed on GPU. More...
#include <conv2d.h>
Public Member Functions | |
Conv2D (const std::string &name, const int kernelSize, const int numInputChannels, const int numOutputChannels, const int stride=1, const Size::Padding padding=Size::Padding::VALID, const bool useBias=true, const int numGroups=1, const ActivationFunction activation=ActivationFunction::DEFAULT) | |
Instantiates a 2D convolution operation. More... | |
bool | isBiasUsed () const |
int | getInputCount () const |
Returns number of operation inputs. More... | |
int | getOutputCount () const |
Returns number of operation outputs. More... | |
bool | acceptsStorageInput (int index=0) const |
Returns true if the operation can take a Storage::View at a specific input. More... | |
bool | acceptsStorageOutput (int index=0) const |
Returns true if the operation can take a Storage::View at a specific output. More... | |
bool | acceptsTextureInput (int index=0) const |
Returns true if the operation can take a GL::TextureHandler at a specific input. More... | |
Size | getOutputSize (int outputIndex=0) const |
Returns full size of a specific operation output. More... | |
Storage::View | getOutput (int index=0) |
Returns a storage view bound to a specific operation output. More... | |
void | setInput (Storage::View &&storage, int inputIndex=0) |
void | setInput (GL::TextureHandler &image, int inputIndex=0) |
void | setOutput (Storage::View &&storage, int outputIndex=0) |
std::map< std::string, std::string > | serialize () const |
Returns a serialized representation of th operation;. More... | |
void | disconnect () |
Assigns empty inputs and outputs. More... | |
void | setResidualInput (Storage::View &&storage) |
Connects a tensor to a residual input. More... | |
unsigned long | countMultiplyAdds () const |
Counts (approximate) number of multiply-adds used by this operation. More... | |
unsigned long | countTexelFetches () const |
Counts (approximate) number of texels fetches. More... | |
Public Member Functions inherited from Beatmup::NNets::AbstractOperation | |
virtual | ~AbstractOperation () |
virtual bool | usesGpu () const |
Returns true if the operation is run on GPU. More... | |
virtual bool | acceptsVectorInput (int index=0) const |
Returns true if the operation can take a GL::Vector at a specific input. More... | |
virtual bool | acceptsVectorOutput (int index=0) const |
Returns true if the operation can take a GL::Vector at a specific output. More... | |
virtual bool | acceptsTextureOutput (int index=0) const |
Returns true if the operation can take a GL::TextureHandler at a specific output. More... | |
virtual void | getOutput (GL::Vector *&vector, int index=0) |
Returns a GL::Vector bound to a specific operation output. More... | |
virtual void | getOutput (GL::TextureHandler *&vector, int index=0) |
Returns a GL::TextureHandler bound to a specific operation output. More... | |
virtual void | setInput (GL::Vector &vector, int index=0) |
virtual void | setOutput (GL::Vector &vector, int index=0) |
virtual void | setOutput (GL::TextureHandler &image, int index=0) |
std::string | getName () const |
Static Public Member Functions | |
static bool | initDeserializer () |
Sets up deserialization of the operation. More... | |
Static Public Attributes | |
static const char * | FILTERS_CHUNK_SUFFIX = "/w" |
suffix added to the op name to get the filters chunk id in the model data More... | |
static const char * | BIAS_CHUNK_SUFFIX = "/b" |
suffix added to the op name to get the bias chunk id in the model data More... | |
Private Member Functions | |
int | getIdx (int output, int input, int x, int y) const |
Maps an (inputChannel, outputChannel, x, y) position to a linear coefficient index in the chunkfile. More... | |
void | prepare (GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank) |
Compiles GLSL shaders. More... | |
void | execute (TaskThread &thread, GraphicPipeline &gpu) |
Executes the operation. More... | |
int | getInputPadding (int index=0) const |
Retrieves minimum required size of zero padding for a given input. More... | |
void | getSampledChannels (int index, int &min, int &max) const |
Retrieves range of input features channels sampled at the same time for a specific input. More... | |
Private Attributes | |
const Size | kernelSize |
const int | numOutputChannels |
number of output feature maps More... | |
const int | numGroups |
number of convolution groups More... | |
const int | stride |
const Size::Padding | padding |
const bool | useInputImage |
if true , input is the texture handler, not the view More... | |
const bool | isDepthwise |
if true , the convolution is depthwise, otherwise regular More... | |
const bool | useBias |
if true , the bias addition is enabled More... | |
bool | ready |
Storage::View | input |
Storage::View | output |
Storage::View | residualInput |
optional tensor to be added to the output before activation More... | |
GL::TextureHandler * | inputImage |
input texture handler to be used instead input view More... | |
std::vector< GL::RenderingProgram * > | programs |
pointers to GLSL program, one per quad of output channels More... | |
std::vector< std::array< float, 4 > > | coeffs |
model data to pass to uniform variables, if used More... | |
std::vector< int > | execOrder |
execution order of GLSL programs More... | |
std::vector< Storage::View > | groupViews |
views per convolution group More... | |
Additional Inherited Members | |
Protected Member Functions inherited from Beatmup::NNets::AbstractOperation | |
AbstractOperation (const std::string &name) | |
virtual void | execute (TaskThread &thread) |
Executes the operation within a specific CPU thread. More... | |
Protected Member Functions inherited from Beatmup::NNets::SpatialFilteringMixin | |
SpatialFilteringMixin (const int nbSizeX, const int nbSizeY) | |
Initializes spatial filtering mixin. More... | |
~SpatialFilteringMixin () | |
void | writeHeader (StringBuilder &code, bool useUniformShift) |
Writes out the very GLSL fragment shader header required for spatial neighborhood sampling. More... | |
void | declare (StringBuilder &code, const char *datatype, bool inlineSampling=false) |
Declares GLSL fragment shader main(..) code part required for spatial neighborhood sampling. More... | |
void | sample (StringBuilder &code, const char *inputName, const int inputIndex, const Point &shift, const bool isFirstSample=true, const char *suffix="") |
Samples a neighborhood of a given texture. More... | |
void | sampleInline (StringBuilder &code, const char *inputName, const int inputIndex, const IntPoint &position, const Point &shift, const char *suffix="") |
void | setup (const int width, const int height) |
Prepares the spatial filtering operation execution. More... | |
void | setUniformShift (GL::Program &program, const IntPoint &shift, const IntPoint &inputSize) |
Applies an offset to the sampling position at runtime. More... | |
void | setupProgram (GL::Program &program) |
Prepares a given program for spatial filtering. More... | |
IntRectangle | getSamplingArea (const IntPoint &size, const IntPoint &stride, const Size::Padding padding) const |
Implements common padding policies by computing a rectangular area of positions the sampling kernel takes in order to get the result with the required padding. More... | |
IntRectangle | getSamplingArea (const Storage::View &storage, const int channel, const IntPoint &stride, const Size::Padding padding) const |
Computes area in pixels to sample a given storage according to specific stride and padding. More... | |
Rectangle | getTextureCoordinates (const Storage::View &storage, const int channel, const IntPoint &stride, const Size::Padding padding, const IntPoint &outputSize) const |
Computes texture coordinates sampling a specific storage channel for given stride, padding and output size. More... | |
std::string | getInputSamplingPos () const |
Retrieves input sampling point position for the current fragment. More... | |
bool | isUniformShiftUsed () const |
Protected Member Functions inherited from Beatmup::NNets::ActivationFunctionMixin | |
ActivationFunctionMixin (const ActivationFunction activationFunc) | |
void | apply (StringBuilder &code, const char *inputVariable) |
Renders a GLSL code applying activation function to a specific variable and writing the result to gl_FragColor shader output variable. More... | |
Protected Attributes inherited from Beatmup::NNets::ActivationFunctionMixin | |
const ActivationFunction | activationFunc |
Static Protected Attributes inherited from Beatmup::NNets::SpatialFilteringMixin | |
static const char * | SAMPLE_ID_PREFIX = "i" |
prefix of variables declaring a neighbor sample More... | |
2D convolution operation computed on GPU.
Has 2 inputs: main and residual (detailed below), and a single output. Constraints:
Raspberry Pi-related constraints:
Features:
Convolution filters and bias are searched in chunks. The chunk names consist of the operation name followed by Conv2D::FILTERS_CHUNK_SUFFIX and Conv2D::BIAS_CHUNK_SUFFIX respectively. The chunk contents is a single precision floating point arrays. The filter coefficients are taken in "OIHW" layout, i.e., there are 'O*I' contiguous packets of 'H*W' values each. "O" and "I" are output and input channel numbers, "H" and "W" are filter height and width.
Conv2D::Conv2D | ( | const std::string & | name, |
const int | kernelSize, | ||
const int | numInputChannels, | ||
const int | numOutputChannels, | ||
const int | stride = 1 , |
||
const Size::Padding | padding = Size::Padding::VALID , |
||
const bool | useBias = true , |
||
const int | numGroups = 1 , |
||
const ActivationFunction | activation = ActivationFunction::DEFAULT |
||
) |
Instantiates a 2D convolution operation.
[in] | name | Operation name |
[in] | kernelSize | Convolution kernel size |
[in] | numInputChannels | Number of input feature map channels (input depth) |
[in] | numOutputChannels | Number of output feature map channels (output depth) |
[in] | stride | Convolution stride |
[in] | padding | Padding policy |
[in] | useBias | If true , the bias addition is enabled. The bias vector is searched in the model data. |
[in] | numGroups | Number of convolution groups to get a group/depthwise convolution |
[in] | activation | Activation function applied to the operation output |
Definition at line 41 of file conv2d.cpp.
|
inlineprivate |
|
privatevirtual |
Compiles GLSL shaders.
[in,out] | gpu | A graphic pipeline instance |
[in,out] | data | Chunkfile containing operation data (e.g. weights and biases) |
[in,out] | bank | A program bank with existing GLSL programs to be reused when possible. If a new program is built, it is added to the bank. |
Implements Beatmup::NNets::AbstractOperation.
Definition at line 88 of file conv2d.cpp.
|
privatevirtual |
Executes the operation.
The operation should be prepared.
[in,out] | thread | Calling CPU thread descriptor |
[in,out] | gpu | A graphic pipeline instance |
Implements Beatmup::NNets::AbstractOperation.
Definition at line 272 of file conv2d.cpp.
|
privatevirtual |
Retrieves minimum required size of zero padding for a given input.
Operations that sample a neighborhood of a pixel may need the input to be padded with zeros, if some of the neighboring samples fall out of the are containing data. In Beatmup the zero padding is handled by allocating a bigger input and putting zeros around the area that is actually filled with data.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 377 of file conv2d.cpp.
|
privatevirtual |
Retrieves range of input features channels sampled at the same time for a specific input.
The operation would typically take the entire storage and sample it at once, if needed. If the number of textures in a storage exceeds the number of texture samplers that the GPU may use simultaneously, an exception occurs. This function provides the necessary information to limit the number of textures in the storage when allocating it. When the limit is reached, multiple channels are packed into a single texture in the storage.
[in] | index | The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive. |
[out] | min | The minimum number of channels that can be sampled at once |
[out] | max | The maximum number of channels that can be sampled at once |
Implements Beatmup::NNets::AbstractOperation.
Definition at line 382 of file conv2d.cpp.
|
inline |
|
inlinevirtual |
Returns number of operation inputs.
Inputs are then indexed from zero to the returned value minus one inclusive.
Reimplemented from Beatmup::NNets::AbstractOperation.
|
inlinevirtual |
Returns number of operation outputs.
Outputs are then indexed from zero to the returned value minus one inclusive.
Reimplemented from Beatmup::NNets::AbstractOperation.
|
inlinevirtual |
Returns true
if the operation can take a Storage::View at a specific input.
Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a storage view on input.
[in] | index | The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 129 of file conv2d.h.
|
inlinevirtual |
Returns true
if the operation can take a Storage::View at a specific output.
Neural network operations may accept different kinds of data containers on outputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a storage view on output.
[in] | index | The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 130 of file conv2d.h.
|
inlinevirtual |
Returns true
if the operation can take a GL::TextureHandler at a specific input.
Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a texture on input.
[in] | index | The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 131 of file conv2d.h.
|
virtual |
Returns full size of a specific operation output.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 397 of file conv2d.cpp.
|
inlinevirtual |
Returns a storage view bound to a specific operation output.
If no view is bound, returns empty view.
[in] | index | The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 135 of file conv2d.h.
|
virtual |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 483 of file conv2d.cpp.
|
virtual |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 518 of file conv2d.cpp.
|
virtual |
|
virtual |
Returns a serialized representation of th operation;.
Implements Beatmup::NNets::AbstractOperation.
Definition at line 415 of file conv2d.cpp.
|
virtual |
Assigns empty inputs and outputs.
Implements Beatmup::NNets::AbstractOperation.
Definition at line 474 of file conv2d.cpp.
|
inline |
Connects a tensor to a residual input.
This input is optional. The tensor is added to the convolution result before the non-linear activation is applied. Its size must match the output size.
[in] | storage | A storage view containing the residual input tensor. |
Definition at line 151 of file conv2d.h.
|
virtual |
Counts (approximate) number of multiply-adds used by this operation.
A single multiply-add is one multiplication and one addition.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 528 of file conv2d.cpp.
|
virtual |
Counts (approximate) number of texels fetches.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 533 of file conv2d.cpp.
|
static |
Sets up deserialization of the operation.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
static |
|
static |