Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

22.1 KB
Raw Blame
#include <cuda_fp16.h>
#include "reconstruct.cuh"
#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/CUDAContext.h>
#include "../util.h"
#include "../util.cuh"
#if !defined(USE_ROCM)
#include "../ptx.cuh"
#endif
#include "exl3_dq.cuh"
#include "hadamard_inner.cuh"
#include "bits_k.cuh"

template <int K, int cb, bool HALF = false>
__device__ __forceinline__
void reconstruct_tile
(
    half* __restrict__ g_unpacked,
    const uint16_t* __restrict__ g_packed,
    int packed_blocks_n,
    int packed_n_offset
)
{
    constexpr int packed_size = 16 * K + (HALF ? 8 : 0);

    int t = threadIdx.x;
    int lane_id = t % 32;
    int warp_id = t / 32;
    int k = blockIdx.y;
    int n = blockIdx.x * 8;
    int tiles_n = gridDim.x;
    int out_blocks_n = tiles_n * 8;

    // Load packed 16*128 tile
    __shared__ uint32_t s_packed[8][packed_size / 2];
    g_packed += (k * packed_blocks_n + packed_n_offset + n) * packed_size;
    if (t < packed_size)
        ((int4*) s_packed)[t] = ((int4*) g_packed)[t];
    __syncthreads();

    // Dequant
    register FragB frag[2];
    dq_dispatch<K, cb, HALF>(s_packed[warp_id], lane_id * 8, frag[0], frag[1]);

    // Shuffle from tensor core layout to row major tile
//    __shared__ half tile[16 * 8 * 16];
    __shared__ half2 tile[16][8][8];

    half2 n0 = __shfl_down_sync(0xFFFFFFFF, frag[0][0], 4, 32);
    half2 n1 = __shfl_down_sync(0xFFFFFFFF, frag[0][1], 4, 32);
    half2 n2 = __shfl_down_sync(0xFFFFFFFF, frag[1][0], 4, 32);
    half2 n3 = __shfl_down_sync(0xFFFFFFFF, frag[1][1], 4, 32);

    if (!(lane_id & 4))
    {
        half2 m0 = __halves2half2(__low2half(frag[0][0]), __low2half(n0));
        half2 m1 = __halves2half2(__high2half(frag[0][0]), __high2half(n0));
        half2 m2 = __halves2half2(__low2half(frag[0][1]), __low2half(n1));
        half2 m3 = __halves2half2(__high2half(frag[0][1]), __high2half(n1));
        half2 m4 = __halves2half2(__low2half(frag[1][0]), __low2half(n2));
        half2 m5 = __halves2half2(__high2half(frag[1][0]), __high2half(n2));
        half2 m6 = __halves2half2(__low2half(frag[1][1]), __low2half(n3));
        half2 m7 = __halves2half2(__high2half(frag[1][1]), __high2half(n3));
        int r0 = (lane_id % 4) * 2;
        int r1 = r0 + 1;
        int r2 = r0 + 8;
        int r3 = r0 + 9;
        int c0 = lane_id / 8;
        int c1 = c0 + 4;
        tile[r0][warp_id][c0] = m0;
        tile[r1][warp_id][c0] = m1;
        tile[r2][warp_id][c0] = m2;
        tile[r3][warp_id][c0] = m3;
        tile[r0][warp_id][c1] = m4;
        tile[r1][warp_id][c1] = m5;
        tile[r2][warp_id][c1] = m6;
        tile[r3][warp_id][c1] = m7;
    }
    __syncthreads();

    // Store unpacked tile
    int r = t / 16;
    int c = t % 16;
    int4* tile_int4 = (reinterpret_cast<int4*> (tile));
    int4* out_int4 = ((int4*) g_unpacked) + (k * 16 + r) * 2 * out_blocks_n + n * 2 + c;
    *out_int4 = tile_int4[t];
}

template <int K, int cb, bool HALF = false>
__global__ __launch_bounds__(256)
void reconstruct_kernel
(
    half* __restrict__ g_unpacked,
    const uint16_t* __restrict__ g_packed,
    int packed_blocks_n,
    int packed_n_offset
)
{
    reconstruct_tile<K, cb, HALF>(g_unpacked, g_packed, packed_blocks_n, packed_n_offset);
}

