diff --git a/sycl/include/strata/core/expert_source.hpp b/sycl/include/strata/core/expert_source.hpp index 2a8731ae..78870cf8 100644 --- a/sycl/include/strata/core/expert_source.hpp +++ b/sycl/include/strata/core/expert_source.hpp @@ -570,6 +570,11 @@ public: bool pcie_layer(int64_t layer) const override; bool transient(int64_t layer, int64_t expert) const override; bool copy_blob(int64_t layer, int64_t expert, uint8_t* dst) override; + /// The blob read from the model file(s) only, never from the RAM copy or an exchange buffer (STRATA_CHECK_SWAPS). + bool copy_blob_from_file(int64_t layer, int64_t expert, uint8_t* dst) const { + return base_ != nullptr && dst != nullptr && layer >= 0 && expert >= 0 && layer < n_layers_ && expert < n_expert_ && + copy_from_files(layer, expert, dst); + } /// 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. diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp index b1a77d5b..4adbda2b 100644 --- a/sycl/src/program/generate.cpp +++ b/sycl/src/program/generate.cpp @@ -333,13 +333,16 @@ bool resident_stage_swaps(strata::core::FileExpertSource &src, if (slot < 0) continue; const SwapHome home = locate(s.layer); const strata::core::OnDevice on(home.dev); - /* - DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. - While the origin API might be synchronous, it depends on the type of - operand memory, so you may need to call wait() on event return by memcpy - API to ensure synchronization behavior. - */ - + // the evicted expert's bytes go back into exchange buffer q BEFORE its slot is overwritten + // (CUDA: cudaMemcpyAsync D2H on the owning card's stream; the dpct migration had dropped the call) + try { + home.stream->memcpy(src.exchange_buffer(q), home.cache->device_slot(slot), + (size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer)); + } catch (sycl::exception const& e) { + std::fprintf(stderr, "strata: copy back of layer %d slot %d (device %d) failed: %s\n", (int) s.layer, + (int) slot, home.dev, e.what()); + return false; + } bool seen = false; for (const SwapHome& u : used) seen = seen || u.stream == home.stream; if (!seen) used.push_back(home); @@ -349,6 +352,32 @@ bool resident_stage_swaps(strata::core::FileExpertSource &src, if (!staged.empty()) { for (const SwapHome& u : used) { const strata::core::OnDevice on(u.dev); + try { + u.stream->wait(); + } catch (sycl::exception const& e) { + std::fprintf(stderr, "strata: copy back sync (device %d) failed: %s\n", u.dev, e.what()); + return false; + } + } + // STRATA_CHECK_SWAPS=1 (a test aid): each evicted expert's exchange buffer must hold the model file's bytes + if (const char* chk = std::getenv("STRATA_CHECK_SWAPS"); chk != nullptr && chk[0] != '\0' && chk[0] != '0') { + static std::atomic n_ok{0}, n_bad{0}; + std::vector ref; + int64_t bad = 0; + for (const Staged& x : staged) { + ref.resize((size_t) strata::kernels::cpu::expert_layout().blob_bytes(x.layer)); + if (!src.copy_blob_from_file(x.layer, x.out, ref.data()) || + std::memcmp(ref.data(), src.exchange_buffer(x.q), ref.size()) != 0) { + ++bad; + std::fprintf(stderr, "strata: STRATA_CHECK_SWAPS: layer %d expert %d: the exchange buffer differs from the file\n", + (int) x.layer, (int) x.out); + } else { + ++n_ok; + } + } + n_bad += bad; + std::fprintf(stderr, "strata: STRATA_CHECK_SWAPS: %zu staged, %lld differ (total %lld ok, %lld differ)\n", + staged.size(), (long long) bad, (long long) n_ok.load(), (long long) n_bad.load()); } for (const Staged& x : staged) if (!src.stage_exchange(x.layer, x.in, x.out, x.q)) { @@ -12564,6 +12593,31 @@ int main(int argc, char **argv) try { 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()); + // STRATA_CHECK_SWAPS=1 (a test aid): after the run every expert must be held (the GPU cache or the RAM copy), + // and every RAM-copy blob must equal the model file's bytes (nothing lost, nothing stale after the swaps) + if (src.complement_ready()) + if (const char* chk = std::getenv("STRATA_CHECK_SWAPS"); chk != nullptr && chk[0] != '\0' && chk[0] != '0') { + int64_t held_ram = 0, held_gpu = 0, lost = 0, stale = 0; + std::vector ref; + for (int64_t l = 0; l < g.n_layers; ++l) + for (int64_t e = 0; e < g.n_expert; ++e) { + const bool ram = src.has_resident(l, e); + const bool gpu = host_res[(size_t) l * (size_t) g.n_expert + (size_t) e] >= 0; + held_ram += ram; + held_gpu += gpu; + if (!ram && !gpu) { ++lost; continue; } + if (!ram) continue; + ref.resize((size_t) strata::kernels::cpu::expert_layout().blob_bytes(l)); + if (!src.copy_blob_from_file(l, e, ref.data()) || + std::memcmp(ref.data(), src.resident_blob(l, e), ref.size()) != 0) { + if (stale++ < 8) + std::fprintf(stderr, "strata: STRATA_CHECK_SWAPS: layer %d expert %d: the RAM copy differs from the file\n", + (int) l, (int) e); + } + } + std::printf("%-24s audit: %lld experts in RAM, %lld in the GPU cache, %lld held by neither, %lld RAM blobs differ from the file\n", + "STRATA_CHECK_SWAPS", (long long) held_ram, (long long) held_gpu, (long long) lost, (long long) stale); + } 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, "