AIfES 2  2.0.0
ailayer_conv2d.h File Reference

Base layer implementation of the Conv2D layer. More...

Go to the source code of this file.

Data Structures

struct  ailayer_conv2d
 General Conv2D layer structure. More...
 

Typedefs

typedef struct ailayer_conv2d ailayer_conv2d_t
 

Functions

ailayer_tailayer_conv2d (ailayer_conv2d_t *layer, ailayer_t *input_layer)
 Initialize and connect the given Conv2D layer. More...
 
void ailayer_conv2d_forward (ailayer_t *self)
 Calculate the forward pass for given Conv2D layer. More...
 
void ailayer_conv2d_backward (ailayer_t *self)
 Calculate the backward pass for given Conv2D layer. More...
 
void ailayer_conv2d_calc_result_shape (ailayer_t *self)
 Calculate the shape of the result tensor (ailayer.result) More...
 
uint32_t ailayer_conv2d_sizeof_bwdmem (const ailayer_t *self)
 Calculate and return the memory size needed for temporary results of the backward pass. More...
 
uint32_t ailayer_conv2d_sizeof_paramem (const ailayer_t *self)
 Calculate and return the parameter memory size needed for this layer. More...
 
void ailayer_conv2d_set_paramem (ailayer_t *self, void *memory_ptr)
 Distribute provided memory to the parameter pointers. More...
 
uint32_t ailayer_conv2d_sizeof_trainmem (const ailayer_t *self)
 Calculate and return the memory size needed by this layer for training. More...
 
void ailayer_conv2d_set_trainmem (ailayer_t *self, void *memory_ptr)
 Distribute provided memory to the gradients pointers. More...
 
void ailayer_conv2d_print_specs (const ailayer_t *self)
 Print the layer specification. More...
 

Variables

const aicore_layertype_tailayer_conv2d_type
 Conv2D layer type. More...
 

Detailed Description

Base layer implementation of the Conv2D layer.

Version
2.2.0
Date
20.10.2020

This is an "abstract" data-type independent implementation. To use the layer use one of the provided implementations for a specific hardware and data-type (for example from ailayer_conv2d_default.h) or set the required math functions on your own.

The Conv2D layer (or 2D convolutional layer) is the core layer of a CNN and performs convolutions on 2-dimensional data slices with trainable weights that can be seen as locally connected neurons with shared weights.

\[ Y = X * W + b \]

When the channel axis equals 1 (channels first), the shapes of input, output and weights are \( [N,C_{in},H_{in},W_{in}] \), \( [N,C_{out},H_{out},W_{out}] \) and \( [C_{out},C_{in},H_{kernel},W_{kernel}] \), respectively.

When the channel axis equals -1 or 3 (channels last), the shapes of input, output and weights are \( [N,H_{in},W_{in},C_{in}] \), \( [N,H_{out},W_{out},C_{out}] \) and \( [C_{out},H_{kernel},W_{kernel},C_{in}] \), respectively.

N is the batch size.

The results of the forward pass of this layer are written to the result tensor of the base ailayer_t struct.

Function Documentation

◆ ailayer_conv2d()

ailayer_t* ailayer_conv2d ( ailayer_conv2d_t layer,
ailayer_t input_layer 
)

Initialize and connect the given Conv2D layer.

This function represents the "constructor" of the abstract Conv2D layer. It initializes the layer structure and connects it to the previous layer.
This function is not intended to call it directly. Instead use one of the data type specific implementations (like for example ailayer_conv2d_f32_default()).

Parameters
*layerThe layer to initialize.
*input_layerThe previous layer that provides the inputs to the layer.
Returns
Pointer to the (successfully) initialized general layer structure (ailayer_conv2d.base)

◆ ailayer_conv2d_backward()

void ailayer_conv2d_backward ( ailayer_t self)

Calculate the backward pass for given Conv2D layer.

Implementation of ailayer.backward.

It uses the deltas tensor of the next layer as input and writes the result of the backward pass to the deltas tensor (ailayer.deltas) of the given layer.

Calculation of the backward pass result:

\[ \partial w \leftarrow w + x_{in} \ast delta_{out} \]

\[ \partial b \leftarrow b + \sum_i delta_{out;i} \]

