cutlass icon indicating copy to clipboard operation
cutlass copied to clipboard

[QST] TiledCopy

Open yibolu96 opened this issue 1 year ago • 2 comments

What is your question?

Getting CopyAtom , src / dst layout doesn't vectorize into registers errors around g2s_tiled_copy_a and s2r_tiled_copy_a when trying to implement a the following int4 gemm:


template<typename Config>
__global__ void w4a4_gemm_multistage(void*       Dptr,
                                     const void*  Aptr,
                                     const void*  Bptr,
                                     const float* AweightScale,
                                     const float* BweightScale,
                                     int          m,
                                     int          n,
                                     int          k,
                                     int          groups)
{
    using namespace cute;
    using X = Underscore;

    using T_IN  = cute::int4b_t;
    using T_OUT = cute::int32_t;

    // tile configuration
    static constexpr int kTileM            = 256;
    static constexpr int kTileN            = 128;
    static constexpr int kTileK            = 64;
    static constexpr int kStage            = 3;
    static constexpr int kSmemLayoutCBatch = 2;

    static constexpr int kShmLoadSwizzleM = 3;
    static constexpr int kShmLoadSwizzleS = 3;
    static constexpr int kShmLoadSwizzleB = 2;

    using SmemLayoutAtom =
        decltype(composition(Swizzle<kShmLoadSwizzleB, kShmLoadSwizzleM, kShmLoadSwizzleS>{},
                             make_layout(make_shape(Int<8>{}, Int<kTileK>{}), make_stride(Int<kTileK>{}, Int<1>{}))));
    using SmemLayoutA =
        decltype(tile_to_shape(SmemLayoutAtom{}, make_shape(Int<kTileM>{}, Int<kTileK>{}, Int<kStage>{})));
    using SmemLayoutB =
        decltype(tile_to_shape(SmemLayoutAtom{}, make_shape(Int<kTileN>{}, Int<kTileK>{}, Int<kStage>{})));

    using mma_op = SM80_16x8x64_S32S4S4S32_TN;

    using mma_traits = MMA_Traits<mma_op>;
    using mma_atom   = MMA_Atom<mma_traits>;

    static constexpr int kMmaEURepeatM = 2;
    static constexpr int kMmaEURepeatN = 2;
    static constexpr int kMmaEURepeatK = 1;

    using mma_atom_shape        = mma_traits::Shape_MNK;
    static constexpr int kMmaPM = 1 * kMmaEURepeatM * get<0>(mma_atom_shape{});
    static constexpr int kMmaPN = 2 * kMmaEURepeatN * get<1>(mma_atom_shape{});
    static constexpr int kMmaPK = 1 * kMmaEURepeatK * get<2>(mma_atom_shape{});

    using MMA_EU_RepeatT =
        decltype(make_layout(make_shape(Int<kMmaEURepeatM>{}, Int<kMmaEURepeatN>{}, Int<kMmaEURepeatK>{})));
    using MMA_P_T = Tile<Int<kMmaPM>, Int<kMmaPN>, Int<kMmaPK>>;

    using MMA = decltype(make_tiled_mma(mma_atom{}, MMA_EU_RepeatT{}, MMA_P_T{}));

    using g2s_copy_op     = SM80_CP_ASYNC_CACHEGLOBAL<cute::uint128_t>;
    using g2s_copy_traits = Copy_Traits<g2s_copy_op>;
    using g2s_copy_atom   = Copy_Atom<g2s_copy_traits, T_IN>;

    using G2SCopyA =
        decltype(make_tiled_copy(g2s_copy_atom{},
                                 make_layout(make_shape(Int<32>{}, Int<4>{}), make_stride(Int<4>{}, Int<1>{})),
                                 make_layout(make_shape(Int<1>{}, Int<32>{}))));
    using G2SCopyB = G2SCopyA;

    // shared memory to register copy
    using s2r_copy_op     = SM75_U32x4_LDSM_N;
    using s2r_copy_traits = Copy_Traits<s2r_copy_op>;
    using s2r_copy_atom   = Copy_Atom<s2r_copy_traits, T_IN>;

    using S2RCopyAtomA = s2r_copy_atom;
    using S2RCopyAtomB = s2r_copy_atom;

    extern __shared__ T_IN shm_data[];

    T_IN* Ashm = shm_data;
    T_IN* Bshm = shm_data + cute::cosize(SmemLayoutA{});

    int idx = threadIdx.x;
    int ix  = blockIdx.x;
    int iy  = blockIdx.y;

    // use Tensor notation to represent device pointer + dimension
    cute::Tensor A = make_tensor(make_gmem_ptr((T_IN*)Aptr), make_shape(m, k), make_stride(k, Int<1>{}));   // (M, K)
    cute::Tensor B = make_tensor(make_gmem_ptr((T_IN*)Bptr), make_shape(n, k), make_stride(k, Int<1>{}));   // (N, K)
    cute::Tensor D = make_tensor(make_gmem_ptr((T_OUT*)Dptr), make_shape(m, n), make_stride(n, Int<1>{}));  // (M, N)

    // slice the tensor to small one which is used for current thread block.
    cute::Tensor gA = local_tile(A, make_tile(Int<kTileM>{}, Int<kTileK>{}), make_coord(iy, _));  // (kTileM, kTileK, k)
    cute::Tensor gB = local_tile(B, make_tile(Int<kTileN>{}, Int<kTileK>{}), make_coord(ix, _));  // (kTileN, kTileK, k)
    cute::Tensor gD = local_tile(D, make_tile(Int<kTileM>{}, Int<kTileN>{}), make_coord(iy, ix));  // (kTileM, kTileN)

    // shared memory
    auto sA = make_tensor(make_smem_ptr<T_IN>(Ashm), SmemLayoutA{});  // (kTileM, kTileK, kStage)
    auto sB = make_tensor(make_smem_ptr<T_IN>(Bshm), SmemLayoutB{});  // (kTileN, kTileK, kStage)

    // dispatch TileA/TileB/TileC mma tensor into thread fragment via partition
    // method
    TiledMMA tiled_mma;
    auto     thr_mma = tiled_mma.get_slice(idx);
    auto     tCrA    = thr_mma.partition_fragment_A(gA(_, _, 0));  // (MMA, MMA_M, MMA_K)
    auto     tCrB    = thr_mma.partition_fragment_B(gB(_, _, 0));  // (MMA, MMA_N, MMA_K)
    auto     tCrD    = thr_mma.partition_fragment_C(gD);           // (MMA, MMA_M, MMA_N)

    // fill zero for accumulator
    clear(tCrD);

    // gmem -cp.async-> shm -ldmatrix-> reg
    auto s2r_tiled_copy_a = make_tiled_copy_A(S2RCopyAtomA{}, tiled_mma);
    auto s2r_thr_copy_a   = s2r_tiled_copy_a.get_slice(idx);
    auto tAsA             = s2r_thr_copy_a.partition_S(sA);  // ? (CPY, CPY_M, CPY_K, kStage)
    auto tCrA_view        = s2r_thr_copy_a.retile_D(tCrA);   // ? (CPY, CPY_M, CPY_K)

    auto s2r_tiled_copy_b = make_tiled_copy_B(S2RCopyAtomB{}, tiled_mma);
    auto s2r_thr_copy_b   = s2r_tiled_copy_b.get_slice(idx);
    auto tBsB             = s2r_thr_copy_b.partition_S(sB);  // ? (CPY, CPY_M, CPY_K, kStage)
    auto tCrB_view        = s2r_thr_copy_b.retile_D(tCrB);   // ? (CPY, CPY_M, CPY_K)

    G2SCopyA g2s_tiled_copy_a;
    auto     g2s_thr_copy_a = g2s_tiled_copy_a.get_slice(idx);
    auto     tAgA_copy      = g2s_thr_copy_a.partition_S(gA);  // (CPY, CPY_M, CPY_K, k)
    auto     tAsA_copy      = g2s_thr_copy_a.partition_D(sA);  // (CPY, CPY_M, CPY_K, kStage)

    G2SCopyB g2s_tiled_copy_b;
    auto     g2s_thr_copy_b = g2s_tiled_copy_b.get_slice(idx);
    auto     tBgB_copy      = g2s_thr_copy_b.partition_S(gB);  // (CPY, CPY_N, CPY_K, k)
    auto     tBsB_copy      = g2s_thr_copy_b.partition_D(sB);  // (CPY, CPY_N, CPY_K, kStage)

    int itile_to_read = 0;
    int ismem_read    = 0;
    int ismem_write   = 0;

    // submit kStage - 1 tile
    // gmem -> shm
#pragma unroll
    for (int istage = 0; istage < kStage - 1; ++istage) {
        cute::copy(g2s_tiled_copy_a, tAgA_copy(_, _, _, istage), tAsA_copy(_, _, _, istage));
        cute::copy(g2s_tiled_copy_b, tBgB_copy(_, _, _, istage), tBsB_copy(_, _, _, istage));
        cp_async_fence();

        ++itile_to_read;
        ++ismem_write;
    }

    // wait one submitted gmem->smem done
    cp_async_wait<kStage - 2>();
    __syncthreads();

    int ik = 0;
    // smem -> reg
    cute::copy(s2r_tiled_copy_a, tAsA(_, _, ik, ismem_read), tCrA_view(_, _, ik));
    cute::copy(s2r_tiled_copy_b, tBsB(_, _, ik, ismem_read), tCrB_view(_, _, ik));

    // loop over k: i. load tile, ii. mma
    int ntile = k / kTileK;
#pragma unroll 1
    for (int itile = 0; itile < ntile; ++itile) {
        int nk = size<2>(tCrA);

#pragma unroll
        for (int ik = 0; ik < nk; ++ik) {
            int ik_next = (ik + 1) % nk;

            if (ik == nk - 1) {
                cp_async_wait<kStage - 2>();
                __syncthreads();

                ismem_read = (ismem_read + 1) % kStage;
            }

            // shm -> reg s[itile][ik + 1] -> r[ik + 1]
            cute::copy(s2r_tiled_copy_a, tAsA(_, _, ik_next, ismem_read), tCrA_view(_, _, ik_next));
            cute::copy(s2r_tiled_copy_b, tBsB(_, _, ik_next, ismem_read), tCrB_view(_, _, ik_next));

            if (ik == 0) {
                if (itile_to_read < ntile) {
                    cute::copy(g2s_tiled_copy_a, tAgA_copy(_, _, _, itile_to_read), tAsA_copy(_, _, _, ismem_write));
                    cute::copy(g2s_tiled_copy_b, tBgB_copy(_, _, _, itile_to_read), tBsB_copy(_, _, _, ismem_write));

                    ++itile_to_read;
                    ismem_write = (ismem_write + 1) % kStage;
                }

                cp_async_fence();
            }

            cute::gemm(tiled_mma, tCrD, tCrA(_, _, ik), tCrB(_, _, ik), tCrD);
        }  // for ik
    }  // itile
}

