| 4014 | } |
| 4015 | |
| 4016 | bool FileExpertSource::pin_cache_complement( |
| 4017 | const ExpertCache& cache, std::string& err, bool pin, |
| 4018 | const std::vector<std::pair<int32_t, int32_t>>& additional_gpu_pairs, int64_t lend_from_slot, |
| 4019 | uint64_t headroom_bytes, uint64_t budget_bytes, const std::vector<std::pair<int32_t, int32_t>>* rank) { |
| 4020 | err.clear(); |
| 4021 | const auto pin_t0 = std::chrono::steady_clock::now(); |
| 4022 | if (base_ == nullptr) { err = "FileExpertSource: open the mapped experts before pinning a complement"; return false; } |
| 4023 | if (complement_ready_) { err = "FileExpertSource: the cache complement is already pinned"; return false; } |
| 4024 | if (!cache.valid()) { err = "FileExpertSource: the GPU expert cache is not open"; return false; } |
| 4025 | if (cache.fills() != cache.resident()) { |
| 4026 | err = "FileExpertSource: the GPU expert cache is not fully filled"; |
| 4027 | return false; |
| 4028 | } |
| 4029 | const cudaError_t sync = cudaDeviceSynchronize(); |
| 4030 | if (sync != cudaSuccess) { |
| 4031 | err = std::string("FileExpertSource: GPU expert cache is not ready: ") + cudaGetErrorString(sync); |
| 4032 | (void) cudaGetLastError(); |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
| 4036 | // The GPU cache's experts, and the bytes each slot's expert takes here (for the lend region below). |
| 4037 | const int64_t n_slots = cache.slots(); |
| 4038 | std::vector<std::pair<int32_t, int32_t>> primary_gpu_pairs; |
| 4039 | std::vector<int32_t> pair_slot; |
| 4040 | std::vector<uint64_t> slot_bytes((size_t) std::max<int64_t>(n_slots, 0), 0); |
| 4041 | primary_gpu_pairs.reserve((size_t) cache.resident()); |
| 4042 | pair_slot.reserve((size_t) cache.resident()); |
| 4043 | for (int64_t layer = 0; layer < n_layers_; ++layer) { |
| 4044 | for (int64_t expert = 0; expert < n_expert_; ++expert) { |
| 4045 | const int32_t slot = cache.slot_of(layer, expert); |
| 4046 | if (slot == kNotResident) continue; |
| 4047 | primary_gpu_pairs.emplace_back((int32_t) layer, (int32_t) expert); |
| 4048 | pair_slot.push_back(slot); |
| 4049 | if (slot >= 0 && slot < n_slots) slot_bytes[(size_t) slot] = layer_blob_bytes_[(size_t) layer]; |
| 4050 | } |
| 4051 | } |
| 4052 | std::vector<uint64_t> offsets; |
| 4053 | uint64_t bytes = 0; |
| 4054 | if (!detail::make_cache_complement_plan(n_layers_, n_expert_, layer_blob_bytes_, primary_gpu_pairs, |
| 4055 | additional_gpu_pairs, offsets, bytes, err)) return false; |
| 4056 | #if defined(_WIN32) |
| 4057 | // #467: the GPU cache's pre-fill touched its experts through the mapping (~19 GiB on a 24 GB card), and Windows |
| 4058 | // counts those file pages in this process's working set, not as available: a 32 GB PC read 0.44 GiB here |
| 4059 | // (20.7 GiB before the start). Trimmed, they move to the standby list (still cached, counted as available). |
| 4060 | // Resident mode only: nothing else calls this function. Locked/pinned pages stay; the rest fault back softly. |
| 4061 | { |
| 4062 | uint64_t before = 0, after = 0; |
| 4063 | const bool read_before = available_memory_bytes(before); |
| 4064 | (void) SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T) -1, (SIZE_T) -1); |
| 4065 | if (read_before && available_memory_bytes(after)) |
| 4066 | std::fprintf(stderr, "FileExpertSource: available RAM %.2f GiB, %.2f GiB after the mapped experts left the " |
| 4067 | "process working set (#467)\n", |
| 4068 | (double) before / 1073741824.0, (double) after / 1073741824.0); |
| 4069 | } |
| 4070 | #endif |
| 4071 | const bool what_fits = budget_bytes == kResidentWhatFits; // #467: the soft mode's second try |
| 4072 | int64_t budget_lent = 0, budget_lendable = 0; // the lend region's slots kept by a budget (below), and its size |
| 4073 | uint64_t budget_physical = 0; // #403: reuse the memory snapshot a budget was sized from |