OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::Layer Class Referenceabstract

Abstract base class for every layer in an OpenNN NeuralNetwork. More...

#include <layer.h>

Inheritance diagram for opennn::Layer:
[legend]

Public Member Functions

virtual ~Layer ()=default
 Virtual destructor; subclasses are owned via unique_ptr<Layer>.
 
const string & get_label () const
 Returns the user-assigned label of this layer.
 
const string & get_name () const
 Returns the canonical type name of this layer.
 
LayerType get_type () const
 Returns the LayerType enumerator for this layer.
 
virtual void set_input_shape (const Shape &)
 Sets the per-sample input shape of this layer.
 
virtual void set_output_shape (const Shape &)
 Sets the per-sample output shape of this layer.
 
void set_label (string new_label)
 Sets the human-readable label of this layer.
 
Index get_parameters_number () const
 Total number of trainable parameters in this layer.
 
virtual vector< Operator * > get_operators ()
 Returns the operators that compose this layer, if any.
 
virtual vector< pair< Shape, Type > > get_parameter_specs () const
 Specifications of the trainable parameter tensors owned by this layer.
 
virtual vector< pair< Shape, Type > > get_state_specs () const
 Specifications of the persistent state tensors of this layer.
 
virtual vector< pair< Shape, Type > > get_forward_specs (Index batch_size) const
 Specifications of the forward intermediate buffers for one batch.
 
virtual vector< pair< Shape, Type > > get_backward_specs (Index batch_size) const
 Specifications of the backward intermediate buffers for one batch.
 
vector< Shapeget_parameter_shapes () const
 Shape-only view of get_parameter_specs().
 
vector< Shapeget_state_shapes () const
 Shape-only view of get_state_specs().
 
vector< Shapeget_forward_shapes (Index b) const
 Shape-only view of get_forward_specs() for batch size b.
 
vector< Shapeget_backward_shapes (Index b) const
 Shape-only view of get_backward_specs() for batch size b.
 
vector< Typeget_parameter_dtypes () const
 Dtype-only view of get_parameter_specs().
 
vector< Typeget_forward_dtypes (Index b) const
 Dtype-only view of get_forward_specs() for batch size b.
 
vector< Typeget_backward_dtypes (Index b) const
 Dtype-only view of get_backward_specs() for batch size b.
 
virtual Shape get_input_shape () const =0
 Per-sample input shape of the layer (excluding the batch dimension).
 
virtual Shape get_output_shape () const =0
 Per-sample output shape of the layer (excluding the batch dimension).
 
virtual Activation::Function get_output_activation () const
 Activation function fused at the end of this layer, if any.
 
Index get_inputs_number () const
 Total number of scalar inputs per sample (product of input dims).
 
Index get_outputs_number () const
 Total number of scalar outputs per sample (product of output dims).
 
virtual void forward_propagate (ForwardPropagation &fp, size_t layer, bool is_training) noexcept
 Forward pass: reads inputs from fp and writes outputs into fp.
 
virtual void back_propagate (ForwardPropagation &, BackPropagation &, size_t) const noexcept
 Backward pass: propagates gradients through this layer.
 
virtual void from_JSON (const JsonDocument &document)
 Loads the layer configuration (hyperparameters) from JSON.
 
virtual void read_JSON_body (const Json *)
 Subclass hook for parsing the body of from_JSON().
 
virtual void load_state_from_JSON (const JsonDocument &document)
 Loads parameter and state tensors from a JSON document.
 
virtual void to_JSON (JsonWriter &writer) const
 Writes the layer configuration to JSON.
 
virtual void write_JSON_body (JsonWriter &) const
 Subclass hook for emitting the body of to_JSON().
 
virtual void print () const
 Prints a human-readable summary of the layer to stdout.
 
bool get_is_trainable () const
 Whether this layer has trainable parameters.
 
Type get_compute_dtype () const
 Numerical type used for forward/backward computation.
 
void set_compute_dtype (Type new_compute_dtype)
 Sets the compute dtype and triggers on_compute_dtype_changed().
 
virtual void on_compute_dtype_changed ()
 Hook invoked after set_compute_dtype() mutates the dtype.
 
virtual float * link_parameters (float *pointer)
 Wires this layer's parameter TensorViews onto an external buffer.
 
virtual float * link_states (float *pointer)
 Wires this layer's state TensorViews onto an external buffer.
 
