OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::ConvolutionalRelu Class Referencefinal

2D convolution + ReLU fused into a single forward op on GPU. More...

#include <convolutional_relu_layer.h>

Inheritance diagram for opennn::ConvolutionalRelu:
[legend]

Public Member Functions

 ConvolutionalRelu (const Shape &input_shape={3, 3, 1}, const Shape &kernel_shape={3, 3, 1, 1}, const Shape &strides={1, 1}, const string &convolution_type="Valid", const string &label="convolutional_relu_layer")
 Constructs a ConvolutionalRelu layer.
 
Shape get_input_shape () const override
 Returns the per-sample input shape (height, width, channels).
 
Shape get_output_shape () const override
 Returns the per-sample output shape.
 
Index get_output_height () const
 Output spatial height after applying stride and padding.
 
Index get_output_width () const
 Output spatial width after applying stride and padding.
 
Index get_input_height () const
 Configured input height.
 
Index get_input_width () const
 Configured input width.
 
Index get_input_channels () const
 Configured input channel count.
 
Index get_kernel_height () const
 Kernel height in pixels.
 
Index get_kernel_width () const
 Kernel width in pixels.
 
Index get_kernel_channels () const
 Kernel channel count (must equal input channels).
 
Index get_kernels_number () const
 Number of kernels (output channel count).
 
Index get_row_stride () const
 Vertical stride in pixels.
 
Index get_column_stride () const
 Horizontal stride in pixels.
 
pair< Index, Index > get_padding () const
 Returns padding sizes for the configured convolution type.
 
Index get_padding_height () const
 Padding rows added on each side.
 
Index get_padding_width () const
 Padding columns added on each side.
 
bool get_use_padding () const
 True if padding is applied (i.e. convolution type is not "Valid").
 
Activation::Function get_output_activation () const override
 Activation function fused at the end of this layer (always ReLU).
 
vector< Operator * > get_operators () override
 Returns the single operator (Convolution with fused ReLU on GPU).
 
vector< pair< Shape, Type > > get_forward_specs (Index batch_size) const override
 Specifications of the forward intermediate buffers.
 
void set (const Shape &input_shape={0, 0, 0}, const Shape &kernel_shape={3, 3, 1, 1}, const Shape &strides={1, 1}, const string &convolution_type="Valid", const string &label="convolutional_relu_layer")
 Re-initializes the layer; same arguments as the constructor.
 
void set_input_shape (const Shape &) override
 Updates the input shape and re-shapes kernel tensors accordingly.
 
void on_compute_dtype_changed () override
 Recreates the convolution operator descriptor when the dtype changes.
 
void set_row_stride (const Index new_row_stride)
 Sets the vertical (row) stride.
 
void set_column_stride (const Index new_column_stride)
 Sets the horizontal (column) stride.
 
void set_convolution_type (const string &)
 Sets the padding mode by name.
 
void forward_propagate (ForwardPropagation &, size_t, bool) noexcept override
 Forward pass: convolution + ReLU fused on GPU, sequential on CPU.
 
void back_propagate (ForwardPropagation &, BackPropagation &, size_t) const noexcept override
 Backward pass through the fused ReLU and Convolution operators.
 
void read_JSON_body (const Json *) override
 Reads the layer-specific JSON body (kernel shape, strides, padding) from the given JSON node.
 
void write_JSON_body (JsonWriter &) const override
 Writes the layer-specific JSON body (kernel shape, strides, padding) to the given JSON writer.
 
- Public Member Functions inherited from opennn::Layer
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_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< 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_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.
 
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 from_JSON (const JsonDocument &document)
 Loads the layer configuration (hyperparameters) 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 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 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.
 

Additional Inherited Members

- Protected Member Functions inherited from opennn::Layer
 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 inherited from opennn::Layer
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

2D convolution + ReLU fused into a single forward op on GPU.

Calls cudnnConvolutionBiasActivationForward with CUDNN_ACTIVATION_RELU. On CPU the activation runs as a separate step. No batch normalization, activation hard-wired to ReLU — keeps forward_propagate() branch-free for CUDA Graph capture.

Use this layer instead of Convolutional when ReLU is the desired activation and the runtime cost of branching matters.

Constructor & Destructor Documentation

◆ ConvolutionalRelu()

opennn::ConvolutionalRelu::ConvolutionalRelu ( const Shape & input_shape = {3, 3, 1},
const Shape & kernel_shape = {3, 3, 1, 1},
const Shape & strides = {1, 1},
const string & convolution_type = "Valid",
const string & label = "convolutional_relu_layer" )

Constructs a ConvolutionalRelu layer.

Parameters
input_shapePer-sample input shape (height, width, channels).
kernel_shapeKernel shape (height, width, channels, count).
stridesRow and column strides (one entry each).
convolution_typePadding mode ("Valid" or "Same").
labelHuman-readable label assigned to this layer.

Member Function Documentation

◆ back_propagate()

void opennn::ConvolutionalRelu::back_propagate ( ForwardPropagation & ,
BackPropagation & ,
size_t  ) const
overridevirtualnoexcept

Backward pass through the fused ReLU and Convolution operators.

Receives the forward intermediates, the BackPropagation buffer in which to accumulate gradients, and this layer's index inside the network.

Reimplemented from opennn::Layer.

◆ forward_propagate()

void opennn::ConvolutionalRelu::forward_propagate ( ForwardPropagation & ,
size_t ,
bool  )
overridevirtualnoexcept

Forward pass: convolution + ReLU fused on GPU, sequential on CPU.

Receives the ForwardPropagation buffer slice, this layer's index and the training flag.

Reimplemented from opennn::Layer.

