| 42 | } |
| 43 | |
| 44 | bool Bridge::load(const std::string& pack, int device, int lanes, const std::vector<int32_t>& vocab, const void* head_rows, size_t row_bytes, int head_type, |
| 45 | std::function<void(int, float*)> embed, std::string& err) { |
| 46 | Impl& I = *p_; |
| 47 | I.device = device; |
| 48 | I.embed = std::move(embed); |
| 49 | // the ring log (host copies of the activation rows for a rebuild) is the main engine's way of resuming a conversation; the split starts a lane's ring over instead |
| 50 | #ifdef _WIN32 |
| 51 | if (!std::getenv("STRATA_DF2_RING_LOG")) _putenv_s("STRATA_DF2_RING_LOG", "0"); |
| 52 | #else |
| 53 | ::setenv("STRATA_DF2_RING_LOG", "0", 0); |
| 54 | #endif |
| 55 | if (cudaSetDevice(device) != cudaSuccess) { err = "dflash2: cannot select the card"; return false; } |
| 56 | I.d.emplace_back(new Dflash2Drafter); |
| 57 | Dflash2Drafter& parent = *I.d[0]; |
| 58 | if (!parent.load(pack, device, err)) return false; |
| 59 | for (int i = 1; i < lanes; ++i) { |
| 60 | I.d.emplace_back(new Dflash2Drafter); |
| 61 | if (!I.d.back()->clone_alloc(parent, err)) return false; |
| 62 | } |
| 63 | const size_t bytes = vocab.size() * row_bytes; |
| 64 | if (cudaMalloc((void**) &I.head, bytes) != cudaSuccess) { err = "dflash2: the draft head's rows do not fit"; return false; } |
| 65 | if (cudaMemcpy(I.head, head_rows, bytes, cudaMemcpyHostToDevice) != cudaSuccess) { err = "dflash2: the draft head's upload"; return false; } |
| 66 | I.extra += bytes; |
| 67 | const int N = parent.n_embd(); |
| 68 | I.mask.assign((size_t) N, 0.f); |
| 69 | I.embed(parent.mask_token(), I.mask.data()); |
| 70 | Impl* ip = &I; |
| 71 | auto fn = [ip](const int32_t*, int n, float* out, cudaStream_t cs) { |
| 72 | const int lane = ip->lane_of(cs); |
| 73 | const int L = lane < 0 ? 0 : lane; |
| 74 | cudaMemcpyAsync(out, ip->stage[(size_t) L], (size_t) n * (size_t) (ip->mask.size()) * 4, cudaMemcpyDeviceToDevice, cs); |
| 75 | }; |
| 76 | for (int i = 0; i < lanes; ++i) { |
| 77 | float* s = nullptr; |
| 78 | if (cudaMalloc((void**) &s, (size_t) kRows * (size_t) N * 4) != cudaSuccess) { err = "dflash2: the embedding staging does not fit"; return false; } |
| 79 | I.extra += (size_t) kRows * (size_t) N * 4; |
| 80 | std::vector<float> rows((size_t) kRows * (size_t) N); |
| 81 | for (int r = 0; r < kRows; ++r) std::memcpy(rows.data() + (size_t) r * N, I.mask.data(), (size_t) N * 4); |
| 82 | cudaMemcpy(s, rows.data(), rows.size() * 4, cudaMemcpyHostToDevice); |
| 83 | I.stage.push_back(s); |
| 84 | I.anchor.emplace_back((size_t) N, 0.f); |
| 85 | } |
| 86 | if (!parent.bind_raw(fn, I.head, head_type, (int64_t) vocab.size(), vocab, err)) return false; |
| 87 | for (int i = 1; i < lanes; ++i) |
| 88 | if (!I.d[(size_t) i]->clone_bind(parent, err)) return false; |
| 89 | for (auto& dr : I.d) dr->set_max_drafts(kDrafts); |
| 90 | I.taps = parent.tap_layers(); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | int Bridge::device() const { return p_->device; } |
| 95 | int Bridge::lanes() const { return (int) p_->d.size(); } |