vector< TensorView > & get_parameter_views ()
 Mutable access to this layer's parameter TensorViews.
 
const vector< TensorView > & get_parameter_views () const
 Read-only access to this layer's parameter TensorViews.
 
vector< TensorView > & get_state_views ()
 Mutable access to this layer's state TensorViews.
 
const vector< TensorView > & get_state_views () const
 Read-only access to this layer's state TensorViews.
 
void redistribute_parameters_to_operators ()
 Forwards the current parameter views down to each composing Operator.
 
void redistribute_parameter_gradients_to_operators (vector< TensorView > &gradient_views)
 Forwards externally provided gradient views down to each Operator.
 
void redistribute_states_to_operators ()
 Forwards the current state views down to each composing Operator.
 

Protected Member Functions

 Layer ()=default
 Default constructor; only invoked by subclasses.
 
float * link_views (float *pointer, const vector< Shape > &shapes, vector< TensorView > &views, const char *tag) const
 Builds views over a contiguous float buffer using shapes.
 
void distribute_to_operators (vector< TensorView > &views, void(Operator::*link)(const vector< TensorView > &), vector< pair< Shape, Type > >(Operator::*specs)() const)
 Generic helper used by the redistribute_*_to_operators() routines.
 

Protected Attributes

string label = "my_layer"
 User-visible label for this layer instance (default "my_layer").
 
string name = "layer"
 Canonical type name set by the subclass (e.g. "dense").
 
LayerType layer_type = LayerType::Dense
 Layer type tag set by the subclass.
 
bool is_trainable = true
 True if the layer has parameters that participate in training.
 
bool is_first_layer = false
 True if this layer is the network's input layer.
 
Type compute_dtype = Type::FP32
 Numerical type used for forward and backward computation.
 
vector< TensorViewparameters
 Parameter TensorViews bound to the network's parameter arena.
 
vector< TensorViewstates
 State TensorViews bound to the network's state arena.
 
vector< unique_ptr< Layer > > layers
 Sub-layers, when this layer is itself a composite.
 

Detailed Description

Abstract base class for every layer in an OpenNN NeuralNetwork.

A Layer declares:

  • Its parameter, state, forward and backward buffer specifications, used by the network to size the shared memory arenas before training.
  • Its input and output shapes (per-sample, batch dimension is added by the caller).
  • Its forward and backward propagation routines, which read from and write into ForwardPropagation / BackPropagation views provided by the caller.
  • JSON serialization hooks so the layer can be persisted as part of a NeuralNetwork file.

Layers do not own batch-dependent buffers; they only own their parameter and state TensorViews into externally allocated memory.

Constructor & Destructor Documentation

◆ ~Layer()

virtual opennn::Layer::~Layer ( )
virtualdefault

Virtual destructor; subclasses are owned via unique_ptr<Layer>.

◆ Layer()

opennn::Layer::Layer ( )
protecteddefault

Default constructor; only invoked by subclasses.

Member Function Documentation

◆ back_propagate()

virtual void opennn::Layer::back_propagate ( ForwardPropagation & ,
BackPropagation & ,
size_t  ) const
inlinevirtualnoexcept

Backward pass: propagates gradients through this layer.

Receives the forward intermediates, the BackPropagation buffer in which to accumulate gradients, and this layer's index inside the network. Throws by default; concrete trainable layers must override.

Reimplemented in opennn::Addition, opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::DenseRelu, opennn::Embedding, opennn::Flatten, opennn::MultiHeadAttention, opennn::Normalization3d, opennn::Pooling3d, opennn::Pooling, and opennn::Recurrent.

◆ distribute_to_operators()

void opennn::Layer::distribute_to_operators ( vector< TensorView > & views,
void(Operator::* link )(const vector< TensorView > &),
vector< pair< Shape, Type > >(Operator::* specs )() const )
protected

Generic helper used by the redistribute_*_to_operators() routines.

Parameters
viewsSource TensorViews to slice.
linkOperator member function that receives each sub-view list.
specsOperator member function returning the per-Operator spec list.

◆ forward_propagate()

virtual void opennn::Layer::forward_propagate ( ForwardPropagation & fp,
size_t layer,
bool is_training )
inlinevirtualnoexcept

Forward pass: reads inputs from fp and writes outputs into fp.

