Skip to content

Commit d9882fa

Browse files
stashuk-olekfacebook-github-bot
authored andcommitted
Widen Triton TBE forward gather issue (#4490)
Summary: Issue up to eight independent index gathers per loop iteration on profitable long-bag shapes. The wider loop increases memory-level parallelism while retaining the four-wide path for shapes where register pressure dominates. Reviewed By: axeisghost Differential Revision: D114280947
1 parent 9023ffe commit d9882fa

1 file changed

Lines changed: 79 additions & 1 deletion

File tree

torchrec/distributed/triton_tbe/triton_table_batched_embeddings.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ def table_batched_embedding_bag_forward_unweighted_kernel(
275275
FEATURE_START: tl.constexpr = 0,
276276
FEATURE_END: tl.constexpr = -1,
277277
BAGS_PER_PROGRAM: tl.constexpr = 1,
278+
UNROLL8: tl.constexpr = False,
278279
FUSED_BOUNDS_CHECK: tl.constexpr = False,
279280
) -> None:
280281
base_b = tl.program_id(0).to(tl.int64) * BAGS_PER_PROGRAM
@@ -317,7 +318,7 @@ def table_batched_embedding_bag_forward_unweighted_kernel(
317318
)
318319
bag_output = tl.zeros((BLOCK_SIZE,), dtype=accumulator_dtype)
319320

320-
step: tl.constexpr = 4
321+
step: tl.constexpr = 8 if UNROLL8 else 4
321322
ns = (end - start) // step
322323
endn = start + step * ns
323324

@@ -357,6 +358,42 @@ def table_batched_embedding_bag_forward_unweighted_kernel(
357358
+ invalid_2.to(tl.int32)
358359
+ invalid_3.to(tl.int32)
359360
)
361+
if UNROLL8:
362+
row_idx_4, invalid_4 = _load_checked_index(
363+
indices_ptr,
364+
idx + 4,
365+
num_rows if FUSED_BOUNDS_CHECK else 0,
366+
True,
367+
FUSED_BOUNDS_CHECK,
368+
)
369+
row_idx_5, invalid_5 = _load_checked_index(
370+
indices_ptr,
371+
idx + 5,
372+
num_rows if FUSED_BOUNDS_CHECK else 0,
373+
True,
374+
FUSED_BOUNDS_CHECK,
375+
)
376+
row_idx_6, invalid_6 = _load_checked_index(
377+
indices_ptr,
378+
idx + 6,
379+
num_rows if FUSED_BOUNDS_CHECK else 0,
380+
True,
381+
FUSED_BOUNDS_CHECK,
382+
)
383+
row_idx_7, invalid_7 = _load_checked_index(
384+
indices_ptr,
385+
idx + 7,
386+
num_rows if FUSED_BOUNDS_CHECK else 0,
387+
True,
388+
FUSED_BOUNDS_CHECK,
389+
)
390+
if FUSED_BOUNDS_CHECK:
391+
warning_count += (
392+
invalid_4.to(tl.int32)
393+
+ invalid_5.to(tl.int32)
394+
+ invalid_6.to(tl.int32)
395+
+ invalid_7.to(tl.int32)
396+
)
360397
row_0 = tl.load(
361398
weight_ptr
362399
+ table_offset
@@ -389,12 +426,52 @@ def table_batched_embedding_bag_forward_unweighted_kernel(
389426
mask=mask,
390427
other=0,
391428
)
429+
if UNROLL8:
430+
row_4 = tl.load(
431+
weight_ptr
432+
+ table_offset
433+
+ row_idx_4 * embedding_dim
434+
+ col_offsets,
435+
mask=mask,
436+
other=0,
437+
)
438+
row_5 = tl.load(
439+
weight_ptr
440+
+ table_offset
441+
+ row_idx_5 * embedding_dim
442+
+ col_offsets,
443+
mask=mask,
444+
other=0,
445+
)
446+
row_6 = tl.load(
447+
weight_ptr
448+
+ table_offset
449+
+ row_idx_6 * embedding_dim
450+
+ col_offsets,
451+
mask=mask,
452+
other=0,
453+
)
454+
row_7 = tl.load(
455+
weight_ptr
456+
+ table_offset
457+
+ row_idx_7 * embedding_dim
458+
+ col_offsets,
459+
mask=mask,
460+
other=0,
461+
)
392462
bag_output += (
393463
row_0.to(tl.float32)
394464
+ row_1.to(tl.float32)
395465
+ row_2.to(tl.float32)
396466
+ row_3.to(tl.float32)
397467
)
468+
if UNROLL8:
469+
bag_output += (
470+
row_4.to(tl.float32)
471+
+ row_5.to(tl.float32)
472+
+ row_6.to(tl.float32)
473+
+ row_7.to(tl.float32)
474+
)
398475

399476
for idx in range(endn, end):
400477
row_idx, invalid = _load_checked_index(
@@ -1587,6 +1664,7 @@ def forward(
15871664
FEATURE_START=feature_start,
15881665
FEATURE_END=feature_end,
15891666
BAGS_PER_PROGRAM=bags_per_program,
1667+
UNROLL8=bags_per_program == 2,
15901668
FUSED_BOUNDS_CHECK=fused_bounds_check,
15911669
num_warps=num_warps,
15921670
)

0 commit comments

Comments
 (0)