Repository navigation
Expand file tree
/
Copy pathrunner.h
More file actions
332 lines (290 loc) · 14.5 KB
/
Copy pathrunner.h
File metadata and controls
332 lines (290 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
// Runs batches from a scheduler through an executor, and carries whole
// generations to completion on top of that.
//
// One engine thread owns the executor and all generation state. Callers reach
// it two ways, neither of which blocks it: a command inbox for session and
// generation work, which must run where the executor lives, and a cancellation
// flag that needs no round trip.
//
// Generation is completion-driven. When a batch settles, the engine thread
// emits what each output-producing task produced and submits its continuation.
// No thread is parked per conversation, so batch occupancy does not depend on
// callers being blocked at the same moment.
//
// Output is pushed: an admitted generation's callbacks run on the engine
// thread as its steps settle. They must not block, because every other
// generation in the batch is waiting behind them. A callback may request
// shutdown, but must not destroy the Runner or wait for work that requires the
// engine thread.
//
// Generation starts from a caller-resolved delta. The session owns its
// executor-committed position; the runner chunks and schedules the delta but
// does not reconcile it against session history.
#include <cstdint>
#include <functional>
#include <future>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>
#include <executorch/extension/llm/batching/executor.h>
#include <executorch/extension/llm/batching/metrics.h>
#include <executorch/extension/llm/batching/scheduler.h>
#include <executorch/extension/llm/batching/types.h>
#include <executorch/runtime/platform/compiler.h> // ET_EXPERIMENTAL
namespace executorch {
namespace extension {
namespace llm {
namespace batching {
// Why a generation stopped.
enum class ET_EXPERIMENTAL FinishReason {
StopToken, // a token in GenConfig::stop_tokens was produced
NewTokenLimit, // GenConfig::max_new_tokens reached
Cancelled, // cancelled explicitly, by Session closure, or by shutdown
Failed, // rejected at start, or a step failed
};
// One streaming or terminal generation event. A missing finish_reason means
// more updates may follow. A present finish_reason marks the last update;
// tokens may be empty when a generation fails or is cancelled. error_message
// is non-empty only when finish_reason is Failed.
struct ET_EXPERIMENTAL GenerationUpdate {
std::vector<Token> tokens;
std::optional<FinishReason> finish_reason;
std::string error_message;
};
// Reports tokens as they are produced, in order, and several at once when a
// speculative executor accepts a run. finish_reason is set on the last call
// and only then, so the final tokens and the reason arrive together and cannot
// be reordered.
//
// Every token reported here is retained by the session. However a generation
// ends, its logical context holds what it held before, plus the delta, plus
// exactly the tokens delivered through this callback: nothing produced but
// undelivered, nothing delivered but dropped. The final delivered token may
// remain pending rather than executor-committed; the next delta carries it.
//
// A delta cancelled before it finished prefilling leaves only its consumed
// prefix in the session. Session::position() reports the executor-committed
// length, excluding any pending generated token.
//
// Admitted generations run callbacks on the engine thread. A request rejected
// before admission completes synchronously on the calling thread, potentially
// before generate_async() returns. Callbacks must not block or wait for work
// serviced by this runner. An exception from a callback is contained and ends
// that generation as Failed.
using GenerationCallback = std::function<void(const GenerationUpdate&)>;
using GenerationInput ET_EXPERIMENTAL =
std::variant<std::vector<Token>, PreparedInputPtr>;
struct ET_EXPERIMENTAL GenConfig {
std::int32_t max_new_tokens = 256;
SamplingParams sampling;
// Ends the generation with FinishReason::StopToken. The matched token is
// included in the terminal GenerationUpdate::tokens and retained by the
// session.
std::vector<Token> stop_tokens;
// Fixed for the generation and installed before its tasks are submitted.
// nullopt requests nondeterministic sampling.
std::optional<std::uint64_t> seed;
// Increasing absolute positions in this prefill, including any pending token.
std::vector<Position> prefill_breakpoints;
};
// Both defined in runner.cpp: callers see neither the lock and flags the first
// holds nor the runner reference the second keeps alive.
struct GenerationHandleState;
struct SessionState;
class RunnerImpl;
// Observes one generation. Output arrives through the callbacks, not here, so
// dropping the handle does not cancel; fire and forget is a valid use.
//
// Copyable and safe from any thread: it holds only shared state, never the
// runner, so it may outlive it.
class ET_EXPERIMENTAL GenerationHandle {
public:
GenerationHandle() = default;
GenerationHandle(const GenerationHandle&) = default;
GenerationHandle& operator=(const GenerationHandle&) = default;
GenerationHandle(GenerationHandle&&) noexcept = default;
GenerationHandle& operator=(GenerationHandle&&) noexcept = default;
// Whether this handle denotes a generation. False for default-constructed
// and moved-from handles.
bool valid() const noexcept;
// Requests cancellation, which lands within one execution step. It does not
// interrupt an in-progress Executor::execute() call, including any work the
// executor performs within that call. A no-op on an invalid handle.
void cancel() const;
// Becomes true after the terminal callback returns or throws, without
// waiting for on_settled to return. False for an invalid handle.
bool done() const;
// Blocks until the generation and its terminal callback have ended. Returns
// immediately if both already have or the handle is invalid. Does not wait
// for on_settled to return. Must not be called from a callback serviced by
// the same runner.
void wait() const;
// The terminal reason once done. nullopt for an invalid or unfinished
// handle.
std::optional<FinishReason> finish_reason() const;
// Diagnostic for a failed generation, when one is available. In particular,
// preserves std::exception::what() when a callback throws. Meaningful only
// when valid() && done(); empty when no diagnostic is available.
std::string error_message() const;
// This generation's timeline and counts, complete once done(). Empty on a
// default-constructed handle.
GenerationMetrics metrics() const;
private:
friend class RunnerImpl;
friend class Session;
explicit GenerationHandle(std::shared_ptr<GenerationHandleState> state)
: state_(std::move(state)) {}
std::shared_ptr<GenerationHandleState> state_;
};
// Sole owner of one open executor session. Move it to transfer ownership.
// Destruction requests an asynchronous close and cancels any active
// generation, so retain it for as long as that generation should run.
//
// The close request never waits for the engine thread. Runner::shutdown()
// remains the deterministic boundary for executor cleanup.
class ET_EXPERIMENTAL Session {
public:
Session();
~Session();
Session(Session&&) noexcept;
Session& operator=(Session&&) noexcept;
Session(const Session&) = delete;
Session& operator=(const Session&) = delete;
// A snapshot of whether this object denotes a logically open session.
// Returns false for default, moved-from, destructing, and shutdown-closed
// sessions. A concurrent close may begin immediately after a true result.
bool valid() const noexcept;
// The absolute position of the next input to the executor. Safe from any
// thread.
//
// If the last callback delivered a token that has not yet been fed back,
// that token is retained internally as pending and will be the next input at
// this position. A subsequent generation prepends it to the caller's delta.
// Without a pending token, the caller's delta begins at this position.
//
// Before each callback, position is updated to reflect all inputs committed
// for the updates delivered so far. It does not include in-flight or
// discarded executor work. Once a handle is done, the value is stable until
// the next generation. Returns 0 for a default or moved-from Session.
Position position() const noexcept;
// An independent, idle session holding committed [0, upto), with no pending
// prediction, generation, or sampling policy. The source may be generating;
// upto is checked against its accepted committed position on the engine
// thread. Speculative or pending tokens beyond that position are excluded.
// The Executor must restore all model state at upto, including recurrent
// and convolution state; some executors can clone only at saved boundaries.
//
// nullopt = invalid/poisoned source or boundary, unavailable retained state,
// unsupported cloning, capacity pressure, or shutdown. An empty prefix may
// be refused. Failure leaves the source unchanged. Once queued, a clone
// survives source destruction queued after it, but shutdown may refuse it.
//
// A model-output callback may enqueue a clone before the next model forward.
// Never wait for the returned future in a callback serviced by this runner.
std::future<std::optional<Session>> clone_async(Position upto) const;
// `delta` is the caller-resolved suffix to append. The session tracks its
// committed position and any pending generated token, so consecutive
// generations continue where prior executor work left them. Retain this
// Session until the asynchronous generation ends;
// destroying it requests close and completes active work as Cancelled.
//
// Opaque backing must pass this Runner's Executor::accepts() check and have
// a stable logical size and layout. A pending prediction is submitted as
// separate raw prefill before the opaque chunks.
//
// The delta must be non-empty and its exclusive end must fit in Position.
// Invalid input and a second concurrent generation end as Failed. A default
// or moved-from Session also completes synchronously as Failed; a retained
// shutdown-closed Session completes synchronously as Cancelled.
//
// Optional on_settled runs exactly once after the handle's final outcome,
// diagnostic, metrics, and done state are published, even if on_update
// throws. It runs on the engine thread or inline for synchronous rejection,
// and may precede generate_async returning. Exceptions are contained without
// changing the outcome; its captures are released after invocation. It must
// not block, invoke user output, destroy Runner, or wait for Runner work.
// This signals generation settlement, not Runner idleness or physical
// session-close completion. Handle wait/done do not wait for it to return.
//
// on_prefill runs at requested breakpoints before later prefill. It may queue
// clones but must not wait for Runner work. Exceptions fail the generation.
GenerationHandle generate_async(
GenerationInput delta,
GenConfig config,
GenerationCallback on_update,
std::function<void()> on_settled = {},
std::function<void(Position)> on_prefill = {}) const;
private:
friend class RunnerImpl;
friend class PrefixCache;
explicit Session(std::unique_ptr<SessionState> state);
std::function<std::future<std::optional<Session>>(Position)>
make_clone_request() const;
std::unique_ptr<SessionState> state_;
};
enum class ET_EXPERIMENTAL InitializationState { Pending, Ready, Failed };
enum class ET_EXPERIMENTAL AcceptanceError { Unavailable, Failed };
using AcceptanceResult ET_EXPERIMENTAL = std::variant<bool, AcceptanceError>;
class ET_EXPERIMENTAL Runner {
public:
// Takes the scheduler, one per runner, which also supplies the prefill chunk
// size prompts are split to. Owned rather than borrowed because a Session
// keeps the runner's internals alive after ~Runner, and the close and cancel
// paths reach the scheduler from there; a caller holding it separately would
// have no way to see that its lifetime had been extended. Must be non-null.
//
// The executor is not owned and must outlive shutdown or destruction on an
// external thread.
Runner(Executor& executor, std::unique_ptr<Scheduler> scheduler);
~Runner();
Runner(const Runner&) = delete;
Runner& operator=(const Runner&) = delete;
// Any thread. Pending until executor initialization returns; Failed if it
// returns false or throws. The result is retained after shutdown, so Ready
// describes successful initialization, not whether work is still accepted.
InitializationState initialization_state() const noexcept;
// Any thread; queued to the engine thread and acked.
//
// nullopt = the executor is at capacity, or the runner is shutting down.
std::future<std::optional<Session>> open_session_async();
// Any thread. Checks metadata compatibility on the initialized engine thread
// without opening or changing sessions. Returns true for accepted input and
// false for rejected or null input. Stopping or failed initialization returns
// AcceptanceError::Unavailable; an exception from accepts() returns
// AcceptanceError::Failed.
// Acceptance reserves no capacity and does not guarantee execution
// success. The command releases its input reference before the future becomes
// ready. Never wait for this future in a callback serviced by this runner.
std::future<AcceptanceResult> accepts_async(PreparedInputPtr input);
// Idempotent. External callers block until the engine is joined, every live
// generation has ended, and every owned session is closed. A generation that
// was still running ends Cancelled; one that had already reached a stop
// token, its token budget, or a failure keeps that reason, its tokens, and
// its error message. Concurrent external callers wait for the same
// completion.
//
// From a GenerationCallback, requests shutdown and returns without waiting;
// a later external call or destructor must join. Do not destroy the Runner
// from its callback.
void shutdown();
// What the engine measured. Read it after shutdown(): the counters are the
// engine thread's, so joining it is what makes them stable and visible. A
// call before then returns a torn snapshot.
EngineMetrics metrics() const;
private:
std::shared_ptr<RunnerImpl> impl_;
};
} // namespace batching
} // namespace llm
} // namespace extension
} // namespace executorch