@@ -931,6 +931,27 @@ def _layernorm_shape(
931931 ]
932932
933933
934+ _RMSNORM_RHT_AMAX_RPC_CANDIDATES = (2 , 4 , 8 )
935+ _RMSNORM_RHT_AMAX_TARGET_MIN_CTAS = 148
936+
937+
938+ def _rmsnorm_rht_amax_pick_rows_per_cta (m : int ) -> int :
939+ for rows_per_cta in reversed (_RMSNORM_RHT_AMAX_RPC_CANDIDATES ):
940+ if m % rows_per_cta != 0 :
941+ continue
942+ if m // rows_per_cta >= _RMSNORM_RHT_AMAX_TARGET_MIN_CTAS :
943+ return rows_per_cta
944+ return _RMSNORM_RHT_AMAX_RPC_CANDIDATES [0 ]
945+
946+
947+ def _squeeze_trailing_unit_spec_shape (
948+ shape : tuple [Any , ...], expected_rank : int
949+ ) -> tuple [Any , ...]:
950+ if len (shape ) == expected_rank + 1 and shape [- 1 ] == 1 :
951+ return shape [:- 1 ]
952+ return shape
953+
954+
934955def _rmsnorm_shape (
935956 input_specs : list [TensorSpec ], attrs : dict [str , Any ]
936957) -> list [TensorSpec ]:
@@ -950,6 +971,68 @@ def _rmsnorm_shape(
950971 ]
951972
952973
974+ def _rmsnorm_rht_amax_shape (
975+ input_specs : list [TensorSpec ], attrs : dict [str , Any ]
976+ ) -> list [TensorSpec ]:
977+ inp = input_specs [0 ]
978+ weight = input_specs [1 ]
979+ x_shape = _squeeze_trailing_unit_spec_shape (tuple (inp .shape ), 2 )
980+ w_shape = _squeeze_trailing_unit_spec_shape (tuple (weight .shape ), 1 )
981+ if len (x_shape ) != 2 :
982+ raise RuntimeError (
983+ "rmsnorm_rht_amax_wrapper_sm100 x_tensor must be 2D"
984+ )
985+ if len (w_shape ) != 1 :
986+ raise RuntimeError (
987+ "rmsnorm_rht_amax_wrapper_sm100 w_tensor must be 1D"
988+ )
989+ m , n = x_shape
990+ if isinstance (n , int ) and isinstance (w_shape [0 ], int ) and w_shape [0 ] != n :
991+ raise RuntimeError (
992+ "rmsnorm_rht_amax_wrapper_sm100 w_tensor length must match "
993+ "x hidden dimension"
994+ )
995+ if isinstance (n , int ) and n % 16 != 0 :
996+ raise RuntimeError (
997+ "rmsnorm_rht_amax_wrapper_sm100 N must be divisible by 16"
998+ )
999+
1000+ rows_per_cta = attrs .get ("rows_per_cta" )
1001+ if rows_per_cta is None :
1002+ if not isinstance (m , int ):
1003+ raise RuntimeError (
1004+ "rmsnorm_rht_amax_wrapper_sm100 requires concrete M when "
1005+ "rows_per_cta is omitted"
1006+ )
1007+ rows_per_cta = _rmsnorm_rht_amax_pick_rows_per_cta (m )
1008+ rows_per_cta = int (rows_per_cta )
1009+ if rows_per_cta <= 0 :
1010+ raise RuntimeError (
1011+ "rmsnorm_rht_amax_wrapper_sm100 rows_per_cta must be positive"
1012+ )
1013+ if isinstance (m , int ):
1014+ if m % rows_per_cta != 0 :
1015+ raise RuntimeError (
1016+ "rmsnorm_rht_amax_wrapper_sm100 M must be divisible by "
1017+ "rows_per_cta"
1018+ )
1019+ amax_shape = (m // rows_per_cta ,)
1020+ else :
1021+ amax_shape = (m ,)
1022+
1023+ return [
1024+ TensorSpec (
1025+ name = "o_tensor" ,
1026+ shape = x_shape ,
1027+ dtype = inp .dtype ,
1028+ layout = "contiguous" ,
1029+ device = inp .device ,
1030+ contiguous = True ,
1031+ ),
1032+ _float32_spec (amax_shape , inp .device ).with_name ("amax_tensor" ),
1033+ ]
1034+
1035+
9531036def _batchnorm_shape (
9541037 input_specs : list [TensorSpec ], attrs : dict [str , Any ]
9551038) -> list [TensorSpec ]:
@@ -1068,6 +1151,51 @@ def _normalize_rmsnorm(
10681151 return input_ids , attrs
10691152
10701153
1154+ def _normalize_rmsnorm_rht_amax (
1155+ ctx : Any , args : tuple [Any , ...], kwargs : dict [str , Any ]
1156+ ) -> tuple [list [int ], dict ]:
1157+ names = ("x_tensor" , "w_tensor" )
1158+ if len (args ) > len (names ):
1159+ raise TypeError (
1160+ "rmsnorm_rht_amax_wrapper_sm100 got too many positional args"
1161+ )
1162+ params = dict (kwargs )
1163+ values = {}
1164+ for name , value in zip (names , args ):
1165+ values [name ] = value
1166+ for name in names [len (args ) :]:
1167+ if name in params :
1168+ values [name ] = params .pop (name )
1169+ missing = [name for name in names if name not in values ]
1170+ if missing :
1171+ raise TypeError (f"rmsnorm_rht_amax_wrapper_sm100 missing { missing [0 ]} " )
1172+ current_stream = params .pop ("current_stream" , None )
1173+ if current_stream is not None :
1174+ raise TypeError (
1175+ "rmsnorm_rht_amax_wrapper_sm100 current_stream is not supported "
1176+ "in graph capture"
1177+ )
1178+ attrs = {
1179+ "eps" : float (params .pop ("eps" , 1e-5 )),
1180+ "num_threads" : params .pop ("num_threads" , None ),
1181+ "rows_per_cta" : params .pop ("rows_per_cta" , None ),
1182+ "name" : params .pop ("name" , "" ),
1183+ }
1184+ if attrs ["num_threads" ] is not None :
1185+ attrs ["num_threads" ] = int (attrs ["num_threads" ])
1186+ if attrs ["rows_per_cta" ] is not None :
1187+ attrs ["rows_per_cta" ] = int (attrs ["rows_per_cta" ])
1188+ if params :
1189+ raise TypeError (
1190+ "rmsnorm_rht_amax_wrapper_sm100 got unsupported graph attrs: "
1191+ f"{ sorted (params )} "
1192+ )
1193+ return [
1194+ ctx .as_value (values ["x_tensor" ], "x_tensor" ),
1195+ ctx .as_value (values ["w_tensor" ], "w_tensor" ),
1196+ ], attrs
1197+
1198+
10711199def _normalize_batchnorm (
10721200 ctx : Any , args : tuple [Any , ...], kwargs : dict [str , Any ]
10731201) -> tuple [list [int ], dict ]:
@@ -2416,6 +2544,21 @@ def run(inputs: list[Any], attrs: dict[str, Any]) -> Any:
24162544 return run
24172545
24182546
2547+ def _run_rmsnorm_rht_amax (flag_ops : Any ) -> RunFn :
2548+ def run (inputs : list [Any ], attrs : dict [str , Any ]) -> Any :
2549+ _require_runtime_backend (inputs [:2 ], "rmsnorm_rht_amax_wrapper_sm100" )
2550+ result = flag_ops .rmsnorm_rht_amax_wrapper_sm100 (
2551+ inputs [0 ],
2552+ inputs [1 ],
2553+ eps = attrs .get ("eps" , 1e-5 ),
2554+ num_threads = attrs .get ("num_threads" ),
2555+ rows_per_cta = attrs .get ("rows_per_cta" ),
2556+ )
2557+ return result ["o_tensor" ], result ["amax_tensor" ]
2558+
2559+ return run
2560+
2561+
24192562def _run_causal_conv1d (flag_ops : Any ) -> RunFn :
24202563 def run (inputs : list [Any ], attrs : dict [str , Any ]) -> torch .Tensor :
24212564 _require_runtime_backend (inputs , "causal_conv1d" )
@@ -2804,6 +2947,17 @@ def register_default_ops() -> None:
28042947 )
28052948 )
28062949
2950+ register_op (
2951+ OpSchema (
2952+ name = "rmsnorm_rht_amax_wrapper_sm100" ,
2953+ normalize_fn = _normalize_rmsnorm_rht_amax ,
2954+ shape_fn = _rmsnorm_rht_amax_shape ,
2955+ run_fn = _run_rmsnorm_rht_amax (flag_ops ),
2956+ num_outputs = 2 ,
2957+ fusible = True ,
2958+ )
2959+ )
2960+
28072961 register_op (
28082962 OpSchema (
28092963 name = "matmul" ,
0 commit comments