hkimi / src /cli /main.cpp
Ozomozyan's picture
hkimi 0.1.0
68888be verified
Raw History Blame Contribute Delete
3.98 kB
#include <cstdio>
#include <cstring>
#include <exception>
#include <string>
#include <vector>
#include "cli/args.h"
#include "cli/commands.h"
#include "common/base.h"
#include "common/console.h"
namespace {
struct Command {
const char* name;
int (*fn)(const hk::Args&);
const char* usage;
const char* help;
};
const Command kCommands[] = {
{"bench-io", hk::cmd_bench_io, "bench-io <file|dir> [--size-gb 4] [--block-mb 16] [--depth 16] [--seconds 10] [--engine auto]",
"Measure direct-read speed of a disk the way the engine reads it."},
{"inspect", hk::cmd_inspect, "inspect <checkpoint|store> [--layers 5,6 | --all]",
"Check a checkpoint's tensors against what the engine expects; list which shards hold which layers."},
{"convert", hk::cmd_convert, "convert <checkpoint> <store> [--layers 5,6] [--requantize] [--delete-source] [--strict]",
"Build a store: trunk as shipped, experts bit plane by bit plane."},
{"sweep", hk::cmd_sweep, "sweep <store> --tokens 1,2,3 [--split p,q [--overshoot k]] [--dump-logits f] [--dump-routing f] [--dump-states f]",
"Run real Kimi over a token sequence (debugging, tests)."},
{"run", hk::cmd_run, "run <store> (--prompt TEXT | --tokens 1,2,3) [--drafter DIR] [--max-new 256] [--think] [--json]",
"Answer one prompt. With --drafter: the drafter guesses, real Kimi checks, every token is real Kimi's own.\n"
" Without it real Kimi writes alone, one sweep per token."},
{"chat", hk::cmd_chat, "chat <store> --drafter DIR [--show-draft]", "Interactive chat, with the drafter."},
{"tokenize", hk::cmd_tokenize, "tokenize <store|tokenizer-dir> (--text TEXT | --file F | --ids 1,2,3) [--chat]",
"Encode or decode text."},
{"layer-test", hk::cmd_layer_test, "layer-test <store> [--layers 5,6] [--rows 48] [--backend cuda] [--dump DIR]",
"Run single layers alone: backend against the CPU reference, timing."},
{"selftest", hk::cmd_selftest, "selftest [--backend cuda] [--quick]",
"Check every backend operation against the CPU reference on random data."},
};
void print_help() {
printf("hkimi %s: run Kimi K3.1 Pro on a home PC. A small drafter guesses, the real model checks every token.\n\n", HK_VERSION);
printf("usage: hkimi <command> [arguments]\n\n");
for (const Command& c : kCommands) printf(" %-13s %s\n %s\n", c.name, c.help, c.usage);
printf("\ncommon options: --backend auto|cpu|cuda --engine auto|uring|iocp|threads --verbose\n");
printf("docs: README.md, docs/architecture.md\n");
}
} // namespace
int main(int raw_argc, char** raw_argv) {
// UTF-8 arguments on every OS
hk::console_init();
std::vector<std::string> args = hk::utf8_args(raw_argc, raw_argv);
std::vector<char*> ptrs;
for (std::string& s : args) ptrs.push_back(&s[0]);
ptrs.push_back(nullptr);
const int argc = (int)args.size();
char** argv = ptrs.data();
if (argc < 2 || !strcmp(argv[1], "help") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
print_help();
return argc < 2 ? 1 : 0;
}
if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "version")) {
printf("hkimi %s\n", HK_VERSION);
return 0;
}
for (const Command& c : kCommands) {
if (strcmp(argv[1], c.name) != 0) continue;
try {
hk::Args a = hk::Args::parse(argc, argv, 2);
if (a.flag("verbose")) hk::set_log_level(hk::LogLevel::Debug);
a.kv.erase("verbose");
if (a.flag("help")) {
printf("usage: hkimi %s\n%s\n", c.usage, c.help);
return 0;
}
return c.fn(a);
} catch (const std::exception& e) {
fflush(stdout);
fprintf(stderr, "hkimi %s: %s\n", c.name, e.what());
return 1;
}
}
fprintf(stderr, "hkimi: unknown command \"%s\"\n\n", argv[1]);
print_help();
return 1;
}