in packages/gguf/scripts/generate-llm.ts [149:218]
tsName: snakeToPascal(matched.groups.cppConst.replace("LLM_", "")),
tensorNames: [],
hparams: [],
});
}
}
/////////////////////////////////////
// extract map constant name to kv name
// for example: LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ==> "%s.attention.layer_norm_rms_epsilon"
const constToKVName: { [cppConst: string]: string } = {};
const matchedKVList = cppSource.match(/LLM_KV_NAMES = (?<names>[^;]+)/)?.groups?.names.split("\n");
if (!matchedKVList?.length) {
throw new Error("LLM_KV_NAMES is empty");
}
for (const line of matchedKVList) {
const matched = line.match(/(?<cppConst>LLM_KV_[A-Z0-9_]+)[,\s]+"(?<name>.+?)"/);
if (matched?.groups) {
constToKVName[matched.groups.cppConst] = matched.groups.name;
}
}
console.log("constToKVName", constToKVName);
/////////////////////////////////////
// extract list of tensor names based on architecture
// TODO: unused for now
const matchedTensorList = cppSource.match(/LLM_TENSOR_NAMES = (?<names>[^;]+)/)?.groups?.names.split("\n");
if (!matchedTensorList?.length) {
throw new Error("LLM_TENSOR_NAMES is empty");
}
let currCppConst = "";
for (const line of matchedTensorList) {
// check if current line has LLM_ARCH_*
const cppConst = line.match(RE_ARCH_NAME)?.[0];
if (cppConst) {
currCppConst = cppConst;
continue;
}
// check if current line has LLM_TENSOR_*
const tensorMatched = line.match(/LLM_TENSOR_[A-Z0-9_]+[,\s]+"(?<name>.+?)"/);
if (tensorMatched?.groups) {
const arch = archList.find((a) => a.cppConst === currCppConst);
if (arch) arch.tensorNames.push(tensorMatched.groups.name);
}
}
/////////////////////////////////////
// extract list of hyper params based on architecture
let insideLoadHParamsFn = false;
currCppConst = "";
for (const line of cppSource.split("\n")) {
// check if current line is function llama_model::load_hparams()
if (line.startsWith("void llama_model::load_hparams")) {
insideLoadHParamsFn = true;
}
if (!insideLoadHParamsFn) {
continue;
}
// check if current line has LLM_ARCH_*
const RE_CASE = new RegExp(`case (${RE_ARCH_NAME.source})`);
const cppConst = line.match(RE_CASE)?.[1];
if (cppConst) {
currCppConst = cppConst;
continue;
}
// check if current line has get_key(...)
const keyConst = line.match(/LLM_KV_[A-Z0-9_]+/)?.[0];
if (keyConst) {
const arch = archList.find((a) => a.cppConst === currCppConst);
if (arch) {