From 14e3902d5136e2fb00ef86c05fdc8d098f736283 Mon Sep 17 00:00:00 2001 From: Alec <35311602+alec-flowers@users.noreply.github.com> Date: Wed, 7 Oct 2026 07:46:44 -0700 Subject: [PATCH] [Frontend][Rust] Add gRPC forbidden token sequences and cache usage (#59837) Signed-off-by: Alec Flowers --- rust/proto/inference.proto | 4 + rust/src/server/src/grpc/convert.rs | 46 +++++++++++- rust/src/server/src/grpc/tests.rs | 2 + .../routes/openai/chat_completions/convert.rs | 1 + .../src/routes/openai/completions/convert.rs | 1 + rust/src/text/src/lower.rs | 75 ++++++++++++++++--- rust/src/text/src/lower/token_ids.rs | 5 ++ rust/src/text/src/request.rs | 3 + 8 files changed, 125 insertions(+), 12 deletions(-) diff --git a/rust/proto/inference.proto b/rust/proto/inference.proto index 2bc35bcb64..8189758212 100644 --- a/rust/proto/inference.proto +++ b/rust/proto/inference.proto @@ -102,6 +102,9 @@ message DecodingParameters { bool json_object = 10; string structural_tag = 11; } + + // Token sequences that must not be generated. Each sequence must be nonempty. + repeated TokenIds bad_words_token_ids = 12; } message StoppingCriteria { @@ -210,6 +213,7 @@ message FinishInfo { google.protobuf.Struct kv_transfer_params = 6; //uint64 seed = 7; google.protobuf.Struct ec_transfer_params = 8; + optional uint32 num_cached_tokens = 9; } // Info for candidate tokens other than the input/sampled diff --git a/rust/src/server/src/grpc/convert.rs b/rust/src/server/src/grpc/convert.rs index d5dd1ae64d..8f2c589f15 100644 --- a/rust/src/server/src/grpc/convert.rs +++ b/rust/src/server/src/grpc/convert.rs @@ -203,6 +203,10 @@ fn build_sampling_params( if !d.allowed_token_ids.is_empty() { params.allowed_token_ids = Some(d.allowed_token_ids.clone()); } + if !d.bad_words_token_ids.is_empty() { + params.bad_words_token_ids = + Some(d.bad_words_token_ids.iter().map(|sequence| sequence.ids.clone()).collect()); + } params.structured_outputs = convert_structured_output(d)?; } @@ -403,6 +407,7 @@ fn to_finish_info(finished: &Finished, token_ids: &[u32]) -> Result>, /// Words to avoid during generation (tokenized to IDs during lowering). pub bad_words: Option>, + /// Pre-tokenized sequences to prohibit, in addition to `bad_words`. + pub bad_words_token_ids: Option>>, /// Specific token IDs for which log probabilities should be returned at /// each position. /// @@ -173,6 +175,7 @@ impl Default for SamplingParams { logit_bias: None, allowed_token_ids: None, bad_words: None, + bad_words_token_ids: None, logprob_token_ids: None, structured_outputs: None, skip_reading_prefix_cache: None,