#if !defined(USE_ROCM)
// Batched variant: blockIdx.z selects the matrix from a pointer table, outputs are consecutive
// [k, n] slabs out_stride halfs apart. Whole matrices only.
template <int K, int cb, bool HALF = false>
__global__ __launch_bounds__(256)
void reconstruct_batch_kernel
(
    half* __restrict__ g_unpacked,
    const uint16_t* const* packed_ptrs,
    int packed_blocks_n,
    size_t out_stride
)
{
    int b = blockIdx.z;
    reconstruct_tile<K, cb, HALF>(g_unpacked + (size_t) b * out_stride, packed_ptrs[b], packed_blocks_n, 0);
}
#endif // USE_ROCM

#define __(i, cb) reconstruct_batch_kernel<i, cb>
#if !defined(USE_ROCM)
constexpr auto reconstruct_batch_kernel_instances = std::array
{
    __(1, 0), __(2, 0), __(3, 0), __(4, 0), __(5, 0), __(6, 0), __(7, 0), __(8, 0),
    __(1, 1), __(2, 1), __(3, 1), __(4, 1), __(5, 1), __(6, 1), __(7, 1), __(8, 1),
    __(1, 2), __(2, 2), __(3, 2), __(4, 2), __(5, 2), __(6, 2), __(7, 2), __(8, 2),
    reconstruct_batch_kernel<1, 2, true>, reconstruct_batch_kernel<2, 2, true>, reconstruct_batch_kernel<3, 2, true>
};
#endif // USE_ROCM
#undef __

#define __(i, cb) reconstruct_kernel<i, cb>
constexpr auto reconstruct_kernel_instances = std::array
{
    __(1, 0), __(2, 0), __(3, 0), __(4, 0), __(5, 0), __(6, 0), __(7, 0), __(8, 0),
    __(1, 1), __(2, 1), __(3, 1), __(4, 1), __(5, 1), __(6, 1), __(7, 1), __(8, 1),
    __(1, 2), __(2, 2), __(3, 2), __(4, 2), __(5, 2), __(6, 2), __(7, 2), __(8, 2),
    reconstruct_kernel<1, 2, true>, reconstruct_kernel<2, 2, true>, reconstruct_kernel<3, 2, true>
};
#undef __

/*
Reconstruct encoded+packed tensor
*/
void reconstruct_slice
(
    at::Tensor unpacked,
    at::Tensor packed,
    float K_,
    bool mcg,
    bool mul1,
    int64_t n_offset
)
{
    const at::cuda::OptionalCUDAGuard device_guard(unpacked.device());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream().stream();
    const BitsK bk = bits_from_K(K_);
    const int K = bk.bits;
    TORCH_CHECK(!bk.half || mul1, "reconstruct: half-integer bitrates require the mul1 codebook");

    TORCH_CHECK_SHAPES(unpacked, 0, packed, 0, 16);
    TORCH_CHECK_SIZE(packed, 2, 16 * K + (bk.half ? 8 : 0));
    TORCH_CHECK_DTYPE(unpacked, kHalf);

    int rows = packed.size(0);
    int packed_cols = packed.size(1);

    if (unpacked.numel() == 0)
        return;

    TORCH_CHECK(unpacked.size(1) % 128 == 0, "unpacked N dimension must be divisible by 128");
    TORCH_CHECK(n_offset % 128 == 0, "n_offset must be divisible by 128");
    TORCH_CHECK(n_offset >= 0, "n_offset must be non-negative");
    TORCH_CHECK(n_offset + unpacked.size(1) <= packed.size(1) * 16, "reconstruct slice exceeds packed tensor bounds");

    int cols = unpacked.size(1) / 16;
    int packed_n_offset = n_offset / 16;

    dim3 blockDim(256);
    dim3 gridDim(cols / 8, rows);

    int cbi = K - 1;
    if (bk.half) cbi += 24;
    else if (mcg) cbi += 8;
    else if (mul1) cbi += 16;
    TORCH_CHECK(cbi >= 0 && cbi < (int) reconstruct_kernel_instances.size(),
                "kernel index out of range: ", cbi);

    auto reconstruct_kernel = reconstruct_kernel_instances[cbi];
    reconstruct_kernel<<<gridDim, blockDim, 0, stream>>>
    (
        (half*) unpacked.data_ptr(),
        (const uint16_t*) packed.data_ptr(),
        packed_cols,
        packed_n_offset
    );
    cuda_check(cudaPeekAtLastError());
}