Parameters
fpForwardPropagation buffer slice owned by the caller.
layerIndex of this layer inside the network.
is_trainingTrue during training (enables dropout, BN updates, etc.).

The default implementation runs every Operator returned by get_operators() in order. Leaf layers override this directly.

Reimplemented in opennn::ConvolutionalRelu, opennn::MultiHeadAttention, opennn::Recurrent, opennn::Scaling, and opennn::Unscaling.

◆ from_JSON()

virtual void opennn::Layer::from_JSON ( const JsonDocument & document)
virtual

Loads the layer configuration (hyperparameters) from JSON.

Parameters
documentParsed JSON document for this layer.

◆ get_backward_dtypes()

vector< Type > opennn::Layer::get_backward_dtypes ( Index b) const
inline

Dtype-only view of get_backward_specs() for batch size b.

◆ get_backward_shapes()

vector< Shape > opennn::Layer::get_backward_shapes ( Index b) const
inline

Shape-only view of get_backward_specs() for batch size b.

◆ get_backward_specs()

virtual vector< pair< Shape, Type > > opennn::Layer::get_backward_specs ( Index batch_size) const
inlinevirtual

Specifications of the backward intermediate buffers for one batch.

Parameters
batch_sizeNumber of samples in the batch.
Returns
Vector of (Shape, Type) pairs. Empty for non-trainable layers.

Reimplemented in opennn::Addition, opennn::MultiHeadAttention, and opennn::Recurrent.

◆ get_compute_dtype()

Type opennn::Layer::get_compute_dtype ( ) const
inline

Numerical type used for forward/backward computation.

Returns
Compute dtype (FP32 by default; FP16/BF16 in mixed precision).

◆ get_forward_dtypes()

vector< Type > opennn::Layer::get_forward_dtypes ( Index b) const
inline

Dtype-only view of get_forward_specs() for batch size b.

◆ get_forward_shapes()

vector< Shape > opennn::Layer::get_forward_shapes ( Index b) const
inline

Shape-only view of get_forward_specs() for batch size b.

◆ get_forward_specs()

virtual vector< pair< Shape, Type > > opennn::Layer::get_forward_specs ( Index batch_size) const
inlinevirtual

Specifications of the forward intermediate buffers for one batch.

Parameters
batch_sizeNumber of samples in the batch.
Returns
Vector of (Shape, Type) pairs. The last entry is the layer output, wired to downstream layers.

Reimplemented in opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::DenseRelu, opennn::Embedding, opennn::MultiHeadAttention, opennn::Normalization3d, opennn::Pooling3d, opennn::Pooling, and opennn::Recurrent.

◆ get_input_shape()

virtual Shape opennn::Layer::get_input_shape ( ) const
pure virtual

◆ get_inputs_number()

Index opennn::Layer::get_inputs_number ( ) const
inline

Total number of scalar inputs per sample (product of input dims).

Returns
Flat input size.

◆ get_is_trainable()

bool opennn::Layer::get_is_trainable ( ) const
inline

Whether this layer has trainable parameters.

Returns
True if gradients should flow through this layer during training.

◆ get_label()

const string & opennn::Layer::get_label ( ) const
inline

Returns the user-assigned label of this layer.

Returns
Reference to the label string (default "my_layer").

◆ get_name()

const string & opennn::Layer::get_name ( ) const
inline

Returns the canonical type name of this layer.

Returns
Reference to the name string (e.g. "Dense", "Convolutional").

◆ get_operators()

virtual vector< Operator * > opennn::Layer::get_operators ( )
inlinevirtual

Returns the operators that compose this layer, if any.

Returns
Vector of non-owning Operator pointers; empty for leaf layers.

Composite layers (e.g. those mixing convolution and activation) override this so that the base forward_propagate() can iterate through them.

Reimplemented in opennn::Addition, opennn::Bounding, opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::DenseRelu, opennn::Embedding, opennn::Flatten, opennn::MultiHeadAttention, opennn::Normalization3d, opennn::Pooling3d, opennn::Pooling, opennn::Scaling, and opennn::Unscaling.

◆ get_output_activation()

virtual Activation::Function opennn::Layer::get_output_activation ( ) const
inlinevirtual

Activation function fused at the end of this layer, if any.

Returns
Activation::Function::Identity by default; layers like DenseRelu / ConvolutionalRelu override this.

Reimplemented in opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, and opennn::DenseRelu.

◆ get_output_shape()