◆ get_column_stride()

Index opennn::ConvolutionalRelu::get_column_stride ( ) const
inline

Horizontal stride in pixels.

◆ get_forward_specs()

vector< pair< Shape, Type > > opennn::ConvolutionalRelu::get_forward_specs ( Index batch_size) const
overridevirtual

Specifications of the forward intermediate buffers.

Parameters
batch_sizeBatch size used for sizing.
Returns
Specs for the Input and Output slots in the Forward enum.

Reimplemented from opennn::Layer.

◆ get_input_channels()

Index opennn::ConvolutionalRelu::get_input_channels ( ) const
inline

Configured input channel count.

◆ get_input_height()

Index opennn::ConvolutionalRelu::get_input_height ( ) const
inline

Configured input height.

◆ get_input_shape()

Shape opennn::ConvolutionalRelu::get_input_shape ( ) const
inlineoverridevirtual

Returns the per-sample input shape (height, width, channels).

Implements opennn::Layer.

◆ get_input_width()

Index opennn::ConvolutionalRelu::get_input_width ( ) const
inline

Configured input width.

◆ get_kernel_channels()

Index opennn::ConvolutionalRelu::get_kernel_channels ( ) const
inline

Kernel channel count (must equal input channels).

◆ get_kernel_height()

Index opennn::ConvolutionalRelu::get_kernel_height ( ) const
inline

Kernel height in pixels.

◆ get_kernel_width()

Index opennn::ConvolutionalRelu::get_kernel_width ( ) const
inline

Kernel width in pixels.

◆ get_kernels_number()

Index opennn::ConvolutionalRelu::get_kernels_number ( ) const
inline

Number of kernels (output channel count).

◆ get_operators()

vector< Operator * > opennn::ConvolutionalRelu::get_operators ( )
inlineoverridevirtual

Returns the single operator (Convolution with fused ReLU on GPU).

Reimplemented from opennn::Layer.

◆ get_output_activation()

Activation::Function opennn::ConvolutionalRelu::get_output_activation ( ) const
inlineoverridevirtual

Activation function fused at the end of this layer (always ReLU).

Reimplemented from opennn::Layer.

◆ get_output_height()

Index opennn::ConvolutionalRelu::get_output_height ( ) const

Output spatial height after applying stride and padding.

◆ get_output_shape()

Shape opennn::ConvolutionalRelu::get_output_shape ( ) const
overridevirtual

Returns the per-sample output shape.

Returns
(output_height, output_width, kernels_number).

Implements opennn::Layer.

◆ get_output_width()

Index opennn::ConvolutionalRelu::get_output_width ( ) const

Output spatial width after applying stride and padding.

◆ get_padding()

pair< Index, Index > opennn::ConvolutionalRelu::get_padding ( ) const
inline

Returns padding sizes for the configured convolution type.

Returns
Pair (padding_height, padding_width) in pixels.

◆ get_padding_height()

Index opennn::ConvolutionalRelu::get_padding_height ( ) const

Padding rows added on each side.

◆ get_padding_width()

Index opennn::ConvolutionalRelu::get_padding_width ( ) const

Padding columns added on each side.

◆ get_row_stride()

Index opennn::ConvolutionalRelu::get_row_stride ( ) const
inline

Vertical stride in pixels.

◆ get_use_padding()

bool opennn::ConvolutionalRelu::get_use_padding ( ) const
inline

True if padding is applied (i.e. convolution type is not "Valid").

◆ on_compute_dtype_changed()

void opennn::ConvolutionalRelu::on_compute_dtype_changed ( )
inlineoverridevirtual

Recreates the convolution operator descriptor when the dtype changes.

Reimplemented from opennn::Layer.

◆ read_JSON_body()

void opennn::ConvolutionalRelu::read_JSON_body ( const Json * )
overridevirtual

Reads the layer-specific JSON body (kernel shape, strides, padding) from the given JSON node.

Reimplemented from opennn::Layer.

◆ set()

void opennn::ConvolutionalRelu::set ( const Shape & input_shape = {0, 0, 0},
const Shape & kernel_shape = {3, 3, 1, 1},
const Shape & strides = {1, 1},
const string & convolution_type = "Valid",
const string & label = "convolutional_relu_layer" )

Re-initializes the layer; same arguments as the constructor.

Parameters
input_shapePer-sample input shape.
kernel_shapeKernel shape (height, width, channels, count).
stridesRow and column strides.
convolution_typePadding mode ("Valid" or "Same").
labelHuman-readable label.

◆ set_column_stride()

void opennn::ConvolutionalRelu::set_column_stride ( const Index new_column_stride)

Sets the horizontal (column) stride.

Parameters
new_column_strideNew column stride in pixels.

◆ set_convolution_type()

void opennn::ConvolutionalRelu::set_convolution_type ( const string & )

Sets the padding mode by name.

Receives the convolution type, "Valid" (no padding) or "Same" (output spatial dimensions equal to input spatial dimensions).

◆ set_input_shape()

void opennn::ConvolutionalRelu::set_input_shape ( const Shape & )
overridevirtual

Updates the input shape and re-shapes kernel tensors accordingly.

Reimplemented from opennn::Layer.

◆ set_row_stride()

void opennn::ConvolutionalRelu::set_row_stride ( const Index new_row_stride)

Sets the vertical (row) stride.

Parameters
new_row_strideNew row stride in pixels.

◆ write_JSON_body()

void opennn::ConvolutionalRelu::write_JSON_body ( JsonWriter & ) const
overridevirtual

Writes the layer-specific JSON body (kernel shape, strides, padding) to the given JSON writer.

Reimplemented from opennn::Layer.