|
OpenNN
Open-source neural networks library
|
Abstract base class for every layer in an OpenNN NeuralNetwork. More...
#include <layer.h>
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< Shape > | get_parameter_shapes () const |
| Shape-only view of get_parameter_specs(). | |
| vector< Shape > | get_state_shapes () const |
| Shape-only view of get_state_specs(). | |
| vector< Shape > | get_forward_shapes (Index b) const |
Shape-only view of get_forward_specs() for batch size b. | |
| vector< Shape > | get_backward_shapes (Index b) const |
Shape-only view of get_backward_specs() for batch size b. | |
| vector< Type > | get_parameter_dtypes () const |
| Dtype-only view of get_parameter_specs(). | |
| vector< Type > | get_forward_dtypes (Index b) const |
Dtype-only view of get_forward_specs() for batch size b. | |
| vector< Type > | get_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< TensorView > | parameters |
| Parameter TensorViews bound to the network's parameter arena. | |
| vector< TensorView > | states |
| State TensorViews bound to the network's state arena. | |
| vector< unique_ptr< Layer > > | layers |
| Sub-layers, when this layer is itself a composite. | |
Abstract base class for every layer in an OpenNN NeuralNetwork.
A Layer declares:
Layers do not own batch-dependent buffers; they only own their parameter and state TensorViews into externally allocated memory.
|
virtualdefault |
Virtual destructor; subclasses are owned via unique_ptr<Layer>.
|
protecteddefault |
Default constructor; only invoked by subclasses.
|
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.
|
protected |
|
inlinevirtualnoexcept |
Forward pass: reads inputs from fp and writes outputs into fp.
| fp | ForwardPropagation buffer slice owned by the caller. |
| layer | Index of this layer inside the network. |
| is_training | True 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.
|
virtual |
Loads the layer configuration (hyperparameters) from JSON.
| document | Parsed JSON document for this layer. |
|
inline |
Dtype-only view of get_backward_specs() for batch size b.
|
inline |
Shape-only view of get_backward_specs() for batch size b.
|
inlinevirtual |
Specifications of the backward intermediate buffers for one batch.
| batch_size | Number of samples in the batch. |
Reimplemented in opennn::Addition, opennn::MultiHeadAttention, and opennn::Recurrent.
|
inline |
Numerical type used for forward/backward computation.
|
inline |
Dtype-only view of get_forward_specs() for batch size b.
|
inline |
Shape-only view of get_forward_specs() for batch size b.
|
inlinevirtual |
Specifications of the forward intermediate buffers for one batch.
| batch_size | Number of samples in the batch. |
Reimplemented in opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, opennn::DenseRelu, opennn::Embedding, opennn::MultiHeadAttention, opennn::Normalization3d, opennn::Pooling3d, opennn::Pooling, and opennn::Recurrent.
|
pure virtual |
Per-sample input shape of the layer (excluding the batch dimension).
Implemented 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::Recurrent, opennn::Scaling, and opennn::Unscaling.
|
inline |
Total number of scalar inputs per sample (product of input dims).
|
inline |
Whether this layer has trainable parameters.
|
inline |
Returns the user-assigned label of this layer.
|
inline |
Returns the canonical type name of this layer.
|
inlinevirtual |
Returns the operators that compose this layer, if any.
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.
|
inlinevirtual |
Activation function fused at the end of this layer, if any.
Reimplemented in opennn::Convolutional, opennn::ConvolutionalRelu, opennn::Dense, and opennn::DenseRelu.
|
pure virtual |
Per-sample output shape of the layer (excluding the batch dimension).
Implemented 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::Recurrent, opennn::Scaling, and opennn::Unscaling.
|
inline |
Total number of scalar outputs per sample (product of output dims).
|
inline |
Dtype-only view of get_parameter_specs().
|
inline |
Shape-only view of get_parameter_specs().
Specifications of the trainable parameter tensors owned by this layer.
Reimplemented in opennn::Recurrent.
|
inline |
Mutable access to this layer's parameter TensorViews.
|
inline |
Read-only access to this layer's parameter TensorViews.
| Index opennn::Layer::get_parameters_number | ( | ) | const |
Total number of trainable parameters in this layer.
|
inline |
Shape-only view of get_state_specs().
|
inline |
Mutable access to this layer's state TensorViews.
|
inline |
Read-only access to this layer's state TensorViews.
|
inline |
|
virtual |
Wires this layer's parameter TensorViews onto an external buffer.
| pointer | Start of the parameter region inside the network's parameter arena. |
|
virtual |
Wires this layer's state TensorViews onto an external buffer.
| pointer | Start of the state region inside the network's state arena. |
|
protected |
Builds views over a contiguous float buffer using shapes.
| pointer | Start of the buffer to slice. |
| shapes | Per-view shapes. |
| views | Output vector of TensorViews, populated in order. |
| tag | Diagnostic label used in error messages. |
shapes.
|
virtual |
Loads parameter and state tensors from a JSON document.
| document | Parsed JSON document containing tensor data. |
|
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.
|
inlinevirtual |
Prints a human-readable summary of the layer to stdout.
Reimplemented in opennn::Unscaling.
|
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.
|
inline |
Forwards externally provided gradient views down to each Operator.
| gradient_views | Per-parameter gradient TensorViews aligned with this layer's parameter specs. |
|
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.
|
inline |
Forwards the current state views down to each composing Operator.
|
inline |
Sets the compute dtype and triggers on_compute_dtype_changed().
| new_compute_dtype | Desired compute dtype. |
|
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.
|
inline |
Sets the human-readable label of this layer.
| new_label | Label moved into the layer. |
|
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.
|
virtual |
Writes the layer configuration to JSON.
| writer | Streaming JSON writer. |
|
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.
|
protected |
Numerical type used for forward and backward computation.
|
protected |
True if this layer is the network's input layer.
|
protected |
True if the layer has parameters that participate in training.
|
protected |
User-visible label for this layer instance (default "my_layer").
|
protected |
Layer type tag set by the subclass.
|
protected |
Sub-layers, when this layer is itself a composite.
|
protected |
Canonical type name set by the subclass (e.g. "dense").
|
protected |
Parameter TensorViews bound to the network's parameter arena.
|
protected |
State TensorViews bound to the network's state arena.