================================ THE TWO ENDS OF A TOKEN ================================
| 1354 | } |
| 1355 | // ================================ THE TWO ENDS OF A TOKEN ================================ |
| 1356 | bool embed_row(const WeightTable& tables, const ModelGeometry& g, int64_t token, float* out_dev, |
| 1357 | void* stream, std::string& err) { |
| 1358 | if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table (IQ model files) |
| 1359 | if (token < 0 || out_dev == nullptr) { err = "embed_row: invalid token or output"; return false; } |
| 1360 | ne->gather_one(token, out_dev, stream); |
| 1361 | return true; |
| 1362 | } |
| 1363 | const WeightRef* w = tables.find(EMBEDDING_NAME); |
| 1364 | if (w == nullptr) { err = "token_embd.weight is missing"; return false; } |
| 1365 | if (w->code_bits != 2 && w->code_bits != 4 && w->code_bits != 8) { |
| 1366 | err = "embed_row: token_embd.weight is not an S2/S4/S8 tensor"; |
| 1367 | return false; |
| 1368 | } |
| 1369 | if (w->codebook_iq4nl) { |
| 1370 | err = "embed_row: an IQ4NL embedding is not supported"; |
| 1371 | return false; |
| 1372 | } |
| 1373 | if (w->data == nullptr || out_dev == nullptr || w->ne0 <= 0 || w->ne1 <= 0 || |
| 1374 | w->ne0 != g.n_embd || w->group_elems <= 0 || |
| 1375 | w->ne0 % w->group_elems != 0 || w->ne0 % (8 / w->code_bits) != 0) { |
| 1376 | err = "embed_row: invalid embedding pointers, dimensions or group size"; |
| 1377 | return false; |
| 1378 | } |
| 1379 | if (token < 0 || token >= w->ne1) { |
| 1380 | err = "embed_row: token " + std::to_string(token) + " is outside 0.." + std::to_string(w->ne1 - 1); |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | const uint64_t row_codes = (uint64_t) (w->ne0 / (8 / w->code_bits)); |
| 1385 | const uint64_t row_groups = (uint64_t) (w->ne0 / w->group_elems); |
| 1386 | const uint64_t rows = (uint64_t) w->ne1; |
| 1387 | // Division-based checks avoid overflow in rows * row_bytes and reject partial |
| 1388 | // code/group rows before any device pointer arithmetic. |
| 1389 | if (w->codes_bytes > w->bytes || w->scales_bytes > w->bytes - w->codes_bytes || |
| 1390 | w->offset_bytes != w->bytes - w->codes_bytes - w->scales_bytes || |
| 1391 | w->codes_bytes % row_codes != 0 || w->codes_bytes / row_codes != rows || |
| 1392 | w->codes_bytes % alignof(float) != 0 || w->scales_bytes % sizeof(float) != 0 || |
| 1393 | (w->scales_bytes / sizeof(float)) % row_groups != 0 || |
| 1394 | (w->scales_bytes / sizeof(float)) / row_groups != rows || |
| 1395 | w->has_offset != (w->offset_bytes != 0) || |
| 1396 | (w->has_offset && w->offset_bytes != w->scales_bytes)) { |
| 1397 | err = "embed_row: embedding planes do not match the loaded tensor shape"; |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
| 1401 | const auto* codes = static_cast<const uint8_t*>(w->data); |
| 1402 | const auto* scales = reinterpret_cast<const float*>(codes + w->codes_bytes); |
| 1403 | const auto* offsets = w->has_offset ? reinterpret_cast<const float*>(codes + w->codes_bytes + w->scales_bytes) |
| 1404 | : nullptr; |
| 1405 | const uint64_t group_offset = (uint64_t) token * row_groups; |
| 1406 | strata::kernels::embedding_gather(codes + (uint64_t) token * row_codes, scales + group_offset, |
| 1407 | offsets ? offsets + group_offset : nullptr, w->ne0, |
| 1408 | w->code_bits, w->code_bias, w->group_elems, out_dev, stream); |
| 1409 | return true; |
| 1410 | } |
| 1411 | bool lm_head_mix(const WeightTable& tables, const ModelGeometry& g, const BlockBuffers& bb, |
| 1412 | void* stream, std::string& err) { |
| 1413 | const WeightRef* wn = tables.find("output_hc_norm.weight"); |
no test coverage detected