@@ -134,6 +134,38 @@ def baddbmm_kernel(
134134 tl .store (o_ptrs , o , mask = mask_c )
135135
136136
137+ def _baddbmm_launch (bias , A , B , beta , alpha , out ):
138+ batch , M , K = A .shape
139+ _ , _ , N = B .shape
140+ A = A .contiguous ()
141+ B = B .contiguous ()
142+ bbias = torch .broadcast_to (bias , (batch , M , N )).contiguous ()
143+ bias_batch_stride = bbias .stride (0 )
144+ bias_M_stride = bbias .stride (1 )
145+ bias_N_stride = bbias .stride (- 1 )
146+
147+ grid = lambda meta : (
148+ triton .cdiv (meta ["M" ], meta ["TILE_M" ]),
149+ triton .cdiv (meta ["N" ], meta ["TILE_N" ]),
150+ batch ,
151+ )
152+ with torch_device_fn .device (A .device ):
153+ baddbmm_kernel [grid ](
154+ A ,
155+ B ,
156+ out ,
157+ bbias ,
158+ alpha ,
159+ beta ,
160+ M ,
161+ N ,
162+ K ,
163+ bias_batch_stride = bias_batch_stride ,
164+ bias_M_stride = bias_M_stride ,
165+ bias_N_stride = bias_N_stride ,
166+ )
167+
168+
137169class BaddbmmFunction (torch .autograd .Function ):
138170 @staticmethod
139171 def forward (ctx , bias , A , B , beta , alpha ):
@@ -145,35 +177,8 @@ def forward(ctx, bias, A, B, beta, alpha):
145177
146178 batch , M , K = A .shape
147179 _ , _ , N = B .shape
148- A = A .contiguous ()
149- B = B .contiguous ()
150180 out = torch .empty ((batch , M , N ), dtype = A .dtype , device = A .device )
151-
152- bbias = torch .broadcast_to (bias , (batch , M , N )).contiguous ()
153- bias_batch_stride = bbias .stride (0 )
154- bias_M_stride = bbias .stride (1 )
155- bias_N_stride = bbias .stride (- 1 )
156-
157- grid = lambda meta : (
158- triton .cdiv (meta ["M" ], meta ["TILE_M" ]),
159- triton .cdiv (meta ["N" ], meta ["TILE_N" ]),
160- batch ,
161- )
162- with torch_device_fn .device (A .device ):
163- baddbmm_kernel [grid ](
164- A ,
165- B ,
166- out ,
167- bbias ,
168- alpha ,
169- beta ,
170- M ,
171- N ,
172- K ,
173- bias_batch_stride = bias_batch_stride ,
174- bias_M_stride = bias_M_stride ,
175- bias_N_stride = bias_N_stride ,
176- )
181+ _baddbmm_launch (bias , A , B , beta , alpha , out )
177182 return out
178183
179184 @staticmethod
@@ -234,6 +239,24 @@ def compute_B_grad(A, d_output, alpha):
234239 return grad_B
235240
236241
242+ def baddbmm_out (bias , A , B , * , beta = 1.0 , alpha = 1.0 , out ):
243+ logger .debug ("GEMS BADDBMM_OUT" )
244+ batch , M , K = A .shape
245+ _ , _ , N = B .shape
246+ assert (
247+ out .shape == (batch , M , N ) and out .dtype == A .dtype
248+ ), "Incompatible output shape or dtype for baddbmm.out"
249+ _baddbmm_launch (
250+ bias .contiguous (),
251+ A .contiguous (),
252+ B .contiguous (),
253+ beta ,
254+ alpha ,
255+ out ,
256+ )
257+ return out
258+
259+
237260def baddbmm (bias , A , B , beta = 1.0 , alpha = 1.0 ):
238261 return BaddbmmFunction .apply (
239262 bias .contiguous (),
0 commit comments