- Split bridge.py -> ops/quantize.py, ops/layouts.py, ops/gemm_runner.py - Renamed classes: CuTeDSLNvfp4Linear -> Nvfp4Linear, etc. - Moved kernel code to dsv4/kernels/ (gemm, attention, compressor, decode, cuda) - Moved PyTorch bridges to dsv4/ops/ - Moved nn.Module layers to dsv4layers/ - Moved reference implementations to dsv4/reference/ - Moved vendored CUTLASS code to vendored/ - Archived ~190 debug tests to tests/archive/ - Kept ~15 canonical tests in tests/unit/ - Updated all import paths - Added stubs for future components (model/, cache/, loader/) - Updated pyproject.toml: dsv4-inference package name
270 lines
13 KiB
Python
270 lines
13 KiB
Python
"""
|
|
Debug: QK only (no PV) with KV-tile interleaving pipeline.
|
|
Outputs P to GMEM to verify QK+softmax pipeline works.
|
|
n=128, single KV tile, identity softmax.
|
|
"""
|
|
import torch, cutlass, cutlass.cute as cute, cutlass.utils as utils, cutlass.pipeline as pipeline
|
|
from cutlass.cute.nvgpu import cpasync, tcgen05
|
|
from cutlass import Float32, BFloat16, Int32, Boolean, const_expr
|
|
from cutlass.utils import LayoutEnum
|
|
from cutlass.utils.tmem_allocator import find_tmem_tensor_col_offset
|
|
import cuda.bindings.driver as cuda
|
|
import cutlass.torch as ct
|
|
|
|
HEAD_DIM = 64
|
|
|
|
|
|
class QkOnlyTest:
|
|
def __init__(self):
|
|
self.acc_dtype = Float32; self.qk_acc_dtype = Float32
|
|
self.q_dtype = BFloat16; self.o_dtype = BFloat16; self.c_dtype = BFloat16
|
|
self.use_2cta_instrs = False; self.epilog_sync_bar_id = 1
|
|
self.cluster_shape_mn = (1, 1); self.cta_group = tcgen05.CtaGroup.ONE
|
|
self.epilogue_warp_id = (0,1,2,3); self.mma_warp_id = 4; self.tma_warp_id = 5
|
|
self.threads_per_cta = 192; self.num_c_stage = 2
|
|
self.kv_stage = 2; self.q_stage = 1
|
|
|
|
def _setup(self, qk_mma):
|
|
qk_ik = cute.size(qk_mma.shape_mnk, mode=[2])
|
|
self.qk_mma_tiler = (128, 128, qk_ik * 4)
|
|
self.mma_tiler = self.qk_mma_tiler
|
|
self.cluster_layout_vmnk = cute.tiled_divide(cute.make_layout((1,1,1)), (qk_mma.thr_id.shape,))
|
|
self.cta_tile_shape_mnk = (self.qk_mma_tiler[0]//cute.size(qk_mma.thr_id.shape), 128, self.qk_mma_tiler[2])
|
|
self.c_layout = LayoutEnum.ROW_MAJOR
|
|
self.epi_tile = utils.sm100.compute_epilogue_tile_shape(self.cta_tile_shape_mnk, False, self.c_layout, self.o_dtype)
|
|
self.num_ab_stage = 1; self.num_acc_stage = 1
|
|
self.q_smem_s = utils.sm100.make_smem_layout_a(qk_mma, self.qk_mma_tiler, self.q_dtype, self.q_stage)
|
|
self.k_smem_s = utils.sm100.make_smem_layout_b(qk_mma, self.qk_mma_tiler, self.q_dtype, self.kv_stage)
|
|
self.c_smem_s = utils.sm100.make_smem_layout_epi(self.o_dtype, self.c_layout, self.epi_tile, 2)
|
|
|
|
qk_thr = qk_mma.get_slice(0); qk_as = qk_thr.partition_shape_C(self.qk_mma_tiler[:2])
|
|
tStS = qk_thr.make_fragment_C(qk_as)
|
|
|
|
self.tilePlikeFP32 = self.qk_mma_tiler[1] // Float32.width * self.o_dtype.width
|
|
self.tmem_s0_offset = 0; self.tmem_p0_offset = 32
|
|
self.tmem_o0_offset = 0 # Output is at S offset for QK-only
|
|
|
|
tCS = qk_mma.make_fragment_C(cute.append(qk_as, self.num_acc_stage))
|
|
self.num_tmem_alloc_cols = utils.get_num_tmem_alloc_cols([tCS], arch="sm_100")
|
|
|
|
cta = cute.size(qk_mma.thr_id.shape)
|
|
q_s = cute.slice_(self.q_smem_s,(None,None,None,0))
|
|
k_s = cute.slice_(self.k_smem_s,(None,None,None,0))
|
|
self.q_tx_bytes = cute.size_in_bytes(self.q_dtype, q_s) * cta
|
|
self.kv_tx_bytes = cute.size_in_bytes(self.q_dtype, k_s) * cta
|
|
|
|
@cute.jit
|
|
def __call__(self, q, k, c, stream):
|
|
self.q_dtype = q.element_type; self.o_dtype = c.element_type; self.c_dtype = self.o_dtype
|
|
self.a_major = LayoutEnum.from_tensor(q).mma_major_mode()
|
|
self.b_major = LayoutEnum.from_tensor(k).mma_major_mode()
|
|
self.c_layout = LayoutEnum.from_tensor(c)
|
|
|
|
qk_mma = utils.sm100.make_trivial_tiled_mma(
|
|
self.q_dtype, self.q_dtype, self.a_major, self.b_major,
|
|
self.qk_acc_dtype, self.cta_group, (128,128), tcgen05.OperandSource.SMEM)
|
|
self._setup(qk_mma)
|
|
|
|
q_s = cute.slice_(self.q_smem_s,(None,None,None,0))
|
|
k_s = cute.slice_(self.k_smem_s,(None,None,None,0))
|
|
|
|
tma_q,mQ = cute.nvgpu.make_tiled_tma_atom_A(
|
|
utils.sm100.cluster_shape_to_tma_atom_A(self.cluster_shape_mn,qk_mma.thr_id),
|
|
q,q_s,self.qk_mma_tiler,qk_mma,self.cluster_layout_vmnk.shape)
|
|
tma_k,mK = cute.nvgpu.make_tiled_tma_atom_B(
|
|
utils.sm100.cluster_shape_to_tma_atom_B(self.cluster_shape_mn,qk_mma.thr_id),
|
|
k,k_s,self.qk_mma_tiler,qk_mma,self.cluster_layout_vmnk.shape)
|
|
epi_s = cute.select(self.c_smem_s,mode=[0,1])
|
|
tma_c,mC = cpasync.make_tiled_tma_atom(cpasync.CopyBulkTensorTileS2GOp(),c,epi_s,self.epi_tile)
|
|
|
|
self._kernel(qk_mma, tma_q, mQ, tma_k, mK, tma_c, mC,
|
|
self.cluster_layout_vmnk, self.q_smem_s, self.k_smem_s, self.c_smem_s, self.epi_tile
|
|
).launch(grid=(1,1,1), block=[self.threads_per_cta,1,1], stream=stream)
|
|
|
|
@cute.kernel
|
|
def _kernel(self, qk_mma, tma_q, mQ, tma_k, mK, tma_c, mC,
|
|
cl_vmnk, q_smem_s, k_smem_s, c_smem_s, epi_tile):
|
|
warp_idx = cute.arch.make_warp_uniform(cute.arch.warp_idx())
|
|
tidx,_,_ = cute.arch.thread_idx()
|
|
|
|
if warp_idx == self.tma_warp_id:
|
|
cpasync.prefetch_descriptor(tma_q); cpasync.prefetch_descriptor(tma_k)
|
|
cpasync.prefetch_descriptor(tma_c)
|
|
|
|
@cute.struct
|
|
class SS:
|
|
q_bar: cute.struct.MemRange[cutlass.Int64, self.q_stage*2]
|
|
kv_bar: cute.struct.MemRange[cutlass.Int64, self.kv_stage*2]
|
|
s_bar: cute.struct.MemRange[cutlass.Int64, 2]
|
|
acc_bar: cute.struct.MemRange[cutlass.Int64, self.num_acc_stage*2]
|
|
tmem_dealloc: cutlass.Int64; holding: cutlass.Int32
|
|
|
|
smem = utils.SmemAllocator(); st = smem.allocate(SS)
|
|
|
|
qp,qc = pipeline.PipelineTmaUmma.create(
|
|
barrier_storage=st.q_bar.data_ptr(), num_stages=self.q_stage,
|
|
producer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread),
|
|
consumer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread,1),
|
|
tx_count=self.q_tx_bytes, cta_layout_vmnk=cl_vmnk, defer_sync=True
|
|
).make_participants()
|
|
|
|
kvp,kvc = pipeline.PipelineTmaUmma.create(
|
|
barrier_storage=st.kv_bar.data_ptr(), num_stages=self.kv_stage,
|
|
producer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread),
|
|
consumer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread,1),
|
|
tx_count=self.kv_tx_bytes, cta_layout_vmnk=cl_vmnk, defer_sync=True
|
|
).make_participants()
|
|
|
|
s_prod,s_cons = pipeline.PipelineUmmaAsync.create(
|
|
barrier_storage=st.s_bar.data_ptr(), num_stages=1,
|
|
producer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread),
|
|
consumer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread, 32*len(self.epilogue_warp_id))
|
|
).make_participants()
|
|
|
|
softmax_done_bar = pipeline.NamedBarrier(barrier_id=3, num_threads=32 + 32*len(self.epilogue_warp_id))
|
|
|
|
acc_pipe = pipeline.PipelineUmmaAsync.create(
|
|
barrier_storage=st.acc_bar.data_ptr(), num_stages=self.num_acc_stage,
|
|
producer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread),
|
|
consumer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread, len(self.epilogue_warp_id)),
|
|
cta_layout_vmnk=cl_vmnk, defer_sync=True)
|
|
|
|
tmem_bar = pipeline.NamedBarrier(barrier_id=2,
|
|
num_threads=32*len((self.mma_warp_id,*self.epilogue_warp_id)))
|
|
tmem = utils.TmemAllocator(st.holding.ptr, barrier_for_retrieve=tmem_bar,
|
|
allocator_warp_id=self.epilogue_warp_id[0],
|
|
is_two_cta=cute.size(qk_mma.thr_id.shape)==2,
|
|
two_cta_tmem_dealloc_mbar_ptr=st.tmem_dealloc.ptr)
|
|
|
|
pipeline.pipeline_init_arrive(cluster_shape_mn=cl_vmnk, is_relaxed=True)
|
|
|
|
sQ = smem.allocate_tensor(element_type=self.q_dtype, layout=q_smem_s.outer, byte_alignment=128, swizzle=q_smem_s.inner)
|
|
sK = smem.allocate_tensor(element_type=self.q_dtype, layout=k_smem_s.outer, byte_alignment=128, swizzle=k_smem_s.inner)
|
|
sC = smem.allocate_tensor(element_type=self.o_dtype, layout=c_smem_s.outer, byte_alignment=128, swizzle=c_smem_s.inner)
|
|
|
|
gQ = cute.local_tile(mQ, cute.slice_(self.qk_mma_tiler,(None,0,None)), (None,None,None))
|
|
gK = cute.local_tile(mK, cute.slice_(self.qk_mma_tiler,(0,None,None)), (None,None,None))
|
|
gC = cute.local_tile(mC, cute.slice_(self.qk_mma_tiler,(None,None,0)), (None,None,None))
|
|
n_kv_tiles = cute.size(gK, mode=[3])
|
|
|
|
qk_thr = qk_mma.get_slice(0)
|
|
tCgQ = qk_thr.partition_A(gQ); tCgK = qk_thr.partition_B(gK)
|
|
tCgC = qk_thr.partition_C(gC)
|
|
|
|
a_lay = cute.make_layout(cute.slice_(cl_vmnk,(0,0,None,0)).shape)
|
|
tAsQ,tAgQ = cpasync.tma_partition(tma_q,0,a_lay,cute.group_modes(sQ,0,3),cute.group_modes(tCgQ,0,3))
|
|
b_lay = cute.make_layout(cute.slice_(cl_vmnk,(0,None,0,0)).shape)
|
|
tBsK,tBgK = cpasync.tma_partition(tma_k,0,b_lay,cute.group_modes(sK,0,3),cute.group_modes(tCgK,0,3))
|
|
tAgQ = tAgQ[(None,0,None,0)]; tBgK = tBgK[(None,0,None,0)]
|
|
|
|
tCrQ = qk_mma.make_fragment_A(sQ); tCrK = qk_mma.make_fragment_B(sK)
|
|
|
|
qk_as = qk_thr.partition_shape_C(self.qk_mma_tiler[:2])
|
|
tStS = qk_thr.make_fragment_C(qk_as)
|
|
tStS0 = cute.make_tensor(tStS.iterator + self.tmem_s0_offset, tStS.layout)
|
|
|
|
tCtS_fake = qk_mma.make_fragment_C(cute.append(qk_as, self.num_acc_stage))
|
|
|
|
pipeline.pipeline_init_wait(cluster_shape_mn=cl_vmnk)
|
|
|
|
# TMA LOAD
|
|
if warp_idx == self.tma_warp_id:
|
|
qp.reset(); qh = qp.acquire_and_advance()
|
|
cute.copy(tma_q, tAgQ[(None,qh.count)], tAsQ[(None,qh.index)], tma_bar_ptr=qh.barrier)
|
|
qp.tail()
|
|
|
|
kvp.reset(); pk = kvp.try_acquire()
|
|
for kt in cutlass.range(n_kv_tiles, unroll=1):
|
|
kh = kvp.acquire_and_advance(pk)
|
|
cute.copy(tma_k, tBgK[(None,kh.count)], tBsK[(None,kh.index)], tma_bar_ptr=kh.barrier)
|
|
pk = cutlass.Boolean(1)
|
|
kvp.tail()
|
|
|
|
# MMA
|
|
if warp_idx == self.mma_warp_id:
|
|
tmem.wait_for_alloc()
|
|
qc.reset(); qh = qc.wait_and_advance(); qh.release()
|
|
kvc.reset(); pk = kvc.try_wait()
|
|
|
|
acc_st = pipeline.make_pipeline_state(pipeline.PipelineUserType.Producer, self.num_acc_stage)
|
|
acc_pipe.producer_acquire(acc_st)
|
|
|
|
for kt in range(n_kv_tiles):
|
|
kh = kvc.wait_and_advance(pk); pk = cutlass.Boolean(1)
|
|
|
|
# QK only, accumulate across KV tiles
|
|
sh = s_prod.acquire_and_advance()
|
|
qk_mma.set(tcgen05.Field.ACCUMULATE, kt != 0)
|
|
for kb in cutlass.range(cute.size(tCrQ,mode=[2]), unroll_full=True):
|
|
cute.gemm(qk_mma, tStS0,
|
|
tCrQ[(None,None,kb,0)], tCrK[(None,None,kb,kh.index)], tStS0)
|
|
qk_mma.set(tcgen05.Field.ACCUMULATE, True)
|
|
cute.arch.fence_view_async_tmem_store()
|
|
sh.commit()
|
|
kh.release()
|
|
|
|
# Wait for softmax (identity: just signal done)
|
|
softmax_done_bar.arrive_and_wait()
|
|
|
|
acc_pipe.producer_commit(acc_st); acc_st.advance()
|
|
acc_pipe.producer_tail(acc_st)
|
|
|
|
# EPILOGUE
|
|
if warp_idx < self.mma_warp_id:
|
|
tmem.allocate(self.num_tmem_alloc_cols)
|
|
tmem.wait_for_alloc()
|
|
tmem_ptr = tmem.retrieve_ptr(self.qk_acc_dtype)
|
|
sfw_idx = tidx % (32 * len(self.epilogue_warp_id))
|
|
|
|
for kt in range(n_kv_tiles):
|
|
si_handle = s_cons.wait_and_advance()
|
|
# Identity softmax: no-op, just signal MMA
|
|
si_handle.release()
|
|
softmax_done_bar.arrive()
|
|
|
|
# Output S (QK result) to GMEM
|
|
tCtS_base = cute.make_tensor(tmem_ptr + self.tmem_s0_offset, tCtS_fake.layout)
|
|
acc_cons_st = pipeline.make_pipeline_state(pipeline.PipelineUserType.Consumer, self.num_acc_stage)
|
|
c_grp = pipeline.CooperativeGroup(pipeline.Agent.Thread, 32 * len(self.epilogue_warp_id))
|
|
c_pipe = pipeline.PipelineTmaStore.create(num_stages=self.num_c_stage, producer_group=c_grp)
|
|
acc_cons_st = utils.gemm.sm100.epilogue_tma_store(
|
|
self, tidx, warp_idx, tma_c, tCtS_base, sC, tCgC,
|
|
epi_tile, 0, const_expr(lambda x: x), (0,0,0), acc_cons_st, acc_pipe, c_pipe)
|
|
c_pipe.producer_tail()
|
|
tmem.relinquish_alloc_permit()
|
|
tmem.free(tmem_ptr)
|
|
|
|
|
|
def test():
|
|
torch.manual_seed(42)
|
|
n = 128
|
|
m, hd = 128, HEAD_DIM
|
|
q = torch.randn(m, hd, 1, dtype=torch.bfloat16, device='cuda')
|
|
k = torch.randn(n, hd, 1, dtype=torch.bfloat16, device='cuda')
|
|
c = torch.zeros(m, n, 1, dtype=torch.bfloat16, device='cuda') # (128, 128) output = S
|
|
|
|
qf = q[:,:,0].float(); kf = k[:,:,0].float()
|
|
ref = (qf @ kf.T)
|
|
|
|
mQ = ct.from_dlpack(q).mark_layout_dynamic(leading_dim=ct.get_leading_dim(q))
|
|
mK = ct.from_dlpack(k).mark_layout_dynamic(leading_dim=ct.get_leading_dim(k))
|
|
mC = ct.from_dlpack(c).mark_layout_dynamic(leading_dim=ct.get_leading_dim(c))
|
|
stream = cuda.CUstream(torch.cuda.current_stream().cuda_stream)
|
|
|
|
kernel = QkOnlyTest()
|
|
print('Compiling...', flush=True)
|
|
compiled = cute.compile(kernel, mQ, mK, mC, stream)
|
|
print('Running...', flush=True)
|
|
compiled(mQ, mK, mC, stream)
|
|
torch.cuda.synchronize()
|
|
out = c[:,:,0].float()
|
|
cos = torch.nn.functional.cosine_similarity(out.flatten().unsqueeze(0), ref.flatten().unsqueeze(0)).item()
|
|
print(f'QK-only n={n}: cosine {cos:.6f} {"PASS" if cos >= 0.99 else "FAIL"}')
|
|
if cos < 0.99:
|
|
print(f' out[0,:4]={out[0,:4].tolist()} ref[0,:4]={ref[0,:4].tolist()}')
|
|
print(f' out stats: min={out.min().item():.4f} max={out.max().item():.4f}')
|
|
print(f' ref stats: min={ref.min().item():.4f} max={ref.max().item():.4f}')
|
|
|
|
if __name__ == '__main__':
|
|
test()
|