Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions engine/cmake/strata_poly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
set_tests_properties(poly_iq3_lanes_test PROPERTIES SKIP_RETURN_CODE 77)
endif()
# a token's attention products in one launch (native_mmvq_mixed(), native_mmvq_many()) against native_mmvq() a
# matrix, bit for bit, on random blocks (no model); CUDA only, needs a card
if(STRATA_ENABLE_CUDA)
# matrix, bit for bit, on random blocks (no model); needs a card. On HIP the native_mmvq_many() cases alone
if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
add_executable(poly_project_mixed_test src/poly/project_mixed_test.cu)
target_include_directories(poly_project_mixed_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
${_strata_gpu_include_directories})
Expand Down
59 changes: 43 additions & 16 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1020,9 +1020,16 @@ bool Inkling2::project(const Block& B, const float* const* x, int n, bool quanti
const bool mixed = n == 1 && mixed_on;
#else
constexpr bool mixed = false;
#endif
#if defined(STRATA_USE_HIP)
// a token's on HIP: a launch for each type among the five, as on Metal (below)
const bool by_type = n == 1;
#else
constexpr bool by_type = false;
#endif
for (const Dev* d : w)
if ((kOneType && !mixed && d->type != w[0]->type) || d->n_in != n_embd_ || d->scale != 1.0f) return false;
if ((kOneType && !mixed && !by_type && d->type != w[0]->type) || d->n_in != n_embd_ || d->scale != 1.0f)
return false;
const float* xs[3];
void* qs[3];
int nx = 0, slot[3];
Expand All @@ -1036,32 +1043,52 @@ bool Inkling2::project(const Block& B, const float* const* x, int n, bool quanti
}
slot[j] = k;
}
#if defined(STRATA_GPU_METAL)
#if defined(STRATA_GPU_METAL) || defined(STRATA_USE_HIP)
// A token's: native_mmvq_many() of the matrices of each type among the five (Unsloth's files mix Q8_0 with K-quants
// and i-quants in a layer), on the inputs quantized once; a type it does not take, lin() of each.
// and i-quants in a layer), on the inputs quantized once; a type it does not take, lin() of each. On HIP too,
// where native_mmvq_mixed() is not built: else each of the five quantized its input again for its own launch.
if (n == 1) {
const Dev* wa[8] = {w[0], w[1], w[2], w[3], w[4]};
float* ya[8] = {y[0], y[1], y[2], y[3], y[4]};
int fa[8] = {from[0], from[1], from[2], from[3], from[4]};
int count = 5;
#if defined(STRATA_USE_HIP)
// on HIP the sink's (input 0) and a full layer's taps (inputs 1 and 2) join where they are quantized matrices
// of n_embd_ inputs (the ggml-org files' Q8_0), as native_mmvq_mixed()'s launch takes them on CUDA
const auto plain = [&](const Dev& d) {
return d.p && d.type != kTypeF32 && d.type != kTypeBF16 && d.n_in == n_embd_ && d.scale == 1.0f;
};
if (extras && plain(B.sink) && (B.swa || (plain(B.k_dyn) && plain(B.v_dyn)))) {
wa[count] = &B.sink, ya[count] = sink_, fa[count++] = 0;
if (!B.swa) {
wa[count] = &B.k_dyn, ya[count] = kdyn_, fa[count++] = 1;
wa[count] = &B.v_dyn, ya[count] = vdyn_, fa[count++] = 2;
}
*extras = true;
}
#endif
if (!quantized) kernels::native_quantize_q8_1_many(xs, qs, nx, n_embd_, st_);
bool done[5] = {};
for (int i = 0; i < 5; ++i) {
bool done[8] = {};
for (int i = 0; i < count; ++i) {
if (done[i]) continue;
kernels::NativeMmvqMany m{};
int at[5], c = 0;
for (int j = i; j < 5; ++j) {
if (done[j] || w[j]->type != w[i]->type) continue;
m.w[c] = w[j]->p;
m.x_q8_1[c] = qs[slot[from[j]]];
m.y[c] = y[j];
m.n_out[c] = w[j]->n_out;
int at[8], c = 0;
for (int j = i; j < count; ++j) {
if (done[j] || wa[j]->type != wa[i]->type) continue;
m.w[c] = wa[j]->p;
m.x_q8_1[c] = qs[slot[fa[j]]];
m.y[c] = ya[j];
m.n_out[c] = wa[j]->n_out;
at[c++] = j;
done[j] = true;
}
m.count = c;
if (w[i]->type != kTypeF32 && w[i]->type != kTypeBF16 &&
kernels::native_mmvq_many_fits((int) w[i]->type, m, n_embd_)) {
kernels::native_mmvq_many((int) w[i]->type, m, n_embd_, st_);
if (wa[i]->type != kTypeF32 && wa[i]->type != kTypeBF16 &&
kernels::native_mmvq_many_fits((int) wa[i]->type, m, n_embd_)) {
kernels::native_mmvq_many((int) wa[i]->type, m, n_embd_, st_);
continue;
}
for (int k = 0; k < c; ++k) lin(*w[at[k]], x[from[at[k]]], y[at[k]], n, qs[slot[from[at[k]]]]);
for (int k = 0; k < c; ++k) lin(*wa[at[k]], x[fa[at[k]]], ya[at[k]], n, qs[slot[fa[at[k]]]]);
}
return true;
}
Expand Down
58 changes: 56 additions & 2 deletions engine/src/poly/project_mixed_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// one native_mmvq() a matrix, to the bit: native_mmvq_mixed() for Q8_0, Q4_K, Q5_K and Q6_K matrices of one width
// (the five of Unsloth's layers, which mix them, plus the sink and the two convolution taps), and native_mmvq_many()
// for the eight of a ggml-org layer, all Q8_0. Random blocks, each matrix on a Q8_1 input of its own or shared.
// On HIP (no native_mmvq_mixed() there): native_mmvq_many() of each type it takes, Q8_0, Q4_0, Q5_0 and IQ4_NL, the
// five's shapes and the eight's, as project() takes them a type at a time, and native_quantize_q8_1_many() against
// native_quantize_q8_1() of each input.
// --bench also times the launches a token's products took before (one quantize and one product a matrix, or the
// five in one launch and three alone) against the one launch.
//
Expand Down Expand Up @@ -45,7 +48,8 @@ template <typename T> T* up(const std::vector<T>& v) {
// random bytes with sane fp16 scales where each format keeps them: Q8_0's d at a 32-value block's start, Q4_K's and
// Q5_K's d and dmin at a 256-value block's start, Q6_K's d at its end
void* random_weights(int type, int n_in, int n_out) {
const int qk = type == 8 ? 32 : 256;
const bool small = type == 8 || type == 2 || type == 6 || type == 20; // Q8_0, Q4_0, Q5_0, IQ4_NL: d first
const int qk = small ? 32 : 256;
const size_t blk = k::native_mmvq_weight_bytes(type, qk, 1);
std::uniform_int_distribution<int> byte(0, 255);
std::uniform_real_distribution<float> scale(1e-3f, 1e-2f);
Expand All @@ -58,7 +62,7 @@ void* random_weights(int type, int n_in, int n_out) {
std::memcpy(p + blk - 2, &d, 2);
} else {
std::memcpy(p, &d, 2);
if (type != 8) std::memcpy(p + 2, &m, 2);
if (!small) std::memcpy(p + 2, &m, 2);
}
}
return up(h);
Expand Down Expand Up @@ -155,6 +159,41 @@ void check(const Case& c, bool many) {
ck(cudaStreamDestroy(st), "stream");
}

// native_quantize_q8_1_many() of three inputs, two of them one buffer, against native_quantize_q8_1() of each
void quantize_many(int n_in) {
cudaStream_t st;
ck(cudaStreamCreate(&st), "stream");
std::normal_distribution<float> nd(0.f, 1.f);
std::vector<float> h((size_t) 2 * n_in);
for (float& v : h) v = nd(rng);
float* x = up(h);
const float* xs[3] = {x, x + n_in, x};
const size_t qb = k::native_q8_1_bytes(n_in);
void *a[3], *b[3];
for (int i = 0; i < 3; ++i) {
ck(cudaMalloc(&a[i], qb), "malloc");
ck(cudaMalloc(&b[i], qb), "malloc");
ck(cudaMemset(a[i], 0x11, qb), "memset");
ck(cudaMemset(b[i], 0x22, qb), "memset");
k::native_quantize_q8_1(xs[i], a[i], n_in, 1, st);
}
k::native_quantize_q8_1_many(xs, b, 3, n_in, st);
ck(cudaStreamSynchronize(st), "sync");
size_t diff = 0;
for (int i = 0; i < 3; ++i) {
std::vector<uint8_t> u(qb), v(qb);
ck(cudaMemcpy(u.data(), a[i], qb, cudaMemcpyDeviceToHost), "download");
ck(cudaMemcpy(v.data(), b[i], qb, cudaMemcpyDeviceToHost), "download");
for (size_t j = 0; j < qb; ++j) diff += u[j] != v[j];
cudaFree(a[i]), cudaFree(b[i]);
}
cudaFree(x);
const std::string what = "quantize_many x3 at " + std::to_string(n_in) + " inputs";
std::printf("%-64s %zu of %zu bytes differ %s\n", what.c_str(), diff, 3 * qb, diff ? "FAIL" : "ok");
if (diff) ++failures;
ck(cudaStreamDestroy(st), "stream");
}

// the declines: a type it has no kernel for, a row too short for the one-row-a-block layout
void declines() {
k::NativeMmvqMany m{};
Expand Down Expand Up @@ -250,12 +289,27 @@ int main(int argc, char** argv) {
{"Q8_0 x6 (window layer: five + sink)", 2304, std::vector<int>(6, 8),
std::vector<int>(outs8.begin(), outs8.begin() + 6), std::vector<int>(in8.begin(), in8.begin() + 6)},
};
#if defined(__HIPCC__)
std::printf("native_mmvq_mixed(): not built on HIP, its %zu cases skipped\n", mixed.size());
for (const int t : {2, 6, 20}) { // Q4_0, Q5_0, IQ4_NL: the five's shapes and the eight's, as project() groups them
const std::string nm = t == 2 ? "Q4_0" : t == 6 ? "Q5_0" : "IQ4_NL";
many.push_back({nm + " x5", 2304, std::vector<int>(5, t), outs5, in5});
many.push_back({nm + " x8", 2304, std::vector<int>(8, t), outs8, in8});
}
many.push_back({"Q8_0 x5 (the five alone)", 2304, std::vector<int>(5, 8), outs5, in5});
#else
for (const Case& c : mixed) check(c, false);
#endif
for (const Case& c : many) check(c, true);
quantize_many(2304);
#if !defined(__HIPCC__)
declines();
#endif
if (do_bench) {
#if !defined(__HIPCC__)
bench(mixed[0]);
bench(mixed[1]);
#endif
bench(many[0]);
bench(many[1]);
}
Expand Down
Loading