File size: 3,977 Bytes
68888be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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;
}