Skip to content
2 changes: 1 addition & 1 deletion lib/sidecar/trtllm/launch/agg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ while [[ $# -gt 0 ]]; do
echo " DYN_HTTP_PORT Dynamo frontend port (default: 8000)"
echo " DYN_SYSTEM_PORT Dynamo sidecar system port (default: 8081)"
echo " TRTLLM_GRPC_PORT TensorRT-LLM gRPC port (default: 50051)"
echo " TRTLLM_CONTEXT_LENGTH Registered model context length (default: 4096)"
echo " TRTLLM_CONTEXT_LENGTH Model context length; overrides what the engine reports (default: 4096)"
exit 0
;;
*)
Expand Down
9 changes: 6 additions & 3 deletions lib/sidecar/trtllm/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ pub(crate) struct Args {

/// Model maximum sequence length (input + output). Used to register the
/// context length and to derive a default `max_tokens` when a request omits
/// one. TensorRT-LLM's `GetModelInfo` gRPC does not report this on current
/// releases (it returns zero), so supply it here; otherwise requests that
/// omit `max_tokens` are rejected. See the note in `convert.rs`.
/// one. A value supplied here takes precedence over the context length
/// TensorRT-LLM's `GetModelInfo` gRPC reports; that report is used only when
/// this argument is omitted, and a disagreement is logged at WARN. Current
/// releases report nothing usable (zero) or the maximum *input* length, so
/// supply it here; otherwise requests that omit `max_tokens` are rejected.
/// See the note in `convert.rs`.
Comment thread
tanmayv25 marked this conversation as resolved.
Resolved
#[arg(long, env = "TRTLLM_CONTEXT_LENGTH")]
pub context_length: Option<u32>,
}
26 changes: 18 additions & 8 deletions lib/sidecar/trtllm/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,25 @@ impl LLMEngine for TrtllmSidecarEngine {
let client = TrtllmClient::connect(&self.endpoint, self.transport).await?;
let connection_count = client.connection_count();

// Prefer a server-reported context length; fall back to the configured
// `--context-length`. GetModelInfo returns zero on current TRT-LLM
// releases, so the argument is currently the only source. The resolved
// value backs the default-`max_tokens` path in `convert::max_tokens`.
// Some TensorRT-LLM releases report `max_input_len` (or zero) as
// `GetModelInfo.max_seq_len`, so a configured `--context-length` wins.
Comment thread
tanmayv25 marked this conversation as resolved.
Resolved
let mut model = self.model.clone();
match client.model_info().await {
Ok(Some(context_length)) => model.context_length = Some(context_length),
Ok(None) => {}
Err(error) => tracing::warn!(%error, "GetModelInfo failed; using --context-length"),
let reported = match client.model_info().await {
Ok(reported) => reported,
Err(error) => {
tracing::warn!(%error, "GetModelInfo failed; using --context-length");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
None
}
};
match (model.context_length, reported) {
(Some(configured), Some(reported)) if configured != reported => tracing::warn!(
configured_context_length = configured,
engine_context_length = reported,
"--context-length disagrees with the context length TensorRT-LLM reported; \
using the configured --context-length"
),
(None, Some(reported)) => model.context_length = Some(reported),
Comment thread
tanmayv25 marked this conversation as resolved.
Resolved
_ => {}
}
if let Some(context_length) = model.context_length {
let _ = self.context_length.set(context_length);
Comment thread
tanmayv25 marked this conversation as resolved.
Resolved
Expand Down
3 changes: 2 additions & 1 deletion lib/sidecar/trtllm/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub(crate) struct ConfiguredModel {
/// HF repo name or local path used for tokenization and templates.
pub source: String,
/// Maximum sequence length (input + output), from the `--context-length`
/// argument or a server `GetModelInfo` report, if known.
/// argument if it was supplied, else from a server `GetModelInfo` report,
/// if known.
pub context_length: Option<u32>,
}

Expand Down
36 changes: 35 additions & 1 deletion lib/sidecar/trtllm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,22 @@ fn transport(connections: usize) -> GrpcTransportConfig {
}

fn engine(endpoint: &str, connections: usize) -> TrtllmSidecarEngine {
engine_with_context_length(endpoint, connections, None)
}

/// `engine` with an explicit `--context-length`, as `from_parsed` would build it
Comment thread
glamr-agent marked this conversation as resolved.
Resolved
/// when the operator supplied one.
fn engine_with_context_length(
endpoint: &str,
connections: usize,
context_length: Option<u32>,
) -> TrtllmSidecarEngine {
TrtllmSidecarEngine::new(
GrpcEndpoint::parse(endpoint, "--grpc-endpoint").expect("valid test endpoint"),
transport(connections),
ConfiguredModel {
source: "model-source".to_string(),
context_length: None,
context_length,
},
)
}
Expand Down Expand Up @@ -632,6 +642,30 @@ async fn aggregated_generation_streams_delta_then_terminal() {
);
}

#[tokio::test]
async fn configured_context_length_overrides_the_engine_report() {
let server = FakeServer::start(FakeTrtllm::default()).await;
// The fake's GetModelInfo reports 4096, standing in for a release that
// under-reports `max_seq_len`; the operator configured 8192.
let engine = engine_with_context_length(&server.endpoint, 1, Some(8192));
let config = engine.start(0).await.expect("start");
assert_eq!(config.llm.unwrap().context_length, Some(8192));

let mut omits_max_tokens = request();
omits_max_tokens.stop_conditions.max_tokens = None;
let outputs = collect(&engine, omits_max_tokens).await;
assert_eq!(
outputs.last().unwrap().finish_reason,
Some(FinishReason::Stop)
);

let requests = server.service.requests.lock().await;
let sent = requests.first().expect("recorded request");
// 8192 configured minus request()'s three prompt tokens; the reported
// 4096 would give 4093.
assert_eq!(sent.max_tokens, 8189);
}

#[tokio::test]
async fn grpc_request_errors_are_propagated() {
let service = FakeTrtllm::default();
Expand Down
Loading