cui-llama.rn 1.3.6 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +22 -1
  2. package/android/src/main/CMakeLists.txt +25 -26
  3. package/android/src/main/java/com/rnllama/LlamaContext.java +31 -9
  4. package/android/src/main/java/com/rnllama/RNLlama.java +98 -0
  5. package/android/src/main/jni-utils.h +94 -0
  6. package/android/src/main/jni.cpp +132 -62
  7. package/android/src/newarch/java/com/rnllama/RNLlamaModule.java +15 -0
  8. package/android/src/oldarch/java/com/rnllama/RNLlamaModule.java +15 -0
  9. package/cpp/common.cpp +1982 -1982
  10. package/cpp/common.h +665 -664
  11. package/cpp/ggml-cpu.c +14122 -14122
  12. package/cpp/ggml-cpu.cpp +627 -627
  13. package/cpp/ggml-metal-impl.h +288 -0
  14. package/cpp/ggml-opt.cpp +854 -0
  15. package/cpp/ggml-opt.h +216 -0
  16. package/cpp/llama-mmap.cpp +589 -589
  17. package/cpp/llama.cpp +12547 -12544
  18. package/cpp/rn-llama.hpp +117 -116
  19. package/cpp/sgemm.h +14 -14
  20. package/ios/RNLlama.mm +47 -0
  21. package/ios/RNLlamaContext.h +3 -1
  22. package/ios/RNLlamaContext.mm +71 -14
  23. package/jest/mock.js +15 -3
  24. package/lib/commonjs/NativeRNLlama.js.map +1 -1
  25. package/lib/commonjs/index.js +33 -37
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/module/NativeRNLlama.js.map +1 -1
  28. package/lib/module/index.js +31 -35
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/typescript/NativeRNLlama.d.ts +26 -6
  31. package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
  32. package/lib/typescript/index.d.ts +21 -36
  33. package/lib/typescript/index.d.ts.map +1 -1
  34. package/llama-rn.podspec +4 -18
  35. package/package.json +2 -3
  36. package/src/NativeRNLlama.ts +32 -13
  37. package/src/index.ts +52 -47
  38. package/cpp/llama.cpp.rej +0 -23
