ggml-impl.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - 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. // GGML internal header
  28. #include "ggml.h"
  29. #include <assert.h>
  30. #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
  31. #include <stdbool.h>
  32. #include <stdint.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #undef MIN
  37. #undef MAX
  38. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  39. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  40. // static_assert should be a #define, but if it's not,
  41. // fall back to the _Static_assert C11 keyword.
  42. // if C99 - static_assert is noop
  43. // ref: https://stackoverflow.com/a/53923785/4039976
  44. #ifndef __cplusplus
  45. #ifndef static_assert
  46. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
  47. #define static_assert(cond, msg) _Static_assert(cond, msg)
  48. #else
  49. #define static_assert(cond, msg) struct global_scope_noop_trick
  50. #endif
  51. #endif
  52. #endif
  53. // bitset
  54. typedef uint32_t ggml_bitset_t;
  55. static_assert(sizeof(ggml_bitset_t) == 4, "bitset_t constants must be updated");
  56. #define BITSET_SHR 5 // log2(sizeof(ggml_bitset_t)*8)
  57. #define BITSET_MASK (sizeof(ggml_bitset_t)*8 - 1)
  58. static size_t ggml_bitset_size(size_t n) {
  59. return (n + BITSET_MASK) >> BITSET_SHR;
  60. }
  61. static inline bool ggml_bitset_get(const ggml_bitset_t * bitset, size_t i) {
  62. return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
  63. }
  64. static inline void ggml_bitset_set(ggml_bitset_t * bitset, size_t i) {
  65. bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
  66. }
  67. static inline void ggml_bitset_clear(ggml_bitset_t * bitset, size_t i) {
  68. bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
  69. }
  70. // hash set
  71. #define GGML_HASHSET_FULL ((size_t)-1)
  72. #define GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
  73. struct ggml_hash_set {
  74. size_t size;
  75. ggml_bitset_t * used; // whether or not the keys are in use i.e. set
  76. struct ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if ggml_bitset_get(used, i)
  77. };
  78. struct ggml_hash_set ggml_hash_set_new(size_t size);
  79. void ggml_hash_set_free(struct ggml_hash_set * hash_set);
  80. // returns the minimum size for a hash set that can hold min_sz elements
  81. size_t ggml_hash_size(size_t min_sz);
  82. // remove all elements from the hash set
  83. void ggml_hash_set_reset(struct ggml_hash_set * hash_set);
  84. // returns true if key is in the hash set
  85. static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  86. // returns GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
  87. static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  88. // returns GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
  89. static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  90. // return index, asserts if table is full
  91. static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
  92. // hash function for ggml_tensor
  93. static inline size_t ggml_hash(const struct ggml_tensor * p) {
  94. // the last 4 bits are always zero due to alignment
  95. return (size_t)(uintptr_t)p >> 4;
  96. }
  97. static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  98. size_t h = ggml_hash(key) % hash_set->size;
  99. // linear probing
  100. size_t i = h;
  101. while (ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
  102. i = (i + 1) % hash_set->size;
  103. if (i == h) {
  104. // visited all hash table entries -> not found
  105. return GGML_HASHSET_FULL;
  106. }
  107. }
  108. return i;
  109. }
  110. static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  111. size_t i = ggml_hash_find(hash_set, key);
  112. return i != GGML_HASHSET_FULL && ggml_bitset_get(hash_set->used, i);
  113. }
  114. static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  115. size_t h = ggml_hash(key) % hash_set->size;
  116. // linear probing
  117. size_t i = h;
  118. do {
  119. if (!ggml_bitset_get(hash_set->used, i)) {
  120. ggml_bitset_set(hash_set->used, i);
  121. hash_set->keys[i] = key;
  122. return i;
  123. }
  124. if (hash_set->keys[i] == key) {
  125. return GGML_HASHSET_ALREADY_EXISTS;
  126. }
  127. i = (i + 1) % hash_set->size;
  128. } while (i != h);
  129. // visited all hash table entries -> not found
  130. GGML_ABORT("fatal error");
  131. }
  132. static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
  133. size_t h = ggml_hash(key) % hash_set->size;
  134. // linear probing
  135. size_t i = h;
  136. do {
  137. if (!ggml_bitset_get(hash_set->used, i)) {
  138. ggml_bitset_set(hash_set->used, i);
  139. hash_set->keys[i] = key;
  140. return i;
  141. }
  142. if (hash_set->keys[i] == key) {
  143. return i;
  144. }
  145. i = (i + 1) % hash_set->size;
  146. } while (i != h);
  147. // visited all hash table entries -> not found
  148. GGML_ABORT("fatal error");
  149. }
  150. // computation graph
  151. enum ggml_cgraph_eval_order {
  152. GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
  153. GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
  154. GGML_CGRAPH_EVAL_ORDER_COUNT
  155. };
  156. struct ggml_cgraph {
  157. int size;
  158. int n_nodes;
  159. int n_leafs;
  160. struct ggml_tensor ** nodes;
  161. struct ggml_tensor ** grads;
  162. struct ggml_tensor ** leafs;
  163. struct ggml_hash_set visited_hash_set;
  164. enum ggml_cgraph_eval_order order;
  165. };
  166. struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);
  167. #ifdef __cplusplus
  168. }
  169. #endif