115 lines
4.9 KiB
C++
115 lines
4.9 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
namespace strata::kar {
|
|
|
|
// Identity of the verified immutable mapping and the exact three tensor slices
|
|
// used for one expert. The mapping is owned for the cache's whole lifetime.
|
|
struct ExpertTensorSlice {
|
|
uint64_t books_offset=0, scales_offset=0, planes_offset=0;
|
|
uint64_t section_offset=0, section_bytes=0, scales_bytes=0, books_bytes=0;
|
|
uint64_t dims[4]={};
|
|
uint64_t n_elements=0, n_padded=0, n_groups=0, words=0;
|
|
uint32_t plane_stride_bytes=0, payload_crc32=0, n_dims=0, group=0;
|
|
uint32_t planes_stored=0, planes_min=0, planes_max=0;
|
|
uint32_t storage_class=0, nesting=0, source_dtype=0, element_order=0;
|
|
};
|
|
|
|
struct ExpertCacheKey {
|
|
uintptr_t mapping=0;
|
|
uint64_t archive_bytes=0;
|
|
uint32_t expert=0, level=0;
|
|
std::array<ExpertTensorSlice,3> tensors{};
|
|
};
|
|
|
|
inline bool operator==(const ExpertTensorSlice& a, const ExpertTensorSlice& b) {
|
|
for (size_t i=0;i<4;++i) if (a.dims[i]!=b.dims[i]) return false;
|
|
return a.books_offset==b.books_offset && a.scales_offset==b.scales_offset &&
|
|
a.planes_offset==b.planes_offset && a.section_offset==b.section_offset &&
|
|
a.section_bytes==b.section_bytes && a.scales_bytes==b.scales_bytes &&
|
|
a.books_bytes==b.books_bytes && a.n_elements==b.n_elements &&
|
|
a.n_padded==b.n_padded && a.n_groups==b.n_groups && a.words==b.words &&
|
|
a.plane_stride_bytes==b.plane_stride_bytes && a.payload_crc32==b.payload_crc32 &&
|
|
a.n_dims==b.n_dims && a.group==b.group && a.planes_stored==b.planes_stored &&
|
|
a.planes_min==b.planes_min && a.planes_max==b.planes_max &&
|
|
a.storage_class==b.storage_class && a.nesting==b.nesting &&
|
|
a.source_dtype==b.source_dtype && a.element_order==b.element_order;
|
|
}
|
|
inline bool operator==(const ExpertCacheKey& a, const ExpertCacheKey& b) {
|
|
return a.mapping==b.mapping && a.archive_bytes==b.archive_bytes &&
|
|
a.expert==b.expert && a.level==b.level && a.tensors==b.tensors;
|
|
}
|
|
|
|
struct ExpertCacheStats {
|
|
uint64_t hits=0, misses=0, uploads=0, evictions=0;
|
|
};
|
|
|
|
// CUDA-independent cache ownership/index. A slot is not published until its
|
|
// upload has completed; any upload fault poisons the index closed.
|
|
class ExpertLruIndex {
|
|
public:
|
|
explicit ExpertLruIndex(size_t slots) : entries_(slots) {}
|
|
size_t capacity() const { return entries_.size(); }
|
|
const ExpertCacheStats& stats() const { return stats_; }
|
|
bool failed() const { return failed_; }
|
|
bool pending() const { return pending_; }
|
|
|
|
bool lookup(const ExpertCacheKey& key, size_t& slot) {
|
|
if (failed_ || pending_ || uploading_) return false;
|
|
for (size_t i=0;i<entries_.size();++i) if (entries_[i].valid && entries_[i].key==key) {
|
|
entries_[i].last_used=next_tick(); ++stats_.hits; slot=i; return true;
|
|
}
|
|
++stats_.misses; return false;
|
|
}
|
|
|
|
bool begin_upload(size_t& slot) {
|
|
if (failed_ || pending_ || uploading_ || entries_.empty()) return false;
|
|
slot=0;
|
|
for (size_t i=0;i<entries_.size();++i) {
|
|
if (!entries_[i].valid) { slot=i; break; }
|
|
if (entries_[i].last_used < entries_[slot].last_used) slot=i;
|
|
}
|
|
replacing_=entries_[slot].valid;
|
|
entries_[slot].valid=false; // overwritten bytes can no longer name the old key
|
|
uploading_=true; uploading_slot_=slot; return true;
|
|
}
|
|
|
|
bool publish_upload(size_t slot, const ExpertCacheKey& key) {
|
|
if (!uploading_ || slot!=uploading_slot_ || failed_) return false;
|
|
entries_[slot].key=key; entries_[slot].valid=true;
|
|
entries_[slot].last_used=next_tick(); ++stats_.uploads;
|
|
if (replacing_) ++stats_.evictions;
|
|
uploading_=false; replacing_=false; return true;
|
|
}
|
|
|
|
void fail_upload() { if (uploading_) entries_[uploading_slot_].valid=false; uploading_=false; replacing_=false; failed_=true; }
|
|
bool begin_run() { if (failed_ || pending_ || uploading_) return false; pending_=true; return true; }
|
|
bool finish_run() { if (!pending_ || failed_) return false; pending_=false; return true; }
|
|
void fail() { failed_=true; pending_=false; uploading_=false; replacing_=false; }
|
|
|
|
private:
|
|
struct Entry { ExpertCacheKey key{}; uint64_t last_used=0; bool valid=false; };
|
|
uint64_t next_tick() {
|
|
// Renormalize before wrap; relative ordering remains unchanged.
|
|
if (clock_==UINT64_MAX) {
|
|
uint64_t min=UINT64_MAX;
|
|
for (const auto& e:entries_) if (e.valid && e.last_used<min) min=e.last_used;
|
|
if (min!=UINT64_MAX) for (auto& e:entries_) if (e.valid) e.last_used-=min;
|
|
clock_=0;
|
|
for (const auto& e:entries_) if (e.valid && e.last_used>clock_) clock_=e.last_used;
|
|
}
|
|
return ++clock_;
|
|
}
|
|
std::vector<Entry> entries_;
|
|
ExpertCacheStats stats_{};
|
|
uint64_t clock_=0;
|
|
size_t uploading_slot_=0;
|
|
bool pending_=false, uploading_=false, replacing_=false, failed_=false;
|
|
};
|
|
|
|
} // namespace strata::kar
|