@@ -1,589 +1,589 @@
1
- #include "llama-mmap.h"
2
-
3
- #include "llama-impl.h"
4
-
5
- #include "ggml.h"
6
-
7
- #include <cstring>
8
- #include <climits>
9
- #include <stdexcept>
10
-
11
- #ifdef __has_include
12
- #if __has_include(<unistd.h>)
13
- #include <unistd.h>
14
- #if defined(_POSIX_MAPPED_FILES)
15
- #include <sys/mman.h>
16
- #include <fcntl.h>
17
- #endif
18
- #if defined(_POSIX_MEMLOCK_RANGE)
19
- #include <sys/resource.h>
20
- #endif
21
- #endif
22
- #endif
23
-
24
- #if defined(_WIN32)
25
- #define WIN32_LEAN_AND_MEAN
26
- #ifndef NOMINMAX
27
- #define NOMINMAX
28
- #endif
29
- #include <windows.h>
30
- #ifndef PATH_MAX
31
- #define PATH_MAX MAX_PATH
32
- #endif
33
- #include <io.h>
34
- #endif
35
-
36
- // TODO: consider moving to llama-impl.h if needed in more places
37
- #if defined(_WIN32)
38
- std::string llama_format_win_err(DWORD err) {
39
- LPSTR buf;
40
- size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
41
- NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
42
- if (!size) {
43
- return "FormatMessageA failed";
44
- }
45
- std::string ret(buf, size);
46
- LocalFree(buf);
47
- return ret;
48
- }
49
- #endif
50
-
51
- // llama_file
52
-
53
- struct llama_file::impl {
54
- #if defined(_WIN32)
55
- HANDLE fp_win32;
56
- std::string GetErrorMessageWin32(DWORD error_code) const {
57
- std::string ret;
58
- LPSTR lpMsgBuf = NULL;
59
- DWORD bufLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
60
- NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0, NULL);
61
- if (!bufLen) {
62
- ret = format("Win32 error code: %lx", error_code);
63
- } else {
64
- ret = lpMsgBuf;
65
- LocalFree(lpMsgBuf);
66
- }
67
-
68
- return ret;
69
- }
70
-
71
- impl(const char * fname, const char * mode) {
72
- fp = lm_ggml_fopen(fname, mode);
73
- if (fp == NULL) {
74
- throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
75
- }
76
- fp_win32 = (HANDLE) _get_osfhandle(_fileno(fp));
77
- seek(0, SEEK_END);
78
- size = tell();
79
- seek(0, SEEK_SET);
80
- }
81
-
82
- size_t tell() const {
83
- LARGE_INTEGER li;
84
- li.QuadPart = 0;
85
- BOOL ret = SetFilePointerEx(fp_win32, li, &li, FILE_CURRENT);
86
- if (!ret) {
87
- throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
88
- }
89
-
90
- return li.QuadPart;
91
- }
92
-
93
- void seek(size_t offset, int whence) const {
94
- static_assert(SEEK_SET == FILE_BEGIN, "SEEK_SET != FILE_BEGIN");
95
- static_assert(SEEK_CUR == FILE_CURRENT, "SEEK_CUR != FILE_CURRENT");
96
- static_assert(SEEK_END == FILE_END, "SEEK_END != FILE_END");
97
-
98
- LARGE_INTEGER li;
99
- li.QuadPart = offset;
100
- BOOL ret = SetFilePointerEx(fp_win32, li, NULL, whence);
101
- if (!ret) {
102
- throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
103
- }
104
- }
105
-
106
- void read_raw(void * ptr, size_t len) const {
107
- size_t bytes_read = 0;
108
- while (bytes_read < len) {
109
- size_t chunk_size = std::min<size_t>(len - bytes_read, 64*1024*1024);
110
- DWORD chunk_read = 0;
111
- BOOL result = ReadFile(fp_win32, reinterpret_cast<char*>(ptr) + bytes_read, chunk_size, &chunk_read, NULL);
112
- if (!result) {
113
- throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
114
- }
115
- if (chunk_read < chunk_size || chunk_read == 0) {
116
- throw std::runtime_error("unexpectedly reached end of file");
117
- }
118
-
119
- bytes_read += chunk_read;
120
- }
121
- }
122
-
123
- uint32_t read_u32() const {
124
- uint32_t val;
125
- read_raw(&val, sizeof(val));
126
- return val;
127
- }
128
-
129
- void write_raw(const void * ptr, size_t len) const {
130
- size_t bytes_written = 0;
131
- while (bytes_written < len) {
132
- size_t chunk_size = std::min<size_t>(len - bytes_written, 64*1024*1024);
133
- DWORD chunk_written = 0;
134
- BOOL result = WriteFile(fp_win32, reinterpret_cast<char const*>(ptr) + bytes_written, chunk_size, &chunk_written, NULL);
135
- if (!result) {
136
- throw std::runtime_error(format("write error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
137
- }
138
- if (chunk_written < chunk_size || chunk_written == 0) {
139
- throw std::runtime_error("unexpectedly failed to write bytes");
140
- }
141
-
142
- bytes_written += chunk_written;
143
- }
144
- }
145
-
146
- void write_u32(uint32_t val) const {
147
- write_raw(&val, sizeof(val));
148
- }
149
-
150
- ~impl() {
151
- if (fp) {
152
- std::fclose(fp);
153
- }
154
- }
155
- #else
156
- impl(const char * fname, const char * mode) {
157
- fp = lm_ggml_fopen(fname, mode);
158
- if (fp == NULL) {
159
- throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
160
- }
161
- seek(0, SEEK_END);
162
- size = tell();
163
- seek(0, SEEK_SET);
164
- }
165
-
166
- size_t tell() const {
167
- // TODO: this ifdef is never true?
168
- #ifdef _WIN32
169
- __int64 ret = _ftelli64(fp);
170
- #else
171
- long ret = std::ftell(fp);
172
- #endif
173
- if (ret == -1) {
174
- throw std::runtime_error(format("ftell error: %s", strerror(errno)));
175
- }
176
-
177
- return (size_t) ret;
178
- }
179
-
180
- void seek(size_t offset, int whence) const {
181
- // TODO: this ifdef is never true?
182
- #ifdef _WIN32
183
- int ret = _fseeki64(fp, (__int64) offset, whence);
184
- #else
185
- int ret = std::fseek(fp, (long) offset, whence);
186
- #endif
187
- if (ret != 0) {
188
- throw std::runtime_error(format("seek error: %s", strerror(errno)));
189
- }
190
- }
191
-
192
- void read_raw(void * ptr, size_t len) const {
193
- if (len == 0) {
194
- return;
195
- }
196
- errno = 0;
197
- std::size_t ret = std::fread(ptr, len, 1, fp);
198
- if (ferror(fp)) {
199
- throw std::runtime_error(format("read error: %s", strerror(errno)));
200
- }
201
- if (ret != 1) {
202
- throw std::runtime_error("unexpectedly reached end of file");
203
- }
204
- }
205
-
206
- uint32_t read_u32() const {
207
- uint32_t ret;
208
- read_raw(&ret, sizeof(ret));
209
- return ret;
210
- }
211
-
212
- void write_raw(const void * ptr, size_t len) const {
213
- if (len == 0) {
214
- return;
215
- }
216
- errno = 0;
217
- size_t ret = std::fwrite(ptr, len, 1, fp);
218
- if (ret != 1) {
219
- throw std::runtime_error(format("write error: %s", strerror(errno)));
220
- }
221
- }
222
-
223
- void write_u32(uint32_t val) const {
224
- write_raw(&val, sizeof(val));
225
- }
226
-
227
- ~impl() {
228
- if (fp) {
229
- std::fclose(fp);
230
- }
231
- }
232
- #endif
233
-
234
- FILE * fp;
235
- size_t size;
236
- };
237
-
238
- llama_file::llama_file(const char * fname, const char * mode) : pimpl(std::make_unique<impl>(fname, mode)) {}
239
- llama_file::~llama_file() = default;
240
-
241
- size_t llama_file::tell() const { return pimpl->tell(); }
242
- size_t llama_file::size() const { return pimpl->size; }
243
-
244
- int llama_file::file_id() const {
245
- #ifdef _WIN32
246
- return _fileno(pimpl->fp);
247
- #else
248
- #if defined(fileno)
249
- return fileno(pimpl->fp);
250
- #else
251
- return ::fileno(pimpl->fp);
252
- #endif
253
- #endif
254
- }
255
-
256
- void llama_file::seek(size_t offset, int whence) const { pimpl->seek(offset, whence); }
257
- void llama_file::read_raw(void * ptr, size_t len) const { pimpl->read_raw(ptr, len); }
258
-
259
- uint32_t llama_file::read_u32() const { return pimpl->read_u32(); }
260
-
261
- void llama_file::write_raw(const void * ptr, size_t len) const { pimpl->write_raw(ptr, len); }
262
- void llama_file::write_u32(uint32_t val) const { pimpl->write_u32(val); }
263
-
264
- // llama_mmap
265
-
266
- struct llama_mmap::impl {
267
- #ifdef _POSIX_MAPPED_FILES
268
- std::vector<std::pair<size_t, size_t>> mapped_fragments;
269
-
270
- impl(struct llama_file * file, size_t prefetch, bool numa) {
271
- size = file->size();
272
- int fd = file->file_id();
273
- int flags = MAP_SHARED;
274
- if (numa) { prefetch = 0; }
275
- #ifdef __linux__
276
- if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
277
- LLAMA_LOG_WARN("warning: posix_fadvise(.., POSIX_FADV_SEQUENTIAL) failed: %s\n",
278
- strerror(errno));
279
- }
280
- if (prefetch) { flags |= MAP_POPULATE; }
281
- #endif
282
- addr = mmap(NULL, file->size(), PROT_READ, flags, fd, 0);
283
- if (addr == MAP_FAILED) {
284
- throw std::runtime_error(format("mmap failed: %s", strerror(errno)));
285
- }
286
-
287
- if (prefetch > 0) {
288
- if (madvise(addr, std::min(file->size(), prefetch), MADV_WILLNEED)) {
289
- LLAMA_LOG_WARN("warning: posix_madvise(.., POSIX_MADV_WILLNEED) failed: %s\n",
290
- strerror(errno));
291
- }
292
- }
293
- if (numa) {
294
- if (madvise(addr, file->size(), MADV_RANDOM)) {
295
- LLAMA_LOG_WARN("warning: posix_madvise(.., POSIX_MADV_RANDOM) failed: %s\n",
296
- strerror(errno));
297
- }
298
- }
299
-
300
- mapped_fragments.emplace_back(0, file->size());
301
- }
302
-
303
- static void align_range(size_t * first, size_t * last, size_t page_size) {
304
- size_t offset_in_page = *first & (page_size - 1);
305
- size_t offset_to_page = offset_in_page == 0 ? 0 : page_size - offset_in_page;
306
- *first += offset_to_page;
307
-
308
- *last = *last & ~(page_size - 1);
309
-
310
- if (*last <= *first) {
311
- *last = *first;
312
- }
313
- }
314
-
315
- void unmap_fragment(size_t first, size_t last) {
316
- int page_size = sysconf(_SC_PAGESIZE);
317
- align_range(&first, &last, page_size);
318
- size_t len = last - first;
319
-
320
- if (len == 0) {
321
- return;
322
- }
323
-
324
- LM_GGML_ASSERT(first % page_size == 0);
325
- LM_GGML_ASSERT(last % page_size == 0);
326
- LM_GGML_ASSERT(last > first);
327
-
328
- void * next_page_start = (uint8_t *) addr + first;
329
-
330
- if (munmap(next_page_start, len)) {
331
- LLAMA_LOG_WARN("warning: munmap failed: %s\n", strerror(errno));
332
- }
333
-
334
- std::vector<std::pair<size_t, size_t>> new_mapped_fragments;
335
- for (const auto & frag : mapped_fragments) {
336
- if (frag.first < first && frag.second > last) {
337
- new_mapped_fragments.emplace_back(frag.first, first);
338
- new_mapped_fragments.emplace_back(last, frag.second);
339
- } else if (frag.first < first && frag.second > first) {
340
- new_mapped_fragments.emplace_back(frag.first, first);
341
- } else if (frag.first < last && frag.second > last) {
342
- new_mapped_fragments.emplace_back(last, frag.second);
343
- } else if (frag.first >= first && frag.second <= last) {
344
- } else {
345
- new_mapped_fragments.push_back(frag);
346
- }
347
- }
348
- mapped_fragments = std::move(new_mapped_fragments);
349
- }
350
-
351
- ~impl() {
352
- for (const auto & frag : mapped_fragments) {
353
- if (munmap((char *) addr + frag.first, frag.second - frag.first)) {
354
- LLAMA_LOG_WARN("warning: munmap failed: %s\n", strerror(errno));
355
- }
356
- }
357
- }
358
- #elif defined(_WIN32)
359
- impl(struct llama_file * file, size_t prefetch, bool numa) {
360
- LM_GGML_UNUSED(numa);
361
-
362
- size = file->size();
363
-
364
- HANDLE hFile = (HANDLE) _get_osfhandle(file->file_id());
365
-
366
- HANDLE hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
367
-
368
- if (hMapping == NULL) {
369
- DWORD error = GetLastError();
370
- throw std::runtime_error(format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()));
371
- }
372
-
373
- addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
374
- DWORD error = GetLastError();
375
- CloseHandle(hMapping);
376
-
377
- if (addr == NULL) {
378
- throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
379
- }
380
-
381
- if (prefetch > 0) {
382
- #if _WIN32_WINNT >= 0x602
383
- BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG);
384
- HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
385
-
386
- pPrefetchVirtualMemory = (decltype(pPrefetchVirtualMemory))(void *) GetProcAddress(hKernel32, "PrefetchVirtualMemory");
387
-
388
- if (pPrefetchVirtualMemory) {
389
- WIN32_MEMORY_RANGE_ENTRY range;
390
- range.VirtualAddress = addr;
391
- range.NumberOfBytes = (SIZE_T) std::min(size, prefetch);
392
- if (!pPrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
393
- LLAMA_LOG_WARN("warning: PrefetchVirtualMemory failed: %s\n",
394
- llama_format_win_err(GetLastError()).c_str());
395
- }
396
- }
397
- #else
398
- throw std::runtime_error("PrefetchVirtualMemory unavailable");
399
- #endif
400
- }
401
- }
402
-
403
- void unmap_fragment(size_t first, size_t last) {
404
- LM_GGML_UNUSED(first);
405
- LM_GGML_UNUSED(last);
406
- }
407
-
408
- ~impl() {
409
- if (!UnmapViewOfFile(addr)) {
410
- LLAMA_LOG_WARN("warning: UnmapViewOfFile failed: %s\n",
411
- llama_format_win_err(GetLastError()).c_str());
412
- }
413
- }
414
- #else
415
- impl(struct llama_file * file, size_t prefetch, bool numa) {
416
- LM_GGML_UNUSED(file);
417
- LM_GGML_UNUSED(prefetch);
418
- LM_GGML_UNUSED(numa);
419
-
420
- throw std::runtime_error("mmap not supported");
421
- }
422
-
423
- void unmap_fragment(size_t first, size_t last) {
424
- LM_GGML_UNUSED(first);
425
- LM_GGML_UNUSED(last);
426
-
427
- throw std::runtime_error("mmap not supported");
428
- }
429
- #endif
430
-
431
- void * addr;
432
- size_t size;
433
- };
434
-
435
- llama_mmap::llama_mmap(struct llama_file * file, size_t prefetch, bool numa) : pimpl(std::make_unique<impl>(file, prefetch, numa)) {}
436
- llama_mmap::~llama_mmap() = default;
437
-
438
- size_t llama_mmap::size() const { return pimpl->size; }
439
- void * llama_mmap::addr() const { return pimpl->addr; }
440
-
441
- void llama_mmap::unmap_fragment(size_t first, size_t last) { pimpl->unmap_fragment(first, last); }
442
-
443
- #if defined(_POSIX_MEMLOCK_RANGE) || defined(_WIN32)
444
- const bool llama_mmap::SUPPORTED = true;
445
- #else
446
- const bool llama_mmap::SUPPORTED = false;
447
- #endif
448
-
449
- // llama_mlock
450
-
451
- struct llama_mlock::impl {
452
- #ifdef _POSIX_MEMLOCK_RANGE
453
- static size_t lock_granularity() {
454
- return (size_t) sysconf(_SC_PAGESIZE);
455
- }
456
-
457
- bool raw_lock(const void * addr, size_t size) const {
458
- if (!mlock(addr, size)) {
459
- return true;
460
- }
461
-
462
- #ifdef __APPLE__
463
- #define MLOCK_SUGGESTION \
464
- "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
465
- "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MEMLOCK (ulimit -l).\n"
466
- #else
467
- #define MLOCK_SUGGESTION \
468
- "Try increasing RLIMIT_MEMLOCK ('ulimit -l' as root).\n"
469
- #endif
470
-
471
- char* errmsg = std::strerror(errno);
472
- bool suggest = (errno == ENOMEM);
473
-
474
- struct rlimit lock_limit;
475
- if (suggest && getrlimit(RLIMIT_MEMLOCK, &lock_limit)) {
476
- suggest = false;
477
- }
478
- if (suggest && (lock_limit.rlim_max > lock_limit.rlim_cur + size)) {
479
- suggest = false;
480
- }
481
-
482
- LLAMA_LOG_WARN("warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n%s",
483
- size, this->size, errmsg, suggest ? MLOCK_SUGGESTION : "");
484
- return false;
485
- }
486
-
487
- static void raw_unlock(void * addr, size_t size) {
488
- if (munlock(addr, size)) {
489
- LLAMA_LOG_WARN("warning: failed to munlock buffer: %s\n", std::strerror(errno));
490
- }
491
- }
492
- #elif defined(_WIN32)
493
- static size_t lock_granularity() {
494
- SYSTEM_INFO si;
495
- GetSystemInfo(&si);
496
- return (size_t) si.dwPageSize;
497
- }
498
-
499
- bool raw_lock(void * ptr, size_t len) const {
500
- for (int tries = 1; ; tries++) {
501
- if (VirtualLock(ptr, len)) {
502
- return true;
503
- }
504
- if (tries == 2) {
505
- LLAMA_LOG_WARN("warning: failed to VirtualLock %zu-byte buffer (after previously locking %zu bytes): %s\n",
506
- len, size, llama_format_win_err(GetLastError()).c_str());
507
- return false;
508
- }
509
-
510
- SIZE_T min_ws_size, max_ws_size;
511
- if (!GetProcessWorkingSetSize(GetCurrentProcess(), &min_ws_size, &max_ws_size)) {
512
- LLAMA_LOG_WARN("warning: GetProcessWorkingSetSize failed: %s\n",
513
- llama_format_win_err(GetLastError()).c_str());
514
- return false;
515
- }
516
- size_t increment = len + 1048576;
517
- min_ws_size += increment;
518
- max_ws_size += increment;
519
- if (!SetProcessWorkingSetSize(GetCurrentProcess(), min_ws_size, max_ws_size)) {
520
- LLAMA_LOG_WARN("warning: SetProcessWorkingSetSize failed: %s\n",
521
- llama_format_win_err(GetLastError()).c_str());
522
- return false;
523
- }
524
- }
525
- }
526
-
527
- static void raw_unlock(void * ptr, size_t len) {
528
- if (!VirtualUnlock(ptr, len)) {
529
- LLAMA_LOG_WARN("warning: failed to VirtualUnlock buffer: %s\n",
530
- llama_format_win_err(GetLastError()).c_str());
531
- }
532
- }
533
- #else
534
- static size_t lock_granularity() {
535
- return (size_t) 65536;
536
- }
537
-
538
- bool raw_lock(const void * addr, size_t len) const {
539
- LLAMA_LOG_WARN("warning: mlock not supported on this system\n");
540
- return false;
541
- }
542
-
543
- static void raw_unlock(const void * addr, size_t len) {}
544
- #endif
545
-
546
- impl() : addr(NULL), size(0), failed_already(false) {}
547
-
548
- void init(void * ptr) {
549
- LM_GGML_ASSERT(addr == NULL && size == 0);
550
- addr = ptr;
551
- }
552
-
553
- void grow_to(size_t target_size) {
554
- LM_GGML_ASSERT(addr);
555
- if (failed_already) {
556
- return;
557
- }
558
- size_t granularity = lock_granularity();
559
- target_size = (target_size + granularity - 1) & ~(granularity - 1);
560
- if (target_size > size) {
561
- if (raw_lock((uint8_t *) addr + size, target_size - size)) {
562
- size = target_size;
563
- } else {
564
- failed_already = true;
565
- }
566
- }
567
- }
568
-
569
- void * addr;
570
- size_t size;
571
-
572
- bool failed_already;
573
- };
574
-
575
- llama_mlock::llama_mlock() : pimpl(std::make_unique<impl>()) {}
576
- llama_mlock::~llama_mlock() = default;
577
-
578
- void llama_mlock::init(void * ptr) { pimpl->init(ptr); }
579
- void llama_mlock::grow_to(size_t target_size) { pimpl->grow_to(target_size); }
580
-
581
- #if defined(_POSIX_MEMLOCK_RANGE) || defined(_WIN32)
582
- const bool llama_mlock::SUPPORTED = true;
583
- #else
584
- const bool llama_mlock::SUPPORTED = false;
585
- #endif
586
-
587
- size_t llama_path_max() {
588
- return PATH_MAX;
589
- }
1
+ #include "llama-mmap.h"
2
+
3
+ #include "llama-impl.h"
4
+
5
+ #include "ggml.h"
6
+
7
+ #include <cstring>
8
+ #include <climits>
9
+ #include <stdexcept>
10
+
11
+ #ifdef __has_include
12
+ #if __has_include(<unistd.h>)
13
+ #include <unistd.h>
14
+ #if defined(_POSIX_MAPPED_FILES)
15
+ #include <sys/mman.h>
16
+ #include <fcntl.h>
17
+ #endif
18
+ #if defined(_POSIX_MEMLOCK_RANGE)
19
+ #include <sys/resource.h>
20
+ #endif
21
+ #endif
22
+ #endif
23
+
24
+ #if defined(_WIN32)
25
+ #define WIN32_LEAN_AND_MEAN
26
+ #ifndef NOMINMAX
27
+ #define NOMINMAX
28
+ #endif
29
+ #include <windows.h>
30
+ #ifndef PATH_MAX
31
+ #define PATH_MAX MAX_PATH
32
+ #endif
33
+ #include <io.h>
34
+ #endif
35
+
36
+ // TODO: consider moving to llama-impl.h if needed in more places
37
+ #if defined(_WIN32)
38
+ std::string llama_format_win_err(DWORD err) {
39
+ LPSTR buf;
40
+ size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
41
+ NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
42
+ if (!size) {
43
+ return "FormatMessageA failed";
44
+ }
45
+ std::string ret(buf, size);
46
+ LocalFree(buf);
47
+ return ret;
48
+ }
49
+ #endif
50
+
51
+ // llama_file
52
+
53
+ struct llama_file::impl {
54
+ #if defined(_WIN32)
55
+ HANDLE fp_win32;
56
+ std::string GetErrorMessageWin32(DWORD error_code) const {
57
+ std::string ret;
58
+ LPSTR lpMsgBuf = NULL;
59
+ DWORD bufLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
60
+ NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0, NULL);
61
+ if (!bufLen) {
62
+ ret = format("Win32 error code: %lx", error_code);
63
+ } else {
64
+ ret = lpMsgBuf;
65
+ LocalFree(lpMsgBuf);
66
+ }
67
+
68
+ return ret;
69
+ }
70
+
71
+ impl(const char * fname, const char * mode) {
72
+ fp = lm_ggml_fopen(fname, mode);
73
+ if (fp == NULL) {
74
+ throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
75
+ }
76
+ fp_win32 = (HANDLE) _get_osfhandle(_fileno(fp));
77
+ seek(0, SEEK_END);
78
+ size = tell();
79
+ seek(0, SEEK_SET);
80
+ }
81
+
82
+ size_t tell() const {
83
+ LARGE_INTEGER li;
84
+ li.QuadPart = 0;
85
+ BOOL ret = SetFilePointerEx(fp_win32, li, &li, FILE_CURRENT);
86
+ if (!ret) {
87
+ throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
88
+ }
89
+
90
+ return li.QuadPart;
91
+ }
92
+
93
+ void seek(size_t offset, int whence) const {
94
+ static_assert(SEEK_SET == FILE_BEGIN, "SEEK_SET != FILE_BEGIN");
95
+ static_assert(SEEK_CUR == FILE_CURRENT, "SEEK_CUR != FILE_CURRENT");
96
+ static_assert(SEEK_END == FILE_END, "SEEK_END != FILE_END");
97
+
98
+ LARGE_INTEGER li;
99
+ li.QuadPart = offset;
100
+ BOOL ret = SetFilePointerEx(fp_win32, li, NULL, whence);
101
+ if (!ret) {
102
+ throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
103
+ }
104
+ }
105
+
106
+ void read_raw(void * ptr, size_t len) const {
107
+ size_t bytes_read = 0;
108
+ while (bytes_read < len) {
109
+ size_t chunk_size = std::min<size_t>(len - bytes_read, 64*1024*1024);
110
+ DWORD chunk_read = 0;
111
+ BOOL result = ReadFile(fp_win32, reinterpret_cast<char*>(ptr) + bytes_read, chunk_size, &chunk_read, NULL);
112
+ if (!result) {
113
+ throw std::runtime_error(format("read error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
114
+ }
115
+ if (chunk_read < chunk_size || chunk_read == 0) {
116
+ throw std::runtime_error("unexpectedly reached end of file");
117
+ }
118
+
119
+ bytes_read += chunk_read;
120
+ }
121
+ }
122
+
123
+ uint32_t read_u32() const {
124
+ uint32_t val;
125
+ read_raw(&val, sizeof(val));
126
+ return val;
127
+ }
128
+
129
+ void write_raw(const void * ptr, size_t len) const {
130
+ size_t bytes_written = 0;
131
+ while (bytes_written < len) {
132
+ size_t chunk_size = std::min<size_t>(len - bytes_written, 64*1024*1024);
133
+ DWORD chunk_written = 0;
134
+ BOOL result = WriteFile(fp_win32, reinterpret_cast<char const*>(ptr) + bytes_written, chunk_size, &chunk_written, NULL);
135
+ if (!result) {
136
+ throw std::runtime_error(format("write error: %s", GetErrorMessageWin32(GetLastError()).c_str()));
137
+ }
138
+ if (chunk_written < chunk_size || chunk_written == 0) {
139
+ throw std::runtime_error("unexpectedly failed to write bytes");
140
+ }
141
+
142
+ bytes_written += chunk_written;
143
+ }
144
+ }
145
+
146
+ void write_u32(uint32_t val) const {
147
+ write_raw(&val, sizeof(val));
148
+ }
149
+
150
+ ~impl() {
151
+ if (fp) {
152
+ std::fclose(fp);
153
+ }
154
+ }
155
+ #else
156
+ impl(const char * fname, const char * mode) {
157
+ fp = lm_ggml_fopen(fname, mode);
158
+ if (fp == NULL) {
159
+ throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
160
+ }
161
+ seek(0, SEEK_END);
162
+ size = tell();
163
+ seek(0, SEEK_SET);
164
+ }
165
+
166
+ size_t tell() const {
167
+ // TODO: this ifdef is never true?
168
+ #ifdef _WIN32
169
+ __int64 ret = _ftelli64(fp);
170
+ #else
171
+ long ret = std::ftell(fp);
172
+ #endif
173
+ if (ret == -1) {
174
+ throw std::runtime_error(format("ftell error: %s", strerror(errno)));
175
+ }
176
+
177
+ return (size_t) ret;
178
+ }
179
+
180
+ void seek(size_t offset, int whence) const {
181
+ // TODO: this ifdef is never true?
182
+ #ifdef _WIN32
183
+ int ret = _fseeki64(fp, (__int64) offset, whence);
184
+ #else
185
+ int ret = std::fseek(fp, (long) offset, whence);
186
+ #endif
187
+ if (ret != 0) {
188
+ throw std::runtime_error(format("seek error: %s", strerror(errno)));
189
+ }
190
+ }
191
+
192
+ void read_raw(void * ptr, size_t len) const {
193
+ if (len == 0) {
194
+ return;
195
+ }
196
+ errno = 0;
197
+ std::size_t ret = std::fread(ptr, len, 1, fp);
198
+ if (ferror(fp)) {
199
+ throw std::runtime_error(format("read error: %s", strerror(errno)));
200
+ }
201
+ if (ret != 1) {
202
+ throw std::runtime_error("unexpectedly reached end of file");
203
+ }
204
+ }
205
+
206
+ uint32_t read_u32() const {
207
+ uint32_t ret;
208
+ read_raw(&ret, sizeof(ret));
209
+ return ret;
210
+ }
211
+
212
+ void write_raw(const void * ptr, size_t len) const {
213
+ if (len == 0) {
214
+ return;
215
+ }
216
+ errno = 0;
217
+ size_t ret = std::fwrite(ptr, len, 1, fp);
218
+ if (ret != 1) {
219
+ throw std::runtime_error(format("write error: %s", strerror(errno)));
220
+ }
221
+ }
222
+
223
+ void write_u32(uint32_t val) const {
224
+ write_raw(&val, sizeof(val));
225
+ }
226
+
227
+ ~impl() {
228
+ if (fp) {
229
+ std::fclose(fp);
230
+ }
231
+ }
232
+ #endif
233
+
234
+ FILE * fp;
235
+ size_t size;
236
+ };
237
+
238
+ llama_file::llama_file(const char * fname, const char * mode) : pimpl(std::make_unique<impl>(fname, mode)) {}
239
+ llama_file::~llama_file() = default;
240
+
241
+ size_t llama_file::tell() const { return pimpl->tell(); }
242
+ size_t llama_file::size() const { return pimpl->size; }
243
+
244
+ int llama_file::file_id() const {
245
+ #ifdef _WIN32
246
+ return _fileno(pimpl->fp);
247
+ #else
248
+ #if defined(fileno)
249
+ return fileno(pimpl->fp);
250
+ #else
251
+ return ::fileno(pimpl->fp);
252
+ #endif
253
+ #endif
254
+ }
255
+
256
+ void llama_file::seek(size_t offset, int whence) const { pimpl->seek(offset, whence); }
257
+ void llama_file::read_raw(void * ptr, size_t len) const { pimpl->read_raw(ptr, len); }
258
+
259
+ uint32_t llama_file::read_u32() const { return pimpl->read_u32(); }
260
+
261
+ void llama_file::write_raw(const void * ptr, size_t len) const { pimpl->write_raw(ptr, len); }
262
+ void llama_file::write_u32(uint32_t val) const { pimpl->write_u32(val); }
263
+
264
+ // llama_mmap
265
+
266
+ struct llama_mmap::impl {
267
+ #ifdef _POSIX_MAPPED_FILES
268
+ std::vector<std::pair<size_t, size_t>> mapped_fragments;
269
+
270
+ impl(struct llama_file * file, size_t prefetch, bool numa) {
271
+ size = file->size();
272
+ int fd = file->file_id();
273
+ int flags = MAP_SHARED;
274
+ if (numa) { prefetch = 0; }
275
+ #ifdef __linux__
276
+ if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
277
+ LLAMA_LOG_WARN("warning: posix_fadvise(.., POSIX_FADV_SEQUENTIAL) failed: %s\n",
278
+ strerror(errno));
279
+ }
280
+ if (prefetch) { flags |= MAP_POPULATE; }
281
+ #endif
282
+ addr = mmap(NULL, file->size(), PROT_READ, flags, fd, 0);
283
+ if (addr == MAP_FAILED) {
284
+ throw std::runtime_error(format("mmap failed: %s", strerror(errno)));
285
+ }
286
+
287
+ if (prefetch > 0) {
288
+ if (madvise(addr, std::min(file->size(), prefetch), MADV_WILLNEED)) {
289
+ fprintf(stderr, "warning: madvise(.., MADV_WILLNEED) failed: %s\n",
290
+ strerror(errno));
291
+ }
292
+ }
293
+ if (numa) {
294
+ if (madvise(addr, file->size(), MADV_RANDOM)) {
295
+ fprintf(stderr, "warning: madvise(.., MADV_RANDOM) failed: %s\n",
296
+ strerror(errno));
297
+ }
298
+ }
299
+
300
+ mapped_fragments.emplace_back(0, file->size());
301
+ }
302
+
303
+ static void align_range(size_t * first, size_t * last, size_t page_size) {
304
+ size_t offset_in_page = *first & (page_size - 1);
305
+ size_t offset_to_page = offset_in_page == 0 ? 0 : page_size - offset_in_page;
306
+ *first += offset_to_page;
307
+
308
+ *last = *last & ~(page_size - 1);
309
+
310
+ if (*last <= *first) {
311
+ *last = *first;
312
+ }
313
+ }
314
+
315
+ void unmap_fragment(size_t first, size_t last) {
316
+ int page_size = sysconf(_SC_PAGESIZE);
317
+ align_range(&first, &last, page_size);
318
+ size_t len = last - first;
319
+
320
+ if (len == 0) {
321
+ return;
322
+ }
323
+
324
+ LM_GGML_ASSERT(first % page_size == 0);
325
+ LM_GGML_ASSERT(last % page_size == 0);
326
+ LM_GGML_ASSERT(last > first);
327
+
328
+ void * next_page_start = (uint8_t *) addr + first;
329
+
330
+ if (munmap(next_page_start, len)) {
331
+ LLAMA_LOG_WARN("warning: munmap failed: %s\n", strerror(errno));
332
+ }
333
+
334
+ std::vector<std::pair<size_t, size_t>> new_mapped_fragments;
335
+ for (const auto & frag : mapped_fragments) {
336
+ if (frag.first < first && frag.second > last) {
337
+ new_mapped_fragments.emplace_back(frag.first, first);
338
+ new_mapped_fragments.emplace_back(last, frag.second);
339
+ } else if (frag.first < first && frag.second > first) {
340
+ new_mapped_fragments.emplace_back(frag.first, first);
341
+ } else if (frag.first < last && frag.second > last) {
342
+ new_mapped_fragments.emplace_back(last, frag.second);
343
+ } else if (frag.first >= first && frag.second <= last) {
344
+ } else {
345
+ new_mapped_fragments.push_back(frag);
346
+ }
347
+ }
348
+ mapped_fragments = std::move(new_mapped_fragments);
349
+ }
350
+
351
+ ~impl() {
352
+ for (const auto & frag : mapped_fragments) {
353
+ if (munmap((char *) addr + frag.first, frag.second - frag.first)) {
354
+ LLAMA_LOG_WARN("warning: munmap failed: %s\n", strerror(errno));
355
+ }
356
+ }
357
+ }
358
+ #elif defined(_WIN32)
359
+ impl(struct llama_file * file, size_t prefetch, bool numa) {
360
+ LM_GGML_UNUSED(numa);
361
+
362
+ size = file->size();
363
+
364
+ HANDLE hFile = (HANDLE) _get_osfhandle(file->file_id());
365
+
366
+ HANDLE hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
367
+
368
+ if (hMapping == NULL) {
369
+ DWORD error = GetLastError();
370
+ throw std::runtime_error(format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()));
371
+ }
372
+
373
+ addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
374
+ DWORD error = GetLastError();
375
+ CloseHandle(hMapping);
376
+
377
+ if (addr == NULL) {
378
+ throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
379
+ }
380
+
381
+ if (prefetch > 0) {
382
+ #if _WIN32_WINNT >= 0x602
383
+ BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG);
384
+ HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
385
+
386
+ pPrefetchVirtualMemory = (decltype(pPrefetchVirtualMemory))(void *) GetProcAddress(hKernel32, "PrefetchVirtualMemory");
387
+
388
+ if (pPrefetchVirtualMemory) {
389
+ WIN32_MEMORY_RANGE_ENTRY range;
390
+ range.VirtualAddress = addr;
391
+ range.NumberOfBytes = (SIZE_T) std::min(size, prefetch);
392
+ if (!pPrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
393
+ LLAMA_LOG_WARN("warning: PrefetchVirtualMemory failed: %s\n",
394
+ llama_format_win_err(GetLastError()).c_str());
395
+ }
396
+ }
397
+ #else
398
+ throw std::runtime_error("PrefetchVirtualMemory unavailable");
399
+ #endif
400
+ }
401
+ }
402
+
403
+ void unmap_fragment(size_t first, size_t last) {
404
+ LM_GGML_UNUSED(first);
405
+ LM_GGML_UNUSED(last);
406
+ }
407
+
408
+ ~impl() {
409
+ if (!UnmapViewOfFile(addr)) {
410
+ LLAMA_LOG_WARN("warning: UnmapViewOfFile failed: %s\n",
411
+ llama_format_win_err(GetLastError()).c_str());
412
+ }
413
+ }
414
+ #else
415
+ impl(struct llama_file * file, size_t prefetch, bool numa) {
416
+ LM_GGML_UNUSED(file);
417
+ LM_GGML_UNUSED(prefetch);
418
+ LM_GGML_UNUSED(numa);
419
+
420
+ throw std::runtime_error("mmap not supported");
421
+ }
422
+
423
+ void unmap_fragment(size_t first, size_t last) {
424
+ LM_GGML_UNUSED(first);
425
+ LM_GGML_UNUSED(last);
426
+
427
+ throw std::runtime_error("mmap not supported");
428
+ }
429
+ #endif
430
+
431
+ void * addr;
432
+ size_t size;
433
+ };
434
+
435
+ llama_mmap::llama_mmap(struct llama_file * file, size_t prefetch, bool numa) : pimpl(std::make_unique<impl>(file, prefetch, numa)) {}
436
+ llama_mmap::~llama_mmap() = default;
437
+
438
+ size_t llama_mmap::size() const { return pimpl->size; }
439
+ void * llama_mmap::addr() const { return pimpl->addr; }
440
+
441
+ void llama_mmap::unmap_fragment(size_t first, size_t last) { pimpl->unmap_fragment(first, last); }
442
+
443
+ #if defined(_POSIX_MEMLOCK_RANGE) || defined(_WIN32)
444
+ const bool llama_mmap::SUPPORTED = true;
445
+ #else
446
+ const bool llama_mmap::SUPPORTED = false;
447
+ #endif
448
+
449
+ // llama_mlock
450
+
451
+ struct llama_mlock::impl {
452
+ #ifdef _POSIX_MEMLOCK_RANGE
453
+ static size_t lock_granularity() {
454
+ return (size_t) sysconf(_SC_PAGESIZE);
455
+ }
456
+
457
+ bool raw_lock(const void * addr, size_t size) const {
458
+ if (!mlock(addr, size)) {
459
+ return true;
460
+ }
461
+
462
+ #ifdef __APPLE__
463
+ #define MLOCK_SUGGESTION \
464
+ "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
465
+ "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MEMLOCK (ulimit -l).\n"
466
+ #else
467
+ #define MLOCK_SUGGESTION \
468
+ "Try increasing RLIMIT_MEMLOCK ('ulimit -l' as root).\n"
469
+ #endif
470
+
471
+ char* errmsg = std::strerror(errno);
472
+ bool suggest = (errno == ENOMEM);
473
+
474
+ struct rlimit lock_limit;
475
+ if (suggest && getrlimit(RLIMIT_MEMLOCK, &lock_limit)) {
476
+ suggest = false;
477
+ }
478
+ if (suggest && (lock_limit.rlim_max > lock_limit.rlim_cur + size)) {
479
+ suggest = false;
480
+ }
481
+
482
+ LLAMA_LOG_WARN("warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n%s",
483
+ size, this->size, errmsg, suggest ? MLOCK_SUGGESTION : "");
484
+ return false;
485
+ }
486
+
487
+ static void raw_unlock(void * addr, size_t size) {
488
+ if (munlock(addr, size)) {
489
+ LLAMA_LOG_WARN("warning: failed to munlock buffer: %s\n", std::strerror(errno));
490
+ }
491
+ }
492
+ #elif defined(_WIN32)
493
+ static size_t lock_granularity() {
494
+ SYSTEM_INFO si;
495
+ GetSystemInfo(&si);
496
+ return (size_t) si.dwPageSize;
497
+ }
498
+
499
+ bool raw_lock(void * ptr, size_t len) const {
500
+ for (int tries = 1; ; tries++) {
501
+ if (VirtualLock(ptr, len)) {
502
+ return true;
503
+ }
504
+ if (tries == 2) {
505
+ LLAMA_LOG_WARN("warning: failed to VirtualLock %zu-byte buffer (after previously locking %zu bytes): %s\n",
506
+ len, size, llama_format_win_err(GetLastError()).c_str());
507
+ return false;
508
+ }
509
+
510
+ SIZE_T min_ws_size, max_ws_size;
511
+ if (!GetProcessWorkingSetSize(GetCurrentProcess(), &min_ws_size, &max_ws_size)) {
512
+ LLAMA_LOG_WARN("warning: GetProcessWorkingSetSize failed: %s\n",
513
+ llama_format_win_err(GetLastError()).c_str());
514
+ return false;
515
+ }
516
+ size_t increment = len + 1048576;
517
+ min_ws_size += increment;
518
+ max_ws_size += increment;
519
+ if (!SetProcessWorkingSetSize(GetCurrentProcess(), min_ws_size, max_ws_size)) {
520
+ LLAMA_LOG_WARN("warning: SetProcessWorkingSetSize failed: %s\n",
521
+ llama_format_win_err(GetLastError()).c_str());
522
+ return false;
523
+ }
524
+ }
525
+ }
526
+
527
+ static void raw_unlock(void * ptr, size_t len) {
528
+ if (!VirtualUnlock(ptr, len)) {
529
+ LLAMA_LOG_WARN("warning: failed to VirtualUnlock buffer: %s\n",
530
+ llama_format_win_err(GetLastError()).c_str());
531
+ }
532
+ }
533
+ #else
534
+ static size_t lock_granularity() {
535
+ return (size_t) 65536;
536
+ }
537
+
538
+ bool raw_lock(const void * addr, size_t len) const {
539
+ LLAMA_LOG_WARN("warning: mlock not supported on this system\n");
540
+ return false;
541
+ }
542
+
543
+ static void raw_unlock(const void * addr, size_t len) {}
544
+ #endif
545
+
546
+ impl() : addr(NULL), size(0), failed_already(false) {}
547
+
548
+ void init(void * ptr) {
549
+ LM_GGML_ASSERT(addr == NULL && size == 0);
550
+ addr = ptr;
551
+ }
552
+
553
+ void grow_to(size_t target_size) {
554
+ LM_GGML_ASSERT(addr);
555
+ if (failed_already) {
556
+ return;
557
+ }
558
+ size_t granularity = lock_granularity();
559
+ target_size = (target_size + granularity - 1) & ~(granularity - 1);
560
+ if (target_size > size) {
561
+ if (raw_lock((uint8_t *) addr + size, target_size - size)) {
562
+ size = target_size;
563
+ } else {
564
+ failed_already = true;
565
+ }
566
+ }
567
+ }
568
+
569
+ void * addr;
570
+ size_t size;
571
+
572
+ bool failed_already;
573
+ };
574
+
575
+ llama_mlock::llama_mlock() : pimpl(std::make_unique<impl>()) {}
576
+ llama_mlock::~llama_mlock() = default;
577
+
578
+ void llama_mlock::init(void * ptr) { pimpl->init(ptr); }
579
+ void llama_mlock::grow_to(size_t target_size) { pimpl->grow_to(target_size); }
580
+
581
+ #if defined(_POSIX_MEMLOCK_RANGE) || defined(_WIN32)
582
+ const bool llama_mlock::SUPPORTED = true;
583
+ #else
584
+ const bool llama_mlock::SUPPORTED = false;
585
+ #endif
586
+
587
+ size_t llama_path_max() {
588
+ return PATH_MAX;
589
+ }