// Fused reconstruct + both-side Hadamard: emits W = diag(suh) . H128 . W_hat . H128 . diag(svh)
// (per 128x128 tile, 1/sqrt(128) per side), i.e. the ORIGINAL-basis weights, so the hgemm
// prefill path can run on the raw input with no standalone input/output had_r_128 launches.
//
// 128x128 tile lives in shared memory with an XOR swizzle on the half4 chunk index
// (chunk ^= (row >> 2) & 31) making all phases bank-conflict-free except the 16-lane dequant
// scatter; both transforms reuse the natural-order Sylvester butterfly (symmetric, so
// column-then-row order is H W H).
#define RH_THREADS 256

template <int K, int cb, bool HALF = false>
__device__ __forceinline__
void reconstruct_had_tile
(
    half* __restrict__ g_unpacked,
    const uint16_t* __restrict__ g_packed,
    const half* __restrict__ suh,
    const half* __restrict__ svh,
    int packed_blocks_n,
    int packed_n_offset
)
{
    constexpr int packed_size = 16 * K + (HALF ? 8 : 0);
    constexpr float r_scale = 0.08838834764831845f;

    int t = threadIdx.x;
    int lane_id = t % 32;
    int warp_id = t / 32;
    int kb = blockIdx.y;
    int nb = blockIdx.x;
    int n = nb * 8;
    int row_len = gridDim.x * 128;

    __shared__ uint32_t s_packed[8][8][packed_size / 2];
    __shared__ half2 stile[128 * 64];

    auto tix = [&] (int R, int q, int p)
    {
        return R * 64 + (q ^ ((R >> 2) & 31)) * 2 + p;
    };

    constexpr int j_int4 = packed_size / 8;
    for (int u = t; u < 8 * 8 * j_int4; u += RH_THREADS)
    {
        int j = u / (8 * j_int4);
        int r = u % (8 * j_int4);
        const uint16_t* gp = g_packed +
            ((size_t) ((kb * 8 + j) * packed_blocks_n + packed_n_offset + n)) * packed_size;
        ((int4*) s_packed[j])[r] = ((const int4*) gp)[r];
    }
    __syncthreads();

    for (int jj = 0; jj < 8 * 8 / (RH_THREADS / 32); ++jj)
    {
        int j = (warp_id / 8) * (8 / (RH_THREADS / 256)) + jj;
        int wn = warp_id % 8;
        register FragB frag[2];
        dq_dispatch<K, cb, HALF>(s_packed[j][wn], lane_id * 8, frag[0], frag[1]);

        half2 n0 = __shfl_down_sync(0xFFFFFFFF, frag[0][0], 4, 32);
        half2 n1 = __shfl_down_sync(0xFFFFFFFF, frag[0][1], 4, 32);
        half2 n2 = __shfl_down_sync(0xFFFFFFFF, frag[1][0], 4, 32);
        half2 n3 = __shfl_down_sync(0xFFFFFFFF, frag[1][1], 4, 32);

        if (!(lane_id & 4))
        {
            half2 m0 = __halves2half2(__low2half(frag[0][0]), __low2half(n0));
            half2 m1 = __halves2half2(__high2half(frag[0][0]), __high2half(n0));
            half2 m2 = __halves2half2(__low2half(frag[0][1]), __low2half(n1));
            half2 m3 = __halves2half2(__high2half(frag[0][1]), __high2half(n1));
            half2 m4 = __halves2half2(__low2half(frag[1][0]), __low2half(n2));
            half2 m5 = __halves2half2(__high2half(frag[1][0]), __high2half(n2));
            half2 m6 = __halves2half2(__low2half(frag[1][1]), __low2half(n3));
            half2 m7 = __halves2half2(__high2half(frag[1][1]), __high2half(n3));
            int r0 = j * 16 + (lane_id % 4) * 2;
            int r1 = r0 + 1;
            int r2 = r0 + 8;
            int r3 = r0 + 9;
            int c0 = lane_id / 8;
            int q0 = (wn * 8 + c0) >> 1, p0 = c0 & 1;
            int q1 = (wn * 8 + c0 + 4) >> 1, p1 = c0 & 1;
            stile[tix(r0, q0, p0)] = m0;
            stile[tix(r1, q0, p0)] = m1;
            stile[tix(r2, q0, p0)] = m2;
            stile[tix(r3, q0, p0)] = m3;
            stile[tix(r0, q1, p1)] = m4;
            stile[tix(r1, q1, p1)] = m5;
            stile[tix(r2, q1, p1)] = m6;
            stile[tix(r3, q1, p1)] = m7;
        }
    }
    __syncthreads();

    const half2 rs2 = __float2half2_rn(r_scale);
    constexpr int CHUNKS_PW = 32 / (RH_THREADS / 32);
    #pragma unroll
    for (int qq = 0; qq < CHUNKS_PW; ++qq)
    {
        int q = warp_id * CHUNKS_PW + qq;
        int qs = q ^ lane_id;
        // a[i]/b[i]: 4 columns x row (4 * lane + i), all four rows share swizzle == lane
        half2 a[4], b[4];
        #pragma unroll
        for (int i = 0; i < 4; ++i)
        {
            half4 v = *((const half4*) (stile + (lane_id * 4 + i) * 64 + qs * 2));
            a[i] = v.x;
            b[i] = v.y;
        }
        // butterfly over the 4 rows, two columns at a time, fp16 with pre-applied scale
        #pragma unroll
        for (int x = 0; x < 2; ++x)
        {
            half2* v = x == 0 ? a : b;
            half2 s0 = __hadd2(v[0], v[1]), d0 = __hsub2(v[0], v[1]);
            half2 s1 = __hadd2(v[2], v[3]), d1 = __hsub2(v[2], v[3]);
            v[0] = __hmul2(__hadd2(s0, s1), rs2);
            v[1] = __hmul2(__hadd2(d0, d1), rs2);
            v[2] = __hmul2(__hsub2(s0, s1), rs2);
            v[3] = __hmul2(__hsub2(d0, d1), rs2);
            #pragma unroll
            for (int i = 0; i < 4; ++i)
                v[i] = shuffle_had_h2x32(v[i], lane_id);
        }
        #pragma unroll
        for (int i = 0; i < 4; ++i)
        {
            half4 v;
            v.x = a[i];
            v.y = b[i];
            *((half4*) (stile + (lane_id * 4 + i) * 64 + qs * 2)) = v;
        }
    }
    __syncthreads();

    // Row transform fused with the store: after the lane butterfly, lane l holds the
    // FINAL columns 4l..4l+3 of row R. Apply the sign scales in registers and write the
    // coalesced 256-byte row directly.
    constexpr int ROWS_PW = 128 / (RH_THREADS / 32);
    const half4 sv4 = ((const half4*) svh)[nb * 32 + lane_id];
    #pragma unroll
    for (int rr = 0; rr < ROWS_PW; ++rr)
    {
        int R = warp_id * ROWS_PW + rr;
        int base = R * 64 + (lane_id ^ ((R >> 2) & 31)) * 2;
        half2 v01 = stile[base];
        half2 v23 = stile[base + 1];
        float v0 = __low2float(v01), v1 = __high2float(v01);
        float v2 = __low2float(v23), v3 = __high2float(v23);
        float s0 = v0 + v1, d0 = v0 - v1;
        float s1 = v2 + v3, d1 = v2 - v3;
        half2 h01 = __hmul2(__floats2half2_rn(s0 + s1, d0 + d1), rs2);
        half2 h23 = __hmul2(__floats2half2_rn(s0 - s1, d0 - d1), rs2);
        h01 = shuffle_had_h2x32(h01, lane_id);
        h23 = shuffle_had_h2x32(h23, lane_id);
        half2 su2 = __half2half2(suh[kb * 128 + R]);
        half4 v;
        v.x = __hmul2(__hmul2(h01, su2), sv4.x);
        v.y = __hmul2(__hmul2(h23, su2), sv4.y);
        *((half4*) (g_unpacked + (size_t) (kb * 128 + R) * row_len + nb * 128 + lane_id * 4)) = v;
    }
}


