Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6886446
refactor(rocke): extract _emit_direct_params_tuple from 9 copies in c…
AviralGoelAMD Oct 20, 2026
807b967
refactor(rocke): extract _implicit_gemm_output_config from 4 copies (…
AviralGoelAMD Oct 20, 2026
c554985
refactor(rocke): extract _iu8_gemm_lane_and_acc from 2 copies into _w…
AviralGoelAMD Oct 20, 2026
42dfba0
refactor(rocke): extract _online_softmax_update from 3 copies in _fmh…
AviralGoelAMD Oct 20, 2026
ad8ca8a
refactor(rocke): extract _decode_staged_slot from 3 copies in loads.p…
AviralGoelAMD Oct 20, 2026
526e5f2
refactor(rocke): extract _issue_dram_load from 3 copies in conv_direc…
AviralGoelAMD Oct 20, 2026
c6dcbb0
Revert "refactor(rocke): extract _issue_dram_load from 3 copies in co…
AviralGoelAMD Oct 20, 2026
80a4037
refactor(rocke): extract _implicit_gemm_warp_grid from 3 copies into …
AviralGoelAMD Oct 20, 2026
6b3fa4f
Revert "refactor(rocke): extract _implicit_gemm_warp_grid from 3 copi…
AviralGoelAMD Oct 20, 2026
a9cab2b
refactor(rocke): extract _declare_fmha_fwd_params from 2 copies in _f…
AviralGoelAMD Oct 20, 2026
9bea3fb
refactor(rocke): extract _c_lane_rows_cols from 3 copies into _conv_i…
AviralGoelAMD Oct 20, 2026
1d29847
refactor(rocke): extract _wmma_lane_tile_setup from int8's copy in _w…
AviralGoelAMD Oct 20, 2026
41d9e2a
refactor(rocke): route _iu8_gemm_lane_and_acc through _wmma_lane_tile…
AviralGoelAMD Oct 20, 2026
93b2fd0
refactor(rocke): extract _sparse_builder_setup from 2 copies in spars…
AviralGoelAMD Oct 20, 2026
b17ce7e
refactor(rocke): extract _gemm_universal_c_lane from 2 copies in gemm…
AviralGoelAMD Oct 20, 2026
1b1903c
refactor(rocke): extract _gemm_universal_c_lane from 2 copies in gemm…
AviralGoelAMD Oct 20, 2026
3c21a2e
refactor(rocke): extract _direct_dgrad_params_read from 2 copies in c…
AviralGoelAMD Oct 20, 2026
54dd61d
refactor(rocke): extract _implicit_gemm_c_ctx from 2 copies into _con…
AviralGoelAMD Oct 20, 2026
4be571f
Revert "refactor(rocke): extract _implicit_gemm_c_ctx from 2 copies i…
AviralGoelAMD Oct 20, 2026
f97b4a5
refactor(rocke): extract _implicit_gemm_buffer_rsrcs from 3 copies (c…
AviralGoelAMD Oct 20, 2026
da90862
Revert "refactor(rocke): extract _implicit_gemm_buffer_rsrcs from 3 c…
AviralGoelAMD Oct 20, 2026
33535a7
refactor(rocke): extract _dgrad_tilde_decomp from 2 copies in conv_im…
AviralGoelAMD Oct 20, 2026
46c8f32
refactor(rocke): extract _dgrad_tilde_decomp from 2 copies in conv_im…
AviralGoelAMD Oct 20, 2026
bfc561e
Revert "refactor(rocke): extract _dgrad_tilde_decomp from 2 copies in…
AviralGoelAMD Oct 20, 2026
aef89f5
refactor(rocke): extract _direct_conv_tid_decode from 3 copies in con…
AviralGoelAMD Oct 20, 2026
0cd25e4
Revert "refactor(rocke): extract _direct_conv_tid_decode from 3 copie…
AviralGoelAMD Oct 20, 2026
37a2efd
refactor(rocke): extract _conv_lds_dims and _direct_conv_lane_setup f…
AviralGoelAMD Oct 20, 2026
3f2a695
refactor(rocke): extract _direct_wgrad_params from 2 copies in conv_d…
AviralGoelAMD Oct 20, 2026
5ba4c97
refactor(rocke): extract _conv_io_buffer_rsrcs from implicit-gemm fwd…
AviralGoelAMD Oct 20, 2026
7e98b3e
refactor(rocke): extract _fma_tap_load from conv_direct_grouped.py (c…
AviralGoelAMD Oct 20, 2026
c8f3691
refactor(rocke): extract _direct_conv_builder from 4 copies in conv_d…
AviralGoelAMD Oct 20, 2026
75f9ded
refactor(rocke): factor _conv_lds_base out of _conv_lds_dims and call…
AviralGoelAMD Oct 20, 2026
3462803
refactor(rocke): extract _direct_store_offset and _reorg_src_dst_off …
AviralGoelAMD Oct 20, 2026
6ec0c35
refactor(rocke): extract _direct_flush_acc from 3 copies in conv_dire…
AviralGoelAMD Oct 20, 2026
824545b
refactor(rocke): extract _direct_conv_h_loop_setup from 3 copies in c…
AviralGoelAMD Oct 20, 2026
4454298
refactor(rocke): extract _direct_conv_d_desc_and_acc_args from 3 copi…
AviralGoelAMD Oct 20, 2026
4052312
refactor(rocke): extract _direct_conv_constants from 3 copies in conv…
AviralGoelAMD Oct 20, 2026
5540fdf
Revert "refactor(rocke): extract _direct_conv_d_desc_and_acc_args fro…
AviralGoelAMD Oct 20, 2026
9d91c10
refactor(rocke): extract _direct_dgrad_params from 2 copies in conv_d…
AviralGoelAMD Oct 20, 2026
1ef4b96
refactor(rocke): extract _sparse_q_setup from 2 copies in sparse_atte…
AviralGoelAMD Oct 20, 2026
21d961d
refactor(rocke): extract _direct_conv_q_constants from 3 copies in co…
AviralGoelAMD Oct 20, 2026
68dc06c
refactor(rocke): extract _direct_conv_lane_head from 2 copies in conv…
AviralGoelAMD Oct 20, 2026
cf11d37
refactor(rocke): merge _direct_conv_h_loop_args over 3 copies in conv…
AviralGoelAMD Oct 20, 2026
886a865
refactor(rocke): extract _direct_conv_lane_s from 2 copies in conv_di…
AviralGoelAMD Oct 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Value,
)
from rocke.helpers.spec import choose_load_vec
from rocke.helpers.tensor_view import make_buffer_resource
from rocke.helpers.transforms import (
TensorDescriptor,
DynamicTensorDescriptor,
Expand Down Expand Up @@ -1309,6 +1310,38 @@ def make_d_descriptor_dynamic(
).transform(*transforms)


def _implicit_gemm_output_config(op, spec):
"""Shared output layout for the implicit-GEMM epilogue copies."""
c_map = op.c_layout()
_fp32_out = spec.data.dtype_d == "fp32"
_bf16_out = spec.data.dtype_d == "bf16"
_elem_bytes = 4 if _fp32_out else 2
flat = 0
return c_map, _fp32_out, _bf16_out, _elem_bytes, flat


def _c_lane_rows_cols(b, atom, lane, kc_m1, kc_nlane, c_per_lane):
"""Decode the per-lane (row, col) pairs for a warp tile's C accumulator slots."""
from rocke.helpers.atoms import make_c_warp_dstr_encoding
from rocke.helpers.distribution import make_static_tile_distribution

c_dist = make_static_tile_distribution(make_c_warp_dstr_encoding(atom))

c_nlane = b.const_i32(kc_nlane)
n_in_atom = b.mod(lane, c_nlane)
m_blk = b.div(lane, c_nlane)
p_lane = [m_blk, n_in_atom]

rows: List[Value] = []
cols: List[Value] = []
for i in range(c_per_lane):
ys = [b.const_i32(i // kc_m1), b.const_i32(i % kc_m1)]
x_row, x_col = c_dist.calculate_x(b, ys=ys, ps=[p_lane])
rows.append(x_row)
cols.append(x_col)
return rows, cols


def emit_direct_epilogue_wmma(
b,
spec,
Expand All @@ -1335,12 +1368,9 @@ def emit_direct_epilogue_wmma(
mfmas_n = spec.mfmas_per_warp_n
warp_m_off = b.mul(warp_m_idx, b.const_i32(mfmas_m * spec.warp_tile_m))
warp_n_off = b.mul(warp_n_idx, b.const_i32(mfmas_n * spec.warp_tile_n))
c_map = op.c_layout()
_fp32_out = spec.data.dtype_d == "fp32"
_bf16_out = spec.data.dtype_d == "bf16"
_elem_bytes = 4 if _fp32_out else 2

flat = 0
c_map, _fp32_out, _bf16_out, _elem_bytes, flat = _implicit_gemm_output_config(
op, spec
)
for mi in range(mfmas_m):
for ni in range(mfmas_n):
acc = accs[flat]
Expand Down Expand Up @@ -1373,3 +1403,16 @@ def emit_direct_epilogue_wmma(
)
else:
b.buffer_store_f16(d_rsrc, safe_off, c0, b.trunc_f32_to_f16(v_f32))


def _conv_io_buffer_rsrcs(b, a, a_bytes, b_ptr, b_bytes, d, d_bytes):
"""Buffer-resource views over the conv IO tensors. Returns
``(a_rsrc, b_rsrc, d_rsrc)`` — the raw :class:`BufferResource` handles.
"""
a_buf_rsrc = make_buffer_resource(b, a, num_bytes=a_bytes)
b_buf_rsrc = make_buffer_resource(b, b_ptr, num_bytes=b_bytes)
d_buf_rsrc = make_buffer_resource(b, d, num_bytes=d_bytes)
a_rsrc = a_buf_rsrc.rsrc
b_rsrc = b_buf_rsrc.rsrc
d_rsrc = d_buf_rsrc.rsrc
return a_rsrc, b_rsrc, d_rsrc
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,26 @@ def _row_base(self, tensor_name: str, tok: Value, hd: Value) -> Value:
self.b.mul(tok, tok_stride),
self.b.mul(hd, head_stride),
)


def _declare_fmha_fwd_params(kb):
"""Declare the shared FMHA-forward kernel ABI: tensors, scalars, strides."""
kb.add_tensor("Q", readonly=True)
kb.add_tensor("K", readonly=True)
kb.add_tensor("V", readonly=True)
kb.add_tensor("O", readonly=False, writeonly=True)
kb.add_scalar("scale_log2", "f32")
kb.add_scalar("seqlen_q", "i32")
kb.add_scalar("seqlen_k", "i32")
kb.add_strides("q", "k", "v", "o")


def _sparse_builder_setup(kb):
"""Decode the grid and read the attention tensor handles for sparse builders."""
kb.decode_grid()
b = kb.builder
Q = kb.tensor("Q")
K = kb.tensor("K")
V = kb.tensor("V")
O = kb.tensor("O") # noqa: E741 - standard attention notation
return b, Q, K, V, O
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@
WARP_SIZE = 64


def _online_softmax_update(b, partial, scale_log2, k_idx, m, lse, apply_mask_and_score):
"""Warp-reduce the partial QK dot, mask/score it, then do the online-softmax update."""
dot = warp_xor_reduce_sum(b, partial, stages=6)
score_log2 = apply_mask_and_score(b.fmul(dot, scale_log2), k_idx)
m_new = b.fmax(m, score_log2)
alpha = b.exp2(b.fsub(m, m_new))
p = b.exp2(b.fsub(score_log2, m_new))
lse_new = b.fadd(b.fmul(lse, alpha), p)
return m_new, alpha, p, lse_new


def fmha_warp_fwd_inner_body(
b: IRBuilder,
*,
Expand Down Expand Up @@ -272,13 +283,9 @@ def _apply_mask_and_score(score_log2: Value, k_idx: Value) -> Value:
for slot in range(ept):
partial = b.fadd(partial, b.fmul(q_f32_list[slot], k_f32_list[slot]))

dot = warp_xor_reduce_sum(b, partial, stages=6)
score_log2 = _apply_mask_and_score(b.fmul(dot, scale_log2), k_idx)

m_new = b.fmax(m, score_log2)
alpha = b.exp2(b.fsub(m, m_new))
p = b.exp2(b.fsub(score_log2, m_new))
lse_new = b.fadd(b.fmul(lse, alpha), p)
m_new, alpha, p, lse_new = _online_softmax_update(
b, partial, scale_log2, k_idx, m, lse, _apply_mask_and_score
)

new_yields = [m_new, lse_new]
for slot in range(ept):
Expand Down Expand Up @@ -328,13 +335,9 @@ def _apply_mask_and_score(score_log2: Value, k_idx: Value) -> Value:
partial = b.fmul(q_scalar, kd)

# Warp-wide butterfly reduce: 64-lane wave64 sum = 6 stages.
dot = warp_xor_reduce_sum(b, partial, stages=6)
score_log2 = _apply_mask_and_score(b.fmul(dot, scale_log2), k_idx)

m_new = b.fmax(m, score_log2)
alpha = b.exp2(b.fsub(m, m_new))
p = b.exp2(b.fsub(score_log2, m_new))
lse_new = b.fadd(b.fmul(lse, alpha), p)
m_new, alpha, p, lse_new = _online_softmax_update(
b, partial, scale_log2, k_idx, m, lse, _apply_mask_and_score
)
new_acc0 = b.fadd(b.fmul(acc0, alpha), b.fmul(p, vd))
b.scf_yield(m_new, lse_new, new_acc0)

Expand Down Expand Up @@ -389,13 +392,9 @@ def _apply_mask_and_score(score_log2: Value, k_idx: Value) -> Value:
partial = b.vector_sum(b.vector_mul(q_vec_f32, k_vec_f32))

# Warp-wide butterfly reduce: 64-lane wave64 sum = 6 stages.
dot = warp_xor_reduce_sum(b, partial, stages=6)
score_log2 = _apply_mask_and_score(b.fmul(dot, scale_log2), k_idx)

m_new = b.fmax(m, score_log2)
alpha = b.exp2(b.fsub(m, m_new))
p = b.exp2(b.fsub(score_log2, m_new))
lse_new = b.fadd(b.fmul(lse, alpha), p)
m_new, alpha, p, lse_new = _online_softmax_update(
b, partial, scale_log2, k_idx, m, lse, _apply_mask_and_score
)

# PV accumulator update: acc = acc * alpha + p * v.
# The splats reduce to a single VGPR broadcast each on AMDGPU
Expand Down
Loading
Loading