Fix GQA computation when fused_qkv=True#4442
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| ) | ||
|
|
||
| def _logical_to_mesh_axes(self, logical_name): | ||
| logical_rules = None if self.config.using_pipeline_parallelism else self.config.logical_axis_rules |
There was a problem hiding this comment.
can you explain this logic - I would think logical rules are still desired here even with PP
There was a problem hiding this comment.
From my understanding, with pipeline parallelism, when logical_rules is None, the Flax logical_to_mesh_axes method will use the rules from the current nn.partitioning.axis_rules(...) context as opposed to the fixed rules provided by the config. These context rules are used with pipeline parallelism here for example. In these cases, we wouldn't want to use the config logical axis rules but the modified rules from the context manager instead.
This is a pattern that is done throughout other parts of the codebase such as here. Let me know if you would like me to consolidate them into a single helper
2694399 to
b5bb27b
Compare
Description
This PR fixes a bug with fused_qkv=True and Grouped Query Attention (GQA) configuration. Previously, when performing GQA (
num_query_heads > num_kv_heads),fused_qkv=Truewould ignore thenum_kv_headsargument and assume that the user was configuring MHA and usenum_query_headsfor the number of query, key, and value heads.This PR fixed that bug by adding support for fused_qkv with GQA so that fused_qkv will appropriately perform GQA when the user configures it.
Previously, when executing a Llama-70B model (which performs GQA with 64 query heads, and 8 kv heads) we would have the following optimized HLO generated when
fused_qkv=Truewith the following (incorrectly reported) performance since the FLOP math that maxtext's metrics computes additionally reported the total FLOPs assuming GQA (since that is what the user configured) despite compiling and executing an MHA-based attention layer prior to the fix.
With the changes in this PR, we get the correct HLO that performs GQA (see num key/value heads < num query heads)
with correctly reported performance (GQA flops with GQA execution step time)
Tests
I validated this change by running Llama3-70b with fused_qkv prior to the new changes, inspected the optimized HLO profile, and observed MHA-based attention despite configuring GQA. Then, with these changes, I re-ran the same workflow and observed the correct computation graph.
Reproducer command:
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.