Team Ai
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes479downloads
gen-docs.cpp146 linesDownload Raw Back to gen-docs
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 format10// Automatically update the markdown docs11 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    // args36    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 hint46    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 text57    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    auto ctx_arg = common_params_parser_init(params, md.ex);74 75    std::vector<common_arg *> common_options;76    std::vector<common_arg *> sparam_options;77    std::vector<common_arg *> specific_options;78    for (auto & opt : ctx_arg.options) {79        // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example80        if (opt.is_sparam) {81            sparam_options.push_back(&opt);82        } else if (opt.in_example(ctx_arg.ex)) {83            specific_options.push_back(&opt);84        } else {85            common_options.push_back(&opt);86        }87    }88 89    ss << HELP_START_MARKER << "\n\n";90 91    ss << NOTE_MESSAGE << "\n\n";92 93    ss << "### Common params\n\n";94    write_table(ss, common_options);95    ss << "\n\n### Sampling params\n\n";96    write_table(ss, sparam_options);97    ss << "\n\n### " << md.specific_section_header << "\n\n";98    write_table(ss, specific_options);99 100    ss << "\n" << HELP_END_MARKER;101}102 103int main(int, char **) {104    std::setlocale(LC_NUMERIC, "C");105 106    for (const auto & md : md_files) {107        std::ifstream infile(md.fname);108        if (!infile.is_open()) {109            fprintf(stderr, "failed to open file '%s' for reading\n", md.fname.c_str());110            return 1;111        }112 113        std::ostringstream ss;114        ss << infile.rdbuf();115        infile.close();116 117        std::string content = ss.str();118 119        size_t help_start = content.find(HELP_START_MARKER);120        size_t help_end   = content.find(HELP_END_MARKER);121 122        if (help_start == std::string::npos || help_end == std::string::npos || help_end <= help_start) {123            fprintf(stderr, "failed to find help markers in file '%s'\n", md.fname.c_str());124            return 1;125        }126 127        std::ostringstream new_help_ss;128        write_help(new_help_ss, md);129        std::string new_help = new_help_ss.str();130 131        content = content.substr(0, help_start) + new_help + content.substr(help_end + strlen(HELP_END_MARKER));132 133        std::ofstream outfile(md.fname);134        if (!outfile.is_open()) {135            fprintf(stderr, "failed to open file '%s' for writing\n", md.fname.c_str());136            return 1;137        }138        outfile << content;139        outfile.close();140 141        printf("Updated help in '%s'\n", md.fname.c_str());142    }143 144    return 0;145}146