template <int K, int cb, bool HALF = false>
__global__ __launch_bounds__(RH_THREADS)
void reconstruct_had_kernel
(
    half* __restrict__ g_unpacked,
    const uint16_t* __restrict__ g_packed,
    const half* __restrict__ suh,
    const half* __restrict__ svh,
    int packed_blocks_n,
    int packed_n_offset
)
{
    reconstruct_had_tile<K, cb, HALF>(g_unpacked, g_packed, suh, svh, packed_blocks_n, packed_n_offset);
}

#if !defined(USE_ROCM)
// Batched variant: blockIdx.z selects the matrix from per-matrix pointer tables, the outputs
// are consecutive [k, n] slabs out_stride halfs apart. Whole matrices only (no n slicing).
template <int K, int cb, bool HALF = false>
__global__ __launch_bounds__(RH_THREADS)
void reconstruct_had_batch_kernel
(
    half* __restrict__ g_unpacked,
    const uint16_t* const* packed_ptrs,
    const half* const* suh_ptrs,
    const half* const* svh_ptrs,
    int packed_blocks_n,
    size_t out_stride
)
{
    int b = blockIdx.z;
    reconstruct_had_tile<K, cb, HALF>
    (
        g_unpacked + (size_t) b * out_stride,
        packed_ptrs[b],
        suh_ptrs[b],
        svh_ptrs[b],
        packed_blocks_n,
        0
    );
}

