File: gen-docs.cpp

package info (click to toggle)
llama.cpp 7593%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 71,012 kB
  • sloc: cpp: 329,391; ansic: 48,249; python: 32,103; lisp: 10,053; sh: 6,070; objc: 1,349; javascript: 924; xml: 384; makefile: 233
file content (142 lines) | stat: -rw-r--r-- 4,522 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "arg.h"
#include "common.h"

#include <fstream>
#include <sstream>
#include <string>

// Export usage message (-h) to markdown format
// Automatically update the markdown docs

#define HELP_START_MARKER "<!-- HELP_START -->"
#define HELP_END_MARKER   "<!-- HELP_END -->"
#define NOTE_MESSAGE      "<!-- IMPORTANT: The list below is auto-generated by llama-gen-docs; do NOT modify it manually -->"

struct md_file {
    llama_example ex;
    std::string fname;
    std::string specific_section_header;
};

std::vector<md_file> md_files = {
    {LLAMA_EXAMPLE_CLI,        "tools/cli/README.md",        "CLI-specific params"},
    {LLAMA_EXAMPLE_COMPLETION, "tools/completion/README.md", "Completion-specific params"},
    {LLAMA_EXAMPLE_SERVER,     "tools/server/README.md",     "Server-specific params"},
};

static void write_table_header(std::ostringstream & ss) {
    ss << "| Argument | Explanation |\n";
    ss << "| -------- | ----------- |\n";
}

static void write_table_entry(std::ostringstream & ss, const common_arg & opt) {
    ss << "| `";
    // args
    auto all_args = opt.get_args();
    for (const auto & arg : all_args) {
    if (arg == all_args.front()) {
            ss << arg;
            if (all_args.size() > 1) ss << ", ";
        } else {
            ss << arg << (arg != all_args.back() ? ", " : "");
        }
    }
    // value hint
    if (opt.value_hint) {
        std::string md_value_hint(opt.value_hint);
        string_replace_all(md_value_hint, "|", "\\|");
        ss << " " << md_value_hint;
    }
    if (opt.value_hint_2) {
        std::string md_value_hint_2(opt.value_hint_2);
        string_replace_all(md_value_hint_2, "|", "\\|");
        ss << " " << md_value_hint_2;
    }
    // help text
    std::string md_help(opt.help);
    md_help = string_strip(md_help);
    string_replace_all(md_help, "\n", "<br/>");
    string_replace_all(md_help, "|", "\\|");
    ss << "` | " << md_help << " |\n";
}

static void write_table(std::ostringstream & ss, std::vector<common_arg *> & opts) {
    write_table_header(ss);
    for (const auto & opt : opts) {
        write_table_entry(ss, *opt);
    }
}

static void write_help(std::ostringstream & ss, const md_file & md) {
    common_params params;
    auto ctx_arg = common_params_parser_init(params, md.ex);

    std::vector<common_arg *> common_options;
    std::vector<common_arg *> sparam_options;
    std::vector<common_arg *> specific_options;
    for (auto & opt : ctx_arg.options) {
        // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
        if (opt.is_sparam) {
            sparam_options.push_back(&opt);
        } else if (opt.in_example(ctx_arg.ex)) {
            specific_options.push_back(&opt);
        } else {
            common_options.push_back(&opt);
        }
    }

    ss << HELP_START_MARKER << "\n\n";

    ss << NOTE_MESSAGE << "\n\n";

    ss << "### Common params\n\n";
    write_table(ss, common_options);
    ss << "\n\n### Sampling params\n\n";
    write_table(ss, sparam_options);
    ss << "\n\n### " << md.specific_section_header << "\n\n";
    write_table(ss, specific_options);

    ss << "\n" << HELP_END_MARKER;
}

int main(int, char **) {
    for (const auto & md : md_files) {
        std::ifstream infile(md.fname);
        if (!infile.is_open()) {
            fprintf(stderr, "failed to open file '%s' for reading\n", md.fname.c_str());
            return 1;
        }

        std::ostringstream ss;
        ss << infile.rdbuf();
        infile.close();

        std::string content = ss.str();

        size_t help_start = content.find(HELP_START_MARKER);
        size_t help_end   = content.find(HELP_END_MARKER);

        if (help_start == std::string::npos || help_end == std::string::npos || help_end <= help_start) {
            fprintf(stderr, "failed to find help markers in file '%s'\n", md.fname.c_str());
            return 1;
        }

        std::ostringstream new_help_ss;
        write_help(new_help_ss, md);
        std::string new_help = new_help_ss.str();

        content = content.substr(0, help_start) + new_help + content.substr(help_end + strlen(HELP_END_MARKER));

        std::ofstream outfile(md.fname);
        if (!outfile.is_open()) {
            fprintf(stderr, "failed to open file '%s' for writing\n", md.fname.c_str());
            return 1;
        }
        outfile << content;
        outfile.close();

        printf("Updated help in '%s'\n", md.fname.c_str());
    }

    return 0;
}