OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::Batch Struct Reference

Owns the host-side and (optional) device-side buffers for one mini-batch of dataset samples. More...

#include <batch.h>

Public Member Functions

 Batch (const Index samples_number=0, const Dataset *dataset=nullptr)
 Constructs a batch sized for a given dataset.
 
void set (const Index samples_number=0, const Dataset *dataset=nullptr)
 (Re)allocates buffers for a given batch size and dataset.
 
void fill (const vector< Index > &sample_indices, const vector< Index > &input_feature_indices, const vector< Index > &decoder_feature_indices, const vector< Index > &target_feature_indices, bool augment=false)
 Fills the host-side buffers from the bound dataset.
 
const vector< TensorView > & get_inputs () const
 Returns the input TensorViews for the active device.
 
const TensorViewget_targets () const
 Returns the target TensorView for the active device.
 
Index get_samples_number () const
 Number of samples currently held in the batch.
 
void print () const
 Prints a human-readable summary of the batch buffers to stdout.
 
bool is_empty () const
 Whether the batch holds zero samples.
 
void copy_device_async (const Index sample_count, cudaStream_t stream)
 Asynchronously copies the host buffers to the device on stream.
 

Public Attributes

Index samples_number = 0
 Number of samples currently held in the batch.
 
const Datasetdataset = nullptr
 Dataset whose shapes determined the batch buffer sizes; not owned.
 
Buffer input
 Owning storage for the input tensor (host or device).
 
Shape input_shape
 Shape of input (per-sample dimensions).
 
Buffer decoder
 Owning storage for the decoder tensor (encoder-decoder models).
 
Shape decoder_shape
 Shape of decoder.
 
Buffer target
 Owning storage for the target tensor.
 
Shape target_shape
 Shape of target.
 
int input_contiguous = -1
 Stride hint passed to Dataset::fill_inputs(); -1 to ignore.
 
int decoder_contiguous = -1
 Stride hint passed to Dataset::fill_inputs() for the decoder; -1 to ignore.
 
int target_contiguous = -1
 Stride hint passed to Dataset::fill_targets(); -1 to ignore.
 
vector< TensorViewinput_views_host_cache
 Host-side input TensorViews (one per input feature group).
 
TensorView target_view_host_cache
 Host-side target TensorView.
 
vector< TensorViewinput_views_cache
 Device-side input TensorViews; populated only on CUDA mode.
 
TensorView target_view_cache
 Device-side target TensorView; populated only on CUDA mode.
 
Index num_input_features = 0
 Total number of input features across all input feature groups.
 
Index num_decoder_features = 0
 Total number of decoder features.
 
Index num_target_features = 0
 Total number of target features.
 
float * inputs_host = nullptr
 Pinned host pointer used to stage inputs into the device buffer.
 
float * decoder_host = nullptr
 Pinned host pointer used to stage decoder inputs into the device buffer.
 
float * targets_host = nullptr
 Pinned host pointer used to stage targets into the device buffer.
 
Index inputs_host_allocated_size = 0
 Allocation capacity of inputs_host, in floats.
 
Index decoder_host_allocated_size = 0
 Allocation capacity of decoder_host, in floats.
 
Index targets_host_allocated_size = 0
 Allocation capacity of targets_host, in floats.
 
Buffer inputs_fp32_staging {Device::CUDA}
 Device-resident FP32 staging buffer used during mixed-precision uploads.
 

Detailed Description

Owns the host-side and (optional) device-side buffers for one mini-batch of dataset samples.

The Batch is filled once per training step from a Dataset using fill(). Its TensorViews (target_view, input_views) are then handed to the NeuralNetwork's forward / backward passes. On CUDA-enabled builds the batch maintains parallel host and device buffers; copy_device_async() uploads the host data on a user-supplied CUDA stream so it can overlap with compute.

Constructor & Destructor Documentation

◆ Batch()

opennn::Batch::Batch ( const Index samples_number = 0,
const Dataset * dataset = nullptr )

Constructs a batch sized for a given dataset.

Parameters
samples_numberNumber of samples per batch.
datasetDataset whose shapes determine the batch buffer sizes.

Member Function Documentation

◆ copy_device_async()

void opennn::Batch::copy_device_async ( const Index sample_count,
cudaStream_t stream )

Asynchronously copies the host buffers to the device on stream.

Parameters
sample_countNumber of samples to copy from the host buffer.
streamCUDA stream used for the asynchronous copy.

◆ fill()

void opennn::Batch::fill ( const vector< Index > & sample_indices,
const vector< Index > & input_feature_indices,
const vector< Index > & decoder_feature_indices,
const vector< Index > & target_feature_indices,
bool augment = false )

