STRATA_HC_REQ8: hyper-connection projections requantized to int8 + fp32 scale per 32 at load
The rebased #704 under the name asked for there (upstream's STRATA_HC_Q8 now reads the GGUF's Q8_0 bytes, a different thing). Opt-in. The four BF16 hc_* projections per layer (1,200 MiB) are skipped by the loader and uploaded as int8 codes with one fp32 scale per 32 values (675 MiB); the freed VRAM goes to the expert cache. - Q8 is a template parameter of the down/up kernels (single- and multi-token): the BF16 instantiations compile exactly as before, the launch sites pick the Q8 ones when w_down / w_up carry scales. The inject rows stay BF16. - The staged / v3 / upmix / PF_HCDOWN / gfx906 split paths read BF16 rows and are skipped for it; the prompt path expands the codes into a bf16 scratch for its GEMM. - The loader seeks with 64-bit offsets. - A layer split keeps BF16 with a warning instead of exiting.
This commit is contained in:
1 parent
653f011360
commit
2f44bf51dd
8 files changed
+267
-39
No files matched your search
@@ -1,5 +1,7 @@
|
||||
// src/core/weights.cpp - the dense-weight loader. See the header for the engine-vs-pack distinction.
|
||||
#include "strata/core/weights.hpp"
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
#include "strata/kernels/f16_bits.hpp"
|
||||
#include "strata/platform/memory.hpp"
|
||||
@@ -513,4 +515,88 @@ const WeightRef* WeightTable::find(const std::string& name) const {
|
||||
return it == table_.end() ? nullptr : &it->second;
|
||||
}
|
||||
|
||||
void WeightTable::hc_q8_names(int64_t n_layers, std::set<std::string>& out) {
|
||||
for (int64_t l = 0; l < n_layers; ++l)
|
||||
for (const char* h : {"hc_attn_", "hc_ffn_"})
|
||||
for (const char* m : {"down", "up"})
|
||||
out.insert("blk." + std::to_string(l) + "." + h + m + ".weight");
|
||||
}
|
||||
|
||||
bool WeightTable::load_hc_q8(const std::string& pack_dir, uint64_t& bytes, std::string& err) {
|
||||
bytes = 0;
|
||||
std::vector<WeightRef*> todo;
|
||||
for (auto& [name, w] : table_) {
|
||||
const bool hc = name.find(".hc_attn_down.") != std::string::npos || name.find(".hc_attn_up.") != std::string::npos ||
|
||||
name.find(".hc_ffn_down.") != std::string::npos || name.find(".hc_ffn_up.") != std::string::npos;
|
||||
if (!hc || w.resident) continue;
|
||||
// the pack holds BF16 either promoted to f32 (4 B/elem) or as it is in the GGUF (a native pack, 2 B/elem)
|
||||
if (w.kind != WeightKind::Bf16InF32 || w.ne0 % 32 != 0 ||
|
||||
(w.src_bytes != (uint64_t) w.elements * 4 && w.src_bytes != (uint64_t) w.elements * 2)) {
|
||||
err = "STRATA_HC_REQ8: " + name + " is not a BF16 tensor of whole 32-value blocks";
|
||||
return false;
|
||||
}
|
||||
todo.push_back(&w);
|
||||
}
|
||||
if (todo.empty()) return true;
|
||||
auto al = [](uint64_t b) { return (b + 255) & ~(uint64_t) 255; };
|
||||
uint64_t total = 0;
|
||||
for (const WeightRef* w : todo) total += al((uint64_t) w->elements) + al((uint64_t) w->elements / 32 * 4);
|
||||
uint8_t* dev = nullptr;
|
||||
if (cudaMalloc((void**) &dev, total) != cudaSuccess) { err = "STRATA_HC_REQ8: cudaMalloc failed"; return false; }
|
||||
std::vector<float> src;
|
||||
std::vector<int8_t> q;
|
||||
std::vector<float> sc;
|
||||
uint64_t at = 0;
|
||||
for (WeightRef* w : todo) {
|
||||
const char* fn = file_name(w->file_id);
|
||||
const std::string path = pack_dir + "/" + (fn ? fn : "?");
|
||||
std::FILE* f = std::fopen(path.c_str(), "rb");
|
||||
if (!f) { err = "STRATA_HC_REQ8: cannot open " + path; return false; }
|
||||
src.resize((size_t) w->elements);
|
||||
const bool raw16 = w->src_bytes == (uint64_t) w->elements * 2;
|
||||
#if defined(_WIN32)
|
||||
bool ok = _fseeki64(f, (long long) w->src_off, SEEK_SET) == 0;
|
||||
#else
|
||||
bool ok = fseeko(f, (off_t) w->src_off, SEEK_SET) == 0; // 64-bit: a pack's dense file can pass 2 GiB
|
||||
#endif
|
||||
if (ok && raw16) {
|
||||
std::vector<uint16_t> h(src.size());
|
||||
ok = std::fread(h.data(), 2, h.size(), f) == h.size();
|
||||
for (size_t i = 0; ok && i < h.size(); ++i) {
|
||||
const uint32_t b = (uint32_t) h[i] << 16; // bf16 -> f32 is exact
|
||||
std::memcpy(&src[i], &b, 4);
|
||||
}
|
||||
} else if (ok) {
|
||||
ok = std::fread(src.data(), 4, src.size(), f) == src.size();
|
||||
}
|
||||
std::fclose(f);
|
||||
if (!ok) { err = "STRATA_HC_REQ8: cannot read the source bytes"; return false; }
|
||||
const size_t nb = src.size() / 32;
|
||||
q.resize(src.size());
|
||||
sc.resize(nb);
|
||||
for (size_t b = 0; b < nb; ++b) { // Q8_0's rule: d = amax / 127, q = round-to-nearest-even(x / d)
|
||||
float amax = 0.f;
|
||||
for (int j = 0; j < 32; ++j) amax = std::max(amax, std::fabs(src[b * 32 + j]));
|
||||
const float d = amax / 127.f, id = d > 0.f ? 1.f / d : 0.f;
|
||||
sc[b] = d;
|
||||
for (int j = 0; j < 32; ++j) q[b * 32 + j] = (int8_t) std::nearbyint(src[b * 32 + j] * id);
|
||||
}
|
||||
uint8_t* codes = dev + at;
|
||||
at += al(q.size());
|
||||
float* scales = (float*) (dev + at);
|
||||
at += al(sc.size() * 4);
|
||||
if (cudaMemcpy(codes, q.data(), q.size(), cudaMemcpyHostToDevice) != cudaSuccess ||
|
||||
cudaMemcpy(scales, sc.data(), sc.size() * 4, cudaMemcpyHostToDevice) != cudaSuccess) {
|
||||
err = "STRATA_HC_REQ8: upload failed";
|
||||
return false;
|
||||
}
|
||||
w->data = codes;
|
||||
w->q8_scale = scales;
|
||||
w->resident = true;
|
||||
w->bytes = q.size() + sc.size() * 4;
|
||||
}
|
||||
bytes = total;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace strata::core
|
||||
Reference in new issue
Block a user