char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
99111b19c
char/aliceai-t5-moe
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 { 17llama_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 ) { 29ss <<"| Argument | Explanation |\n" ; 30ss <<"| -------- | ----------- |\n" ; 31} 32 33static void write_table_entry (std::ostringstream & ss ,const common_arg & opt ) { 34ss <<"| `" ; 35// args 36auto all_args = opt .get_args (); 37for (const auto & arg :all_args ) { 38if (arg == all_args .front ()) { 39ss <<arg ; 40if (all_args .size ()> 1 )ss <<", " ; 41 }else { 42ss <<arg << (arg != all_args .back () ?", " :"" ); 43 } 44 } 45// value hint 46if (opt .value_hint ) { 47 std::string md_value_hint (opt .value_hint ); 48string_replace_all (md_value_hint ,"|" ,"\\|" ); 49ss <<" " <<md_value_hint ; 50 } 51if (opt .value_hint_2 ) { 52 std::string md_value_hint_2 (opt .value_hint_2 ); 53string_replace_all (md_value_hint_2 ,"|" ,"\\|" ); 54ss <<" " <<md_value_hint_2 ; 55 } 56// help text 57 std::string md_help (opt .help ); 58md_help = string_strip (md_help ); 59string_replace_all (md_help ,"\n" ,"<br/>" ); 60string_replace_all (md_help ,"|" ,"\\|" ); 61ss <<"` | " <<md_help <<" |\n" ; 62} 63 64static void write_table (std::ostringstream & ss , std::vector < common_arg *> & opts ) { 65write_table_header (ss ); 66for (const auto & opt :opts ) { 67write_table_entry (ss ,* opt ); 68 } 69} 70 71static void write_help (std::ostringstream & ss ,const md_file & md ) { 72common_params params ; 73params .is_gen_docs = true; 74 75auto 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 ; 80for (auto & opt :ctx_arg .options ) { 81// in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example 82if (opt .is_sampling ) { 83sampling_options .push_back (& opt ); 84 }else if (opt .in_example (ctx_arg .ex )) { 85specific_options .push_back (& opt ); 86 }else { 87common_options .push_back (& opt ); 88 } 89 } 90 91ss <<HELP_START_MARKER <<"\n\n" ; 92 93ss <<NOTE_MESSAGE <<"\n\n" ; 94 95ss <<"### Common params\n\n" ; 96write_table (ss ,common_options ); 97ss <<"\n\n### Sampling params\n\n" ; 98write_table (ss ,sampling_options ); 99ss <<"\n\n### " <<md .specific_section_header <<"\n\n" ; 100write_table (ss ,specific_options ); 101 102ss <<"\n" <<HELP_END_MARKER ; 103} 104 105int main (int ,char ** ) { 106 std::setlocale (LC_NUMERIC ,"C" ); 107 108for (const auto & md :md_files ) { 109 std::ifstream infile (md .fname ); 110if (!infile .is_open ()) { 111fprintf (stderr ,"failed to open file '%s' for reading\n" ,md .fname .c_str ()); 112return 1 ; 113 } 114 115 std::ostringstream ss ; 116ss <<infile .rdbuf (); 117infile .close (); 118 119 std::string content = ss .str (); 120 121size_t help_start = content .find (HELP_START_MARKER ); 122size_t help_end = content .find (HELP_END_MARKER ); 123 124if (help_start == std::string::npos || help_end == std::string::npos || help_end <=help_start ) { 125fprintf (stderr ,"failed to find help markers in file '%s'\n" ,md .fname .c_str ()); 126return 1 ; 127 } 128 129 std::ostringstream new_help_ss ; 130write_help (new_help_ss ,md ); 131 std::string new_help = new_help_ss .str (); 132 133content = content .substr (0 ,help_start )+ new_help + content .substr (help_end + strlen (HELP_END_MARKER )); 134 135 std::ofstream outfile (md .fname ); 136if (!outfile .is_open ()) { 137fprintf (stderr ,"failed to open file '%s' for writing\n" ,md .fname .c_str ()); 138return 1 ; 139 } 140outfile <<content ; 141outfile .close (); 142 143printf ("Updated help in '%s'\n" ,md .fname .c_str ()); 144 } 145 146return 0 ; 147}