Commit 9fcc40d3d for llama.cpp
commit 9fcc40d3d4c42d8187e567243f840899985929e0
Author: Evan Huus <eapache@gmail.com>
Date: Sun Nov 29 19:31:06 2026 -0500
common : share the kv arg parser with llama-bench (#32831)
Assisted-by: Codex
diff --git a/common/arg.cpp b/common/arg.cpp
index 80d2037eb..7b9db4263 100644
--- a/common/arg.cpp
+++ b/common/arg.cpp
@@ -291,13 +291,21 @@ const std::vector<ggml_type> kv_cache_types = {
GGML_TYPE_Q5_1,
};
-static ggml_type kv_cache_type_from_str(const std::string & s) {
+ggml_type common_parse_kv_cache_type(const std::string & s) {
for (const auto & type : kv_cache_types) {
if (ggml_type_name(type) == s) {
return type;
}
}
- throw std::runtime_error("Unsupported cache type: " + s);
+ return GGML_TYPE_COUNT;
+}
+
+static ggml_type kv_cache_type_from_str(const std::string & s) {
+ const ggml_type type = common_parse_kv_cache_type(s);
+ if (type == GGML_TYPE_COUNT) {
+ throw std::runtime_error("Unsupported cache type: " + s);
+ }
+ return type;
}
static std::string get_all_kv_cache_types() {
diff --git a/common/arg.h b/common/arg.h
index a7cf30be4..21c092449 100644
--- a/common/arg.h
+++ b/common/arg.h
@@ -19,6 +19,8 @@
// CLI argument parsing
//
+ggml_type common_parse_kv_cache_type(const std::string & value);
+
struct common_arg {
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
std::set<enum llama_example> excludes = {};
diff --git a/tools/llama-bench/llama-bench.cpp b/tools/llama-bench/llama-bench.cpp
index 1ddd73ce8..981599de0 100644
--- a/tools/llama-bench/llama-bench.cpp
+++ b/tools/llama-bench/llama-bench.cpp
@@ -493,38 +493,6 @@ static void print_usage(int /* argc */, char ** argv) {
"'first-last' or 'first-last+step' or 'first-last*mult'.\n");
}
-static ggml_type ggml_type_from_name(const std::string & s) {
- if (s == "f16") {
- return GGML_TYPE_F16;
- }
- if (s == "bf16") {
- return GGML_TYPE_BF16;
- }
- if (s == "f8_e4m3") {
- return GGML_TYPE_F8_E4M3;
- }
- if (s == "q8_0") {
- return GGML_TYPE_Q8_0;
- }
- if (s == "q4_0") {
- return GGML_TYPE_Q4_0;
- }
- if (s == "q4_1") {
- return GGML_TYPE_Q4_1;
- }
- if (s == "q5_0") {
- return GGML_TYPE_Q5_0;
- }
- if (s == "q5_1") {
- return GGML_TYPE_Q5_1;
- }
- if (s == "iq4_nl") {
- return GGML_TYPE_IQ4_NL;
- }
-
- return GGML_TYPE_COUNT;
-}
-
static cmd_params parse_cmd_params(int argc, char ** argv) {
cmd_params params;
std::string arg;
@@ -644,7 +612,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
std::vector<ggml_type> types;
for (const auto & t : p) {
- ggml_type gt = ggml_type_from_name(t);
+ ggml_type gt = common_parse_kv_cache_type(t);
if (gt == GGML_TYPE_COUNT) {
invalid_param = true;
break;
@@ -664,7 +632,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
std::vector<ggml_type> types;
for (const auto & t : p) {
- ggml_type gt = ggml_type_from_name(t);
+ ggml_type gt = common_parse_kv_cache_type(t);
if (gt == GGML_TYPE_COUNT) {
invalid_param = true;
break;