-
- Notifications
You must be signed in to change notification settings - Fork 11.2k
[Rocm][torch.compile] Adding layernorm + fp8 block quant and silu + fp8 block quant for Aiter #25693
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
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.
Code Review
This pull request introduces new fusion passes for ROCm AITer, specifically for layernorm + fp8 block quant and silu + fp8 block quant. This is achieved by adding a new pattern AiterSiluMulFp8BlockQuantPattern and registering a new custom operator. Additionally, the changes in fp8_utils.py extend AITer support to non-MI300 series GPUs by providing a Triton-based fallback, which is a great enhancement.
My main feedback is on a performance concern in fp8_utils.py where an import is performed inside a performance-critical function. I've suggested a refactoring to move the import to the module level to avoid repeated overhead.
| # MI300's fp8nuz should be enough to detect if we call ck vs triton | ||
| if current_platform.is_fp8_fnuz(): | ||
| from aiter import gemm_a8w8_blockscale | ||
| else: | ||
| from aiter.ops.triton.gemm_a8w8_blockscale import gemm_a8w8_blockscale | ||
| return gemm_a8w8_blockscale(A, B, As, Bs, dtype=output_dtype) |
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.
Importing inside a function that is on a hot path, like this custom op implementation, can introduce performance overhead. It's best practice to move imports to the module level to ensure they are only executed once.
I'd recommend defining a module-level variable that holds the correct gemm_a8w8_blockscale function based on the platform, and then using that variable within this function. This avoids repeated import lookups.
For example, you could add the following logic at the module level (e.g., near the top of the file):
_gemm_a8w8_blockscale = None if current_platform.is_rocm(): try: # MI300's fp8nuz should be enough to detect if we call ck vs triton if current_platform.is_fp8_fnuz(): from aiter import gemm_a8w8_blockscale else: from aiter.ops.triton.gemm_a8w8_blockscale import gemm_a8w8_blockscale _gemm_a8w8_blockscale = gemm_a8w8_blockscale except ImportError: # aiter is not installed, which is fine. # The error will be raised when the op is actually used. passAnd then this function's body can be simplified as suggested.
| # MI300's fp8nuz should be enough to detect if we call ck vs triton | |
| if current_platform.is_fp8_fnuz(): | |
| from aiter import gemm_a8w8_blockscale | |
| else: | |
| from aiter.ops.triton.gemm_a8w8_blockscale import gemm_a8w8_blockscale | |
| return gemm_a8w8_blockscale(A, B, As, Bs, dtype=output_dtype) | |
| if _gemm_a8w8_blockscale is None: | |
| raise ImportError( | |
| "Aiter backend for gemm_a8w8_blockscale not available. " | |
| "Please install aiter.") | |
| return _gemm_a8w8_blockscale(A, B, As, Bs, dtype=output_dtype) |
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.
Let's do this dispatch outside yeah
Signed-off-by: Micah Williamson <micah.williamson@amd.com>
| I'm currently overhauling custom op matching in #24604. We also recently added a torch implementation of group quant, could you compare its performance with AITER? Also could you compare the perf of the fused AITER kernel to the fused torch.compile kernel for rmsnorm+quant. Happy to help out with instructions, but overall:
|
| SiluMulFp8StaticQuantPattern, | ||
| SiluMulNvfp4QuantPattern) | ||
| SiluMulNvfp4QuantPattern, | ||
| AiterSiluMulFp8BlockQuantPattern) |
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.
This symbol definition is conditional on is_rocm_aiter_linear_enabled():
Any run will fail here if not enabled.
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.
Should be fixed now cd059b9
| return x_fp8, out_bs | ||
| | ||
| direct_register_custom_op( | ||
| op_name="rocm_aiter_act_mul_and_fp8_group_quant", |
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.
Can you check if the latest aiter allows you to skip direct register custom ops? I remember most ops now should be able to work without calling direct_register_custom_ops on vLLM side as it is done in AITER repository. Moreover, removing the direct_register_custom_ops wrappers can reduce additional CPU overhead. Doing direct_register_custom_ops can be costly in terms of overhead.
Please take a look at the benchmarking results in this PR ROCm#717 (the second and third case) where it shows that removing the direct_register_custom_ops on vLLM side improves the perf.
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.
Hey, thanks for the feedback. Is there a version of aiter which has aiter.ops.triton.fused_fp8_quant and also has these direct_register_custom_ops that you mentioned? I wasn't able to figure out how to call act_mul_and_fp8_group_quant without calling direct_register_custom_op first. Would be happy to investigate further if you can point me in the right direction, otherwise I think we can always come back and get rid of these direct_register_custom_op calls if needed.
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.
We can come back to this in later PR as the 355_wip aiter commit does not have that feature.
Signed-off-by: Micah Williamson <micah.williamson@amd.com>
Signed-off-by: Micah Williamson <micah.williamson@amd.com>
| This pull request has merge conflicts that must be resolved before it can be |
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
ProExpertProg left a comment
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.
Will take a look sometime next week, just placing a temp hold while #24604 gets merged
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: Micah Williamson <micah.williamson@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: Micah Williamson <micah.williamson@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
ProExpertProg left a comment
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.
I think we should add the AITER ops to MatcherRMSNorm/MatcherQuantFP8/MatcherSiluMul/... instead of creating separate patterns for the AITER ops, so that we don't need to duplicate these for every pass (think allreduce-rms-quant fusion, all of the rope fusions, etc.)
vllm/compilation/matcher_utils.py Outdated
| def empty_bf16(self, *args, **kws): | ||
| return torch.empty(*args, dtype=torch.bfloat16, device=self.device, **kws) |
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.
I think this one should just use empty as that uses the model dtype
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.
Done
vllm/compilation/fusion.py Outdated
| if current_platform.is_rocm() and envs.VLLM_ROCM_USE_AITER: | ||
| from .matcher_utils import MatcherAiterFusedMulAdd | ||
| | ||
| class AiterMulAddFusionPattern: |
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.
Please add a comment describing what kind of fusion is done in this pass?
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.
pass removed.
vllm/compilation/fusion.py Outdated
| return | ||
| | ||
| def pattern(x: torch.Tensor, a: torch.Tensor, b: torch.Tensor): | ||
| mul_add = self.fused_mul_add_matcher.forward_native(x, a, b) |
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.
This is abusing the matcher abstraction - it's meant to be a reusable matcher for a simple op and not represent fused/unfused impls
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.
pass removed.
vllm/compilation/fusion.py Outdated
| ): | ||
| return self.fused_mul_add_matcher.forward_custom(x, a, b) | ||
| else: | ||
| return self.fused_mul_add_matcher.forward_native(x, a, b) |
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.
Why is this conditional inside the replacement? The tracing will make this always pick a single branch depending on the graph inputs. I think it would be clearer to add an extra_check parameter to the register_replacement function
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.
pass removed.
| # MI300's fp8nuz should be enough to detect if we call ck vs triton | ||
| if current_platform.is_fp8_fnuz(): | ||
| from aiter import gemm_a8w8_blockscale | ||
| else: | ||
| from aiter.ops.triton.gemm_a8w8_blockscale import gemm_a8w8_blockscale | ||
| return gemm_a8w8_blockscale(A, B, As, Bs, dtype=output_dtype) |
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.
Let's do this dispatch outside yeah
vllm/compilation/matcher_utils.py Outdated
| return SiluAndMul.forward_native(x) | ||
| | ||
| | ||
| if current_platform.is_rocm() and envs.VLLM_ROCM_USE_AITER: |
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.
All of this can live with the fusion pass
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.
pass removed
vllm/compilation/pass_manager.py Outdated
| if self.pass_config.enable_fusion: | ||
| self.passes += [RMSNormQuantFusionPass(config)] | ||
| self.passes += [ActivationQuantFusionPass(config)] | ||
| self.passes += [MulAddFusionPass(config)] |
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.
New flag and new file please
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.
pass removed.
vllm/compilation/fusion.py Outdated
| ) | ||
| | ||
| | ||
| class MulAddFusionPass(VllmPatternMatcherPass): |
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.
New file please
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.
pass removed
| at1 = auto_functionalized( | ||
| SILU_MUL_OP, result=result_silu_mul, input=input | ||
| ) |
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.
Please use the MatcherSiluMul
Signed-off-by: charlifu <charlifu@amd.com>
| We found that mul+add fusion is not helping performance. So we are removing this pass.
|
Signed-off-by: charlifu <charlifu@amd.com>
Co-authored-by: Luka Govedič <ProExpertProg@users.noreply.github.com> Signed-off-by: Charlie Fu <Charlie.Fu@amd.com>
| This pull request has merge conflicts that must be resolved before it can be |
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
Signed-off-by: charlifu <charlifu@amd.com>
| This pull request has merge conflicts that must be resolved before it can be |
Signed-off-by: charlifu <charlifu@amd.com>
This PR adds a few fusion passes for Aiter to fusion layernorm + fp8 block quant and silu + fp8 block quant.