mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-10-11 07:20:33 +02:00
common : read a GGUF's trained context from its metadata
common_get_gguf_n_ctx_train opens only the file's metadata (no_alloc, like common_get_decision_type) and reads <arch>.context_length, so a caller can learn the trained context without loading the model. It accepts both u32 and u64 values and returns 0 when the file is missing, unreadable, invalid, or reports no context length. Assisted-by: pi:zai-org/GLM-5.3-Flash
This commit is contained in:
@@ -1230,6 +1230,42 @@ common_decision_type common_get_decision_type(const std::string & fname) {
|
|||||||
return common_decision_type_from_string(gguf_get_val_str(gguf_ctx.get(), type_id));
|
return common_decision_type_from_string(gguf_get_val_str(gguf_ctx.get(), type_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t common_get_gguf_n_ctx_train(const std::string & fname) {
|
||||||
|
struct gguf_init_params gguf_params = {
|
||||||
|
/* .no_alloc = */ true,
|
||||||
|
/* .ctx = */ nullptr,
|
||||||
|
};
|
||||||
|
|
||||||
|
gguf_context_ptr gguf_ctx(gguf_init_from_file(fname.c_str(), gguf_params));
|
||||||
|
if (!gguf_ctx) {
|
||||||
|
return 0; // missing or unreadable file
|
||||||
|
}
|
||||||
|
|
||||||
|
const int64_t arch_id = gguf_find_key(gguf_ctx.get(), "general.architecture");
|
||||||
|
if (arch_id < 0 || gguf_get_kv_type(gguf_ctx.get(), arch_id) != GGUF_TYPE_STRING) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string key = std::string(gguf_get_val_str(gguf_ctx.get(), arch_id)) + ".context_length";
|
||||||
|
const int64_t key_id = gguf_find_key(gguf_ctx.get(), key.c_str());
|
||||||
|
if (key_id < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// writers are not strict about the width of the hparams, so accept both
|
||||||
|
switch (gguf_get_kv_type(gguf_ctx.get(), key_id)) {
|
||||||
|
case GGUF_TYPE_UINT32:
|
||||||
|
return gguf_get_val_u32(gguf_ctx.get(), key_id);
|
||||||
|
case GGUF_TYPE_UINT64:
|
||||||
|
{
|
||||||
|
const uint64_t val = gguf_get_val_u64(gguf_ctx.get(), key_id);
|
||||||
|
return val <= UINT32_MAX ? (uint32_t) val : 0;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
common_init_result::common_init_result(common_params & params, bool model_only) :
|
common_init_result::common_init_result(common_params & params, bool model_only) :
|
||||||
pimpl(new impl{}) {
|
pimpl(new impl{}) {
|
||||||
auto mparams = common_model_params_to_llama(params);
|
auto mparams = common_model_params_to_llama(params);
|
||||||
|
|||||||
@@ -974,6 +974,10 @@ common_decision_type common_get_decision_type(const struct llama_model * model);
|
|||||||
// returns COMMON_DECISION_TYPE_UNKNOWN if the file is missing, unreadable, or invalid
|
// returns COMMON_DECISION_TYPE_UNKNOWN if the file is missing, unreadable, or invalid
|
||||||
common_decision_type common_get_decision_type(const std::string & fname);
|
common_decision_type common_get_decision_type(const std::string & fname);
|
||||||
|
|
||||||
|
// reads the trained context of a GGUF file from its metadata; it does not load the model
|
||||||
|
// returns 0 if the file is missing, unreadable, invalid, or reports no context length
|
||||||
|
uint32_t common_get_gguf_n_ctx_train(const std::string & fname);
|
||||||
|
|
||||||
// note: defines the model, context, samplers, ets. lifetimes
|
// note: defines the model, context, samplers, ets. lifetimes
|
||||||
struct common_init_result {
|
struct common_init_result {
|
||||||
common_init_result(common_params & params, bool model_only = false);
|
common_init_result(common_params & params, bool model_only = false);
|
||||||
|
|||||||
Reference in New Issue
Block a user