| 2466 | } |
| 2467 | |
| 2468 | void FileExpertSource::prefetch(int64_t layer, const int64_t* experts, int64_t n) { |
| 2469 | if (!staged() || n <= 0 || layer < 0 || layer >= n_layers_) return; |
| 2470 | if (io_fill_ && io_pf_ && direct_.empty()) { // hand the layer's uncached blobs to the workers and go on: the kernels |
| 2471 | for (int64_t i = 0; i < n; ++i) { // that reach one before it lands wait on its pages, the rest compute |
| 2472 | const int64_t e = experts[i]; |
| 2473 | if (e < 0 || e >= n_expert_) continue; |
| 2474 | const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) e; |
| 2475 | if (pf_mark_ && pf_mark_[index].exchange(0, std::memory_order_relaxed)) { // 149-prefetch: read ahead, now asked for |
| 2476 | io_pf_used_.fetch_add(1, std::memory_order_relaxed); |
| 2477 | io_pf_used_bytes_.fetch_add(layer_blob_bytes_[(size_t) layer], std::memory_order_relaxed); |
| 2478 | } |
| 2479 | if (complement_ready_ && resident_blob(index) != nullptr) continue; |
| 2480 | if (!override_.empty() && override_[index] != nullptr) continue; |
| 2481 | uint64_t total = 0; |
| 2482 | if (cached_bytes(layer, e, total, false) >= total) continue; |
| 2483 | io_pf_demand_.fetch_add(1, std::memory_order_relaxed); |
| 2484 | io_enqueue(layer, e); |
| 2485 | } |
| 2486 | return; |
| 2487 | } |
| 2488 | std::vector<Fill> todo; |
| 2489 | const auto t0 = std::chrono::steady_clock::now(); |
| 2490 | claim_todo(layer, experts, n, todo, nullptr); |
| 2491 | fill_many(todo); |
| 2492 | if (io_pf_ && !todo.empty()) { |
| 2493 | io_crit_us_.fetch_add((uint64_t) std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count(), |
| 2494 | std::memory_order_relaxed); |
| 2495 | io_crit_n_.fetch_add(1, std::memory_order_relaxed); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | void FileExpertSource::claim_todo(int64_t layer, const int64_t* experts, int64_t n, std::vector<Fill>& todo, uint8_t* pending, |
| 2500 | bool zc) { |