DFlash2 (src/df2_bridge.hpp): the pack, the draft vocabulary's rows of output.weight and the embedding of a token (the model's Q8_0 rows, dequantized as Engine::embed_token does) handed to the bridge, which puts them on the first card
| 1059 | // DFlash2 (src/df2_bridge.hpp): the pack, the draft vocabulary's rows of output.weight and the embedding of a token (the model's Q8_0 rows, dequantized as |
| 1060 | // Engine::embed_token does) handed to the bridge, which puts them on the first card |
| 1061 | void load_dflash2(fnx::df2::Bridge& br, const fnx::Model& m, const Args& a) { |
| 1062 | std::vector<int32_t> vocab; |
| 1063 | { |
| 1064 | std::FILE* f = std::fopen(a.draft_vocab.c_str(), "rb"); |
| 1065 | if (!f) throw std::runtime_error("dflash2: cannot open the draft vocabulary " + a.draft_vocab); |
| 1066 | int32_t id; |
| 1067 | while (std::fread(&id, 4, 1, f) == 1) vocab.push_back(id); |
| 1068 | std::fclose(f); |
| 1069 | if (vocab.empty()) throw std::runtime_error("dflash2: " + a.draft_vocab + " is empty"); |
| 1070 | for (int32_t t : vocab) |
| 1071 | if (t < 0 || t >= m.vocab) throw std::runtime_error("dflash2: " + a.draft_vocab + " holds a token outside the vocabulary"); |
| 1072 | } |
| 1073 | const fnx::Tensor* out = m.g.find("output.weight"); |
| 1074 | if (!out || out->type != fnx::T_Q8_0) throw std::runtime_error("dflash2: output.weight is not Q8_0"); |
| 1075 | const size_t rb = out->row_bytes(); |
| 1076 | std::vector<uint8_t> rows(vocab.size() * rb); |
| 1077 | for (size_t i = 0; i < vocab.size(); ++i) std::memcpy(rows.data() + i * rb, out->data + (size_t) vocab[i] * rb, rb); |
| 1078 | const fnx::Tensor* te = m.tok_embd; |
| 1079 | auto embed = [te, &m](int tok, float* o) { |
| 1080 | if (tok < 0 || tok >= m.vocab) throw std::runtime_error("dflash2: embedding of token " + std::to_string(tok)); |
| 1081 | const uint8_t* row = te->data + (size_t) tok * te->row_bytes(); |
| 1082 | for (int b = 0; b < fnx::N / 32; ++b) { |
| 1083 | uint16_t dh; |
| 1084 | std::memcpy(&dh, row + b * 34, 2); |
| 1085 | const float d = half_bits_to_float(dh); |
| 1086 | for (int j = 0; j < 32; ++j) o[b * 32 + j] = d * (float) (int8_t) row[b * 34 + 2 + j]; |
| 1087 | } |
| 1088 | }; |
| 1089 | std::string err; |
| 1090 | if (!br.load(a.draft_model, m.cards[0].dev, a.batch, vocab, rows.data(), rb, 8, embed, err)) throw std::runtime_error(err); |
| 1091 | std::fprintf(stderr, "sfnx: DFlash2 on card 0: %zu draft tokens, %d drafter(s), %.0f MiB of VRAM\n", vocab.size(), br.lanes(), (double) br.vram_bytes() / 1048576.0); |
| 1092 | } |
| 1093 | |
| 1094 | // the engine state after a failure part way is unknown: say why and _exit at once (the front end starts it again). No |
| 1095 | // destructors: freeing the conversation cache's pinned memory takes minutes, and the stdin reader thread keeps the |
no test coverage detected