File size: 1,603 Bytes
89565fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// ref: https://github.com/ggml-org/llama.cpp/issues/30556

#include "ggml-backend.h"

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <mach/mach.h>

static uint64_t phys_footprint() {
    task_vm_info_data_t info;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    const kern_return_t ret = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &info, &count);
    GGML_ASSERT(ret == KERN_SUCCESS);
    return info.phys_footprint;
}

static bool test_release(ggml_backend_buffer_type_t buft) {
    const size_t size = 256ull*1024*1024;

    ggml_backend_buffer_t buf = ggml_backend_buft_alloc_buffer(buft, size);
    GGML_ASSERT(buf != nullptr);
    ggml_backend_buffer_clear(buf, 1);

    const uint64_t footprint_alloc = phys_footprint();
    ggml_backend_buffer_free(buf);
    const uint64_t footprint_free = phys_footprint();

    printf("%s: phys_footprint %.1f MiB -> %.1f MiB after freeing %.1f MiB\n", ggml_backend_buft_name(buft), footprint_alloc/1048576.0, footprint_free/1048576.0, size/1048576.0);

    // do not poll, the footprint must drop at free time
    return footprint_free < footprint_alloc - 0.75*size;
}

int main() {
    ggml_backend_load_all();

    bool ok = test_release(ggml_backend_cpu_buffer_type());

    // private Metal buffers are allocated by Metal, not by ggml_metal_host_malloc
    ggml_backend_dev_t dev = ggml_backend_dev_by_name("MTL0");
    if (dev && strcmp(ggml_backend_buft_name(ggml_backend_dev_buffer_type(dev)), "MTL0") == 0) {
        ok = test_release(ggml_backend_dev_buffer_type(dev)) && ok;
    }

    return ok ? 0 : 1;
}