Fills the host-side buffers from the bound dataset.

Parameters
sample_indicesIndices of samples copied into this batch.
input_feature_indicesDataset columns used as inputs.
decoder_feature_indicesDataset columns used as decoder inputs (empty when the model has no decoder).
target_feature_indicesDataset columns used as targets.
augmentTrue to apply Dataset::augment_inputs() to the input buffer.

◆ get_inputs()

const vector< TensorView > & opennn::Batch::get_inputs ( ) const
inline

Returns the input TensorViews for the active device.

Returns
Device-side views on CUDA builds, host-side views otherwise.

◆ get_samples_number()

Index opennn::Batch::get_samples_number ( ) const

Number of samples currently held in the batch.

◆ get_targets()

const TensorView & opennn::Batch::get_targets ( ) const
inline

Returns the target TensorView for the active device.

Returns
Device-side view on CUDA builds, host-side view otherwise.

◆ is_empty()

bool opennn::Batch::is_empty ( ) const

Whether the batch holds zero samples.

◆ print()

void opennn::Batch::print ( ) const

Prints a human-readable summary of the batch buffers to stdout.

◆ set()

void opennn::Batch::set ( const Index samples_number = 0,
const Dataset * dataset = nullptr )

(Re)allocates buffers for a given batch size and dataset.

Parameters
samples_numberNumber of samples per batch.
datasetDataset whose shapes determine the batch buffer sizes.

Member Data Documentation

◆ dataset

const Dataset* opennn::Batch::dataset = nullptr

Dataset whose shapes determined the batch buffer sizes; not owned.

◆ decoder

Buffer opennn::Batch::decoder

Owning storage for the decoder tensor (encoder-decoder models).

◆ decoder_contiguous

int opennn::Batch::decoder_contiguous = -1

Stride hint passed to Dataset::fill_inputs() for the decoder; -1 to ignore.

◆ decoder_host

float* opennn::Batch::decoder_host = nullptr

Pinned host pointer used to stage decoder inputs into the device buffer.

◆ decoder_host_allocated_size

Index opennn::Batch::decoder_host_allocated_size = 0

Allocation capacity of decoder_host, in floats.

◆ decoder_shape

Shape opennn::Batch::decoder_shape

◆ input

Buffer opennn::Batch::input

Owning storage for the input tensor (host or device).

◆ input_contiguous

int opennn::Batch::input_contiguous = -1

Stride hint passed to Dataset::fill_inputs(); -1 to ignore.

◆ input_shape

Shape opennn::Batch::input_shape

Shape of input (per-sample dimensions).

◆ input_views_cache

vector<TensorView> opennn::Batch::input_views_cache

Device-side input TensorViews; populated only on CUDA mode.

◆ input_views_host_cache

vector<TensorView> opennn::Batch::input_views_host_cache

Host-side input TensorViews (one per input feature group).

◆ inputs_fp32_staging

Buffer opennn::Batch::inputs_fp32_staging {Device::CUDA}

Device-resident FP32 staging buffer used during mixed-precision uploads.

◆ inputs_host

float* opennn::Batch::inputs_host = nullptr

Pinned host pointer used to stage inputs into the device buffer.

◆ inputs_host_allocated_size

Index opennn::Batch::inputs_host_allocated_size = 0

Allocation capacity of inputs_host, in floats.

◆ num_decoder_features

Index opennn::Batch::num_decoder_features = 0

Total number of decoder features.

◆ num_input_features

Index opennn::Batch::num_input_features = 0

Total number of input features across all input feature groups.

◆ num_target_features

Index opennn::Batch::num_target_features = 0

Total number of target features.

◆ samples_number

Index opennn::Batch::samples_number = 0

Number of samples currently held in the batch.

◆ target

Buffer opennn::Batch::target

Owning storage for the target tensor.

◆ target_contiguous

int opennn::Batch::target_contiguous = -1

Stride hint passed to Dataset::fill_targets(); -1 to ignore.

◆ target_shape

Shape opennn::Batch::target_shape

◆ target_view_cache

TensorView opennn::Batch::target_view_cache

Device-side target TensorView; populated only on CUDA mode.

◆ target_view_host_cache

TensorView opennn::Batch::target_view_host_cache

Host-side target TensorView.

◆ targets_host

float* opennn::Batch::targets_host = nullptr

Pinned host pointer used to stage targets into the device buffer.

◆ targets_host_allocated_size

Index opennn::Batch::targets_host_allocated_size = 0

Allocation capacity of targets_host, in floats.