Repository navigation
fix(ple): add rocm disk wait-sync support #776
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
jason-fxz
merged 3 commits into
FlashML-org:main
from
LZ-QWQ:fix/rocm-ple-wait-sync-review-20261020
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from concurrent.futures import Future, ThreadPoolExecutor | ||
| from contextlib import contextmanager | ||
| from dataclasses import dataclass | ||
| from typing import Sequence | ||
|
|
@@ -202,12 +203,58 @@ def __init__( | |
|
|
||
| self._wait_sync = probe_wait_sync(os.getenv(_SYNC_ENV, "auto"), self._device) | ||
| # one flag for all graphs: the readback event orders a fill after the previous graph, so signals never overlap | ||
| self._flag = alloc_pinned_tensor(1, dtype=torch.int64) | ||
| self._flag.zero_() | ||
| from freetoken.kernel.row_store import alloc_sync_flag | ||
|
|
||
| self._flag = alloc_sync_flag() if self._wait_sync else None | ||
| self._token_readback = alloc_pinned_tensor(max_graph_rows, dtype=torch.int32) | ||
| self._readback_event = torch.cuda.Event() | ||
| sync = "wait-sync" if self._wait_sync else "launch-gating" | ||
| logger.info_rank0(f"PLE disk backend: {self._store.io_backend()}, {sync}") | ||
| self._worker = ( | ||
| ThreadPoolExecutor(max_workers=1, thread_name_prefix="ple-fill") | ||
| if self._wait_sync and torch.version.hip is not None else None | ||
| ) | ||
| self._pending: Future | None = None | ||
| self._failed = False | ||
|
|
||
| def _fill_decode(self, histories: Sequence[Sequence[int]], bs: int) -> None: | ||
| try: | ||
| self._readback_event.synchronize() | ||
| tokens = self._token_readback[:bs].to(torch.int64).tolist() | ||
| runs = [torch.tensor([*history, t], dtype=torch.int64) | ||
| for history, t in zip(histories, tokens)] | ||
| self.fill(runs, graph=True) | ||
| except BaseException: | ||
| from freetoken.kernel.row_store import signal | ||
|
|
||
| # unblock the stream before surfacing; the step's output is discarded | ||
| signal(self._flag) | ||
| raise | ||
|
|
||
| def _start_fill(self, histories: Sequence[Sequence[int]], bs: int) -> None: | ||
| @torch.inference_mode() | ||
| def _complete() -> None: | ||
|
Collaborator
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. This |
||
| self._fill_decode(histories, bs) | ||
|
|
||
| # Like Engram, release HIP's WAIT independently of a queue-blocked graph launch. | ||
| self._pending = self._worker.submit(_complete) | ||
|
|
||
| def _await_pending(self) -> None: | ||
| if self._pending is not None: | ||
| pending, self._pending = self._pending, None | ||
| try: | ||
| pending.result() | ||
| except BaseException: | ||
| self._failed = True | ||
| if self._worker is not None: | ||
| # An interrupted waiter must drain the fill before recording its consumed event. | ||
| self._worker.shutdown(wait=True) | ||
| raise | ||
|
|
||
| def __del__(self) -> None: | ||
| worker = getattr(self, "_worker", None) | ||
| if worker is not None: | ||
| worker.shutdown(wait=False) | ||
|
|
||
| # ---------------- host side (engine thread, before the forward launches) ---------------- | ||
|
|
||
|
|
@@ -237,28 +284,25 @@ def _ple_context(self, ids: torch.Tensor, position: int) -> list[int]: | |
| return [self.image_token_id if t >= MM_PAD_SHIFT_VALUE else t for t in _context(ids, position, self.eos_token_id)] | ||
|
|
||
| def host_fill_batch(self, batch: Batch, use_graph: bool): | ||
| """Stage this batch's rows; returns the post-dispatch fill callable under flag-sync, else None.""" | ||
| """Stage this batch's rows; returns the deferred fill for wait-sync without a worker.""" | ||
| if torch.version.hip is not None: | ||
| self._await_pending() | ||
| if self._failed: | ||
| raise RuntimeError("PLE disk table failed; recreate the engine before dispatch") | ||
| eos = self.eos_token_id | ||
| if batch.is_decode: | ||
| reqs = list(batch.reqs) | ||
| if use_graph and self._wait_sync: | ||
| bs = batch.padded_size | ||
| self._token_readback[:bs].copy_(batch.input_ids, non_blocking=True) | ||
| self._readback_event.record(torch.cuda.current_stream(self._device)) | ||
| histories = [self._ple_context(r.input_ids, r.device_len - 1) for r in reqs] | ||
| if self._worker is not None: | ||
| self._start_fill(histories, bs) | ||
| return None | ||
|
|
||
| def _complete() -> None: | ||
| try: | ||
| self._readback_event.synchronize() | ||
| tokens = self._token_readback[:bs].to(torch.int64).tolist() | ||
| runs = [torch.tensor([*self._ple_context(r.input_ids, r.device_len - 1), t], dtype=torch.int64) | ||
| for r, t in zip(reqs, tokens)] | ||
| self.fill(runs, graph=True) | ||
| except BaseException: | ||
| from freetoken.kernel.row_store import signal | ||
|
|
||
| # unblock the stream before surfacing; the step's output is discarded | ||
| signal(self._flag) | ||
| raise | ||
| self._fill_decode(histories, bs) | ||
|
|
||
| return _complete | ||
| # launch-gating: this D2H is the step's readback and orders the fill after sampling | ||
|
|
@@ -279,8 +323,22 @@ def _complete() -> None: | |
|
|
||
| @contextmanager | ||
| def forward_host_ctx(self, batch: Batch, use_graph: bool): | ||
| """Around one dispatch: stage on enter, run the deferred fill+signal on exit.""" | ||
| """Around one dispatch: stage on enter, finish the fill+signal on exit.""" | ||
| deferred = self.host_fill_batch(batch, use_graph) | ||
| if self._worker is not None: | ||
| try: | ||
| yield | ||
| except BaseException: | ||
| self._failed = True | ||
| raise | ||
| finally: | ||
| try: | ||
| self._await_pending() | ||
| finally: | ||
| # The worker must finish before recording the event it waits on at the next fill. | ||
| if use_graph: | ||
| self._graph_consumed.record(torch.cuda.current_stream(self._device)) | ||
| return | ||
| yield | ||
| # no try/finally: a failed launch leaves no WAIT pending, so the fill must not run | ||
| if deferred is not None: | ||
|
|
||
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
Oops, something went wrong.
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.
Nit: the module docstring (L6-7) still says the captured
lookupWAITs on a pinned flag. With this function it is pinned on CUDA and HIP signal memory on ROCm.