#define __(i, cb) reconstruct_had_batch_kernel<i, cb>
constexpr auto reconstruct_had_batch_kernel_instances = std::array
{
    __(1, 0), __(2, 0), __(3, 0), __(4, 0), __(5, 0), __(6, 0), __(7, 0), __(8, 0),
    __(1, 1), __(2, 1), __(3, 1), __(4, 1), __(5, 1), __(6, 1), __(7, 1), __(8, 1),
    __(1, 2), __(2, 2), __(3, 2), __(4, 2), __(5, 2), __(6, 2), __(7, 2), __(8, 2),
    reconstruct_had_batch_kernel<1, 2, true>, reconstruct_had_batch_kernel<2, 2, true>, reconstruct_had_batch_kernel<3, 2, true>
};
#endif // USE_ROCM
#undef __

#define __(i, cb) reconstruct_had_kernel<i, cb>
constexpr auto reconstruct_had_kernel_instances = std::array
{
    __(1, 0), __(2, 0), __(3, 0), __(4, 0), __(5, 0), __(6, 0), __(7, 0), __(8, 0),
    __(1, 1), __(2, 1), __(3, 1), __(4, 1), __(5, 1), __(6, 1), __(7, 1), __(8, 1),
    __(1, 2), __(2, 2), __(3, 2), __(4, 2), __(5, 2), __(6, 2), __(7, 2), __(8, 2),
    reconstruct_had_kernel<1, 2, true>, reconstruct_had_kernel<2, 2, true>, reconstruct_had_kernel<3, 2, true>
};
#undef __

