char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenserver: add get_info tool (#26522)99111b19c

char/aliceai-t5-moe
4.5 KiB147 linesraw
1#include "arg.h"
2#include "common.h"
3
4#include <clocale>
5#include <fstream>
6#include <sstream>
7#include <string>
8
9// Export usage message (-h) to markdown format
10// Automatically update the markdown docs
11
12#define HELP_START_MARKER "<!-- HELP_START -->"
13#define HELP_END_MARKER   "<!-- HELP_END -->"
14#define NOTE_MESSAGE      "<!-- IMPORTANT: The list below is auto-generated by llama-gen-docs; do NOT modify it manually -->"
15
16struct md_file {
17    llama_example ex;
18    std::string fname;
19    std::string specific_section_header;
20};
21
22std::vector<md_file> md_files = {
23    {LLAMA_EXAMPLE_CLI,        "tools/cli/README.md",        "CLI-specific params"},
24    {LLAMA_EXAMPLE_COMPLETION, "tools/completion/README.md", "Completion-specific params"},
25    {LLAMA_EXAMPLE_SERVER,     "tools/server/README.md",     "Server-specific params"},
26};
27
28static void write_table_header(std::ostringstream & ss) {
29    ss << "| Argument | Explanation |\n";
30    ss << "| -------- | ----------- |\n";
31}
32
33static void write_table_entry(std::ostringstream & ss, const common_arg & opt) {
34    ss << "| `";
35    // args
36    auto all_args = opt.get_args();
37    for (const auto & arg : all_args) {
38    if (arg == all_args.front()) {
39            ss << arg;
40            if (all_args.size() > 1) ss << ", ";
41        } else {
42            ss << arg << (arg != all_args.back() ? ", " : "");
43        }
44    }
45    // value hint
46    if (opt.value_hint) {
47        std::string md_value_hint(opt.value_hint);
48        string_replace_all(md_value_hint, "|", "\\|");
49        ss << " " << md_value_hint;
50    }
51    if (opt.value_hint_2) {
52        std::string md_value_hint_2(opt.value_hint_2);
53        string_replace_all(md_value_hint_2, "|", "\\|");
54        ss << " " << md_value_hint_2;
55    }
56    // help text
57    std::string md_help(opt.help);
58    md_help = string_strip(md_help);
59    string_replace_all(md_help, "\n", "<br/>");
60    string_replace_all(md_help, "|", "\\|");
61    ss << "` | " << md_help << " |\n";
62}
63
64static void write_table(std::ostringstream & ss, std::vector<common_arg *> & opts) {
65    write_table_header(ss);
66    for (const auto & opt : opts) {
67        write_table_entry(ss, *opt);
68    }
69}
70
71static void write_help(std::ostringstream & ss, const md_file & md) {
72    common_params params;
73    params.is_gen_docs = true;
74
75    auto ctx_arg = common_params_parser_init(params, md.ex);
76
77    std::vector<common_arg *> common_options;
78    std::vector<common_arg *> sampling_options;
79    std::vector<common_arg *> specific_options;
80    for (auto & opt : ctx_arg.options) {
81        // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example
82        if (opt.is_sampling) {
83            sampling_options.push_back(&opt);
84        } else if (opt.in_example(ctx_arg.ex)) {
85            specific_options.push_back(&opt);
86        } else {
87            common_options.push_back(&opt);
88        }
89    }
90
91    ss << HELP_START_MARKER << "\n\n";
92
93    ss << NOTE_MESSAGE << "\n\n";
94
95    ss << "### Common params\n\n";
96    write_table(ss, common_options);
97    ss << "\n\n### Sampling params\n\n";
98    write_table(ss, sampling_options);
99    ss << "\n\n### " << md.specific_section_header << "\n\n";
100    write_table(ss, specific_options);
101
102    ss << "\n" << HELP_END_MARKER;
103}
104
105int main(int, char **) {
106    std::setlocale(LC_NUMERIC, "C");
107
108    for (const auto & md : md_files) {
109        std::ifstream infile(md.fname);
110        if (!infile.is_open()) {
111            fprintf(stderr, "failed to open file '%s' for reading\n", md.fname.c_str());
112            return 1;
113        }
114
115        std::ostringstream ss;
116        ss << infile.rdbuf();
117        infile.close();
118
119        std::string content = ss.str();
120
121        size_t help_start = content.find(HELP_START_MARKER);
122        size_t help_end   = content.find(HELP_END_MARKER);
123
124        if (help_start == std::string::npos || help_end == std::string::npos || help_end <= help_start) {
125            fprintf(stderr, "failed to find help markers in file '%s'\n", md.fname.c_str());
126            return 1;
127        }
128
129        std::ostringstream new_help_ss;
130        write_help(new_help_ss, md);
131        std::string new_help = new_help_ss.str();
132
133        content = content.substr(0, help_start) + new_help + content.substr(help_end + strlen(HELP_END_MARKER));
134
135        std::ofstream outfile(md.fname);
136        if (!outfile.is_open()) {
137            fprintf(stderr, "failed to open file '%s' for writing\n", md.fname.c_str());
138            return 1;
139        }
140        outfile << content;
141        outfile.close();
142
143        printf("Updated help in '%s'\n", md.fname.c_str());
144    }
145
146    return 0;
147}