Skip to content

Commit e39576d

Browse files
authored
Merge branch 'main' into feature/gpu_set_layout&gpu_alloc
2 parents d5be4a9 + fd3ea29 commit e39576d

8 files changed

Lines changed: 797 additions & 103 deletions

File tree

python/triton/experimental/tle/language/gpu/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ def wgmma(
841841
trans_b = _require_wgmma_bool(trans_b, "trans_b")
842842
mthreads_enabled = mthreads_common.enabled()
843843
if mthreads_enabled:
844-
mthreads_wgmma.validate_operands(a, b, acc, trans_a, trans_b)
844+
a, b = mthreads_wgmma.prepare_operands(a, b, acc, trans_a, trans_b, _semantic)
845845
else:
846846
a, b = _canonicalize_wgmma_operands(a, b, trans_a, trans_b, _semantic)
847847

python/triton/experimental/tle/language/gpu/mthreads/wgmma.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import triton.language as tl
2222

23+
from .. import types as tle
24+
2325

2426
def _unwrap(value):
2527
return value.value if isinstance(value, tl.constexpr) else value
@@ -36,9 +38,7 @@ def mark_auto_shared_layout(builder, handle) -> None:
3638
builder.mark_musa_tle_auto_shared_layout(handle)
3739

3840

39-
def validate_operands(a, b, acc, trans_a: bool, trans_b: bool) -> None:
40-
if trans_a or trans_b:
41-
raise ValueError("initial mthreads TLE wgmma does not support trans_a/trans_b")
41+
def validate_operands(a, b, acc) -> None:
4242
for name, operand in (("a", a), ("b", b)):
4343
operand_type = getattr(operand, "type", None)
4444
if not hasattr(operand_type, "storage"):
@@ -56,6 +56,38 @@ def validate_operands(a, b, acc, trans_a: bool, trans_b: bool) -> None:
5656
raise ValueError("mthreads TLE wgmma requires an f32 accumulator")
5757

5858

59+
def _transpose_smem_operand(operand, semantic):
60+
order = [1, 0]
61+
handle = semantic.builder.create_memdesc_trans(operand.handle, order)
62+
shape = [operand.type.shape[index] for index in order]
63+
64+
alloc_shape = operand.type.alloc_shape
65+
leading_rank = len(alloc_shape) - len(operand.type.shape)
66+
alloc_tail = alloc_shape[leading_rank:]
67+
transposed_alloc_shape = alloc_shape[:leading_rank] + [alloc_tail[index] for index in order]
68+
69+
layout = operand.type.layout.make_permute(order)
70+
return tle.buffered_tensor(
71+
handle,
72+
operand.dtype,
73+
shape,
74+
operand.type.storage,
75+
layout,
76+
semantic,
77+
alloc_shape=transposed_alloc_shape,
78+
)
79+
80+
81+
def prepare_operands(a, b, acc, trans_a: bool, trans_b: bool, semantic):
82+
"""Validate mthreads SQMMA operands and build descriptor transpose views."""
83+
validate_operands(a, b, acc)
84+
if trans_a:
85+
a = _transpose_smem_operand(a, semantic)
86+
if trans_b:
87+
b = _transpose_smem_operand(b, semantic)
88+
return a, b
89+
90+
5991
def validate_options(max_num_imprecise_acc: int, out_dtype) -> None:
6092
if max_num_imprecise_acc != 0:
6193
raise ValueError("mthreads TLE wgmma requires max_num_imprecise_acc=0")

python/triton/runtime/adjust_kernel_param.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,16 +1337,16 @@ def auto_adjust_block_sizes(nargs, fn, configs, current, config):
13371337
adjust_block_size_dot_m_dim_only(nargs, current, config, tma_m_map, 64) # mthreads
13381338

13391339
if ge_k_map: # tl.dot with general tl.load
1340-
if FLAGTREE_BACKEND == "":
1340+
if FLAGTREE_BACKEND in ("", "ppu"):
13411341
if knobs.autotuning.print:
13421342
print("[AABS] 4. adjust bs in tl.dot with general tl.load")
13431343
adjust_block_size_general_dot_mn_dim(nargs, current, config, ge_k_map, 16)
1344-
if FLAGTREE_BACKEND == "hcu":
1344+
elif FLAGTREE_BACKEND == "hcu":
13451345
if knobs.autotuning.print:
13461346
print("[AABS] 4. adjust bs in tl.dot with general tl.load")
13471347
adjust_block_size_general_dot_mn_dim(nargs, current, config, ge_m_map, 16)
13481348
adjust_block_size_general_dot_mn_dim(nargs, current, config, ge_n_map, 16)
1349-
if FLAGTREE_BACKEND == "sunrise":
1349+
elif FLAGTREE_BACKEND == "sunrise":
13501350
# sunrise min_dot_size = (M=8, N=8, K=16/4) (see sunrise compiler.py
13511351
# min_dot_size). The tl.load shrink path above can lower a BLOCK that
13521352
# also feeds tl.dot below the dot lower bound; bump M/N/K back up to

third_party/mthreads/musa/include/TritonMUSACommon/MemDescUtils.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,44 @@ inline Value materializeReshapedMemDescForTarget(
504504
return {};
505505
}
506506

507+
inline void setInsertionPointAfterSameBlockDep(RewriterBase &rewriter,
508+
Operation *anchor, Value dep) {
509+
rewriter.setInsertionPoint(anchor);
510+
if (Operation *def = dep.getDefiningOp())
511+
if (def->getBlock() == anchor->getBlock() && anchor->isBeforeInBlock(def))
512+
rewriter.setInsertionPointAfter(def);
513+
}
514+
515+
inline void moveMemDescViewChainAfterDef(ArrayRef<Operation *> directUsers,
516+
Operation *def) {
517+
Block *block = def->getBlock();
518+
SmallVector<Operation *> chain;
519+
SmallPtrSet<Operation *, 8> seen;
520+
SmallVector<Operation *> worklist(directUsers.begin(), directUsers.end());
521+
while (!worklist.empty()) {
522+
Operation *op = worklist.pop_back_val();
523+
if (!seen.insert(op).second)
524+
continue;
525+
if (op->getBlock() != block)
526+
continue;
527+
if (!op->hasTrait<OpTrait::MemDescViewTrait>())
528+
continue;
529+
chain.push_back(op);
530+
llvm::append_range(worklist, op->getUsers());
531+
}
532+
if (chain.empty())
533+
return;
534+
llvm::sort(chain,
535+
[](Operation *a, Operation *b) { return a->isBeforeInBlock(b); });
536+
Operation *anchor = def;
537+
for (Operation *op : chain) {
538+
if (anchor->isBeforeInBlock(op))
539+
continue;
540+
op->moveAfter(anchor);
541+
anchor = op;
542+
}
543+
}
544+
507545
inline bool replaceTensorLocalAllocWithMemDesc(RewriterBase &rewriter,
508546
Operation *user,
509547
Value sourceMemDesc) {
@@ -514,13 +552,17 @@ inline bool replaceTensorLocalAllocWithMemDesc(RewriterBase &rewriter,
514552
if (!targetTy)
515553
return false;
516554
OpBuilder::InsertionGuard guard(rewriter);
517-
rewriter.setInsertionPoint(localAlloc);
555+
setInsertionPointAfterSameBlockDep(rewriter, localAlloc, sourceMemDesc);
518556
Value replacement =
519557
adaptMemDescValue(rewriter, localAlloc.getLoc(), sourceMemDesc, targetTy,
520558
localAlloc.getOperation());
521559
if (!replacement)
522560
return false;
561+
SmallVector<Operation *> users(localAlloc->getUsers().begin(),
562+
localAlloc->getUsers().end());
523563
rewriter.replaceOp(localAlloc, replacement);
564+
if (Operation *def = replacement.getDefiningOp())
565+
moveMemDescViewChainAfterDef(users, def);
524566
return true;
525567
}
526568

@@ -617,7 +659,7 @@ inline bool tryReplaceTensorUserWithMemDesc(RewriterBase &rewriter,
617659
if (!targetTy)
618660
continue;
619661
OpBuilder::InsertionGuard guard(rewriter);
620-
rewriter.setInsertionPoint(localAlloc);
662+
setInsertionPointAfterSameBlockDep(rewriter, localAlloc, sourceMemDesc);
621663
Value replacement = materializeTransformedMemDescForTarget(
622664
rewriter, transOp, sourceMemDesc, targetTy,
623665
localAlloc.getOperation());
@@ -640,7 +682,7 @@ inline bool tryReplaceTensorUserWithMemDesc(RewriterBase &rewriter,
640682
if (!targetTy)
641683
continue;
642684
OpBuilder::InsertionGuard guard(rewriter);
643-
rewriter.setInsertionPoint(localAlloc);
685+
setInsertionPointAfterSameBlockDep(rewriter, localAlloc, sourceMemDesc);
644686
Value replacement = materializeReshapedMemDescForTarget(
645687
rewriter, reshapeOp, sourceMemDesc, targetTy,
646688
localAlloc.getOperation());

0 commit comments

Comments
 (0)