Team Ai
Datasetpublic

Brunobkr/llama.cpp_AlgMor24_github

ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes3.1kdownloads
ggml-impl.h784 linesDownload Raw Back to src
1#pragma once2 3// GGML internal header4 5#include "ggml.h"6#include "gguf.h"7 8#include <assert.h>9#include <math.h>10#include <stdlib.h> // load `stdlib.h` before other headers to work around MinGW bug: https://sourceforge.net/p/mingw-w64/bugs/192/11#include <stdbool.h>12#include <stdint.h>13#include <string.h>14 15#ifdef __ARM_FEATURE_SVE16#include <arm_sve.h>17#endif // __ARM_FEATURE_SVE18 19#if defined(__ARM_NEON) && !defined(__CUDACC__) && !defined(__MUSACC__)20// if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:21//22//   $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/23//24#include <arm_neon.h>25#endif26 27#ifdef __cplusplus28extern "C" {29#endif30 31void ggml_print_backtrace(void);32 33uint64_t ggml_graph_next_uid(void);34 35#ifndef MIN36#    define MIN(a, b) ((a) < (b) ? (a) : (b))37#endif38 39#ifndef MAX40#    define MAX(a, b) ((a) > (b) ? (a) : (b))41#endif42 43// required for mmap as gguf only guarantees 32-byte alignment44#define TENSOR_ALIGNMENT 3245 46// static_assert should be a #define, but if it's not,47// fall back to the _Static_assert C11 keyword.48// if C99 - static_assert is noop49// ref: https://stackoverflow.com/a/53923785/403997650#ifndef __cplusplus51    #ifndef static_assert52        #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)53            #define static_assert(cond, msg) _Static_assert(cond, msg)54        #else55            #define static_assert(cond, msg) struct global_scope_noop_trick56        #endif57    #endif58#endif59 60static inline int ggml_up32(int n) {61    return (n + 31) & ~31;62}63 64//static inline int ggml_up64(int n) {65//    return (n + 63) & ~63;66//}67 68static inline int ggml_up(int n, int m) {69    // assert m is a power of 270    GGML_ASSERT((m & (m - 1)) == 0);71    return (n + m - 1) & ~(m - 1);72}73 74// TODO: move to ggml.h? (won't be able to inline)75static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {76    if (a->type != b->type) {77        return false;78    }79    for (int i = 0; i < GGML_MAX_DIMS; i++) {80        if (a->ne[i] != b->ne[i]) {81            return false;82        }83        if (a->nb[i] != b->nb[i]) {84            return false;85        }86    }87    return true;88}89 90static bool ggml_op_is_empty(enum ggml_op op) {91    switch (op) {92        case GGML_OP_NONE:93        case GGML_OP_RESHAPE:94        case GGML_OP_TRANSPOSE:95        case GGML_OP_VIEW:96        case GGML_OP_PERMUTE:97            return true;98        default:99            return false;100    }101}102 103static inline bool ggml_impl_is_view(const struct ggml_tensor * t) {104    return t->view_src != NULL;105}106 107static inline float ggml_compute_softplus_f32(float input) {108    return (input > 20.0f) ? input : logf(1 + expf(input));109}110//111// logging112//113 114GGML_ATTRIBUTE_FORMAT(2, 3)115GGML_API void ggml_log_internal        (enum ggml_log_level level, const char * format, ...);116GGML_API void ggml_log_callback_default(enum ggml_log_level level, const char * text, void * user_data);117 118#define GGML_LOG(...)       ggml_log_internal(GGML_LOG_LEVEL_NONE , __VA_ARGS__)119#define GGML_LOG_INFO(...)  ggml_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)120#define GGML_LOG_WARN(...)  ggml_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)121#define GGML_LOG_ERROR(...) ggml_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)122#define GGML_LOG_DEBUG(...) ggml_log_internal(GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)123#define GGML_LOG_CONT(...)  ggml_log_internal(GGML_LOG_LEVEL_CONT , __VA_ARGS__)124 125#define GGML_DEBUG 0126 127#if (GGML_DEBUG >= 1)128#define GGML_PRINT_DEBUG(...) GGML_LOG_DEBUG(__VA_ARGS__)129#else130#define GGML_PRINT_DEBUG(...)131#endif132 133#if (GGML_DEBUG >= 5)134#define GGML_PRINT_DEBUG_5(...) GGML_LOG_DEBUG(__VA_ARGS__)135#else136#define GGML_PRINT_DEBUG_5(...)137#endif138 139#if (GGML_DEBUG >= 10)140#define GGML_PRINT_DEBUG_10(...) GGML_LOG_DEBUG(__VA_ARGS__)141#else142#define GGML_PRINT_DEBUG_10(...)143#endif144 145// tensor params146 147static void ggml_set_op_params(struct ggml_tensor * tensor, const void * params, size_t params_size) {148    GGML_ASSERT(tensor != NULL); // silence -Warray-bounds warnings149    assert(params_size <= GGML_MAX_OP_PARAMS);150    memcpy(tensor->op_params, params, params_size);151}152 153static int32_t ggml_get_op_params_i32(const struct ggml_tensor * tensor, uint32_t i) {154    assert(i < GGML_MAX_OP_PARAMS / sizeof(int32_t));155    return ((const int32_t *)(tensor->op_params))[i];156}157 158static float ggml_get_op_params_f32(const struct ggml_tensor * tensor, uint32_t i) {159    assert(i < GGML_MAX_OP_PARAMS / sizeof(float));160    return ((const float *)(tensor->op_params))[i];161}162 163static void ggml_set_op_params_i32(struct ggml_tensor * tensor, uint32_t i, int32_t value) {164    assert(i < GGML_MAX_OP_PARAMS / sizeof(int32_t));165    ((int32_t *)(tensor->op_params))[i] = value;166}167 168static void ggml_set_op_params_f32(struct ggml_tensor * tensor, uint32_t i, float value) {169    assert(i < GGML_MAX_OP_PARAMS / sizeof(float));170    ((float *)(tensor->op_params))[i] = value;171}172 173struct ggml_map_custom1_op_params {174    ggml_custom1_op_t  fun;175    int                n_tasks;176    void             * userdata;177};178 179struct ggml_map_custom2_op_params {180    ggml_custom2_op_t   fun;181    int                 n_tasks;182    void              * userdata;183};184 185struct ggml_map_custom3_op_params {186    ggml_custom3_op_t fun;187    int               n_tasks;188    void            * userdata;189};190 191struct ggml_custom_op_params {192    ggml_custom_op_t fun;193    int              n_tasks;194    void           * userdata;195};196 197// bitset198 199typedef uint32_t ggml_bitset_t;200 201static_assert(sizeof(ggml_bitset_t) == 4, "bitset_t constants must be updated");202#define BITSET_SHR 5 // log2(sizeof(ggml_bitset_t)*8)203#define BITSET_MASK (sizeof(ggml_bitset_t)*8 - 1)204 205static size_t ggml_bitset_size(size_t n) {206    return (n + BITSET_MASK) >> BITSET_SHR;207}208 209static inline bool ggml_bitset_get(const ggml_bitset_t * bitset, size_t i) {210    return !!(bitset[i >> BITSET_SHR] & (1u << (i & BITSET_MASK)));211}212 213static inline void ggml_bitset_set(ggml_bitset_t * bitset, size_t i) {214    bitset[i >> BITSET_SHR] |= (1u << (i & BITSET_MASK));215}216 217static inline void ggml_bitset_clear(ggml_bitset_t * bitset, size_t i) {218    bitset[i >> BITSET_SHR] &= ~(1u << (i & BITSET_MASK));219}220 221// hash set222 223#define GGML_HASHSET_FULL ((size_t)-1)224#define GGML_HASHSET_ALREADY_EXISTS ((size_t)-2)225 226struct ggml_hash_set {227    size_t size;228    ggml_bitset_t * used;       // whether or not the keys are in use i.e. set229    struct ggml_tensor ** keys; // actual tensors in the set, keys[i] is only defined if ggml_bitset_get(used, i)230};231 232struct ggml_hash_set ggml_hash_set_new(size_t size);233void                 ggml_hash_set_free(struct ggml_hash_set * hash_set);234 235// returns the minimum size for a hash set that can hold min_sz elements236size_t ggml_hash_size(size_t min_sz);237 238// remove all elements from the hash set239void ggml_hash_set_reset(struct ggml_hash_set * hash_set);240 241// returns true if key is in the hash set242static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key);243 244// returns GGML_HASHSET_FULL if table is full, otherwise the current index of the key or where it should be inserted245static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, const struct ggml_tensor * key);246 247// returns GGML_HASHSET_ALREADY_EXISTS if key already exists, index otherwise, asserts if table is full248static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);249 250// return index, asserts if table is full251static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key);252 253// hash function for ggml_tensor254static inline size_t ggml_hash(const struct ggml_tensor * p) {255    // the last 4 bits are always zero due to alignment256    return (size_t)(uintptr_t)p >> 4;257}258 259static size_t ggml_hash_find(const struct ggml_hash_set * hash_set, const struct ggml_tensor * key) {260    size_t h = ggml_hash(key) % hash_set->size;261 262    // linear probing263    size_t i = h;264    while (ggml_bitset_get(hash_set->used, i) && hash_set->keys[i] != key) {265        i = (i + 1) % hash_set->size;266        if (i == h) {267            // visited all hash table entries -> not found268            return GGML_HASHSET_FULL;269        }270    }271    return i;272}273 274static bool ggml_hash_contains(const struct ggml_hash_set * hash_set, struct ggml_tensor * key) {275    size_t i = ggml_hash_find(hash_set, key);276    return i != GGML_HASHSET_FULL && ggml_bitset_get(hash_set->used, i);277}278 279static size_t ggml_hash_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {280    size_t h = ggml_hash(key) % hash_set->size;281 282    // linear probing283    size_t i = h;284    do {285        if (!ggml_bitset_get(hash_set->used, i)) {286            ggml_bitset_set(hash_set->used, i);287            hash_set->keys[i] = key;288            return i;289        }290        if (hash_set->keys[i] == key) {291            return GGML_HASHSET_ALREADY_EXISTS;292        }293        i = (i + 1) % hash_set->size;294    } while (i != h);295 296    // visited all hash table entries -> not found297    GGML_ABORT("fatal error");298}299 300static size_t ggml_hash_find_or_insert(struct ggml_hash_set * hash_set, struct ggml_tensor * key) {301    size_t h = ggml_hash(key) % hash_set->size;302 303    // linear probing304    size_t i = h;305    do {306        if (!ggml_bitset_get(hash_set->used, i)) {307            ggml_bitset_set(hash_set->used, i);308            hash_set->keys[i] = key;309            return i;310        }311        if (hash_set->keys[i] == key) {312            return i;313        }314        i = (i + 1) % hash_set->size;315    } while (i != h);316 317    // visited all hash table entries -> not found318    GGML_ABORT("fatal error");319}320 321// computation graph322 323enum ggml_cgraph_eval_order {324    GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT = 0,325    GGML_CGRAPH_EVAL_ORDER_RIGHT_TO_LEFT,326    GGML_CGRAPH_EVAL_ORDER_COUNT327};328 329struct ggml_cgraph {330    int size;    // maximum number of nodes/leafs/grads/grad_accs331    int n_nodes; // number of nodes currently in use332    int n_leafs; // number of leafs currently in use333 334    struct ggml_tensor ** nodes;     // tensors with data that can change if the graph is evaluated335    struct ggml_tensor ** grads;     // the outputs of these tensors are the gradients of the nodes336    struct ggml_tensor ** grad_accs; // accumulators for node gradients337    struct ggml_tensor ** leafs;     // tensors with constant data338    int32_t             * use_counts;// number of uses of each tensor, indexed by hash table slot339 340    struct ggml_hash_set visited_hash_set;341 342    enum ggml_cgraph_eval_order order;343 344    // an optional identifier that can be utilized to recognize same graphs if two non-zero values match345    // a value of 0 means it is not set and should be ignored346    uint64_t uid;347};348 349// returns a slice of cgraph with nodes [i0, i1)350// the slice does not have leafs or gradients351// if you need the gradients, get them from the original graph352struct ggml_cgraph ggml_graph_view(struct ggml_cgraph * cgraph, int i0, int i1);353 354// ggml-alloc.c: true if the operation can reuse memory from its sources355GGML_API bool ggml_op_can_inplace(enum ggml_op op);356 357 358// Memory allocation359 360GGML_API void * ggml_aligned_malloc(size_t size);361GGML_API void ggml_aligned_free(void * ptr, size_t size);362 363// FP16 <-> FP32364// ref: https://github.com/Maratyszcza/FP16365 366static inline float fp32_from_bits(uint32_t w) {367    union {368        uint32_t as_bits;369        float as_value;370    } fp32;371    fp32.as_bits = w;372    return fp32.as_value;373}374 375static inline uint32_t fp32_to_bits(float f) {376    union {377        float as_value;378        uint32_t as_bits;379    } fp32;380    fp32.as_value = f;381    return fp32.as_bits;382}383 384static inline float ggml_compute_fp16_to_fp32(ggml_fp16_t h) {385    const uint32_t w = (uint32_t) h << 16;386    const uint32_t sign = w & UINT32_C(0x80000000);387    const uint32_t two_w = w + w;388 389    const uint32_t exp_offset = UINT32_C(0xE0) << 23;390#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)391    const float exp_scale = 0x1.0p-112f;392#else393    const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));394#endif395    const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;396 397    const uint32_t magic_mask = UINT32_C(126) << 23;398    const float magic_bias = 0.5f;399    const float denormalized_value = fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;400 401    const uint32_t denormalized_cutoff = UINT32_C(1) << 27;402    const uint32_t result = sign |403        (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value) : fp32_to_bits(normalized_value));404    return fp32_from_bits(result);405}406 407static inline ggml_fp16_t ggml_compute_fp32_to_fp16(float f) {408#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)) && (!defined(__cplusplus) || __cplusplus >= 201703L)409    const float scale_to_inf = 0x1.0p+112f;410    const float scale_to_zero = 0x1.0p-110f;411#else412    const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));413    const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));414#endif415    float base = (fabsf(f) * scale_to_inf) * scale_to_zero;416 417    const uint32_t w = fp32_to_bits(f);418    const uint32_t shl1_w = w + w;419    const uint32_t sign = w & UINT32_C(0x80000000);420    uint32_t bias = shl1_w & UINT32_C(0xFF000000);421    if (bias < UINT32_C(0x71000000)) {422        bias = UINT32_C(0x71000000);423    }424 425    base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;426    const uint32_t bits = fp32_to_bits(base);427    const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);428    const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);429    const uint32_t nonsign = exp_bits + mantissa_bits;430    return (sign >> 16) | (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign);431}432 433#define GGML_COMPUTE_FP16_TO_FP32(x) ggml_compute_fp16_to_fp32(x)434#define GGML_COMPUTE_FP32_TO_FP16(x) ggml_compute_fp32_to_fp16(x)435 436#define GGML_FP16_TO_FP32(x) GGML_COMPUTE_FP16_TO_FP32(x)437#define GGML_FP32_TO_FP16(x) GGML_COMPUTE_FP32_TO_FP16(x)438 439static inline float ggml_e8m0_to_fp32(uint8_t x) {440    uint32_t bits;  // Stores the raw bit representation of the float441 442    // Handle special case for minimum exponent (denormalized float)443    if (x == 0) {444        // Bit pattern for 2^(-127):445        // - Sign bit: 0 (positive)446        // - Exponent: 0 (denormalized number)447        // - Mantissa: 0x400000 (0.5 in fractional form)448        // Value = 0.5 * 2^(-126) = 2^(-127)449        bits = 0x00400000;450    }451    // note: disabled as we don't need to handle NaNs452    //// Handle special case for NaN (all bits set)453    //else if (x == 0xFF) {454    //    // Standard quiet NaN pattern:455    //    // - Sign bit: 0456    //    // - Exponent: all 1s (0xFF)457    //    // - Mantissa: 0x400000 (quiet NaN flag)458    //    bits = 0x7FC00000;459    //}460    // Normalized values (most common case)461    else {462        // Construct normalized float by shifting exponent into position:463        // - Exponent field: 8 bits (positions 30-23)464        // - Mantissa: 0 (implicit leading 1)465        // Value = 2^(x - 127)466        bits = (uint32_t) x << 23;467    }468 469    float result;  // Final float value470                   // Safely reinterpret bit pattern as float without type-punning issues471    memcpy(&result, &bits, sizeof(float));472    return result;473}474 475// Equal to ggml_e8m0_to_fp32/2476// Useful with MXFP4 quantization since the E0M2 values are doubled477static inline float ggml_e8m0_to_fp32_half(uint8_t x) {478    uint32_t bits;479 480    // For x < 2: use precomputed denormal patterns481    if (x < 2) {482        // 0x00200000 = 2^(-128), 0x00400000 = 2^(-127)483        bits = 0x00200000 << x;484    }485    // For x >= 2: normalized exponent adjustment486    else {487        // 0.5 * 2^(x-127) = 2^(x-128) = normalized with exponent (x-1)488        bits = (uint32_t)(x - 1) << 23;489    }490    // Note: NaNs are not handled here491 492    float result;493    memcpy(&result, &bits, sizeof(float));494    return result;495}496 497#define GGML_E8M0_TO_FP32(x) ggml_e8m0_to_fp32(x)498#define GGML_E8M0_TO_FP32_HALF(x) ggml_e8m0_to_fp32_half(x)499 500// UE4M3: unsigned, 4 exp bits (bias=7), 3 mantissa bits501// Returns value * 0.5 to match kvalues_mxfp4 convention (kvalues = 2 * E2M1_float)502static inline float ggml_ue4m3_to_fp32(uint8_t x) {503    if (x == 0 || x == 0x7F) {504        return 0.0f;505    }506    int   exp = (x >> 3) & 0xF;507    int   man = x & 0x7;508    float raw;509    if (exp == 0) {510        raw = ldexpf((float) man, -9);511    } else {512        raw = ldexpf(1.0f + (float) man / 8.0f, exp - 7);513    }514    return raw * 0.5f;515}516 517static inline uint8_t ggml_fp32_to_ue4m3(float x) {518    if (!(x > 0.0f)) {519        return 0;520    }521    if (x > 448.0f) {522        x = 448.0f;523    }524    uint32_t bits;525    memcpy(&bits, &x, 4);526    int fp32_exp  = ((bits >> 23) & 0xFF) - 127;527    int fp32_man  = (bits >> 20) & 0x7;528    int ue4m3_exp = fp32_exp + 7;529    if (ue4m3_exp <= 0) {530        // subnormal: value = man * 2^-9, man = round(x * 2^9)531        int man = (int) (x * 512.0f + 0.5f);532        if (man > 7) {533            man = 7;534        }535        if (man < 1) {536            return 0;537        }538        return (uint8_t) man;539    }540    if (ue4m3_exp >= 15) {541        return 0x7E;542    }543    int round_bit = (bits >> 19) & 1;544    int ue4m3_man = fp32_man + round_bit;545    if (ue4m3_man > 7) {546        ue4m3_man = 0;547        ue4m3_exp++;548        if (ue4m3_exp >= 15) {549            return 0x7E;550        }551    }552    return (uint8_t) ((ue4m3_exp << 3) | ue4m3_man);553}554 555/**556 * Converts brain16 to float32.557 *558 * The bfloat16 floating point format has the following structure:559 *560 *       ┌sign561 *       │562 *       │   ┌exponent563 *       │   │564 *       │   │      ┌mantissa565 *       │   │      │566 *       │┌──┴───┐┌─┴───┐567 *     0b0000000000000000 brain16568 *569 * Since bf16 has the same number of exponent bits as a 32bit float,570 * encoding and decoding numbers becomes relatively straightforward.571 *572 *       ┌sign573 *       │574 *       │   ┌exponent575 *       │   │576 *       │   │      ┌mantissa577 *       │   │      │578 *       │┌──┴───┐┌─┴───────────────────┐579 *     0b00000000000000000000000000000000 IEEE binary32580 *581 * For comparison, the standard fp16 format has fewer exponent bits.582 *583 *       ┌sign584 *       │585 *       │  ┌exponent586 *       │  │587 *       │  │    ┌mantissa588 *       │  │    │589 *       │┌─┴─┐┌─┴──────┐590 *     0b0000000000000000 IEEE binary16591 *592 * @see IEEE 754-2008593 */594static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {595    union {596        float f;597        uint32_t i;598    } u;599    u.i = (uint32_t)h.bits << 16;600    return u.f;601}602 603/**604 * Converts float32 to brain16.605 *606 * This is binary identical with Google Brain float conversion.607 * Floats shall round to nearest even, and NANs shall be quiet.608 * Subnormals aren't flushed to zero, except perhaps when used.609 * This code should vectorize nicely if using modern compilers.610 */611static inline ggml_bf16_t ggml_compute_fp32_to_bf16(float s) {612    ggml_bf16_t h;613    union {614        float f;615        uint32_t i;616    } u;617    u.f = s;618    if ((u.i & 0x7fffffff) > 0x7f800000) { /* nan */619        h.bits = (u.i >> 16) | 64; /* force to quiet */620        return h;621    }622    h.bits = (u.i + (0x7fff + ((u.i >> 16) & 1))) >> 16;623    return h;624}625 626#define GGML_FP32_TO_BF16(x) ggml_compute_fp32_to_bf16(x)627#define GGML_BF16_TO_FP32(x) ggml_compute_bf16_to_fp32(x)628 629static inline int32_t ggml_node_get_use_count(const struct ggml_cgraph * cgraph, int node_idx) {630    const struct ggml_tensor * node = cgraph->nodes[node_idx];631 632    size_t hash_pos = ggml_hash_find(&cgraph->visited_hash_set, node);633    if (!ggml_bitset_get(cgraph->visited_hash_set.used, hash_pos)) {634        return 0;635    }636    return cgraph->use_counts[hash_pos];637}638 639// return true if the node's results are only used by N other nodes640// and can be fused into their calculations.641static inline bool ggml_node_has_n_uses(const struct ggml_cgraph * cgraph, int node_idx, int32_t n_uses) {642    const struct ggml_tensor * node = cgraph->nodes[node_idx];643 644    // check the use count against how many we're replacing645    if (ggml_node_get_use_count(cgraph, node_idx) != n_uses) {646        return false;647    }648 649    // if node is a view, some other node might be using the intermediate result650    // via the view source.651    if (node->view_src) {652        return false;653    }654 655    // If the user requested output for the node, can't fuse656    if (node->flags & GGML_TENSOR_FLAG_OUTPUT) {657        return false;658    }659 660    return true;661}662 663// Returns true if nodes with indices { node_idxs } are the sequence of ggml_ops in ops[]664// and are fusable. Nodes are considered fusable according to this function if:665// - all nodes except the last have only one use and are not views/outputs (see ggml_node_has_N_uses).666// - all nodes except the last are a src of the following node.667// - all nodes are the same shape.668// TODO: Consider allowing GGML_OP_NONE nodes in between669static inline bool ggml_can_fuse_ext(const struct ggml_cgraph * cgraph, const int * node_idxs, const enum ggml_op * ops, int num_ops) {670    for (int i = 0; i < num_ops; ++i) {671        if (node_idxs[i] >= cgraph->n_nodes) {672            return false;673        }674 675        struct ggml_tensor * node = cgraph->nodes[node_idxs[i]];676        if (node->op != ops[i]) {677            return false;678        }679        if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) {680            return false;681        }682        if (i < num_ops - 1 && !ggml_node_has_n_uses(cgraph, node_idxs[i], 1)) {683            return false;684        }685        if (i > 0) {686            struct ggml_tensor * prev = cgraph->nodes[node_idxs[i - 1]];687            if (node->src[0] != prev && node->src[1] != prev) {688                return false;689            }690            if (!ggml_are_same_shape(node, prev)) {691                return false;692            }693        }694    }695    return true;696}697 698// same as above, for sequential indices starting at node_idx699static inline bool ggml_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, const enum ggml_op * ops, int num_ops) {700    assert(num_ops < 32);701 702    if (node_idx + num_ops > cgraph->n_nodes) {703        return false;704    }705 706    int idxs[32];707    for (int i = 0; i < num_ops; ++i) {708        idxs[i] = node_idx + i;709    }710 711    return ggml_can_fuse_ext(cgraph, idxs, ops, num_ops);712}713 714GGML_API bool ggml_can_fuse_subgraph_ext(const struct ggml_cgraph * cgraph,715                                         const int *                node_idxs,716                                         int                        count,717                                         const enum ggml_op *       ops,718                                         const int *                outputs,719                                         int                        num_outputs);720 721// Returns true if the subgraph formed by {node_idxs} can be fused722// checks whethers all nodes which are not part of outputs can be elided723// by checking if their num_uses are confined to the subgraph724static inline bool ggml_can_fuse_subgraph(const struct ggml_cgraph * cgraph,725                                          int                        node_idx,726                                          int                        count,727                                          const enum ggml_op *       ops,728                                          const int *                outputs,729                                          int                        num_outputs) {730    GGML_ASSERT(count < 32);731    if (node_idx + count > cgraph->n_nodes) {732        return false;733    }734 735    int idxs[32];736 737    for (int i = 0; i < count; ++i) {738        idxs[i] = node_idx + i;739    }740 741    return ggml_can_fuse_subgraph_ext(cgraph, idxs, count, ops, outputs, num_outputs);742}743 744#ifdef __cplusplus745}746#endif747 748#ifdef __cplusplus749#include <array>750#include <initializer_list>751#include <vector>752 753// nicer C++ syntax for ggml_can_fuse754inline bool ggml_can_fuse(const struct ggml_cgraph * cgraph, int node_idx, std::initializer_list<enum ggml_op> ops) {755    return ggml_can_fuse(cgraph, node_idx, ops.begin(), (int)ops.size());756}757 758inline bool ggml_can_fuse_subgraph(const struct ggml_cgraph *          cgraph,759                                   int                                 start_idx,760                                   std::initializer_list<enum ggml_op> ops,761                                   std::initializer_list<int>          outputs = {}) {762    return ggml_can_fuse_subgraph(cgraph, start_idx, ops.size(), ops.begin(), outputs.begin(), outputs.size());763}764 765// Return true if the edges in the graph match expectations.766inline bool ggml_check_edges(const struct ggml_cgraph *                cgraph,767                             int                                       start_idx,768                             std::initializer_list<std::array<int, 3>> edges) {769    for (const auto & edge : edges) {770        int dst_node = edge[0];771        int src_idx  = edge[1];772        int src_node = edge[2];773        if (cgraph->nodes[start_idx + dst_node]->src[src_idx] != cgraph->nodes[start_idx + src_node]) {774            return false;775        }776    }777    return true;778}779 780// expose GGUF internals for test code781GGML_API size_t gguf_type_size(enum gguf_type type);782GGML_API void gguf_write_to_buf(const struct gguf_context * ctx, std::vector<int8_t> & buf, bool only_meta);783#endif // __cplusplus784 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai