@@ -126,7 +126,7 @@ def te_general_grouped_gemm_fl(
126126 D : Optional [List [torch .Tensor ]],
127127 D_type : Any ,
128128 m_splits : List [int ],
129- bias : List [torch .Tensor ],
129+ bias : List [torch .Tensor ], # bias or grad_bias
130130 bias_type : Any ,
131131 single_output : bool ,
132132 pre_gelu_out : List [torch .Tensor ],
@@ -148,30 +148,25 @@ def te_general_grouped_gemm_fl(
148148 n = B [i ].shape [0 ] if transb else B [i ].shape [1 ]
149149 D .append (torch .empty ((m , n ), dtype = D [i ].dtype , device = A [0 ].device ))
150150
151- def gelu_backward (grad_output , x ):
152- # Approximation of GELU derivative commonly used in Transformer Engine
153- cdf = 0.5 * (1.0 + flag_gems .erf (x / flag_gems .sqrt (2.0 )))
154- pdf = flag_gems .exp (- 0.5 * x * x ) / flag_gems .sqrt (2.0 * math .pi )
155- return flag_gems .mul (grad_output , (cdf + x * pdf ))
156-
157151 temp_D = []
158152 for i in range (num_gemms ):
159153 # Handle the special case of zero-element inputs
160154 if A [i ].numel () == 0 or B [i ].numel () == 0 :
161155 if not single_output :
162156 if D [i ].numel () != 0 and not accumulate :
163- D [i ]. zero_ ( )
157+ flag_gems . copy_ ( D [i ], flag_gems . zeros ( D [ i ]. shape ) )
164158 else :
165- out = torch .zeros (A [i ].shape [0 ], B [i ].shape [1 ])
159+ out = flag_gems .zeros (( A [i ].shape [0 ], B [i ].shape [1 ]) )
166160 if grad and len (bias ) > i and bias [i ] is not None and bias [i ].numel () != 0 :
167- bias [i ]. zero_ ( )
161+ flag_gems . copy_ ( bias [i ], flag_gems . zeros ( bias [ i ]. shape ) )
168162 if (
169163 len (pre_gelu_out ) > i
170164 and pre_gelu_out [i ] is not None
171165 and pre_gelu_out [i ].numel () != 0
172166 ):
173- pre_gelu_out [i ]. zero_ ( )
167+ flag_gems . copy_ ( pre_gelu_out [i ], flag_gems . zeros ( pre_gelu_out [ i ]. shape ) )
174168 continue
169+
175170 a = A [i ].t () if transa else A [i ]
176171 b = B [i ].t () if transb else B [i ]
177172 # Determine presence of epilogue tensors
@@ -190,33 +185,35 @@ def gelu_backward(grad_output, x):
190185
191186 # Apply GELU epilogue if pre_gelu_out is provided
192187 if has_pre_gelu :
193- pre_gelu_out [i ]. copy_ ( out )
188+ flag_gems . copy_ ( pre_gelu_out [i ], out )
194189 out = flag_gems .gelu (out )
195190 else :
196191 out = flag_gems .mm (a , b )
192+
193+ # Apply dGELU epilogue if requested
197194 if has_pre_gelu :
198- out = gelu_backward (out , pre_gelu_out [i ])
195+ out = flag_gems . gelu_backward (out , pre_gelu_out [i ])
199196
200197 # Compute bias gradients if requested
201198 if has_bias :
202- bias_grad = out . sum ( dim = 0 )
199+ bias_grad = flag_gems . sum_dim ( out , dim = 0 )
203200 if accumulate :
204- bias [i ]. add_ ( bias_grad )
201+ flag_gems . add_ ( bias [i ], bias_grad )
205202 else :
206- bias [i ]. copy_ ( bias_grad )
203+ flag_gems . copy_ ( bias [i ], bias_grad )
207204
208205 if not single_output :
209206 # Store output
210207 if accumulate :
211- D [i ]. add_ ( out .to (D [i ].dtype ))
208+ flag_gems . add_ ( D [i ], out .to (D [i ].dtype ))
212209 else :
213- D [i ]. copy_ ( out .to (D [i ].dtype ))
210+ flag_gems . copy_ ( D [i ], out .to (D [i ].dtype ))
214211 else :
215212 temp_D .append (out .to (D [0 ].dtype ))
216213
217214 if single_output :
218215 if temp_D :
219- temp = torch .cat (temp_D , dim = 0 )
220- D [0 ]. copy_ ( temp )
216+ temp = flag_gems .cat (temp_D , dim = 0 )
217+ flag_gems . copy_ ( D [0 ], temp )
221218
222219 return bias
0 commit comments