OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
batch.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// B A T C H S T R U C T H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include "tensor_utilities.h"
12#include "dataset.h"
13
14namespace opennn
15{
16
18struct Batch
19{
23 Batch(const Index = 0, const Dataset* = nullptr);
25
26 Batch(const Batch&) = delete;
27 Batch& operator=(const Batch&) = delete;
28 Batch(Batch&&) = delete;
29 Batch& operator=(Batch&&) = delete;
30
34 void set(const Index = 0, const Dataset* = nullptr);
35
43 void fill(const vector<Index>&,
44 const vector<Index>&,
45 const vector<Index>&,
46 const vector<Index>&,
47 bool is_training = true,
48 bool parallelize_samples = true);
49
51 const vector<TensorView>& get_inputs() const
52 {
53#ifdef OPENNN_HAS_CUDA
54 if (is_gpu()) return input_views_cache;
55#endif
57 }
58
60 const TensorView& get_targets() const
61 {
62#ifdef OPENNN_HAS_CUDA
63 if (is_gpu()) return target_view_cache;
64#endif
66 }
67
69 Index get_samples_number() const;
70
72 void print() const;
73
75 bool is_empty() const;
76
77 Index samples_number = 0;
78 Index current_sample_count = 0; // set by fill(); may be < samples_number
79
80 const Dataset* dataset = nullptr;
81
84
87
90
94
95#ifdef OPENNN_HAS_CUDA
100 void copy_device_async(const Index, cudaStream_t, float* fp32_staging);
101#endif
102
104
105 vector<TensorView> input_views_host_cache;
107
108 vector<TensorView> input_views_cache; // GPU views; populated only on CUDA mode
110
114
115 float* inputs_host = nullptr;
116 float* decoder_host = nullptr;
117 float* targets_host = nullptr;
118
122
123 bool needs_fp32_staging = false;
124};
125
126}
127
128// OpenNN: Open Neural Networks Library.
129// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
130// Licensed under the GNU Lesser General Public License v2.1 or later.
Abstract base class for OpenNN datasets, owning samples, variables, and metadata.
Definition dataset.h:61
Definition adaptive_moment_estimation.h:14
bool is_gpu()
Returns true when the resolved configuration runs on a CUDA GPU.
Definition configuration.h:186
void * cudaStream_t
Definition pch.h:82
Index decoder_features_number
Definition batch.h:112
const vector< TensorView > & get_inputs() const
Returns the tensor views over the input buffer (device on GPU mode, host on CPU mode).
Definition batch.h:51
const TensorView & get_targets() const
Returns the tensor view over the target buffer (device on GPU mode, host on CPU mode).
Definition batch.h:60
float * targets_host
Definition batch.h:117
Index inputs_host_allocated_size
Definition batch.h:119
int target_contiguous
Definition batch.h:93
TensorView target_view_host_cache
Definition batch.h:106
Batch(const Index=0, const Dataset *=nullptr)
Constructs a batch sized for samples_number samples drawn from dataset.
Index get_input_elements() const
Definition batch.h:103
Index input_features_number
Definition batch.h:111
Batch & operator=(Batch &&)=delete
void set(const Index=0, const Dataset *=nullptr)
Reconfigures the batch for a new size or dataset; reuses allocations when possible.
Shape input_shape
Definition batch.h:83
vector< TensorView > input_views_host_cache
Definition batch.h:105
Index get_samples_number() const
Returns the current sample count (set by fill(); may be < samples_number).
Buffer decoder
Definition batch.h:85
Batch(const Batch &)=delete
int decoder_contiguous
Definition batch.h:92
int input_contiguous
Definition batch.h:91
Batch(Batch &&)=delete
bool needs_fp32_staging
Definition batch.h:123
vector< TensorView > input_views_cache
Definition batch.h:108
Shape decoder_shape
Definition batch.h:86
TensorView target_view_cache
Definition batch.h:109
Buffer input
Definition batch.h:82
Index current_sample_count
Definition batch.h:78
Index samples_number
Definition batch.h:77
const Dataset * dataset
Definition batch.h:80
Index decoder_host_allocated_size
Definition batch.h:120
Index target_features_number
Definition batch.h:113
void fill(const vector< Index > &, const vector< Index > &, const vector< Index > &, const vector< Index > &, bool is_training=true, bool parallelize_samples=true)
Loads the indicated samples from the dataset into the batch buffers.
Index targets_host_allocated_size
Definition batch.h:121
Shape target_shape
Definition batch.h:89
Batch & operator=(const Batch &)=delete
float * inputs_host
Definition batch.h:115
void print() const
Prints a human-readable summary of the batch shapes and contents.
float * decoder_host
Definition batch.h:116
Buffer target
Definition batch.h:88
bool is_empty() const
Returns true when the batch is uninitialized or holds zero samples.
Owning raw byte buffer that lives on CPU or CUDA memory, with aligned (re)allocation.
Definition tensor_utilities.h:166
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42
Non-owning view over a tensor: pointer, shape, and data type with rich reshape helpers.
Definition tensor_utilities.h:293