@@ -138,6 +138,38 @@ def baddbmm_kernel(
138138 tl .store (o_ptrs , o , mask = mask_c )
139139
140140
141+ def _baddbmm_launch (bias , A , B , beta , alpha , out ):
142+ batch , M , K = A .shape
143+ _ , _ , N = B .shape
144+ A = A .contiguous ()
145+ B = B .contiguous ()
146+ bbias = torch .broadcast_to (bias , (batch , M , N )).contiguous ()
147+ bias_batch_stride = bbias .stride (0 )
148+ bias_M_stride = bbias .stride (1 )
149+ bias_N_stride = bbias .stride (- 1 )
150+
151+ grid = lambda meta : (
152+ triton .cdiv (meta ["M" ], meta ["TILE_M" ]),
153+ triton .cdiv (meta ["N" ], meta ["TILE_N" ]),
154+ batch ,
155+ )
156+ with torch_device_fn .device (A .device ):
157+ baddbmm_kernel [grid ](
158+ A ,
159+ B ,
160+ out ,
161+ bbias ,
162+ alpha ,
163+ beta ,
164+ M ,
165+ N ,
166+ K ,
167+ bias_batch_stride = bias_batch_stride ,
168+ bias_M_stride = bias_M_stride ,
169+ bias_N_stride = bias_N_stride ,
170+ )
171+
172+
141173class BaddbmmFunction (torch .autograd .Function ):
142174 @staticmethod
143175 def forward (ctx , bias , A , B , beta , alpha ):
@@ -149,36 +181,8 @@ def forward(ctx, bias, A, B, beta, alpha):
149181
150182 batch , M , K = A .shape
151183 _ , _ , N = B .shape
152- A = A .contiguous ()
153- B = B .contiguous ()
154184 out = torch .empty ((batch , M , N ), dtype = A .dtype , device = A .device )
155-
156- bbias = torch .broadcast_to (bias , (batch , M , N )).contiguous ()
157- bias_batch_stride = bbias .stride (0 )
158- bias_M_stride = bbias .stride (1 )
159- bias_N_stride = bbias .stride (- 1 )
160-
161- grid = lambda meta : (
162- triton .cdiv (meta ["M" ], meta ["TILE_M" ]),
163- triton .cdiv (meta ["N" ], meta ["TILE_N" ]),
164- batch ,
165- )
166- with torch_device_fn .device (A .device ):
167- baddbmm_kernel [grid ](
168- A ,
169- B ,
170- out ,
171- bbias ,
172- alpha ,
173- beta ,
174- M ,
175- N ,
176- K ,
177- bias_batch_stride = bias_batch_stride ,
178- bias_M_stride = bias_M_stride ,
179- bias_N_stride = bias_N_stride ,
180- IS_FP64 = A .dtype == torch .float64 ,
181- )
185+ _baddbmm_launch (bias , A , B , beta , alpha , out )
182186 return out
183187
184188 @staticmethod
@@ -239,6 +243,24 @@ def compute_B_grad(A, d_output, alpha):
239243 return grad_B
240244
241245
246+ def baddbmm_out (bias , A , B , * , beta = 1.0 , alpha = 1.0 , out ):
247+ logger .debug ("GEMS BADDBMM_OUT" )
248+ batch , M , K = A .shape
249+ _ , _ , N = B .shape
250+ assert (
251+ out .shape == (batch , M , N ) and out .dtype == A .dtype
252+ ), "Incompatible output shape or dtype for baddbmm.out"
253+ _baddbmm_launch (
254+ bias .contiguous (),
255+ A .contiguous (),
256+ B .contiguous (),
257+ beta ,
258+ alpha ,
259+ out ,
260+ )
261+ return out
262+
263+
242264def baddbmm (bias , A , B , beta = 1.0 , alpha = 1.0 ):
243265 return BaddbmmFunction .apply (
244266 bias .contiguous (),
0 commit comments