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

Abstract base class for all OpenNN layers; orchestrates operators and shape propagation. More...

#include <layer.h>

Inheritance diagram for opennn::Layer:
[legend]

Public Member Functions

virtual ~Layer ()=default
 
const string & get_label () const
 
const string & get_name () const
 
LayerType get_type () const
 
virtual void set_input_shape (const Shape &)
 Sets the input shape; subclasses override to derive dependent dimensions.
 
virtual void set_output_shape (const Shape &)
 Sets the output shape; subclasses override when the output is user-configurable.
 
void set_label (string new_label)
 
Index get_parameters_number () const
 Returns the total number of trainable parameters owned by this layer.
 
const vector< Operator * > & get_operators () const
 
virtual vector< TensorSpecget_parameter_specs () const
 Returns the tensor specs of trainable parameters; subclasses override.
 
virtual vector< TensorSpecget_state_specs () const
 Returns the tensor specs of persistent state (e.g. running mean/variance).
 
virtual vector< TensorSpecget_forward_specs (Index batch_size) const
 Returns the tensor specs of the forward workspace; defaults to a single output tensor.
 
virtual vector< TensorSpecget_backward_specs (Index batch_size) const
 Returns the tensor specs of the backward workspace; empty for non-trainable layers.
 
virtual Shape get_input_shape () const
 Returns the input shape stored by the layer.
 
virtual Shape get_output_shape () const =0
 Returns the output shape; subclasses must implement this to expose their geometry.
 
virtual ActivationOp::Function get_output_activation () const
 Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).
 
Index get_inputs_number () const
 
Index get_outputs_number () const
 
virtual void forward_propagate (ForwardPropagation &fp, size_t layer, bool is_training) noexcept
 Runs the forward pass by chaining the layer's operators in order.
 
virtual void back_propagate (ForwardPropagation &fp, BackPropagation &bp, size_t i) const noexcept
 Runs the backward pass by chaining the layer's operators in reverse order.
 
virtual void from_JSON (const JsonDocument &document)
 Restores layer configuration and parameters from a JSON document.
 
virtual void read_JSON_body (const Json *)
 Subclass hook reading the body section of the layer's JSON node.
 
virtual void load_state_from_JSON (const JsonDocument &document)
 Restores persistent state (e.g. running statistics) from a JSON document.
 
virtual void to_JSON (JsonWriter &writer) const
 Serializes layer configuration and parameters to a JSON writer.
 
virtual void write_JSON_body (JsonWriter &) const
 Subclass hook writing the body section of the layer's JSON node.
 
virtual string write_expression (const vector< string > &, const vector< string > &) const
 Returns a human-readable mathematical expression for this layer (empty by default).
 
virtual void print () const
 Prints a human-readable summary of the layer to standard output.
 
bool get_is_trainable () const
 
Type get_compute_dtype () const
 
void set_compute_dtype (Type new_compute_dtype)
 Sets the compute dtype and notifies subclasses via on_compute_dtype_changed().
 
virtual void on_compute_dtype_changed ()
 Subclass hook invoked when the compute dtype changes; default is no-op.
 
virtual float * link_states (float *pointer)
 Binds the persistent-state region of the shared buffer to operator views.
 
float * link_gradients (float *pointer, vector< TensorView > &gradient_views)
 Binds the gradient slice of the shared buffer to operator gradient views.
 
vector< TensorView > & get_parameter_views ()
 
const vector< TensorView > & get_parameter_views () const
 
void redistribute_parameters_to_operators ()
 Re-binds operator parameter views after the parameter buffer has been resized or moved.
 

Protected Types

enum  Forward { Input , Output }
 
enum  Backward { OutputDelta , InputDelta }
 

Protected Member Functions

 Layer ()=default
 
 Layer (LayerType t, bool trainable=true)
 
float * link_views_to_operators (vector< TensorView > &views, float *pointer, vector< TensorSpec >(Operator::*specs_fn)() const, void(Operator::*link_fn)(span< const TensorView >))
 

Protected Attributes

string label = "my_layer"
 
LayerType layer_type = LayerType::Dense
 
bool is_trainable = true
 
Shape input_shape
 
Type compute_dtype = Type::FP32
 
vector< TensorViewparameters
 
vector< TensorViewstates
 
vector< Operator * > operators
 

Detailed Description

Abstract base class for all OpenNN layers; orchestrates operators and shape propagation.

Member Enumeration Documentation

◆ Backward

enum opennn::Layer::Backward
protected
Enumerator
OutputDelta 
InputDelta 

◆ Forward

enum opennn::Layer::Forward
protected
Enumerator
Input 
Output 

Constructor & Destructor Documentation

◆ ~Layer()

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

◆ Layer() [1/2]

opennn::Layer::Layer ( )
protecteddefault

◆ Layer() [2/2]

opennn::Layer::Layer ( LayerType t,
bool trainable = true )
inlineprotected

Member Function Documentation

◆ back_propagate()

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

Runs the backward pass by chaining the layer's operators in reverse order.

