Loading...
Searching...
No Matches
forward_propagation.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 <tuple>
11
12namespace opennn
13{
14
16{
17 const vector<Index>* host = nullptr;
18 const int* device = nullptr;
19
20 explicit operator bool() const noexcept { return host || device; }
21};
22
23template<size_t... Kind>
24array<Buffer, sizeof...(Kind)> cuda_workspace_buffers(index_sequence<Kind...>)
25{
26 return {(static_cast<void>(Kind), Buffer{Device::CUDA})...};
27}
28
29class Network;
30class TrainingArenaPlan;
31
37
38inline bool is_training(ForwardPropagationMode pass) noexcept
39{
41}
42
50
52{
53 ForwardPropagation() = default;
54
58 bool inputs_pre_scaled = false,
59 span<const MemoryPoolEntry> co_planned_lifetimes = {},
60 bool exhaustive_training_plan = false);
61
63
66
67 void set(Index, Network*, Buffer* external_storage = nullptr,
70 bool inputs_pre_scaled = false,
71 span<const MemoryPoolEntry> co_planned_lifetimes = {},
72 bool exhaustive_training_plan = false);
73
74 vector<Index> co_planned_offsets;
75
77
78 void set_active_sequence_length(Index length);
79
81
82 // Grows explicitly enabled Qwen BF16 caches; other models are unchanged.
83 // Shared propagations must invalidate their graph when this returns true.
84 bool reserve_kv_cache(Index required, Index preserved_tokens);
85
86 void set_output_sequence_window(Index start, Index count);
88
89 Index get_sequence_capacity() const noexcept { return sequence_capacity; }
90 Index get_final_output_capacity() const noexcept { return final_output_capacity; }
91 Index get_final_output_layer() const noexcept { return final_output_layer; }
92 Index get_execution_start_layer() const noexcept { return execution_start_layer; }
93 bool needs_position_staging() const noexcept { return position_staging_required; }
94
96
98
99 // Loss-aware contexts can retain an eligible sigmoid head's affine output.
100 // Ordinary probability inference does not allocate this training scratch.
101 void enable_binary_logits(Buffer* shared_storage = nullptr);
102
103 void recompute_for_backward(Index layer_index);
104
105 void set_cuda_graph(bool enabled);
106 void reset_cuda_graph() noexcept;
109 device::GraphWorkspaceViews get_cuda_graph_workspace_views() const noexcept;
110
111 Index batch_size = 0;
113
114 Index past_length = 0;
115
119 vector<device::PinnedBuffer> layer_pinned_storage;
122
125
129
130 vector<vector<uint16_t>> host_bf16_input_scratch;
131 vector<uint16_t> host_bf16_output_scratch;
132
135
136 vector<vector<TensorView>> inputs;
137 vector<vector<TensorView>> slots;
138 vector<uint8_t> drelu_fused_by_layer;
139 vector<tuple<size_t, size_t, size_t>> passthrough_overrides;
140
141 vector<vector<Index>> valid_lengths;
142 vector<const int*> device_valid_lengths;
144
145 const vector<Index>* input_valid_lengths(size_t layer, size_t input_ordinal) const;
146 const int* input_device_valid_lengths(size_t layer, size_t input_ordinal) const;
147 SequenceLengths input_sequence_lengths(size_t layer, size_t input_ordinal) const;
148
149 int* device_valid_lengths_slot(size_t layer, Index batch_size);
150
151 void inherit_valid_lengths(size_t layer);
152
153 Index valid_lengths_source(size_t layer, size_t input_ordinal) const;
154
155 bool use_cuda_graph = false;
156 bool cuda_graph_failed = false;
159 vector<const void*> captured_input_pointers;
160
163 = cuda_workspace_buffers(make_index_sequence<size_t(device::GraphWorkspaceKind::Count)>{});
164
165 // The network's parameter version, for operators holding a cache derived
166 // from the weights. Defined out of line so callers do not need the full
167 // Network definition. Returns 0 when there is no network, which every
168 // cache must read as "cannot vouch for this, do not reuse".
169 uint64_t get_parameters_version() const;
170
171private:
172
173 friend struct TrainingContext;
174
175 void set(Index, Network*, Buffer* external_storage,
176 bool inputs_pre_scaled, TrainingArenaPlan&);
177
178 friend class ChatSession;
179 void release_inference_storage();
180
181 struct OutputWindow
182 {
183 Buffer input;
184 Index start = 0;
185 Index count = 0;
186 };
187
188 Index bind_slots(const vector<vector<TensorSpec>>& forward_specs,
189 const vector<vector<Index>>& slot_offsets,
190 const vector<vector<Index>>& transient_slot_offsets);
191
192 TensorView get_layer_outputs(Index layer) const;
193
194 Network* network = nullptr;
195 vector<vector<TensorView>> capacity_inputs;
196 vector<vector<TensorView>> capacity_slots;
197 vector<size_t> recomputable_slots;
198 Index sequence_capacity = 0;
199 Index active_sequence_length = 0;
200 Index execution_start_layer = 0;
201 bool position_staging_required = false;
202 Index final_output_capacity = 0;
203 Index final_output_layer = -1;
204 optional<OutputWindow> output_window;
205};
206
207}
Definition network.h:87
Definition training_arena_plan.h:18
Definition training.h:13
Definition device_backend.h:254
Definition energy_meter.h:7
unique_ptr< remove_pointer_t< DeviceGraphExec >, GraphExecDeleter > GraphExecHandle
Definition device_backend.h:326
std::array< Index, static_cast< size_t >(GraphWorkspaceKind::Count)> GraphWorkspaceRequirements
Definition device_backend.h:122
@ Count
Definition device_backend.h:108
Definition configuration.h:10
bool is_training(ForwardPropagationMode pass) noexcept
Definition forward_propagation.h:38
@ Inference
Definition batch.h:20
@ CUDA
Definition configuration.h:12
Eigen::array< T, N > array
Definition tensor_types.h:736
cudaStream_t DeviceStream
Definition opennn_types.h:135
array< Buffer, sizeof...(Kind)> cuda_workspace_buffers(index_sequence< Kind... >)
Definition forward_propagation.h:24
ForwardPropagationMode
Definition forward_propagation.h:33
@ Training
Definition forward_propagation.h:34
Definition tensor_types.h:340
Index past_length
Definition forward_propagation.h:114
vector< vector< Index > > valid_lengths
Definition forward_propagation.h:141
Buffer arena
Definition forward_propagation.h:116
void set(Index, Network *, Buffer *external_storage=nullptr, ForwardPropagationMode=ForwardPropagationMode::Training, InferenceShapePolicy={}, bool inputs_pre_scaled=false, span< const MemoryPoolEntry > co_planned_lifetimes={}, bool exhaustive_training_plan=false)
void stage_position(DeviceStream stream)
vector< TensorView > staged_inputs
Definition forward_propagation.h:121
void set_active_sequence_length(Index length)
device::PinnedBuffer position_pinned
Definition forward_propagation.h:134
bool use_cuda_graph
Definition forward_propagation.h:155
array< Buffer, size_t(device::GraphWorkspaceKind::Count)> inference_graph_workspaces
Definition forward_propagation.h:163
bool cuda_graph_failed
Definition forward_propagation.h:156
void reset_cuda_graph() noexcept
bool needs_position_staging() const noexcept
Definition forward_propagation.h:93
vector< uint16_t > host_bf16_output_scratch
Definition forward_propagation.h:131
device::GraphWorkspaceViews get_cuda_graph_workspace_views() const noexcept
const int * input_device_valid_lengths(size_t layer, size_t input_ordinal) const
vector< Buffer > layer_state_storage
Definition forward_propagation.h:117
Index cuda_graph_warmup_calls
Definition forward_propagation.h:157
Buffer loss_target_workspace
Definition forward_propagation.h:124
Index get_final_output_capacity() const noexcept
Definition forward_propagation.h:90
TensorView binary_logits
Definition forward_propagation.h:127
void enable_binary_logits(Buffer *shared_storage=nullptr)
Buffer position_device
Definition forward_propagation.h:133
bool cuda_graph_workspaces_need_growth() const noexcept
ForwardPropagation(const ForwardPropagation &)=delete
vector< vector< TensorView > > inputs
Definition forward_propagation.h:136
void set_cuda_graph(bool enabled)
Index batch_size
Definition forward_propagation.h:111
vector< device::PinnedBuffer > layer_pinned_storage
Definition forward_propagation.h:119
vector< const void * > captured_input_pointers
Definition forward_propagation.h:159
void recompute_for_backward(Index layer_index)
vector< uint8_t > drelu_fused_by_layer
Definition forward_propagation.h:138
Index get_sequence_capacity() const noexcept
Definition forward_propagation.h:89
Buffer loss_workspace
Definition forward_propagation.h:123
SequenceLengths input_sequence_lengths(size_t layer, size_t input_ordinal) const
ForwardPropagation(Index, Network *, ForwardPropagationMode=ForwardPropagationMode::Training, InferenceShapePolicy={}, bool inputs_pre_scaled=false, span< const MemoryPoolEntry > co_planned_lifetimes={}, bool exhaustive_training_plan=false)
ForwardPropagationMode mode
Definition forward_propagation.h:112
void set_output_sequence_window(Index start, Index count)
vector< Buffer > device_valid_length_storage
Definition forward_propagation.h:143
TensorView get_outputs() const
device::GraphExecHandle inference_graph_exec
Definition forward_propagation.h:158
void inherit_valid_lengths(size_t layer)
Index get_execution_start_layer() const noexcept
Definition forward_propagation.h:92
vector< vector< uint16_t > > host_bf16_input_scratch
Definition forward_propagation.h:130
vector< tuple< size_t, size_t, size_t > > passthrough_overrides
Definition forward_propagation.h:139
Index get_final_output_layer() const noexcept
Definition forward_propagation.h:91
Buffer binary_logits_storage
Definition forward_propagation.h:126
vector< Buffer > staged_input_storage
Definition forward_propagation.h:120
vector< const int * > device_valid_lengths
Definition forward_propagation.h:142
vector< Index > co_planned_offsets
Definition forward_propagation.h:74
Index valid_lengths_source(size_t layer, size_t input_ordinal) const
device::GraphWorkspaceRequirements inference_graph_workspace_requirements
Definition forward_propagation.h:161
vector< vector< TensorView > > slots
Definition forward_propagation.h:137
bool reserve_kv_cache(Index required, Index preserved_tokens)
shared_ptr< vector< Buffer > > layer_session_state_storage
Definition forward_propagation.h:118
int * device_valid_lengths_slot(size_t layer, Index batch_size)
friend class ChatSession
Definition forward_propagation.h:178
friend struct TrainingContext
Definition forward_propagation.h:173
void share_session_state_from(const ForwardPropagation &source)
const vector< Index > * input_valid_lengths(size_t layer, size_t input_ordinal) const
ForwardPropagation & operator=(const ForwardPropagation &)=delete
TensorView get_last_trainable_layer_outputs() const
uint64_t get_parameters_version() const
Index binary_logits_layer
Definition forward_propagation.h:128
Definition forward_propagation.h:44
Index sequence_capacity
Definition forward_propagation.h:45
bool capture_binary_logits
Definition forward_propagation.h:48
Index final_output_capacity
Definition forward_propagation.h:46
vector< Index > retained_output_layers
Definition forward_propagation.h:47
Definition forward_propagation.h:16
const int * device
Definition forward_propagation.h:18
const vector< Index > * host
Definition forward_propagation.h:17
Definition tensor_types.h:540