gguf_metadata.cpp

src/inference/gguf_metadata.cpp

@file gguf_metadata.cpp @brief Metadata-only GGUF reads for the residency estimator (v2.13.1).

Owners: @tvanfossen

read_gguf_shape

GgufShape entropic::read_gguf_shape(const std::string &path)

Read a GGUF's shape from its metadata, without loading tensor data.

src/inference/gguf_metadata.cpp:64

Show on the map →

Called by SCENARIO, TEST_CASE, derive_tier_auto_placement, footprint_inputs_for, host_expert_bytes_for, reserve_forced_partial

Calls block_index_of, u32_or

Source · 51 lines
GgufShape read_gguf_shape(const std::string& path) {
    GgufShape shape;

    // no_alloc: map the header, the KV metadata and the tensor index; read
    // none of the tensor data. A 10 GB model costs the same as a 1 GB one.
    gguf_init_params params{};
    params.no_alloc = true;
    params.ctx = nullptr;

    gguf_context* ctx = gguf_init_from_file(path.c_str(), params);
    if (ctx == nullptr) {
        return shape;  // known stays false — caller must fall back.
    }

    const int64_t arch_id = gguf_find_key(ctx, "general.architecture");
    if (arch_id < 0) {
        gguf_free(ctx);
        return shape;  // No architecture means no way to name the other keys.
    }
    shape.architecture = gguf_get_val_str(ctx, arch_id);

    shape.block_count =
        u32_or(ctx, shape.architecture + ".block_count", 0);
    shape.expert_count =
        u32_or(ctx, shape.architecture + ".expert_count", 0);

    // Sum the tensor index. Expert bytes are summed by MATCHING llama.cpp's
    // own expert pattern rather than by assuming experts are some fraction
    // of a layer — the difference between a measurement and a heuristic.
    const std::regex experts(kExpertTensorRegex);
    const int64_t n_tensors = gguf_get_n_tensors(ctx);
    for (int64_t i = 0; i < n_tensors; ++i) {
        const std::string name = gguf_get_tensor_name(ctx, i);
        const auto bytes = static_cast<uint64_t>(gguf_get_tensor_size(ctx, i));
        if (block_index_of(name) < 0) {
            shape.non_block_bytes += bytes;
            continue;
        }
        shape.block_bytes += bytes;
        if (std::regex_search(name, experts)) {
            shape.expert_bytes += bytes;
        }
    }

    gguf_free(ctx);

    // A file whose tensors carry no block prefix tells us nothing about
    // per-layer cost, so it is not a usable shape even though it parsed.
    shape.known = shape.block_count > 0 && shape.block_bytes > 0;
    return shape;
}
Activity · 28 steps
lines 65–65GgufShape shape;lines 69–69gguf_init_params params{};lines 70–70params.no_alloc = true;lines 71–71params.ctx = nullptr;lines 73–73gguf_context* ctx = gguf_init_from_file(path.c_str(), params);lines 74–74◇ if (ctx == nullptr)lines 75–75return shape;lines 78–78const int64_t arch_id = gguf_find_key(ctx, "general.architecture");lines 79–79◇ if (arch_id < 0)lines 80–80gguf_free(ctx);lines 81–81return shape;lines 83–83shape.architecture = gguf_get_val_str(ctx, arch_id);lines 85–86shape.block_count = u32_or(ctx, shape.architecture + ".block_count", 0);lines 87–88shape.expert_count = u32_or(ctx, shape.architecture + ".expert_count", 0);lines 93–93const std::regex experts(kExpertTensorRegex);lines 94–94const int64_t n_tensors = gguf_get_n_tensors(ctx);lines 95–95◇ for (int64_t i = 0; i < n_tensors; ++i)lines 96–96const std::string name = gguf_get_tensor_name(ctx, i);lines 97–97const auto bytes = static_cast<uint64_t>(gguf_get_tensor_size(ctx, i));lines 98–98◇ if (block_index_of(name) < 0)lines 99–99shape.non_block_bytes += bytes;lines 100–100continue;lines 102–102shape.block_bytes += bytes;lines 103–103◇ if (std::regex_search(name, experts))lines 104–104shape.expert_bytes += bytes;lines 108–108gguf_free(ctx);lines 112–112shape.known = shape.block_count > 0 && shape.block_bytes > 0;lines 113–113return shape;

Requirements

REQ-INFER-019