the card's ExpertCache: a slot for each resident expert (res) in its size's pool, filled from its host copy; the layers' tables. With arena_score (FNX_ARENA) the largest pool's slots go coldest first and are neither allocated nor filled here: the engine puts them in its arena (Engine::arena_fill); until then its experts point at RAM
| 314 | // layers' tables. With arena_score (FNX_ARENA) the largest pool's slots go coldest first and are neither allocated |
| 315 | // nor filled here: the engine puts them in its arena (Engine::arena_fill); until then its experts point at RAM |
| 316 | void expert_cache(const std::vector<std::vector<char>>& res, const std::vector<const char*>& hgu, const std::vector<const char*>& hdn, |
| 317 | const std::vector<double>* arena_score) { |
| 318 | ExpertCache& ec = c.ec; |
| 319 | std::vector<std::pair<size_t, size_t>> sizes; // each pool's (gate|up, down) bytes |
| 320 | for (int l = 0; l < NL; ++l) { |
| 321 | const LayerW& L = c.L[(size_t) l]; |
| 322 | if (L.gu_e_stride % 16 || L.down_e_stride % 16) throw std::runtime_error("expert cache: expert strides not in 16-byte units"); |
| 323 | const std::pair<size_t, size_t> s{L.gu_e_stride, L.down_e_stride}; |
| 324 | auto it = std::find(sizes.begin(), sizes.end(), s); |
| 325 | if (it == sizes.end()) { |
| 326 | if (sizes.size() == EC_POOLS) throw std::runtime_error("expert cache: more than " + std::to_string(EC_POOLS) + " expert sizes"); |
| 327 | sizes.push_back(s); |
| 328 | it = sizes.end() - 1; |
| 329 | } |
| 330 | ec.pool_of[l] = (int) (it - sizes.begin()); |
| 331 | } |
| 332 | ec.n_pools = (int) sizes.size(); |
| 333 | if (arena_score) { // the largest pool by bytes |
| 334 | size_t most = 0; |
| 335 | for (int p = 0; p < ec.n_pools; ++p) { |
| 336 | size_t b = 0; |
| 337 | for (int l = 0; l < NL; ++l) b += ec.pool_of[l] == p ? (size_t) NEXP * (sizes[(size_t) p].first + sizes[(size_t) p].second) : 0; |
| 338 | if (b > most) most = b, ec.arena_pool = p; |
| 339 | } |
| 340 | } |
| 341 | std::vector<int> owner, slot_of((size_t) NL * NEXP, -1); |
| 342 | for (int p = 0; p < ec.n_pools; ++p) { |
| 343 | ec.s0[p] = (int) owner.size(); |
| 344 | for (int l = 0; l < NL; ++l) |
| 345 | if (ec.pool_of[l] == p) |
| 346 | for (int e = 0; e < NEXP; ++e) |
| 347 | if (res[(size_t) l][(size_t) e]) owner.push_back(l * NEXP + e); |
| 348 | if (p == ec.arena_pool) |
| 349 | std::stable_sort(owner.begin() + ec.s0[p], owner.end(), |
| 350 | [&](int a, int b) { return (*arena_score)[(size_t) a] < (*arena_score)[(size_t) b]; }); |
| 351 | for (int s = ec.s0[p]; s < (int) owner.size(); ++s) slot_of[(size_t) owner[(size_t) s]] = s; |
| 352 | ec.n[p] = (int) owner.size() - ec.s0[p]; |
| 353 | if (ec.n[p] < EXPERT_WINDOW_SLOTS) |
| 354 | throw std::runtime_error("expert cache: pool " + std::to_string(p) + " has " + std::to_string(ec.n[p]) + " slots, a window needs " + |
| 355 | std::to_string(EXPERT_WINDOW_SLOTS) + " (fewer experts in RAM)"); |
| 356 | ec.dn_off[p] = sizes[(size_t) p].first; |
| 357 | ec.slot_bytes[p] = sizes[(size_t) p].first + sizes[(size_t) p].second; |
| 358 | if (p == ec.arena_pool) continue; |
| 359 | ck(cudaMalloc(&ec.pool[p], (size_t) ec.n[p] * ec.slot_bytes[p]), "expert cache: slots"); |
| 360 | if (track) track->push_back(ec.pool[p]); |
| 361 | c.bytes += (size_t) ec.n[p] * ec.slot_bytes[p]; |
| 362 | } |
| 363 | ec.n_slots = (int) owner.size(); |
| 364 | ec.arena_full = arena_score && ec.n_slots == NL * NEXP; |
| 365 | std::vector<const char*> epg(hgu), epd(hdn); |
| 366 | for (int s = 0; s < ec.n_slots; ++s) { |
| 367 | const int i = owner[(size_t) s], p = ec.pool_of[i / NEXP]; |
| 368 | if (p == ec.arena_pool) continue; |
| 369 | const LayerW& L = c.L[(size_t) (i / NEXP)]; |
| 370 | char* at = ec.pool[p] + (size_t) (s - ec.s0[p]) * ec.slot_bytes[p]; |
| 371 | ck(cudaMemcpy(at, hgu[(size_t) i], L.gu_e_stride, cudaMemcpyDefault), "expert cache: fill"); |
| 372 | ck(cudaMemcpy(at + ec.dn_off[p], hdn[(size_t) i], L.down_e_stride, cudaMemcpyDefault), "expert cache: fill"); |
| 373 | epg[(size_t) i] = at; |
no test coverage detected