| 185 | } |
| 186 | |
| 187 | bool Dflash2Drafter::load(const std::string& path, int device, std::string& err) { |
| 188 | device_ = device; |
| 189 | const OnDevice on_device(device_); |
| 190 | std::FILE* f = std::fopen(path.c_str(), "rb"); |
| 191 | if (f == nullptr) { err = "dflash2: cannot open " + path; return false; } |
| 192 | struct Closer { std::FILE* f; ~Closer() { std::fclose(f); } } closer{f}; |
| 193 | uint32_t hdr[3]; |
| 194 | if (std::fread(hdr, 4, 3, f) != 3 || std::memcmp(hdr, "DF2P", 4) != 0 || hdr[1] != 1) { err = "dflash2: " + path + " is not a DF2P v1 pack"; return false; } |
| 195 | std::string index(hdr[2], ' '); |
| 196 | if (std::fread(index.data(), 1, hdr[2], f) != hdr[2]) { err = "dflash2: the pack's index is cut short"; return false; } |
| 197 | const long data_off = 12 + (long) hdr[2]; |
| 198 | uint64_t total = 0; |
| 199 | for (size_t i = 0; i < index.size();) { |
| 200 | const size_t j = index.find('\n', i); |
| 201 | if (j == std::string::npos) break; |
| 202 | const std::string line = index.substr(i, j - i); |
| 203 | i = j + 1; |
| 204 | char a[256], b[64], c[256]; |
| 205 | long long v1, v2, v3, v4; |
| 206 | if (std::sscanf(line.c_str(), "cfg %63s %255s", b, c) == 2) { |
| 207 | const std::string key = b; |
| 208 | if (key == "hidden") N_ = std::atoi(c); |
| 209 | else if (key == "layers") L_ = std::atoi(c); |
| 210 | else if (key == "heads") NH_ = std::atoi(c); |
| 211 | else if (key == "kv_heads") NKV_ = std::atoi(c); |
| 212 | else if (key == "head_dim") HD_ = std::atoi(c); |
| 213 | else if (key == "inter") I_ = std::atoi(c); |
| 214 | else if (key == "window") window_ = std::atoi(c); |
| 215 | else if (key == "rope_theta") theta_ = (float) std::atof(c); |
| 216 | else if (key == "eps") eps_ = (float) std::atof(c); |
| 217 | else if (key == "conv_group") group_ = std::atoi(c); |
| 218 | else if (key == "rank") rank_ = std::atoi(c); |
| 219 | else if (key == "mask_token") mask_token_ = std::atoi(c); |
| 220 | else if (key == "vocab") vocab_ = std::atoll(c); |
| 221 | else if (key == "taps") { |
| 222 | taps_.clear(); |
| 223 | for (const char* q = c; *q;) { taps_.push_back(std::atoi(q)); while (*q && *q != ',') ++q; if (*q == ',') ++q; } |
| 224 | } |
| 225 | } else if (std::sscanf(line.c_str(), "t %255s %63s %lld %lld %lld %lld", a, b, &v1, &v2, &v3, &v4) == 6) { |
| 226 | Tensor t; |
| 227 | t.name = a; t.dtype = b; t.rows = v1; t.cols = v2; t.off = (uint64_t) v3; t.bytes = (uint64_t) v4; |
| 228 | tensors_.push_back(t); |
| 229 | total = std::max(total, t.off + t.bytes); |
| 230 | } |
| 231 | } |
| 232 | if (tensors_.empty() || NH_ != 32 || NKV_ != 8 || HD_ != 128 || rank_ != 256 || N_ != 2560 || L_ > 8 || window_ + 16 > kRing) { |
| 233 | err = "dflash2: the pack's shape is not the Flash-Next drafter this engine was written for"; |
| 234 | return false; |
| 235 | } |
| 236 | if (cudaMalloc((void**) &blob_, total) != cudaSuccess) { err = "dflash2: the pack does not fit in VRAM (" + std::to_string(total >> 20) + " MiB)"; return false; } |
| 237 | vram_ += total; |
| 238 | uint64_t max_bytes = 0; |
| 239 | for (const Tensor& t : tensors_) max_bytes = std::max(max_bytes, t.bytes); |
| 240 | std::vector<uint8_t> stage(max_bytes); |
| 241 | uint8_t* tmp = nullptr; |
| 242 | if (cudaMalloc((void**) &tmp, max_bytes) != cudaSuccess) { err = "dflash2: upload scratch"; return false; } |
| 243 | struct TmpFree { uint8_t* p; ~TmpFree() { cudaFree(p); } } tmp_free{tmp}; |
| 244 | for (const Tensor& t : tensors_) { |
nothing calls this directly
no test coverage detected