MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / common_init_sampler_from_model

Function common_init_sampler_from_model

common/common.cpp:1069–1127  ·  view source on GitHub ↗

TODO: move to common/sampling

Source from the content-addressed store, hash-verified

1067
1068// TODO: move to common/sampling
1069static void common_init_sampler_from_model(
1070 const llama_model * model,
1071 common_params_sampling & sparams) {
1072
1073 const uint64_t config = sparams.user_sampling_config;
1074
1075 auto get_int32 = [&](const char * key, int32_t & dst, uint64_t user_config) {
1076 if (config & user_config) {
1077 return;
1078 }
1079
1080 char buf[64] = {0};
1081 if (llama_model_meta_val_str(model, key, buf, sizeof(buf)) > 0) {
1082 char * end = nullptr;
1083 int32_t v = strtol(buf, &end, 10);
1084 if (end && end != buf) {
1085 dst = v;
1086 }
1087 }
1088 };
1089
1090 auto get_float = [&](const char * key, float & dst, uint64_t user_config) {
1091 if (config & user_config) {
1092 return;
1093 }
1094
1095 char buf[128] = {0};
1096 if (llama_model_meta_val_str(model, key, buf, sizeof(buf)) > 0) {
1097 char * end = nullptr;
1098 float v = strtof(buf, &end);
1099 if (end && end != buf) {
1100 dst = v;
1101 }
1102 }
1103 };
1104
1105 // Sampling sequence
1106 if (!(config & common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_SAMPLERS)) {
1107 char buf[512] = {0};
1108 if (llama_model_meta_val_str(model, llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_SEQUENCE), buf, sizeof(buf)) > 0) {
1109 const std::vector<std::string> sampler_names = string_split<std::string>(std::string(buf), ';');
1110 if (!sampler_names.empty()) {
1111 sparams.samplers = common_sampler_types_from_names(sampler_names, true);
1112 }
1113 }
1114 }
1115
1116 get_int32(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_TOP_K), sparams.top_k, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_TOP_K);
1117 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_TOP_P), sparams.top_p, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_TOP_P);
1118 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_MIN_P), sparams.min_p, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIN_P);
1119 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_XTC_PROBABILITY), sparams.xtc_probability, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_XTC_PROBABILITY);
1120 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_XTC_THRESHOLD), sparams.xtc_threshold, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_XTC_THRESHOLD);
1121 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_TEMP), sparams.temp, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_TEMP);
1122 get_int32(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_PENALTY_LAST_N), sparams.penalty_last_n, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_PENALTY_LAST_N);
1123 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_PENALTY_REPEAT), sparams.penalty_repeat, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_PENALTY_REPEAT);
1124 get_int32(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_MIROSTAT), sparams.mirostat, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT);
1125 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_MIROSTAT_TAU), sparams.mirostat_tau, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT_TAU);
1126 get_float(llama_model_meta_key_str(LLAMA_MODEL_META_KEY_SAMPLING_MIROSTAT_ETA), sparams.mirostat_eta, common_params_sampling_config::COMMON_PARAMS_SAMPLING_CONFIG_MIROSTAT_ETA);

Callers 1

common_init_resultMethod · 0.85

Calls 5

llama_model_meta_val_strFunction · 0.85
llama_model_meta_key_strFunction · 0.85
stringClass · 0.85
emptyMethod · 0.45

Tested by

no test coverage detected