virtual Shape opennn::Layer::get_output_shape ( ) const
pure virtual

◆ get_outputs_number()

Index opennn::Layer::get_outputs_number ( ) const
inline

Total number of scalar outputs per sample (product of output dims).

Returns
Flat output size.

◆ get_parameter_dtypes()

vector< Type > opennn::Layer::get_parameter_dtypes ( ) const
inline

Dtype-only view of get_parameter_specs().

◆ get_parameter_shapes()

vector< Shape > opennn::Layer::get_parameter_shapes ( ) const
inline

Shape-only view of get_parameter_specs().

◆ get_parameter_specs()

virtual vector< pair< Shape, Type > > opennn::Layer::get_parameter_specs ( ) const
virtual

Specifications of the trainable parameter tensors owned by this layer.

Returns
Vector of (Shape, Type) pairs, one per parameter tensor.

Reimplemented in opennn::Recurrent.

◆ get_parameter_views() [1/2]

vector< TensorView > & opennn::Layer::get_parameter_views ( )
inline

Mutable access to this layer's parameter TensorViews.

◆ get_parameter_views() [2/2]

const vector< TensorView > & opennn::Layer::get_parameter_views ( ) const
inline

Read-only access to this layer's parameter TensorViews.

◆ get_parameters_number()

Index opennn::Layer::get_parameters_number ( ) const

Total number of trainable parameters in this layer.

Returns
Sum of the sizes of all parameter TensorViews.

◆ get_state_shapes()

vector< Shape > opennn::Layer::get_state_shapes ( ) const
inline

Shape-only view of get_state_specs().

◆ get_state_specs()

virtual vector< pair< Shape, Type > > opennn::Layer::get_state_specs ( ) const
virtual

Specifications of the persistent state tensors of this layer.

Returns
Vector of (Shape, Type) pairs, one per state tensor (e.g. running statistics for batch normalization).

◆ get_state_views() [1/2]

vector< TensorView > & opennn::Layer::get_state_views ( )
inline

Mutable access to this layer's state TensorViews.

◆ get_state_views() [2/2]

const vector< TensorView > & opennn::Layer::get_state_views ( ) const
inline

Read-only access to this layer's state TensorViews.

◆ get_type()

LayerType opennn::Layer::get_type ( ) const
inline

Returns the LayerType enumerator for this layer.

Returns
Layer type tag set by the concrete subclass.

◆ link_parameters()

virtual float * opennn::Layer::link_parameters ( float * pointer)
virtual

Wires this layer's parameter TensorViews onto an external buffer.

Parameters
pointerStart of the parameter region inside the network's parameter arena.
Returns
Pointer advanced past the bytes consumed by this layer.

◆ link_states()

virtual float * opennn::Layer::link_states ( float * pointer)
virtual

Wires this layer's state TensorViews onto an external buffer.

Parameters
pointerStart of the state region inside the network's state arena.
Returns
Pointer advanced past the bytes consumed by this layer.

◆ link_views()

float * opennn::Layer::link_views ( float * pointer,
const vector< Shape > & shapes,
vector< TensorView > & views,
const char * tag ) const
protected

Builds views over a contiguous float buffer using shapes.

Parameters
pointerStart of the buffer to slice.
shapesPer-view shapes.
viewsOutput vector of TensorViews, populated in order.
tagDiagnostic label used in error messages.
Returns
Pointer advanced past the bytes covered by shapes.

◆ load_state_from_JSON()

virtual void opennn::Layer::load_state_from_JSON ( const JsonDocument & document)
virtual

Loads parameter and state tensors from a JSON document.

Parameters
documentParsed JSON document containing tensor data.

◆ on_compute_dtype_changed()

virtual void opennn::Layer::on_compute_dtype_changed ( )
inlinevirtual

Hook invoked after set_compute_dtype() mutates the dtype.

Subclasses override this to reconfigure cached dtype-dependent resources (CUDA descriptors, cached scratch buffers, etc.).

Reimplemented in opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::DenseRelu, and opennn::MultiHeadAttention.

◆ print()

virtual void opennn::Layer::print ( ) const
inlinevirtual

Prints a human-readable summary of the layer to stdout.

Reimplemented in opennn::Unscaling.

◆ read_JSON_body()

virtual void opennn::Layer::read_JSON_body ( const Json * )
inlinevirtual

Subclass hook for parsing the body of from_JSON().

