@@ -74,6 +74,7 @@ def gmm(
7474 qwix_rule : qwix .QtRule | None = None ,
7575 use_manual_quantization : bool = False , # used in batchsplit
7676 use_gmm_v2 : bool = False ,
77+ partial_sum : jnp .ndarray | None = None ,
7778):
7879 """Grouped matrix multiplication operation."""
7980 quantization_rule = None
@@ -112,6 +113,7 @@ def gmm(
112113 lhs_vma_axes ,
113114 rhs_vma_axes ,
114115 use_gmm_v2 ,
116+ partial_sum ,
115117 )
116118
117119
@@ -142,6 +144,7 @@ def _gmm_fwd(
142144 lhs_vma_axes : tuple = tuple (),
143145 rhs_vma_axes : tuple = tuple (),
144146 use_gmm_v2 : bool = False ,
147+ partial_sum : jnp .ndarray | None = None ,
145148) -> tuple [
146149 jnp .ndarray ,
147150 tuple [
@@ -200,6 +203,7 @@ def _gmm_fwd(
200203 tile_n = tiling [2 ],
201204 ),
202205 preferred_element_type = preferred_element_type ,
206+ partial_sum = partial_sum ,
203207 )
204208 elif use_tokamax_backend :
205209 # manual_axis_type is for gmm with shard_map check_vma=True, needs tokamax > 0.0.12
@@ -234,7 +238,7 @@ def _gmm_fwd(
234238 for axis in lhs_vma_axes :
235239 out = jax .lax .pcast (out , axis_name = axis , to = "varying" )
236240
237- return out , (lhs , rhs , group_sizes , group_offset )
241+ return out , (lhs , rhs , group_sizes , group_offset , partial_sum )
238242
239243
240244def _gmm_bwd (
@@ -256,12 +260,13 @@ def _gmm_bwd(
256260 jnp .ndarray | qpl .QArray ,
257261 jnp .ndarray ,
258262 jnp .ndarray | None ,
263+ jnp .ndarray | None ,
259264 ],
260265 grad : jnp .ndarray ,
261- ) -> tuple [jnp .ndarray , jnp .ndarray , None , None , jnp .ndarray ]:
266+ ) -> tuple [jnp .ndarray , jnp .ndarray , None , None , jnp .ndarray , jnp . ndarray | None ]:
262267 """Backward function for throughput GMM VJP."""
263268 del preferred_element_type
264- lhs , rhs , group_sizes , group_offset = residual
269+ lhs , rhs , group_sizes , group_offset , partial_sum_fwd = residual
265270 num_actual_groups = rhs .shape [0 ]
266271
267272 # Jargon used here:
@@ -406,4 +411,5 @@ def _gmm_bwd(
406411 #
407412 # TODO(tgale, enriqueps, apaske): Fuse this transposition into the tgmm.
408413 drhs = drhs .swapaxes (1 , 2 ) if transpose_rhs else drhs
409- return dlhs , drhs , None , None , grad
414+ dpartial_sum = grad if partial_sum_fwd is not None else None
415+ return dlhs , drhs , None , None , grad , dpartial_sum
0 commit comments