-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[None][refactor] Centralize FMHA availability and support capability checks #19008
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
yuxianq
merged 2 commits into
NVIDIA:main
from
yuxianq:feat/fmha-capability-check-support
Sep 11, 2026
Merged
Changes from all commits
Commits
Show all changes
2 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
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
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,98 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import cast | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
| import torch | ||
| from fmha_test_utils import FakeAttention | ||
|
|
||
| from tensorrt_llm._torch.attention.backends.fmha.fallback import FallbackFmha | ||
| from tensorrt_llm._torch.attention.backends.fmha.interface import Fmha, FmhaPhase | ||
| from tensorrt_llm._torch.attention.backends.fmha.registry import FMHA_LIBS | ||
| from tensorrt_llm._torch.attention.backends.interface import AttentionForwardArgs | ||
| from tensorrt_llm._torch.attention.backends.trtllm import TrtllmAttention, TrtllmAttentionMetadata | ||
|
|
||
|
|
||
| class _MinimalFmha(Fmha): | ||
| def forward( | ||
| self, | ||
| q: torch.Tensor, | ||
| k: torch.Tensor | None, | ||
| v: torch.Tensor | None, | ||
| metadata: TrtllmAttentionMetadata, | ||
| forward_args: AttentionForwardArgs, | ||
| ) -> None: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("fmha_cls", FMHA_LIBS.values(), ids=FMHA_LIBS.keys()) | ||
| @pytest.mark.parametrize("threshold", [0.0, 0.1]) | ||
| @pytest.mark.parametrize("implementation_available", [False, True]) | ||
| def test_availability_checks_capabilities_before_implementation( | ||
| fmha_cls: type[Fmha], | ||
| threshold: float, | ||
| implementation_available: bool, | ||
| ) -> None: | ||
| attn = cast(TrtllmAttention, FakeAttention()) | ||
| attn.skip_correction_threshold = threshold | ||
| capability_supported = threshold == 0.0 or fmha_cls is FallbackFmha | ||
|
|
||
| with patch.object(fmha_cls, "_is_available", return_value=implementation_available) as hook: | ||
| assert fmha_cls.is_available(attn) is (capability_supported and implementation_available) | ||
| if capability_supported: | ||
| hook.assert_called_once_with(attn) | ||
| else: | ||
| hook.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("phase", [None, FmhaPhase.CONTEXT, FmhaPhase.GENERATION]) | ||
| @pytest.mark.parametrize("supported", [False, True]) | ||
| def test_support_forwards_request_and_phase(phase: FmhaPhase | None, supported: bool) -> None: | ||
| attn = cast(TrtllmAttention, FakeAttention()) | ||
| fmha = _MinimalFmha(attn) | ||
| q, k, v = (torch.empty((2, 4)) for _ in range(3)) | ||
| metadata = Mock(spec=TrtllmAttentionMetadata) | ||
| forward_args = AttentionForwardArgs() | ||
|
|
||
| with patch.object(fmha, "_is_supported", return_value=supported) as hook: | ||
| assert fmha.is_supported(q, k, v, metadata, forward_args, phase=phase) is supported | ||
| hook.assert_called_once_with(q, k, v, metadata, forward_args, phase=phase) | ||
|
|
||
|
|
||
| def test_default_hooks_accept_requests() -> None: | ||
| attn = cast(TrtllmAttention, FakeAttention()) | ||
| fmha = _MinimalFmha(attn) | ||
|
|
||
| assert _MinimalFmha.is_available(attn) | ||
| assert fmha.is_supported( | ||
| torch.empty((2, 4)), None, None, Mock(spec=TrtllmAttentionMetadata), AttentionForwardArgs() | ||
| ) | ||
|
|
||
|
|
||
| def test_inherited_availability_hook_uses_subclass_capabilities() -> None: | ||
| class _SkipCorrectionFmha(_MinimalFmha): | ||
| supports_skip_correction = True | ||
|
|
||
| @classmethod | ||
| def _is_available(cls, attn: TrtllmAttention) -> bool: | ||
| return super()._is_available(attn) | ||
|
|
||
| attn = cast(TrtllmAttention, FakeAttention()) | ||
| attn.skip_correction_threshold = 0.1 | ||
|
|
||
| assert not _MinimalFmha.is_available(attn) | ||
| assert _SkipCorrectionFmha.is_available(attn) |
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.
Uh oh!
There was an error while loading. Please reload this page.