| 528 | } // namespace |
| 529 | |
| 530 | bool vision_encode_cpu(const VisionWeights& W, const PreparedImage& img, std::vector<float>& out, std::string& err, int threads, VisionCheckpoints* ck) { |
| 531 | const VisionCfg& c = W.c; |
| 532 | if (img.pix.empty() || img.gh % c.merge || img.gw % c.merge) { err = "vision: empty or misaligned picture"; return false; } |
| 533 | if (threads <= 0) threads = static_cast<int>(std::max(1u, std::thread::hardware_concurrency())); |
| 534 | const int S = img.gh * img.gw, D = c.hidden, H = c.heads, hd = c.head_dim(), F = c.ffn, PV = c.patch_vals(); |
| 535 | const int gh = img.gh, gw = img.gw, M2 = c.merge; |
| 536 | std::vector<float> x(static_cast<size_t>(S) * D), h(static_cast<size_t>(S) * D), qkv(static_cast<size_t>(S) * 3 * D), att(static_cast<size_t>(S) * D), mlp(static_cast<size_t>(S) * F); |
| 537 | // patch embed (conv3d as a linear layer over the flattened [C, T, P, P]) + the learned position grid, bilinearly resampled to (gh, gw) |
| 538 | gemm(S, D, PV, img.pix.data(), PV, WSrc{W.patch_w.data(), true, false, PV}, W.patch_b.data(), x.data(), D, false, threads); |
| 539 | { |
| 540 | const int side = c.pos_side; |
| 541 | auto lin = [&](int i, int n, double& idx, int& fl, int& ce, double& d) { |
| 542 | idx = n > 1 ? i * (static_cast<double>(side - 1) / (n - 1)) : 0.0; |
| 543 | fl = static_cast<int>(idx); |
| 544 | ce = std::min(fl + 1, side - 1); |
| 545 | d = idx - fl; |
| 546 | }; |
| 547 | for (int br = 0; br < gh / M2; ++br) |
| 548 | for (int bc = 0; bc < gw / M2; ++bc) |
| 549 | for (int dy = 0; dy < M2; ++dy) |
| 550 | for (int dx = 0; dx < M2; ++dx) { |
| 551 | const int row = br * M2 + dy, col = bc * M2 + dx; |
| 552 | double hi, wi, dh, dw; |
| 553 | int hf, hc, wf, wc; |
| 554 | lin(row, gh, hi, hf, hc, dh); |
| 555 | lin(col, gw, wi, wf, wc, dw); |
| 556 | const float w11 = static_cast<float>(dh * dw), w10 = static_cast<float>(dh - dh * dw), w01 = static_cast<float>(dw - dh * dw), w00 = static_cast<float>(1 - dh - (dw - dh * dw)); |
| 557 | const uint16_t* p00 = &W.pos[(static_cast<size_t>(hf) * side + wf) * D]; |
| 558 | const uint16_t* p01 = &W.pos[(static_cast<size_t>(hf) * side + wc) * D]; |
| 559 | const uint16_t* p10 = &W.pos[(static_cast<size_t>(hc) * side + wf) * D]; |
| 560 | const uint16_t* p11 = &W.pos[(static_cast<size_t>(hc) * side + wc) * D]; |
| 561 | float* xp = &x[(((static_cast<size_t>(br) * (gw / M2) + bc) * M2 + dy) * M2 + dx) * D]; |
| 562 | for (int i = 0; i < D; ++i) xp[i] += bf16f(p00[i]) * w00 + bf16f(p01[i]) * w01 + bf16f(p10[i]) * w10 + bf16f(p11[i]) * w11; |
| 563 | } |
| 564 | } |
| 565 | if (ck) ck->embed = x; |
| 566 | // 2-D rope tables: row angles on head dims 0..17, column angles on 18..35; dim j pairs with j + 36 (rotate_half) |
| 567 | const int half = hd / 2, nf = half / 2; // 36, 18 |
| 568 | std::vector<float> cr(static_cast<size_t>(std::max(gh, gw)) * nf), sr(cr.size()); |
| 569 | for (int p = 0; p < std::max(gh, gw); ++p) |
| 570 | for (int f = 0; f < nf; ++f) { |
| 571 | const double inv = 1.0 / std::pow(10000.0, static_cast<double>(2 * f) / half); |
| 572 | cr[static_cast<size_t>(p) * nf + f] = static_cast<float>(std::cos(p * inv)); |
| 573 | sr[static_cast<size_t>(p) * nf + f] = static_cast<float>(std::sin(p * inv)); |
| 574 | } |
| 575 | std::vector<int> prow(static_cast<size_t>(S)), pcol(static_cast<size_t>(S)); |
| 576 | for (int br = 0; br < gh / M2; ++br) |
| 577 | for (int bc = 0; bc < gw / M2; ++bc) |
| 578 | for (int dy = 0; dy < M2; ++dy) |
| 579 | for (int dx = 0; dx < M2; ++dx) { |
| 580 | const size_t i = ((static_cast<size_t>(br) * (gw / M2) + bc) * M2 + dy) * M2 + dx; |
| 581 | prow[i] = br * M2 + dy; |
| 582 | pcol[i] = bc * M2 + dx; |
| 583 | } |
| 584 | const float scale = 1.0f / std::sqrt(static_cast<float>(hd)); |
| 585 | const int QB = 64; |
| 586 | for (int il = 0; il < c.depth; ++il) { |
| 587 | const VisionBlock& B = W.blk[static_cast<size_t>(il)]; |