the 27B's draft head: this card's rows of output.weight for its share of the draft vocabulary (the layer itself came with the model). dir names where the layer comes from, so a Flash-Next runtime directory in a 27B config fails here
| 1173 | // the 27B's draft head: this card's rows of output.weight for its share of the draft vocabulary (the layer itself came |
| 1174 | // with the model). dir names where the layer comes from, so a Flash-Next runtime directory in a 27B config fails here |
| 1175 | Mtp::Mtp(const Model& m, const std::string& dir, const std::string& vocab_file) { |
| 1176 | if (dir != "nextn") throw std::runtime_error("mtp: the 27B's draft layer is the GGUF's own: its directory is \"nextn\", not \"" + dir + "\""); |
| 1177 | if (vocab_file.empty()) throw std::runtime_error("mtp: the 27B needs a draft vocabulary file"); |
| 1178 | { |
| 1179 | std::FILE* f = std::fopen(vocab_file.c_str(), "rb"); |
| 1180 | if (!f) throw std::runtime_error("mtp: cannot open " + vocab_file); |
| 1181 | #ifdef _WIN32 |
| 1182 | struct _stat64 stb{}; |
| 1183 | const int st_ok = _fstat64(_fileno(f), &stb); |
| 1184 | #else |
| 1185 | struct stat stb{}; |
| 1186 | const int st_ok = fstat(fileno(f), &stb); |
| 1187 | #endif |
| 1188 | if (st_ok != 0 || stb.st_size % 4 != 0) { |
| 1189 | std::fclose(f); |
| 1190 | throw std::runtime_error("mtp: " + vocab_file + " is not whole int32 token ids (" + std::to_string((long long) stb.st_size) + " bytes)"); |
| 1191 | } |
| 1192 | int32_t id; |
| 1193 | while (std::fread(&id, 4, 1, f) == 1) vocab.push_back(id); |
| 1194 | const bool io_bad = std::ferror(f) != 0; |
| 1195 | std::fclose(f); |
| 1196 | if (io_bad || vocab.size() * 4 != (size_t) stb.st_size) |
| 1197 | throw std::runtime_error("mtp: " + vocab_file + " stopped at " + std::to_string(vocab.size() * 4) + |
| 1198 | " of its " + std::to_string((long long) stb.st_size) + " bytes"); |
| 1199 | if (vocab.empty()) throw std::runtime_error("mtp: " + vocab_file + " is empty"); |
| 1200 | for (int id2 : vocab) |
| 1201 | if (id2 < 0 || id2 >= m.vocab) throw std::runtime_error("mtp: " + vocab_file + " holds a token outside the vocabulary"); |
| 1202 | } |
| 1203 | cards.resize(NCARD); |
| 1204 | const int step = ((int) vocab.size() + NCARD - 1) / NCARD; |
| 1205 | std::vector<std::thread> th; |
| 1206 | std::vector<std::string> errs(NCARD); |
| 1207 | for (int c = 0; c < NCARD; ++c) { |
| 1208 | cards[c].dv0 = std::min((int) vocab.size(), c * step); |
| 1209 | cards[c].dv1 = std::min((int) vocab.size(), (c + 1) * step); |
| 1210 | th.emplace_back([&, c] { |
| 1211 | try { |
| 1212 | MtpCard& mc = cards[c]; |
| 1213 | ck(cudaSetDevice(m.cards[c].dev), "cudaSetDevice"); |
| 1214 | CardW sink; // Up counts bytes into a CardW |
| 1215 | sink.dev = mc.dev = m.cards[c].dev; |
| 1216 | Up up{sink, nullptr, &mc.allocs}; |
| 1217 | mc.head = up.dense(up.rows(*m.g.find("output.weight"), std::vector<int>(vocab.begin() + mc.dv0, vocab.begin() + mc.dv1))); |
| 1218 | if (!mc.head.s.q) throw std::runtime_error("mtp: output.weight is not Q8_0"); |
| 1219 | } catch (const std::exception& e) { errs[c] = e.what(); } |
| 1220 | }); |
| 1221 | } |
| 1222 | for (auto& t : th) t.join(); |
| 1223 | for (int c = 0; c < NCARD; ++c) |
| 1224 | if (!errs[c].empty()) { |
| 1225 | free_mtp_cards(cards); |
| 1226 | throw std::runtime_error("mtp card " + std::to_string(c) + ": " + errs[c]); |
| 1227 | } |
| 1228 | } |
| 1229 | #else |
| 1230 | Mtp::Mtp(const Model& m, const std::string& dir, const std::string& vocab_file) { |
| 1231 | { |
nothing calls this directly
no test coverage detected