llama-impl.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  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. #pragma once
  27. #include "llama.h"
  28. #include <string>
  29. #include <vector>
  30. #include <stdexcept>
  31. #ifdef __GNUC__
  32. #ifdef __MINGW32__
  33. #define LLAMA_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
  34. #else
  35. #define LLAMA_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
  36. #endif
  37. #else
  38. #define LLAMA_ATTRIBUTE_FORMAT(...)
  39. #endif
  40. //
  41. // logging
  42. //
  43. LLAMA_ATTRIBUTE_FORMAT(2, 3)
  44. void llama_log_internal (ggml_log_level level, const char * format, ...);
  45. void llama_log_callback_default(ggml_log_level level, const char * text, void * user_data);
  46. #define LLAMA_LOG(...) llama_log_internal(GGML_LOG_LEVEL_NONE , __VA_ARGS__)
  47. #define LLAMA_LOG_INFO(...) llama_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)
  48. #define LLAMA_LOG_WARN(...) llama_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)
  49. #define LLAMA_LOG_ERROR(...) llama_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
  50. #define LLAMA_LOG_DEBUG(...) llama_log_internal(GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
  51. #define LLAMA_LOG_CONT(...) llama_log_internal(GGML_LOG_LEVEL_CONT , __VA_ARGS__)
  52. //
  53. // helpers
  54. //
  55. struct time_meas {
  56. time_meas(int64_t & t_acc, bool disable = false) : t_start_us(disable ? -1 : ggml_time_us()), t_acc(t_acc) {}
  57. ~time_meas() {
  58. if (t_start_us >= 0) {
  59. t_acc += ggml_time_us() - t_start_us;
  60. }
  61. }
  62. const int64_t t_start_us;
  63. int64_t & t_acc;
  64. };
  65. static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
  66. if (search.empty()) {
  67. return;
  68. }
  69. std::string builder;
  70. builder.reserve(s.length());
  71. size_t pos = 0;
  72. size_t last_pos = 0;
  73. while ((pos = s.find(search, last_pos)) != std::string::npos) {
  74. builder.append(s, last_pos, pos - last_pos);
  75. builder.append(replace);
  76. last_pos = pos + search.length();
  77. }
  78. builder.append(s, last_pos, std::string::npos);
  79. s = std::move(builder);
  80. }
  81. const std::vector<std::pair<std::string, struct ggml_tensor *>> & llama_internal_get_tensor_map(
  82. struct llama_context * ctx
  83. );
  84. // the ring buffer works similarly to std::deque, but with a fixed capacity
  85. template<typename T>
  86. struct ring_buffer {
  87. ring_buffer(size_t cap) : capacity(cap), data(cap) {}
  88. T & front() {
  89. if (sz == 0) {
  90. throw std::runtime_error("ring buffer is empty");
  91. }
  92. return data[first];
  93. }
  94. const T & front() const {
  95. if (sz == 0) {
  96. throw std::runtime_error("ring buffer is empty");
  97. }
  98. return data[first];
  99. }
  100. T & back() {
  101. if (sz == 0) {
  102. throw std::runtime_error("ring buffer is empty");
  103. }
  104. return data[pos];
  105. }
  106. const T & back() const {
  107. if (sz == 0) {
  108. throw std::runtime_error("ring buffer is empty");
  109. }
  110. return data[pos];
  111. }
  112. void push_back(const T & value) {
  113. if (capacity == 0) {
  114. throw std::runtime_error("ring buffer: capacity is zero");
  115. }
  116. if (sz == capacity) {
  117. // advance the start when buffer is full
  118. first = (first + 1) % capacity;
  119. } else {
  120. sz++;
  121. }
  122. data[pos] = value;
  123. pos = (pos + 1) % capacity;
  124. }
  125. T pop_front() {
  126. if (sz == 0) {
  127. throw std::runtime_error("ring buffer is empty");
  128. }
  129. T value = data[first];
  130. first = (first + 1) % capacity;
  131. sz--;
  132. return value;
  133. }
  134. //T & operator[](size_t i) {
  135. // if (i >= sz) {
  136. // throw std::runtime_error("ring buffer: index out of bounds");
  137. // }
  138. // return data[(first + i) % capacity];
  139. //}
  140. //const T & at(size_t i) const {
  141. // if (i >= sz) {
  142. // throw std::runtime_error("ring buffer: index out of bounds");
  143. // }
  144. // return data[(first + i) % capacity];
  145. //}
  146. const T & rat(size_t i) const {
  147. if (i >= sz) {
  148. throw std::runtime_error("ring buffer: index out of bounds");
  149. }
  150. return data[(first + sz - i - 1) % capacity];
  151. }
  152. std::vector<T> to_vector() const {
  153. std::vector<T> result;
  154. result.reserve(sz);
  155. for (size_t i = 0; i < sz; i++) {
  156. result.push_back(data[(first + i) % capacity]);
  157. }
  158. return result;
  159. }
  160. void clear() {
  161. // here only reset the status of the buffer
  162. sz = 0;
  163. first = 0;
  164. pos = 0;
  165. }
  166. bool empty() const {
  167. return sz == 0;
  168. }
  169. size_t size() const {
  170. return sz;
  171. }
  172. size_t capacity = 0;
  173. size_t sz = 0;
  174. size_t first = 0;
  175. size_t pos = 0;
  176. std::vector<T> data;
  177. };