@@ -5614,6 +5614,75 @@ def _impl_v1(cls, bb, inputs, attr, params):
56145614 return relax .Tuple ([output , placeholder , placeholder ])
56155615
56165616
5617+ class SimplifiedLayerNormalization (OnnxOpConverter ):
5618+ """Converts a microsoft contrib SimplifiedLayerNormalization node into a Relax expression."""
5619+
5620+ @classmethod
5621+ def _impl_v1 (cls , bb , inputs , attr , params ):
5622+ data = inputs [0 ]
5623+ scale = inputs [1 ]
5624+ bias = inputs [2 ] if len (inputs ) > 2 and inputs [2 ] is not None else None
5625+ axis = attr .get ("axis" , - 1 )
5626+ epsilon = attr .get ("epsilon" , 1e-05 )
5627+ stash_type = attr .get ("stash_type" , 1 )
5628+
5629+ ndim = _get_known_tensor_rank (data )
5630+ if ndim is None :
5631+ raise ValueError ("SimplifiedLayerNormalization requires a statically known input rank." )
5632+ axis = _normalize_constant_axes ([axis ], ndim , "SimplifiedLayerNormalization" )[0 ]
5633+ axes = list (range (axis , ndim ))
5634+
5635+ input_dtype = data .ty .dtype .dtype
5636+ scale_dtype = scale .ty .dtype .dtype
5637+
5638+ # Broadcast scale to the normalized shape if necessary
5639+ if isinstance (data .ty .shape , relax .ShapeExpr ):
5640+ normalized_shape = relax .ShapeExpr (data .ty .shape .values [axis :])
5641+ if not tvm_ffi .structural_equal (scale .ty .shape , normalized_shape ):
5642+ scale = relax .op .broadcast_to (scale , normalized_shape )
5643+
5644+ # stash_type == 1 -> compute in float32, otherwise in the input dtype
5645+ compute_dtype = "float32" if stash_type == 1 else input_dtype
5646+
5647+ data_compute = (
5648+ data if input_dtype == compute_dtype else relax .op .astype (data , compute_dtype )
5649+ )
5650+ scale_compute = (
5651+ scale if scale_dtype == compute_dtype else relax .op .astype (scale , compute_dtype )
5652+ )
5653+
5654+ num_outputs = attr .get ("tvm_custom" , {}).get ("num_outputs" , 1 )
5655+ full_reduce = len (axes ) == ndim
5656+
5657+ if full_reduce or num_outputs > 1 :
5658+ mean_square = relax .op .mean (
5659+ relax .op .multiply (data_compute , data_compute ), axis = axes , keepdims = full_reduce
5660+ )
5661+ inv_std = relax .op .rsqrt (
5662+ relax .op .add (mean_square , relax .const (epsilon , dtype = compute_dtype ))
5663+ )
5664+
5665+ if full_reduce :
5666+ # Decompose to avoid the TOPI rms_norm 0-dim indexing bug
5667+ output = relax .op .multiply (relax .op .multiply (data_compute , inv_std ), scale_compute )
5668+ else :
5669+ output = relax .op .nn .rms_norm (data_compute , scale_compute , axes , epsilon )
5670+
5671+ if bias is not None :
5672+ output = relax .op .add (output , relax .op .astype (bias , compute_dtype ))
5673+
5674+ if compute_dtype != scale_dtype :
5675+ output = relax .op .astype (output , scale_dtype )
5676+
5677+ if num_outputs == 1 :
5678+ return output
5679+
5680+ if full_reduce :
5681+ inv_std = relax .op .squeeze (inv_std )
5682+
5683+ return relax .Tuple ([output , inv_std ])
5684+
5685+
56175686class SkipSimplifiedLayerNormalization (OnnxOpConverter ):
56185687 """Converts a microsoft contrib SkipSimplifiedLayerNormalization node into a Relax expression."""
56195688
@@ -5624,16 +5693,34 @@ def _impl_v1(cls, bb, inputs, attr, params):
56245693 gamma = inputs [2 ]
56255694 bias = inputs [3 ] if len (inputs ) > 3 and inputs [3 ] is not None else None
56265695
5627- epsilon = attr .get ("epsilon" , 1e-5 )
5696+ epsilon = attr .get ("epsilon" , 1e-12 )
5697+ num_outputs = attr .get ("tvm_custom" , {}).get ("num_outputs" , 3 )
5698+
5699+ input_dtype = data .ty .dtype .dtype
5700+ low_precision = input_dtype in ("float16" , "bfloat16" )
5701+ if low_precision :
5702+ data = relax .op .astype (data , "float32" )
5703+ skip = relax .op .astype (skip , "float32" )
5704+ gamma = relax .op .astype (gamma , "float32" )
5705+ if bias is not None :
5706+ bias = relax .op .astype (bias , "float32" )
56285707
56295708 data = relax .op .add (data , skip )
56305709 if bias is not None :
56315710 data = relax .op .add (data , bias )
56325711
56335712 output = relax .op .nn .rms_norm (data , gamma , axes = - 1 , epsilon = epsilon )
56345713
5635- # Expects three outputs though only the first is used. Construct a placeholder for others.
5714+ if low_precision :
5715+ output = relax .op .astype (output , input_dtype )
5716+ if num_outputs > 3 :
5717+ data = relax .op .astype (data , input_dtype )
5718+
56365719 placeholder = relax .const (0 , dtype = "float32" )
5720+
5721+ if num_outputs > 3 :
5722+ # Output 4 - input_skip_bias_sum
5723+ return relax .Tuple ([output , placeholder , placeholder , data ])
56375724 return relax .Tuple ([output , placeholder , placeholder ])
56385725
56395726
@@ -6915,7 +7002,7 @@ def _get_convert_map():
69157002 "BatchNormalization" : BatchNormalization ,
69167003 "LayerNormalization" : LayerNormalization ,
69177004 "RMSNormalization" : RMSNormalization ,
6918- "SimplifiedLayerNormalization" : RMSNormalization ,
7005+ "SimplifiedLayerNormalization" : SimplifiedLayerNormalization ,
69197006 "GroupNormalization" : GroupNormalization ,
69207007 "SkipLayerNormalization" : SkipLayerNormalization ,
69217008 "SkipSimplifiedLayerNormalization" : SkipSimplifiedLayerNormalization ,
0 commit comments