/*
Reconstruct in the original (unrotated) basis: fused suh/H/W_hat/H/svh. Both dims must be
multiples of 128. n_offset slices along n; svh must be pre-offset by the caller.
*/
void reconstruct_had_slice
(
    at::Tensor unpacked,
    at::Tensor packed,
    at::Tensor suh,
    at::Tensor svh,
    float K_,
    bool mcg,
    bool mul1,
    int64_t n_offset
)
{
    const at::cuda::OptionalCUDAGuard device_guard(unpacked.device());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream().stream();
    const BitsK bk = bits_from_K(K_);
    const int K = bk.bits;
    TORCH_CHECK(!bk.half || mul1, "reconstruct: half-integer bitrates require the mul1 codebook");

    TORCH_CHECK_SHAPES(unpacked, 0, packed, 0, 16);
    TORCH_CHECK_SIZE(packed, 2, 16 * K + (bk.half ? 8 : 0));
    TORCH_CHECK_DTYPE(unpacked, kHalf);
    TORCH_CHECK_DTYPE(suh, kHalf);
    TORCH_CHECK_DTYPE(svh, kHalf);

    if (unpacked.numel() == 0)
        return;

    TORCH_CHECK(unpacked.size(0) % 128 == 0, "reconstruct_had: K dimension must be divisible by 128");
    TORCH_CHECK(unpacked.size(1) % 128 == 0, "reconstruct_had: N dimension must be divisible by 128");
    TORCH_CHECK(n_offset % 128 == 0, "n_offset must be divisible by 128");
    TORCH_CHECK(n_offset >= 0, "n_offset must be non-negative");
    TORCH_CHECK(n_offset + unpacked.size(1) <= packed.size(1) * 16, "reconstruct slice exceeds packed tensor bounds");
    TORCH_CHECK(suh.numel() >= unpacked.size(0), "reconstruct_had: suh size");
    TORCH_CHECK(svh.numel() >= unpacked.size(1), "reconstruct_had: svh size");

    dim3 blockDim(256);
    dim3 gridDim(unpacked.size(1) / 128, unpacked.size(0) / 128);

    int cbi = K - 1;
    if (bk.half) cbi += 24;
    else if (mcg) cbi += 8;
    else if (mul1) cbi += 16;
    TORCH_CHECK(cbi >= 0 && cbi < (int) reconstruct_had_kernel_instances.size(),
                "kernel index out of range: ", cbi);

    auto reconstruct_had_kernel = reconstruct_had_kernel_instances[cbi];
    reconstruct_had_kernel<<<gridDim, RH_THREADS, 0, stream>>>
    (
        (half*) unpacked.data_ptr(),
        (const uint16_t*) packed.data_ptr(),
        (const half*) suh.data_ptr(),
        (const half*) svh.data_ptr(),
        packed.size(1),
        (int) (n_offset / 16)
    );
    cuda_check(cudaPeekAtLastError());
}

void reconstruct
(
    at::Tensor unpacked,
    at::Tensor packed,
    float K,
    bool mcg,
    bool mul1
)
{
    TORCH_CHECK_SHAPES(unpacked, 1, packed, 1, 16);
    reconstruct_slice(unpacked, packed, K, mcg, mul1, 0);
}


#if !defined(USE_ROCM)
/*
Batched reconstruct_had over B whole matrices of one shape: unpacked is [B, k, n] fp16
(contiguous), packed_ptrs / suh_ptrs / svh_ptrs are int64 device tensors of B addresses of
[k/16, n/16, 16K] trellis tensors and their k-length suh / n-length svh vectors. One launch
dequantizes all B matrices into the original basis (see reconstruct_had_slice).
*/
void reconstruct_had_batch
(
    at::Tensor unpacked,
    at::Tensor packed_ptrs,
    at::Tensor suh_ptrs,
    at::Tensor svh_ptrs,
    float K_,
    bool mcg,
    bool mul1
)
{
    const at::cuda::OptionalCUDAGuard device_guard(unpacked.device());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream().stream();
    const BitsK bk = bits_from_K(K_);
    const int K = bk.bits;
    TORCH_CHECK(!bk.half || mul1, "reconstruct: half-integer bitrates require the mul1 codebook");

    TORCH_CHECK_DTYPE(unpacked, kHalf);
    TORCH_CHECK_DIM(unpacked, 3);
    TORCH_CHECK(unpacked.is_contiguous(), "reconstruct_had_batch: unpacked must be contiguous");
    TORCH_CHECK_DTYPE(packed_ptrs, kLong);
    TORCH_CHECK_DTYPE(suh_ptrs, kLong);
    TORCH_CHECK_DTYPE(svh_ptrs, kLong);
    TORCH_CHECK(packed_ptrs.device() == unpacked.device(), "reconstruct_had_batch: pointer tables must be on the output device");
    TORCH_CHECK(packed_ptrs.is_contiguous() && suh_ptrs.is_contiguous() && svh_ptrs.is_contiguous(),
                "reconstruct_had_batch: pointer tables must be contiguous");

    int batch = unpacked.size(0);
    int k = unpacked.size(1);
    int n = unpacked.size(2);
    TORCH_CHECK(packed_ptrs.numel() >= batch && suh_ptrs.numel() >= batch && svh_ptrs.numel() >= batch,
                "reconstruct_had_batch: pointer table too short");
    if (!batch || !unpacked.numel())
        return;
    TORCH_CHECK(k % 128 == 0, "reconstruct_had_batch: K dimension must be divisible by 128");
    TORCH_CHECK(n % 128 == 0, "reconstruct_had_batch: N dimension must be divisible by 128");

    dim3 blockDim(RH_THREADS);
    dim3 gridDim(n / 128, k / 128, batch);

    int cbi = K - 1;
    if (bk.half) cbi += 24;
    else if (mcg) cbi += 8;
    else if (mul1) cbi += 16;
    TORCH_CHECK(cbi >= 0 && cbi < (int) reconstruct_had_batch_kernel_instances.size(),
                "kernel index out of range: ", cbi);

    auto kernel = reconstruct_had_batch_kernel_instances[cbi];
    kernel<<<gridDim, blockDim, 0, stream>>>
    (
        (half*) unpacked.data_ptr(),
        (const uint16_t* const*) packed_ptrs.data_ptr(),
        (const half* const*) suh_ptrs.data_ptr(),
        (const half* const*) svh_ptrs.data_ptr(),
        n / 16,
        (size_t) k * n
    );
    cuda_check(cudaPeekAtLastError());
}


