88 PatternMatcherPass , fwd_only ,
99 register_replacement )
1010
11+ from tensorrt_llm .mapping import Mapping
12+
1113from ...custom_ops .torch_custom_ops import BufferKind
1214from ...distributed import AllReduceFusionOp , AllReduceStrategy
15+ from . import MATCHER_SUBSYSTEM
1316
1417aten = torch .ops .aten
15- from tensorrt_llm .mapping import Mapping
18+
19+
20+ def _append_named_pass (custom_passes : List [PatternMatcherPass ], pass_name : str ):
21+ custom_passes .append (PatternMatcherPass (pass_name , MATCHER_SUBSYSTEM ))
22+
23+
24+ def _check_getitem_only_users (match : Match , pattern_node ) -> bool :
25+ node = match .ctx .pattern_to_node [pattern_node ]
26+ if not isinstance (node , torch .fx .graph .Node ):
27+ return False
28+ for user in node .users :
29+ if user .op != "call_function" or user .target is not getitem :
30+ return False
31+ return True
32+
33+
34+ def _has_getitem_user (match : Match , pattern_node , index : int ) -> bool :
35+ node = match .ctx .pattern_to_node [pattern_node ]
36+ if not isinstance (node , torch .fx .graph .Node ):
37+ return False
38+ for user in node .users :
39+ if (user .op == "call_function" and user .target is getitem
40+ and user .args [1 ] == index ):
41+ return True
42+ return False
43+
44+
45+ def _make_fp8_quant_extra_check (input_node , strategy_node , quant_node ,
46+ require_scale_output : bool ):
47+
48+ def extra_check (match : Match ) -> bool :
49+ return (check_f16_bf16_input (match , input_node )
50+ and check_non_ub_strategy (match , strategy_node )
51+ and _check_getitem_only_users (match , quant_node ) and
52+ _has_getitem_user (match , quant_node , 1 ) == require_scale_output )
53+
54+ return extra_check
1655
1756
1857def register_ar_residual_norm (custom_pass : PatternMatcherPass , mapping : Mapping ,
@@ -135,15 +174,16 @@ def register_ar_residual_norm_out_fp8_quant(custom_pass: PatternMatcherPass,
135174 torch .ops .tensorrt_llm .static_quantize_e4m3_per_tensor .default ,
136175 getitem_0 ,
137176 KeywordArg ("scale" ),
138- _users = 2 )
139- getitem_2 = CallFunction (getitem ,
140- static_quantize_e4m3_per_tensor_default ,
141- 0 ,
142- _users = 2 )
177+ _users = MULTIPLE )
178+ getitem_2 = CallFunction (getitem , static_quantize_e4m3_per_tensor_default ,
179+ 0 )
143180 getitem_3 = CallFunction (getitem , static_quantize_e4m3_per_tensor_default ,
144181 1 )
145- pattern = MultiOutputPattern ([getitem_0 , getitem_1 , getitem_2 , getitem_3
146- ]) # norm_out, residual_out, quant_out, scale
182+ pattern_with_scale = MultiOutputPattern (
183+ [getitem_0 , getitem_1 , getitem_2 ,
184+ getitem_3 ]) # norm_out, residual_out, quant_out, scale
185+ pattern_without_scale = MultiOutputPattern (
186+ [getitem_0 , getitem_1 , getitem_2 ]) # norm_out, residual_out, quant_out
147187
148188 def empty_pattern (
149189 input : torch .Tensor ,
@@ -174,18 +214,48 @@ def target_pattern(
174214 trigger_completion_at_end )
175215 return allreduce [0 ], allreduce [2 ], allreduce [1 ], scale
176216
177- def extra_check (match : Match ) -> bool :
178- return check_f16_bf16_input (
179- match , input_node ) and check_non_ub_strategy (match , strategy_node )
217+ def target_pattern_without_scale (
218+ input : torch .Tensor ,
219+ residual : torch .Tensor ,
220+ gamma : torch .Tensor ,
221+ workspace : torch .LongTensor ,
222+ strategy : int ,
223+ eps : float ,
224+ scale : torch .Tensor ,
225+ trigger_completion_at_end : bool ,
226+ ):
227+ allreduce = allreduce_func (
228+ input , residual , gamma , scale , None , workspace , mapping .tp_group ,
229+ int (strategy ),
230+ int (AllReduceFusionOp .RESIDUAL_RMS_NORM_OUT_QUANT_FP8 ), float (eps ),
231+ trigger_completion_at_end )
232+ return allreduce [0 ], allreduce [2 ], allreduce [1 ]
233+
234+ extra_check_with_scale = _make_fp8_quant_extra_check (
235+ input_node , strategy_node , static_quantize_e4m3_per_tensor_default ,
236+ True )
237+ extra_check_without_scale = _make_fp8_quant_extra_check (
238+ input_node , strategy_node , static_quantize_e4m3_per_tensor_default ,
239+ False )
180240
181241 register_replacement (
182242 empty_pattern ,
183243 target_pattern ,
184244 [],
185245 fwd_only ,
186246 custom_pass ,
187- search_fn_pattern = pattern ,
188- extra_check = extra_check ,
247+ search_fn_pattern = pattern_with_scale ,
248+ extra_check = extra_check_with_scale ,
249+ )
250+
251+ register_replacement (
252+ empty_pattern ,
253+ target_pattern_without_scale ,
254+ [],
255+ fwd_only ,
256+ custom_pass ,
257+ search_fn_pattern = pattern_without_scale ,
258+ extra_check = extra_check_without_scale ,
189259 )
190260
191261
@@ -213,15 +283,15 @@ def register_ar_residual_norm_fp8_quant(custom_pass: PatternMatcherPass,
213283 torch .ops .tensorrt_llm .static_quantize_e4m3_per_tensor .default ,
214284 getitem_0 ,
215285 KeywordArg ("scale" ),
216- _users = 2 )
217- getitem_2 = CallFunction (getitem ,
218- static_quantize_e4m3_per_tensor_default ,
219- 0 ,
220- _users = 2 )
286+ _users = MULTIPLE )
287+ getitem_2 = CallFunction (getitem , static_quantize_e4m3_per_tensor_default ,
288+ 0 )
221289 getitem_3 = CallFunction (getitem , static_quantize_e4m3_per_tensor_default ,
222290 1 )
223- pattern = MultiOutputPattern ([getitem_1 , getitem_2 ,
224- getitem_3 ]) # residual_out, quant_out, scale
291+ pattern_with_scale = MultiOutputPattern (
292+ [getitem_1 , getitem_2 , getitem_3 ]) # residual_out, quant_out, scale
293+ pattern_without_scale = MultiOutputPattern ([getitem_1 , getitem_2
294+ ]) # residual_out, quant_out
225295
226296 def empty_pattern (
227297 input : torch .Tensor ,
@@ -251,18 +321,47 @@ def target_pattern(
251321 float (eps ), trigger_completion_at_end )
252322 return allreduce [1 ], allreduce [0 ], scale
253323
254- def extra_check (match : Match ) -> bool :
255- return check_f16_bf16_input (
256- match , input_node ) and check_non_ub_strategy (match , strategy_node )
324+ def target_pattern_without_scale (
325+ input : torch .Tensor ,
326+ residual : torch .Tensor ,
327+ gamma : torch .Tensor ,
328+ workspace : torch .LongTensor ,
329+ strategy : int ,
330+ eps : float ,
331+ scale : torch .Tensor ,
332+ trigger_completion_at_end : bool ,
333+ ):
334+ allreduce = allreduce_func (
335+ input , residual , gamma , scale , None , workspace , mapping .tp_group ,
336+ int (strategy ), int (AllReduceFusionOp .RESIDUAL_RMS_NORM_QUANT_FP8 ),
337+ float (eps ), trigger_completion_at_end )
338+ return allreduce [1 ], allreduce [0 ]
339+
340+ extra_check_with_scale = _make_fp8_quant_extra_check (
341+ input_node , strategy_node , static_quantize_e4m3_per_tensor_default ,
342+ True )
343+ extra_check_without_scale = _make_fp8_quant_extra_check (
344+ input_node , strategy_node , static_quantize_e4m3_per_tensor_default ,
345+ False )
257346
258347 register_replacement (
259348 empty_pattern ,
260349 target_pattern ,
261350 [],
262351 fwd_only ,
263352 custom_pass ,
264- search_fn_pattern = pattern ,
265- extra_check = extra_check ,
353+ search_fn_pattern = pattern_with_scale ,
354+ extra_check = extra_check_with_scale ,
355+ )
356+
357+ register_replacement (
358+ empty_pattern ,
359+ target_pattern_without_scale ,
360+ [],
361+ fwd_only ,
362+ custom_pass ,
363+ search_fn_pattern = pattern_without_scale ,
364+ extra_check = extra_check_without_scale ,
266365 )
267366
268367
@@ -773,16 +872,20 @@ def extra_check(match: Match) -> bool:
773872 extra_check = extra_check ,
774873 )
775874
776- custom_passes .append (PatternMatcherPass ())
875+ _append_named_pass (
876+ custom_passes ,
877+ f"ub_convert_supported_ar_to_ub:{ allreduce_func .__name__ } " )
777878 register_convert_supported_ar_to_ub (custom_passes [- 1 ])
778879
779- custom_passes . append ( PatternMatcherPass () )
880+ _append_named_pass ( custom_passes , f"ub_prologue: { allreduce_func . __name__ } " )
780881 register_ub_prologue_patterns (custom_passes [- 1 ])
781882
782- custom_passes . append ( PatternMatcherPass () )
883+ _append_named_pass ( custom_passes , f"ub_finalize: { allreduce_func . __name__ } " )
783884 register_ub_finalize_patterns (custom_passes [- 1 ])
784885
785- custom_passes .append (PatternMatcherPass ())
886+ _append_named_pass (
887+ custom_passes ,
888+ f"insert_copy_for_graph_output:{ allreduce_func .__name__ } " )
786889 insert_copy_for_graph_output (custom_passes [- 1 ])
787890
788891
@@ -793,7 +896,7 @@ def register_ar_fusions(custom_passes: List[PatternMatcherPass],
793896 register_ar_residual_norm (custom_passes [- 1 ], mapping ,
794897 torch .ops .trtllm .tunable_allreduce )
795898
796- custom_passes . append ( PatternMatcherPass () )
899+ _append_named_pass ( custom_passes , "ar_residual_norm_quant" )
797900 for allreduce_func in [
798901 torch .ops .trtllm .allreduce , torch .ops .trtllm .tunable_allreduce
799902 ]:
0 commit comments