-
Notifications
You must be signed in to change notification settings - Fork 19
bugfix:DBO ubatch NPUGraph capture passes per-stage metadata to the standard full-graph updater #178
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
Merged
bugfix:DBO ubatch NPUGraph capture passes per-stage metadata to the standard full-graph updater #178
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the AFD plugin project | ||
| """Route vLLM-Ascend MLA graph parameters through AFD forward contexts. | ||
|
|
||
| Upstream source: ``vllm_ascend/attention/mla_v1.py``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| AFD_MLA_GRAPH_PARAMS_KEY = "afd_mla_graph_params" | ||
| _MLA_GRAPH_PATCH_ATTR = "_afd_plugin_mla_graph_patch_state" | ||
|
|
||
|
|
||
| def apply_afd_mla_graph_patch() -> bool: | ||
| """Install the MLA resolver, returning whether it is available.""" | ||
|
|
||
| try: | ||
| from vllm.forward_context import ( | ||
| get_forward_context, | ||
| is_forward_context_available, | ||
| ) | ||
| from vllm_ascend.attention import mla_v1 | ||
| except ImportError: | ||
| return False | ||
|
|
||
| if hasattr(mla_v1, _MLA_GRAPH_PATCH_ATTR): | ||
| return True | ||
|
|
||
| original_get_graph_params = mla_v1.get_graph_params | ||
|
|
||
| # Patch reason: AFD aggregate graph capture owns one GraphParams per ubatch. | ||
| # Patch functionality: resolve graph params from the active AFD forward | ||
| # context while preserving upstream process-global behavior otherwise. | ||
| # Signature: matches upstream; no added parameters. | ||
| def get_graph_params(): | ||
| # ### PATCH START: AFD MLA graph registry | ||
| if is_forward_context_available(): | ||
| forward_context = get_forward_context() | ||
| additional_kwargs = forward_context.additional_kwargs or {} | ||
| graph_params = additional_kwargs.get(AFD_MLA_GRAPH_PARAMS_KEY) | ||
| if graph_params is not None: | ||
| return graph_params | ||
| # ### PATCH END: AFD MLA graph registry | ||
| return original_get_graph_params() | ||
|
|
||
| mla_v1.get_graph_params = get_graph_params | ||
| setattr( | ||
| mla_v1, | ||
| _MLA_GRAPH_PATCH_ATTR, | ||
| original_get_graph_params, | ||
| ) | ||
| return True | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "AFD_MLA_GRAPH_PARAMS_KEY", | ||
| "apply_afd_mla_graph_patch", | ||
| ] |
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,127 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the AFD plugin project | ||
| """Graph-local MLA parameter helpers for Ascend ubatch execution.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
| from contextlib import contextmanager | ||
| from typing import TYPE_CHECKING, TypeVar | ||
|
|
||
| from afd_plugin.compat.patches.npu.mla_graph import ( | ||
| AFD_MLA_GRAPH_PARAMS_KEY, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| import torch | ||
| from vllm.forward_context import ForwardContext | ||
| from vllm_ascend.compilation.acl_graph import GraphParams | ||
|
|
||
| MetadataT = TypeVar("MetadataT") | ||
|
|
||
|
|
||
| def new_mla_graph_params( | ||
|
yujuancao07 marked this conversation as resolved.
|
||
| num_tokens: int, | ||
| workspace: torch.Tensor, | ||
| ) -> GraphParams: | ||
| """Create an empty MLA registry for one token shape and FIA workspace.""" | ||
| # Delay the NPU-only import so this helper remains CPU-import-safe. | ||
| from vllm_ascend.compilation.acl_graph import GraphParams | ||
|
|
||
| return GraphParams( | ||
| events={num_tokens: []}, | ||
| workspaces={num_tokens: workspace}, | ||
| handles={num_tokens: []}, | ||
| attn_params={num_tokens: []}, | ||
| ) | ||
|
|
||
|
|
||
| def merge_mla_graph_params( | ||
| attn_metadata: list[dict[str, MetadataT]], | ||
| graph_params: tuple[GraphParams, GraphParams], | ||
| num_tokens: int, | ||
| ) -> tuple[dict[tuple[str, int], MetadataT], GraphParams]: | ||
| """Validate and merge two stage-local MLA registries for graph replay.""" | ||
| if len(attn_metadata) != 2 or len(graph_params) != 2: | ||
| raise RuntimeError( | ||
| "MLA DBO FULL graph requires exactly two metadata stages; " | ||
| f"got metadata={len(attn_metadata)}, params={len(graph_params)}", | ||
| ) | ||
|
|
||
| layer_keys = tuple(attn_metadata[0]) | ||
| if tuple(attn_metadata[1]) != layer_keys: | ||
| raise RuntimeError("MLA DBO FULL graph layer order differs by stage") | ||
|
|
||
| expected_records = len(layer_keys) | ||
| for stage_index, params in enumerate(graph_params): | ||
| record_counts = ( | ||
| len(params.events[num_tokens]), | ||
| len(params.handles[num_tokens]), | ||
| len(params.attn_params[num_tokens]), | ||
| ) | ||
| if record_counts != ( | ||
| expected_records, | ||
| expected_records, | ||
| expected_records, | ||
| ): | ||
| raise RuntimeError( | ||
| "MLA DBO FULL graph record count mismatch for " | ||
| f"stage {stage_index}: metadata={expected_records}, " | ||
| f"events={record_counts[0]}, handles={record_counts[1]}, " | ||
| f"params={record_counts[2]}", | ||
| ) | ||
|
|
||
| workspace = graph_params[0].workspaces[num_tokens] | ||
| if graph_params[1].workspaces[num_tokens] is not workspace: | ||
| raise RuntimeError( | ||
| "MLA DBO FULL graph requires one shared FIA workspace", | ||
| ) | ||
|
|
||
| merged_metadata: dict[tuple[str, int], MetadataT] = {} | ||
| merged = new_mla_graph_params(num_tokens, workspace) | ||
|
|
||
| # Preserve the updater's layer-major, stage-minor record order. | ||
| for layer_index, layer_key in enumerate(layer_keys): | ||
| for stage_index in range(2): | ||
| params = graph_params[stage_index] | ||
| merged_key = (layer_key, stage_index) | ||
| merged_metadata[merged_key] = attn_metadata[stage_index][layer_key] | ||
| merged.events[num_tokens].append( | ||
| params.events[num_tokens][layer_index], | ||
| ) | ||
| merged.handles[num_tokens].append( | ||
| params.handles[num_tokens][layer_index], | ||
| ) | ||
| merged.attn_params[num_tokens].append( | ||
| params.attn_params[num_tokens][layer_index], | ||
| ) | ||
|
|
||
| return merged_metadata, merged | ||
|
|
||
|
|
||
| @contextmanager | ||
| def override_mla_graph_params( | ||
| forward_context: ForwardContext, | ||
| attn_metadata: dict[tuple[str, int], MetadataT], | ||
| graph_params: GraphParams, | ||
| ) -> Iterator[None]: | ||
| """Temporarily expose merged MLA state to the upstream graph updater.""" | ||
| original_metadata = forward_context.attn_metadata | ||
| original_additional_kwargs = forward_context.additional_kwargs | ||
| # Do not mutate the parent context's shared additional_kwargs mapping. | ||
| temporary_kwargs = dict(original_additional_kwargs or {}) | ||
| temporary_kwargs[AFD_MLA_GRAPH_PARAMS_KEY] = graph_params | ||
| forward_context.attn_metadata = attn_metadata | ||
| forward_context.additional_kwargs = temporary_kwargs | ||
| try: | ||
| yield | ||
| finally: | ||
| forward_context.attn_metadata = original_metadata | ||
| forward_context.additional_kwargs = original_additional_kwargs | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "merge_mla_graph_params", | ||
| "new_mla_graph_params", | ||
| "override_mla_graph_params", | ||
| ] | ||
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.
Uh oh!
There was an error while loading. Please reload this page.