Receives the raw JSON node for this layer. Overridden by concrete layers to read their specific fields after the base class has read the common ones.

Reimplemented in opennn::Bounding, opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::Embedding, opennn::MultiHeadAttention, opennn::Normalization3d, opennn::Pooling3d, opennn::Pooling, opennn::Recurrent, opennn::Scaling, and opennn::Unscaling.

◆ redistribute_parameter_gradients_to_operators()

void opennn::Layer::redistribute_parameter_gradients_to_operators ( vector< TensorView > & gradient_views)
inline

Forwards externally provided gradient views down to each Operator.

Parameters
gradient_viewsPer-parameter gradient TensorViews aligned with this layer's parameter specs.

◆ redistribute_parameters_to_operators()

void opennn::Layer::redistribute_parameters_to_operators ( )
inline

Forwards the current parameter views down to each composing Operator.

Composite layers slice their shared parameter buffer and hand sub-views to their Operators so the operators can run independently.

◆ redistribute_states_to_operators()

void opennn::Layer::redistribute_states_to_operators ( )
inline

Forwards the current state views down to each composing Operator.

◆ set_compute_dtype()

void opennn::Layer::set_compute_dtype ( Type new_compute_dtype)
inline

Sets the compute dtype and triggers on_compute_dtype_changed().

Parameters
new_compute_dtypeDesired compute dtype.

◆ set_input_shape()

virtual void opennn::Layer::set_input_shape ( const Shape & )
virtual

Sets the per-sample input shape of this layer.

The argument is the new input shape, excluding the batch dimension. Subclasses override this to also resize the output shape and any shape-dependent parameters.

Reimplemented in opennn::Addition, opennn::Bounding, opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::DenseRelu, opennn::Flatten, opennn::MultiHeadAttention, opennn::Normalization3d, opennn::Pooling3d, opennn::Pooling, opennn::Recurrent, opennn::Scaling, and opennn::Unscaling.

◆ set_label()

void opennn::Layer::set_label ( string new_label)
inline

Sets the human-readable label of this layer.

Parameters
new_labelLabel moved into the layer.

◆ set_output_shape()

virtual void opennn::Layer::set_output_shape ( const Shape & )
virtual

Sets the per-sample output shape of this layer.

The argument is the new output shape, excluding the batch dimension. Subclasses override this to also resize parameters whose dimensions depend on the output shape.

Reimplemented in opennn::Bounding, opennn::Dense, opennn::DenseRelu, opennn::Recurrent, opennn::Scaling, and opennn::Unscaling.

◆ to_JSON()

virtual void opennn::Layer::to_JSON ( JsonWriter & writer) const
virtual

Writes the layer configuration to JSON.

Parameters
writerStreaming JSON writer.

◆ write_JSON_body()

virtual void opennn::Layer::write_JSON_body ( JsonWriter & ) const
inlinevirtual

Subclass hook for emitting the body of to_JSON().

Receives the streaming JSON writer; overridden by concrete layers to emit their subclass-specific fields.

Reimplemented in opennn::Bounding, opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Embedding, opennn::MultiHeadAttention, opennn::Pooling3d, opennn::Pooling, opennn::Recurrent, opennn::Scaling, and opennn::Unscaling.

Member Data Documentation

◆ compute_dtype

Type opennn::Layer::compute_dtype = Type::FP32
protected

Numerical type used for forward and backward computation.

◆ is_first_layer

bool opennn::Layer::is_first_layer = false
protected

True if this layer is the network's input layer.

◆ is_trainable

bool opennn::Layer::is_trainable = true
protected

True if the layer has parameters that participate in training.

◆ label

string opennn::Layer::label = "my_layer"
protected

User-visible label for this layer instance (default "my_layer").

◆ layer_type

LayerType opennn::Layer::layer_type = LayerType::Dense
protected

Layer type tag set by the subclass.

◆ layers

vector<unique_ptr<Layer> > opennn::Layer::layers
protected

Sub-layers, when this layer is itself a composite.

◆ name

string opennn::Layer::name = "layer"
protected

Canonical type name set by the subclass (e.g. "dense").

◆ parameters

vector<TensorView> opennn::Layer::parameters
protected

Parameter TensorViews bound to the network's parameter arena.

◆ states

vector<TensorView> opennn::Layer::states
protected

State TensorViews bound to the network's state arena.