[JAX] Align fused attn backward output gradient sharding and other test strengthening - #3516
KshitijLakhani wants to merge 7 commits into
Conversation
c25335d to
332cfb5
Compare
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
332cfb5 to
4f54dbf
Compare
|
/te-ci jax L0 L1 |
|
| print_debug_tensor_stats(f"primitive_out", primitive_out) | ||
| print_debug_tensor_stats(f"reference_grad_valid", reference_out) | ||
| print_debug_tensor_stats(f"diff_grad", jnp.abs(primitive_out - reference_out)) | ||
| assert_allclose( |
There was a problem hiding this comment.
Was this assert_allclose also part of the debugging code you're removing here? Thanks for removing the debug prints
| ) or (window_size == (-1, -1) and not attn_mask_type.is_bottom_right()): | ||
| return _segment_ids_pos_to_seqlens_offsets_fast_causal_path( | ||
| segment_ids_q, segment_ids_kv, segment_pos_q, segment_pos_kv, max_segments_per_seq | ||
| fast_path_requested = allow_fast_causal_path and ( |
There was a problem hiding this comment.
What is the performance impact of not picking the fast path?
If it is significant, it might be nice to log a warning with the reasoning the fast path was unable to be taken. This would give users insight if they see a sudden TE perf regression if they take a path where the fast path is not supported
| qkv_layout, | ||
| window_size, | ||
| max_segments_per_seq, | ||
| allow_fast_causal_path=True, |
There was a problem hiding this comment.
When would a user want to intentionally set this to False? It seems internally we already automatically switch to a fallback if this allow_fast_causal_path is True but it isn't supported in that configuration.
If there isn't a valid use case for allow_fast_causal_path=False at the user level, maybe we keep this logic internal and don't expose this knob on this public API function.
If there is a valid use case where setting it to False is required/recommended, then let's expand the docstring to document that use case
| doutput_idx = 8 | ||
| arg_shardings[doutput_idx] = arg_shardings[output_idx] | ||
| # Each segment position tensor describes the tokens in its matching ID tensor. | ||
| arg_shardings[-1] = arg_shardings[-3] |
There was a problem hiding this comment.
This is okay for now and pretty much the best we can do in the current sharding paradigm with automatic sharding propagation. But it could in theory trigger an AG and replication if one of the shardings was replicated, since it would copy that to the other arg's sharding. However, that is correct behavior since it ensures correct outputs and is mitigating the user-provided incorrect sharding
Good news, when we move to explicit sharding, the shardings are propagated at trace time so we can actually raise an error in cases like this and require the user to provide matching sharding. This will avoid cases like this where we have to do something safe but un-optimal (due to incorrect user-provided sharding)
There was a problem hiding this comment.
It's nice that you refactored this commonly re-used code into a helper so when we move to explicit sharding we can handle this error raising in one place
Description
Fix JAX fused-attention bwd sharding and THD metadata handling, while strengthening bwd test coverage with nonuniform output gradients.
The starting point was fixing the JAX fused-attention bwd sharding, but along the way as I decided to strengthen the testing as part of root causing and then just fixed any ripple effects it had in exposing smaller issues in our feature code
Type of change
Changes
Align bwd doutput sharding with the saved fwd output sharding : JAX can infer a doutput sharding that is incompatible with the cuDNN bwd kernel, particularly when CP reorder operations are differentiated through. TE now explicitly aligns doutput sharding with the saved output sharding.
Use nonuniform output gradients in CP tests: Existing tests generated a uniform output gradient from mean(output). Uniform values are unchanged by permutation and therefore masked incorrect CP placement.
NOTE: This was the reason why TE tests did not catch the doutput sharding bug from above and so I decided to go with a more generic case.
Route rotated THD ring metadata through the general path: The THD fast metadata path assumes Q and KV describe matching token metadata. Rotated ring-attention steps may have different local Q and KV boundaries, so those steps now explicitly disable the fast path.
NOTE: This was found while strengthening the CP test with a nonuniform doutput as mentioned above
Make general THD path selection metadata-aware: Non-CP calls retain the fast path when Q and KV metadata match. Equal-length cross-attention with independent metadata uses the general path.
Strengthen bwd fused attn tests:
Correct DP/FSDP test data placement (side effect of Bugfix spec check for DP+FSDP configurations #3272) :
Checklist: