optimize more

This commit is contained in:
Xuan Son Nguyen 2024-09-07 18:41:42 +02:00
parent eb7d8f85a2
commit e625f5fd1e
3 changed files with 40 additions and 35 deletions

View file

@ -373,7 +373,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params, std::vecto
} }
} catch (std::exception & e) { } catch (std::exception & e) {
throw std::invalid_argument(format( throw std::invalid_argument(format(
"error while handling environment variable \"%s\": %s\n\n", opt.env.c_str(), e.what())); "error while handling environment variable \"%s\": %s\n\n", opt.env, e.what()));
} }
} }
} }
@ -395,7 +395,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params, std::vecto
} }
auto opt = *arg_to_options[arg]; auto opt = *arg_to_options[arg];
if (opt.has_value_from_env()) { if (opt.has_value_from_env()) {
fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env.c_str(), arg.c_str()); fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env, arg.c_str());
} }
try { try {
if (opt.handler_void) { if (opt.handler_void) {
@ -595,15 +595,19 @@ std::string llama_arg::to_string() {
std::string leading_spaces(n_leading_spaces, ' '); std::string leading_spaces(n_leading_spaces, ' ');
std::ostringstream ss; std::ostringstream ss;
for (const auto & arg : args) { for (const auto arg : args) {
if (arg == args.front()) { if (arg == args.front()) {
ss << (args.size() == 1 ? arg : format("%-7s", (arg + ",").c_str())); if (args.size() == 1) {
ss << arg;
} else {
ss << format("%-7s", arg) << ", ";
}
} else { } else {
ss << arg << (arg != args.back() ? ", " : ""); ss << arg << (arg != args.back() ? ", " : "");
} }
} }
if (!value_hint.empty()) ss << " " << value_hint; if (value_hint) ss << " " << value_hint;
if (!value_hint_2.empty()) ss << " " << value_hint_2; if (value_hint_2) ss << " " << value_hint_2;
if (ss.tellp() > n_leading_spaces - 3) { if (ss.tellp() > n_leading_spaces - 3) {
// current line is too long, add new line // current line is too long, add new line
ss << "\n" << leading_spaces; ss << "\n" << leading_spaces;
@ -675,7 +679,7 @@ std::vector<llama_arg> gpt_params_parser_init(gpt_params & params, llama_example
if (seen_args.find(a) == seen_args.end()) { if (seen_args.find(a) == seen_args.end()) {
seen_args.insert(a); seen_args.insert(a);
} else { } else {
throw std::runtime_error(format("found duplicated argument in source code: %s", a.c_str())); throw std::runtime_error(format("found duplicated argument in source code: %s", a));
} }
} }
options.push_back(std::move(arg)); options.push_back(std::move(arg));
@ -693,7 +697,7 @@ std::vector<llama_arg> gpt_params_parser_init(gpt_params & params, llama_example
add_opt(llama_arg( add_opt(llama_arg(
{"--version"}, {"--version"},
"show version and build info", "show version and build info",
[](gpt_params & params) { [](gpt_params &) {
fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT); fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET); fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
exit(0); exit(0);
@ -2248,32 +2252,32 @@ std::vector<llama_arg> gpt_params_parser_init(gpt_params & params, llama_example
add_opt(llama_arg( add_opt(llama_arg(
{"--log-test"}, {"--log-test"},
"Log test", "Log test",
[](gpt_params & params) { log_param_single_parse("--log-test"); } [](gpt_params &) { log_param_single_parse("--log-test"); }
)); ));
add_opt(llama_arg( add_opt(llama_arg(
{"--log-disable"}, {"--log-disable"},
"Log disable", "Log disable",
[](gpt_params & params) { log_param_single_parse("--log-disable"); } [](gpt_params &) { log_param_single_parse("--log-disable"); }
)); ));
add_opt(llama_arg( add_opt(llama_arg(
{"--log-enable"}, {"--log-enable"},
"Log enable", "Log enable",
[](gpt_params & params) { log_param_single_parse("--log-enable"); } [](gpt_params &) { log_param_single_parse("--log-enable"); }
)); ));
add_opt(llama_arg( add_opt(llama_arg(
{"--log-new"}, {"--log-new"},
"Log new", "Log new",
[](gpt_params & params) { log_param_single_parse("--log-new"); } [](gpt_params &) { log_param_single_parse("--log-new"); }
)); ));
add_opt(llama_arg( add_opt(llama_arg(
{"--log-append"}, {"--log-append"},
"Log append", "Log append",
[](gpt_params & params) { log_param_single_parse("--log-append"); } [](gpt_params &) { log_param_single_parse("--log-append"); }
)); ));
add_opt(llama_arg( add_opt(llama_arg(
{"--log-file"}, "FNAME", {"--log-file"}, "FNAME",
"Log file", "Log file",
[](gpt_params & params, const std::string & value) { log_param_pair_parse(false, "--log-file", value); } [](gpt_params &, const std::string & value) { log_param_pair_parse(false, "--log-file", value); }
)); ));
#endif // LOG_DISABLE_LOGS #endif // LOG_DISABLE_LOGS

View file

@ -305,10 +305,10 @@ struct gpt_params {
struct llama_arg { struct llama_arg {
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON}; std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
std::vector<std::string> args; std::vector<const char *> args;
std::string value_hint; // help text or example for arg value const char * value_hint = nullptr; // help text or example for arg value
std::string value_hint_2; // for second arg value const char * value_hint_2 = nullptr; // for second arg value
std::string env; const char * env = nullptr;
std::string help; std::string help;
void (*handler_void) (gpt_params & params) = nullptr; void (*handler_void) (gpt_params & params) = nullptr;
void (*handler_string) (gpt_params & params, const std::string &) = nullptr; void (*handler_string) (gpt_params & params, const std::string &) = nullptr;
@ -316,42 +316,42 @@ struct llama_arg {
void (*handler_int) (gpt_params & params, int) = nullptr; void (*handler_int) (gpt_params & params, int) = nullptr;
llama_arg( llama_arg(
const std::initializer_list<std::string> & args, const std::initializer_list<const char *> & args,
const std::string & value_hint, const char * value_hint,
const std::string & help, const std::string & help,
void (*handler)(gpt_params & params, const std::string &) void (*handler)(gpt_params & params, const std::string &)
) : args(args), value_hint(value_hint), help(help), handler_string(handler) {} ) : args(args), value_hint(value_hint), help(help), handler_string(handler) {}
llama_arg( llama_arg(
const std::initializer_list<std::string> & args, const std::initializer_list<const char *> & args,
const std::string & value_hint, const char * value_hint,
const std::string & help, const std::string & help,
void (*handler)(gpt_params & params, int) void (*handler)(gpt_params & params, int)
) : args(args), value_hint(value_hint), help(help), handler_int(handler) {} ) : args(args), value_hint(value_hint), help(help), handler_int(handler) {}
llama_arg( llama_arg(
const std::initializer_list<std::string> & args, const std::initializer_list<const char *> & args,
const std::string & help, const std::string & help,
void (*handler)(gpt_params & params) void (*handler)(gpt_params & params)
) : args(args), help(help), handler_void(handler) {} ) : args(args), help(help), handler_void(handler) {}
// support 2 values for arg // support 2 values for arg
llama_arg( llama_arg(
const std::initializer_list<std::string> & args, const std::initializer_list<const char *> & args,
const std::string & value_hint, const char * value_hint,
const std::string & value_hint_2, const char * value_hint_2,
const std::string & help, const std::string & help,
void (*handler)(gpt_params & params, const std::string &, const std::string &) void (*handler)(gpt_params & params, const std::string &, const std::string &)
) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {} ) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
llama_arg & set_examples(std::set<enum llama_example> examples) { llama_arg & set_examples(std::initializer_list<enum llama_example> examples) {
this->examples = std::move(examples); this->examples = std::move(examples);
return *this; return *this;
} }
llama_arg & set_env(std::string env) { llama_arg & set_env(const char * env) {
help = help + "\n(env: " + env + ")"; help = help + "\n(env: " + env + ")";
this->env = std::move(env); this->env = env;
return *this; return *this;
} }
@ -360,8 +360,8 @@ struct llama_arg {
} }
bool get_value_from_env(std::string & output) const { bool get_value_from_env(std::string & output) const {
if (env.empty()) return false; if (env == nullptr) return false;
char * value = std::getenv(env.c_str()); char * value = std::getenv(env);
if (value) { if (value) {
output = value; output = value;
return true; return true;
@ -370,7 +370,7 @@ struct llama_arg {
} }
bool has_value_from_env() const { bool has_value_from_env() const {
return std::getenv(env.c_str()); return env != nullptr && std::getenv(env);
} }
std::string to_string(); std::string to_string();

View file

@ -22,18 +22,19 @@ static void export_md(std::string fname, llama_example ex) {
// args // args
for (const auto & arg : opt.args) { for (const auto & arg : opt.args) {
if (arg == opt.args.front()) { if (arg == opt.args.front()) {
file << (opt.args.size() == 1 ? arg : (arg + ", ")); file << arg;
if (opt.args.size() > 1) file << ", ";
} else { } else {
file << arg << (arg != opt.args.back() ? ", " : ""); file << arg << (arg != opt.args.back() ? ", " : "");
} }
} }
// value hint // value hint
if (!opt.value_hint.empty()) { if (opt.value_hint) {
std::string md_value_hint(opt.value_hint); std::string md_value_hint(opt.value_hint);
string_replace_all(md_value_hint, "|", "\\|"); string_replace_all(md_value_hint, "|", "\\|");
file << " " << md_value_hint; file << " " << md_value_hint;
} }
if (!opt.value_hint_2.empty()) { if (opt.value_hint_2) {
std::string md_value_hint_2(opt.value_hint_2); std::string md_value_hint_2(opt.value_hint_2);
string_replace_all(md_value_hint_2, "|", "\\|"); string_replace_all(md_value_hint_2, "|", "\\|");
file << " " << md_value_hint_2; file << " " << md_value_hint_2;