Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion helios/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ impl LiveRequest {
pub fn reused_prompt_tokens(&self) -> u64 {
self.admission_reused_tokens
}
pub(super) fn initial_prefill_tokens(&self) -> usize {
self.prompt_tokens
.saturating_sub(self.admission_reused_tokens as usize)
.max(1)
}
pub fn snapshot_destination(&self) -> SnapshotId {
self.boundary_capture
.map(|c| c.snapshot)
Expand Down Expand Up @@ -2669,6 +2674,32 @@ impl RequestScheduler {
let mut window_snapshots = Vec::new();
for (row, lane) in step.lanes.iter().enumerate() {
let kind = step.lane_kind(row);
// A prefill stopped strictly by an armed anchor cut on a prompt that
// otherwise fit in one eligible prefill graph keeps its first-output
// wait clock across the forced split so its short tail is not re-queued
// behind active decoders at zero waiting age.
let preserve_first_output_wait = kind.is_prefill()
&& self.request(lane.request).is_ok_and(|request| {
let record = &records[lane.request.slot as usize];
let finished = record.status == 3
|| ((record.status >= 2
|| record.generated_tokens == request.max_new_tokens as i64)
&& record.consumed_tokens >= request.prompt_tokens as i64);
let prefill =
(record.consumed_tokens as u64).saturating_sub(request.consumed_tokens);
let output = record.generated_tokens - request.generated_tokens as i64;
record.status != 3
&& !finished
&& prefill > 0
&& output == 0
&& !request.deferred_prefill
&& (record.consumed_tokens as usize) < request.prompt_tokens
&& request.checkpoint_capture.is_some_and(|capture| {
capture.role == RetentionRole::Anchor
&& capture.endpoint == record.consumed_tokens as u64
})
&& self.fits_single_prefill_from(request, request.consumed_tokens as usize)
});
let Ok(request) = self.request_mut(lane.request) else {
continue;
};
Expand Down Expand Up @@ -2757,7 +2788,9 @@ impl RequestScheduler {
};
let output = (record.generated_tokens - request.generated_tokens as i64) as u32;
if record.status != 3 {
request.accounting.credit(prefill, output, now_ns);
request
.accounting
.credit(prefill, output, now_ns, preserve_first_output_wait);
useful_tokens[lane.request.slot as usize] = prefill + u64::from(output);
}
if kind == GraphKind::TerminalPrefill && record.status != 3 {
Expand Down
100 changes: 88 additions & 12 deletions helios/src/scheduler/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl PolicyConfig {
pub struct RequestAccounting {
pub arrival_ns: u64,
pub last_progress_ns: u64,
pub first_output_wait_ns: u64,
pub last_output_ns: Option<u64>,
pub terminal_since_ns: Option<u64>,
pub last_output_fence: Option<Fence>,
Expand All @@ -67,11 +68,20 @@ pub struct RequestAccounting {
}

impl RequestAccounting {
pub(super) fn credit(&mut self, prefill: u64, output: u32, now: u64) {
pub(super) fn credit(
&mut self,
prefill: u64,
output: u32,
now: u64,
preserve_first_output_wait: bool,
) {
self.committed_prefill_tokens += prefill;
self.committed_output_tokens += u64::from(output);
if prefill != 0 || output != 0 {
self.last_progress_ns = now;
if !preserve_first_output_wait {
self.first_output_wait_ns = now;
}
}
}
}
Expand Down Expand Up @@ -254,6 +264,19 @@ impl RequestScheduler {
let request = self.slots[handle.slot as usize].request.as_mut().unwrap();
request.accounting.arrival_ns = arrival_ns;
request.accounting.last_progress_ns = arrival_ns;
request.accounting.first_output_wait_ns = arrival_ns;
Ok(())
}

pub fn set_first_output_wait(&mut self, handle: RequestHandle, wait_ns: u64) -> Result<()> {
let request = self.request(handle)?;
if wait_ns > self.now_ns || request.last_use != Fence(0) {
return Err(
"first-output wait must precede admission and be assigned before dispatch".into(),
);
}
let request = self.slots[handle.slot as usize].request.as_mut().unwrap();
request.accounting.first_output_wait_ns = wait_ns.max(request.accounting.arrival_ns);
Ok(())
}

Expand Down Expand Up @@ -284,6 +307,7 @@ impl RequestScheduler {
RequestAccounting {
arrival_ns: self.now_ns,
last_progress_ns: self.now_ns,
first_output_wait_ns: self.now_ns,
..Default::default()
}
}
Expand Down Expand Up @@ -413,6 +437,32 @@ impl RequestScheduler {
best.map(|(capacity, _)| capacity)
}

/// Whether `[start, request.prompt_tokens)` fits in a single eligible prefill
/// graph when no checkpoint cut is imposed.
pub(super) fn fits_single_prefill_from(&self, request: &LiveRequest, start: usize) -> bool {
if request.prefill_limit() != request.prompt_tokens || start >= request.prompt_tokens {
return false;
}
let remaining = request.prompt_tokens - start;
let terminal_only = self.prefill_requires_terminal_completion_from(request, start, None);
let catalog = if request.prompt_logprobs {
&self.scored_prefill_catalog
} else if terminal_only {
&self.terminal_prefill_catalog
} else {
&self.prefill_catalog
};
catalog.iter().copied().any(|capacity| {
let work = self.prefill_work_from(request, start, capacity, None);
if work != remaining {
return false;
}
let resolved = self.resolve_prefill_kind_from(request, start, capacity, work, None);
!(terminal_only && resolved == GraphKind::Prefill)
&& self.costs.prefill_entry(resolved, capacity).is_some()
})
}

pub fn submission_window_available(&self, duration_ns: u64) -> bool {
self.submissions.len() < 2
&& !self.submissions.values().any(|s| s.requires_feedback)
Expand Down Expand Up @@ -473,10 +523,7 @@ impl RequestScheduler {
}
}
} else if !r.prompt_logprobs && r.scheduled_consumed < r.prompt_tokens as u64 {
let initial_prefill = r
.prompt_tokens
.saturating_sub(r.admission_reused_tokens as usize)
.max(1);
let initial_prefill = r.initial_prefill_tokens();
let post_prefill = (r.max_new_tokens as usize)
.min(self.limits.max_sequence_tokens.saturating_sub(r.prompt_tokens))
.saturating_sub(1);
Expand Down Expand Up @@ -624,7 +671,8 @@ impl RequestScheduler {
.map(|r| self.service_rates(r))
.unwrap_or_default()
});
let references = self.delivery_references(&rates.map(|(_, d)| d));
let (references, incumbent_decode_batch_ms) =
self.delivery_references_and_decode_batch(&rates.map(|(_, d)| d));
let mut scale = 0.0f64
.max(self.policy.output_token_value.ln())
.max(self.policy.decode_age_price.ln())
Expand Down Expand Up @@ -678,6 +726,29 @@ impl RequestScheduler {
.unwrap_or_else(|| r.accounting.last_output_ns.unwrap_or(r.accounting.last_progress_ns));
values[slot].age_ms = self.now_ns.saturating_add(window_ns)
.saturating_sub(last_delivery) as f64 / 1e6;
} else if r.consumed_tokens < r.prompt_tokens as u64
&& r.max_new_tokens > 0
&& self.policy.decode_age_price > 0.0
&& incumbent_decode_batch_ms > 0.0
{
let wait_start = self
.submissions
.values()
.filter(|submission| {
submission.lanes.iter().enumerate().any(|(row, lane)| {
lane.request == r.handle && submission.lane_kind(row).is_prefill()
})
})
.map(|submission| submission.expected_finish_ns)
.max()
.unwrap_or(r.accounting.first_output_wait_ns);
let age_ms = self
.now_ns
.saturating_add(window_ns)
.saturating_sub(wait_start) as f64
/ 1e6;
values[slot].first_output_value =
self.policy.decode_age_price * incumbent_decode_batch_ms * age_ms;
}
scale = scale
.max(token_log[slot])
Expand All @@ -694,6 +765,7 @@ impl RequestScheduler {
value.delivery = (delivery_log[slot] - scale).exp();
value.terminal *= unit;
value.age_price *= unit;
value.first_output_value *= unit;
age_price += value.age_price;
}
(values, age_price, scale)
Expand Down Expand Up @@ -731,8 +803,9 @@ impl RequestScheduler {
}
consider(
ns,
value.prefill(
value.prefill_within(
work,
r.initial_prefill_tokens(),
r.scheduled_consumed + work as u64 == r.prompt_tokens as u64,
),
);
Expand Down Expand Up @@ -783,8 +856,9 @@ impl RequestScheduler {
if work > 0 {
consider(
ns,
value.prefill(
value.prefill_within(
work,
r.initial_prefill_tokens(),
r.scheduled_consumed + work as u64 == r.prompt_tokens as u64,
),
);
Expand Down Expand Up @@ -1220,10 +1294,12 @@ impl RequestScheduler {
value: LaneValue,
) -> f64 {
match kind {
GraphKind::Prefill | GraphKind::TerminalPrefill | GraphKind::ScoredPrefill => value.prefill(
tokens,
r.scheduled_consumed + tokens as u64 == r.prompt_tokens as u64,
),
GraphKind::Prefill | GraphKind::TerminalPrefill | GraphKind::ScoredPrefill => value
.prefill_within(
tokens,
r.initial_prefill_tokens(),
r.scheduled_consumed + tokens as u64 == r.prompt_tokens as u64,
),
GraphKind::Decode | GraphKind::Speculative => value.decode_within(
self.expected_speculative_tokens(r, tokens),
preceding_ns as f64 / 1e6,
Expand Down
Loading
Loading