#include <assert.h> #include <math.h> #include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/ #include <stdbool.h> #include <stdint.h> #include <string.h>
#ifdefined(__ARM_NEON) && !defined(__CUDACC__) && !defined(__MUSACC__) // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example: // // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/ // #include <arm_neon.h> #endif
#ifndef MIN # define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif
#ifndef MAX # define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif
// required for mmap as gguf only guarantees 32-byte alignment #define TENSOR_ALIGNMENT 32
// 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 #ifndef __cplusplus #ifndef static_assert #ifdefined(__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 #endif
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)
};
// returns the minimum size for a hash set that can hold min_sz elements
size_t ggml_hash_size(size_t min_sz);
// remove all elements from the hash set void ggml_hash_set_reset(struct ggml_hash_set * hash_set);
// returns true if key is in the hash set staticbool ggml_hash_contains(conststruct ggml_hash_set * hash_set, struct ggml_tensor * key);
// 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(conststruct ggml_hash_set * hash_set, conststruct 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);
// return index, asserts if table is full static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);
// hash function for ggml_tensor staticinline size_t ggml_hash(conststruct ggml_tensor * p) { // the last 4 bits are always zero due to alignment return (size_t)(uintptr_t)p >> 4;
}
// 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;
}
// 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");
}
// 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");
}
struct ggml_cgraph { int size; // maximum number of nodes/leafs/grads/grad_accs int n_nodes; // number of nodes currently in use int n_leafs; // number of leafs currently in use
struct ggml_tensor ** nodes; // tensors with data that can change if the graph is evaluated struct ggml_tensor ** grads; // the outputs of these tensors are the gradients of the nodes struct ggml_tensor ** grad_accs; // accumulators for node gradients struct ggml_tensor ** leafs; // tensors with constant data
int32_t * use_counts;// number of uses of each tensor, indexed by hash table slot
struct ggml_hash_set visited_hash_set;
enum ggml_cgraph_eval_order order;
};
// returns a slice of cgraph with nodes [i0, i1) // the slice does not have leafs or gradients // if you need the gradients, get them from the original graph struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);
staticinlinefloat ggml_e8m0_to_fp32(uint8_t x) {
uint32_t bits; // Stores the raw bit representation of the float
// Handle special case for minimum exponent (denormalized float) if (x == 0) { // Bit pattern for 2^(-127): // - Sign bit: 0 (positive) // - Exponent: 0 (denormalized number) // - Mantissa: 0x400000 (0.5 in fractional form) // Value = 0.5 * 2^(-126) = 2^(-127)
bits = 0x00400000;
} // note: disabled as we don't need to handle NaNs //// Handle special case for NaN (all bits set) //else if (x == 0xFF) { // // Standard quiet NaN pattern: // // - Sign bit: 0 // // - Exponent: all 1s (0xFF) // // - Mantissa: 0x400000 (quiet NaN flag) // bits = 0x7FC00000; //} // Normalized values (most common case) else { // Construct normalized float by shifting exponent into position: // - Exponent field: 8 bits (positions 30-23) // - Mantissa: 0 (implicit leading 1) // Value = 2^(x - 127)
bits = (uint32_t) x << 23;
}
float result; // Final float value // Safely reinterpret bit pattern as float without type-punning issues
memcpy(&result, &bits, sizeof(float)); return result;
}
// Equal to ggml_e8m0_to_fp32/2 // Useful with MXFP4 quantization since the E0M2 values are doubled staticinlinefloat ggml_e8m0_to_fp32_half(uint8_t x) {
uint32_t bits;
// For x < 2: use precomputed denormal patterns if (x < 2) { // 0x00200000 = 2^(-128), 0x00400000 = 2^(-127)
bits = 0x00200000 << x;
} // For x >= 2: normalized exponent adjustment else { // 0.5 * 2^(x-127) = 2^(x-128) = normalized with exponent (x-1)
bits = (uint32_t)(x - 1) << 23;
} // Note: NaNs are not handled here
// return true if the node's results are only used by N other nodes // and can be fused into their calculations. staticinlinebool ggml_node_has_n_uses(conststruct ggml_cgraph * cgraph, int node_idx, int32_t n_uses) { conststruct ggml_tensor * node = cgraph->nodes[node_idx];
// check the use count against how many we're replacing
size_t hash_pos = ggml_hash_find(&cgraph->visited_hash_set, node); if (!ggml_bitset_get(cgraph->visited_hash_set.used, hash_pos) || cgraph->use_counts[hash_pos] != n_uses) { returnfalse;
}
// if node is a view, some other node might be using the intermediate result // via the view source. if (node->view_src) { returnfalse;
}
// If the user requested output for the node, can't fuse if (node->flags & GGML_TENSOR_FLAG_OUTPUT) { returnfalse;
}
return true;
}
// Returns true if nodes [i, i+ops.size()) are the sequence of ggml_ops in ops[] // and are fusable. Nodes are considered fusable according to this function if: // - all nodes except the last have only one use and are not views/outputs (see ggml_node_has_N_uses). // - all nodes except the last are a src of the following node. // - all nodes are the same shape. // TODO: Consider allowing GGML_OP_NONE nodes in between staticinlinebool ggml_can_fuse(conststruct ggml_cgraph * cgraph, int node_idx, constenum ggml_op * ops, int num_ops) { if (node_idx + num_ops > cgraph->n_nodes) { returnfalse;
}
for (int i = 0; i < num_ops; ++i) { struct ggml_tensor * node = cgraph->nodes[node_idx + i]; if (node->op != ops[i]) { returnfalse;
} if (i < num_ops - 1 && !ggml_node_has_n_uses(cgraph, node_idx + i, 1)) { returnfalse;
} if (i > 0) { struct ggml_tensor * prev = cgraph->nodes[node_idx + i - 1]; if (node->src[0] != prev && node->src[1] != prev) { returnfalse;
} if (!ggml_are_same_shape(node, prev)) { returnfalse;
}
}
} return true;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.