|
| 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/runtime/platform/compiler.h> |
| 20 | + |
| 21 | +namespace executorch { |
| 22 | +namespace extension { |
| 23 | +namespace llm { |
| 24 | +namespace serving { |
| 25 | + |
| 26 | +struct ET_EXPERIMENTAL TextSegment { |
| 27 | + std::string text; |
| 28 | +}; |
| 29 | + |
| 30 | +struct ET_EXPERIMENTAL TokenIdsSegment { |
| 31 | + std::vector<batching::Token> ids; |
| 32 | +}; |
| 33 | + |
| 34 | +// Extend this variant with typed segments when another modality is supported. |
| 35 | +using PromptSegment ET_EXPERIMENTAL = |
| 36 | + std::variant<TextSegment, TokenIdsSegment>; |
| 37 | + |
| 38 | +// The full prompt, not a session delta. Preparation encodes each text segment |
| 39 | +// separately and appends ID segments verbatim, in order, without adding |
| 40 | +// BOS/EOS. Segment boundaries therefore matter even between adjacent text |
| 41 | +// segments. |
| 42 | +struct ET_EXPERIMENTAL PromptInput { |
| 43 | + std::vector<PromptSegment> segments; |
| 44 | +}; |
| 45 | + |
| 46 | +struct ET_EXPERIMENTAL GenerationOptions { |
| 47 | + // Unset uses the available context, or the service default if unknown. |
| 48 | + std::optional<std::int32_t> max_new_tokens; |
| 49 | + batching::SamplingParams sampling; |
| 50 | + std::vector<batching::Token> stop_tokens; |
| 51 | + std::vector<std::string> stop_strings; |
| 52 | + std::optional<std::uint64_t> seed; |
| 53 | +}; |
| 54 | + |
| 55 | +enum class ET_EXPERIMENTAL FinishReason { |
| 56 | + Stop, // EOS, a stop token, or a stop string |
| 57 | + Length, |
| 58 | + Cancelled, |
| 59 | + Failed, |
| 60 | +}; |
| 61 | + |
| 62 | +enum class ET_EXPERIMENTAL ErrorCode { |
| 63 | + InvalidArgument, |
| 64 | + NotReady, |
| 65 | + SessionNotFound, |
| 66 | + SessionBusy, |
| 67 | + CapacityExceeded, |
| 68 | + Internal, |
| 69 | +}; |
| 70 | + |
| 71 | +struct ET_EXPERIMENTAL ServingError { |
| 72 | + ErrorCode code = ErrorCode::Internal; |
| 73 | + std::string message; |
| 74 | +}; |
| 75 | + |
| 76 | +struct ET_EXPERIMENTAL GenerationStats { |
| 77 | + std::size_t prompt_tokens = 0; |
| 78 | + // Non-terminal generated tokens, including tokens hidden by a string stop. |
| 79 | + // Neither this count nor visible text describes the session's logical |
| 80 | + // history. |
| 81 | + std::size_t completion_tokens = 0; |
| 82 | + std::size_t reused_prompt_tokens = 0; |
| 83 | + // Actually consumed prompt tokens; may be partial on cancellation or failure. |
| 84 | + std::size_t prefilled_prompt_tokens = 0; |
| 85 | + double prefill_ms = 0.0; |
| 86 | + double decode_ms = 0.0; |
| 87 | + double total_ms = 0.0; |
| 88 | + std::string session_reset_reason; |
| 89 | + // Absent when output trimming or cancellation makes exact replay unsafe. |
| 90 | + std::optional<std::vector<batching::Token>> generated_token_ids; |
| 91 | +}; |
| 92 | + |
| 93 | +struct ET_EXPERIMENTAL TextEvent { |
| 94 | + std::string text; |
| 95 | +}; |
| 96 | + |
| 97 | +struct ET_EXPERIMENTAL TerminalEvent { |
| 98 | + FinishReason finish_reason = FinishReason::Stop; |
| 99 | + GenerationStats stats; |
| 100 | + // Present exactly when finish_reason is Failed. |
| 101 | + std::optional<ServingError> error; |
| 102 | +}; |
| 103 | + |
| 104 | +// Per-request order: zero or more text events, then exactly one terminal event. |
| 105 | +using GenerationEvent ET_EXPERIMENTAL = std::variant<TextEvent, TerminalEvent>; |
| 106 | + |
| 107 | +struct ET_EXPERIMENTAL ServingInfo { |
| 108 | + bool ready = false; |
| 109 | + std::size_t max_context_length = 0; |
| 110 | + std::size_t max_sessions = 0; |
| 111 | + // Logical serving slots, including slots reserved during reset/reopen. |
| 112 | + // This is not a count of physically live executor sessions. |
| 113 | + std::size_t active_sessions = 0; |
| 114 | +}; |
| 115 | + |
| 116 | +} // namespace serving |
| 117 | +} // namespace llm |
| 118 | +} // namespace extension |
| 119 | +} // namespace executorch |
0 commit comments