Repository navigation
prefill: pipeline a prompt's chunks over the parts of a --device split #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Vasyl-Trefilov
merged 6 commits into
RustingStudio:main
from
SubgridAI:pipelined-prefill
Dec 6, 2026
+345
−10
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d4c572
prefill: pipeline the chunks of a prompt over the parts of a --device…
SubgridAI 8c5c969
STRATA_PF_PROF=1: per-part prefill component timing
SubgridAI 0682543
prefill pipeline: hand the rows over in reused portable pinned buffer…
SubgridAI c6e55e3
pf_prof: report each chunk's gap and lead (setup, embed)
SubgridAI 05afbc7
prefill: embed the head part's chunk on the rayon pool
SubgridAI 3ad0ff0
prefill pipeline: keep the parts attached on an allocation error or a…
SubgridAI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| //! `STRATA_PF_PROF=1`: per-part prefill timing for profiling builds. Each `mark` waits for every stream of the part's | ||
| //! GPU and books the wall time since the previous mark to one component, so the components add up to the part's | ||
| //! chunk time (the waits remove the overlap inside a part, not between parts). One line per part per chunk. | ||
| //! Off (the default) it costs one cached env check per call. | ||
| use std::cell::Cell; | ||
| use std::sync::LazyLock; | ||
| use std::time::Instant; | ||
|
|
||
| static ON: LazyLock<bool> = LazyLock::new(|| std::env::var("STRATA_PF_PROF").is_ok_and(|v| !v.is_empty() && v != "0")); | ||
|
|
||
| pub fn on() -> bool { *ON } | ||
|
|
||
| /// Whether this thread is inside a profiled prefill chunk (decode may be under graph capture: no waits there). | ||
| pub fn active() -> bool { ACTIVE.with(|c| c.get()) } | ||
|
|
||
| thread_local! { | ||
| static ACTIVE: Cell<bool> = const { Cell::new(false) }; | ||
| static SCORE: Cell<f64> = const { Cell::new(0.0) }; | ||
| static WAIT_IN: Cell<f64> = const { Cell::new(0.0) }; | ||
| static WAIT_OUT: Cell<f64> = const { Cell::new(0.0) }; | ||
| /// When this thread printed its last chunk line: the time from there to the next chunk's start is that chunk's | ||
| /// `gap` (work outside the profiled window, such as the head's embedding, plus the pipeline waits). | ||
| static LAST: Cell<Option<Instant>> = const { Cell::new(None) }; | ||
| /// This chunk's work before its window: setup (dflash commit, prefetch start) and the input embedding. | ||
| static LEAD: Cell<(f64, f64)> = const { Cell::new((0.0, 0.0)) }; | ||
| } | ||
|
|
||
| pub fn lead(setup: f64, embed: f64) { LEAD.with(|c| c.set((setup, embed))); } | ||
|
|
||
| /// `qsa_score` time inside `qsa_select` (the CUDA backend books it here). | ||
| pub fn add_score(s: f64) { SCORE.with(|c| c.set(c.get() + s)); } | ||
| /// Pipeline waits of this thread: for the part before (`recv`) and for the part after (`send`). | ||
| pub fn add_wait(recv: f64, send: f64) { | ||
| WAIT_IN.with(|c| c.set(c.get() + recv)); | ||
| WAIT_OUT.with(|c| c.set(c.get() + send)); | ||
| } | ||
|
|
||
| pub const IN: usize = 0; | ||
| pub const PRE: usize = 1; | ||
| pub const DELTA: usize = 2; | ||
| pub const ATTN_PROJ: usize = 3; | ||
| pub const QSA_IDX: usize = 4; | ||
| pub const QSA_SEL: usize = 5; | ||
| pub const ATTN: usize = 6; | ||
| pub const POST: usize = 7; | ||
| pub const MOE: usize = 8; | ||
| pub const TAIL: usize = 9; | ||
| pub const OUT: usize = 10; | ||
| const N: usize = 11; | ||
| const NAMES: [&str; N] = ["in", "pre", "delta", "attn_proj", "qsa_idx", "qsa_sel", "attn", "post", "moe", "tail", "out"]; | ||
|
|
||
| pub struct Pp { | ||
| on: bool, | ||
| start: Instant, | ||
| last: Instant, | ||
| gap: f64, | ||
| acc: [f64; N], | ||
| reported: Cell<bool>, | ||
| } | ||
|
|
||
| impl Drop for Pp { | ||
| fn drop(&mut self) { | ||
| if !self.on { return; } | ||
| ACTIVE.with(|c| c.set(false)); | ||
| // A chunk that failed before `report`: its score and waits must not land on the next chunk's line. | ||
| if !self.reported.get() { | ||
| SCORE.with(|c| c.set(0.0)); | ||
| WAIT_IN.with(|c| c.set(0.0)); | ||
| WAIT_OUT.with(|c| c.set(0.0)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Pp { | ||
| pub fn new(g: &dyn crate::backend::Backend) -> anyhow::Result<Self> { | ||
| let on = on(); | ||
| if on { | ||
| g.sync_freed()?; | ||
| SCORE.with(|c| c.set(0.0)); | ||
| ACTIVE.with(|c| c.set(true)); | ||
| } | ||
| let now = Instant::now(); | ||
| let gap = LAST.with(|c| c.get()).map_or(0.0, |l| (now - l).as_secs_f64()); | ||
| Ok(Pp { on, start: now, last: now, gap, acc: [0.0; N], reported: Cell::new(false) }) | ||
| } | ||
|
|
||
| pub fn mark(&mut self, g: &dyn crate::backend::Backend, cat: usize) -> anyhow::Result<()> { | ||
| if !self.on { return Ok(()); } | ||
| g.sync_freed()?; | ||
| let now = Instant::now(); | ||
| self.acc[cat] += (now - self.last).as_secs_f64(); | ||
| self.last = now; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Print the chunk's line: part (first layer), chunk start position, tokens, total and each component in ms. | ||
| pub fn report(&self, first: usize, pos: usize, t: usize) { | ||
| if !self.on { return; } | ||
| self.reported.set(true); | ||
| let score = SCORE.with(|c| c.replace(0.0)); | ||
| let (wi, wo) = (WAIT_IN.with(|c| c.replace(0.0)), WAIT_OUT.with(|c| c.replace(0.0))); | ||
| let mut s = format!("pf_prof part L{first} pos {pos} t {t} total {:.1}", (self.last - self.start).as_secs_f64() * 1e3); | ||
| for (i, name) in NAMES.iter().enumerate() { | ||
| s += &format!(" {name} {:.1}", self.acc[i] * 1e3); | ||
| if i == QSA_SEL { s += &format!(" (score {:.1})", score * 1e3); } | ||
| } | ||
| s += &format!(" | wait_prev {:.1} wait_next {:.1} gap {:.1} setup {:.1} embed {:.1}", wi * 1e3, wo * 1e3, self.gap * 1e3, | ||
| LEAD.with(|c| c.get()).0 * 1e3, LEAD.with(|c| c.get()).1 * 1e3); | ||
| crate::say!("{s}"); | ||
| LAST.with(|c| c.set(Some(Instant::now()))); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Correctness · 4 of 4
Pp error paths skip report, leaving WAIT_IN/WAIT_OUT stale for next chunk Pp::new resets SCORE but not WAIT_IN/WAIT_OUT; report() is the only place that clears them. Any ? error inside prefill_chunk_to drops Pp without report, so the next chunk's line includes the failed chunk's pipeline waits, and LAST is not updated so gap is also wrong.
Prompt for your AI agent
Reply
/dismiss(optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 of 4 (Medium): Pp error path leaves WAIT_IN/WAIT_OUT stale — FIXED
Fixed. If a chunk drops its
Ppwithout reporting, it clears its score and pipeline waits, so they no longer land on the next chunk's line. This only affects theSTRATA_PF_PROF=1output.