Parameters
fpForward propagation workspace (read-only here).
bpBack propagation workspace receiving gradients.
iIndex of this layer in the workspace.

Reimplemented in opennn::Recurrent.

◆ forward_propagate()

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

Runs the forward pass by chaining the layer's operators in order.

Parameters
fpForward propagation workspace.
layerIndex of this layer in the workspace.
is_trainingIf true, enables training-only behavior in child operators.

Reimplemented in opennn::Recurrent.

◆ from_JSON()

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

Restores layer configuration and parameters from a JSON document.

◆ get_backward_specs()

virtual vector< TensorSpec > opennn::Layer::get_backward_specs ( Index batch_size) const
inlinenodiscardvirtual

Returns the tensor specs of the backward workspace; empty for non-trainable layers.

Parameters
batch_sizeBatch size used to size each tensor.

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

◆ get_compute_dtype()

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

◆ get_forward_specs()

virtual vector< TensorSpec > opennn::Layer::get_forward_specs ( Index batch_size) const
inlinenodiscardvirtual

Returns the tensor specs of the forward workspace; defaults to a single output tensor.

Parameters
batch_sizeBatch size used to size each tensor.

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

◆ get_input_shape()

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

◆ get_inputs_number()

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

◆ get_is_trainable()

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

◆ get_label()

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

◆ get_name()

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

◆ get_operators()

const vector< Operator * > & opennn::Layer::get_operators ( ) const
inlinenodiscard

◆ get_output_activation()

virtual ActivationOp::Function opennn::Layer::get_output_activation ( ) const
inlinenodiscardvirtual

Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).

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

◆ get_output_shape()

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

◆ get_outputs_number()

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

◆ get_parameter_specs()

virtual vector< TensorSpec > opennn::Layer::get_parameter_specs ( ) const
nodiscardvirtual

Returns the tensor specs of trainable parameters; subclasses override.

Reimplemented in opennn::Recurrent.

◆ get_parameter_views() [1/2]

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

◆ get_parameter_views() [2/2]

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

◆ get_parameters_number()

Index opennn::Layer::get_parameters_number ( ) const
nodiscard

Returns the total number of trainable parameters owned by this layer.

◆ get_state_specs()

virtual vector< TensorSpec > opennn::Layer::get_state_specs ( ) const
nodiscardvirtual

Returns the tensor specs of persistent state (e.g. running mean/variance).

◆ get_type()

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

◆ link_gradients()

float * opennn::Layer::link_gradients ( float * pointer,
vector< TensorView > & gradient_views )

Binds the gradient slice of the shared buffer to operator gradient views.

Parameters
pointerStart of the layer's slice in the shared gradient buffer.
gradient_viewsOutput list of views the optimizer iterates over.
Returns
Pointer advanced past this layer's gradient region.

◆ link_states()

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

Binds the persistent-state region of the shared buffer to operator views.

Parameters
pointerStart of the layer's slice in the shared state buffer.
Returns
Pointer advanced past this layer's state region.

Reimplemented in opennn::Bounding, opennn::Scaling, and opennn::Unscaling.

◆ link_views_to_operators()

float * opennn::Layer::link_views_to_operators ( vector< TensorView > & views,
float * pointer,
vector< TensorSpec >(Operator::* specs_fn )() const,
void(Operator::* link_fn )(span< const TensorView >) )
protected

◆ load_state_from_JSON()

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

Restores persistent state (e.g. running statistics) from a JSON document.

◆ on_compute_dtype_changed()

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

Subclass hook invoked when the compute dtype changes; default is no-op.

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 standard output.

◆ read_JSON_body()

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

◆ redistribute_parameters_to_operators()

void opennn::Layer::redistribute_parameters_to_operators ( )

Re-binds operator parameter views after the parameter buffer has been resized or moved.

◆ set_compute_dtype()

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

Sets the compute dtype and notifies subclasses via on_compute_dtype_changed().

◆ set_input_shape()

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

◆ set_label()

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

◆ set_output_shape()

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

Sets the output shape; subclasses override when the output is user-configurable.

Reimplemented in opennn::Dense, opennn::DenseRelu, opennn::Pooling3d, opennn::Recurrent, and opennn::Unscaling.

◆ to_JSON()

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

Serializes layer configuration and parameters to a JSON writer.

◆ write_expression()

virtual string opennn::Layer::write_expression ( const vector< string > & ,
const vector< string > &  ) const
inlinenodiscardvirtual

Returns a human-readable mathematical expression for this layer (empty by default).

Parameters
input_namesNames assigned to the inputs.
output_namesNames assigned to the outputs.

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

◆ write_JSON_body()

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

Member Data Documentation

◆ compute_dtype

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

◆ input_shape

Shape opennn::Layer::input_shape
protected

◆ is_trainable

bool opennn::Layer::is_trainable = true
protected

◆ label

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

◆ layer_type

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

◆ operators

vector<Operator*> opennn::Layer::operators
protected

◆ parameters

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

◆ states

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