Repository navigation
Studio images: family batch adapters, measured batching race and per-size CUDA graphs #19837
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
Changes from 1 commit
b24c827
28a9ecb
f437380
58e0db6
6c55fd6
f5b1cec
e8879fb
731542d
a8e5e17
9f0b357
51c1217
bae160e
95de9ec
fe47b92
1a97100
53efa94
8ca902e
3221b2c
92efbc1
705da24
0d7d96d
395de8b
e5b8e38
71e85dc
67a7dcd
0dac8dd
7859edb
9173835
6e6b15b
6332acb
9993ecd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…of its own canvas effective_verdict reduced the live batched graphs to batch sizes, so a batch-2 graph recorded while racing one canvas satisfied a persisted graph-only batch-2 verdict for another canvas, whose requests then ran eager (GraphedForward keys on tensor shapes) with seeded joins enabled. The race now labels every batched graph it records or replays with its canvas token (width, height, guidance, steps, prompt layout) via batch_rows(canvas=...), stores the token on the verdict as graph_canvas, and a graph-only win is admitted only when every step-graph handle holds a batch-size graph carrying that label; otherwise the eager arm's verdict applies (legacy verdicts without the token included). [skip ci]
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -457,6 +457,7 @@ def run_race( | |
| if guidance is not None: | ||
| common["guidance"] = float(guidance) | ||
| handles = tuple(getattr(state, "cuda_graphs", ()) or ()) | ||
| canvas = canvas_token(width, height, guidance, steps, layout) | ||
| report: dict = {"width": int(width), "height": int(height), "guidance": guidance, "sizes": {}} | ||
|
|
||
| class _Aborted(Exception): | ||
|
|
@@ -480,7 +481,7 @@ def batch( | |
| raise _Aborted() | ||
| start = clock() | ||
| with ( | ||
| cuda_graph.batch_rows(len(prompts), capture = capture), | ||
| cuda_graph.batch_rows(len(prompts), capture = capture, canvas = canvas), | ||
| _graphs_bypassed(handles) if eager else _nullctx(), | ||
| # generate() re-arms the graphs before every chunk; this scope keeps them bypassed through it. | ||
| cuda_graph.forced_eager() if eager else _nullctx(), | ||
|
|
@@ -535,6 +536,7 @@ def batch( | |
| arms = verdicts, | ||
| size = size, | ||
| layout = layout_token(layout), | ||
| graph_canvas = canvas, | ||
| t = time.time(), | ||
| ) | ||
| if accuracy: | ||
|
|
@@ -652,6 +654,40 @@ def _neighbour_check(solo, batch, prompts: list, seeds: list, *, eager: bool) -> | |
| } | ||
|
|
||
|
|
||
| def canvas_token(width: Any, height: Any, guidance: Any, steps: Any, layout: Any) -> str: | ||
| """The race's shape identity (canvas, guidance, steps, prompt layout): labels the batched graphs it records.""" | ||
| import json | ||
| return json.dumps( | ||
| { | ||
| "w": int(width), | ||
| "h": int(height), | ||
| "g": None if guidance is None else float(guidance), | ||
| "s": None if steps is None else int(steps), | ||
| "l": layout_token(layout), | ||
| }, | ||
| sort_keys = True, | ||
| ) | ||
|
|
||
|
|
||
| def batched_graph_canvas(state: Any, size: int, canvas: Optional[str]) -> bool: | ||
| """Every step-graph handle holds a batch-``size`` graph labelled with ``canvas`` now (GraphedForward keys on tensor | ||
| shapes: a same-size graph of another canvas replays nothing for this one).""" | ||
| if canvas is None: | ||
| return False | ||
| for handle in getattr(state, "cuda_graphs", ()) or (): | ||
| if not hasattr(handle, "_batched"): | ||
| continue | ||
| batched = getattr(handle, "_batched", None) or {} | ||
| cache = getattr(handle, "cache", None) or {} | ||
| labels = getattr(handle, "_batched_canvas", None) or {} | ||
| if not any( | ||
| k in cache and int(v) == int(size) and canvas in (labels.get(k) or ()) | ||
| for k, v in batched.items() | ||
| ): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def batched_graph_sizes(state: Any) -> Optional[set]: | ||
| """Batch sizes every step-graph handle of this load holds a graph for now; None when no handle constrains it. | ||
|
|
||
|
|
@@ -687,10 +723,11 @@ def effective_verdict( | |
| state: Any, | ||
| layout: Any = None, | ||
| ) -> dict: | ||
| """The verdict as it applies now: one won on the graphed arm holds only while that batch-size graph exists (user | ||
| calls never record one) and for the prompt layout it was raced at (graphs key on tensor shapes, so another T5 | ||
| bucket has no graph at the same canvas); otherwise the size is admitted only if the eager arm passed on its own. | ||
| An unknown ``layout`` cannot establish a matching graph.""" | ||
| """The verdict as it applies now: one won on the graphed arm holds only while a batch-size graph recorded at its | ||
| own canvas exists (user calls never record one; graphs key on tensor shapes, so a same-size graph of another canvas | ||
| serves nothing here) and for the prompt layout it was raced at (another T5 bucket has no graph at the same canvas); | ||
| otherwise the size is admitted only if the eager arm passed on its own. An unknown ``layout`` cannot establish a | ||
| matching graph, nor can a verdict without ``graph_canvas``.""" | ||
| if not isinstance(verdict, dict) or verdict.get("arm") != "batched": | ||
| return verdict | ||
|
Comment on lines
+858
to
+859
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If a size wins on the eager arm at canvas A, then a later race wins on the graph arm at canvas B with the same tensor shapes (for example, the same dimensions and guidance but a different step count), this early return still admits A as eager. The second race has installed a graph that Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6332acb. |
||
| held = batched_graph_sizes(state) | ||
|
|
@@ -699,7 +736,11 @@ def effective_verdict( | |
| same_layout = verdict.get("layout") is not None and layout_token(layout) == verdict.get( | ||
| "layout" | ||
| ) | ||
| if int(size) in held and same_layout: | ||
| if ( | ||
| int(size) in held | ||
| and same_layout | ||
| and batched_graph_canvas(state, size, verdict.get("graph_canvas")) | ||
| ): | ||
| return verdict | ||
| eager = (verdict.get("arms") or {}).get("batched_eager") | ||
| if isinstance(eager, dict): | ||
|
|
||
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.
When a freshly loaded model has
speed_deferred=True, the race's own renders can trigger_engage_deferred_speed()on the thirdgenerate()call, which installs graphs and replacesstate.cuda_graphs. This snapshot remains empty, however, so subsequent graph-backed measurements are recorded aspath="eager", no eager comparison arm is run, and cleanup cannot drop the newly recorded batch graphs. An eager-only G2c PASS can consequently authorize a graph-backed win. Settle the deferred profile before measuring, or restart the race when its execution configuration changes.Useful? React with 👍 / 👎.
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.
Fixed in b8e1016bc2: batch_race now refuses (the endpoint's 409) while the load's speed profile is still deferred, so a race never starts on a load whose graphs can still be installed by its own renders. run_race also re-checks the graph handles and speed profile before saving each size; if they changed mid-race it saves nothing, drops the batch graphs the new handles recorded and refuses. Settling the profile inside the race was not done because that would engage speed outside generate()'s lock. Both tests fail without the fix.