common,train,examples: using constexpr string and strlen for microoptimizations

This commit is contained in:
Herman Semenov 2024-08-26 02:19:18 +03:00
parent f91fc5639b
commit 66c8bb05b2
7 changed files with 16 additions and 16 deletions

View file

@ -280,12 +280,12 @@ void gpt_params_handle_model_default(gpt_params & params) {
bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) { bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
bool invalid_param = false; bool invalid_param = false;
std::string arg; std::string arg;
const std::string arg_prefix = "--"; static constexpr auto arg_prefix = "--";
llama_sampling_params & sparams = params.sparams; llama_sampling_params & sparams = params.sparams;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
arg = argv[i]; arg = argv[i];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
std::replace(arg.begin(), arg.end(), '_', '-'); std::replace(arg.begin(), arg.end(), '_', '-');
} }
if (!gpt_params_find_arg(argc, argv, arg, params, i, invalid_param)) { if (!gpt_params_find_arg(argc, argv, arg, params, i, invalid_param)) {

View file

@ -1147,8 +1147,8 @@ bool consume_common_train_arg(
) { ) {
int& i = *idx; int& i = *idx;
std::string arg = argv[i]; std::string arg = argv[i];
const std::string arg_prefix = "--"; static constexpr auto arg_prefix = "--";
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
std::replace(arg.begin(), arg.end(), '_', '-'); std::replace(arg.begin(), arg.end(), '_', '-');
} }
if (arg == "--train-data") { if (arg == "--train-data") {

View file

@ -812,11 +812,11 @@ static bool params_parse(int argc, char ** argv, struct train_params * params) {
bool reqd_param_found = false; bool reqd_param_found = false;
std::string arg; std::string arg;
struct train_params default_params = get_default_train_params(); struct train_params default_params = get_default_train_params();
const std::string arg_prefix = "--"; static constexpr auto arg_prefix = "--";
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
arg = argv[i]; arg = argv[i];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
std::replace(arg.begin(), arg.end(), '_', '-'); std::replace(arg.begin(), arg.end(), '_', '-');
} }

View file

@ -349,10 +349,10 @@ static bool get_hidden_layers(llama_context * ctx, std::vector<llama_token> & to
static void export_gguf(const std::vector<struct ggml_tensor *> & v_ctrl, const std::string fname, const std::string model_hint) { static void export_gguf(const std::vector<struct ggml_tensor *> & v_ctrl, const std::string fname, const std::string model_hint) {
struct gguf_context * ctx = gguf_init_empty(); struct gguf_context * ctx = gguf_init_empty();
const std::string arch = "controlvector"; static constexpr auto arch = "controlvector";
gguf_set_val_str(ctx, "general.architecture", arch.c_str()); gguf_set_val_str(ctx, "general.architecture", arch);
gguf_set_val_str(ctx, (arch + ".model_hint").c_str(), model_hint.c_str()); gguf_set_val_str(ctx, (std::string{arch} + ".model_hint").c_str(), model_hint.c_str());
gguf_set_val_i32(ctx, (arch + ".layer_count").c_str(), v_ctrl.size()); gguf_set_val_i32(ctx, (std::string{arch} + ".layer_count").c_str(), v_ctrl.size());
for (size_t i = 0; i < v_ctrl.size(); ++i) { for (size_t i = 0; i < v_ctrl.size(); ++i) {
gguf_add_tensor(ctx, v_ctrl[i]); gguf_add_tensor(ctx, v_ctrl[i]);

View file

@ -115,12 +115,12 @@ static void hash_print_usage(const char * executable) {
static void hash_params_parse_ex(int argc, const char ** argv, hash_params & params) { static void hash_params_parse_ex(int argc, const char ** argv, hash_params & params) {
std::string arg; std::string arg;
bool invalid_param = false; bool invalid_param = false;
const std::string arg_prefix = "--"; static constexpr auto arg_prefix = "--";
int arg_idx = 1; int arg_idx = 1;
for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) { for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
arg = argv[arg_idx]; arg = argv[arg_idx];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
std::replace(arg.begin(), arg.end(), '_', '-'); std::replace(arg.begin(), arg.end(), '_', '-');
} }

View file

@ -76,13 +76,13 @@ static size_t split_str_to_n_bytes(std::string str) {
static void split_params_parse_ex(int argc, const char ** argv, split_params & params) { static void split_params_parse_ex(int argc, const char ** argv, split_params & params) {
std::string arg; std::string arg;
const std::string arg_prefix = "--"; static constexpr auto arg_prefix = "--";
bool invalid_param = false; bool invalid_param = false;
int arg_idx = 1; int arg_idx = 1;
for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) { for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) {
arg = argv[arg_idx]; arg = argv[arg_idx];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
std::replace(arg.begin(), arg.end(), '_', '-'); std::replace(arg.begin(), arg.end(), '_', '-');
} }

View file

@ -330,7 +330,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
cmd_params params; cmd_params params;
std::string arg; std::string arg;
bool invalid_param = false; bool invalid_param = false;
const std::string arg_prefix = "--"; static constexpr auto arg_prefix = "--";
const char split_delim = ','; const char split_delim = ',';
params.verbose = cmd_params_defaults.verbose; params.verbose = cmd_params_defaults.verbose;
@ -341,7 +341,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
arg = argv[i]; arg = argv[i];
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
std::replace(arg.begin(), arg.end(), '_', '-'); std::replace(arg.begin(), arg.end(), '_', '-');
} }