yibolu96 avatar Feb 10 '25 01:02 yibolu96

Removing all the extra code, your SMEM Tensor is

auto smem_layout_atom = composition(Swizzle<2, 3, 3>{}, Layout<Shape<_8,_64>,Stride<_64,_1>>{}); // (M8,K64)
auto smem_layout      = tile_to_shape(smem_layout_atom, Shape<_256,_64,_3>{});                   // (M256,K64,S3)
Tensor sA = make_tensor(make_smem_ptr<uint4b_t>(Ashm), smem_layout);

Because of the swizzle, there are only 8 consecutive int4b_ts that can be vectorized by all threads across each row, which comes to 32b. You can view the smem layout atom with print_latex: Image But you're asking for a 128b copy and it's telling you that is impossible.

I usually recommend designing data layouts without swizzles first, then using them if they are absolutely necessary to resolve bank conflicts in either the read or the write.

ccecka avatar Feb 10 '25 02:02 ccecka

Removing all the extra code, your SMEM Tensor is

auto smem_layout_atom = composition(Swizzle<2, 3, 3>{}, Layout<Shape<_8,_64>,Stride<_64,_1>>{}); // (M8,K64) auto smem_layout = tile_to_shape(smem_layout_atom, Shape<_256,_64,_3>{}); // (M256,K64,S3) Tensor sA = make_tensor(make_smem_ptr<uint4b_t>(Ashm), smem_layout); Because of the swizzle, there are only 8 consecutive int4b_ts that can be vectorized by all threads across each row, which comes to 32b. You can view the smem layout atom with print_latex: Image But you're asking for a 128b copy and it's telling you that is impossible.

I usually recommend designing data layouts without swizzles first, then using them if they are absolutely necessary to resolve bank conflicts in either the read or the write.

That explains all, thanks

yibolu96 avatar Feb 10 '25 06:02 yibolu96