A prefill engine gives every step a time budget and chooses each request's chunk so that the predicted step time uses the budget exactly. The prediction comes from a fitted quadratic cost model. Implement the fit and the scheduler.
fit_cost_model(samples)
samples is a list of (q, c, t_ns): q new tokens processed with a cached prefix of c tokens took t_ns nanoseconds. Fit the model (no intercept) by least squares and return (a, b, d) rounded to 6 decimals.chunk_for(remaining_ns, c, coeffs, cap=32768)
q such that the model's cost of q tokens with prefix c does not exceed remaining_ns, capped at cap. Return 0 if remaining_ns <= 0. If a == 0 the model is linear in q.plan_step(queue, budgets, coeffs, cap=32768)
queue is this rank's FIFO: a list of (tokens_left, prefix_len). budgets holds the remaining budgets of every rank in the DP group, in nanoseconds; the step plans against their minimum, as the group's all-reduce MIN would.min(chunk_for(remaining, prefix_len), tokens_left) tokens; stop at the first request that would get 0. Subtract the model cost of each chunk from the remaining budget.{"chunks": [(queue_index, q), ...], "used_ns": int, "left_ns": int} with both integers rounded.