Skip to main content

SequenceState

Struct SequenceState 

Source
pub struct SequenceState {
Show 29 fields pub request_id: RequestId, pub original_request: InferenceRequest, pub input_tokens: Vec<TokenId>, pub generated_tokens: Vec<TokenId>, pub phase: RequestPhase, pub rng: SamplingRng, pub prefill_complete: bool, pub prefill_tokens_processed: usize, pub stream_sender: Option<Sender<Result<StreamChunk>>>, pub response_sender: Option<Sender<Result<InferenceResponse>>>, pub start_time: Instant, pub first_emit_at: Option<Instant>, pub last_emit_at: Option<Instant>, pub emitted_chunks: u32, pub tokens_this_iteration: usize, pub preemption_count: usize, pub structured_output_processor: Option<StructuredOutputProcessor>, pub stop_token_ids: HashSet<u32>, pub model_eos_token_ids: Vec<u32>, pub forbidden_token_ids: HashSet<u32>, pub initial_forbidden_token_ids: HashSet<u32>, pub tokenizer_base_vocab_size: Option<usize>, pub allowed_extended_token_ids: HashSet<u32>, pub stop_text_seqs: Vec<String>, pub argmax_token_mask: Option<TokenSelectionMask>, pub initial_argmax_token_mask: Option<TokenSelectionMask>, pub pending_decoded_utf8_fragment: bool, pub pending_decoded_utf8_bytes: Vec<u8>, pub streamed_text_len: usize, /* private fields */
}
Expand description

State of a running sequence in the continuous batch.

Fields§

§request_id: RequestId§original_request: InferenceRequest

Original request — kept for re-submission after preemption.

§input_tokens: Vec<TokenId>§generated_tokens: Vec<TokenId>§phase: RequestPhase§rng: SamplingRng§prefill_complete: bool§prefill_tokens_processed: usize

Number of prompt tokens already written into the model KV cache by opt-in unified chunked prefill. Zero for the normal full-prefill path.

§stream_sender: Option<Sender<Result<StreamChunk>>>§response_sender: Option<Sender<Result<InferenceResponse>>>§start_time: Instant§first_emit_at: Option<Instant>

Wall-clock Instant at which the first SSE chunk was actually sent to the client stream. Populated lazily by send_stream_update the first time a non-empty delta is emitted (multi-byte UTF-8 buffering can defer that past the first scheduler-completed token). Used to record ferrum.engine.ttft_seconds and as the start point of the TPOT window.

§last_emit_at: Option<Instant>

Wall-clock Instant at the most recent successfully-sent chunk. Used to compute per-token ITL deltas (ferrum.engine.itl_seconds).

§emitted_chunks: u32

Count of stream chunks successfully sent to the client. Lags generated_tokens.len() by the number of tokens currently buffered for a multi-byte UTF-8 sequence (so a Chinese char split across 2 BPE tokens emits once, increments the count by 1).

§tokens_this_iteration: usize§preemption_count: usize

Number of times this request has been preempted.

§structured_output_processor: Option<StructuredOutputProcessor>

Tokenizer-aware hard grammar for json_object and strict schema.

§stop_token_ids: HashSet<u32>

Single-token stop ids: model’s EOS + any stop_sequences that encode to exactly one token. Checked against the last generated token each step — replaces the old “token id near top of vocab = EOS” placeholder. Built from tokenizer.eos_token, a common-EOS fallback list (</s>, <|im_end|>, <|endoftext|>, <|eot_id|>), and one-token encodings of sampling_params.stop_sequences.

§model_eos_token_ids: Vec<u32>

Model-owned EOS ids, kept separate from user stop conditions so a response-completion boundary can delay only model termination.

§forbidden_token_ids: HashSet<u32>

Token IDs that should never be sampled as normal output. Used for tokenizer/model vocab holes such as Qwen3’s reserved tail IDs and literal <unk / <unk> pieces.

§initial_forbidden_token_ids: HashSet<u32>

Token IDs masked only before the first generated token.

§tokenizer_base_vocab_size: Option<usize>

Base tokenizer vocabulary size. IDs above this are allowed only when they are explicitly whitelisted in allowed_extended_token_ids.

§allowed_extended_token_ids: HashSet<u32>§stop_text_seqs: Vec<String>

Multi-token text stop sequences (stop_sequences entries that don’t resolve to a single token). Checked via accumulated decoded text.

