llama-util.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /**
  2. * llama.cpp - git f64d44a9b9581cd58f7ec40f4fa1c3ca5ca18e1e
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023 Georgi Gerganov
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. // Internal header to be included only by llama.cpp.
  27. // Contains wrappers around OS interfaces.
  28. #ifndef LLAMA_UTIL_H
  29. #define LLAMA_UTIL_H
  30. #include <cstdio>
  31. #include <cstdint>
  32. #include <cerrno>
  33. #include <cstring>
  34. #include <cstdarg>
  35. #include <cstdlib>
  36. #include <climits>
  37. #include <string>
  38. #include <vector>
  39. #include <stdexcept>
  40. #ifdef __has_include
  41. #if __has_include(<unistd.h>)
  42. #include <unistd.h>
  43. #if defined(_POSIX_MAPPED_FILES)
  44. #include <sys/mman.h>
  45. #endif
  46. #if defined(_POSIX_MEMLOCK_RANGE)
  47. #include <sys/resource.h>
  48. #endif
  49. #endif
  50. #endif
  51. #if defined(_WIN32)
  52. #define WIN32_LEAN_AND_MEAN
  53. #ifndef NOMINMAX
  54. #define NOMINMAX
  55. #endif
  56. #include <windows.h>
  57. #include <io.h>
  58. #include <stdio.h> // for _fseeki64
  59. #endif
  60. #define LLAMA_ASSERT(x) \
  61. do { \
  62. if (!(x)) { \
  63. fprintf(stderr, "LLAMA_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, #x); \
  64. abort(); \
  65. } \
  66. } while (0)
  67. #ifdef __GNUC__
  68. #ifdef __MINGW32__
  69. __attribute__((format(gnu_printf, 1, 2)))
  70. #else
  71. __attribute__((format(printf, 1, 2)))
  72. #endif
  73. #endif
  74. static std::string format(const char * fmt, ...) {
  75. va_list ap, ap2;
  76. va_start(ap, fmt);
  77. va_copy(ap2, ap);
  78. int size = vsnprintf(NULL, 0, fmt, ap);
  79. LLAMA_ASSERT(size >= 0 && size < INT_MAX);
  80. std::vector<char> buf(size + 1);
  81. int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
  82. LLAMA_ASSERT(size2 == size);
  83. va_end(ap2);
  84. va_end(ap);
  85. return std::string(buf.data(), size);
  86. }
  87. struct llama_file {
  88. // use FILE * so we don't have to re-open the file to mmap
  89. FILE * fp;
  90. size_t size;
  91. llama_file(const char * fname, const char * mode) {
  92. fp = std::fopen(fname, mode);
  93. if (fp == NULL) {
  94. throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
  95. }
  96. seek(0, SEEK_END);
  97. size = tell();
  98. seek(0, SEEK_SET);
  99. }
  100. size_t tell() const {
  101. #ifdef _WIN32
  102. __int64 ret = _ftelli64(fp);
  103. #else
  104. long ret = std::ftell(fp);
  105. #endif
  106. LLAMA_ASSERT(ret != -1); // this really shouldn't fail
  107. return (size_t) ret;
  108. }
  109. void seek(size_t offset, int whence) {
  110. #ifdef _WIN32
  111. int ret = _fseeki64(fp, (__int64) offset, whence);
  112. #else
  113. int ret = std::fseek(fp, (long) offset, whence);
  114. #endif
  115. LLAMA_ASSERT(ret == 0); // same
  116. }
  117. void read_raw(void * ptr, size_t len) const {
  118. if (len == 0) {
  119. return;
  120. }
  121. errno = 0;
  122. std::size_t ret = std::fread(ptr, len, 1, fp);
  123. if (ferror(fp)) {
  124. throw std::runtime_error(format("read error: %s", strerror(errno)));
  125. }
  126. if (ret != 1) {
  127. throw std::runtime_error(std::string("unexpectedly reached end of file"));
  128. }
  129. }
  130. std::uint32_t read_u32() {
  131. std::uint32_t ret;
  132. read_raw(&ret, sizeof(ret));
  133. return ret;
  134. }
  135. std::string read_string(std::uint32_t len) {
  136. std::vector<char> chars(len);
  137. read_raw(chars.data(), len);
  138. return std::string(chars.data(), len);
  139. }
  140. void write_raw(const void * ptr, size_t len) const {
  141. if (len == 0) {
  142. return;
  143. }
  144. errno = 0;
  145. size_t ret = std::fwrite(ptr, len, 1, fp);
  146. if (ret != 1) {
  147. throw std::runtime_error(format("write error: %s", strerror(errno)));
  148. }
  149. }
  150. void write_u32(std::uint32_t val) {
  151. write_raw(&val, sizeof(val));
  152. }
  153. ~llama_file() {
  154. if (fp) {
  155. std::fclose(fp);
  156. }
  157. }
  158. };
  159. // llama_context_data
  160. struct llama_data_context {
  161. virtual void write(const void * src, size_t size) = 0;
  162. virtual size_t get_size_written() = 0;
  163. virtual ~llama_data_context() = default;
  164. };
  165. struct llama_data_buffer_context : llama_data_context {
  166. uint8_t* ptr;
  167. size_t size_written = 0;
  168. llama_data_buffer_context(uint8_t * p) : ptr(p) {}
  169. void write(const void * src, size_t size) override {
  170. memcpy(ptr, src, size);
  171. ptr += size;
  172. size_written += size;
  173. }
  174. size_t get_size_written() override {
  175. return size_written;
  176. }
  177. };
  178. struct llama_data_file_context : llama_data_context {
  179. llama_file* file;
  180. size_t size_written = 0;
  181. llama_data_file_context(llama_file * f) : file(f) {}
  182. void write(const void * src, size_t size) override {
  183. file->write_raw(src, size);
  184. size_written += size;
  185. }
  186. size_t get_size_written() override {
  187. return size_written;
  188. }
  189. };
  190. #if defined(_WIN32)
  191. static std::string llama_format_win_err(DWORD err) {
  192. LPSTR buf;
  193. size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  194. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
  195. if (!size) {
  196. return "FormatMessageA failed";
  197. }
  198. std::string ret(buf, size);
  199. LocalFree(buf);
  200. return ret;
  201. }
  202. #endif
  203. struct llama_mmap {
  204. void * addr;
  205. size_t size;
  206. llama_mmap(const llama_mmap &) = delete;
  207. #ifdef _POSIX_MAPPED_FILES
  208. static constexpr bool SUPPORTED = true;
  209. llama_mmap(struct llama_file * file, size_t prefetch = (size_t) -1 /* -1 = max value */, bool numa = false) {
  210. size = file->size;
  211. int fd = fileno(file->fp);
  212. int flags = MAP_SHARED;
  213. // prefetch/readahead impairs performance on NUMA systems
  214. if (numa) { prefetch = 0; }
  215. #ifdef __linux__
  216. if (prefetch >= file->size) { flags |= MAP_POPULATE; }
  217. #endif
  218. addr = mmap(NULL, file->size, PROT_READ, flags, fd, 0);
  219. if (addr == MAP_FAILED) {
  220. throw std::runtime_error(format("mmap failed: %s", strerror(errno)));
  221. }
  222. if (prefetch > 0) {
  223. // Advise the kernel to preload the mapped memory
  224. if (madvise(addr, std::min(file->size, prefetch), MADV_WILLNEED)) {
  225. fprintf(stderr, "warning: madvise(.., MADV_WILLNEED) failed: %s\n",
  226. strerror(errno));
  227. }
  228. }
  229. if (numa) {
  230. // advise the kernel not to use readahead
  231. // (because the next page might not belong on the same node)
  232. if (madvise(addr, file->size, MADV_RANDOM)) {
  233. fprintf(stderr, "warning: madvise(.., MADV_RANDOM) failed: %s\n",
  234. strerror(errno));
  235. }
  236. }
  237. }
  238. ~llama_mmap() {
  239. munmap(addr, size);
  240. }
  241. #elif defined(_WIN32)
  242. static constexpr bool SUPPORTED = true;
  243. llama_mmap(struct llama_file * file, bool prefetch = true, bool numa = false) {
  244. (void) numa;
  245. size = file->size;
  246. HANDLE hFile = (HANDLE) _get_osfhandle(_fileno(file->fp));
  247. HANDLE hMapping = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  248. DWORD error = GetLastError();
  249. if (hMapping == NULL) {
  250. throw std::runtime_error(format("CreateFileMappingA failed: %s", llama_format_win_err(error).c_str()));
  251. }
  252. addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
  253. error = GetLastError();
  254. CloseHandle(hMapping);
  255. if (addr == NULL) {
  256. throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
  257. }
  258. #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
  259. if (prefetch) {
  260. // Advise the kernel to preload the mapped memory
  261. WIN32_MEMORY_RANGE_ENTRY range;
  262. range.VirtualAddress = addr;
  263. range.NumberOfBytes = (SIZE_T)size;
  264. if (!PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
  265. fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
  266. llama_format_win_err(GetLastError()).c_str());
  267. }
  268. }
  269. #else
  270. #pragma message("warning: You are building for pre-Windows 8; prefetch not supported")
  271. #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN8
  272. }
  273. ~llama_mmap() {
  274. if (!UnmapViewOfFile(addr)) {
  275. fprintf(stderr, "warning: UnmapViewOfFile failed: %s\n",
  276. llama_format_win_err(GetLastError()).c_str());
  277. }
  278. }
  279. #else
  280. static constexpr bool SUPPORTED = false;
  281. llama_mmap(struct llama_file *, bool prefetch = true, bool numa = false) {
  282. (void) prefetch;
  283. (void) numa;
  284. throw std::runtime_error(std::string("mmap not supported"));
  285. }
  286. #endif
  287. };
  288. // Represents some region of memory being locked using mlock or VirtualLock;
  289. // will automatically unlock on destruction.
  290. struct llama_mlock {
  291. void * addr = NULL;
  292. size_t size = 0;
  293. bool failed_already = false;
  294. llama_mlock() {}
  295. llama_mlock(const llama_mlock &) = delete;
  296. ~llama_mlock() {
  297. if (size) {
  298. raw_unlock(addr, size);
  299. }
  300. }
  301. void init(void * ptr) {
  302. LLAMA_ASSERT(addr == NULL && size == 0);
  303. addr = ptr;
  304. }
  305. void grow_to(size_t target_size) {
  306. LLAMA_ASSERT(addr);
  307. if (failed_already) {
  308. return;
  309. }
  310. size_t granularity = lock_granularity();
  311. target_size = (target_size + granularity - 1) & ~(granularity - 1);
  312. if (target_size > size) {
  313. if (raw_lock((uint8_t *) addr + size, target_size - size)) {
  314. size = target_size;
  315. } else {
  316. failed_already = true;
  317. }
  318. }
  319. }
  320. #ifdef _POSIX_MEMLOCK_RANGE
  321. static constexpr bool SUPPORTED = true;
  322. size_t lock_granularity() {
  323. return (size_t) sysconf(_SC_PAGESIZE);
  324. }
  325. #ifdef __APPLE__
  326. #define MLOCK_SUGGESTION \
  327. "Try increasing the sysctl values 'vm.user_wire_limit' and 'vm.global_user_wire_limit' and/or " \
  328. "decreasing 'vm.global_no_user_wire_amount'. Also try increasing RLIMIT_MLOCK (ulimit -l).\n"
  329. #else
  330. #define MLOCK_SUGGESTION \
  331. "Try increasing RLIMIT_MLOCK ('ulimit -l' as root).\n"
  332. #endif
  333. bool raw_lock(const void * addr, size_t size) {
  334. if (!mlock(addr, size)) {
  335. return true;
  336. } else {
  337. char* errmsg = std::strerror(errno);
  338. bool suggest = (errno == ENOMEM);
  339. // Check if the resource limit is fine after all
  340. struct rlimit lock_limit;
  341. if (suggest && getrlimit(RLIMIT_MEMLOCK, &lock_limit))
  342. suggest = false;
  343. if (suggest && (lock_limit.rlim_max > lock_limit.rlim_cur + size))
  344. suggest = false;
  345. fprintf(stderr, "warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n%s",
  346. size, this->size, errmsg, suggest ? MLOCK_SUGGESTION : "");
  347. return false;
  348. }
  349. }
  350. #undef MLOCK_SUGGESTION
  351. void raw_unlock(void * addr, size_t size) {
  352. if (munlock(addr, size)) {
  353. fprintf(stderr, "warning: failed to munlock buffer: %s\n", std::strerror(errno));
  354. }
  355. }
  356. #elif defined(_WIN32)
  357. static constexpr bool SUPPORTED = true;
  358. size_t lock_granularity() {
  359. SYSTEM_INFO si;
  360. GetSystemInfo(&si);
  361. return (size_t) si.dwPageSize;
  362. }
  363. bool raw_lock(void * ptr, size_t len) {
  364. for (int tries = 1; ; tries++) {
  365. if (VirtualLock(ptr, len)) {
  366. return true;
  367. }
  368. if (tries == 2) {
  369. fprintf(stderr, "warning: failed to VirtualLock %zu-byte buffer (after previously locking %zu bytes): %s\n",
  370. len, size, llama_format_win_err(GetLastError()).c_str());
  371. return false;
  372. }
  373. // It failed but this was only the first try; increase the working
  374. // set size and try again.
  375. SIZE_T min_ws_size, max_ws_size;
  376. if (!GetProcessWorkingSetSize(GetCurrentProcess(), &min_ws_size, &max_ws_size)) {
  377. fprintf(stderr, "warning: GetProcessWorkingSetSize failed: %s\n",
  378. llama_format_win_err(GetLastError()).c_str());
  379. return false;
  380. }
  381. // Per MSDN: "The maximum number of pages that a process can lock
  382. // is equal to the number of pages in its minimum working set minus
  383. // a small overhead."
  384. // Hopefully a megabyte is enough overhead:
  385. size_t increment = len + 1048576;
  386. // The minimum must be <= the maximum, so we need to increase both:
  387. min_ws_size += increment;
  388. max_ws_size += increment;
  389. if (!SetProcessWorkingSetSize(GetCurrentProcess(), min_ws_size, max_ws_size)) {
  390. fprintf(stderr, "warning: SetProcessWorkingSetSize failed: %s\n",
  391. llama_format_win_err(GetLastError()).c_str());
  392. return false;
  393. }
  394. }
  395. }
  396. void raw_unlock(void * ptr, size_t len) {
  397. if (!VirtualUnlock(ptr, len)) {
  398. fprintf(stderr, "warning: failed to VirtualUnlock buffer: %s\n",
  399. llama_format_win_err(GetLastError()).c_str());
  400. }
  401. }
  402. #else
  403. static constexpr bool SUPPORTED = false;
  404. size_t lock_granularity() {
  405. return (size_t) 65536;
  406. }
  407. bool raw_lock(const void * addr, size_t len) {
  408. fprintf(stderr, "warning: mlock not supported on this system\n");
  409. return false;
  410. }
  411. void raw_unlock(const void * addr, size_t len) {}
  412. #endif
  413. };
  414. // Replacement for std::vector<uint8_t> that doesn't require zero-initialization.
  415. struct llama_buffer {
  416. uint8_t * addr = NULL;
  417. size_t size = 0;
  418. llama_buffer() = default;
  419. void resize(size_t len) {
  420. #ifdef GGML_USE_METAL
  421. free(addr);
  422. int result = posix_memalign((void **) &addr, getpagesize(), len);
  423. if (result == 0) {
  424. memset(addr, 0, len);
  425. }
  426. else {
  427. addr = NULL;
  428. }
  429. #else
  430. delete[] addr;
  431. addr = new uint8_t[len];
  432. #endif
  433. size = len;
  434. }
  435. ~llama_buffer() {
  436. #ifdef GGML_USE_METAL
  437. free(addr);
  438. #else
  439. delete[] addr;
  440. #endif
  441. addr = NULL;
  442. }
  443. // disable copy and move
  444. llama_buffer(const llama_buffer&) = delete;
  445. llama_buffer(llama_buffer&&) = delete;
  446. llama_buffer& operator=(const llama_buffer&) = delete;
  447. llama_buffer& operator=(llama_buffer&&) = delete;
  448. };
  449. #ifdef GGML_USE_CUBLAS
  450. #include "ggml-cuda.h"
  451. struct llama_ctx_buffer {
  452. uint8_t * addr = NULL;
  453. bool is_cuda;
  454. size_t size = 0;
  455. llama_ctx_buffer() = default;
  456. void resize(size_t size) {
  457. free();
  458. addr = (uint8_t *) ggml_cuda_host_malloc(size);
  459. if (addr) {
  460. is_cuda = true;
  461. }
  462. else {
  463. // fall back to pageable memory
  464. addr = new uint8_t[size];
  465. is_cuda = false;
  466. }
  467. this->size = size;
  468. }
  469. void free() {
  470. if (addr) {
  471. if (is_cuda) {
  472. ggml_cuda_host_free(addr);
  473. }
  474. else {
  475. delete[] addr;
  476. }
  477. }
  478. addr = NULL;
  479. }
  480. ~llama_ctx_buffer() {
  481. free();
  482. }
  483. // disable copy and move
  484. llama_ctx_buffer(const llama_ctx_buffer&) = delete;
  485. llama_ctx_buffer(llama_ctx_buffer&&) = delete;
  486. llama_ctx_buffer& operator=(const llama_ctx_buffer&) = delete;
  487. llama_ctx_buffer& operator=(llama_ctx_buffer&&) = delete;
  488. };
  489. #else
  490. typedef llama_buffer llama_ctx_buffer;
  491. #endif
  492. #endif