Skip to content

Commit 9643733

Browse files
Merge pull request AI-Hypercomputer#4262 from AI-Hypercomputer:chengnuojin-ragged-followup
PiperOrigin-RevId: 940019978
2 parents 415ef37 + bd32f80 commit 9643733

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/maxtext/kernels/ragged/ragged_gather_reduce.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def _preprocess(
385385
reduce_group_size: int,
386386
num_row_partitions: int,
387387
num_simd_lanes: int,
388-
) -> tuple[jax.Array, jax.Array, jax.Array, jax.Array]:
388+
) -> tuple[jax.Array, jax.Array, jax.Array, jax.Array, jax.Array]:
389389
"""Preprocesses indices for ragged gather reduce."""
390390
assert indices.ndim == 1, "Ragged scatter only supports 1d indices."
391391

@@ -416,12 +416,16 @@ def _preprocess(
416416
num_src_rows_per_row_partition.astype(jnp.int32),
417417
(0, num_simd_lanes - num_row_partitions),
418418
)
419+
# If there is no valid source row in a reduce group, we set the mask to
420+
# False, so that the output for that group is set to zero.
421+
mask = jnp.any(valid_rows_mask.reshape(-1, reduce_group_size), axis=-1)
419422

420423
return (
421424
src_indices,
422425
dst_indices,
423426
topk_weights,
424427
num_src_rows_per_row_partition,
428+
mask,
425429
)
426430

427431

@@ -481,11 +485,6 @@ def ragged_gather_reduce(
481485
# Heuristic threshold on whether to fallback for small inputs.
482486
dtype = x.dtype
483487
dtype_bytes = jax.dtypes.itemsize_bits(dtype) // 8
484-
if jnp.size(x) * dtype_bytes * 2 < pltpu.get_tpu_info().vmem_capacity_bytes * 0.6:
485-
# For small {input + output}, it's likely that both can be put in TC VMEM,
486-
# so it's likely faster to run TC-based implementation on it than going
487-
# through SC, without data movement to/from HBM.
488-
return _fallback_implementation(x, indices, topk_weights, valid_rows_mask, reduce_group_size)
489488

490489
hidden_size = x.shape[-1]
491490
input_size = indices.size
@@ -533,6 +532,7 @@ def ragged_gather_reduce(
533532
dst_indices,
534533
topk_weights,
535534
num_src_rows_per_row_partition,
535+
mask,
536536
) = _preprocess(
537537
indices,
538538
topk_weights,
@@ -588,4 +588,10 @@ def ragged_gather_reduce(
588588
},
589589
)(num_src_rows_per_row_partition, x, src_indices, dst_indices, topk_weights)
590590

591-
return out.astype(x.dtype)
591+
# If there is no valid source row in a reduce group, set that group's output
592+
# to zero.
593+
return jnp.where(
594+
mask[:, None],
595+
out.astype(x.dtype),
596+
jnp.zeros_like(out, dtype=x.dtype),
597+
)[: (input_size // reduce_group_size), :hidden_size]

src/maxtext/kernels/ragged/ragged_sort.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def _ring_ragged_sort_bwd(res, g_out):
186186
valid_rows_mask=valid_rows_mask,
187187
reduce_group_size=topk,
188188
enforce_fallback=enforce_gather_reduce_fallback,
189+
flops_override=gather_reduce_flops_override,
190+
bytes_accessed_override=gather_reduce_bytes_accessed_override,
189191
)
190192
else:
191193
# Buffering: g_x has size `local_buffer_size` (packed).
@@ -209,6 +211,8 @@ def _ring_ragged_sort_bwd(res, g_out):
209211
valid_rows_mask=valid_rows_mask,
210212
reduce_group_size=topk,
211213
enforce_fallback=enforce_gather_reduce_fallback,
214+
flops_override=gather_reduce_flops_override,
215+
bytes_accessed_override=gather_reduce_bytes_accessed_override,
212216
)
213217
return grad_hidden_states, None
214218

@@ -303,6 +307,8 @@ def _ring_ragged_unsort_fwd(sorted_tokens_local, group_sizes_local, topk_argsort
303307
valid_rows_mask=valid_rows_mask,
304308
reduce_group_size=topk,
305309
enforce_fallback=enforce_gather_reduce_fallback,
310+
flops_override=gather_reduce_flops_override,
311+
bytes_accessed_override=gather_reduce_bytes_accessed_override,
306312
)
307313
else:
308314
# Shift indices so they map to the packed local buffer [0, local_num_tokens).
@@ -319,6 +325,8 @@ def _ring_ragged_unsort_fwd(sorted_tokens_local, group_sizes_local, topk_argsort
319325
valid_rows_mask=valid_rows_mask,
320326
reduce_group_size=topk,
321327
enforce_fallback=enforce_gather_reduce_fallback,
328+
flops_override=gather_reduce_flops_override,
329+
bytes_accessed_override=gather_reduce_bytes_accessed_override,
322330
)
323331

324332
res = (
@@ -375,6 +383,8 @@ def _ring_ragged_unsort_bwd(res, g_out):
375383
weights=weight_for_sorted,
376384
has_weights=True,
377385
enforce_fallback=enforce_gather_fallback,
386+
flops_override=gather_flops_override,
387+
bytes_accessed_override=gather_bytes_accessed_override,
378388
)
379389
else:
380390
# Slice the inverse permutation to match the packed local buffer.
@@ -392,6 +402,8 @@ def _ring_ragged_unsort_bwd(res, g_out):
392402
weights=sliced_weights,
393403
has_weights=True,
394404
enforce_fallback=enforce_gather_fallback,
405+
flops_override=gather_flops_override,
406+
bytes_accessed_override=gather_bytes_accessed_override,
395407
)
396408
return grad_sorted_tokens, None, None, None
397409

0 commit comments

Comments
 (0)