"""
Stage B N-tiling v6: Run BOTH (128,128) and (128,16) PV MMAs in the same kernel.
Use the working (128,128) epilogue for output. Compare first 16 columns.

This tests whether the (128,16) PV MMA's C→A TMEM bridge is correct
without needing a (128,16) output epilogue (which is broken).
The (128,16) result is written to a separate TMEM region, then
manually padded and copied to the (128,128) output buffer.
"""
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


class DualPvKernel:
    """Run QK → softmax → PV(128,128) and PV(128,16) side by side.
    Output PV(128,128) result using working epilogue_tma_store.
    """
    def __init__(self, mma_tiler_mn):
        self.acc_dtype = Float32; self.qk_acc_dtype = Float32
        self.q_dtype = BFloat16; self.o_dtype = BFloat16; self.c_dtype = BFloat16
        self.mma_tiler_mn = mma_tiler_mn; self.mma_tiler = (*mma_tiler_mn, 1)
        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

    def _setup(self, qk_mma, pv128_mma, pv16_mma):
        qk_inst_k = cute.size(qk_mma.shape_mnk, mode=[2])
        self.qk_mma_tiler = (*self.mma_tiler_mn, qk_inst_k * 4)
        # PV(128,128): same as v30
        self.pv128_mma_tiler = (self.qk_mma_tiler[0], self.qk_mma_tiler[1], self.qk_mma_tiler[1])
        # PV(128,16): N-tiling with N=16
        self.pv16_mma_tiler = (self.qk_mma_tiler[0], qk_inst_k, self.qk_mma_tiler[1])
        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),
            self.qk_mma_tiler[1], 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.a_smem_s = utils.sm100.make_smem_layout_a(qk_mma, self.mma_tiler, self.q_dtype, 1)
        self.b_smem_s = utils.sm100.make_smem_layout_b(qk_mma, self.mma_tiler, self.q_dtype, 1)
        self.v_smem_s = utils.sm100.make_smem_layout_b(pv128_mma, self.pv128_mma_tiler, self.q_dtype, 1)
        self.p_tmem_s = utils.sm100.make_smem_layout_a(pv128_mma, self.pv128_mma_tiler, self.q_dtype, 1)
        self.p16_tmem_s = utils.sm100.make_smem_layout_a(pv16_mma, self.pv16_mma_tiler, self.q_dtype, 1)
        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_acc_shape = qk_thr.partition_shape_C(self.qk_mma_tiler[:2])
        tStS = qk_thr.make_fragment_C(qk_acc_shape)
        s_cols = find_tmem_tensor_col_offset(tStS)

        pv128_thr = pv128_mma.get_slice(0)
        pv128_acc_shape = pv128_thr.partition_shape_C(self.pv128_mma_tiler[:2])
        tOtO = pv128_thr.make_fragment_C(pv128_acc_shape)
        o_cols = find_tmem_tensor_col_offset(tOtO)

        # PV(128,16) accumulator
        pv16_thr = pv16_mma.get_slice(0)
        pv16_acc_shape = pv16_thr.partition_shape_C(self.pv16_mma_tiler[:2])
        tOtO16 = pv16_thr.make_fragment_C(pv16_acc_shape)
        o16_cols = find_tmem_tensor_col_offset(tOtO16)

        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 = s_cols  # PV(128,128) accumulator
        self.tmem_o16_offset = o_cols  # PV(128,16) accumulator (separate TMEM)

        tCtS_fake = qk_mma.make_fragment_C(cute.append(qk_acc_shape, self.num_acc_stage))
        tCtO_fake = pv128_mma.make_fragment_C(cute.append(pv128_acc_shape, self.num_acc_stage))
        tCtO16_fake = pv16_mma.make_fragment_C(cute.append(pv16_acc_shape, self.num_acc_stage))
        self.num_tmem_alloc_cols = utils.get_num_tmem_alloc_cols([tCtS_fake, tCtO_fake, tCtO16_fake], arch="sm_100")

        a_smem = cute.slice_(self.a_smem_s, (None, None, None, 0))
        b_smem = cute.slice_(self.b_smem_s, (None, None, None, 0))
        v_smem = cute.slice_(self.v_smem_s, (None, None, None, 0))
        self.num_tma_load_bytes = (
            cute.size_in_bytes(self.q_dtype, a_smem) + cute.size_in_bytes(self.q_dtype, b_smem) +
            cute.size_in_bytes(self.q_dtype, v_smem)
        ) * cute.size(qk_mma.thr_id.shape)

    @cute.jit
    def __call__(self, q, k, v, 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.v_major = LayoutEnum.from_tensor(v).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, self.mma_tiler_mn, tcgen05.OperandSource.SMEM)
        pv128_mma = utils.sm100.make_trivial_tiled_mma(
            self.q_dtype, self.q_dtype, cute.nvgpu.OperandMajorMode.K, self.v_major,
            self.qk_acc_dtype, self.cta_group, self.mma_tiler_mn, tcgen05.OperandSource.TMEM)
        pv16_mma = utils.sm100.make_trivial_tiled_mma(
            self.q_dtype, self.q_dtype, cute.nvgpu.OperandMajorMode.K, self.v_major,
            self.qk_acc_dtype, self.cta_group, (128, 16), tcgen05.OperandSource.TMEM)
        self._setup(qk_mma, pv128_mma, pv16_mma)

        q_smem = cute.slice_(self.a_smem_s, (None, None, None, 0))
        k_smem = cute.slice_(self.b_smem_s, (None, None, None, 0))
        v_smem = cute.slice_(self.v_smem_s, (None, None, None, 0))
        tma_q, tma_tq = 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_smem, self.mma_tiler, qk_mma, self.cluster_layout_vmnk.shape)
        tma_k, tma_tk = 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_smem, self.mma_tiler, qk_mma, self.cluster_layout_vmnk.shape)
        tma_v, tma_tv = cute.nvgpu.make_tiled_tma_atom_B(
            utils.sm100.cluster_shape_to_tma_atom_B(self.cluster_shape_mn, pv128_mma.thr_id),
            v, v_smem, self.pv128_mma_tiler, pv128_mma, self.cluster_layout_vmnk.shape)
        epi_smem = cute.select(self.c_smem_s, mode=[0, 1])
        tma_c, tma_tc = cpasync.make_tiled_tma_atom(cpasync.CopyBulkTensorTileS2GOp(), c, epi_smem, self.epi_tile)

        self._kernel(qk_mma, pv128_mma, pv16_mma, tma_q, tma_tq, tma_k, tma_tk, tma_v, tma_tv,
            tma_c, tma_tc, self.cluster_layout_vmnk,
            self.a_smem_s, self.b_smem_s, self.v_smem_s, self.p_tmem_s, self.p16_tmem_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, pv128_mma, pv16_mma, tma_q, mQ, tma_k, mK, tma_v, mV,
                tma_c, mC, cl_vmnk, a_smem_s, b_smem_s, v_smem_s, p_tmem_s, p16_tmem_s, c_smem_s, epi_tile):
        warp_idx = cute.arch.make_warp_uniform(cute.arch.warp_idx())
        tidx, _, _ = cute.arch.thread_idx()
        use_2cta = cute.size(qk_mma.thr_id.shape) == 2

        if warp_idx == self.tma_warp_id:
            cpasync.prefetch_descriptor(tma_q); cpasync.prefetch_descriptor(tma_k)
            cpasync.prefetch_descriptor(tma_v); cpasync.prefetch_descriptor(tma_c)

        @cute.struct
        class SS:
            ab_bar: cute.struct.MemRange[cutlass.Int64, self.num_ab_stage * 2]
            mma_si_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)

        ab_p, ab_c = pipeline.PipelineTmaUmma.create(
            barrier_storage=st.ab_bar.data_ptr(), num_stages=self.num_ab_stage,
            producer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread),
            consumer_group=pipeline.CooperativeGroup(pipeline.Agent.Thread, 1),
            tx_count=self.num_tma_load_bytes, cta_layout_vmnk=cl_vmnk, defer_sync=True
        ).make_participants()

        mma_si_prod, mma_si_cons = pipeline.PipelineUmmaAsync.create(
            barrier_storage=st.mma_si_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()

        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) * (2 if use_2cta else 1)),
            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=use_2cta,
            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=a_smem_s.outer, byte_alignment=128, swizzle=a_smem_s.inner)
        sK = smem.allocate_tensor(element_type=self.q_dtype, layout=b_smem_s.outer, byte_alignment=128, swizzle=b_smem_s.inner)
        sV = smem.allocate_tensor(element_type=self.q_dtype, layout=v_smem_s.outer, byte_alignment=128, swizzle=v_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))
        k_cnt = cute.size(gQ, mode=[3])

        qk_thr = qk_mma.get_slice(0)
        pv128_thr = pv128_mma.get_slice(0)
        pv16_thr = pv16_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)]

        gV = cute.local_tile(mV, cute.slice_(self.pv128_mma_tiler, (0,None,None)), (None,None,None))
        tCgV = pv128_thr.partition_B(gV)
        tVsV, tVgV = cpasync.tma_partition(tma_v, 0, b_lay, cute.group_modes(sV,0,3), cute.group_modes(tCgV,0,3))
        tVgV = tVgV[(None,0,None,0)]

        tCrQ = qk_mma.make_fragment_A(sQ); tCrK = qk_mma.make_fragment_B(sK)
        tCrV = pv128_mma.make_fragment_B(sV)

        # QK accumulator
        qk_acc_shape = qk_thr.partition_shape_C(self.qk_mma_tiler[:2])
        tStS = qk_thr.make_fragment_C(qk_acc_shape)
        tStS0 = cute.make_tensor(tStS.iterator + self.tmem_s0_offset, tStS.layout)

        # PV(128,128) accumulator
        pv128_acc_shape = pv128_thr.partition_shape_C(self.pv128_mma_tiler[:2])
        tOtO = pv128_thr.make_fragment_C(pv128_acc_shape)
        tOtO0 = cute.make_tensor(tOtO.iterator + self.tmem_o0_offset, tOtO.layout)

        # PV(128,16) accumulator
        pv16_acc_shape = pv16_thr.partition_shape_C(self.pv16_mma_tiler[:2])
        tOtO16 = pv16_thr.make_fragment_C(pv16_acc_shape)
        tOtO16_0 = cute.make_tensor(tOtO16.iterator + self.tmem_o16_offset, tOtO16.layout)

        # P A-fragment for PV(128,128) — reads from softmax P in TMEM
        tP = cute.make_tensor(tStS.iterator, p_tmem_s.outer)
        tOrP_base = pv128_thr.make_fragment_A(tP)
        tOrP = tOrP_base[(None, None, None, 0)]
        tOrP0 = cute.make_tensor(
            tOrP.iterator + self.qk_acc_dtype.width // self.q_dtype.width * self.tmem_p0_offset,
            tOrP.layout)

        # P A-fragment for PV(128,16) — reads from the SAME softmax P in TMEM
        tP16 = cute.make_tensor(tStS.iterator, p16_tmem_s.outer)
        tOrP16_base = pv16_thr.make_fragment_A(tP16)
        tOrP16 = tOrP16_base[(None, None, None, 0)]
        tOrP16_0 = cute.make_tensor(
            tOrP16.iterator + self.qk_acc_dtype.width // self.q_dtype.width * self.tmem_p0_offset,
            tOrP16.layout)

        tCtS_fake = qk_mma.make_fragment_C(cute.append(qk_acc_shape, self.num_acc_stage))
        tCtO_fake = pv128_mma.make_fragment_C(cute.append(pv128_acc_shape, self.num_acc_stage))
        tCtO16_fake = pv16_mma.make_fragment_C(cute.append(pv16_acc_shape, self.num_acc_stage))

        pipeline.pipeline_init_wait(cluster_shape_mn=cl_vmnk)

        # ═══ TMA LOAD WARP ═══
        if warp_idx == self.tma_warp_id:
            ab_p.reset(); peek = ab_p.try_acquire()
            for kt in cutlass.range(k_cnt, unroll=1):
                h = ab_p.acquire_and_advance(peek)
                cute.copy(tma_q, tAgQ[(None,h.count)], tAsQ[(None,h.index)], tma_bar_ptr=h.barrier)
                cute.copy(tma_k, tBgK[(None,h.count)], tBsK[(None,h.index)], tma_bar_ptr=h.barrier)
                cute.copy(tma_v, tVgV[(None,h.count)], tVsV[(None,h.index)], tma_bar_ptr=h.barrier)
                peek = cutlass.Boolean(1)
                if h.count+1<k_cnt: peek = ab_p.try_acquire()
            ab_p.tail()

        # ═══ MMA WARP ═══
        if warp_idx == self.mma_warp_id:
            tmem.wait_for_alloc()
            ab_c.reset(); peek = ab_c.try_wait()
            s0_handle = mma_si_prod.acquire_and_advance()

            acc_prod_st = pipeline.make_pipeline_state(pipeline.PipelineUserType.Producer, self.num_acc_stage)
            acc_pipe.producer_acquire(acc_prod_st)

            # QK MMA
            qk_mma.set(tcgen05.Field.ACCUMULATE, False)
            for kt in range(k_cnt):
                h = ab_c.wait_and_advance(peek)
                nblk = cute.size(tCrQ, mode=[2])
                for kb in cutlass.range(nblk, unroll_full=True):
                    cute.gemm(qk_mma, tStS0, tCrQ[(None,None,kb,h.index)], tCrK[(None,None,kb,h.index)], tStS0)
                    qk_mma.set(tcgen05.Field.ACCUMULATE, True)
                h.release(); peek = cutlass.Boolean(1)
                if h.count+1<k_cnt: peek = ab_c.try_wait()

            cute.arch.fence_view_async_tmem_store()
            s0_handle.commit()
            s0_handle = mma_si_prod.acquire_and_advance()

            # PV(128,128) MMA: P @ V=I → O = P
            pv128_mma.set(tcgen05.Field.ACCUMULATE, False)
            tCrV_s = tCrV[(None, None, None, 0)]
            nblk_pv = cute.size(tOrP0, mode=[2])
            for kb in cutlass.range(nblk_pv, unroll_full=True):
                cute.gemm(pv128_mma, tOtO0, tOrP0[(None,None,kb)], tCrV_s[(None,None,kb)], tOtO0)
                pv128_mma.set(tcgen05.Field.ACCUMULATE, True)

            # PV(128,16) MMA: P @ V[:16,:] → O16 = P[:,:16]
            # Note: PV(128,16) reads P from the same TMEM location as PV(128,128)
            # but the A-fragment is for (128,16) PV MMA which has different layout
            pv16_mma.set(tcgen05.Field.ACCUMULATE, False)
            for kb in cutlass.range(cute.size(tOrP16_0, mode=[2]), unroll_full=True):
                cute.gemm(pv16_mma, tOtO16_0, tOrP16_0[(None,None,kb)], tCrV_s[(None,None,kb)], tOtO16_0)
                pv16_mma.set(tcgen05.Field.ACCUMULATE, True)

            acc_pipe.producer_commit(acc_prod_st)
            acc_prod_st.advance()
            acc_pipe.producer_tail(acc_prod_st)

        # ═══ EPILOGUE WARPS ═══
        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))

            # Softmax packing: FP32 S → BF16 P
            tmem_load_atom = cute.make_copy_atom(
                tcgen05.copy.Ld32x32bOp(tcgen05.copy.Repetition(32)), self.qk_acc_dtype)
            tiled_tmem_load = tcgen05.make_tmem_copy(tmem_load_atom, tStS0)
            thr_load = tiled_tmem_load.get_slice(sfw_idx)
            tTMEM_LOADtS = thr_load.partition_S(tStS0)
            cS = cute.make_identity_tensor((self.qk_mma_tiler[0], self.qk_mma_tiler[1]))
            tScS = qk_thr.partition_C(cS)
            tTMEM_LOADcS = thr_load.partition_D(tScS)

            tStS_P_layout = cute.composition(tStS.layout, cute.make_layout((128, self.tilePlikeFP32)))
            tStS_P = cute.make_tensor(tStS.iterator + self.tmem_p0_offset, tStS_P_layout)
            tmem_store_atom = cute.make_copy_atom(
                tcgen05.copy.St32x32bOp(tcgen05.copy.Repetition(32)), self.qk_acc_dtype)
            tiled_tmem_store = tcgen05.make_tmem_copy(tmem_store_atom, tStS_P)
            thr_store = tiled_tmem_store.get_slice(sfw_idx)
            tTMEM_STOREtS_x4 = thr_store.partition_D(tStS_P)
            tScS_P_layout = cute.composition(tScS.layout, cute.make_layout((128, self.tilePlikeFP32)))
            tScS_P = cute.make_tensor(tScS.iterator, tScS_P_layout)
            tTMEM_STOREcS = thr_store.partition_S(tScS_P)

            si_handle = mma_si_cons.wait_and_advance()

            tTMEM_LOADrS = cute.make_rmem_tensor(tTMEM_LOADcS.shape, self.qk_acc_dtype)
            cute.copy(tiled_tmem_load, tTMEM_LOADtS, tTMEM_LOADrS)
            tTMEM_STORErS_x4 = cute.make_rmem_tensor(tTMEM_STOREcS.shape, self.qk_acc_dtype)
            tTMEM_STORErS_x4_e = cute.make_tensor(
                cute.recast_ptr(tTMEM_STORErS_x4.iterator, dtype=self.q_dtype),
                tTMEM_LOADrS.layout)
            frg_cnt = 4
            frg_tile = cute.size(tTMEM_LOADrS) // frg_cnt
            tTMEM_LOADrS_frg = cute.logical_divide(tTMEM_LOADrS, cute.make_layout(frg_tile))
            tTMEM_STORErS_x4_e_frg = cute.logical_divide(
                tTMEM_STORErS_x4_e, cute.make_layout(frg_tile))
            for j in range(frg_cnt):
                s_vec = tTMEM_LOADrS_frg[None, j].load()
                tTMEM_STORErS_x4_e_frg[None, j].store(s_vec.to(self.q_dtype))
            cute.copy(tiled_tmem_store, tTMEM_STORErS_x4, tTMEM_STOREtS_x4)
            cute.arch.fence_view_async_tmem_store()
            si_handle.release()

            # Output: PV(128,128) result using working epilogue
            tCtO_base = cute.make_tensor(tmem_ptr + self.tmem_o0_offset, tCtO_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, tCtO_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)
    m, n = 128, 128
    q = torch.randn(m, 128, 1, dtype=torch.bfloat16, device='cuda')
    k = torch.randn(n, 128, 1, dtype=torch.bfloat16, device='cuda')
    # V = identity (128,128) MN-major — same as v30
    v = torch.eye(128, dtype=torch.bfloat16, device='cuda')
    v = v.as_strided((128, 128), (1, 128)).unsqueeze(-1)
    c = torch.zeros(m, 128, 1, dtype=torch.bfloat16, device='cuda')

    qf = q[:,:,0].float(); kf = k[:,:,0].float()
    # V=I → O = P = (Q@K^T).bf16()
    ref = (qf @ kf.T).bfloat16().float()

    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))
    mV = ct.from_dlpack(v).mark_layout_dynamic(leading_dim=ct.get_leading_dim(v))
    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 = DualPvKernel(mma_tiler_mn=(128, 128))
    print('Compiling dual-PV test...', flush=True)
    compiled = cute.compile(kernel, mQ, mK, mV, mC, stream)
    print('Running...', flush=True)
    compiled(mQ, mK, mV, 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'PV(128,128) output: cosine {cos:.6f} {"PASS" if cos >= 0.99 else "FAIL"}')
    # If PV(128,128) still works, the QK+softmax pipeline is fine
    # The PV(128,16) result is in TMEM at tmem_o16_offset but not yet output
    # We need to read it to compare


if __name__ == '__main__':
    test()