/*
Batched reconstruct over B whole matrices of one shape: unpacked is [B, k, n] fp16 (contiguous),
packed_ptrs an int64 device tensor of B addresses of [k/16, n/16, 16K] trellis tensors. One
launch dequantizes all B matrices (rotated basis, as reconstruct).
*/
void reconstruct_batch
(
    at::Tensor unpacked,
    at::Tensor packed_ptrs,
    float K_,
    bool mcg,
    bool mul1
)
{
    const at::cuda::OptionalCUDAGuard device_guard(unpacked.device());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream().stream();
    const BitsK bk = bits_from_K(K_);
    const int K = bk.bits;
    TORCH_CHECK(!bk.half || mul1, "reconstruct: half-integer bitrates require the mul1 codebook");

    TORCH_CHECK_DTYPE(unpacked, kHalf);
    TORCH_CHECK_DIM(unpacked, 3);
    TORCH_CHECK(unpacked.is_contiguous(), "reconstruct_batch: unpacked must be contiguous");
    TORCH_CHECK_DTYPE(packed_ptrs, kLong);
    TORCH_CHECK(packed_ptrs.device() == unpacked.device(), "reconstruct_batch: pointer table must be on the output device");
    TORCH_CHECK(packed_ptrs.is_contiguous(), "reconstruct_batch: pointer table must be contiguous");

    int batch = unpacked.size(0);
    int k = unpacked.size(1);
    int n = unpacked.size(2);
    TORCH_CHECK(packed_ptrs.numel() >= batch, "reconstruct_batch: pointer table too short");
    if (!batch || !unpacked.numel())
        return;
    TORCH_CHECK(k % 16 == 0, "reconstruct_batch: K dimension must be divisible by 16");
    TORCH_CHECK(n % 128 == 0, "reconstruct_batch: N dimension must be divisible by 128");

    dim3 blockDim(256);
    dim3 gridDim(n / 128, k / 16, batch);

    int cbi = K - 1;
    if (bk.half) cbi += 24;
    else if (mcg) cbi += 8;
    else if (mul1) cbi += 16;
    TORCH_CHECK(cbi >= 0 && cbi < (int) reconstruct_batch_kernel_instances.size(),
                "kernel index out of range: ", cbi);

    auto kernel = reconstruct_batch_kernel_instances[cbi];
    kernel<<<gridDim, blockDim, 0, stream>>>
    (
        (half*) unpacked.data_ptr(),
        (const uint16_t* const*) packed_ptrs.data_ptr(),
        n / 16,
        (size_t) k * n
    );
    cuda_check(cudaPeekAtLastError());
}
#endif // USE_ROCM