CS-T speed: the file tier fetched in parallel, PCIe planning without file reads, a partial pin of the RAM tier, bit-exact AVX2 multi-token kernels for Q4_K / Q5_1 / Q8_0 experts, decode-only tier stats
Where the 288 ms/token at a 40 GiB budget went (UD-Q4_K_XL, --stats): of the 945 ms CPU pool call per verify round (3.46 tokens), 871 ms were the dispatch thread assembling missed experts from the mapped GGUF one after another (~391 blobs, 1.3 GB per round, ~1.5 GB/s: page faults from the SSD); the kernels were 42 ms gate/up and 30 ms down. At 24 GiB, 771 ms/round more went into the PCIe planning, which asked blob() - a file read with the GGUF in place - of every miss just to learn it was not pinned. - ExpertSource::prefetch: the dispatch hands the source the layer's missed experts before its jobs loop; FileExpertSource with the GGUF in place assembles them on STRATA_FETCH_THREADS threads (default 8) so the SSD sees a queue instead of one fault at a time. A no-op for every other source. Staging buffers being filled are never reused, and a blob asked while its fill runs waits for it. - The PCIe planning asks pinned() before blob(): the same decisions and bytes for every source. - --resident-budget-gib: when the driver refuses to page-lock the whole budget, the locked arena's hottest prefix (profile order) is registered with cudaHostRegister, as much as it takes (from the whole down in 2 GiB steps), so the GPU can read those misses over PCIe and the prompt path can DMA them; pinned() answers for that prefix only. STRATA_PARTIAL_PIN=0 is the A/B arm. - src/kernels/cpu/kq_avx2.cpp: ggml-cpu's AVX2 vec_dot for Q4_K x Q8_K, Q5_1 x Q8_1 and Q8_0 x Q8_0 with the token loop inside the block loop - the weight side decoded once per verify window, each token's integer chain and float operations exactly ggml's, so the results are ggml's to the bit for any group size (no #152 rule). native_gu_rows / native_down_rows use them for those types (only UD-Q4_K_XL has them); STRATA_NO_KQ256 is the A/B arm. native_expert_parity checks them bit for bit against vec_dot for 1..8 tokens and times 4 tokens. - Stats: the decode's file tier alone (blobs, MB, MB/round, reading ms/round and GB/s per thread) and the prompt path's copies separately.
This commit is contained in:
1 parent
1e663cc70e
commit
003ec62140
9 files changed
+522
-28
No files matched your search
+3
-1
@@ -781,15 +781,17 @@ if(STRATA_NATIVE_EXPERTS)
|
||||
# part of strata_kernels_cpu (the pool calls these and they call its CPU probe: one library, so GNU ld's
|
||||
# one-pass static linking has no cycle to resolve)
|
||||
target_sources(strata_kernels_cpu PRIVATE src/kernels/cpu/native_expert.cpp src/kernels/cpu/iq_avx512.cpp
|
||||
src/kernels/cpu/iq_avx2.cpp)
|
||||
src/kernels/cpu/iq_avx2.cpp src/kernels/cpu/kq_avx2.cpp)
|
||||
target_include_directories(strata_kernels_cpu PRIVATE third_party/ggml)
|
||||
if(MSVC)
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
||||
set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
||||
else()
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx512.cpp PROPERTIES COMPILE_OPTIONS
|
||||
"-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx512vbmi;-mf16c;-mfma")
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")
|
||||
set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")
|
||||
endif()
|
||||
target_link_libraries(strata_kernels_cpu PUBLIC ggml-cpu ggml-base)
|
||||
target_compile_definitions(strata_kernels_cpu PUBLIC STRATA_NATIVE_EXPERTS=1)
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
@@ -132,6 +133,9 @@ public:
|
||||
/// The blob's bytes into `dst` (blob_bytes(layer) of them). Safe from several threads for a source whose
|
||||
/// `transient` can be true.
|
||||
virtual bool copy_blob(int64_t layer, int64_t expert, uint8_t* dst);
|
||||
/// The `n` experts of `layer` the CPU is about to ask `blob` for, all at once: a source that reads a file may
|
||||
/// fetch them in parallel. The bytes `blob` then returns are the same. Default: nothing.
|
||||
virtual void prefetch(int64_t layer, const int64_t* experts, int64_t n) { (void) layer; (void) experts; (void) n; }
|
||||
};
|
||||
|
||||
/// Plan v0.3 P6: what the GPU computes in a verify window's layer, written by the pool (mapped host memory) right
|
||||
@@ -381,7 +385,7 @@ public:
|
||||
|
||||
bool mapped() const { return base_ != nullptr; }
|
||||
int64_t blobs() const { return blobs_; }
|
||||
uint64_t pinned_bytes() const { return complement_pinned_ ? complement_bytes_ : 0; }
|
||||
uint64_t pinned_bytes() const { return complement_pinned_ ? complement_pin_limit_ : 0; }
|
||||
uint64_t resident_bytes() const { return complement_bytes_; }
|
||||
bool complement_pinned() const { return complement_pinned_; }
|
||||
bool complement_ready() const { return complement_ready_; }
|
||||
@@ -412,6 +416,12 @@ public:
|
||||
/// `file_reads` counts, plus the prompt path's copies of them).
|
||||
int64_t ram_reads() const { return ram_reads_.load(std::memory_order_relaxed); }
|
||||
uint64_t file_read_bytes() const { return file_read_bytes_.load(std::memory_order_relaxed); }
|
||||
/// Of those, the bytes `blob` read (decode windows, adaptive swaps; the rest are the prompt path's copies), and
|
||||
/// the time spent reading the files, summed over threads.
|
||||
uint64_t file_blob_bytes() const { return file_blob_bytes_.load(std::memory_order_relaxed); }
|
||||
double file_ms() const { return (double) file_us_.load(std::memory_order_relaxed) / 1000.0; }
|
||||
/// Threads `prefetch` reads the GGUF with (STRATA_FETCH_THREADS, default 8).
|
||||
void set_fetch_threads(int n) { fetch_threads_ = n < 1 ? 1 : n; }
|
||||
|
||||
const uint8_t* blob(int64_t layer, int64_t expert) override;
|
||||
bool pinned(int64_t layer, int64_t expert) const override;
|
||||
@@ -421,6 +431,8 @@ public:
|
||||
bool copy_blob(int64_t layer, int64_t expert, uint8_t* dst) override;
|
||||
/// CS-T: advances the assembled blobs' age (see staged_blob).
|
||||
void begin_layer(int64_t layer, const int32_t* ids, int64_t k) override;
|
||||
/// CS-T: the GGUF in place assembles the missed experts on `fetch_threads_` threads.
|
||||
void prefetch(int64_t layer, const int64_t* experts, int64_t n) override;
|
||||
|
||||
/// Blobs touched, for the driver to report. With `h = 0` this is `48 * k` per token and the number is only
|
||||
/// interesting once Phase 3 makes it not so.
|
||||
@@ -432,6 +444,8 @@ private:
|
||||
bool copy_from_files(int64_t layer, int64_t expert, uint8_t* dst) const;
|
||||
bool open_gguf(std::string& err);
|
||||
const uint8_t* staged_blob(int64_t layer, int64_t expert);
|
||||
bool claim_stage(int64_t key, size_t& v, bool& fill);
|
||||
bool fill_stage(size_t v, int64_t layer, int64_t expert, uint8_t* dst);
|
||||
static constexpr uint64_t kNoComplement = detail::kNoCacheComplement;
|
||||
// ---- CS-T: the GGUF shards in place
|
||||
std::string gguf_;
|
||||
@@ -456,6 +470,10 @@ private:
|
||||
std::vector<std::unique_ptr<uint8_t[]>> stage_buf_;
|
||||
std::vector<int64_t> stage_key_;
|
||||
std::vector<uint64_t> stage_epoch_, stage_used_;
|
||||
std::vector<char> stage_busy_; ///< being filled (outside stage_mu_): never a victim
|
||||
std::condition_variable stage_cv_;
|
||||
int fetch_threads_ = 8;
|
||||
std::atomic<uint64_t> file_blob_bytes_{0}, file_us_{0};
|
||||
std::unordered_map<int64_t, size_t> stage_of_;
|
||||
uint64_t stage_blob_ = 0;
|
||||
uint64_t stage_seq_ = 0;
|
||||
@@ -476,6 +494,8 @@ private:
|
||||
uint64_t complement_bytes_ = 0;
|
||||
std::vector<uint64_t> complement_offsets_;
|
||||
bool complement_pinned_ = false;
|
||||
bool complement_partial_ = false; ///< CS-T: only the first complement_pin_limit_ bytes are registered
|
||||
uint64_t complement_pin_limit_ = 0;
|
||||
bool complement_ready_ = false;
|
||||
uint64_t complement_locked_ = 0; ///< bytes held in the working set (pin refused)
|
||||
int64_t complement_lent_slots_ = 0;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// include/strata/kernels/cpu/kq_avx2.hpp - AVX-2 multi-token dot products for Unsloth UD-Q4_K_XL's expert formats
|
||||
// (Q4_K gate/up against Q8_K activations; Q5_1 down against Q8_1, Q8_0 down against Q8_0), bit-exact against
|
||||
// ggml-cpu's AVX2 vec_dot for every token. See src/kernels/cpu/kq_avx2.cpp.
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace strata::kernels::cpu {
|
||||
|
||||
/// Q4_K (12), Q5_1 (7), Q8_0 (8).
|
||||
bool kq256_supported(int ggml_type) noexcept;
|
||||
/// ff[t][r] = silu(gate_r . a[t]) * (up_r . a[t]), rows [r0, r1); gate rows at blob, up rows at blob + up_off.
|
||||
void kq256_gu_rows(int ggml_type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act,
|
||||
int nt, float* const* ff, int r0, int r1);
|
||||
/// out[t][r] = w_r . a[t], rows [r0, r1).
|
||||
void kq256_rows(int ggml_type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt,
|
||||
float* const* out, int r0, int r1);
|
||||
|
||||
} // namespace strata::kernels::cpu
|
||||
+154
-20
@@ -448,8 +448,9 @@ bool FileExpertSource::open(const std::string& pack_dir, int64_t n_layers, int64
|
||||
|
||||
void FileExpertSource::close() {
|
||||
if (complement_arena_ != nullptr) {
|
||||
if (complement_pinned_) (void) cudaFreeHost(complement_arena_);
|
||||
if (complement_pinned_ && !complement_partial_) (void) cudaFreeHost(complement_arena_);
|
||||
else {
|
||||
if (complement_partial_) (void) cudaHostUnregister(complement_arena_);
|
||||
if (complement_locked_ > 0) strata::platform::unlock_resident(complement_arena_, complement_locked_);
|
||||
std::free(complement_arena_);
|
||||
}
|
||||
@@ -472,6 +473,8 @@ void FileExpertSource::close() {
|
||||
complement_bytes_ = 0;
|
||||
complement_offsets_.clear();
|
||||
complement_pinned_ = false;
|
||||
complement_partial_ = false;
|
||||
complement_pin_limit_ = 0;
|
||||
complement_ready_ = false;
|
||||
complement_locked_ = 0;
|
||||
complement_lent_slots_ = 0;
|
||||
@@ -497,6 +500,7 @@ void FileExpertSource::close() {
|
||||
stage_key_.clear();
|
||||
stage_epoch_.clear();
|
||||
stage_used_.clear();
|
||||
stage_busy_.clear();
|
||||
stage_of_.clear();
|
||||
stage_blob_ = 0;
|
||||
stage_seq_ = 0;
|
||||
@@ -506,6 +510,8 @@ void FileExpertSource::close() {
|
||||
}
|
||||
ram_reads_.store(0);
|
||||
file_read_bytes_.store(0);
|
||||
file_blob_bytes_.store(0);
|
||||
file_us_.store(0);
|
||||
#if defined(_WIN32)
|
||||
if (base_ != nullptr) UnmapViewOfFile((LPCVOID) base_);
|
||||
if (mapping_ != nullptr) CloseHandle((HANDLE) mapping_);
|
||||
@@ -636,32 +642,37 @@ bool FileExpertSource::copy_from_files(int64_t layer, int64_t expert, uint8_t* d
|
||||
|
||||
// A blob assembled from the three role slices. The buffer of a (layer, expert) is reused for another only once
|
||||
// its blob has not been asked for during `kStageAge` layers (begin_layer) or 256 assemblies, whichever comes
|
||||
// first: the pool computes a layer's misses before it starts the next, and a fill (the GPU cache at startup, an
|
||||
// adaptive swap, a helper GPU) copies the blob right away.
|
||||
const uint8_t* FileExpertSource::staged_blob(int64_t layer, int64_t expert) {
|
||||
// first, and never while it is being filled: the pool computes a layer's misses before it starts the next, and a
|
||||
// fill (the GPU cache at startup, an adaptive swap, a helper GPU) copies the blob right away.
|
||||
//
|
||||
// `claim_stage` finds or reserves the buffer of `key` (stage_mu_ held): true when the blob is already there (or
|
||||
// being filled by another thread - the caller then waits), false when the caller must fill buffer `v`.
|
||||
bool FileExpertSource::claim_stage(int64_t key, size_t& v, bool& fill) {
|
||||
constexpr uint64_t kStageSeq = 256;
|
||||
const int64_t key = layer * n_expert_ + expert;
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
const uint64_t seq = ++stage_seq_;
|
||||
auto it = stage_of_.find(key);
|
||||
fill = false;
|
||||
if (it != stage_of_.end()) {
|
||||
stage_epoch_[it->second] = epoch_;
|
||||
stage_used_[it->second] = seq;
|
||||
return stage_buf_[it->second].get();
|
||||
v = it->second;
|
||||
stage_epoch_[v] = epoch_;
|
||||
stage_used_[v] = seq;
|
||||
return true;
|
||||
}
|
||||
size_t v = stage_buf_.size();
|
||||
v = stage_buf_.size();
|
||||
uint64_t oldest = std::numeric_limits<uint64_t>::max();
|
||||
for (size_t i = 0; i < stage_buf_.size(); ++i)
|
||||
if ((stage_epoch_[i] + kStageAge <= epoch_ || stage_used_[i] + kStageSeq <= seq) && stage_used_[i] < oldest) {
|
||||
if (!stage_busy_[i] && (stage_epoch_[i] + kStageAge <= epoch_ || stage_used_[i] + kStageSeq <= seq) &&
|
||||
stage_used_[i] < oldest) {
|
||||
oldest = stage_used_[i];
|
||||
v = i;
|
||||
}
|
||||
if (v == stage_buf_.size()) {
|
||||
stage_buf_.emplace_back(new (std::nothrow) uint8_t[(size_t) stage_blob_]);
|
||||
if (!stage_buf_.back()) { stage_buf_.pop_back(); return nullptr; }
|
||||
if (!stage_buf_.back()) { stage_buf_.pop_back(); return false; }
|
||||
stage_key_.push_back(-1);
|
||||
stage_epoch_.push_back(0);
|
||||
stage_used_.push_back(0);
|
||||
stage_busy_.push_back(0);
|
||||
if (stage_buf_.size() == 512 && !stage_grew_) {
|
||||
stage_grew_ = true;
|
||||
std::fprintf(stderr, "FileExpertSource: %zu blobs assembled from the GGUF are in use at once (%.2f GiB)\n",
|
||||
@@ -670,14 +681,87 @@ const uint8_t* FileExpertSource::staged_blob(int64_t layer, int64_t expert) {
|
||||
} else {
|
||||
stage_of_.erase(stage_key_[v]);
|
||||
}
|
||||
uint8_t* dst = stage_buf_[v].get();
|
||||
if (!copy_from_files(layer, expert, dst)) { stage_key_[v] = -1; return nullptr; }
|
||||
file_read_bytes_.fetch_add(layer_blob_bytes_[(size_t) layer], std::memory_order_relaxed);
|
||||
stage_key_[v] = key;
|
||||
stage_epoch_[v] = epoch_;
|
||||
stage_used_[v] = seq;
|
||||
stage_busy_[v] = 1;
|
||||
stage_of_[key] = v;
|
||||
return dst;
|
||||
fill = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fills buffer `v` (reserved by claim_stage) outside the lock, then publishes it.
|
||||
bool FileExpertSource::fill_stage(size_t v, int64_t layer, int64_t expert, uint8_t* dst) {
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
const bool ok = copy_from_files(layer, expert, dst);
|
||||
const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count();
|
||||
file_us_.fetch_add((uint64_t) us, std::memory_order_relaxed);
|
||||
if (ok) {
|
||||
file_read_bytes_.fetch_add(layer_blob_bytes_[(size_t) layer], std::memory_order_relaxed);
|
||||
file_blob_bytes_.fetch_add(layer_blob_bytes_[(size_t) layer], std::memory_order_relaxed);
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
stage_busy_[v] = 0;
|
||||
if (!ok) {
|
||||
stage_of_.erase(stage_key_[v]);
|
||||
stage_key_[v] = -1;
|
||||
}
|
||||
}
|
||||
stage_cv_.notify_all();
|
||||
return ok;
|
||||
}
|
||||
|
||||
const uint8_t* FileExpertSource::staged_blob(int64_t layer, int64_t expert) {
|
||||
const int64_t key = layer * n_expert_ + expert;
|
||||
size_t v = 0;
|
||||
bool fill = false;
|
||||
uint8_t* dst = nullptr;
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(stage_mu_);
|
||||
const bool have = claim_stage(key, v, fill);
|
||||
if (!have && !fill) return nullptr;
|
||||
dst = stage_buf_[v].get();
|
||||
if (have) {
|
||||
// another thread (a prefetch, the adaptive tier) is filling it: wait for that
|
||||
stage_cv_.wait(lk, [&] { return !stage_busy_[v] || stage_key_[v] != key; });
|
||||
if (stage_key_[v] != key) return nullptr; // its fill failed
|
||||
return dst;
|
||||
}
|
||||
}
|
||||
return fill_stage(v, layer, expert, dst) ? dst : nullptr;
|
||||
}
|
||||
|
||||
void FileExpertSource::prefetch(int64_t layer, const int64_t* experts, int64_t n) {
|
||||
if (role_ptr_.empty() || n <= 0 || layer < 0 || layer >= n_layers_) return;
|
||||
struct Fill { size_t v; int64_t e; uint8_t* dst; };
|
||||
std::vector<Fill> todo;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(stage_mu_);
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const int64_t e = experts[i];
|
||||
if (e < 0 || e >= n_expert_) continue;
|
||||
const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) e;
|
||||
if (complement_ready_ && index < complement_offsets_.size() && complement_offsets_[index] != kNoComplement)
|
||||
continue; // in the RAM copy
|
||||
if (!override_.empty() && override_[index] != nullptr) continue;
|
||||
size_t v = 0;
|
||||
bool fill = false;
|
||||
if (!claim_stage(layer * n_expert_ + e, v, fill) && fill) todo.push_back({v, e, stage_buf_[v].get()});
|
||||
}
|
||||
}
|
||||
if (todo.empty()) return;
|
||||
// the page faults of a mapped read are one outstanding request each: several threads keep the SSD's queue full
|
||||
std::atomic<size_t> next{0};
|
||||
auto work = [&] {
|
||||
for (size_t i; (i = next.fetch_add(1)) < todo.size();)
|
||||
(void) fill_stage(todo[i].v, layer, todo[i].e, todo[i].dst);
|
||||
};
|
||||
const size_t nt = std::min<size_t>(todo.size(), (size_t) fetch_threads_);
|
||||
std::vector<std::thread> th;
|
||||
for (size_t t = 1; t < nt; ++t) th.emplace_back(work);
|
||||
work();
|
||||
for (auto& t : th) t.join();
|
||||
}
|
||||
|
||||
void FileExpertSource::begin_layer(int64_t layer, const int32_t* ids, int64_t k) {
|
||||
@@ -713,7 +797,10 @@ bool FileExpertSource::copy_blob(int64_t layer, int64_t expert, uint8_t* dst) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
if (!copy_from_files(layer, expert, dst)) return false;
|
||||
file_us_.fetch_add((uint64_t) std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count(),
|
||||
std::memory_order_relaxed);
|
||||
file_read_bytes_.fetch_add(bytes, std::memory_order_relaxed);
|
||||
return true;
|
||||
}
|
||||
@@ -852,11 +939,13 @@ bool FileExpertSource::pin_cache_complement(
|
||||
const uint8_t* device = nullptr;
|
||||
bool pinned_ok = false;
|
||||
uint64_t locked = 0;
|
||||
uint64_t partial_pin = 0; ///< CS-T: a registered prefix of a locked arena
|
||||
std::string note;
|
||||
auto release = [&]() {
|
||||
if (arena == nullptr) return;
|
||||
if (pinned_ok) (void) cudaFreeHost(arena);
|
||||
else {
|
||||
if (partial_pin > 0) (void) cudaHostUnregister(arena);
|
||||
if (locked > 0) strata::platform::unlock_resident(arena, locked);
|
||||
std::free(arena);
|
||||
}
|
||||
@@ -900,6 +989,37 @@ bool FileExpertSource::pin_cache_complement(
|
||||
const strata::platform::LockResult lr = strata::platform::lock_resident(arena, bytes);
|
||||
locked = lr.locked_bytes;
|
||||
note += (note.empty() ? "" : "; ") + lr.note;
|
||||
// CS-T, a RAM budget: its bytes are in profile order, hottest first, so the driver is asked to
|
||||
// register the largest prefix it takes (from the whole, down in 2 GiB steps). Those experts can be
|
||||
// read by the GPU over PCIe (--pcie-frac) and copied by DMA; the rest stay locked-only.
|
||||
static const bool partial_on = [] {
|
||||
const char* v = std::getenv("STRATA_PARTIAL_PIN"); // 0: the A/B arm without it
|
||||
return v == nullptr || std::atoi(v) != 0;
|
||||
}();
|
||||
if (budget_bytes > 0 && partial_on) {
|
||||
const uint64_t step = 2ull << 30;
|
||||
for (uint64_t want = bytes; want >= step; want = want > step ? want - step : 0) {
|
||||
const uint64_t w = want - want % (64ull << 10);
|
||||
if (cudaHostRegister(arena, (size_t) w, cudaHostRegisterMapped | cudaHostRegisterPortable) ==
|
||||
cudaSuccess) {
|
||||
void* alias = nullptr;
|
||||
if (cudaHostGetDevicePointer(&alias, arena, 0) == cudaSuccess && alias != nullptr) {
|
||||
device = (const uint8_t*) alias;
|
||||
partial_pin = w;
|
||||
} else {
|
||||
(void) cudaGetLastError();
|
||||
(void) cudaHostUnregister(arena);
|
||||
}
|
||||
break;
|
||||
}
|
||||
(void) cudaGetLastError();
|
||||
if (want <= step) break;
|
||||
}
|
||||
char msg[160];
|
||||
std::snprintf(msg, sizeof msg, "%.2f GiB of it registered for the GPU (the hottest)",
|
||||
(double) partial_pin / 1073741824.0);
|
||||
note += std::string("; ") + msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
host = (const uint8_t*) arena;
|
||||
@@ -992,7 +1112,9 @@ bool FileExpertSource::pin_cache_complement(
|
||||
complement_device_ = device;
|
||||
complement_bytes_ = bytes;
|
||||
complement_offsets_ = std::move(offsets);
|
||||
complement_pinned_ = pinned_ok && bytes > 0;
|
||||
complement_pinned_ = (pinned_ok || partial_pin > 0) && bytes > 0;
|
||||
complement_pin_limit_ = pinned_ok ? bytes : partial_pin;
|
||||
complement_partial_ = !pinned_ok && partial_pin > 0;
|
||||
complement_locked_ = locked;
|
||||
complement_lent_slots_ = lend ? n_slots - keep_from : 0;
|
||||
complement_ready_ = true;
|
||||
@@ -1116,7 +1238,10 @@ bool FileExpertSource::pinned(int64_t layer, int64_t expert) const {
|
||||
if (!complement_ready_ || !complement_pinned_ || complement_host_ == nullptr || layer < 0 || expert < 0 ||
|
||||
layer >= n_layers_ || expert >= n_expert_) return false;
|
||||
const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert;
|
||||
return index < complement_offsets_.size() && complement_offsets_[index] != kNoComplement;
|
||||
if (index >= complement_offsets_.size() || complement_offsets_[index] == kNoComplement) return false;
|
||||
// a partial pin (CS-T): only the registered prefix
|
||||
return !complement_partial_ ||
|
||||
complement_offsets_[index] + layer_blob_bytes_[(size_t) layer] <= complement_pin_limit_;
|
||||
}
|
||||
|
||||
const uint8_t* FileExpertSource::device_alias(int64_t layer, int64_t expert) const {
|
||||
@@ -1332,8 +1457,8 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
|
||||
: (size_t) slot * (size_t) d.cache_blob));
|
||||
} else {
|
||||
if (miss_rank >= nmiss - m && fetches < P.staging_cap && fetches < 64) {
|
||||
const uint8_t* src = d.src->blob(d.layers, e);
|
||||
if (src != nullptr && d.src->pinned(d.layers, e)) {
|
||||
const uint8_t* src = d.src->pinned(d.layers, e) ? d.src->blob(d.layers, e) : nullptr;
|
||||
if (src != nullptr) {
|
||||
kd = 1;
|
||||
dma_src[fetches] = src;
|
||||
pcie_i0[fetches] = i0;
|
||||
@@ -1405,6 +1530,15 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
|
||||
else
|
||||
for (int64_t t = 0; t < n_tok; ++t) act_quant_q8_1(x_f + (size_t) t * H, H, d.act_multi[(size_t) t]);
|
||||
const auto c2 = std::chrono::steady_clock::now();
|
||||
{ // CS-T: the experts the CPU computes, fetched together (the GGUF in place reads them on several threads)
|
||||
static thread_local std::vector<int64_t> miss;
|
||||
miss.clear();
|
||||
for (int64_t i = 0; i < n_tok * k; ++i)
|
||||
if (kind[i] < 0 && ids[i] >= 0 && ids[i] < d.n_expert &&
|
||||
std::find(miss.begin(), miss.end(), (int64_t) ids[i]) == miss.end())
|
||||
miss.push_back(ids[i]);
|
||||
d.src->prefetch(d.layers, miss.data(), (int64_t) miss.size());
|
||||
}
|
||||
int njobs = 0;
|
||||
for (int64_t t = 0; t < n_tok; ++t)
|
||||
for (int64_t j = 0; j < k; ++j) {
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
// src/kernels/cpu/kq_avx2.cpp - Unsloth UD-Q4_K_XL's expert rows (Q4_K gate/up, Q5_1 and Q8_0 down) for several
|
||||
// tokens at once, BIT-EXACT against ggml-cpu's own dot products.
|
||||
//
|
||||
// ggml-cpu is built for AVX2 here (STRATA_PORTABLE: GGML_AVX2=ON, GGML_AVX512=OFF), so its vec_dot for these formats
|
||||
// is the __AVX2__ branch of ggml-cpu/arch/x86/quants.c: ggml_vec_dot_q4_K_q8_K, ggml_vec_dot_q5_1_q8_1,
|
||||
// ggml_vec_dot_q8_0_q8_0. The functions below are those branches with the loop over tokens moved inside the loop
|
||||
// over blocks: everything that depends only on the weights (the 4-bit unpacking, the 6-bit scales and mins, the
|
||||
// fifth bits, the absolute values of the Q8_0 weights, the fp16 scales) is done once per block, and every token then
|
||||
// runs exactly ggml's integer chain and ggml's float operations in ggml's order - so each token's result is ggml's
|
||||
// to the bit (native_expert_parity checks it), whatever the number of tokens. Unlike the i-quant kernels (#152)
|
||||
// the group size therefore never changes an answer.
|
||||
//
|
||||
// The gain is the weight-side work and the weight bytes, read once per verify window instead of once per token.
|
||||
#include "strata/kernels/cpu/kq_avx2.hpp"
|
||||
|
||||
#define GGML_COMMON_DECL_CPP
|
||||
#define GGML_COMMON_IMPL_CPP
|
||||
#include "ggml-common.h"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
namespace strata::kernels::cpu {
|
||||
namespace {
|
||||
|
||||
constexpr int MAXT = 8; // the verify window's tokens per group (kVerifyMaxT)
|
||||
|
||||
inline float h2f(ggml_half h) {
|
||||
uint16_t u;
|
||||
std::memcpy(&u, &h, 2);
|
||||
return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128((int) u)));
|
||||
}
|
||||
// the fp16 pair {d, dmin} / {d, m} / {d, s} a block starts with (a union in ggml-common.h's C++ declarations)
|
||||
inline float half_at(const void* block, int i) {
|
||||
ggml_half h;
|
||||
std::memcpy(&h, (const uint8_t*) block + 2 * i, 2);
|
||||
return h2f(h);
|
||||
}
|
||||
|
||||
// ---- ggml's helpers (ggml-cpu/arch/x86/quants.c, the AVX2 / non-VNNI forms), verbatim
|
||||
inline float hsum_float_8(const __m256 x) {
|
||||
__m128 res = _mm256_extractf128_ps(x, 1);
|
||||
res = _mm_add_ps(res, _mm256_castps256_ps128(x));
|
||||
res = _mm_add_ps(res, _mm_movehl_ps(res, res));
|
||||
res = _mm_add_ss(res, _mm_movehdup_ps(res));
|
||||
return _mm_cvtss_f32(res);
|
||||
}
|
||||
inline __m256i bytes_from_bits_32(const uint8_t* x) {
|
||||
uint32_t x32;
|
||||
std::memcpy(&x32, x, sizeof(uint32_t));
|
||||
const __m256i shuf_mask = _mm256_set_epi64x(0x0303030303030303, 0x0202020202020202, 0x0101010101010101,
|
||||
0x0000000000000000);
|
||||
__m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32((int) x32), shuf_mask);
|
||||
const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
|
||||
bytes = _mm256_or_si256(bytes, bit_mask);
|
||||
return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
|
||||
}
|
||||
inline __m256i bytes_from_nibbles_32(const uint8_t* rsi) {
|
||||
const __m128i tmp = _mm_loadu_si128((const __m128i*) rsi);
|
||||
const __m256i bytes = _mm256_insertf128_si256(_mm256_castsi128_si256(tmp), _mm_srli_epi16(tmp, 4), 1);
|
||||
const __m256i lowMask = _mm256_set1_epi8(0xF);
|
||||
return _mm256_and_si256(lowMask, bytes);
|
||||
}
|
||||
inline __m256 sum_i16_pairs_float(const __m256i x) {
|
||||
const __m256i ones = _mm256_set1_epi16(1);
|
||||
return _mm256_cvtepi32_ps(_mm256_madd_epi16(ones, x));
|
||||
}
|
||||
inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
|
||||
return sum_i16_pairs_float(_mm256_maddubs_epi16(ax, sy));
|
||||
}
|
||||
alignas(32) const uint8_t k_shuffle[256] = {
|
||||
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
|
||||
2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
|
||||
4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5,
|
||||
6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
|
||||
8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9,
|
||||
10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
|
||||
12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
|
||||
14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15};
|
||||
inline __m256i get_scale_shuffle_k4(int i) { return _mm256_loadu_si256((const __m256i*) k_shuffle + i); }
|
||||
|
||||
// ---- Q4_K x Q8_K (ggml_vec_dot_q4_K_q8_K, __AVX2__)
|
||||
void q4k_dot(const block_q4_K* x, int nb, const void* const* act, int nt, float* res) {
|
||||
static const uint32_t kmask1 = 0x3f3f3f3f, kmask2 = 0x0f0f0f0f, kmask3 = 0x03030303;
|
||||
const __m256i m4 = _mm256_set1_epi8(0xF);
|
||||
__m256 acc[MAXT];
|
||||
__m128 acc_m[MAXT];
|
||||
for (int t = 0; t < nt; ++t) { acc[t] = _mm256_setzero_ps(); acc_m[t] = _mm_setzero_ps(); }
|
||||
for (int i = 0; i < nb; ++i) {
|
||||
const float xd = half_at(&x[i], 0), xdmin = half_at(&x[i], 1);
|
||||
uint32_t utmp[4];
|
||||
std::memcpy(utmp, x[i].scales, 12);
|
||||
utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
|
||||
const uint32_t uaux = utmp[1] & kmask1;
|
||||
utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
|
||||
utmp[2] = uaux;
|
||||
utmp[0] &= kmask1;
|
||||
const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32((int) utmp[3], (int) utmp[2],
|
||||
(int) utmp[1], (int) utmp[0]));
|
||||
const __m128i mins = _mm256_extracti128_si256(mins_and_scales, 1);
|
||||
const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
|
||||
const __m256i scales = _mm256_insertf128_si256(_mm256_castsi128_si256(sc128), sc128, 1);
|
||||
__m256i scale_l[4], scale_h[4], q4l[4], q4h[4];
|
||||
const uint8_t* q4 = x[i].qs;
|
||||
for (int j = 0; j < QK_K / 64; ++j) {
|
||||
scale_l[j] = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2 * j + 0));
|
||||
scale_h[j] = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2 * j + 1));
|
||||
const __m256i q4bits = _mm256_loadu_si256((const __m256i*) q4);
|
||||
q4 += 32;
|
||||
q4l[j] = _mm256_and_si256(q4bits, m4);
|
||||
q4h[j] = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
|
||||
}
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
const block_q8_K* y = (const block_q8_K*) act[t] + i;
|
||||
const float d = y->d * xd;
|
||||
const float dmin = -y->d * xdmin;
|
||||
const int8_t* q8 = y->qs;
|
||||
const __m256i q8sums = _mm256_loadu_si256((const __m256i*) y->bsums);
|
||||
const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
|
||||
const __m128i prod = _mm_madd_epi16(mins, q8s);
|
||||
acc_m[t] = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m[t]);
|
||||
__m256i sumi = _mm256_setzero_si256();
|
||||
for (int j = 0; j < QK_K / 64; ++j) {
|
||||
const __m256i q8l = _mm256_loadu_si256((const __m256i*) q8);
|
||||
q8 += 32;
|
||||
__m256i p16l = _mm256_maddubs_epi16(q4l[j], q8l);
|
||||
p16l = _mm256_madd_epi16(scale_l[j], p16l);
|
||||
const __m256i q8h = _mm256_loadu_si256((const __m256i*) q8);
|
||||
q8 += 32;
|
||||
__m256i p16h = _mm256_maddubs_epi16(q4h[j], q8h);
|
||||
p16h = _mm256_madd_epi16(scale_h[j], p16h);
|
||||
sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16l, p16h));
|
||||
}
|
||||
acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), acc[t]);
|
||||
}
|
||||
}
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
__m128 m = acc_m[t];
|
||||
m = _mm_add_ps(m, _mm_movehl_ps(m, m));
|
||||
m = _mm_add_ss(m, _mm_movehdup_ps(m));
|
||||
res[t] = hsum_float_8(acc[t]) + _mm_cvtss_f32(m);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Q5_1 x Q8_1 (ggml_vec_dot_q5_1_q8_1, __AVX2__)
|
||||
void q5_1_dot(const block_q5_1* x, int nb, const void* const* act, int nt, float* res) {
|
||||
__m256 acc[MAXT];
|
||||
float summs[MAXT];
|
||||
for (int t = 0; t < nt; ++t) { acc[t] = _mm256_setzero_ps(); summs[t] = 0.0f; }
|
||||
for (int ib = 0; ib < nb; ++ib) {
|
||||
const __m256 dx = _mm256_set1_ps(half_at(&x[ib], 0));
|
||||
const float xm = half_at(&x[ib], 1);
|
||||
__m256i qx = bytes_from_nibbles_32(x[ib].qs);
|
||||
__m256i bxhi = bytes_from_bits_32(x[ib].qh);
|
||||
bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
|
||||
qx = _mm256_or_si256(qx, bxhi);
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
const block_q8_1* y = (const block_q8_1*) act[t] + ib;
|
||||
summs[t] += xm * half_at(y, 1);
|
||||
const __m256 dy = _mm256_set1_ps(half_at(y, 0));
|
||||
const __m256i qy = _mm256_loadu_si256((const __m256i*) y->qs);
|
||||
const __m256 q = mul_sum_us8_pairs_float(qx, qy);
|
||||
acc[t] = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc[t]);
|
||||
}
|
||||
}
|
||||
for (int t = 0; t < nt; ++t) res[t] = hsum_float_8(acc[t]) + summs[t];
|
||||
}
|
||||
|
||||
// ---- Q8_0 x Q8_0 (ggml_vec_dot_q8_0_q8_0, __AVX2__; mul_sum_i8_pairs_float without AVX-VNNI-INT8)
|
||||
void q8_0_dot(const block_q8_0* x, int nb, const void* const* act, int nt, float* res) {
|
||||
__m256 acc[MAXT];
|
||||
for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps();
|
||||
for (int ib = 0; ib < nb; ++ib) {
|
||||
const float xd = h2f(x[ib].d);
|
||||
const __m256i qx = _mm256_loadu_si256((const __m256i*) x[ib].qs);
|
||||
const __m256i ax = _mm256_sign_epi8(qx, qx);
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
const block_q8_0* y = (const block_q8_0*) act[t] + ib;
|
||||
const __m256 d = _mm256_set1_ps(xd * h2f(y->d));
|
||||
const __m256i qy = _mm256_loadu_si256((const __m256i*) y->qs);
|
||||
const __m256i sy = _mm256_sign_epi8(qy, qx);
|
||||
const __m256 q = mul_sum_us8_pairs_float(ax, sy);
|
||||
acc[t] = _mm256_fmadd_ps(d, q, acc[t]);
|
||||
}
|
||||
}
|
||||
for (int t = 0; t < nt; ++t) res[t] = hsum_float_8(acc[t]);
|
||||
}
|
||||
|
||||
void dot_rows(int type, const uint8_t* row, int n, const void* const* act, int nt, float* res) {
|
||||
switch (type) {
|
||||
case 12: q4k_dot((const block_q4_K*) row, n / QK_K, act, nt, res); break;
|
||||
case 7: q5_1_dot((const block_q5_1*) row, n / QK5_1, act, nt, res); break;
|
||||
case 8: q8_0_dot((const block_q8_0*) row, n / QK8_0, act, nt, res); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool kq256_supported(int type) noexcept { return type == 12 || type == 7 || type == 8; }
|
||||
|
||||
void kq256_gu_rows(int type, const uint8_t* blob, size_t gu_row, size_t up_off, int n, const void* const* act, int nt,
|
||||
float* const* ff, int r0, int r1) {
|
||||
for (int t0 = 0; t0 < nt; t0 += MAXT) {
|
||||
const int m = nt - t0 < MAXT ? nt - t0 : MAXT;
|
||||
float g[MAXT], u[MAXT];
|
||||
for (int r = r0; r < r1; ++r) {
|
||||
dot_rows(type, blob + (size_t) r * gu_row, n, act + t0, m, g);
|
||||
dot_rows(type, blob + up_off + (size_t) r * gu_row, n, act + t0, m, u);
|
||||
// native_gu_rows' own SwiGLU expression: the same bits as the per-token ggml path
|
||||
for (int t = 0; t < m; ++t) ff[t0 + t][r] = (g[t] / (1.f + std::exp(-g[t]))) * u[t];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void kq256_rows(int type, const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt, float* const* out,
|
||||
int r0, int r1) {
|
||||
for (int t0 = 0; t0 < nt; t0 += MAXT) {
|
||||
const int m = nt - t0 < MAXT ? nt - t0 : MAXT;
|
||||
float s[MAXT];
|
||||
for (int r = r0; r < r1; ++r) {
|
||||
dot_rows(type, w + (size_t) r * row_bytes, n, act + t0, m, s);
|
||||
for (int t = 0; t < m; ++t) out[t0 + t][r] = s[t];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace strata::kernels::cpu
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "strata/kernels/cpu/expert.hpp"
|
||||
#include "strata/kernels/cpu/iq_avx512.hpp"
|
||||
#include "strata/kernels/cpu/iq_avx2.hpp"
|
||||
#include "strata/kernels/cpu/kq_avx2.hpp"
|
||||
#include "strata/kernels/cpu/expert_layout.hpp"
|
||||
|
||||
#include "ggml.h"
|
||||
@@ -89,6 +90,13 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const*
|
||||
// can depend on how many drafts a verify window held. STRATA_IQ_MT_MIN=1 (opt-in, 0.1.30) uses the multi-token
|
||||
// kernels for every group: output independent of the drafting, at a measured -1..-3% decode on IQ3_S (AVX-512).
|
||||
static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
|
||||
// Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot, so it
|
||||
// runs for every group size (no #152 rule). STRATA_NO_KQ256 falls back to ggml's vec_dot.
|
||||
static const bool kq = std::getenv("STRATA_NO_KQ256") == nullptr;
|
||||
if (kq && f.gu_type == 12) {
|
||||
kq256_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
|
||||
return;
|
||||
}
|
||||
if (nt >= mt_min && iq512_supported(f.gu_type)) {
|
||||
if (avx512) {
|
||||
iq512_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
|
||||
@@ -119,6 +127,11 @@ void native_down_rows(const NativeFmt& f, const uint8_t* blob, const void* const
|
||||
// block instead of once per token; ggml-cpu's dot is single-token. STRATA_NO_IQ4NL falls back to it.
|
||||
static const bool iq4nl_mt = std::getenv("STRATA_NO_IQ4NL") == nullptr;
|
||||
static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
|
||||
static const bool kq = std::getenv("STRATA_NO_KQ256") == nullptr;
|
||||
if (kq && (f.d_type == 7 || f.d_type == 8)) { // Q5_1 / Q8_0 down: bit-exact, every group size
|
||||
kq256_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
|
||||
return;
|
||||
}
|
||||
if (nt >= mt_min && f.d_type == 20 && iq4nl_mt) { // #152: the same rule as the gate/up rows
|
||||
iq4nl256_down_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
|
||||
return;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "strata/kernels/cpu/expert.hpp"
|
||||
#include "strata/kernels/cpu/iq_avx512.hpp"
|
||||
#include "strata/kernels/cpu/iq_avx2.hpp"
|
||||
#include "strata/kernels/cpu/kq_avx2.hpp"
|
||||
#include "strata/kernels/cpu/expert_layout.hpp"
|
||||
#include "ggml-cpu.h"
|
||||
#include "strata/kernels/iq_kernels.hpp"
|
||||
@@ -172,6 +173,53 @@ int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int se
|
||||
gu_diff, dn_diff);
|
||||
if (gu_diff || dn_diff) ++failures;
|
||||
}
|
||||
// UD-Q4_K_XL's formats: the multi-token AVX2 kernels (kq_avx2.cpp) against ggml-cpu's vec_dot per token,
|
||||
// BIT FOR BIT, for 1..8 tokens; then the time of 4 tokens (a verify window) both ways
|
||||
for (int role = 0; role < 2; ++role) {
|
||||
const int type = role == 0 ? f.gu_type : f.d_type;
|
||||
if (!cpu::kq256_supported(type) || (role == 0 && type != 12)) continue;
|
||||
const int n = role == 0 ? (int) H : (int) FF, rows = role == 0 ? (int) FF : (int) H;
|
||||
const size_t rb = role == 0 ? f.gu_row : f.d_row;
|
||||
const uint8_t* w = blob.data() + (role == 0 ? 0 : f.down_off);
|
||||
const auto* tc = ggml_get_type_traits_cpu((ggml_type) type);
|
||||
const ggml_type at = tc->vec_dot_type;
|
||||
std::vector<std::vector<uint8_t>> acts(8, std::vector<uint8_t>(ggml_row_size(at, n)));
|
||||
std::mt19937 arng(77 + seed);
|
||||
std::normal_distribution<float> and_(0.f, 1.f);
|
||||
std::vector<float> xs((size_t) n);
|
||||
const void* ap[8];
|
||||
for (int t = 0; t < 8; ++t) {
|
||||
for (auto& v : xs) v = and_(arng);
|
||||
ggml_get_type_traits_cpu(at)->from_float(xs.data(), acts[t].data(), n);
|
||||
ap[t] = acts[t].data();
|
||||
}
|
||||
std::vector<float> ref((size_t) 8 * rows), got((size_t) 8 * rows);
|
||||
for (int t = 0; t < 8; ++t)
|
||||
for (int r = 0; r < rows; ++r) tc->vec_dot(n, &ref[(size_t) t * rows + r], 0, w + (size_t) r * rb, 0, ap[t], 0, 1);
|
||||
size_t differ = 0;
|
||||
for (int nt = 1; nt <= 8; ++nt) {
|
||||
float* op[8];
|
||||
for (int t = 0; t < nt; ++t) op[t] = got.data() + (size_t) t * rows;
|
||||
cpu::kq256_rows(type, w, rb, n, ap, nt, op, 0, rows);
|
||||
for (int t = 0; t < nt; ++t)
|
||||
differ += std::memcmp(op[t], ref.data() + (size_t) t * rows, (size_t) rows * 4) != 0;
|
||||
}
|
||||
const int it = 30;
|
||||
float* op4[4] = {got.data(), got.data() + rows, got.data() + 2 * rows, got.data() + 3 * rows};
|
||||
auto t0 = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < it; ++i) cpu::kq256_rows(type, w, rb, n, ap, 4, op4, 0, rows);
|
||||
auto t1 = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < it; ++i)
|
||||
for (int r = 0; r < rows; ++r)
|
||||
for (int t = 0; t < 4; ++t) tc->vec_dot(n, op4[t] + r, 0, w + (size_t) r * rb, 0, ap[t], 0, 1);
|
||||
auto t2 = std::chrono::steady_clock::now();
|
||||
const double us_k = std::chrono::duration<double, std::micro>(t1 - t0).count() / it;
|
||||
const double us_g = std::chrono::duration<double, std::micro>(t2 - t1).count() / it;
|
||||
std::printf(" %s %s rows, AVX2 multi-token vs ggml vec_dot: %zu of 36 token-sets differ in any bit; "
|
||||
"4 tokens one thread %.0f us vs ggml %.0f us (%.2fx)\n", ggml_type_name((ggml_type) type),
|
||||
role == 0 ? "gate" : "down", differ, us_k, us_g, us_g / us_k);
|
||||
if (differ) ++failures;
|
||||
}
|
||||
if (f.d_type == 42) {
|
||||
// (b2) the GGUF-layout Q2_0 kernel the pool uses for Q2_0 down projections - the AVX-512 one
|
||||
// where the CPU has it, the AVX-2 one (q2_avx2.cpp) where it does not. Calling the AVX-512
|
||||
|
||||
@@ -2320,6 +2320,8 @@ int main(int argc, char** argv) {
|
||||
// CS-T: a native pack without experts.bin maps the model's GGUF shards instead (native_experts.txt's spans,
|
||||
// checked against the files first) - no 30-77 GB copy of the experts on the disk
|
||||
src.set_gguf(o.native_preset);
|
||||
if (const char* v = std::getenv("STRATA_FETCH_THREADS"); v != nullptr && std::atoi(v) > 0)
|
||||
src.set_fetch_threads(std::atoi(v));
|
||||
if (!src.open(o.pack, g.n_layers, g.n_expert, err)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
|
||||
return 1;
|
||||
@@ -5477,6 +5479,10 @@ int main(int argc, char** argv) {
|
||||
if (drive.d.pcie_num < 0) drive.d.pcie_num = 0;
|
||||
if (drive.d.pcie_num > 256) drive.d.pcie_num = 256;
|
||||
const int64_t pcie0 = drive.d.pcie_experts;
|
||||
// CS-T: the file tier since the decode began (the prompt path's copies are before this)
|
||||
const int64_t files0 = src.file_reads(), ram0 = src.ram_reads();
|
||||
const uint64_t fbytes0 = src.file_blob_bytes(), fall0 = src.file_read_bytes();
|
||||
const double fms0 = src.file_ms();
|
||||
if (o.adapt_every > 0 && o.adapt_swaps > 0) drive.d.usage.assign((size_t) (g.n_layers * g.n_expert), 0.0f);
|
||||
int64_t swaps_total = 0;
|
||||
double ms_adapt = 0;
|
||||
@@ -5716,12 +5722,15 @@ int main(int argc, char** argv) {
|
||||
std::printf("%-24s %.2f GiB of experts in RAM, %lld exchanged with the VRAM tier, %lld blob reads from "
|
||||
"the file\n", "resident RAM", (double) src.resident_bytes() / 1073741824.0,
|
||||
(long long) src.exchanges(), (long long) src.file_reads());
|
||||
if (srcp == &src) // CS-T: the RAM and file tiers (the GPU cache's share is the hit rate above)
|
||||
std::printf("%-24s RAM %lld blobs, files %lld blobs, %.1f MB read from the files (%.2f MB/round)%s\n",
|
||||
"expert tiers", (long long) src.ram_reads(), (long long) src.file_reads(),
|
||||
(double) src.file_read_bytes() / 1e6,
|
||||
rounds > 0 ? (double) src.file_read_bytes() / 1e6 / rounds : 0.0,
|
||||
src.gguf_mode() ? " (the GGUF in place)" : "");
|
||||
if (srcp == &src) { // CS-T: the RAM and file tiers (the GPU cache's share is the hit rate above)
|
||||
const double fms = src.file_ms() - fms0, fmb = (double) (src.file_blob_bytes() - fbytes0) / 1e6;
|
||||
std::printf("%-24s decode: RAM %lld blobs, files %lld blobs, %.1f MB read from the files (%.2f MB/round, "
|
||||
"%.1f ms/round of reading, %.2f GB/s per reading thread)%s; prompt copies %.1f MB\n",
|
||||
"expert tiers", (long long) (src.ram_reads() - ram0), (long long) (src.file_reads() - files0),
|
||||
fmb, rounds > 0 ? fmb / rounds : 0.0, rounds > 0 ? fms / rounds : 0.0,
|
||||
fms > 0 ? fmb / fms : 0.0, src.gguf_mode() ? " (the GGUF in place)" : "",
|
||||
(double) (fall0 - fbytes0) / 1e6);
|
||||
}
|
||||
if (rounds > 0 && drive.d.pcie_num > 0)
|
||||
std::printf("%-24s %.2f distinct experts per layer read over PCIe (share %d/256 of the misses)\n",
|
||||
"pcie experts", (double) (drive.d.pcie_experts - pcie0) / (double) (rounds * g.n_layers),
|
||||
|
||||
@@ -240,6 +240,24 @@ void arena() {
|
||||
check(p == q && std::memcmp(p, dst.data() + L.blob_offset(0, 1), (size_t) L.blob_bytes(0)) == 0,
|
||||
"an assembled blob stays put while it is in use");
|
||||
check(fs.file_read_bytes() > 0, "the file tier counts its bytes (" + std::to_string(fs.file_read_bytes()) + ")");
|
||||
// prefetch: a layer's experts fetched together on several threads, then the same bytes from blob()
|
||||
bool pre = true;
|
||||
for (int round = 0; round < 3; ++round)
|
||||
for (int64_t l = 0; l < 2; ++l) {
|
||||
fs.begin_layer(l, nullptr, 0);
|
||||
fs.begin_layer(l == 0 ? 1 : 0, nullptr, 0); // age the buffers so the prefetch refills them
|
||||
fs.begin_layer(l, nullptr, 0);
|
||||
const int64_t both[2] = {1, 0};
|
||||
fs.set_fetch_threads(2);
|
||||
fs.prefetch(l, both, 2);
|
||||
for (int64_t e = 0; e < NE; ++e) {
|
||||
const uint8_t* b = fs.blob(l, e);
|
||||
pre = pre && b != nullptr &&
|
||||
std::memcmp(b, dst.data() + L.blob_offset(l, e), (size_t) L.blob_bytes(l)) == 0;
|
||||
}
|
||||
}
|
||||
check(pre && fs.file_blob_bytes() > 0 && fs.file_ms() >= 0.0,
|
||||
"prefetch on 2 threads, then blob(): the same bytes");
|
||||
}
|
||||
fs.close();
|
||||
// with experts.bin present, the pack's file is mapped as before, whatever set_gguf says
|
||||
|
||||
Reference in new issue
Block a user