Loading...
Searching...
No Matches
batch.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
9
10#include <atomic>
11#include <mutex>
12#include <thread>
13#include <utility>
14
15namespace opennn
16{
17
18class Dataset;
19
21
22// Constant for a whole run, where the samples a batch draws change every call.
24{
25 vector<Index> inputs;
26 vector<Index> decoder;
27 vector<Index> targets;
28};
29
42
44{
45 vector<int> row_indices;
48
49 Index window_past = 0;
50 Index window_future = 0;
51 Index window_features = 0;
54 bool window_multi_target = false;
55};
56
58
59struct Batch
60{
61 Batch(Index,
62 const Dataset*,
63 const EffectiveConfig&,
64 bool prefetch_only = false);
66
67 Batch(const Batch&) = delete;
68 Batch& operator=(const Batch&) = delete;
69 Batch(Batch&&) = delete;
70 Batch& operator=(Batch&&) = delete;
71
72 void set(Index,
73 const Dataset*,
74 const EffectiveConfig&,
75 bool prefetch_only = false);
76
77 void fill(const vector<Index>& sample_indices,
78 const FeatureSelection&,
80
81 const vector<TensorView>& get_inputs() const
82 {
84 }
85
86 const TensorView& get_targets() const
87 {
89 }
90
91 bool uses_cuda() const
92 {
93 return input.buffer.get_device() == Device::CUDA && device::is_cuda_build();
94 }
95
96 Index get_batch_size() const { return batch_size; }
97
98 bool is_empty() const { return input.buffer.empty() && decoder.buffer.empty() && target.buffer.empty(); }
99
100 Index batch_size = 0;
101 const Dataset* dataset = nullptr;
102
106
108
110
113
115 bool h2d_done_recorded = false;
116
119
120 vector<TensorView> input_views_host_cache;
122
123 vector<TensorView> input_views_cache;
125
126 optional<DeviceGather> device_gather;
129};
130
141
143{
146
149
150 Batch* wait(Index iteration);
151
152 bool publish(Index iteration, Batch* batch);
153
154 bool acquire(Batch*& batch) { return empty_queue.wait_pop(batch); }
155 void release(Batch* batch) { empty_queue.push(batch); }
156 Index claim_iteration() { return next_iteration.fetch_add(1); }
157
158 template <typename Worker>
159 void add_worker(Worker&& worker)
160 {
161 threads.emplace_back(std::forward<Worker>(worker));
162 }
163
165
167
168private:
169
170 enum class SlotState : uint8_t { Pending, Ready, Aborted };
171
172 ThreadSafeQueue<Batch*>& empty_queue;
173 vector<Batch*> ready_batches;
174 vector<atomic<SlotState>> slot_states;
175 atomic<Index> next_iteration{0};
176 mutex error_mutex;
177 exception_ptr worker_error;
178 atomic<bool> error_pending{false};
179 vector<jthread> threads;
180};
181
182}
Definition dataset.h:48
Definition thread_safe_queue.h:16
Definition training.h:13
Definition device_backend.h:298
Definition device_backend.h:254
constexpr bool is_cuda_build() noexcept
Definition device_backend.h:72
Definition configuration.h:10
FillMode
Definition batch.h:20
@ Validation
Definition batch.h:20
@ Inference
Definition batch.h:20
@ Training
Definition batch.h:20
@ CUDA
Definition configuration.h:12
Type
Definition configuration.h:13
@ FP32
Definition configuration.h:13
bool bf16_host_input_cast_enabled() noexcept
cudaStream_t DeviceStream
Definition opennn_types.h:135
ColumnContiguity
Definition tensor_types.h:747
@ Unknown
Definition tensor_types.h:747
Definition batch.h:132
ThreadSafeQueue< Batch * > validation_empty_queue
Definition batch.h:134
vector< unique_ptr< Batch > > training_pool
Definition batch.h:136
ThreadSafeQueue< Batch * > training_empty_queue
Definition batch.h:133
ThreadSafeQueue< Batch * > & validation_queue()
vector< unique_ptr< Batch > > validation_pool
Definition batch.h:137
void add_worker(Worker &&worker)
Definition batch.h:159
Batch * wait(Index iteration)
void release(Batch *batch)
Definition batch.h:155
bool acquire(Batch *&batch)
Definition batch.h:154
BatchPrefetchSession(const BatchPrefetchSession &)=delete
BatchPrefetchSession(ThreadSafeQueue< Batch * > &, Index batches_number)
Index claim_iteration()
Definition batch.h:156
bool publish(Index iteration, Batch *batch)
BatchPrefetchSession & operator=(const BatchPrefetchSession &)=delete
Definition batch.h:31
Buffer buffer
Definition batch.h:34
ColumnContiguity column_contiguity
Definition batch.h:38
Shape shape
Definition batch.h:35
Type type
Definition batch.h:36
device::PinnedBuffer host
Definition batch.h:40
bool has_data() const noexcept
Definition batch.h:32
Definition batch.h:60
const vector< TensorView > & get_inputs() const
Definition batch.h:81
BatchSlot target
Definition batch.h:105
Index get_batch_size() const
Definition batch.h:96
const TensorView & get_targets() const
Definition batch.h:86
device::CudaEvent h2d_done_event
Definition batch.h:114
void wait_h2d_on_compute_stream()
device::PinnedBuffer gather_indices_host
Definition batch.h:127
TensorView target_view_host_cache
Definition batch.h:121
bool uses_cuda() const
Definition batch.h:91
Batch & operator=(Batch &&)=delete
void upload_to_device_batch_async(Batch &, DeviceStream)
void wait_h2d_complete()
void fill(const vector< Index > &sample_indices, const FeatureSelection &, FillMode mode=FillMode::Training)
void record_h2d_done(DeviceStream)
vector< TensorView > input_views_host_cache
Definition batch.h:120
Batch(const Batch &)=delete
Batch(Batch &&)=delete
vector< TensorView > input_views_cache
Definition batch.h:123
void set(Index, const Dataset *, const EffectiveConfig &, bool prefetch_only=false)
TensorView target_view_cache
Definition batch.h:124
device::PinnedBuffer input_host_bf16
Definition batch.h:111
const Dataset * dataset
Definition batch.h:101
optional< DeviceGather > device_gather
Definition batch.h:126
Index batch_size
Definition batch.h:100
BatchSlot decoder
Definition batch.h:104
Batch & operator=(const Batch &)=delete
Batch(Index, const Dataset *, const EffectiveConfig &, bool prefetch_only=false)
bool h2d_done_recorded
Definition batch.h:115
BatchSlot input
Definition batch.h:103
Buffer gather_indices_device
Definition batch.h:128
Buffer fp32_staging
Definition batch.h:112
bool is_empty() const
Definition batch.h:98
Definition tensor_types.h:340
Definition batch.h:44
Index input_col_offset
Definition batch.h:46
Index window_past
Definition batch.h:49
Index window_target_cols
Definition batch.h:52
Index window_features
Definition batch.h:51
vector< int > row_indices
Definition batch.h:45
Index window_future
Definition batch.h:50
bool window_multi_target
Definition batch.h:54
Index window_matrix_rows
Definition batch.h:53
Index target_col_offset
Definition batch.h:47
Definition configuration.h:58
Definition batch.h:24
vector< Index > targets
Definition batch.h:27
vector< Index > inputs
Definition batch.h:25
vector< Index > decoder
Definition batch.h:26
Definition tensor_types.h:172
Definition tensor_types.h:540