char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovtests : avoid building get-model.cpp many times (#26317)47f686f53

char/aliceai-t5-moe
1.8 KiB53 linesraw
1// ref: https://github.com/ggml-org/llama.cpp/issues/25937
2// only works reliably when run with a large model that occupies 3GB+ of wired memory
3// thus, this test is not run by default
4// example model to run with: google/gemma-4-E4B-it-qat-q4_0-gguf
5
6#include "llama.h"
7#include "common.h"
8
9#include <cstdint>
10#include <mach/mach.h>
11#include <mach/mach_host.h>
12#include <unistd.h>
13
14static uint64_t wired_memory() {
15    vm_statistics64_data_t vmstat;
16    mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
17    if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vmstat, &count) != KERN_SUCCESS) {
18        return UINT64_MAX;
19    }
20    return static_cast<uint64_t>(vmstat.wire_count) * vm_kernel_page_size;
21}
22
23int main(int argc, char ** argv) {
24    auto * model_path = common_get_model_or_exit(argc, argv);
25
26    llama_backend_init();
27
28    const uint64_t wired_initial = wired_memory();
29
30    llama_model_params params = llama_model_default_params();
31    params.load_mode = LLAMA_LOAD_MODE_NONE;
32    struct llama_model* model = llama_model_load_from_file(model_path, params);
33
34    const uint64_t wired_loaded = wired_memory();
35    const uint64_t wired_delta = wired_loaded - wired_initial;
36    // system memory fluctuates, so we need to allocate enough to reliably detect the release
37    GGML_ASSERT(wired_delta > 2'000'000'000); // 2GB
38
39    llama_model_free(model);
40
41    const uint64_t t_start_ms = ggml_time_ms();
42
43    // expect most of the allocated memory to be released within 10 seconds
44    // we allow for some tolerance due to system-wide memory fluctuations
45    while (wired_memory() > wired_loaded - 0.75 * wired_delta) {
46        GGML_ASSERT(ggml_time_ms() - t_start_ms < 10'000);
47        usleep(100'000); // 100ms
48    }
49
50    llama_backend_free();
51
52    return 0;
53}