-
Notifications
You must be signed in to change notification settings - Fork 7
Integrate CUDA Graph 4D in test_megatron_e2e_pipeline.py #121
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
Changes from all commits
a212d82
c76df6a
1213e31
d6c9d92
fa9cf1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,16 +38,12 @@ def forward_pre_core_attn(layer: TransformerLayer, args: Dict[str, Any]): | |
| ) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(after pre core attn)") | ||
|
|
||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(before pre mlp to attn)") | ||
| signal = layer._pre_mlp_to_attn(query, key, value, args["packed_seq_params"]) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(after pre mlp to attn)") | ||
|
|
||
| args["query"] = query | ||
| args["key"] = key | ||
| args["value"] = value | ||
| args["residual"] = residual | ||
| args["attn_mask_type"] = attn_mask_type | ||
| args["signal"] = signal | ||
| forward_pre_core_attn_comm(layer, args) | ||
|
|
||
|
|
||
| # Record timing if enabled | ||
|
|
@@ -59,6 +55,16 @@ def forward_pre_core_attn(layer: TransformerLayer, args: Dict[str, Any]): | |
| return args | ||
|
|
||
|
|
||
| def forward_pre_core_attn_comm(layer: TransformerLayer, args: Dict[str, Any]): | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(before pre mlp to attn)") | ||
| signal = layer._pre_mlp_to_attn( | ||
| args["query"], args["key"], args["value"], args["packed_seq_params"], | ||
| ) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(after pre mlp to attn)") | ||
| args["signal"] = signal | ||
| return args | ||
|
|
||
|
|
||
| def layout_mlp_to_attn(layer: TransformerLayer, args: Dict[str, Any]): | ||
| log_memory_usage(f"(L{layer.layer_number}) layout_mlp_to_attn:(start)") | ||
| bwd_resend_qkv = args["packed_seq_params"].bwd_packed_seq_params is not None | ||
|
|
@@ -126,17 +132,7 @@ def layout_attn_to_mlp(layer: TransformerLayer, args: Dict[str, Any]): | |
| return args | ||
|
|
||
|
|
||
| def forward_post_core_attn(layer: TransformerLayer, args: Dict[str, Any]): | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_post_core_attn:(start)") | ||
|
|
||
| # Setup timing if enabled | ||
| start_event = None | ||
| end_event = None | ||
| if os.getenv("UNIFIED_RECORD_TICK_TIMES", "0") == "1" and _current_tick_sample_id is not None: | ||
| start_event = torch.cuda.Event(enable_timing=True) | ||
| end_event = torch.cuda.Event(enable_timing=True) | ||
| start_event.record() | ||
|
|
||
| def forward_post_core_attn_comm(layer: TransformerLayer, args: Dict[str, Any]): | ||
| signal = args.pop("signal") | ||
| packed_seq_params: PingPangSingleStepPackedSeqParams = args["packed_seq_params"] | ||
| bwd_resend_qkv = packed_seq_params.bwd_packed_seq_params is not None | ||
|
|
@@ -151,10 +147,25 @@ def forward_post_core_attn(layer: TransformerLayer, args: Dict[str, Any]): | |
| args.pop("query"), args.pop("key"), args.pop("value") | ||
| else: | ||
| core_attn_out = layer._post_attn_to_mlp(signal, args["packed_seq_params"]) | ||
| residual = args.pop("residual") | ||
| args["core_attn_out"] = core_attn_out | ||
| return args | ||
|
|
||
|
|
||
| def forward_post_core_attn(layer: TransformerLayer, args: Dict[str, Any]): | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_post_core_attn:(start)") | ||
|
|
||
| # Setup timing if enabled | ||
| start_event = None | ||
| end_event = None | ||
| if os.getenv("UNIFIED_RECORD_TICK_TIMES", "0") == "1" and _current_tick_sample_id is not None: | ||
| start_event = torch.cuda.Event(enable_timing=True) | ||
| end_event = torch.cuda.Event(enable_timing=True) | ||
| start_event.record() | ||
|
|
||
| forward_post_core_attn_comm(layer, args) | ||
| mlp_output, context = layer._forward_post_core_attn( | ||
| core_attn_out, | ||
| residual, | ||
| args.pop("core_attn_out"), | ||
| args.pop("residual"), | ||
| args["context"], | ||
| args["context_mask"], | ||
| ) | ||
|
|
@@ -171,6 +182,67 @@ def forward_post_core_attn(layer: TransformerLayer, args: Dict[str, Any]): | |
| return args | ||
|
|
||
|
|
||
| def forward_post_then_pre_core_attn_cuda_graph(layer: TransformerLayer, args: Dict[str, Any]): | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_post_then_pre_core_attn:(start)") | ||
| assert args["context"] is None and args["context_mask"] is None, "not supported in cudagraph" | ||
| forward_post_core_attn_comm(layer, args) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_post_then_pre_core_attn:(before non core attn)") | ||
| query, key, value, residual, attn_mask_type = layer._forward_pre_attn_cuda_graph( | ||
| (args.pop("core_attn_out"), args.pop("residual")), | ||
| args["rotary_pos_emb"], | ||
| args["rotary_pos_cos"], | ||
| args["rotary_pos_sin"], | ||
| args["mlp_packed_seq_params"], | ||
| args["sequence_len_offset"], | ||
| ) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_post_then_pre_core_attn:(after non core attn)") | ||
| args["query"] = query | ||
| args["key"] = key | ||
| args["value"] = value | ||
| args["residual"] = residual | ||
| args["attn_mask_type"] = attn_mask_type | ||
| forward_pre_core_attn_comm(layer, args) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_then_pre_core_attn:(return)") | ||
| return args | ||
|
|
||
|
|
||
| def forward_pre_core_attn_cuda_graph(layer: TransformerLayer, args: Dict[str, Any]): | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(start)") | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(before pre core attn)") | ||
| hidden_states = args.pop("hidden_states") | ||
| query, key, value, residual, attn_mask_type = layer._forward_pre_attn_cuda_graph( | ||
| (hidden_states,), | ||
| args["rotary_pos_emb"], | ||
| args["rotary_pos_cos"], | ||
| args["rotary_pos_sin"], | ||
| args["mlp_packed_seq_params"], | ||
| args["sequence_len_offset"], | ||
| ) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(after pre core attn)") | ||
|
|
||
| args["query"] = query | ||
| args["key"] = key | ||
| args["value"] = value | ||
| args["residual"] = residual | ||
| args["attn_mask_type"] = attn_mask_type | ||
| forward_pre_core_attn_comm(layer, args) | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_pre_core_attn:(return)") | ||
| return args | ||
|
|
||
|
|
||
| def forward_post_core_attn_cuda_graph(layer: TransformerLayer, args: Dict[str, Any]): | ||
| forward_post_core_attn_comm(layer, args) | ||
| mlp_output = layer._forward_post_attn_cuda_graph( | ||
| args.pop("core_attn_out"), | ||
| args.pop("residual"), | ||
| args["context"], | ||
| args["context_mask"], | ||
| ) | ||
| args["hidden_states"] = mlp_output | ||
| log_memory_usage(f"(L{layer.layer_number}) forward_post_core_attn:(end)") | ||
| return args | ||
|
|
||
|
|
||
| def tick_sync(compute_stream, comm_stream, arg_group_0, keys_0, arg_group_1, keys_1, | ||
| layer_info="unknown", operation_info="unknown"): | ||
| log_memory_usage(f"(L?) tick_sync:(start)") | ||
|
|
@@ -213,6 +285,15 @@ def tick_nonca_compute( | |
| arg_group = forward_pre_core_attn(layer, arg_group) | ||
| return arg_group | ||
|
|
||
| def tick_nonca_compute_cuda_graph( | ||
| layer: TransformerLayer, prev_layer: Optional[TransformerLayer], | ||
| arg_group: Dict[str, Any], is_last_layer_post_attn: bool | ||
| ): | ||
| if is_last_layer_post_attn: | ||
| return forward_post_core_attn_cuda_graph(layer, arg_group) | ||
| if prev_layer is None: | ||
| return forward_pre_core_attn(layer, arg_group) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Wrong function called for first layer in CUDA graph modeIn |
||
| return forward_post_then_pre_core_attn_cuda_graph(layer, arg_group) | ||
|
|
||
| # ========== Tick Operations Timing Collection ========== | ||
| # TODO: (Refactor) Move this to somewhere else for sole measurement purpose only. | ||
|
|
||
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.
Bug: Previous layer not passed to CUDA graph communication function
In
forward_post_then_pre_core_attn_cuda_graph, the functionforward_post_core_attn_commis called withlayer(the current layer), but it should use the previous layer for processing the previous layer's attention output. Thetick_nonca_compute_cuda_graphfunction has access toprev_layerbut doesn't pass it toforward_post_then_pre_core_attn_cuda_graph. In the non-CUDA-graph versiontick_nonca_compute,forward_post_core_attn(prev_layer, arg_group)correctly usesprev_layer. This causeslayer._post_attn_to_mlpand config values to be read from the wrong layer.Additional Locations (1)
d2/runtime/megatron/ping_pong/tick_ops.py#L295-L296