the split engine (src/split) binds through bind_raw only: bind() copies head rows between cards with the peer API
| 369 | |
| 370 | #ifndef STRATA_DF2_NO_BIND // the split engine (src/split) binds through bind_raw only: bind() copies head rows between cards with the peer API |
| 371 | bool Dflash2Drafter::bind(const WeightTable& wt, const NativeHead* head, const std::string& vocab_file, std::string& err, int head_device) { |
| 372 | const OnDevice on_device(device_); |
| 373 | if (head == nullptr || !head->loaded()) { err = "dflash2: the drafter needs the native head (--native)"; return false; } |
| 374 | const WeightRef* we = wt.find("token_embd.weight"); |
| 375 | if (we == nullptr && native_embed() == nullptr) { err = "dflash2: token_embd.weight is missing"; return false; } // (a later stage of a layer split may not carry it: the native table serves) |
| 376 | EmbedFn embed = [we](const int32_t* tok, int n, float* out, cudaStream_t cs) { |
| 377 | if (const NativeEmbed* ne = native_embed()) { |
| 378 | ne->gather_dev(tok, n, out, cs); |
| 379 | } else if (we != nullptr) { |
| 380 | const auto* codes = (const uint8_t*) we->data; |
| 381 | const auto* scales = (const float*) (codes + we->codes_bytes); |
| 382 | const auto* offsets = we->has_offset ? (const float*) (codes + we->codes_bytes + we->scales_bytes) : nullptr; |
| 383 | kernels::embedding_gather_dev(codes, scales, offsets, tok, n, we->ne0, we->code_bits, we->code_bias, we->group_elems, |
| 384 | (uint64_t) (we->ne0 / (8 / we->code_bits)), (uint64_t) (we->ne0 / we->group_elems), out, cs); |
| 385 | } |
| 386 | }; |
| 387 | std::vector<int32_t> vocab; |
| 388 | // STRATA_DF2_HEAD=native: the drafter runs its head on the target's own resident head (every vocabulary row, no 213 MiB pruned copy, no vocabulary file read) |
| 389 | // (a head on another card, a layer split's last stage, is not read in place: the pruned copy is gathered there and copied over, as before) |
| 390 | const bool head_native = Dflash2Drafter::head_native() && (head_device < 0 || head_device == device_); |
| 391 | if (!vocab_file.empty() && !head_native) { |
| 392 | if (std::FILE* vf = std::fopen(vocab_file.c_str(), "rb")) { |
| 393 | std::fseek(vf, 0, SEEK_END); |
| 394 | const long size = std::ftell(vf); |
| 395 | std::fseek(vf, 0, SEEK_SET); |
| 396 | if (size >= 4 && size % 4 == 0) { |
| 397 | vocab.resize((size_t) size / 4); |
| 398 | if (std::fread(vocab.data(), 4, vocab.size(), vf) != vocab.size()) vocab.clear(); |
| 399 | } |
| 400 | std::fclose(vf); |
| 401 | } |
| 402 | } |
| 403 | const void* hw = head->weights(); |
| 404 | const int64_t row_bytes = (int64_t) head->row_bytes(); |
| 405 | int64_t head_rows = row_bytes > 0 ? (int64_t) (head->weight_bytes() / (uint64_t) row_bytes) : 0; |
| 406 | if (!vocab.empty() && shared_.rows != nullptr && !std::getenv("STRATA_DF2_SHARE_HEAD_OFF") && shared_.type == head->type() && |
| 407 | shared_.n == (int64_t) vocab.size() && shared_.vocab_host != nullptr && *shared_.vocab_host == vocab && shared_.vocab_dev != nullptr) { |
| 408 | // DF-05: the MTP drafter gathered these very rows (same vocabulary ids in the same order, the main head's own layout and type): use them |
| 409 | dhead_ = const_cast<uint8_t*>(shared_.rows); |
| 410 | dhead_shared_ = true; |
| 411 | hw = dhead_; |
| 412 | head_rows = (int64_t) vocab.size(); |
| 413 | } else if (!vocab.empty() && head_device >= 0 && head_device != device_) { |
| 414 | // the head is on another card: gather the subset's rows there, copy them over once |
| 415 | const size_t bytes = (size_t) ((int64_t) vocab.size() * row_bytes); |
| 416 | uint8_t* tmp = nullptr; |
| 417 | int32_t* dv = nullptr; |
| 418 | { |
| 419 | const OnDevice on_head(head_device); |
| 420 | if (cudaMalloc((void**) &dv, vocab.size() * 4) != cudaSuccess || cudaMalloc((void**) &tmp, bytes) != cudaSuccess) { |
| 421 | if (dv) cudaFree(dv); |
| 422 | err = "dflash2: the draft vocabulary's head rows do not fit on the head's card"; |
| 423 | return false; |
| 424 | } |
| 425 | cudaMemcpy(dv, vocab.data(), vocab.size() * 4, cudaMemcpyHostToDevice); |
| 426 | kernels::gather_rows((const uint8_t*) hw, row_bytes, dv, (int64_t) vocab.size(), tmp, nullptr); |
| 427 | cudaDeviceSynchronize(); |
| 428 | cudaFree(dv); |
nothing calls this directly
no test coverage detected