Merge f-dsv41-cuk into f-dsv41-rel (per-device light-set scratch and tensor-core check, faster IQ1_M/IQ2_S/IQ3_XXS token kernels)
This commit is contained in:
commit
d689e6080d
3 files changed
+309
-60
No files matched your search
@@ -36,6 +36,7 @@ inline hipError_t hipMemcpy(T* dst, const T* src, size_t n, int kind) {
|
||||
}
|
||||
inline hipError_t hipMemset(void* p, int v, size_t n) { return cudaMemset(p, v, n); }
|
||||
inline hipError_t hipDeviceSynchronize() { return cudaDeviceSynchronize(); }
|
||||
inline hipError_t hipSetDevice(int d) { return cudaSetDevice(d); }
|
||||
inline const char* hipGetErrorString(hipError_t e) { return cudaGetErrorString(e); }
|
||||
inline hipError_t hipEventCreate(hipEvent_t* e) { return cudaEventCreate(e); }
|
||||
inline hipError_t hipEventRecord(hipEvent_t e, hipStream_t s = nullptr) { return cudaEventRecord(e, s); }
|
||||
|
||||
+166
-58
@@ -15,8 +15,10 @@
|
||||
#include "ggml-common.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -143,10 +145,9 @@ struct Sub {
|
||||
float d, m0, m1;
|
||||
int s0, s1;
|
||||
};
|
||||
// (b: the block's address, ib: the sub-block in it (0 for the 32-value types); the token kernels keep ib fixed a lane)
|
||||
template <int T>
|
||||
__device__ __forceinline__ void decode(const uint8_t* __restrict__ row, int kb, const uint32_t* tab, Sub& s) {
|
||||
const int ib = kb & 7;
|
||||
const uint8_t* b = row + (size_t) (kQK<T> == 32 ? kb : kb >> 3) * Ty<T>::B;
|
||||
__device__ __forceinline__ void decode_b(const uint8_t* __restrict__ b, int ib, const uint32_t* tab, Sub& s) {
|
||||
if constexpr (T == kMXFP4) { // codes: value j in byte j's low nibble, j + 16 in its high one (any alignment)
|
||||
const uint32_t t[4] = {0x03020100u, 0x0C080604u, 0xFDFEFF00u, 0xF4F8FAFCu}; // ggml's kvalues_mxfp4
|
||||
#pragma unroll
|
||||
@@ -340,6 +341,10 @@ __device__ __forceinline__ void decode(const uint8_t* __restrict__ row, int kb,
|
||||
s.d = h2f(b + 108);
|
||||
}
|
||||
}
|
||||
template <int T>
|
||||
__device__ __forceinline__ void decode(const uint8_t* __restrict__ row, int kb, const uint32_t* tab, Sub& s) {
|
||||
decode_b<T>(row + (size_t) (kQK<T> == 32 ? kb : kb >> 3) * Ty<T>::B, kb & 7, tab, s);
|
||||
}
|
||||
// the sub-block against 32 int8 activations (xa: values 0..15, xb: 16..31), before their scale: an exact integer dot,
|
||||
// its scales in F32 (explicit roundings: every instantiation the same bits)
|
||||
template <int T>
|
||||
@@ -489,6 +494,57 @@ __device__ __forceinline__ void deq8(const uint8_t* __restrict__ row, int kb, in
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------- the token kernels' faster decode (CUDA only)
|
||||
// The token kernels (gate_up_kernel / down_kernel) are bound by instructions per weight, not by memory, so on CUDA a type may
|
||||
// decode through a larger shared-memory table that folds more of the work into one lookup. The arithmetic is unchanged
|
||||
// (the same integer codes into the same dp4a sums, the same F32 scales in the same order), so the results are the same bits.
|
||||
#if !defined(STRATA_USE_HIP)
|
||||
template <int T> struct Fast { static constexpr int WORDS = 0; }; // 0: this type decodes as decode<T>
|
||||
// IQ1_M: the 12-bit (neg, 11-bit grid index) -> its 8 int8 codes (iq1_codes with the sub-block delta's sign), 2 words
|
||||
template <> struct Fast<kIQ1_M> { static constexpr int WORDS = 8192; };
|
||||
#else
|
||||
template <int T> struct Fast { static constexpr int WORDS = 0; };
|
||||
#endif
|
||||
template <int T> constexpr int kTabWords = Fast<T>::WORDS > 0 ? Fast<T>::WORDS : (Ty<T>::TAB > 0 ? Ty<T>::TAB : 1);
|
||||
|
||||
template <int T>
|
||||
__device__ __forceinline__ void load_tab_fast(uint32_t* tab) {
|
||||
if constexpr (Fast<T>::WORDS == 0) {
|
||||
load_tab<T>(tab);
|
||||
} else if constexpr (T == kIQ1_M) {
|
||||
for (int j = threadIdx.x; j < 4096; j += blockDim.x) {
|
||||
const uint32_t g = iq1s_grid_gpu[j & 2047];
|
||||
const bool neg = (j & 2048) != 0;
|
||||
((uint2*) tab)[j] = make_uint2(iq1_codes(g & 0x0F0F0F0Fu, neg), iq1_codes((g >> 4) & 0x0F0F0F0Fu, neg));
|
||||
}
|
||||
}
|
||||
}
|
||||
template <int T>
|
||||
__device__ __forceinline__ void decode_fast_b(const uint8_t* __restrict__ b, int ib, const uint32_t* tab, Sub& s) {
|
||||
if constexpr (Fast<T>::WORDS == 0) {
|
||||
decode_b<T>(b, ib, tab, s);
|
||||
} else if constexpr (T == kIQ1_M) { // (56-byte blocks in 1120-byte rows at 256-aligned offsets: 8-byte aligned)
|
||||
const uint32_t qsw = *(const uint32_t*) (b + 4 * ib);
|
||||
const uint32_t qhw = *(const uint16_t*) (b + 32 + 2 * ib);
|
||||
const uint2 sw = *(const uint2*) (b + 48);
|
||||
#pragma unroll
|
||||
for (int l = 0; l < 4; ++l) {
|
||||
const uint32_t i = ((qsw >> (8 * l)) & 255u) | ((l < 3 ? qhw << (8 - 4 * l) : qhw >> 4) & 0xF00u);
|
||||
const uint2 c = ((const uint2*) tab)[i];
|
||||
s.q[2 * l] = (int) c.x;
|
||||
s.q[2 * l + 1] = (int) c.y;
|
||||
}
|
||||
const uint32_t word = ib < 4 ? sw.x : sw.y;
|
||||
const uint32_t sc16 = (ib & 2 ? word >> 16 : word) & 0xffffu;
|
||||
const int sh = 6 * (ib & 1);
|
||||
s.s0 = 2 * (int) ((sc16 >> sh) & 7u) + 1;
|
||||
s.s1 = 2 * (int) ((sc16 >> (sh + 3)) & 7u) + 1;
|
||||
const uint32_t d16 = (sw.x >> 12 & 0xfu) | ((sw.x >> 24) & 0xf0u) | ((sw.y >> 4) & 0xf00u) | ((sw.y >> 16) & 0xf000u);
|
||||
s.d = __half2float(__ushort_as_half((uint16_t) d16)) * 0.125f;
|
||||
}
|
||||
}
|
||||
|
||||
__device__ __forceinline__ float warp_sum(float v) {
|
||||
#pragma unroll
|
||||
for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o);
|
||||
@@ -532,18 +588,18 @@ constexpr int kGuRW = 1, kDnRW = 2; // rows a warp: gate / up (n_embd 5120: 10
|
||||
template <int T, int NE, int RW = kGuRW>
|
||||
__global__ void __launch_bounds__(kThreads) gate_up_kernel(const Args a) {
|
||||
constexpr int LPR = 32 / RW;
|
||||
__shared__ uint32_t tab[Ty<T>::TAB > 0 ? Ty<T>::TAB : 1];
|
||||
constexpr int kBPS = kQK<T> == 32 ? 1 : 8, kStep = LPR / kBPS; // sub-blocks a block; blocks a lane steps
|
||||
static_assert(LPR % kBPS == 0, "a lane keeps one sub-block position");
|
||||
__shared__ __align__(16) uint32_t tab[kTabWords<T>];
|
||||
__shared__ int es[NE], xr[NE];
|
||||
const uint8_t* blob;
|
||||
int m0, m1;
|
||||
if (!group(a, blockIdx.y, blob, m0, m1)) return; // (uniform)
|
||||
load_tab<T>(tab);
|
||||
load_tab_fast<T>(tab);
|
||||
const int lane32 = threadIdx.x & 31;
|
||||
const int row = (blockIdx.x * kWarps + (threadIdx.x >> 5)) * RW + lane32 / LPR, lane = lane32 % LPR;
|
||||
const size_t rb = row_bytes_t<T>(a.n_embd);
|
||||
const uint8_t* g = blob + a.tr[0] + (size_t) row * rb;
|
||||
const uint8_t* u = blob + a.tr[1] + (size_t) row * rb;
|
||||
const int nsb = a.n_embd / 32;
|
||||
const int nsb = a.n_embd / 32, nblk = nsb / kBPS;
|
||||
const int ntile = a.n_ff / (kWarps * RW);
|
||||
for (int mm = m0; mm < m1; mm += NE) {
|
||||
const int ne = min(NE, m1 - mm);
|
||||
__syncthreads();
|
||||
@@ -553,30 +609,38 @@ __global__ void __launch_bounds__(kThreads) gate_up_kernel(const Args a) {
|
||||
xr[threadIdx.x] = a.grp_ptr != nullptr ? e >> 3 : 0;
|
||||
}
|
||||
__syncthreads();
|
||||
if (row >= a.n_ff) continue; // (a whole warp: n_ff % (8 RW) == 0)
|
||||
float ag[NE], au[NE];
|
||||
// (a persistent grid: blockIdx.x strides over the row tiles, the codebook is loaded once a block)
|
||||
for (int tile = blockIdx.x; tile < ntile; tile += gridDim.x) {
|
||||
const int row = (tile * kWarps + (threadIdx.x >> 5)) * RW + lane32 / LPR, lane = lane32 % LPR;
|
||||
const int ib0 = kBPS == 8 ? lane & 7 : 0, blk0 = kBPS == 8 ? lane >> 3 : lane;
|
||||
const uint8_t* g = blob + a.tr[0] + (size_t) row * rb;
|
||||
const uint8_t* u = blob + a.tr[1] + (size_t) row * rb;
|
||||
float ag[NE], au[NE];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i) ag[i] = au[i] = 0.0f;
|
||||
for (int kb = lane; kb < nsb; kb += LPR) {
|
||||
Sub sg, su;
|
||||
decode<T>(g, kb, tab, sg);
|
||||
decode<T>(u, kb, tab, su);
|
||||
for (int i = 0; i < NE; ++i) ag[i] = au[i] = 0.0f;
|
||||
// a lane's sub-blocks: kb = lane + LPR j = 8 blk + ib with ib fixed (the 32-value types: blk = kb)
|
||||
for (int blk = blk0; blk < nblk; blk += kStep) {
|
||||
const int kb = blk * kBPS + ib0;
|
||||
Sub sg, su;
|
||||
decode_fast_b<T>(g + (size_t) blk * Ty<T>::B, ib0, tab, sg);
|
||||
decode_fast_b<T>(u + (size_t) blk * Ty<T>::B, ib0, tab, su);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i)
|
||||
if (i < ne) {
|
||||
const int4* xp = (const int4*) (a.xq + (size_t) xr[i] * a.n_embd + (size_t) kb * 32);
|
||||
const int4 xa = xp[0], xb = xp[1];
|
||||
const float dx = a.xd[(size_t) xr[i] * nsb + kb];
|
||||
ag[i] = fmaf(dx, sub_dot<T>(sg, xa, xb), ag[i]);
|
||||
au[i] = fmaf(dx, sub_dot<T>(su, xa, xb), au[i]);
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i)
|
||||
if (i < ne) {
|
||||
const int4* xp = (const int4*) (a.xq + (size_t) xr[i] * a.n_embd + (size_t) kb * 32);
|
||||
const int4 xa = xp[0], xb = xp[1];
|
||||
const float dx = a.xd[(size_t) xr[i] * nsb + kb];
|
||||
ag[i] = fmaf(dx, sub_dot<T>(sg, xa, xb), ag[i]);
|
||||
au[i] = fmaf(dx, sub_dot<T>(su, xa, xb), au[i]);
|
||||
const float gv = lanes_sum<RW>(ag[i]), uv = lanes_sum<RW>(au[i]);
|
||||
if (lane == 0) a.h[(size_t) es[i] * a.n_ff + row] = swiglu(gv, uv, a.limit);
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i)
|
||||
if (i < ne) {
|
||||
const float gv = lanes_sum<RW>(ag[i]), uv = lanes_sum<RW>(au[i]);
|
||||
if (lane == 0) a.h[(size_t) es[i] * a.n_ff + row] = swiglu(gv, uv, a.limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -584,42 +648,48 @@ __global__ void __launch_bounds__(kThreads) gate_up_kernel(const Args a) {
|
||||
template <int T, int NE, int RW = kDnRW>
|
||||
__global__ void __launch_bounds__(kThreads) down_kernel(const Args a) {
|
||||
constexpr int LPR = 32 / RW;
|
||||
__shared__ uint32_t tab[Ty<T>::TAB > 0 ? Ty<T>::TAB : 1];
|
||||
constexpr int kBPS = kQK<T> == 32 ? 1 : 8, kStep = LPR / kBPS; // sub-blocks a block; blocks a lane steps
|
||||
static_assert(LPR % kBPS == 0, "a lane keeps one sub-block position");
|
||||
__shared__ __align__(16) uint32_t tab[kTabWords<T>];
|
||||
__shared__ int es[NE];
|
||||
const uint8_t* blob;
|
||||
int m0, m1;
|
||||
if (!group(a, blockIdx.y, blob, m0, m1)) return;
|
||||
load_tab<T>(tab);
|
||||
load_tab_fast<T>(tab);
|
||||
const int lane32 = threadIdx.x & 31;
|
||||
const int row = (blockIdx.x * kWarps + (threadIdx.x >> 5)) * RW + lane32 / LPR, lane = lane32 % LPR;
|
||||
const uint8_t* d = blob + a.tr[2] + (size_t) row * row_bytes_t<T>(a.n_ff);
|
||||
const int nsb = a.n_ff / 32;
|
||||
const int nsb = a.n_ff / 32, nblk = nsb / kBPS;
|
||||
const int ntile = a.n_embd / (kWarps * RW);
|
||||
for (int mm = m0; mm < m1; mm += NE) {
|
||||
const int ne = min(NE, m1 - mm);
|
||||
__syncthreads();
|
||||
if ((int) threadIdx.x < ne) es[threadIdx.x] = a.grp_ptr != nullptr ? a.grp_ent[mm + threadIdx.x] : mm + threadIdx.x;
|
||||
__syncthreads();
|
||||
if (row >= a.n_embd) continue;
|
||||
float ad[NE];
|
||||
for (int tile = blockIdx.x; tile < ntile; tile += gridDim.x) {
|
||||
const int row = (tile * kWarps + (threadIdx.x >> 5)) * RW + lane32 / LPR, lane = lane32 % LPR;
|
||||
const int ib0 = kBPS == 8 ? lane & 7 : 0, blk0 = kBPS == 8 ? lane >> 3 : lane;
|
||||
const uint8_t* d = blob + a.tr[2] + (size_t) row * row_bytes_t<T>(a.n_ff);
|
||||
float ad[NE];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i) ad[i] = 0.0f;
|
||||
for (int kb = lane; kb < nsb; kb += LPR) {
|
||||
Sub sd;
|
||||
decode<T>(d, kb, tab, sd);
|
||||
for (int i = 0; i < NE; ++i) ad[i] = 0.0f;
|
||||
for (int blk = blk0; blk < nblk; blk += kStep) {
|
||||
const int kb = blk * kBPS + ib0;
|
||||
Sub sd;
|
||||
decode_fast_b<T>(d + (size_t) blk * Ty<T>::B, ib0, tab, sd);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i)
|
||||
if (i < ne) {
|
||||
const int4* hp = (const int4*) (a.hq + (size_t) es[i] * a.n_ff + (size_t) kb * 32);
|
||||
const int4 ha = hp[0], hb = hp[1];
|
||||
ad[i] = fmaf(a.hd[(size_t) es[i] * nsb + kb], sub_dot<T>(sd, ha, hb), ad[i]);
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i)
|
||||
if (i < ne) {
|
||||
const int4* hp = (const int4*) (a.hq + (size_t) es[i] * a.n_ff + (size_t) kb * 32);
|
||||
const int4 ha = hp[0], hb = hp[1];
|
||||
ad[i] = fmaf(a.hd[(size_t) es[i] * nsb + kb], sub_dot<T>(sd, ha, hb), ad[i]);
|
||||
const float v = lanes_sum<RW>(ad[i]);
|
||||
if (lane == 0) a.part[(size_t) es[i] * a.n_embd + row] = v;
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < NE; ++i)
|
||||
if (i < ne) {
|
||||
const float v = lanes_sum<RW>(ad[i]);
|
||||
if (lane == 0) a.part[(size_t) es[i] * a.n_embd + row] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -921,14 +991,20 @@ bool tc_ok() { // as ds4_gguf.cu's: sm_70 and later, STRATA_DS4_NO_TC=1 keeps
|
||||
#if defined(STRATA_USE_HIP)
|
||||
return false;
|
||||
#else
|
||||
static const bool ok = [] {
|
||||
int dev = 0, major = 0;
|
||||
cudaGetDevice(&dev);
|
||||
// (per device: the cards of a multi-GPU run need not be the same generation)
|
||||
static std::atomic<int> state[16]; // 0: unknown, 1: tensor cores, 2: the F32 kernel
|
||||
int dev = 0;
|
||||
cudaGetDevice(&dev);
|
||||
if (dev < 0 || dev >= 16) return false;
|
||||
int v = state[dev].load(std::memory_order_relaxed);
|
||||
if (v == 0) {
|
||||
int major = 0;
|
||||
cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev);
|
||||
const char* v = std::getenv("STRATA_DS4_NO_TC");
|
||||
return major >= 7 && !(v != nullptr && v[0] == '1');
|
||||
}();
|
||||
return ok;
|
||||
const char* e = std::getenv("STRATA_DS4_NO_TC");
|
||||
v = major >= 7 && !(e != nullptr && e[0] == '1') ? 1 : 2;
|
||||
state[dev].store(v, std::memory_order_relaxed);
|
||||
}
|
||||
return v == 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -951,10 +1027,40 @@ bool tc_ok() { // as ds4_gguf.cu's: sm_70 and later, STRATA_DS4_NO_TC=1 keeps
|
||||
default: return false; \
|
||||
}
|
||||
|
||||
// CUDA: a persistent grid (the resident blocks of the device, split over the groups), each block looping over row tiles with
|
||||
// its codebook loaded once; HIP keeps the full grid (one tile a block)
|
||||
template <typename K>
|
||||
int resident_ctas(K kernel) {
|
||||
#if defined(STRATA_USE_HIP)
|
||||
(void) kernel;
|
||||
return 1 << 30;
|
||||
#else
|
||||
static std::atomic<int> cache[16];
|
||||
int dev = 0;
|
||||
cudaGetDevice(&dev);
|
||||
if (dev < 0 || dev >= 16) return 1 << 30;
|
||||
int v = cache[dev].load(std::memory_order_relaxed);
|
||||
if (v == 0) {
|
||||
int sms = 0, per = 0;
|
||||
cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev);
|
||||
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per, kernel, kThreads, 0);
|
||||
if (const char* e = std::getenv("DS4_IQ_CTA_PER_SM")) per = std::min(per, std::max(1, std::atoi(e)));
|
||||
v = std::max(1, sms * std::max(per, 1));
|
||||
cache[dev].store(v, std::memory_order_relaxed);
|
||||
}
|
||||
return v;
|
||||
#endif
|
||||
}
|
||||
template <int T, int NE>
|
||||
void launch_gu(const Args& a, dim3 grid, cudaStream_t s) { gate_up_kernel<T, NE><<<grid, kThreads, 0, s>>>(a); }
|
||||
void launch_gu(const Args& a, dim3 grid, cudaStream_t s) {
|
||||
grid.x = (unsigned) std::max(1, std::min((int) grid.x, resident_ctas(gate_up_kernel<T, NE, kGuRW>) / (int) grid.y));
|
||||
gate_up_kernel<T, NE><<<grid, kThreads, 0, s>>>(a);
|
||||
}
|
||||
template <int T, int NE>
|
||||
void launch_dn(const Args& a, dim3 grid, cudaStream_t s) { down_kernel<T, NE><<<grid, kThreads, 0, s>>>(a); }
|
||||
void launch_dn(const Args& a, dim3 grid, cudaStream_t s) {
|
||||
grid.x = (unsigned) std::max(1, std::min((int) grid.x, resident_ctas(down_kernel<T, NE, kDnRW>) / (int) grid.y));
|
||||
down_kernel<T, NE><<<grid, kThreads, 0, s>>>(a);
|
||||
}
|
||||
template <int T>
|
||||
void gu_ne(const Args& a, dim3 grid, int ne, cudaStream_t s) {
|
||||
if (ne <= 1) launch_gu<T, 1>(a, grid, s);
|
||||
@@ -991,10 +1097,12 @@ struct LightBuf {
|
||||
size_t cap = 0;
|
||||
};
|
||||
std::mutex g_lmu;
|
||||
std::unordered_map<cudaStream_t, LightBuf> g_light;
|
||||
std::map<std::pair<int, cudaStream_t>, LightBuf> g_light; // (device, stream): the default stream 0 is one handle on every device
|
||||
LightBuf* light_buf(cudaStream_t s, int k) {
|
||||
std::lock_guard<std::mutex> lk(g_lmu);
|
||||
LightBuf& b = g_light[s];
|
||||
int dev = 0;
|
||||
cudaGetDevice(&dev);
|
||||
LightBuf& b = g_light[{dev, s}];
|
||||
const size_t need = (size_t) kLightMaxExp * kLightRows * (size_t) std::max(k, 8192);
|
||||
if (b.cap < need) {
|
||||
if (b.q != nullptr) cudaFree(b.q);
|
||||
|
||||
@@ -144,24 +144,155 @@ static void bench(int type) {
|
||||
(double) NE * 3 * FF * E / ((ms_gu + ms_dn) * 1e-3) / 1e12, 40.0 * (ms_gu + ms_dn));
|
||||
}
|
||||
|
||||
|
||||
// --dump F / --cmp F: every device output of a run as raw bits, to a file / against one (a kernel change that keeps the sums
|
||||
// exact and the float order must leave all of them identical)
|
||||
static FILE* g_dump = nullptr; static FILE* g_cmp = nullptr; static long g_cmp_bad = 0, g_cmp_n = 0;
|
||||
static void bits(const char* what, int type, const void* p, size_t bytes) {
|
||||
if (!g_dump && !g_cmp) return;
|
||||
if (g_dump) { std::fwrite(p, 1, bytes, g_dump); return; }
|
||||
std::vector<uint8_t> ref(bytes);
|
||||
if (std::fread(ref.data(), 1, bytes, g_cmp) != bytes) { std::printf("cmp: golden file too short at %s %s\n", iq::name(type), what); ++g_cmp_bad; return; }
|
||||
++g_cmp_n;
|
||||
if (std::memcmp(ref.data(), p, bytes) != 0) {
|
||||
size_t bad = 0, first = 0; for (size_t i = 0; i < bytes; ++i) if (ref[i] != ((const uint8_t*) p)[i]) { if (!bad) first = i; ++bad; }
|
||||
std::printf("%-7s %-22s BITS DIFFER: %zu of %zu bytes (first at %zu)\n", iq::name(type), what, bad, bytes, first);
|
||||
++g_cmp_bad;
|
||||
}
|
||||
}
|
||||
// the real shapes (n_embd 5120, n_ff 2304): the plan path (6 groups of one entry) and the grouped path (windows of 3 and 2)
|
||||
static void real_shape_bits(int type) {
|
||||
std::mt19937_64 rng(1000 + type); // (its own stream: the main loop's random experts are the same with or without this)
|
||||
const int E = 5120, FF = 2304, NG = 2, NEnt = 5;
|
||||
const size_t rb_g = iq::row_bytes(type, E), rb_d = iq::row_bytes(type, FF);
|
||||
auto al = [](size_t n) { return (n + 255) / 256 * 256; };
|
||||
const size_t off[3] = {0, al(FF * rb_g), al(FF * rb_g) * 2}, blob = off[2] + al((size_t) E * rb_d);
|
||||
std::vector<uint8_t> hb((size_t) NG * blob);
|
||||
for (auto& b : hb) b = (uint8_t) rng();
|
||||
for (size_t b = 0; b < hb.size() / (size_t) iq::block_bytes(type); ++b) { // sane F16 scales (the same placement as make_mat)
|
||||
const uint16_t d = f2h(0.02f + 0.0002f * (float) (rng() % 600));
|
||||
uint8_t* p = &hb[b * (size_t) iq::block_bytes(type)];
|
||||
if (type == 29) { p[49] = (uint8_t) ((p[49] & 0x0f) | ((d & 0x000f) << 4)); p[51] = (uint8_t) ((p[51] & 0x0f) | (d & 0x00f0)); p[53] = (uint8_t) ((p[53] & 0x0f) | ((d & 0x0f00) >> 4)); p[55] = (uint8_t) ((p[55] & 0x0f) | ((d & 0xf000) >> 8)); }
|
||||
else std::memcpy(p, &d, 2);
|
||||
}
|
||||
uint8_t* dblob = up(hb);
|
||||
std::vector<int8_t> xq(E), hq((size_t) NEnt * FF); std::vector<float> xd(E / 32), hd((size_t) NEnt * FF / 32);
|
||||
for (auto& v : xq) v = (int8_t) ((int) (rng() % 255) - 127);
|
||||
for (auto& v : hq) v = (int8_t) ((int) (rng() % 255) - 127);
|
||||
for (auto& v : xd) v = 0.002f + 0.0001f * (float) (rng() % 100);
|
||||
for (auto& v : hd) v = 0.002f + 0.0001f * (float) (rng() % 100);
|
||||
iq::Args a{};
|
||||
a.n_embd = E; a.n_ff = FF; a.limit = 10.0f; a.xq = up(xq); a.xd = up(xd); a.hq = up(hq); a.hd = up(hd); a.tg = a.td = type;
|
||||
for (int r = 0; r < 3; ++r) a.tr[r] = off[r];
|
||||
float *dh, *dp; CK(hipMalloc(&dh, (size_t) NEnt * FF * 4)); CK(hipMalloc(&dp, (size_t) NEnt * E * 4)); a.h = dh; a.part = dp;
|
||||
std::vector<unsigned long long> plan = {(unsigned long long) dblob, (unsigned long long) (dblob + blob), 0ull, (unsigned long long) dblob};
|
||||
a.plan_ptr = up(plan);
|
||||
CK(hipMemset(dh, 0, (size_t) NEnt * FF * 4)); CK(hipMemset(dp, 0, (size_t) NEnt * E * 4));
|
||||
if (!iq::gate_up(a, 4, 1, 0) || !iq::down(a, 4, 1, 0)) { std::printf("real shape: refused\n"); return; }
|
||||
CK(hipDeviceSynchronize());
|
||||
{ auto h = down_(dh, (size_t) NEnt * FF); bits("real plan h", type, h.data(), h.size() * 4); auto p = down_(dp, (size_t) NEnt * E); bits("real plan part", type, p.data(), p.size() * 4); }
|
||||
std::vector<unsigned long long> gp = {(unsigned long long) dblob, (unsigned long long) (dblob + blob)};
|
||||
std::vector<int> gs = {0, 3, 5}, ge = {0, 1, 2, 3, 4}, ng = {NG};
|
||||
a.plan_ptr = nullptr; a.grp_ptr = up(gp); a.grp_start = up(gs); a.grp_ent = up(ge); a.n_groups = up(ng);
|
||||
CK(hipMemset(dh, 0, (size_t) NEnt * FF * 4)); CK(hipMemset(dp, 0, (size_t) NEnt * E * 4));
|
||||
if (!iq::gate_up(a, NG, 3, 0) || !iq::down(a, NG, 3, 0)) { std::printf("real shape grouped: refused\n"); return; }
|
||||
CK(hipDeviceSynchronize());
|
||||
{ auto h = down_(dh, (size_t) NEnt * FF); bits("real grp h", type, h.data(), h.size() * 4); auto p = down_(dp, (size_t) NEnt * E); bits("real grp part", type, p.data(), p.size() * 4); }
|
||||
CK(hipFree(dblob));
|
||||
}
|
||||
|
||||
// the same DeepSeek decode token over a working set far past L2 (>= 640 MiB of distinct expert blobs, rotated launch by launch),
|
||||
// so the rate is DRAM's, not L2's: gate/up and down timed apart; ne = entries a group (1: the decode token)
|
||||
static void bench_big(int type, int mib) {
|
||||
const int E = 5120, FF = 2304, NE = 6;
|
||||
const size_t rb_g = iq::row_bytes(type, E), rb_d = iq::row_bytes(type, FF);
|
||||
auto al = [](size_t n) { return (n + 255) / 256 * 256; };
|
||||
const size_t off[3] = {0, al(FF * rb_g), al(FF * rb_g) * 2}, blob = off[2] + al((size_t) E * rb_d);
|
||||
const int L = std::max(2, (int) (((size_t) mib << 20) / (NE * blob)) + 1); // layers rotated
|
||||
std::mt19937_64 rng(5);
|
||||
std::vector<uint8_t> one(blob);
|
||||
for (auto& b : one) b = (uint8_t) rng();
|
||||
std::vector<unsigned long long> plan((size_t) L * NE);
|
||||
for (auto& p : plan) { uint8_t* d; CK(hipMalloc(&d, blob)); CK(hipMemcpy(d, one.data(), blob, hipMemcpyHostToDevice)); p = (unsigned long long) d; }
|
||||
std::vector<int8_t> xq(E, 3), hq((size_t) NE * FF, 3); std::vector<float> xd(E / 32, 0.01f), hd((size_t) NE * FF / 32, 0.01f);
|
||||
for (int i = 0; i < E; ++i) xq[i] = (int8_t) ((int) (rng() % 255) - 127);
|
||||
for (size_t i = 0; i < hq.size(); ++i) hq[i] = (int8_t) ((int) (rng() % 255) - 127);
|
||||
unsigned long long* dplan = up(plan);
|
||||
iq::Args a{};
|
||||
a.n_embd = E; a.n_ff = FF; a.limit = 10.0f; a.xq = up(xq); a.xd = up(xd); a.hq = up(hq); a.hd = up(hd); a.tg = a.td = type;
|
||||
for (int r = 0; r < 3; ++r) a.tr[r] = off[r];
|
||||
float *dh, *dp; CK(hipMalloc(&dh, (size_t) NE * FF * 4)); CK(hipMalloc(&dp, (size_t) NE * E * 4)); a.h = dh; a.part = dp;
|
||||
hipEvent_t e0, e1; hipEventCreate(&e0); hipEventCreate(&e1);
|
||||
double us[2] = {0, 0};
|
||||
for (int part = 0; part < 2; ++part) {
|
||||
auto run = [&](int reps) {
|
||||
hipEventRecord(e0);
|
||||
for (int rep = 0; rep < reps; ++rep) {
|
||||
a.plan_ptr = dplan + (size_t) (rep % L) * NE;
|
||||
if (part == 0) iq::gate_up(a, NE, 1, 0); else iq::down(a, NE, 1, 0);
|
||||
}
|
||||
hipEventRecord(e1); hipEventSynchronize(e1);
|
||||
float ms; hipEventElapsedTime(&ms, e0, e1);
|
||||
return (double) ms;
|
||||
};
|
||||
double ms = 0; int reps = 2 * L;
|
||||
while (ms < 300.0) { ms = run(reps); reps *= 2; } // warm-up: clocks ramp, ~300 ms
|
||||
const int n = std::max(L, (int) (reps / 2 * 40.0 / ms)); // ~40 ms a window
|
||||
double best = 1e30;
|
||||
for (int w = 0; w < 7; ++w) best = std::min(best, run(n) * 1e3 / n); // the best of 7 windows (clocks/thermals wander)
|
||||
us[part] = best;
|
||||
}
|
||||
const double bg = (double) NE * (off[2]) , bd = (double) NE * al((size_t) E * rb_d); // bytes read a launch
|
||||
std::printf("big %-7s ws %4.0f MiB: gate/up %6.1f us (%5.0f GB/s) down %6.1f us (%5.0f GB/s) layer %6.1f us -> %5.0f GB/s, %.2f Tw/s\n",
|
||||
iq::name(type), (double) L * NE * blob / 1048576.0, us[0], bg / us[0] * 1e-3, us[1], bd / us[1] * 1e-3, us[0] + us[1],
|
||||
(bg + bd) / (us[0] + us[1]) * 1e-3, (double) NE * 3 * FF * E / ((us[0] + us[1]) * 1e-6) / 1e12);
|
||||
for (auto p : plan) hipFree((void*) p);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::vector<int> types = {16, 17, 22, 18, 21, 23, 29};
|
||||
std::vector<int> devs;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (std::string(argv[i]) == "--bench") {
|
||||
if (std::string(argv[i]) == "--bench-big") { // [types] [MiB]
|
||||
std::vector<int> bt; int mib = 640;
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') { for (const char* p = argv[++i]; *p;) { bt.push_back(std::atoi(p)); while (*p && *p != ',') ++p; if (*p) ++p; } }
|
||||
else bt = types;
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') mib = std::atoi(argv[++i]);
|
||||
for (int t : bt) bench_big(t, mib);
|
||||
return 0;
|
||||
} else if (std::string(argv[i]) == "--bench") {
|
||||
std::vector<int> bt;
|
||||
if (i + 1 < argc && argv[i + 1][0] != '-') { for (const char* p = argv[++i]; *p;) { bt.push_back(std::atoi(p)); while (*p && *p != ',') ++p; if (*p) ++p; } }
|
||||
else bt = types;
|
||||
for (int t : bt) bench(t);
|
||||
return 0;
|
||||
} else if (std::string(argv[i]) == "--devices" && i + 1 < argc) { // every check on each device in turn, in this process;
|
||||
devs.clear(); for (const char* p = argv[++i]; *p;) { devs.push_back(std::atoi(p)); while (*p && *p != ',') ++p; if (*p) ++p; } // the later ones bit-compared to the first
|
||||
} else if (std::string(argv[i]) == "--dump" && i + 1 < argc) {
|
||||
g_dump = std::fopen(argv[++i], "wb"); if (!g_dump) { std::printf("cannot write %s\n", argv[i]); return 2; }
|
||||
} else if (std::string(argv[i]) == "--cmp" && i + 1 < argc) {
|
||||
g_cmp = std::fopen(argv[++i], "rb"); if (!g_cmp) { std::printf("cannot read %s\n", argv[i]); return 2; }
|
||||
} else if (std::string(argv[i]) == "--type" && i + 1 < argc) {
|
||||
types.clear(); for (const char* p = argv[++i]; *p;) { types.push_back(std::atoi(p)); while (*p && *p != ',') ++p; if (*p) ++p; }
|
||||
} else if (std::string(argv[i]) == "--iq256" && i + 1 < argc) {
|
||||
gg::gg_cpu_set_iq256(std::atoi(argv[++i]));
|
||||
}
|
||||
}
|
||||
int fails = 0;
|
||||
if (devs.size() > 1) { // the first device writes its outputs, the others compare to them
|
||||
if (g_dump || g_cmp) { std::printf("--devices takes neither --dump nor --cmp\n"); return 2; }
|
||||
}
|
||||
for (size_t di = 0; di < std::max<size_t>(1, devs.size()); ++di) {
|
||||
if (!devs.empty()) {
|
||||
CK(hipSetDevice(devs[di]));
|
||||
if (devs.size() > 1) {
|
||||
if (g_dump) { std::fclose(g_dump); g_dump = nullptr; }
|
||||
if (di == 0) g_dump = std::fopen("iq_devices.bin", "wb"); else { g_cmp = std::fopen("iq_devices.bin", "rb"); g_cmp_n = g_cmp_bad = 0; }
|
||||
}
|
||||
std::printf("---- device %d\n", devs[di]);
|
||||
}
|
||||
std::mt19937_64 rng(11);
|
||||
std::normal_distribution<float> nd(0.f, 1.f);
|
||||
int fails = 0;
|
||||
auto report = [&](const char* what, int type, double worst, double tol) {
|
||||
const bool ok = worst <= tol;
|
||||
std::printf("%-7s %-22s worst %.2e (tol %.0e) %s\n", iq::name(type), what, worst, tol, ok ? "ok" : "FAIL");
|
||||
@@ -218,6 +349,7 @@ int main(int argc, char** argv) {
|
||||
if (!iq::gate_up(a, NEXP, 1, 0)) { std::printf("gate_up refused\n"); return 1; }
|
||||
CK(hipDeviceSynchronize());
|
||||
const auto h = down_(dh, (size_t) NEXP * FF);
|
||||
bits("token h", type, h.data(), h.size() * 4);
|
||||
double worst = 0;
|
||||
for (int e = 0; e < NEXP; ++e)
|
||||
for (int r = 0; r < FF; ++r) {
|
||||
@@ -233,6 +365,7 @@ int main(int argc, char** argv) {
|
||||
if (!iq::down(a, NEXP, 1, 0)) { std::printf("down refused\n"); return 1; }
|
||||
CK(hipDeviceSynchronize());
|
||||
const auto part = down_(dp, (size_t) NEXP * E);
|
||||
bits("token part", type, part.data(), part.size() * 4);
|
||||
worst = 0;
|
||||
for (int e = 0; e < NEXP; ++e)
|
||||
for (int r = 0; r < E; ++r) {
|
||||
@@ -253,6 +386,7 @@ int main(int argc, char** argv) {
|
||||
if (!iq::set_gate_up(q, 0)) { std::printf("set_gate_up refused\n"); return 1; }
|
||||
CK(hipDeviceSynchronize());
|
||||
const auto h16 = down_(dh16, (size_t) NR * FF);
|
||||
bits("set h16", type, h16.data(), h16.size() * 2);
|
||||
double w2 = 0;
|
||||
for (int e = 0; e < 3; ++e)
|
||||
for (int lr = bounds[e]; lr < bounds[e + 1]; ++lr)
|
||||
@@ -267,6 +401,8 @@ int main(int argc, char** argv) {
|
||||
if (!iq::set_down(q, 0)) { std::printf("set_down refused\n"); return 1; }
|
||||
CK(hipDeviceSynchronize());
|
||||
const auto y = down_(dy, (size_t) NR * E);
|
||||
bits("set y", type, y.data(), y.size() * 4);
|
||||
real_shape_bits(type);
|
||||
w2 = 0;
|
||||
for (int e = 0; e < 3; ++e)
|
||||
for (int lr = bounds[e]; lr < bounds[e + 1]; ++lr) {
|
||||
@@ -316,6 +452,10 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
CK(hipFree(dblob));
|
||||
}
|
||||
if (g_cmp && devs.size() > 1) { std::fclose(g_cmp); g_cmp = nullptr; std::printf("device %d vs device %d: %ld buffers, %ld differ\n", devs[di], devs[0], g_cmp_n, g_cmp_bad); fails += g_cmp_bad != 0; }
|
||||
}
|
||||
if (g_cmp) { std::printf("bit-compare vs golden: %ld buffers, %ld differ -> %s\n", g_cmp_n, g_cmp_bad, g_cmp_bad ? "DIFFERENT" : "IDENTICAL"); fails += g_cmp_bad != 0; }
|
||||
if (g_dump) std::fclose(g_dump);
|
||||
std::printf("%s\n", fails ? "FAILED" : "ALL OK");
|
||||
return fails;
|
||||
}
|
||||
Reference in new issue
Block a user