Skip to content

Commit 316f837

Browse files
compiladecebtenzzre
authored andcommitted
llama : fix segfault from unknown model arch name (ggml-org#5820)
* llama : fix segfault from unknown model arch name * 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 ggml-org#5820 (comment) * llama : remove redundant inner const for LLM_TENSOR_NAMES The extra const won't do anything here as const maps return const references to values. Co-authored-by: Jared Van Bortel <[email protected]> * llama : remove redundant nullptr check in llm_arch_from_string Since LLM_ARCH_NAMES is a const map, no spurious elements with a NULL name are inserted anymore, so this check is dead code. --------- Co-authored-by: Jared Van Bortel <[email protected]>
1 parent e789d25 commit 316f837

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

llama.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ enum llm_arch {
216216
LLM_ARCH_UNKNOWN,
217217
};
218218

219-
static std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
219+
static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
220220
{ LLM_ARCH_LLAMA, "llama" },
221221
{ LLM_ARCH_FALCON, "falcon" },
222222
{ LLM_ARCH_GPT2, "gpt2" },
@@ -241,6 +241,7 @@ static std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
241241
{ LLM_ARCH_MINICPM, "minicpm" },
242242
{ LLM_ARCH_GEMMA, "gemma" },
243243
{ LLM_ARCH_STARCODER2, "starcoder2" },
244+
{ LLM_ARCH_UNKNOWN, "(unknown)" },
244245
};
245246

246247
enum llm_kv {
@@ -301,7 +302,7 @@ enum llm_kv {
301302
LLM_KV_TOKENIZER_RWKV,
302303
};
303304

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

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

@@ -400,7 +401,7 @@ enum llm_tensor {
400401
LLM_TENSOR_LAYER_OUT_NORM,
401402
};
402403

403-
static std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES = {
404+
static const std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES = {
404405
{
405406
LLM_ARCH_LLAMA,
406407
{
@@ -833,46 +834,46 @@ struct LLM_TN {
833834
llm_arch arch;
834835

835836
std::string operator()(llm_tensor tensor) const {
836-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
837+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
837838
return "__missing__";
838839
}
839-
return LLM_TENSOR_NAMES[arch].at(tensor);
840+
return LLM_TENSOR_NAMES.at(arch).at(tensor);
840841
}
841842

842843
std::string operator()(llm_tensor tensor, const std::string & suffix) const {
843-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
844+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
844845
return "__missing__";
845846
}
846-
return LLM_TENSOR_NAMES[arch].at(tensor) + "." + suffix;
847+
return LLM_TENSOR_NAMES.at(arch).at(tensor) + "." + suffix;
847848
}
848849

849850
std::string operator()(llm_tensor tensor, int bid) const {
850-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
851+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
851852
return "__missing__";
852853
}
853-
return ::format(LLM_TENSOR_NAMES[arch].at(tensor).c_str(), bid);
854+
return ::format(LLM_TENSOR_NAMES.at(arch).at(tensor).c_str(), bid);
854855
}
855856

856857
std::string operator()(llm_tensor tensor, const std::string & suffix, int bid) const {
857-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
858+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
858859
return "__missing__";
859860
}
860-
return ::format(LLM_TENSOR_NAMES[arch].at(tensor).c_str(), bid) + "." + suffix;
861+
return ::format(LLM_TENSOR_NAMES.at(arch).at(tensor).c_str(), bid) + "." + suffix;
861862
}
862863

863864
std::string operator()(llm_tensor tensor, const std::string & suffix, int bid, int xid) const {
864-
if (LLM_TENSOR_NAMES[arch].find(tensor) == LLM_TENSOR_NAMES[arch].end()) {
865+
if (LLM_TENSOR_NAMES.at(arch).find(tensor) == LLM_TENSOR_NAMES.at(arch).end()) {
865866
return "__missing__";
866867
}
867-
return ::format(LLM_TENSOR_NAMES[arch].at(tensor).c_str(), bid, xid) + "." + suffix;
868+
return ::format(LLM_TENSOR_NAMES.at(arch).at(tensor).c_str(), bid, xid) + "." + suffix;
868869
}
869870
};
870871

871872
//
872873
// gguf helpers
873874
//
874875

875-
static std::map<int32_t, const char *> LLAMA_ROPE_SCALING_TYPES = {
876+
static const std::map<int32_t, const char *> LLAMA_ROPE_SCALING_TYPES = {
876877
{ LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
877878
{ LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
878879
{ LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },

0 commit comments

Comments
 (0)