|
| 1 | +// pflash_drafter_ipc_daemon.cpp - PFlash drafter IPC daemon body. |
| 2 | + |
| 3 | +#include "pflash_drafter_ipc.h" |
| 4 | + |
| 5 | +#include "dflash27b.h" |
| 6 | +#include "dflash_draft_ipc.h" |
| 7 | +#include "qwen3/qwen3_drafter.h" |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <cstdio> |
| 11 | +#include <iostream> |
| 12 | +#include <sstream> |
| 13 | + |
| 14 | +namespace dflash::common { |
| 15 | + |
| 16 | +int run_pflash_drafter_ipc_daemon(const char * drafter_path, |
| 17 | + int drafter_gpu, |
| 18 | + int stream_fd) { |
| 19 | +#if defined(_WIN32) |
| 20 | + (void)drafter_path; (void)drafter_gpu; (void)stream_fd; |
| 21 | + std::fprintf(stderr, "PFlash drafter IPC daemon is only implemented on POSIX hosts\n"); |
| 22 | + return 2; |
| 23 | +#else |
| 24 | + if (!drafter_path || stream_fd < 0) { |
| 25 | + std::fprintf(stderr, |
| 26 | + "usage: backend_ipc_daemon --backend-ipc-mode=pflash-compress <drafter.gguf> " |
| 27 | + "--stream-fd=FD [--draft-gpu=N]\n"); |
| 28 | + return 2; |
| 29 | + } |
| 30 | + |
| 31 | + DrafterContext ctx; |
| 32 | + if (!load_drafter(drafter_path, /*gpu_layers=*/999, std::max(0, drafter_gpu), ctx)) { |
| 33 | + std::fprintf(stderr, "[pflash-ipc-daemon] drafter load failed: %s\n", |
| 34 | + dflash27b_last_error()); |
| 35 | + stream_status(stream_fd, -1); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + std::fprintf(stderr, "[pflash-ipc-daemon] ready gpu=%d\n", std::max(0, drafter_gpu)); |
| 40 | + stream_status(stream_fd, 0); |
| 41 | + |
| 42 | + std::string line; |
| 43 | + while (std::getline(std::cin, line)) { |
| 44 | + std::istringstream iss(line); |
| 45 | + std::string cmd; |
| 46 | + iss >> cmd; |
| 47 | + if (cmd == "quit" || cmd == "exit") break; |
| 48 | + if (cmd == "compress") { |
| 49 | + int keep_x1000 = 0; |
| 50 | + iss >> keep_x1000; |
| 51 | + std::string path = read_line_tail(iss); |
| 52 | + if (keep_x1000 < 0 || keep_x1000 > 1000 || path.empty()) { |
| 53 | + std::fprintf(stderr, "[pflash-ipc-daemon] bad compress: %s\n", |
| 54 | + line.c_str()); |
| 55 | + stream_status(stream_fd, -1); |
| 56 | + continue; |
| 57 | + } |
| 58 | + auto input_ids = read_int32_file(path); |
| 59 | + if (input_ids.empty()) { |
| 60 | + std::fprintf(stderr, "[pflash-ipc-daemon] read tokens failed: %s\n", |
| 61 | + path.c_str()); |
| 62 | + stream_status(stream_fd, -1); |
| 63 | + continue; |
| 64 | + } |
| 65 | + const float keep = (float)keep_x1000 / 1000.0f; |
| 66 | + auto compressed = drafter_score_and_compress(ctx, input_ids, keep); |
| 67 | + if (compressed.empty()) { |
| 68 | + std::fprintf(stderr, "[pflash-ipc-daemon] compress returned empty\n"); |
| 69 | + stream_status(stream_fd, -1); |
| 70 | + continue; |
| 71 | + } |
| 72 | + const int32_t n_out = (int32_t)compressed.size(); |
| 73 | + if (!stream_status(stream_fd, 0) || |
| 74 | + !write_exact_fd(stream_fd, &n_out, sizeof(n_out)) || |
| 75 | + !write_exact_fd(stream_fd, compressed.data(), |
| 76 | + compressed.size() * sizeof(int32_t))) { |
| 77 | + std::fprintf(stderr, "[pflash-ipc-daemon] stream write failed\n"); |
| 78 | + break; |
| 79 | + } |
| 80 | + continue; |
| 81 | + } |
| 82 | + std::fprintf(stderr, "[pflash-ipc-daemon] unknown command: %s\n", line.c_str()); |
| 83 | + stream_status(stream_fd, -1); |
| 84 | + } |
| 85 | + |
| 86 | + free_drafter(ctx); |
| 87 | + std::fprintf(stderr, "[pflash-ipc-daemon] stopped\n"); |
| 88 | + return 0; |
| 89 | +#endif |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace dflash::common |
0 commit comments