mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-11-27 07:40:05 +00:00
Some checks are pending
CI (3rd-party) / ubuntu-24-llguidance (push) Waiting to run
CI (android) / default (push) Waiting to run
CI (android) / ndk (push) Waiting to run
CI (android) / arm64 (push) Waiting to run
CI (apple) / macos-latest-arm64 (push) Waiting to run
CI (apple) / macos-latest-x64 (push) Waiting to run
CI (apple) / macos-latest-ios-xcode (push) Waiting to run
CI (apple) / macos-latest-tvos (push) Waiting to run
CI (apple) / macos-latest-visionos (push) Waiting to run
CI (apple) / macos-latest-swift (generic/platform=iOS) (push) Blocked by required conditions
CI (apple) / macos-latest-swift (generic/platform=macOS) (push) Blocked by required conditions
CI (apple) / macos-latest-swift (generic/platform=tvOS) (push) Blocked by required conditions
CI (cpu) / build-cmake-pkg (push) Waiting to run
CI (cpu) / ubuntu (arm64, ubuntu-24.04-arm) (push) Waiting to run
CI (cpu) / ubuntu (x64, ubuntu-22.04) (push) Waiting to run
CI (cpu) / windows / arm64 (push) Waiting to run
CI (cpu) / windows / x64-cpu-static (push) Waiting to run
CI (cpu) / windows / x64-openblas (push) Waiting to run
CI (CUDA, ubuntu) / cuda (push) Waiting to run
CI (CUDA, ubuntu) / hip (push) Waiting to run
CI (CUDA, ubuntu) / musa (push) Waiting to run
CI (ibm) / ubuntu-24-s390x (VXE OFF) (push) Waiting to run
CI (ibm) / ubuntu-24-s390x (VXE ON) (push) Waiting to run
CI (ibm) / ubuntu-26-zdnn-s390x (push) Waiting to run
CI (ibm) / ubuntu-24-ppc64le (push) Waiting to run
CI (opencl) / windows-2025-opencl-adreno (push) Waiting to run
CI (openvino) / ubuntu-24-openvino (push) Waiting to run
CI (openvino) / openvino-windows-2022 (push) Waiting to run
CI (riscv) / ubuntu-cpu-riscv64-native (push) Waiting to run
CI (sanitize) / ctest (ubuntu-24.04, ADDRESS) (push) Waiting to run
CI (sanitize) / ctest (ubuntu-24.04, THREAD) (push) Waiting to run
CI (sanitize) / ctest ([self-hosted X64 Linux], UNDEFINED) (push) Waiting to run
CI (sycl) / ubuntu-24-sycl (fp16, ON) (push) Waiting to run
CI (sycl) / ubuntu-24-sycl (fp32, OFF) (push) Waiting to run
CI (sycl) / windows-latest-sycl (push) Waiting to run
CI (virtgpu) / ubuntu-24-virtgpu (push) Waiting to run
CI (vulkan) / ubuntu-arm64 (push) Waiting to run
CI (vulkan) / ubuntu-llvmpipe (push) Waiting to run
CI (vulkan) / windows (push) Waiting to run
CI (wasm) / ubuntu-webgpu (push) Waiting to run
CI (webgpu) / format (push) Waiting to run
CI (webgpu) / macos (push) Waiting to run
CI (webgpu) / ubuntu (push) Waiting to run
CI (self-hosted CPU backend) / cpu-arm64-high-perf-graviton4 (push) Waiting to run
CI (self-hosted CPU backend) / cpu-x64-high-perf (push) Waiting to run
CI (self-hosted CUDA backend) / gpu-cuda (push) Waiting to run
CI (self-hosted CUDA backend) / gpu-rocm (push) Waiting to run
CI (self-hosted KleidiAI backend) / cpu-arm64-graviton4-kleidiai (push) Waiting to run
CI (self-hosted Metal backend) / gpu-metal (push) Waiting to run
CI (self-hosted OpenVINO backend) / gpu-openvino-low-perf (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-nvidia-cm (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-nvidia-cm2 (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-apple (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-intel-linux (push) Waiting to run
CI (self-hosted Vulkan backend) / gpu-vulkan-intel-windows (push) Waiting to run
CI (self-hosted WebGPU backend) / gpu-webgpu-nvidia (push) Waiting to run
CI (self-hosted WebGPU backend) / gpu-webgpu-apple (push) Waiting to run
Code Style Checker / model-naming (push) Waiting to run
EditorConfig Checker / editorconfig (push) Waiting to run
Release / macos-cpu (x64, x64, -DGGML_METAL=OFF -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3, macos-15-intel) (push) Blocked by required conditions
Release / windows-cpu / arm64 (push) Blocked by required conditions
Release / check-release (push) Waiting to run
Release / macos-cpu (arm64, arm64, -DGGML_METAL_EMBED_LIBRARY=ON -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3, macos-26) (push) Blocked by required conditions
Release / ubuntu-cpu (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Release / ubuntu-cpu (s390x, ubuntu-24.04-s390x) (push) Blocked by required conditions
Release / ubuntu-cpu (x64, ubuntu-22.04) (push) Blocked by required conditions
Release / ubuntu-vulkan (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Release / ubuntu-vulkan (x64, ubuntu-22.04) (push) Blocked by required conditions
Release / ubuntu-cuda (13.4, arm64) (push) Blocked by required conditions
Release / ubuntu-cuda (12.8, x64) (push) Blocked by required conditions
Release / ubuntu-cuda (13.4, x64) (push) Blocked by required conditions
Release / android-arm64 (push) Blocked by required conditions
Release / android-arm64-snapdragon (push) Blocked by required conditions
Release / linux-arm64-snapdragon (push) Blocked by required conditions
Release / ubuntu-24-openvino (push) Blocked by required conditions
Release / windows-openvino (push) Blocked by required conditions
Release / windows-cpu / x64 (push) Blocked by required conditions
Release / windows-rocm (10.0.0, x64, gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Blocked by required conditions
Release / windows (arm64, opencl-adreno, -G "Ninja Multi-Config" -D CMAKE_TOOLCHAIN_FILE=cmake/arm64-windows-llvm.cmake -DCMAKE_PREFIX_PATH="$env:RUNNER_TEMP/opencl-arm64-release" -DGGML_OPENCL=ON -DGGML_OPENCL_USE_ADRENO_KERNELS=ON, ggml-opencl) (push) Blocked by required conditions
Release / windows (x64, vulkan, -DGGML_VULKAN=ON, ggml-vulkan) (push) Blocked by required conditions
Release / windows-cuda (13.4, arm64) (push) Blocked by required conditions
Release / windows-cuda (12.4, x64) (push) Blocked by required conditions
Release / windows-cuda (13.4, x64) (push) Blocked by required conditions
Release / windows-sycl (push) Blocked by required conditions
Release / ubuntu-24-sycl (fp16, ON) (push) Blocked by required conditions
Release / ubuntu-24-sycl (fp32, OFF) (push) Blocked by required conditions
Release / ubuntu-24-rocm (10.0.0, x64, gfx908;gfx90a;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1200;gfx1201) (push) Blocked by required conditions
Release / ios-xcode (push) Blocked by required conditions
Release / ui-build (push) Blocked by required conditions
Server (sanitize) / server (RelWithDebInfo, ADDRESS) (push) Waiting to run
Server (sanitize) / server (RelWithDebInfo, UNDEFINED) (push) Waiting to run
Server (self-hosted) / server-metal (push) Waiting to run
Server (self-hosted) / server-cuda (push) Waiting to run
Server (self-hosted) / server-kleidiai (push) Waiting to run
Server / ubuntu (push) Waiting to run
Server / windows (push) Waiting to run
UI / Checks (push) Blocked by required conditions
UI / Build static output (push) Waiting to run
UI / E2E Tests (push) Blocked by required conditions
* Use activations to calculate the stats * Determine calculation mode * Compute entropy for activations * Compute cosine similarity based on activations * Compute l2 norm * Add compute_layer_statistics() function * Update aggregated statistic report layout * Fix printing l2 norm when calc_mode = 1 * Refactor variable name * Compute aggregated (per layer) l2 norm * Update aggregated sum of squared activations per layer * Make ZD Score two-tailed * Update report layout * Reverse conditional logic to match convention * Rename report heading * Add --activation-statistics parameter * Add Euclidean–Cosine Score (ECS) * Add --activation-statistics logic to avoid doubling the imatrix size by default * Update stats output sort based on imatrix type * Process external NextN draft files (-md / --model-draft) * Refactor to use new llama_batch_ext Co-authored-by: compilade <git@compilade.net> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>
190 lines
6.7 KiB
C++
190 lines
6.7 KiB
C++
#include "imatrix-loader.h"
|
|
#include "common.h"
|
|
#include "log.h"
|
|
#include "gguf.h"
|
|
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
|
|
static bool common_imatrix_load_legacy(const std::string & fname, common_imatrix & imatrix) {
|
|
std::ifstream in(fname, std::ios::binary);
|
|
if (!in) {
|
|
LOG_ERR("%s: failed to open %s\n", __func__, fname.c_str());
|
|
return false;
|
|
}
|
|
|
|
int n_entries;
|
|
in.read((char *) &n_entries, sizeof(n_entries));
|
|
if (in.fail() || n_entries < 1) {
|
|
LOG_ERR("%s: no data in file %s\n", __func__, fname.c_str());
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < n_entries; ++i) {
|
|
int32_t len = 0;
|
|
in.read((char *) &len, sizeof(len));
|
|
std::vector<char> name_as_vec(len + 1);
|
|
in.read((char *) name_as_vec.data(), len);
|
|
if (in.fail()) {
|
|
LOG_ERR("%s: failed reading name for entry %d from %s\n", __func__, i + 1, fname.c_str());
|
|
return false;
|
|
}
|
|
name_as_vec[len] = 0;
|
|
std::string name{ name_as_vec.data() };
|
|
|
|
int32_t ncall = 0;
|
|
in.read((char *) &ncall, sizeof(ncall));
|
|
int32_t nval = 0;
|
|
in.read((char *) &nval, sizeof(nval));
|
|
if (in.fail() || nval < 1) {
|
|
LOG_ERR("%s: failed reading number of values for entry %d\n", __func__, i);
|
|
return false;
|
|
}
|
|
|
|
auto & e = imatrix.entries[std::move(name)];
|
|
e.sums.resize(nval);
|
|
in.read((char *) e.sums.data(), nval * sizeof(float));
|
|
if (in.fail()) {
|
|
LOG_ERR("%s: failed reading data for entry %d\n", __func__, i);
|
|
return false;
|
|
}
|
|
|
|
e.counts.resize(1);
|
|
e.counts[0] = ncall;
|
|
}
|
|
|
|
// the trailing data (chunk count + dataset name) is optional
|
|
if (in.peek() != EOF) {
|
|
int32_t n_calls = 0;
|
|
in.read((char *) &n_calls, sizeof(n_calls));
|
|
imatrix.chunk_count = n_calls;
|
|
|
|
if (!in.fail()) {
|
|
int32_t len = 0;
|
|
in.read((char *) &len, sizeof(len));
|
|
if (!in.fail() && len > 0) {
|
|
std::vector<char> dataset(len + 1, 0);
|
|
in.read(dataset.data(), len);
|
|
if (!in.fail()) {
|
|
imatrix.datasets.push_back(dataset.data());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
imatrix.chunk_size = 0;
|
|
imatrix.is_legacy = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool common_imatrix_load(const std::string & fname, common_imatrix & imatrix) {
|
|
struct ggml_context * ctx = nullptr;
|
|
struct gguf_init_params meta_gguf_params = {
|
|
/* .no_alloc = */ false,
|
|
/* .ctx = */ &ctx,
|
|
};
|
|
struct gguf_context * ctx_gguf = gguf_init_from_file(fname.c_str(), meta_gguf_params);
|
|
if (!ctx_gguf) {
|
|
return common_imatrix_load_legacy(fname, imatrix);
|
|
}
|
|
|
|
const int32_t n_entries = gguf_get_n_tensors(ctx_gguf);
|
|
if (n_entries < 1) {
|
|
LOG_ERR("%s: no data in file %s\n", __func__, fname.c_str());
|
|
gguf_free(ctx_gguf);
|
|
ggml_free(ctx);
|
|
return false;
|
|
}
|
|
|
|
const int64_t datasets_key = gguf_find_key(ctx_gguf, LLM_KV_IMATRIX_DATASETS);
|
|
const int64_t chunk_count_key = gguf_find_key(ctx_gguf, LLM_KV_IMATRIX_CHUNK_COUNT);
|
|
const int64_t chunk_size_key = gguf_find_key(ctx_gguf, LLM_KV_IMATRIX_CHUNK_SIZE);
|
|
const int64_t nextn_key = gguf_find_key(ctx_gguf, LLM_KV_IMATRIX_N_LAYER_NEXTN);
|
|
|
|
if (datasets_key != -1 && gguf_get_kv_type(ctx_gguf, datasets_key) == GGUF_TYPE_ARRAY &&
|
|
gguf_get_arr_type(ctx_gguf, datasets_key) == GGUF_TYPE_STRING) {
|
|
const int64_t n = gguf_get_arr_n(ctx_gguf, datasets_key);
|
|
imatrix.datasets.reserve(imatrix.datasets.size() + n);
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
imatrix.datasets.push_back(gguf_get_arr_str(ctx_gguf, datasets_key, i));
|
|
}
|
|
}
|
|
|
|
imatrix.has_metadata = datasets_key != -1 && chunk_count_key != -1 && chunk_size_key != -1;
|
|
imatrix.chunk_count = chunk_count_key != -1 ? gguf_get_val_u32(ctx_gguf, chunk_count_key) : 0;
|
|
imatrix.chunk_size = chunk_size_key != -1 ? gguf_get_val_u32(ctx_gguf, chunk_size_key) : 0;
|
|
imatrix.n_layer_nextn = nextn_key != -1 ? gguf_get_val_u32(ctx_gguf, nextn_key) : 0;
|
|
|
|
const std::string in_sum_suffix{ ".in_sum" };
|
|
const std::string in_sum2_suffix{ ".in_sum2" };
|
|
const std::string counts_suffix{ ".counts" };
|
|
|
|
struct sum_tensors {
|
|
struct ggml_tensor * in_sum = nullptr;
|
|
struct ggml_tensor * in_sum2 = nullptr;
|
|
struct ggml_tensor * counts = nullptr;
|
|
};
|
|
|
|
std::map<std::string, sum_tensors> sums_counts_for;
|
|
for (struct ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
|
|
std::string name = cur->name;
|
|
if (name.empty()) { continue; }
|
|
|
|
if (string_remove_suffix(name, in_sum_suffix)) {
|
|
sums_counts_for[std::move(name)].in_sum = cur;
|
|
} else if (string_remove_suffix(name, in_sum2_suffix)) {
|
|
sums_counts_for[std::move(name)].in_sum2 = cur;
|
|
} else if (string_remove_suffix(name, counts_suffix)) {
|
|
sums_counts_for[std::move(name)].counts = cur;
|
|
}
|
|
}
|
|
|
|
for (const auto & sc : sums_counts_for) {
|
|
const std::string & name = sc.first;
|
|
const struct ggml_tensor * in_sum = sc.second.in_sum;
|
|
const struct ggml_tensor * in_sum2 = sc.second.in_sum2;
|
|
const struct ggml_tensor * counts = sc.second.counts;
|
|
|
|
if (!in_sum2 || !counts || (in_sum != nullptr && ggml_nelements(in_sum) != ggml_nelements(in_sum2))) {
|
|
LOG_ERR("%s: mismatched sums and counts for %s\n", __func__, name.c_str());
|
|
gguf_free(ctx_gguf);
|
|
ggml_free(ctx);
|
|
return false;
|
|
}
|
|
|
|
if (in_sum2->type != GGML_TYPE_F32 || counts->type != GGML_TYPE_F32) {
|
|
LOG_ERR("%s: sums and counts for %s must be F32\n", __func__, name.c_str());
|
|
gguf_free(ctx_gguf);
|
|
ggml_free(ctx);
|
|
return false;
|
|
}
|
|
|
|
auto & e = imatrix.entries[name];
|
|
|
|
const int64_t nval = ggml_nelements(in_sum2);
|
|
const int64_t ncounts = ggml_nelements(counts);
|
|
|
|
e.sums.resize(nval);
|
|
for (int64_t j = 0; j < nval; ++j) {
|
|
e.sums[j] = ((const float *) in_sum2->data)[j];
|
|
}
|
|
|
|
e.counts.resize(ncounts);
|
|
for (int64_t j = 0; j < ncounts; ++j) {
|
|
e.counts[j] = std::lround(((const float *) counts->data)[j]);
|
|
}
|
|
|
|
if (in_sum && ggml_nelements(in_sum) == nval) {
|
|
e.activations.resize(nval);
|
|
for (int64_t j = 0; j < nval; ++j) {
|
|
e.activations[j] = ((const float *) in_sum->data)[j];
|
|
}
|
|
}
|
|
}
|
|
|
|
gguf_free(ctx_gguf);
|
|
ggml_free(ctx);
|
|
return true;
|
|
}
|