#pragma once #include #include #include #include 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 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;iclock_) clock_=e.last_used; } return ++clock_; } std::vector entries_; ExpertCacheStats stats_{}; uint64_t clock_=0; size_t uploading_slot_=0; bool pending_=false, uploading_=false, replacing_=false, failed_=false; }; } // namespace strata::kar