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
0likes3kdownloads
test-gguf.cpp1458 linesDownload Raw Back to tests
1#include "ggml.h"2#include "ggml-backend.h"3#include "../ggml/src/ggml-impl.h"4#include "gguf.h"5 6#include <algorithm>7#include <array>8#include <cmath>9#include <cstdint>10#include <cstdio>11#include <random>12#include <string>13#include <vector>14 15constexpr int offset_has_kv      = 1000;16constexpr int offset_has_tensors = 2000;17constexpr int offset_has_data    = 3000;18 19enum handcrafted_file_type {20    HANDCRAFTED_HEADER_BAD_MAGIC           =  10,21    HANDCRAFTED_HEADER_BAD_VERSION_0       =  15,22    HANDCRAFTED_HEADER_BAD_VERSION_1       =  20,23    HANDCRAFTED_HEADER_BAD_VERSION_FUTURE  =  30,24    HANDCRAFTED_HEADER_BAD_N_TENSORS       =  40,25    HANDCRAFTED_HEADER_BAD_N_KV            =  50,26    HANDCRAFTED_HEADER_EMPTY               = 800,27 28    HANDCRAFTED_KV_BAD_KEY_SIZE            =  10 + offset_has_kv,29    HANDCRAFTED_KV_EMPTY_KEY               =  15 + offset_has_kv,30    HANDCRAFTED_KV_BAD_TYPE                =  20 + offset_has_kv,31    // HANDCRAFTED_KV_BAD_VALUE_SIZE          =  30 + offset_has_kv, // removed because it can result in allocations > 1 TB (default sanitizer limit)32    HANDCRAFTED_KV_DUPLICATE_KEY           =  40 + offset_has_kv,33    HANDCRAFTED_KV_BAD_ALIGN               =  50 + offset_has_kv,34    HANDCRAFTED_KV_SUCCESS                 = 800 + offset_has_kv,35 36    HANDCRAFTED_TENSORS_BAD_NAME_SIZE      =  10 + offset_has_tensors,37    HANDCRAFTED_TENSORS_BAD_N_DIMS         =  20 + offset_has_tensors,38    HANDCRAFTED_TENSORS_BAD_SHAPE          =  30 + offset_has_tensors,39    HANDCRAFTED_TENSORS_NE_TOO_BIG         =  40 + offset_has_tensors,40    HANDCRAFTED_TENSORS_NBYTES_TOO_BIG     =  45 + offset_has_tensors,41    HANDCRAFTED_TENSORS_BAD_TYPE           =  50 + offset_has_tensors,42    HANDCRAFTED_TENSORS_BAD_OFFSET         =  60 + offset_has_tensors,43    HANDCRAFTED_TENSORS_DUPLICATE_NAME     =  70 + offset_has_tensors,44    HANDCRAFTED_TENSORS_BAD_ALIGN          =  75 + offset_has_tensors,45    HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN =  80 + offset_has_tensors,46    HANDCRAFTED_TENSORS_SUCCESS            = 800 + offset_has_tensors,47    HANDCRAFTED_TENSORS_CUSTOM_ALIGN       = 810 + offset_has_tensors,48 49    HANDCRAFTED_DATA_NOT_ENOUGH_DATA       =  10 + offset_has_data,50    HANDCRAFTED_DATA_BAD_ALIGN             =  15 + offset_has_data,51    HANDCRAFTED_DATA_INCONSISTENT_ALIGN    =  20 + offset_has_data,52    HANDCRAFTED_DATA_MEM_SIZE_OVERFLOW     =  30 + offset_has_data,53    HANDCRAFTED_DATA_SUCCESS               = 800 + offset_has_data,54    HANDCRAFTED_DATA_CUSTOM_ALIGN          = 810 + offset_has_data,55};56 57static std::string handcrafted_file_type_name(const enum handcrafted_file_type hft) {58    switch (hft) {59        case HANDCRAFTED_HEADER_BAD_MAGIC:           return "HEADER_BAD_MAGIC";60        case HANDCRAFTED_HEADER_BAD_VERSION_0:       return "HEADER_BAD_VERSION_0";61        case HANDCRAFTED_HEADER_BAD_VERSION_1:       return "HEADER_BAD_VERSION_1";62        case HANDCRAFTED_HEADER_BAD_VERSION_FUTURE:  return "HEADER_BAD_VERSION_FUTURE";63        case HANDCRAFTED_HEADER_BAD_N_KV:            return "HEADER_BAD_N_KV";64        case HANDCRAFTED_HEADER_BAD_N_TENSORS:       return "HEADER_BAD_N_TENSORS";65        case HANDCRAFTED_HEADER_EMPTY:               return "HEADER_EMPTY";66 67        case HANDCRAFTED_KV_BAD_KEY_SIZE:            return "KV_BAD_KEY_SIZE";68        case HANDCRAFTED_KV_EMPTY_KEY:               return "KV_EMPTY_KEY";69        case HANDCRAFTED_KV_BAD_TYPE:                return "KV_BAD_TYPE";70        case HANDCRAFTED_KV_DUPLICATE_KEY:           return "KV_DUPLICATE_KEY";71        case HANDCRAFTED_KV_BAD_ALIGN:               return "KV_BAD_ALIGN";72        case HANDCRAFTED_KV_SUCCESS:                 return "KV_RANDOM_KV";73 74        case HANDCRAFTED_TENSORS_BAD_NAME_SIZE:      return "TENSORS_BAD_NAME_SIZE";75        case HANDCRAFTED_TENSORS_BAD_N_DIMS:         return "TENSORS_BAD_N_DIMS";76        case HANDCRAFTED_TENSORS_BAD_SHAPE:          return "TENSORS_BAD_SHAPE";77        case HANDCRAFTED_TENSORS_NE_TOO_BIG:         return "TENSORS_NE_TOO_BIG";78        case HANDCRAFTED_TENSORS_NBYTES_TOO_BIG:     return "TENSORS_NBYTES_TOO_BIG";79        case HANDCRAFTED_TENSORS_BAD_TYPE:           return "TENSORS_BAD_TYPE";80        case HANDCRAFTED_TENSORS_BAD_OFFSET:         return "TENSORS_BAD_OFFSET";81        case HANDCRAFTED_TENSORS_DUPLICATE_NAME:     return "TENSORS_DUPLICATE_NAME";82        case HANDCRAFTED_TENSORS_BAD_ALIGN:          return "TENSORS_BAD_ALIGN";83        case HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN: return "TENSORS_INCONSISTENT_ALIGN";84        case HANDCRAFTED_TENSORS_SUCCESS:            return "TENSORS_SUCCESS";85        case HANDCRAFTED_TENSORS_CUSTOM_ALIGN:       return "TENSORS_CUSTOM_ALIGN";86 87        case HANDCRAFTED_DATA_NOT_ENOUGH_DATA:       return "DATA_NOT_ENOUGH_DATA";88        case HANDCRAFTED_DATA_BAD_ALIGN:             return "DATA_BAD_ALIGN";89        case HANDCRAFTED_DATA_INCONSISTENT_ALIGN:    return "DATA_INCONSISTENT_ALIGN";90        case HANDCRAFTED_DATA_MEM_SIZE_OVERFLOW:     return "DATA_MEM_SIZE_OVERFLOW";91        case HANDCRAFTED_DATA_SUCCESS:               return "DATA_SUCCESS";92        case HANDCRAFTED_DATA_CUSTOM_ALIGN:          return "DATA_CUSTOM_ALIGN";93    }94    GGML_ABORT("fatal error");95}96 97static bool expect_context_not_null(const enum handcrafted_file_type hft) {98    if (hft < offset_has_kv) {99        return hft >= HANDCRAFTED_HEADER_EMPTY;100    }101    if (hft < offset_has_tensors) {102        return hft >= HANDCRAFTED_KV_SUCCESS;103    }104    if (hft < offset_has_data) {105        return hft >= HANDCRAFTED_TENSORS_SUCCESS;106    }107    return hft >= HANDCRAFTED_DATA_SUCCESS;108}109 110typedef std::pair<enum ggml_type, std::array<int64_t, GGML_MAX_DIMS>> tensor_config_t;111 112static std::vector<tensor_config_t> get_tensor_configs(std::mt19937 & rng) {113    std::vector<tensor_config_t> tensor_configs;114    tensor_configs.reserve(100);115 116    for (int i = 0; i < 100; ++i) {117        const enum ggml_type type = ggml_type(rng() % GGML_TYPE_COUNT);118        if (ggml_type_size(type) == 0) {119            continue;120        }121 122        std::array<int64_t, GGML_MAX_DIMS> shape = {1, 1, 1, 1};123        shape[0] = (1 + rng() % 10) * ggml_blck_size(type);124        const int n_dims = 1 + rng() % GGML_MAX_DIMS;125        for (int i = 1; i < n_dims; ++i) {126            shape[i] = 1 + rng() % 10;127        }128 129        tensor_configs.push_back(std::make_pair(type, shape));130    }131 132    return tensor_configs;133}134 135static std::vector<std::pair<enum gguf_type, enum gguf_type>> get_kv_types(std::mt19937 rng) {136    std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types;137    kv_types.reserve(100);138 139    for (int i = 0; i < 100; ++i) {140        const gguf_type type = gguf_type(rng() % GGUF_TYPE_COUNT);141 142        if (type == GGUF_TYPE_ARRAY) {143            const gguf_type type_arr = gguf_type(rng() % GGUF_TYPE_COUNT);144            if (type_arr == GGUF_TYPE_ARRAY) {145                continue;146            }147            kv_types.push_back(std::make_pair(type, type_arr));148            continue;149        }150 151        kv_types.push_back(std::make_pair(type, gguf_type(-1)));152    }153    std::shuffle(kv_types.begin(), kv_types.end(), rng);154 155    return kv_types;156}157 158template <typename T>159static void helper_write(FILE * file, const T & val) {160    GGML_ASSERT(fwrite(&val, 1, sizeof(val), file) == sizeof(val));161}162 163static void helper_write(FILE * file, const void * data, const size_t nbytes) {164    GGML_ASSERT(fwrite(data, 1, nbytes, file) == nbytes);165}166 167static std::vector<uint8_t> read_file_to_buffer(FILE * file) {168    GGML_ASSERT(file != nullptr);169    GGML_ASSERT(fseek(file, 0, SEEK_END) == 0);170 171    const long size = ftell(file);172    GGML_ASSERT(size >= 0);173 174    rewind(file);175 176    std::vector<uint8_t> data(static_cast<size_t>(size));177    GGML_ASSERT(fread(data.data(), 1, data.size(), file) == data.size());178 179    rewind(file);180    return data;181}182 183struct callback_reader_data {184    const uint8_t * data;185    size_t size;186};187 188static size_t read_buffer_callback(void * userdata, void * output, uint64_t offset, size_t len) {189    GGML_ASSERT(len > 0);190 191    const callback_reader_data & reader = *static_cast<callback_reader_data *>(userdata);192 193    if (offset > reader.size || len > reader.size - offset) {194        return 0;195    }196 197    const size_t data_offset = static_cast<size_t>(offset);198    const size_t nread = std::min(len, reader.size - data_offset);199    memcpy(static_cast<uint8_t *>(output), reader.data + data_offset, nread);200    return nread;201}202 203static FILE * get_handcrafted_file(const unsigned int seed, const enum handcrafted_file_type hft, const int extra_bytes = 0) {204    FILE * file = tmpfile();205 206    if (!file) {207        return file;208    }209 210    std::mt19937 rng(seed);211    uint32_t alignment = GGUF_DEFAULT_ALIGNMENT;212 213    if (hft == HANDCRAFTED_HEADER_BAD_MAGIC) {214        const char bad_magic[4] = {'F', 'U', 'G', 'G'};215        helper_write(file, bad_magic, sizeof(bad_magic));216    } else {217        helper_write(file, GGUF_MAGIC, 4);218    }219 220    if (hft == HANDCRAFTED_HEADER_BAD_VERSION_0) {221        const uint32_t version = 0;222        helper_write(file, version);223    } else if (hft == HANDCRAFTED_HEADER_BAD_VERSION_1) {224        const uint32_t version = 1;225        helper_write(file, version);226    } else if (hft == HANDCRAFTED_HEADER_BAD_VERSION_FUTURE) {227        const uint32_t version = GGUF_VERSION + 1;228        helper_write(file, version);229    } else {230        const uint32_t version = GGUF_VERSION;231        helper_write(file, version);232    }233 234    std::vector<tensor_config_t> tensor_configs;235    if (hft >= offset_has_tensors) {236        tensor_configs = get_tensor_configs(rng);237    }238 239    if (hft == HANDCRAFTED_DATA_MEM_SIZE_OVERFLOW) {240        tensor_configs.resize(2);241 242        tensor_configs[0] = { GGML_TYPE_I8, { 0x7FFFFFFFFFFFFFC0, 1, 1, 1 } };243        tensor_configs[1] = { GGML_TYPE_I8, { 0x7FFFFFFFFFFFFFC0, 1, 1, 1 } };244    }245 246    if (hft == HANDCRAFTED_HEADER_BAD_N_TENSORS) {247        const uint64_t n_tensors = -1;248        helper_write(file, n_tensors);249    } else {250        const uint64_t n_tensors = tensor_configs.size();251        helper_write(file, n_tensors);252    }253 254    std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types;255    if (hft >= offset_has_kv) {256        kv_types = get_kv_types(rng);257    }258    {259        uint64_t n_kv = kv_types.size();260        if (hft == HANDCRAFTED_KV_BAD_ALIGN      ||261            hft == HANDCRAFTED_TENSORS_BAD_ALIGN || hft == HANDCRAFTED_TENSORS_CUSTOM_ALIGN ||262            hft == HANDCRAFTED_DATA_BAD_ALIGN    || hft == HANDCRAFTED_DATA_CUSTOM_ALIGN) {263 264            n_kv += 1;265        } else if (hft == HANDCRAFTED_HEADER_BAD_N_KV) {266            n_kv = -1;267        }268        helper_write(file, n_kv);269    }270 271    if (hft < offset_has_kv) {272        while (ftell(file) % alignment != 0) {273            const char pad = 0;274            helper_write(file, pad);275        }276 277        for (int i = 0; i < extra_bytes; ++i) {278            const char tmp = 0;279            helper_write(file, tmp);280        }281        rewind(file);282        return file;283    }284 285    for (int i = 0; i < int(kv_types.size()); ++i) {286        const enum gguf_type type     = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].first);287        const enum gguf_type type_arr = gguf_type(hft == HANDCRAFTED_KV_BAD_TYPE ? GGUF_TYPE_COUNT : kv_types[i].second);288 289        const std::string key = hft == HANDCRAFTED_KV_EMPTY_KEY290            ? ""291            : "my_key_" + std::to_string((hft == HANDCRAFTED_KV_DUPLICATE_KEY ? i/2 : i));292 293        if (hft == HANDCRAFTED_KV_BAD_KEY_SIZE) {294            const uint64_t n = -1;295            helper_write(file, n);296        } else {297            const uint64_t n = key.length();298            helper_write(file, n);299        }300        helper_write(file, key.data(), key.length());301 302        {303            const int32_t type32 = int32_t(type);304            helper_write(file, type32);305        }306 307        uint32_t data[16];308        for (int j = 0; j < 16; ++j) {309            data[j] = rng();310            if (type == GGUF_TYPE_STRING || type_arr == GGUF_TYPE_STRING) {311                data[j] |= 0x01010101; // avoid random null-termination of string312            }313        }314 315        if (type == GGUF_TYPE_STRING) {316            const uint64_t n = rng() % sizeof(data);317            helper_write(file, n);318            helper_write(file, data, n);319            continue;320        }321 322        if (type == GGUF_TYPE_ARRAY) {323            {324                const int32_t type32 = int32_t(type_arr);325                helper_write(file, type32);326            }327            if (type_arr == GGUF_TYPE_STRING) {328                const uint64_t nstr = rng() % (16 + 1);329                helper_write(file, nstr);330                for (uint64_t istr = 0; istr < nstr; ++istr) {331                    const uint64_t n = rng() % (sizeof(uint32_t) + 1);332                    helper_write(file, n);333                    helper_write(file, &data[istr], n);334                }335                continue;336            }337            const size_t type_size = gguf_type_size(type_arr);338            const uint64_t n = (rng() % sizeof(data)) / type_size;339            helper_write(file, n);340            helper_write(file, &data, n*type_size);341            continue;342        }343 344        helper_write(file, data, hft == HANDCRAFTED_KV_BAD_TYPE ? 1 : gguf_type_size(type));345    }346 347    if (hft == HANDCRAFTED_KV_BAD_ALIGN      ||348        hft == HANDCRAFTED_TENSORS_BAD_ALIGN || hft == HANDCRAFTED_TENSORS_CUSTOM_ALIGN ||349        hft == HANDCRAFTED_DATA_BAD_ALIGN    || hft == HANDCRAFTED_DATA_CUSTOM_ALIGN) {350 351        const uint64_t n = strlen(GGUF_KEY_GENERAL_ALIGNMENT);352        helper_write(file, n);353        helper_write(file, GGUF_KEY_GENERAL_ALIGNMENT, n);354 355        const int32_t type = gguf_type(GGUF_TYPE_UINT32);356        helper_write(file, type);357 358        alignment = expect_context_not_null(hft) ? 1 : 13;359        helper_write(file, alignment);360    }361 362    if (hft < offset_has_tensors) {363        while (ftell(file) % alignment != 0) {364            const char pad = 0;365            helper_write(file, pad);366        }367 368        for (int i = 0; i < extra_bytes; ++i) {369            const char tmp = 0;370            helper_write(file, tmp);371        }372        rewind(file);373        return file;374    }375 376    if (hft == HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN || hft == HANDCRAFTED_DATA_INCONSISTENT_ALIGN) {377        alignment = 1;378    }379 380    uint64_t offset = 0;381    for (int i = 0; i < int(tensor_configs.size()); ++i) {382        const ggml_type                          type  = hft == HANDCRAFTED_TENSORS_NBYTES_TOO_BIG ? GGML_TYPE_I64 : tensor_configs[i].first;383        const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;384 385        std::string name = "my_tensor";386        if (hft != HANDCRAFTED_TENSORS_DUPLICATE_NAME) {387            name += "_" + std::to_string(i);388        }389        if (hft == HANDCRAFTED_TENSORS_BAD_NAME_SIZE) {390            name += "_with_a_very_long_name_which_is_longer_than_what_is_allowed_for_ggml_tensors";391            GGML_ASSERT(name.length() >= GGML_MAX_NAME);392        }393        {394            const uint64_t n = name.length();395            helper_write(file, n);396        }397        helper_write(file, name.data(), name.length());398 399        uint32_t n_dims = (hft == HANDCRAFTED_TENSORS_NE_TOO_BIG || hft == HANDCRAFTED_TENSORS_NBYTES_TOO_BIG) ? 2 : 1;400        for (int i = GGML_MAX_DIMS-1; i >= 1; --i) {401            if (shape[i] != 1) {402                n_dims = i + 1;403                break;404            }405        }406        if (hft == HANDCRAFTED_TENSORS_BAD_N_DIMS) {407            const uint32_t n_dims_bad = GGML_MAX_DIMS + 1;408            helper_write(file, n_dims_bad);409        } else {410            helper_write(file, n_dims);411        }412 413        if (hft == HANDCRAFTED_TENSORS_BAD_SHAPE) {414            const int64_t bad_dim = -1;415            for (uint32_t j = 0; j < n_dims; ++j) {416                helper_write(file, bad_dim);417            }418        } else if (hft == HANDCRAFTED_TENSORS_NE_TOO_BIG){419            const int64_t big_dim = 4*int64_t(INT32_MAX);420            for (uint32_t j = 0; j < n_dims; ++j) {421                helper_write(file, big_dim);422            }423        } else if (hft == HANDCRAFTED_TENSORS_NBYTES_TOO_BIG){424            const size_t  big_ne  = SIZE_MAX/ggml_type_size(type);425            const int64_t big_dim = GGML_PAD(int64_t(1.01f*std::pow(big_ne, 1.0f/n_dims)) + 1, ggml_blck_size(type));426            for (uint32_t j = 0; j < n_dims; ++j) {427                helper_write(file, big_dim);428            }429        } else {430            helper_write(file, shape.data(), n_dims*sizeof(int64_t));431        }432 433        {434            const int32_t type32 = hft == HANDCRAFTED_TENSORS_BAD_TYPE ? GGML_TYPE_COUNT : int32_t(type);435            helper_write(file, type32);436        }437 438        if (hft == HANDCRAFTED_TENSORS_BAD_OFFSET) {439            const uint64_t bad_offset = -1;440            helper_write(file, bad_offset);441        } else {442            helper_write(file, offset);443        }444 445        int64_t ne = shape[0];446        for (uint32_t i = 1; i < n_dims; ++i) {447            ne *= shape[i];448        }449 450        offset += GGML_PAD(ggml_row_size(type, ne), (uint64_t) alignment);451    }452 453    while (ftell(file) % alignment != 0) {454        const char pad = 0;455        helper_write(file, pad);456    }457 458    if (hft >= offset_has_data) {459        rng.seed(seed + 1);460        uint64_t nbytes = offset;461        if (hft == HANDCRAFTED_DATA_NOT_ENOUGH_DATA) {462            nbytes -= 1;463        }464        if (hft == HANDCRAFTED_DATA_MEM_SIZE_OVERFLOW) {465            nbytes = 32;466        }467        for (uint64_t i = 0; i < nbytes; ++i) {468            const uint8_t random_byte = i % 256;469            helper_write(file, random_byte);470        }471    }472 473    for (int i = 0; i < extra_bytes; ++i) {474        const char tmp = 0;475        helper_write(file, tmp);476    }477    rewind(file);478    return file;479}480 481static bool handcrafted_check_header(const gguf_context * gguf_ctx, const unsigned int seed, const bool has_kv, const bool has_tensors, const bool alignment_defined) {482    if (!gguf_ctx) {483        return false;484    }485 486    std::mt19937 rng(seed);487 488    std::vector<tensor_config_t> tensor_configs;489    if (has_tensors) {490        tensor_configs = get_tensor_configs(rng);491    }492    std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types;493    if (has_kv) {494        kv_types = get_kv_types(rng);495    }496 497    bool ok = true;498 499    if (gguf_get_version(gguf_ctx) != GGUF_VERSION) {500        ok = false;501    }502    if (gguf_get_n_tensors(gguf_ctx) != int(tensor_configs.size())) {503        ok = false;504    }505    if (gguf_get_n_kv(gguf_ctx) != int(alignment_defined ? kv_types.size() + 1 : kv_types.size())) {506        ok = false;507    }508 509    return ok;510}511 512static bool handcrafted_check_kv(const gguf_context * gguf_ctx, const unsigned int seed, const bool has_tensors, const bool alignment_defined) {513    if (!gguf_ctx) {514        return false;515    }516 517    std::mt19937 rng(seed);518 519    std::vector<tensor_config_t> tensor_configs;520    if (has_tensors) {521        tensor_configs = get_tensor_configs(rng);522    }523 524    std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types = get_kv_types(rng);525 526    bool ok = true;527 528    for (int i = 0; i < int(kv_types.size()); ++i) {529        const enum gguf_type type     = gguf_type(kv_types[i].first);530        const enum gguf_type type_arr = gguf_type(kv_types[i].second);531 532        const std::string key = "my_key_" + std::to_string(i);533 534        uint32_t data[16];535        for (int j = 0; j < 16; ++j) {536            data[j] = rng();537            if (type == GGUF_TYPE_STRING || type_arr == GGUF_TYPE_STRING) {538                data[j] |= 0x01010101; // avoid random null-termination of string539            }540        }541 542        const char * data8 = reinterpret_cast<const char *>(data);543        const int id = gguf_find_key(gguf_ctx, key.c_str());544 545        if (type == GGUF_TYPE_STRING) {546            const char * str = gguf_get_val_str(gguf_ctx, id);547            const uint64_t n = strlen(str);548            const uint64_t n_expected = rng() % sizeof(data);549            if (n != n_expected) {550                ok = false;551                continue;552            }553            if (!std::equal(str, str + n, data8)) {554                ok = false;555            }556            continue;557        }558 559        if (type == GGUF_TYPE_ARRAY) {560            const size_t type_size = gguf_type_size(type_arr);561            const uint64_t arr_n = gguf_get_arr_n(gguf_ctx, id);562 563            if (type_arr == GGUF_TYPE_STRING) {564                const uint64_t nstr_expected = rng() % (16 + 1);565                if (arr_n != nstr_expected) {566                    ok = false;567                    continue;568                }569                for (uint64_t istr = 0; istr < nstr_expected; ++istr) {570                    const char * str = gguf_get_arr_str(gguf_ctx, id, istr);571                    const uint64_t n = strlen(str);572                    const uint64_t n_expected = rng() % (sizeof(uint32_t) + 1);573 574                    if (n != n_expected) {575                        ok = false;576                        continue;577                    }578                    const char * str_expected = reinterpret_cast<const char *>(&data[istr]);579                    if (strncmp(str, str_expected, n) != 0) {580                        ok = false;581                        continue;582                    }583                }584                continue;585            }586 587            const uint64_t arr_n_expected = (rng() % sizeof(data)) / type_size;588            if (arr_n != arr_n_expected) {589                ok = false;590                continue;591            }592 593            const char * data_gguf = reinterpret_cast<const char *>(gguf_get_arr_data(gguf_ctx, id));594 595            if (type_arr == GGUF_TYPE_BOOL) {596                for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) {597                    if (bool(data8[arr_i]) != bool(data_gguf[arr_i])) {598                        ok = false;599                    }600                }601                continue;602            }603 604            if (!std::equal(data8, data8 + arr_n*type_size, data_gguf)) {605                ok = false;606            }607            continue;608        }609 610        const char * data_gguf = reinterpret_cast<const char *>(gguf_get_val_data(gguf_ctx, id));611 612        if (type == GGUF_TYPE_BOOL) {613            if (bool(*data8) != bool(*data_gguf)) {614                ok = false;615            }616            continue;617        }618 619        if (!std::equal(data8, data8 + gguf_type_size(type), data_gguf)) {620            ok = false;621        }622    }623 624    const uint32_t expected_alignment = alignment_defined ? 1 : GGUF_DEFAULT_ALIGNMENT;625    if (gguf_get_alignment(gguf_ctx) != expected_alignment) {626        ok = false;627    }628 629    return ok;630}631 632static bool handcrafted_check_tensors(const gguf_context * gguf_ctx, const unsigned int seed) {633    if (!gguf_ctx) {634        return false;635    }636 637    std::mt19937 rng(seed);638 639    std::vector<tensor_config_t> tensor_configs = get_tensor_configs(rng);640 641    // Call get_kv_types to get the same RNG state:642    get_kv_types(rng);643 644    bool ok = true;645 646    const int id_alignment = gguf_find_key(gguf_ctx, GGUF_KEY_GENERAL_ALIGNMENT);647    const uint32_t alignment = id_alignment >= 0 ? gguf_get_val_u32(gguf_ctx, id_alignment) : GGUF_DEFAULT_ALIGNMENT;648 649    uint64_t expected_offset = 0;650    for (int i = 0; i < int(tensor_configs.size()); ++i) {651        const ggml_type                          type  = tensor_configs[i].first;652        const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;653 654        const std::string name = "my_tensor_" + std::to_string(i);655        const int id = gguf_find_tensor(gguf_ctx, name.c_str());656 657        if (id >= 0) {658            if (std::string(gguf_get_tensor_name(gguf_ctx, id)) != name) {659                ok = false;660            }661 662            if (gguf_get_tensor_type(gguf_ctx, id) != type) {663                ok = false;664            }665 666            const int64_t * ne = gguf_get_tensor_ne(gguf_ctx, id);667            for (int j = 0; j < GGML_MAX_DIMS; ++j) {668                if (ne[j] != shape[j]) {669                    ok = false;670                }671            }672        } else {673            ok = false;674            continue;675        }676 677        const size_t offset = gguf_get_tensor_offset(gguf_ctx, id);678 679        if (offset != expected_offset) {680            ok = false;681        }682 683        int64_t ne = shape[0];684        for (size_t j = 1; j < GGML_MAX_DIMS; ++j) {685            ne *= shape[j];686        }687        expected_offset += GGML_PAD(ggml_row_size(type, ne), alignment);688    }689 690    return ok;691}692 693static bool handcrafted_check_tensor_data(const gguf_context * gguf_ctx, const unsigned int seed, FILE * file) {694    if (!gguf_ctx) {695        return false;696    }697 698    std::mt19937 rng(seed);699 700    std::vector<tensor_config_t> tensor_configs = get_tensor_configs(rng);701 702    bool ok = true;703 704    for (int i = 0; i < int(tensor_configs.size()); ++i) {705        const ggml_type                          type  = tensor_configs[i].first;706        const std::array<int64_t, GGML_MAX_DIMS> shape = tensor_configs[i].second;707 708        int64_t ne = shape[0];709        for (size_t j = 1; j < GGML_MAX_DIMS; ++j) {710            ne *= shape[j];711        }712        const size_t size = ggml_row_size(type, ne);713 714        const std::string name = "my_tensor_" + std::to_string(i);715        const size_t offset = gguf_get_tensor_offset(gguf_ctx, gguf_find_tensor(gguf_ctx, name.c_str()));716 717        std::vector<uint8_t> data(size);718        GGML_ASSERT(fseek(file, gguf_get_data_offset(gguf_ctx) + offset, SEEK_SET) == 0);719        GGML_ASSERT(fread(data.data(), 1, data.size(), file) == data.size());720 721        for (size_t j = 0; j < size; ++j) {722            const uint8_t expected_byte = (j + offset) % 256;723            if (data[j] != expected_byte) {724                ok = false;725            }726        }727    }728 729    return ok;730}731 732static std::pair<int, int> test_handcrafted_file(const unsigned int seed) {733    int npass = 0;734    int ntest = 0;735 736    const std::vector<handcrafted_file_type> hfts = {737        HANDCRAFTED_HEADER_BAD_MAGIC,738        HANDCRAFTED_HEADER_BAD_VERSION_0,739        HANDCRAFTED_HEADER_BAD_VERSION_1,740        HANDCRAFTED_HEADER_BAD_VERSION_FUTURE,741        HANDCRAFTED_HEADER_BAD_N_KV,742        HANDCRAFTED_HEADER_BAD_N_TENSORS,743        HANDCRAFTED_HEADER_EMPTY,744 745        HANDCRAFTED_KV_BAD_KEY_SIZE,746        HANDCRAFTED_KV_EMPTY_KEY,747        HANDCRAFTED_KV_BAD_TYPE,748        HANDCRAFTED_KV_DUPLICATE_KEY,749        HANDCRAFTED_KV_BAD_ALIGN,750        HANDCRAFTED_KV_SUCCESS,751 752        HANDCRAFTED_TENSORS_BAD_NAME_SIZE,753        HANDCRAFTED_TENSORS_BAD_N_DIMS,754        HANDCRAFTED_TENSORS_BAD_SHAPE,755        HANDCRAFTED_TENSORS_NE_TOO_BIG,756        HANDCRAFTED_TENSORS_NBYTES_TOO_BIG,757        HANDCRAFTED_TENSORS_BAD_TYPE,758        HANDCRAFTED_TENSORS_BAD_OFFSET,759        HANDCRAFTED_TENSORS_DUPLICATE_NAME,760        HANDCRAFTED_TENSORS_BAD_ALIGN,761        HANDCRAFTED_TENSORS_INCONSISTENT_ALIGN,762        HANDCRAFTED_TENSORS_SUCCESS,763        HANDCRAFTED_TENSORS_CUSTOM_ALIGN,764 765        HANDCRAFTED_DATA_NOT_ENOUGH_DATA,766        HANDCRAFTED_DATA_BAD_ALIGN,767        HANDCRAFTED_DATA_INCONSISTENT_ALIGN,768        HANDCRAFTED_DATA_MEM_SIZE_OVERFLOW,769        HANDCRAFTED_DATA_SUCCESS,770        HANDCRAFTED_DATA_CUSTOM_ALIGN,771    };772 773    for (enum handcrafted_file_type hft : hfts) {774        printf("%s: handcrafted_file_type=%s\n", __func__, handcrafted_file_type_name(hft).c_str());775        FILE * file = get_handcrafted_file(seed, hft);776 777#ifdef _WIN32778        if (!file) {779            printf("failed to create tmpfile(), needs elevated privileges on Windows");780            printf("skipping tests");781            continue;782        }783#else784        GGML_ASSERT(file);785#endif // _WIN32786 787        struct ggml_context * ctx = nullptr;788        struct gguf_init_params gguf_params = {789            /*no_alloc =*/ false,790            /*ctx      =*/ hft >= offset_has_data ? &ctx : nullptr,791        };792 793        struct gguf_context * gguf_ctx = gguf_init_from_file_ptr(file, gguf_params);794 795        if (expect_context_not_null(hft)) {796            printf("%s:   - context_not_null: ", __func__);797        } else {798            printf("%s:   - context_null: ", __func__);799        }800        if (bool(gguf_ctx) == expect_context_not_null(hft)) {801            printf("\033[1;32mOK\033[0m\n");802            npass++;803        } else {804            printf("\033[1;31mFAIL\033[0m\n");805        }806        ntest++;807 808        if (hft >= offset_has_data && !expect_context_not_null(hft)) {809            printf("%s:   - no_dangling_ggml_context_pointer: ", __func__);810            if (ctx) {811                printf("\033[1;31mFAIL\033[0m\n");812            } else {813                printf("\033[1;32mOK\033[0m\n");814                npass++;815            }816            ntest++;817        }818 819        const bool alignment_defined = hft == HANDCRAFTED_TENSORS_CUSTOM_ALIGN || hft == HANDCRAFTED_DATA_CUSTOM_ALIGN;820 821        if (expect_context_not_null(hft)) {822            printf("%s:   - check_header: ", __func__);823            if (handcrafted_check_header(gguf_ctx, seed, hft >= offset_has_kv, hft >= offset_has_tensors, alignment_defined)) {824                printf("\033[1;32mOK\033[0m\n");825                npass++;826            } else {827                printf("\033[1;31mFAIL\033[0m\n");828            }829            ntest++;830        }831 832        if (expect_context_not_null(hft) && hft >= offset_has_kv) {833            printf("%s:   - check_kv: ", __func__);834            if (handcrafted_check_kv(gguf_ctx, seed, hft >= offset_has_tensors, alignment_defined)) {835                printf("\033[1;32mOK\033[0m\n");836                npass++;837            } else {838                printf("\033[1;31mFAIL\033[0m\n");839            }840            ntest++;841        }842 843        if (expect_context_not_null(hft) && hft >= offset_has_tensors) {844            printf("%s:   - check_tensors: ", __func__);845            if (handcrafted_check_tensors(gguf_ctx, seed)) {846                printf("\033[1;32mOK\033[0m\n");847                npass++;848            } else {849                printf("\033[1;31mFAIL\033[0m\n");850            }851            ntest++;852        }853 854        if (expect_context_not_null(hft) && hft >= offset_has_data) {855            printf("%s:   - check_tensor_data: ", __func__);856            if (handcrafted_check_tensor_data(gguf_ctx, seed, file)) {857                printf("\033[1;32mOK\033[0m\n");858                npass++;859            } else {860                printf("\033[1;31mFAIL\033[0m\n");861            }862            ntest++;863        }864 865        fclose(file);866        if (gguf_ctx) {867            ggml_free(ctx);868            gguf_free(gguf_ctx);869        }870        printf("\n");871    }872 873 874    return std::make_pair(npass, ntest);875}876 877struct random_gguf_context_result {878    struct gguf_context * gguf_ctx;879    struct ggml_context * ctx;880    ggml_backend_buffer_t buffer;881};882 883static struct random_gguf_context_result get_random_gguf_context(ggml_backend_t backend, const unsigned int seed) {884    std::mt19937 rng(seed);885 886    struct gguf_context * gguf_ctx = gguf_init_empty();887 888    for (int i = 0; i < 256; ++i) {889        const std::string key = "my_key_" + std::to_string(rng() % 1024);890        const enum gguf_type type = gguf_type(rng() % GGUF_TYPE_COUNT);891 892        switch (type) {893            case GGUF_TYPE_UINT8:   gguf_set_val_u8  (gguf_ctx, key.c_str(), rng() % (1 <<  7));             break;894            case GGUF_TYPE_INT8:    gguf_set_val_i8  (gguf_ctx, key.c_str(), rng() % (1 <<  7) - (1 <<  6)); break;895            case GGUF_TYPE_UINT16:  gguf_set_val_u16 (gguf_ctx, key.c_str(), rng() % (1 << 15));             break;896            case GGUF_TYPE_INT16:   gguf_set_val_i16 (gguf_ctx, key.c_str(), rng() % (1 << 15) - (1 << 14)); break;897            case GGUF_TYPE_UINT32:  gguf_set_val_u32 (gguf_ctx, key.c_str(), rng());                         break;898            case GGUF_TYPE_INT32:   gguf_set_val_i32 (gguf_ctx, key.c_str(), rng()             - (1 << 30)); break;899            case GGUF_TYPE_FLOAT32: gguf_set_val_f32 (gguf_ctx, key.c_str(), rng() % 1024      - 512);       break;900            case GGUF_TYPE_BOOL:    gguf_set_val_bool(gguf_ctx, key.c_str(), rng() % 2 == 0);                break;901            case GGUF_TYPE_STRING:  gguf_set_val_str (gguf_ctx, key.c_str(), std::to_string(rng()).c_str()); break;902            case GGUF_TYPE_UINT64:  gguf_set_val_u64 (gguf_ctx, key.c_str(), rng());                         break;903            case GGUF_TYPE_INT64:   gguf_set_val_i64 (gguf_ctx, key.c_str(), rng()             - (1 << 30)); break;904            case GGUF_TYPE_FLOAT64: gguf_set_val_f32 (gguf_ctx, key.c_str(), rng() % 1024      - 512);       break;905            case GGUF_TYPE_ARRAY: {906                const enum gguf_type type_arr = gguf_type(rng() % GGUF_TYPE_COUNT);907                const uint64_t ne = rng() % 1024;908 909                switch (type_arr) {910                    case GGUF_TYPE_UINT8:911                    case GGUF_TYPE_INT8:912                    case GGUF_TYPE_UINT16:913                    case GGUF_TYPE_INT16:914                    case GGUF_TYPE_UINT32:915                    case GGUF_TYPE_INT32:916                    case GGUF_TYPE_FLOAT32:917                    case GGUF_TYPE_BOOL:918                    case GGUF_TYPE_UINT64:919                    case GGUF_TYPE_INT64:920                    case GGUF_TYPE_FLOAT64: {921                        const size_t nbytes = ne*gguf_type_size(type_arr);922                        std::vector<uint32_t> random_data((nbytes + sizeof(uint32_t) - 1) / sizeof(uint32_t));923                        for (size_t j = 0; j < random_data.size(); ++j) {924                            random_data[j] = rng();925                            if (type_arr == GGUF_TYPE_BOOL) {926                                random_data[j] &= 0x01010101; // the sanitizer complains if booleans are not 0 or 1927                            }928                        }929                        gguf_set_arr_data(gguf_ctx, key.c_str(), type_arr, random_data.data(), ne);930                    } break;931                    case GGUF_TYPE_STRING: {932                        std::vector<std::string>  data_cpp(ne);933                        std::vector<const char *> data_c(ne);934                        for (size_t j = 0; j < data_cpp.size(); ++j) {935                            data_cpp[j] = std::to_string(rng());936                            data_c[j]   = data_cpp[j].c_str();937                        }938                        gguf_set_arr_str(gguf_ctx, key.c_str(), data_c.data(), ne);939                    } break;940                    case GGUF_TYPE_ARRAY: {941                        break; // not supported942                    }943                    case GGUF_TYPE_COUNT:944                    default: {945                        GGML_ABORT("fatal error");946                    }947                }948            } break;949            case GGUF_TYPE_COUNT:950            default: {951                GGML_ABORT("fatal error");952            }953        }954    }955 956    struct ggml_init_params ggml_params = {957        /*.mem_size   =*/ 256*ggml_tensor_overhead(),958        /*.mem_buffer =*/ nullptr,959        /*.no_alloc   =*/ true,960    };961    struct ggml_context * ctx = ggml_init(ggml_params);962 963    for (int i = 0; i < 256; ++i) {964        const std::string name = "my_tensor_" + std::to_string(i);965        const enum ggml_type type = ggml_type(rng() % GGML_TYPE_COUNT);966        const size_t type_size = ggml_type_size(type);967 968        if (type_size == 0) {969            continue;970        }971 972        const int n_dims = 1 + rng() % GGML_MAX_DIMS;973        int64_t ne[GGML_MAX_DIMS];974        ne[0] = (1 + rng() % 10) * ggml_blck_size(type);975        for (int j = 1; j < n_dims; ++j) {976            ne[j] = 1 + rng() % 10;977        }978 979        struct ggml_tensor * tensor = ggml_new_tensor(ctx, type, n_dims, ne);980        ggml_set_name(tensor, name.c_str());981    }982 983    ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx, backend);984    for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {985        const size_t nbytes = ggml_nbytes(t);986        std::vector<uint32_t> random_data((nbytes + sizeof(uint32_t) - 1) / sizeof(uint32_t));987        for (size_t j = 0; j < random_data.size(); ++j) {988            random_data[j] = rng();989        }990        ggml_backend_tensor_set(t, random_data.data(), 0, nbytes);991 992        gguf_add_tensor(gguf_ctx, t);993    }994 995    return {gguf_ctx, ctx, buf};996}997 998static bool all_kv_in_other(const gguf_context * ctx, const gguf_context * other) {999    bool ok = true;1000 1001    const int n_kv = gguf_get_n_kv(ctx);1002    for (int id = 0; id < n_kv; ++id) {1003        const char * name = gguf_get_key(ctx, id);1004 1005        const int idx_other = gguf_find_key(other, name);1006        if (idx_other < 0) {1007            ok = false;1008            continue;1009        }1010 1011        const gguf_type type = gguf_get_kv_type(ctx, id);1012        if (type != gguf_get_kv_type(other, idx_other)) {1013            ok = false;1014            continue;1015        }1016 1017        if (type == GGUF_TYPE_ARRAY) {1018            const size_t arr_n = gguf_get_arr_n(ctx, id);1019            if (arr_n != gguf_get_arr_n(other, idx_other)) {1020                ok = false;1021                continue;1022            }1023 1024            const gguf_type type_arr = gguf_get_arr_type(ctx, id);1025            if (type_arr != gguf_get_arr_type(other, idx_other)) {1026                ok = false;1027                continue;1028            }1029 1030            if (type_arr == GGUF_TYPE_BOOL) {1031                const int8_t * data       = reinterpret_cast<const int8_t *>(gguf_get_arr_data(ctx,   id));1032                const int8_t * data_other = reinterpret_cast<const int8_t *>(gguf_get_arr_data(other, idx_other));1033                for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) {1034                    if (bool(data[arr_i]) != bool(data_other[arr_i])) {1035                        ok = false;1036                    }1037                }1038                continue;1039            }1040 1041            if (type_arr == GGUF_TYPE_STRING) {1042                for (size_t arr_i = 0; arr_i < arr_n; ++arr_i) {1043                    const std::string str       = gguf_get_arr_str(ctx,   id,       arr_i);1044                    const std::string str_other = gguf_get_arr_str(other, idx_other, arr_i);1045                    if (str != str_other) {1046                        ok = false;1047                    }1048                }1049                continue;1050            }1051 1052            const int8_t * data       = reinterpret_cast<const int8_t *>(gguf_get_arr_data(ctx,   id));1053            const int8_t * data_other = reinterpret_cast<const int8_t *>(gguf_get_arr_data(other, idx_other));1054            if (!std::equal(data, data + arr_n*gguf_type_size(type_arr), data_other)) {1055                ok = false;1056            }1057            continue;1058        }1059 1060        if (type == GGUF_TYPE_STRING) {1061            const std::string str       = gguf_get_val_str(ctx,   id);1062            const std::string str_other = gguf_get_val_str(other, idx_other);1063            if (str != str_other) {1064                ok = false;1065            }1066            continue;1067        }1068 1069        const char * data       = reinterpret_cast<const char *>(gguf_get_val_data(ctx,   id));1070        const char * data_other = reinterpret_cast<const char *>(gguf_get_val_data(other, idx_other));1071        if (!std::equal(data, data + gguf_type_size(type), data_other)) {1072            ok = false;1073        }1074    }1075 1076    return ok;1077}1078 1079static bool all_tensors_in_other(const gguf_context * ctx, const gguf_context * other) {1080    bool ok = true;1081 1082    const int n_tensors = gguf_get_n_tensors(ctx);1083    for (int id = 0; id < n_tensors; ++id) {1084        const std::string name = gguf_get_tensor_name(ctx, id);1085 1086        const int idx_other = gguf_find_tensor(other, name.c_str());1087        if (id != idx_other) {1088            ok = false;1089            if (idx_other < 0) {1090                continue;1091            }1092        }1093 1094        const ggml_type type = gguf_get_tensor_type(ctx, id);1095        if (type != gguf_get_tensor_type(other, id)) {1096            ok = false;1097        }1098 1099        const size_t offset = gguf_get_tensor_offset(ctx, id);1100        if (offset != gguf_get_tensor_offset(other, id)) {1101            ok = false;1102        }1103    }1104 1105    return ok;1106}1107 1108static bool same_tensor_data(const struct ggml_context * orig, const struct ggml_context * read) {1109    bool ok = true;1110 1111    struct ggml_tensor * t_orig = ggml_get_first_tensor(orig);1112    struct ggml_tensor * t_read = ggml_get_first_tensor(read);1113 1114    if (std::string(t_read->name) != "GGUF tensor data binary blob") {1115        return false;1116    }1117    t_read = ggml_get_next_tensor(read, t_read);1118 1119    while (t_orig) {1120        if (!t_read) {1121            ok = false;1122            break;1123        }1124 1125        const size_t nbytes = ggml_nbytes(t_orig);1126        if (ggml_nbytes(t_read) != nbytes) {1127            ok = false;1128            break;1129        }1130        std::vector<char> data_orig(nbytes);1131        ggml_backend_tensor_get(t_orig, data_orig.data(), 0, nbytes);1132        if (!std::equal(data_orig.data(), data_orig.data() + nbytes, reinterpret_cast<const char *>(t_read->data))) {1133            ok = false;1134        }1135 1136        t_orig = ggml_get_next_tensor(orig, t_orig);1137        t_read = ggml_get_next_tensor(read, t_read);1138    }1139    if (t_read) {1140        ok = false;1141    }1142 1143    return ok;1144}1145 1146enum roundtrip_read_mode {1147    ROUNDTRIP_READ_MODE_FILE,1148    ROUNDTRIP_READ_MODE_BUFFER,1149    ROUNDTRIP_READ_MODE_CALLBACK,1150};1151 1152static const char * roundtrip_read_mode_name(const roundtrip_read_mode mode) {1153    switch (mode) {1154        case ROUNDTRIP_READ_MODE_FILE:     return "file";1155        case ROUNDTRIP_READ_MODE_BUFFER:   return "buffer";1156        case ROUNDTRIP_READ_MODE_CALLBACK: return "callback";1157    }1158 1159    GGML_ABORT("fatal error");1160}1161 1162static std::pair<int, int> test_roundtrip(1163        ggml_backend_dev_t dev, const unsigned int seed, const bool only_meta,1164        const roundtrip_read_mode read_mode) {1165    ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);1166    printf("%s: device=%s, backend=%s, only_meta=%s, read_mode=%s\n",1167        __func__, ggml_backend_dev_description(dev), ggml_backend_name(backend),1168        only_meta ? "yes" : "no", roundtrip_read_mode_name(read_mode));1169 1170    int npass = 0;1171    int ntest = 0;1172 1173    struct gguf_context * gguf_ctx_0;1174    struct ggml_context * ctx_0;1175    ggml_backend_buffer_t bbuf;1176    {1177        struct random_gguf_context_result result = get_random_gguf_context(backend, seed);1178        gguf_ctx_0 = result.gguf_ctx;1179        ctx_0      = result.ctx;1180        bbuf       = result.buffer;1181    }1182 1183    FILE * file = tmpfile();1184 1185#ifdef _WIN321186    if (!file) {1187        printf("failed to create tmpfile(), needs elevated privileges on Windows");1188        printf("skipping tests");1189        return std::make_pair(0, 0);1190    }1191#else1192    GGML_ASSERT(file);1193#endif // _WIN321194 1195    gguf_write_to_file_ptr(gguf_ctx_0, file, only_meta);1196    rewind(file);1197 1198    struct ggml_context * ctx_1 = nullptr;1199    struct gguf_init_params gguf_params = {1200        /*no_alloc =*/ false,

Showing the first 1,200 of 1458 lines. Download the file for the rest.

Brunobkr/llama.cpp_AlgMor24_github · Team Ai