2023-10-30 17:19:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// GGML internal header
|
|
|
|
|
2024-09-16 14:22:07 +00:00
|
|
|
#include "ggml.h"
|
|
|
|
|
2023-10-30 17:19:15 +00:00
|
|
|
#include <assert.h>
|
2024-01-04 08:12:26 +00:00
|
|
|
#include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/
|
2023-10-30 17:19:15 +00:00
|
|
|
#include <stdbool.h>
|
2024-09-16 14:22:07 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2023-10-30 17:19:15 +00:00
|
|
|
|
2024-04-25 12:12:28 +00:00
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
|
|
|
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
2023-10-30 17:19:15 +00:00
|
|
|
// static_assert should be a #define, but if it's not,
|
|
|
|
// fall back to the _Static_assert C11 keyword.
|
|
|
|
// if C99 - static_assert is noop
|
|
|
|
// ref: https://stackoverflow.com/a/53923785/4039976
|
2024-02-05 12:09:47 +00:00
|
|
|
#ifndef __cplusplus
|
2023-10-30 17:19:15 +00:00
|
|
|
#ifndef static_assert
|
|
|
|
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)
|
|
|
|
#define static_assert(cond, msg) _Static_assert(cond, msg)
|
|
|
|
#else
|
|
|
|
#define static_assert(cond, msg) struct global_scope_noop_trick
|
|
|
|
#endif
|
|
|
|
#endif
|
2024-02-05 12:09:47 +00:00
|
|
|
#endif
|
2023-10-30 17:19:15 +00:00
|
|
|
|
2024-07-27 02:41:55 +00:00
|
|
|
// bitset
|
|
|
|
|
2024-09-12 11:23:49 +00:00
|
|
|
typedef uint32_t ggml_bitset_t;
|
|
|
|
|
2024-07-27 02:41:55 +00:00
|
|
|
static_assert(sizeof(ggml_bitset_t) == 4, "bitset_t constants must be updated");
|
|
|
|
#define BITSET_SHR 5 // log2(sizeof(ggml_bitset_t)*8)
|
|
|
|
#define BITSET_MASK (sizeof(ggml_bitset_t)*8 - 1)
|
|
|
|
|
|
|
|
static size_t ggml_bitset_size(size_t n) {
|
|
|
|
return (n + BITSET_MASK) >> BITSET_SHR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ggml_bitset_get(const ggml_bitset_t * bitset, size_t i) {
|
|
|
|
return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ggml_bitset_set(ggml_bitset_t * bitset, size_t i) {
|
|
|
|
bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ggml_bitset_clear(ggml_bitset_t * bitset, size_t i) {
|
|
|
|
bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));
|
|
|
|
}
|
|
|
|
|
|
|
|
// hash set
|
|
|
|
|
|
|
|
#define GGML_HASHSET_FULL ((size_t)-1)
|
|
|
|
#define GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-09-12 11:23:49 +00:00
|
|
|
struct ggml_hash_set {
|
|
|
|
size_t size;
|
|
|
|
ggml_bitset_t * used; // whether or not the keys are in use i.e. set
|
|
|
|
struct ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if ggml_bitset_get(used, i)
|
|
|
|
};
|
|
|
|
|
2024-01-12 19:07:38 +00:00
|
|
|
struct ggml_hash_set ggml_hash_set_new(size_t size);
|
2024-07-27 02:41:55 +00:00
|
|
|
void ggml_hash_set_free(struct ggml_hash_set * hash_set);
|
|
|
|
|
|
|
|
// returns the minimum size for a hash set that can hold min_sz elements
|
|
|
|
size_t ggml_hash_size(size_t min_sz);
|
2024-01-12 19:07:38 +00:00
|
|
|
|
2024-07-27 02:41:55 +00:00
|
|
|
// remove all elements from the hash set
|
|
|
|
void ggml_hash_set_reset(struct ggml_hash_set * hash_set);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-07-27 02:41:55 +00:00
|
|
|
// returns true if key is in the hash set
|
|
|
|
static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
2024-07-27 02:41:55 +00:00
|
|
|
// returns GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted
|
|
|
|
static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);
|
|
|
|
|
|
|
|
// returns GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full
|
|
|
|
static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
|
2023-11-13 12:16:23 +00:00
|
|
|
|
|
|
|
// return index, asserts if table is full
|
2024-07-27 02:41:55 +00:00
|
|
|
static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
|
|
|
|
|
|
|
|
// hash function for ggml_tensor
|
|
|
|
static inline size_t ggml_hash(const struct ggml_tensor * p) {
|
|
|
|
// the last 4 bits are always zero due to alignment
|
|
|
|
return (size_t)(uintptr_t)p >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
|
|
|
|
size_t h = ggml_hash(key) % hash_set->size;
|
|
|
|
|
|
|
|
// linear probing
|
|
|
|
size_t i = h;
|
|
|
|
while (ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {
|
|
|
|
i = (i + 1) % hash_set->size;
|
|
|
|
if (i == h) {
|
|
|
|
// visited all hash table entries -> not found
|
|
|
|
return GGML_HASHSET_FULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
|
|
|
|
size_t i = ggml_hash_find(hash_set, key);
|
|
|
|
return i != GGML_HASHSET_FULL && ggml_bitset_get(hash_set->used, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
|
|
|
|
size_t h = ggml_hash(key) % hash_set->size;
|
|
|
|
|
|
|
|
// linear probing
|
|
|
|
size_t i = h;
|
|
|
|
do {
|
|
|
|
if (!ggml_bitset_get(hash_set->used, i)) {
|
|
|
|
ggml_bitset_set(hash_set->used, i);
|
|
|
|
hash_set->keys[i] = key;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (hash_set->keys[i] == key) {
|
|
|
|
return GGML_HASHSET_ALREADY_EXISTS;
|
|
|
|
}
|
|
|
|
i = (i + 1) % hash_set->size;
|
|
|
|
} while (i != h);
|
|
|
|
|
|
|
|
// visited all hash table entries -> not found
|
|
|
|
GGML_ABORT("fatal error");
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {
|
|
|
|
size_t h = ggml_hash(key) % hash_set->size;
|
|
|
|
|
|
|
|
// linear probing
|
|
|
|
size_t i = h;
|
|
|
|
do {
|
|
|
|
if (!ggml_bitset_get(hash_set->used, i)) {
|
|
|
|
ggml_bitset_set(hash_set->used, i);
|
|
|
|
hash_set->keys[i] = key;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (hash_set->keys[i] == key) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i = (i + 1) % hash_set->size;
|
|
|
|
} while (i != h);
|
|
|
|
|
|
|
|
// visited all hash table entries -> not found
|
|
|
|
GGML_ABORT("fatal error");
|
|
|
|
}
|
2023-10-30 17:19:15 +00:00
|
|
|
|
2024-09-12 11:23:49 +00:00
|
|
|
// computation graph
|
|
|
|
|
2024-09-16 14:22:07 +00:00
|
|
|
enum ggml_cgraph_eval_order {
|
|
|
|
GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,
|
|
|
|
GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,
|
|
|
|
GGML_CGRAPH_EVAL_ORDER_COUNT
|
|
|
|
};
|
|
|
|
|
2024-09-12 11:23:49 +00:00
|
|
|
struct ggml_cgraph {
|
|
|
|
int size;
|
|
|
|
int n_nodes;
|
|
|
|
int n_leafs;
|
|
|
|
|
|
|
|
struct ggml_tensor ** nodes;
|
|
|
|
struct ggml_tensor ** grads;
|
|
|
|
struct ggml_tensor ** leafs;
|
|
|
|
|
|
|
|
struct ggml_hash_set visited_hash_set;
|
|
|
|
|
|
|
|
enum ggml_cgraph_eval_order order;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);
|
|
|
|
|
2023-10-30 17:19:15 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|