strata-ling32 --vision-encode VISION.strata IMAGE [IMAGE ...] [--vision-device auto|cpu|gpu] [--device N] [--vision-threads N] [--out FILE] : picture -> embeddings, no language model loaded
| 1636 | |
| 1637 | // strata-ling32 --vision-encode VISION.strata IMAGE [IMAGE ...] [--vision-device auto|cpu|gpu] [--device N] [--vision-threads N] [--out FILE] : picture -> embeddings, no language model loaded |
| 1638 | int cmd_vision_encode(int argc, char** argv) { |
| 1639 | VisionRunOpts vo; |
| 1640 | vo.file = argv[2]; |
| 1641 | std::string out; |
| 1642 | for (int i = 3; i < argc; ++i) { |
| 1643 | const std::string a = argv[i]; |
| 1644 | auto nx = [&]() -> std::string { return i + 1 < argc ? argv[++i] : std::string(); }; |
| 1645 | if (a == "--vision-device") vo.backend = nx(); |
| 1646 | else if (a == "--device") vo.device = std::atoi(nx().c_str()); |
| 1647 | else if (a == "--vision-threads") vo.threads = std::atoi(nx().c_str()); |
| 1648 | else if (a == "--out") out = nx(); |
| 1649 | else vo.images.push_back(a); |
| 1650 | } |
| 1651 | std::string err; |
| 1652 | std::ofstream of; |
| 1653 | if (!out.empty()) of.open(out, std::ios::binary); |
| 1654 | #ifdef LING32_HAVE_CUDA |
| 1655 | VisionRun vr(vo); |
| 1656 | if (!vr.init(err)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; } |
| 1657 | std::printf("encoder: %s\n", vr.backend().c_str()); |
| 1658 | const VisionCfg c = vr.cfg(); |
| 1659 | #else |
| 1660 | if (vo.backend == "gpu") { std::fprintf(stderr, "strata-ling32: this build has no CUDA\n"); return 1; } |
| 1661 | VisionWeights W; |
| 1662 | if (!W.load(vo.file, err)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; } |
| 1663 | const VisionCfg c = W.c; |
| 1664 | std::printf("encoder: cpu\n"); |
| 1665 | #endif |
| 1666 | for (const std::string& img : vo.images) { |
| 1667 | PreparedImage im; |
| 1668 | if (!vision_prepare_file(c, img, im, err)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; } |
| 1669 | std::vector<float> e; |
| 1670 | const double t0 = now_s(); |
| 1671 | #ifdef LING32_HAVE_CUDA |
| 1672 | if (!vr.encode(im, e, err)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; } |
| 1673 | #else |
| 1674 | if (!vision_encode_cpu(W, im, e, err, vo.threads)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; } |
| 1675 | #endif |
| 1676 | double ss = 0; |
| 1677 | size_t bad = 0; |
| 1678 | for (float v : e) { ss += static_cast<double>(v) * v; bad += !std::isfinite(v); } |
| 1679 | std::printf("%s: %dx%d -> grid %dx%d, %d embeddings x %d, rms %.5f, non-finite %zu, %.0f ms\n", img.c_str(), im.w, im.h, im.gh, im.gw, im.tokens(), c.out_dim, std::sqrt(ss / std::max<size_t>(1, e.size())), bad, |
| 1680 | (now_s() - t0) * 1000.0); |
| 1681 | #ifdef LING32_HAVE_CUDA |
| 1682 | if (vr.gpu()) std::printf(" gpu memory: weights %.0f MiB resident, activations of this picture %.0f MiB (allocated for the call, freed after it)\n", vr.gpu_stats().weight_bytes / 1048576.0, vr.gpu_stats().scratch_bytes / 1048576.0); |
| 1683 | #endif |
| 1684 | if (of) of.write(reinterpret_cast<const char*>(e.data()), static_cast<std::streamsize>(e.size() * 4)); |
| 1685 | } |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | } // namespace |
| 1690 |
no test coverage detected