|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cstddef> |
| 12 | +#include <cstdint> |
| 13 | +#include <optional> |
| 14 | +#include <string> |
| 15 | +#include <variant> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include <executorch/extension/llm/batching/types.h> |
| 19 | +#include <executorch/extension/llm/runner/multimodal_input.h> |
| 20 | +#include <executorch/runtime/platform/compiler.h> |
| 21 | + |
| 22 | +namespace executorch { |
| 23 | +namespace extension { |
| 24 | +namespace llm { |
| 25 | +namespace serving { |
| 26 | + |
| 27 | +// The full prompt, not a session delta. Preparation encodes each text segment |
| 28 | +// separately and appends ID segments verbatim, in order, without adding |
| 29 | +// BOS/EOS. Segment boundaries therefore matter even between adjacent text |
| 30 | +// segments. Only text and token segments are currently supported; other |
| 31 | +// modalities, encoding failures, and an empty prepared prompt are rejected with |
| 32 | +// InvalidArgument before changing session history. |
| 33 | +struct ET_EXPERIMENTAL PromptInput { |
| 34 | + std::vector<MultimodalInput> segments; |
| 35 | +}; |
| 36 | + |
| 37 | +struct ET_EXPERIMENTAL GenerationOptions { |
| 38 | + // Must be positive when set. Capped by the context remaining after the full |
| 39 | + // prompt; a prompt leaving no generation room is invalid. Unset uses that |
| 40 | + // remaining context, or the service default when the context limit is |
| 41 | + // unknown. |
| 42 | + std::optional<std::int32_t> max_new_tokens; |
| 43 | + // Per-request policy: finite temperature >= 0, finite top_p in (0, 1], |
| 44 | + // and top_k >= 0. Invalid options leave existing session history unchanged. |
| 45 | + batching::SamplingParams sampling; |
| 46 | + // Added to the service's default stop tokens. A matched token is retained in |
| 47 | + // logical session history, but excluded from text and generated_token_ids. |
| 48 | + std::vector<batching::Token> stop_tokens; |
| 49 | + // Non-empty strings matched across decoded pieces. The match and everything |
| 50 | + // after it are hidden; a match invalidates exact token replay and warm reuse. |
| 51 | + std::vector<std::string> stop_strings; |
| 52 | + // Forwarded to the executor for this generation, together with sampling. |
| 53 | + std::optional<std::uint64_t> seed; |
| 54 | +}; |
| 55 | + |
| 56 | +enum class ET_EXPERIMENTAL FinishReason { |
| 57 | + Stop, // EOS, a stop token, or a stop string |
| 58 | + Length, // Generation budget exhausted |
| 59 | + Cancelled, // Request cancellation, session close/reset, or shutdown |
| 60 | + Failed, // See TerminalEvent::error |
| 61 | +}; |
| 62 | + |
| 63 | +enum class ET_EXPERIMENTAL ErrorCode { |
| 64 | + InvalidArgument, |
| 65 | + NotReady, |
| 66 | + SessionNotFound, |
| 67 | + SessionBusy, |
| 68 | + CapacityExceeded, |
| 69 | + Internal, |
| 70 | +}; |
| 71 | + |
| 72 | +struct ET_EXPERIMENTAL ServingError { |
| 73 | + ErrorCode code = ErrorCode::Internal; |
| 74 | + std::string message; |
| 75 | +}; |
| 76 | + |
| 77 | +struct ET_EXPERIMENTAL GenerationStats { |
| 78 | + // Full prepared prompt size, including any reused prefix. |
| 79 | + std::size_t prompt_tokens = 0; |
| 80 | + // Tokens processed by text output, excluding EOS/stop tokens but including |
| 81 | + // the token that completes a string stop. Later discarded tokens do not |
| 82 | + // count. Neither this count nor visible text describes logical session |
| 83 | + // history. |
| 84 | + std::size_t completion_tokens = 0; |
| 85 | + // Committed prompt prefix reused without execution. A pending prediction |
| 86 | + // fed by this request counts as prefilled, not reused. |
| 87 | + std::size_t reused_prompt_tokens = 0; |
| 88 | + // Actually consumed prompt tokens; may be partial on cancellation or failure. |
| 89 | + std::size_t prefilled_prompt_tokens = 0; |
| 90 | + double prefill_ms = 0.0; |
| 91 | + double decode_ms = 0.0; |
| 92 | + double total_ms = 0.0; |
| 93 | + std::string session_reset_reason; |
| 94 | + // Original output IDs counted by completion_tokens, excluding EOS/stop |
| 95 | + // tokens; not a retokenization of visible text or the full logical history |
| 96 | + // suffix. Present only for Stop/Length completion without a matched string |
| 97 | + // stop or rendering error. An empty vector is distinct from absent. |
| 98 | + std::optional<std::vector<batching::Token>> generated_token_ids; |
| 99 | +}; |
| 100 | + |
| 101 | +struct ET_EXPERIMENTAL TextEvent { |
| 102 | + std::string text; |
| 103 | +}; |
| 104 | + |
| 105 | +struct ET_EXPERIMENTAL TerminalEvent { |
| 106 | + FinishReason finish_reason = FinishReason::Stop; |
| 107 | + GenerationStats stats; |
| 108 | + // Present exactly when finish_reason is Failed. |
| 109 | + std::optional<ServingError> error; |
| 110 | +}; |
| 111 | + |
| 112 | +// Each admitted request emits zero or more text events, then one terminal event |
| 113 | +// after final text flushing and session finalization. Synchronous rejection |
| 114 | +// emits no events. If the sink throws, it is not called again, even for the |
| 115 | +// terminal. |
| 116 | +using GenerationEvent ET_EXPERIMENTAL = std::variant<TextEvent, TerminalEvent>; |
| 117 | + |
| 118 | +struct ET_EXPERIMENTAL ServingInfo { |
| 119 | + bool ready = false; |
| 120 | + std::size_t max_context_length = 0; |
| 121 | + std::size_t max_sessions = 0; |
| 122 | + // Logical serving slots, including slots reserved during reset/reopen. |
| 123 | + // This is not a count of physically live executor sessions. |
| 124 | + std::size_t active_sessions = 0; |
| 125 | +}; |
| 126 | + |
| 127 | +} // namespace serving |
| 128 | +} // namespace llm |
| 129 | +} // namespace extension |
| 130 | +} // namespace executorch |
0 commit comments