Skip to content

Commit 6cf481b

Browse files
committed
llama : make all LLM maps const
This also requires using `std::map::at` instead of its `operator[]` which does not exist for const maps. * llama : name LLM_ARCH_UNKNOWN to "(unknown)" This avoids errors from `std::map::at` when getting the general name of the model architecture. Using "(unknown)" instead of an empty string as per suggestion #5820 (comment)
1 parent 3b257f8 commit 6cf481b

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

llama.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ enum llm_arch {
215215
LLM_ARCH_UNKNOWN,
216216
};
217217

218-
static std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
218+
static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
219219
{ LLM_ARCH_LLAMA, "llama" },
220220
{ LLM_ARCH_FALCON, "falcon" },
221221
{ LLM_ARCH_GPT2, "gpt2" },
@@ -240,6 +240,7 @@ static std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
240240
{ LLM_ARCH_MINICPM, "minicpm" },
241241
{ LLM_ARCH_GEMMA, "gemma" },
242242
{ LLM_ARCH_STARCODER2, "starcoder2" },
243+
{ LLM_ARCH_UNKNOWN, "(unknown)" },
243244
};
244245

245246
enum llm_kv {
@@ -300,7 +301,7 @@ enum llm_kv {
300301
LLM_KV_TOKENIZER_RWKV,
301302
};
302303

303-
static std::map<llm_kv, const char *> LLM_KV_NAMES = {
304+
static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
304305
{ LLM_KV_GENERAL_ARCHITECTURE, "general.architecture" },
305306
{ LLM_KV_GENERAL_QUANTIZATION_VERSION, "general.quantization_version" },
306307
{ LLM_KV_GENERAL_ALIGNMENT, "general.alignment" },
@@ -364,7 +365,7 @@ struct LLM_KV {
364365
llm_arch arch;
365366

366367
std::string operator()(llm_kv kv) const {
367-
return ::format(LLM_KV_NAMES[kv], LLM_ARCH_NAMES[arch]);
368+
return ::format(LLM_KV_NAMES.at(kv), LLM_ARCH_NAMES.at(arch));
368369
}
369370
};
370371

@@ -399,7 +400,7 @@ enum llm_tensor {
399400
LLM_TENSOR_LAYER_OUT_NORM,
400401
};
401402

402-
static std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES = {
403+
static const std::map<llm_arch, const std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES = {
403404
{
404405
LLM_ARCH_LLAMA,
405406
{
@@ -810,13 +811,6 @@ static std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES =
810811
static llm_arch llm_arch_from_string(const std::string & name) {
811812
for (const auto & kv : LLM_ARCH_NAMES) { // NOLINT
812813
if (kv.second == nullptr) {
813-
// LLM_ARCH_UNKNOWN does not have a name,
814-
// but is somehow still in the LLM_ARCH_NAMES map.
815-
if (kv.first == LLM_ARCH_UNKNOWN) {
816-
// skip string comparison
817-
continue;
818-
}
819-
// known architectures should always have a name
820814
GGML_ASSERT(false && "missing architecture in LLM_ARCH_NAMES");
821815
}
822816
if (kv.second == name) {
@@ -842,46 +836,46 @@ struct LLM_TN {
842836
llm_arch arch;
843837

844838
std::string operator()(llm_tensor tensor) const {
845-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
839+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
846840
return "__missing__";
847841
}
848-
return LLM_TENSOR_NAMES[arch].at(tensor);
842+
return LLM_TENSOR_NAMES.at(arch).at(tensor);
849843
}
850844

851845
std::string operator()(llm_tensor tensor, const std::string & suffix) const {
852-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
846+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
853847
return "__missing__";
854848
}
855-
return LLM_TENSOR_NAMES[arch].at(tensor) + "." + suffix;
849+
return LLM_TENSOR_NAMES.at(arch).at(tensor) + "." + suffix;
856850
}
857851

858852
std::string operator()(llm_tensor tensor, int bid) const {
859-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
853+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
860854
return "__missing__";
861855
}
862-
return ::format(LLM_TENSOR_NAMES[arch].at(tensor).c_str(), bid);
856+
return ::format(LLM_TENSOR_NAMES.at(arch).at(tensor).c_str(), bid);
863857
}
864858

865859
std::string operator()(llm_tensor tensor, const std::string & suffix, int bid) const {
866-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
860+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
867861
return "__missing__";
868862
}
869-
return ::format(LLM_TENSOR_NAMES[arch].at(tensor).c_str(), bid) + "." + suffix;
863+
return ::format(LLM_TENSOR_NAMES.at(arch).at(tensor).c_str(), bid) + "." + suffix;
870864
}
871865

872866
std::string operator()(llm_tensor tensor, const std::string & suffix, int bid, int xid) const {
873-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
867+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
874868
return "__missing__";
875869
}
876-
return ::format(LLM_TENSOR_NAMES[arch].at(tensor).c_str(), bid, xid) + "." + suffix;
870+
return ::format(LLM_TENSOR_NAMES.at(arch).at(tensor).c_str(), bid, xid) + "." + suffix;
877871
}
878872
};
879873

880874
//
881875
// gguf helpers
882876
//
883877

884-
static std::map<int32_t, const char *> LLAMA_ROPE_SCALING_TYPES = {
878+
static const std::map<int32_t, const char *> LLAMA_ROPE_SCALING_TYPES = {
885879
{ LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
886880
{ LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
887881
{ LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },

0 commit comments

Comments
 (0)