| 1293 | } |
| 1294 | |
| 1295 | Model::Model(const std::string& path, Load how, Store* store, const std::string* image) : g(path) { |
| 1296 | // the cards first: reading the embeddings into RAM below takes seconds on a cold cache |
| 1297 | { |
| 1298 | int ndev = 0; |
| 1299 | ck(cudaGetDeviceCount(&ndev), "cudaGetDeviceCount"); |
| 1300 | // a build for another count than the cards it is given is a config or build mistake (a 2-card binary on |
| 1301 | // prod's three would serve, slower): CUDA_VISIBLE_DEVICES (the config's "gpu") must name exactly NCARD |
| 1302 | if (ndev != NCARD) |
| 1303 | throw std::runtime_error("fnx: built for " + std::to_string(NCARD) + " cards (FNX_NCARD), " + std::to_string(ndev) + |
| 1304 | " visible: set CUDA_VISIBLE_DEVICES (a config's \"gpu\") to " + std::to_string(NCARD) + |
| 1305 | " of them, or build for " + std::to_string(ndev) + " (make FNX_NCARD=" + std::to_string(ndev) + ")"); |
| 1306 | check_cards(); |
| 1307 | } |
| 1308 | check_supported(g); |
| 1309 | #if FNX_27B |
| 1310 | const std::string p = "qwen35."; |
| 1311 | // block_count holds the draft (nextn) layer too |
| 1312 | if (g.i(p + "block_count") != NL + g.i(p + "nextn_predict_layers") || g.i(p + "nextn_predict_layers") != 1 || |
| 1313 | g.i(p + "embedding_length") != N || g.i(p + "feed_forward_length") != FFD || g.i(p + "ssm.group_count") != NKH || |
| 1314 | g.i(p + "ssm.time_step_rank") != NVH || g.i(p + "attention.head_count") != NQH || |
| 1315 | g.i(p + "attention.head_count_kv") != NKVH || g.i(p + "full_attention_interval") != 4 || |
| 1316 | g.i(p + "attention.key_length") != HD || g.i(p + "attention.value_length") != HD || g.i(p + "ssm.state_size") != HS || |
| 1317 | g.i(p + "ssm.inner_size") != NVH * HS || g.i(p + "ssm.conv_kernel") != 4 || g.i(p + "rope.dimension_count") != NROT) |
| 1318 | throw std::runtime_error("27b: this GGUF's geometry is not Qwen3.8-27B's"); |
| 1319 | #else |
| 1320 | const std::string p = "qwen4exp."; |
| 1321 | if (g.i(p + "block_count") != NL || g.i(p + "embedding_length") != N || g.i(p + "expert_count") != NEXP || |
| 1322 | g.i(p + "ssm.group_count") != NKH || g.i(p + "ssm.time_step_rank") != NVH || g.i(p + "attention.head_count") != NQH || |
| 1323 | g.i(p + "attention.head_count_kv") != NKVH || g.i(p + "hyper_connection.count") != HC || |
| 1324 | g.i(p + "hyper_connection.low_rank") != LR || g.i(p + "expert_feed_forward_length") != FF || |
| 1325 | g.i(p + "attention.indexer.head_count") != QSA_HEADS || g.i(p + "attention.indexer.key_length") != QSA_KEY || |
| 1326 | g.i(p + "attention.indexer.top_k") != QSA_TOP_K || g.i(p + "expert_used_count") != TOPK || |
| 1327 | g.i(p + "rope.dimension_count") != NROT) |
| 1328 | throw std::runtime_error("fnx: this GGUF's geometry is not Flash-Next's"); |
| 1329 | // the shared expert is split by the routed experts' ff ranges (CardW ff0, ff1) |
| 1330 | for (int l = 0; l < NL; ++l) { |
| 1331 | const std::string b = "blk." + std::to_string(l) + "."; |
| 1332 | if (g.t(b + "ffn_gate_shexp.weight").ne[1] != FF || g.t(b + "ffn_up_shexp.weight").ne[1] != FF || g.t(b + "ffn_down_shexp.weight").ne[0] != FF) |
| 1333 | throw std::runtime_error("fnx: layer " + std::to_string(l) + "'s shared expert is not " + std::to_string(FF) + " wide"); |
| 1334 | } |
| 1335 | #endif |
| 1336 | eps = (float) g.f(p + "attention.layer_norm_rms_epsilon"); |
| 1337 | rope_base = (float) g.f(p + "rope.freq_base"); |
| 1338 | // the trained length bounds the context (Engine refuses past it, or past it x FNX_ROPE_SCALE), so a model must name it |
| 1339 | { |
| 1340 | const int64_t n = g.i(p + "context_length"); |
| 1341 | if (n <= 0 || n > INT32_MAX) throw std::runtime_error("fnx: " + p + "context_length " + std::to_string(n) + " is not a context length"); |
| 1342 | ctx_train = (int) n; |
| 1343 | } |
| 1344 | // the model's own rotary scaling, which Engine runs by default (choose_rope); what sFNX cannot follow throws here |
| 1345 | { |
| 1346 | const ModelRope mr = model_rope(g, p); |
| 1347 | yarn_factor = mr.yarn_factor; |
| 1348 | yarn_orig = mr.yarn_orig; |
| 1349 | } |
| 1350 | tok_embd = &g.t("token_embd.weight"); |
| 1351 | #if FNX_27B |
| 1352 | if (tok_embd->type != T_Q8_0) throw std::runtime_error("27b: token_embd.weight is not Q8_0 (the host's embedding reads Q8_0)"); |