Default typed compress: delegates to handle_compress via temp file + DaemonIO collector.
| 63 | |
| 64 | // Default typed compress: delegates to handle_compress via temp file + DaemonIO collector. |
| 65 | ModelBackend::CompressResult ModelBackend::compress(const CompressRequest & req) { |
| 66 | CompressResult result; |
| 67 | |
| 68 | if (req.input_ids.empty()) return result; |
| 69 | |
| 70 | // Write input IDs to temp file (handle_compress reads from file) |
| 71 | const size_t to_write = req.input_ids.size() * sizeof(int32_t); |
| 72 | std::string tmp_path; |
| 73 | #if defined(_WIN32) |
| 74 | { |
| 75 | static std::atomic<unsigned long long> ctr{0}; |
| 76 | const auto uniq = |
| 77 | std::to_string((unsigned long long) |
| 78 | std::chrono::steady_clock::now().time_since_epoch().count()) + |
| 79 | "_" + std::to_string(ctr++); |
| 80 | std::filesystem::path p = |
| 81 | std::filesystem::temp_directory_path() / ("pflash_" + uniq + ".bin"); |
| 82 | tmp_path = p.string(); |
| 83 | FILE * f = std::fopen(tmp_path.c_str(), "wb"); |
| 84 | if (!f) return result; |
| 85 | const size_t w = std::fwrite(req.input_ids.data(), 1, to_write, f); |
| 86 | std::fclose(f); |
| 87 | if (w != to_write) { std::remove(tmp_path.c_str()); return result; } |
| 88 | } |
| 89 | #else |
| 90 | { |
| 91 | char tmpl[] = "/tmp/pflash_XXXXXX.bin"; |
| 92 | int tmp_fd = mkstemps(tmpl, 4); |
| 93 | if (tmp_fd < 0) return result; |
| 94 | tmp_path = tmpl; |
| 95 | const char *src = reinterpret_cast<const char *>(req.input_ids.data()); |
| 96 | size_t remaining = to_write; |
| 97 | while (remaining > 0) { |
| 98 | ssize_t n = ::write(tmp_fd, src, remaining); |
| 99 | if (n <= 0) { |
| 100 | ::close(tmp_fd); |
| 101 | ::unlink(tmp_path.c_str()); |
| 102 | return result; |
| 103 | } |
| 104 | src += n; |
| 105 | remaining -= (size_t)n; |
| 106 | } |
| 107 | ::close(tmp_fd); |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | // Build collecting DaemonIO |
| 112 | DaemonIO io; |
| 113 | io.stream_fd = -1; |
| 114 | io.on_token = [&](int32_t tok) -> bool { |
| 115 | result.compressed_ids.push_back(tok); |
| 116 | return true; |
| 117 | }; |
| 118 | |
| 119 | // Build command string for legacy handle_compress |
| 120 | int keep_x1000 = (int)(req.keep_ratio * 1000.0f); |
| 121 | std::string cmd = std::string("compress ") + tmp_path + " " |
| 122 | + std::to_string(keep_x1000) + " " + req.drafter_path; |