§argmax_token_mask: Option<TokenSelectionMask>

Base token-validity mask for model-side greedy argmax.

§initial_argmax_token_mask: Option<TokenSelectionMask>

First-token variant that also applies initial_forbidden_token_ids.

§pending_decoded_utf8_fragment: bool

A byte-level token may decode to a trailing replacement character until a later token supplies the remaining UTF-8 bytes. The next step needs full logits so the engine can reject candidates that would flush that incomplete fragment into user-visible output.

§pending_decoded_utf8_bytes: Vec<u8>

Raw byte-level tokenizer suffix that is a syntactically valid but incomplete UTF-8 scalar. Unlike a lossy decoded string, this preserves whether a continuation token can actually complete the pending scalar.

§streamed_text_len: usize

Bytes of decoded generated_tokens already flushed via the stream channel. Used by send_stream_update to compute per-call delta from the full-history decode, so multi-byte UTF-8 sequences (Chinese chars, emoji) that span several BPE tokens don’t get rendered as \u{FFFD} replacement chars when decoded one token at a time.

Implementations§

Source§

impl SequenceState

Source

pub fn new(request: InferenceRequest, input_tokens: Vec<TokenId>) -> Self

Source

pub fn new_with_tokenizer( request: InferenceRequest, input_tokens: Vec<TokenId>, tokenizer: Option<Arc<dyn Tokenizer + Send + Sync>>, ) -> Self

Build sequence state, optionally wiring a tokenizer for constrained decoding. Test-only direct constructors build a local grammar factory; product entrypoints use the fallible shared-factory constructor below.

Source

pub fn new_with_tokenizer_and_model_vocab_size( request: InferenceRequest, input_tokens: Vec<TokenId>, tokenizer: Option<Arc<dyn Tokenizer + Send + Sync>>, model_vocab_size: Option<usize>, ) -> Self

Source

pub fn try_new_with_tokenizer_model_vocab_and_structured_factory( request: InferenceRequest, input_tokens: Vec<TokenId>, tokenizer: Option<Arc<dyn Tokenizer + Send + Sync>>, model_vocab_size: Option<usize>, shared_structured_factory: Option<&StructuredOutputFactory>, ) -> Result<Self>

Source

pub fn total_tokens(&self) -> usize

Source

pub fn sampling_params(&self) -> &SamplingParams

Original immutable sampling parameters used to prepare this request.

Source

pub fn prefill_context_tokens(&self) -> Vec<TokenId>

Source

pub fn prefill_context_len(&self) -> usize

Source

pub fn model_decode_metadata(&self) -> HashMap<String, Value>

Source

pub fn model_decode_logits_policy(&self) -> LogitsReturnPolicy

Source

pub fn validate_and_commit_model_greedy_argmax_token( &mut self, tokenizer: Option<&(dyn Tokenizer + Send + Sync)>, token: TokenId, ) -> Result<()>

Source

pub fn requires_engine_full_logits_for_sampling(&self) -> bool

Source

pub fn has_structured_output_constraint(&self) -> bool

Source

pub fn requires_full_logits_for_sampling(&self) -> bool

Source

pub fn reset_guided_processors(&mut self) -> Result<()>

Source

pub fn stop_reason( &self, tokenizer: Option<&(dyn Tokenizer + Send + Sync)>, ) -> Option<FinishReason>

Return the reason this sequence should stop, if any.

Checks: (1) last generated token is in the resolved stop_token_ids set (model EOS + any single-token stop_sequences), (2) decoded text contains a multi-token user stop sequence, (3) max-tokens budget is exhausted. Text-stop decoding only runs for requests that supplied a multi-token stop string, so the common EOS path stays cheap.

Source

pub fn should_stop(&self) -> bool

Cheap stop check for tests and callers that do not have tokenizer access. Engine hot paths use stop_reason through EngineInner.

Source

pub fn sample_and_commit_with_processors( &mut self, logits: &mut [f32], ) -> Result<TokenId>

Sample and commit the next token with the full processor chain.

Source

pub fn sample_and_commit_with_processors_and_tokenizer( &mut self, logits: &mut [f32], tokenizer: Option<&(dyn Tokenizer + Send + Sync)>, ) -> Result<TokenId>

Trait Implementations§

Source§

impl Debug for SequenceState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for SequenceState

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more