-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[None][feat] AutoDeploy: Perf improvement for mamba layers #8991
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
nvchenghaoz
merged 8 commits into
NVIDIA:main
from
nv-auto-deploy:chenghao/perf-nemotron-1106
Nov 11, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
350a613
Perf improvement: Minor fixes
nvchenghaoz 45fbb9d
Merge branch 'main' into chenghao/perf-nemotron-1106
suyoggupta 76530a4
Add conv act fusion
nvchenghaoz c63abe0
Merge branch 'main' into chenghao/perf-nemotron-1106
suyoggupta 8eb0c25
fix unit tests
suyoggupta 324181a
fix tests
suyoggupta e833d2f
Address reviewer's comments
nvchenghaoz eb7c92b
Merge branch 'main' into chenghao/perf-nemotron-1106
nvchenghaoz 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
120 changes: 120 additions & 0 deletions
120
tensorrt_llm/_torch/auto_deploy/transform/library/fuse_causal_conv.py
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,120 @@ | ||
| """Fusion transform for fusing activation functions into causal_conv1d operations.""" | ||
|
|
||
| from typing import List, Optional, Tuple | ||
|
|
||
| import torch | ||
| import torch.nn.functional as F | ||
| from torch.fx import GraphModule, Node | ||
|
|
||
| from ...models.factory import ModelFactory | ||
| from ...shim.interface import CachedSequenceInterface | ||
| from ...utils.node_utils import is_op | ||
| from ..interface import BaseTransform, SharedConfig, TransformInfo, TransformRegistry | ||
|
|
||
|
|
||
| def _match_causal_conv_activation_pattern( | ||
| graph: GraphModule, | ||
| target_op, | ||
| ) -> List[Tuple[Node, Node, str]]: | ||
| """ | ||
| Match the causal_conv + activation pattern in the graph. | ||
|
|
||
| The pattern corresponds to: | ||
| conv_out = cuda_cached_causal_conv1d(...) | ||
| out = activation(conv_out) | ||
|
|
||
| Args: | ||
| graph: The graph module to search | ||
| target_op: The target causal conv op to match | ||
|
|
||
| Returns: | ||
| A list of tuples (conv_node, activation_node, activation_name) for each match | ||
| """ | ||
| matches = [] | ||
|
|
||
| for node in graph.nodes: | ||
| if not is_op(node, target_op): | ||
| continue | ||
|
|
||
| # Check if this node has exactly one user and it's an activation | ||
| if len(node.users) != 1: | ||
| continue | ||
|
|
||
| activation_node = list(node.users.keys())[0] | ||
| if activation_node.op != "call_function": | ||
| continue | ||
|
|
||
| # Detect activation type | ||
| activation_name: Optional[str] = None | ||
| if activation_node.target in (torch.ops.aten.silu.default, F.silu): | ||
| activation_name = "silu" | ||
| # Can extend to support more activations here: | ||
| # elif activation_node.target in (torch.ops.aten.gelu.default, F.gelu): | ||
| # activation_name = "gelu" | ||
|
|
||
| if activation_name is not None: | ||
| matches.append((node, activation_node, activation_name)) | ||
|
|
||
| return matches | ||
|
|
||
|
|
||
| @TransformRegistry.register("fuse_causal_conv_activation") | ||
| class FuseCausalConvActivation(BaseTransform): | ||
| """Fuses activation functions into cached CUDA causal_conv1d operations. | ||
|
|
||
| This transform detects patterns like: | ||
| conv_out = cuda_cached_causal_conv1d(...) | ||
| out = silu(conv_out) | ||
|
|
||
| And replaces them with: | ||
| out = cuda_cached_causal_conv1d(..., activation="silu") | ||
|
|
||
| This optimization allows the backend CUDA kernels to fuse the activation, | ||
| reducing memory bandwidth and improving performance. | ||
|
|
||
| Note: This runs AFTER insert_cached_causal_conv, so it operates on the | ||
| cached CUDA operations, not the uncached torch operations. | ||
| """ | ||
|
|
||
| def _apply( | ||
| self, | ||
| gm: GraphModule, | ||
| cm: CachedSequenceInterface, | ||
| factory: ModelFactory, | ||
| shared_config: SharedConfig, | ||
| ) -> Tuple[GraphModule, TransformInfo]: | ||
| graph = gm.graph | ||
|
|
||
| # Step 1: Identify causal_conv + activation pattern | ||
| matches = _match_causal_conv_activation_pattern( | ||
| graph, | ||
| target_op=torch.ops.auto_deploy.cuda_cached_causal_conv1d, | ||
| ) | ||
|
|
||
| # Step 2: Replace matched patterns with fused version | ||
| for conv_node, activation_node, activation_name in matches: | ||
| with graph.inserting_after(conv_node): | ||
| # Create new call with fused activation | ||
| # Replace the last arg (activation=None) with activation_name | ||
| new_args = list(conv_node.args[:-1]) + [activation_name] | ||
| fused_node = graph.call_function( | ||
| torch.ops.auto_deploy.cuda_cached_causal_conv1d, | ||
| args=tuple(new_args), | ||
| ) | ||
|
|
||
| # Replace all uses of activation_node with fused_node | ||
| activation_node.replace_all_uses_with(fused_node) | ||
|
|
||
| # Remove the old nodes | ||
| graph.erase_node(activation_node) | ||
| graph.erase_node(conv_node) | ||
|
|
||
| gm.recompile() | ||
|
|
||
| info = TransformInfo( | ||
| skipped=False, | ||
| num_matches=len(matches), | ||
| is_clean=False, | ||
| has_valid_shapes=False, | ||
| ) | ||
| return gm, info |
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
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.