Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

feat(gateway): carry sealed generation GPU time

krisztian-gajdar2a4a3a1bb47e
diff --git a/packages/sie_gateway/src/handlers/proxy.rs b/packages/sie_gateway/src/handlers/proxy.rs
index f0e03519c..2080d7b0f 100644
--- a/packages/sie_gateway/src/handlers/proxy.rs
+++ b/packages/sie_gateway/src/handlers/proxy.rs
@@ -3820,7 +3820,12 @@ pub(crate) async fn run_streaming_generate(
.as_ref()
.map(|g| g.max_new_tokens)
.unwrap_or(512);
- let timeout_config = generation_timeout_config(state, dispatch_model, params, max_new_tokens);
+ let timeout_config = generation_timeout_config(
+ state.model_registry.as_ref(),
+ dispatch_model,
+ params,
+ max_new_tokens,
+ );
let first_chunk_timeout = timeout_config.first_chunk;
let inter_chunk_timeout = timeout_config.inter_chunk;
let effective_overall = timeout_config.overall;
@@ -8857,8 +8862,10 @@ fn env_seconds_or(key: &str, default: f64) -> f64 {
.unwrap_or(default)
}
+/// Effective deadlines for one generation request after applying the shared
+/// environment, request-option, model-profile, and fallback precedence.
#[derive(Debug, Clone, Copy)]
-pub(crate) struct GenerationTimeoutConfig {
+pub struct GenerationTimeoutConfig {
pub first_chunk: Duration,
pub inter_chunk: Duration,
pub overall: Duration,
@@ -8895,8 +8902,10 @@ static TIMEOUT_ENV_OVERRIDES: std::sync::LazyLock<TimeoutEnvOverrides> =
}
});
-pub(crate) fn generation_timeout_config(
- state: &AppState,
+/// Resolve the generation timeout policy used by the HTTP stream driver.
+/// Alternate dispatchers use the same result to bound downstream execution.
+pub fn generation_timeout_config(
+ model_registry: &crate::state::model_registry::ModelRegistry,
model: &str,
params: &publisher::WorkParams,
max_new_tokens: u32,
@@ -8909,8 +8918,7 @@ pub(crate) fn generation_timeout_config(
.and_then(Value::as_str)
.unwrap_or("default");
- let runtime = state
- .model_registry
+ let runtime = model_registry
.get_model_info(model)
.and_then(|entry| entry.profile_configs.get(profile_name).cloned())
.and_then(|profile| profile.adapter_options)
@@ -15020,6 +15028,7 @@ mod tests {
text: "ok".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 1,
@@ -18554,7 +18563,12 @@ mod tests {
Err(QueueParseError::PreBuilt(response)) => response,
Err(_) => panic!("options.{key}={seconds:e} must fail as a prebuilt 400"),
Ok(params) => {
- let config = generation_timeout_config(&state, "m", &params, 8);
+ let config = generation_timeout_config(
+ state.model_registry.as_ref(),
+ "m",
+ &params,
+ 8,
+ );
panic!("options.{key}={seconds:e} was accepted as {config:?}");
}
};
@@ -18581,7 +18595,7 @@ mod tests {
let Ok(params) = work_params_from_json(&body, "generate") else {
panic!("timeouts of {seconds:e} s fit a duration and must be accepted");
};
- let config = generation_timeout_config(&state, "m", &params, 8);
+ let config = generation_timeout_config(state.model_registry.as_ref(), "m", &params, 8);
let start = tokio::time::Instant::now();
for timeout in [config.first_chunk, config.inter_chunk, config.overall] {
assert!(start + timeout > start);
@@ -18626,8 +18640,12 @@ mod tests {
})
.unwrap();
- let config =
- generation_timeout_config(&state, "org/slow", &publisher::WorkParams::default(), 8);
+ let config = generation_timeout_config(
+ state.model_registry.as_ref(),
+ "org/slow",
+ &publisher::WorkParams::default(),
+ 8,
+ );
let start = tokio::time::Instant::now();
for timeout in [config.first_chunk, config.inter_chunk, config.overall] {
assert!(start + timeout > start);
@@ -18655,6 +18673,7 @@ mod tests {
text: "Hello world!".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -23459,6 +23478,7 @@ mod tests {
text: String::new(),
finish_reason: "stop".to_string(),
usage: Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -23516,6 +23536,7 @@ mod tests {
text: String::new(),
finish_reason: "tool_calls".to_string(),
usage: Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 6,
@@ -23585,6 +23606,7 @@ mod tests {
text: String::new(),
finish_reason: "stop".to_string(),
usage: Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 3,
@@ -23969,6 +23991,7 @@ mod tests {
text: "a continuation".to_string(),
finish_reason: "length".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 4,
@@ -24594,6 +24617,7 @@ mod tests {
text: "a joke".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -24628,6 +24652,7 @@ mod tests {
#[test]
fn test_responses_usage_reports_cached_input_tokens() {
let usage = crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: Some(crate::queue::streaming::PromptTokensDetails {
cached_tokens: 4,
@@ -24676,6 +24701,7 @@ mod tests {
text: "Hi there!".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -24768,6 +24794,7 @@ mod tests {
text: "Hi".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 1,
@@ -24807,6 +24834,7 @@ mod tests {
text: "Hi".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 1,
@@ -24841,6 +24869,7 @@ mod tests {
text: String::new(),
finish_reason: "tool_calls".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 7,
@@ -25443,6 +25472,7 @@ mod tests {
text: "Hi".to_string(),
finish_reason: "stop".to_string(),
usage: Some(crate::queue::streaming::UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 1,
diff --git a/packages/sie_gateway/src/handlers/sse.rs b/packages/sie_gateway/src/handlers/sse.rs
index c49f4150d..16f14c8ec 100644
--- a/packages/sie_gateway/src/handlers/sse.rs
+++ b/packages/sie_gateway/src/handlers/sse.rs
@@ -261,7 +261,7 @@ pub async fn build_sse_response(params: SseParams<'_>) -> Response {
.map(|g| g.max_new_tokens)
.unwrap_or(512);
let timeout_config = crate::handlers::proxy::generation_timeout_config(
- state,
+ state.model_registry.as_ref(),
&dispatch_model,
&work_params,
max_new_tokens,
@@ -1815,6 +1815,7 @@ mod tests {
// whose usage is the count-so-far.
finish_reason: "cancelled".to_string(),
usage: Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -2112,6 +2113,7 @@ mod tests {
let mut terminal = _terminal_chunk("error", None);
terminal.seq = 42;
terminal.usage = Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 3,
@@ -2135,6 +2137,7 @@ mod tests {
let mut terminal = _terminal_chunk("error", None);
terminal.seq = 42;
terminal.usage = Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 3,
@@ -2824,6 +2827,7 @@ mod tests {
let chunk = _terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 10,
@@ -3243,6 +3247,7 @@ mod tests {
collector.apply(_terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 2,
@@ -3280,6 +3285,7 @@ mod tests {
let terminal = _terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -3402,6 +3408,7 @@ mod tests {
let terminal = _terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -3449,6 +3456,7 @@ mod tests {
let terminal = _terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -3516,6 +3524,7 @@ mod tests {
let terminal = _terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -3552,6 +3561,7 @@ mod tests {
let terminal = _terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -3686,6 +3696,7 @@ mod tests {
collector.apply(_terminal_chunk(
"stop",
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 1,
diff --git a/packages/sie_gateway/src/queue/streaming.rs b/packages/sie_gateway/src/queue/streaming.rs
index 9dc4c97e0..959dac8df 100644
--- a/packages/sie_gateway/src/queue/streaming.rs
+++ b/packages/sie_gateway/src/queue/streaming.rs
@@ -203,6 +203,10 @@ pub struct UsageBlock {
pub total_tokens: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub images: Option<u32>,
+ /// Host-measured GPU time for a sealed custom-model request. This is
+ /// absent from catalog-worker terminals and older worker versions.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub gpu_second: Option<u64>,
/// OpenAI-compatible prompt-token breakdown. Absent when the worker's
/// engine does not report prefix-cache hits (and from older workers).
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -233,6 +237,8 @@ struct WorkerUsageBlock {
#[serde(default)]
images: Option<u32>,
#[serde(default)]
+ gpu_second: Option<u64>,
+ #[serde(default)]
prompt_tokens_details: Option<PromptTokensDetails>,
}
@@ -248,6 +254,7 @@ impl From<WorkerUsageBlock> for UsageBlock {
completion_tokens: raw.completion_tokens,
total_tokens: raw.total_tokens,
images: raw.images,
+ gpu_second: raw.gpu_second,
prompt_tokens_details,
}
}
@@ -1256,6 +1263,7 @@ mod tests {
finish_reason: if done { Some("stop".to_string()) } else { None },
usage: if done {
Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 5,
@@ -1357,6 +1365,41 @@ mod tests {
assert_eq!(legacy.error.unwrap().validated_retry_after_s(), None);
}
+ #[test]
+ fn test_terminal_usage_carries_optional_gpu_seconds() {
+ let decode = |gpu_second: Option<serde_json::Value>| {
+ let mut usage = serde_json::json!({
+ "prompt_tokens": 3,
+ "completion_tokens": 5,
+ "total_tokens": 8,
+ });
+ if let Some(value) = gpu_second {
+ usage["gpu_second"] = value;
+ }
+ let bytes = rmp_serde::to_vec_named(&usage).expect("encode usage block");
+ rmp_serde::from_slice::<UsageBlock>(&bytes)
+ };
+
+ let measured = decode(Some(serde_json::json!(7))).expect("measured GPU time decodes");
+ assert_eq!(measured.gpu_second, Some(7));
+ assert_eq!(serde_json::to_value(measured).unwrap()["gpu_second"], 7);
+
+ let legacy = decode(None).expect("older usage block decodes");
+ assert_eq!(legacy.gpu_second, None);
+ assert!(serde_json::to_value(legacy)
+ .unwrap()
+ .get("gpu_second")
+ .is_none());
+
+ for invalid in [
+ serde_json::json!(-1),
+ serde_json::json!(1.5),
+ serde_json::json!(true),
+ ] {
+ assert!(decode(Some(invalid)).is_err());
+ }
+ }
+
#[test]
fn test_worker_error_public_contract_fails_closed_for_parameters() {
let public_message = "PUBLIC_MESSAGE";
@@ -1707,6 +1750,7 @@ mod tests {
collector.last_output_at = Some(first + std::time::Duration::from_millis(400));
collector.output_event_count = 2;
collector.final_meta.as_mut().expect("terminal").usage = Some(UsageBlock {
+ gpu_second: None,
images: None,
prompt_tokens_details: None,
prompt_tokens: 1,