interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-zdnn / scratch.cpp

45 lines · 1382 bytes Raw Blame History
1#include "ggml.h"
2#include "scratch.hpp"
3
4#include <algorithm>
5#include <cstdint>
6#include <cstdlib>
7
8ggml_zdnn_pool::~ggml_zdnn_pool() {
9 for (const buffer & b : cached) {
10 std::free(b.ptr);
11 }
12}
13
14void * ggml_zdnn_pool::alloc(size_t size, size_t * actual_size) {
15 int64_t ibest = -1;
16 for (int64_t i = 0; i < (int64_t)cached.size(); i++) {
17 if (cached[i].size >= size && (ibest < 0 || cached[i].size < cached[ibest].size)) {
18 ibest = i;
19 }
20 }
21
22 if (ibest >= 0) {
23 void * ptr = cached[ibest].ptr;
24 *actual_size = cached[ibest].size;
25 cached.erase(cached.begin() + ibest);
26 return ptr;
27 }
28
29 // every cached buffer is too small, free the largest so the pool does not grow while sizes grow with the context
30 if (!cached.empty()) {
31 const auto largest = std::max_element(cached.begin(), cached.end(), [](const buffer & a, const buffer & b) { return a.size < b.size; });
32 std::free(largest->ptr);
33 cached.erase(largest);
34 }
35
36 // 5% more, so slowly growing sizes do not allocate every time
37 *actual_size = GGML_PAD(std::max<size_t>(1, (size_t)(1.05 * size)), 4096);
38 void * ptr = std::aligned_alloc(4096, *actual_size);
39 GGML_ASSERT(ptr != nullptr);
40 return ptr;
41}
42
43void ggml_zdnn_pool::free(void * ptr, size_t size) {
44 cached.push_back({ ptr, size });
45}