\[ delta_{in} = delta_{out} \ast' w \]

\( \cdot \ast' \cdot \) is a transposed convolution.
\( w \): Weights (convolution kernels)
\( b \): Bias vector
\( \partial w \): Gradients with respect to the weights
\( \partial b \): Gradients with respect to the bias
\( x_{in} \): Result of the forward pass of the previous layer
\( \delta_{in} \): Result of the backward pass of this layer
\( \delta_{out} \): Result of the backward pass of the next layer

Used math functions:

Parameters
*selfLayer to calculate the backward path for.

◆ ailayer_conv2d_calc_result_shape()

void ailayer_conv2d_calc_result_shape ( ailayer_t self)

Calculate the shape of the result tensor (ailayer.result)

Implementation of ailayer.calc_result_shape.

Resulting shape is \( [N,C_{out},H_{out},W_{out}] \) (channels first) or \( [N,H_{out},W_{out},C_{out}] \) (channels last) with

\[ H_{out} = floor \left( \frac{H_{in} + 2 * P_h - D_h * (H_{kernel} - 1) - 1}{S_h} \right) + 1 \]

\[ W_{out} = floor \left( \frac{W_{in} + 2 * P_w - D_w * (W_{kernel} - 1) - 1}{S_w} \right) + 1 \]

Parameters
*selfLayer to calculate the resulting shape for.

◆ ailayer_conv2d_forward()

void ailayer_conv2d_forward ( ailayer_t self)

Calculate the forward pass for given Conv2D layer.

Implementation of ailayer.forward.

It uses the result tensor of the previous layer as input and writes the result of the forward pass to the result tensor (ailayer.result) of the given layer.

Calculation of the forward pass result:

\[ x_{out} = x_{in} \ast w + b \]

\( w \): Weights (convolution kernels)
\( b \): Bias vector
\( x_{in} \): Result of the forward pass of the previous layer
\( x_{out} \): Result of the forward pass of this layer

Used math functions:

Parameters
*selfLayer to calculate the forward path for.

◆ ailayer_conv2d_print_specs()

void ailayer_conv2d_print_specs ( const ailayer_t self)

Print the layer specification.

Parameters
*selfThe layer to print the specification for

◆ ailayer_conv2d_set_paramem()

void ailayer_conv2d_set_paramem ( ailayer_t self,
void *  memory_ptr 
)

Distribute provided memory to the parameter pointers.

Implementation of ailayer.set_paramem.

Distributes the given buffer to the parameter pointers and sets the tensor parameters for kernels and bias holding structures.
The required parameter size can be calculated with ailayer_conv2d_sizeof_paramem()

Parameters
*selfThe layer to set the memory fields for.
*memory_ptrThe memory that can be used for the parameters

◆ ailayer_conv2d_set_trainmem()

void ailayer_conv2d_set_trainmem ( ailayer_t self,
void *  memory_ptr 
)

Distribute provided memory to the gradients pointers.

Implementation of ailayer.set_trainmem.

The required memory size can be calculated with ailayer_conv2d_sizeof_trainmem().

Parameters
*selfThe layer to set the memory fields for.
*memory_ptrThe memory that can be used for the gradients

◆ ailayer_conv2d_sizeof_bwdmem()

uint32_t ailayer_conv2d_sizeof_bwdmem ( const ailayer_t self)

Calculate and return the memory size needed for temporary results of the backward pass.

Implementation of ailayer.sizeof_bwdmem.

Memory is required for temporary results of weights gradients and bias gradients.

Parameters
*selfThe layer to calculate the memory size for
Returns
Calculated memory size in bytes.

◆ ailayer_conv2d_sizeof_paramem()

uint32_t ailayer_conv2d_sizeof_paramem ( const ailayer_t self)

Calculate and return the parameter memory size needed for this layer.

Implementation of ailayer.sizeof_paramem.

The parameter size is calculated for the weights and bias tensors.

Parameters
*selfThe layer to calculate the parameter memory size for
Returns
Calculated parameter memory size in bytes.

◆ ailayer_conv2d_sizeof_trainmem()

uint32_t ailayer_conv2d_sizeof_trainmem ( const ailayer_t self)

Calculate and return the memory size needed by this layer for training.

Implementation of ailayer.sizeof_trainmem.

The memory size is calculated for the gradient tensors of weights and bias.

Parameters
*selfThe layer to calculate the gradient memory size for.
Returns
Calculated gradient memory size in bytes.

Variable Documentation

◆ ailayer_conv2d_type

const aicore_layertype_t* ailayer_conv2d_type
extern

Conv2D layer type.

Defines the type of the layer (for example for type checks and debug prints). See aicore_layertype for more information about the layer type.