From bb42aefaeb115a882d839a3c658ab119b692e075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Yusuf=20Sar=C4=B1g=C3=B6z?= Date: Mon, 31 Jul 2023 17:46:12 +0300 Subject: [PATCH] gguf : mmap tensor data example --- examples/gguf/gguf.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/examples/gguf/gguf.cpp b/examples/gguf/gguf.cpp index 84388c219..deb4a8359 100644 --- a/examples/gguf/gguf.cpp +++ b/examples/gguf/gguf.cpp @@ -1,4 +1,5 @@ #include "ggml.h" +#include "llama-util.h" #include #include @@ -376,7 +377,31 @@ bool gguf_ex_read_2(const std::string & fname) { // TODO: mmap based on tensor infos - fprintf(stdout, "%s: ctx_data size: %zu\n", __func__, ggml_get_mem_size(ctx_data)); + + struct llama_file file(fname.c_str(), "rb"); + llama_mmap data_mmap(&file, 0, false); + const int n_tensors = gguf_get_n_tensors(ctx); + + for (int i = 0; i < n_tensors; ++i) { + const char * name = gguf_get_tensor_name(ctx, i); + const size_t offset = gguf_get_data_offset(ctx) + gguf_get_tensor_offset(ctx, i); + struct ggml_tensor * cur = ggml_get_tensor(ctx_data, name); + + cur->data = static_cast(data_mmap.addr) + offset; + + // print first 10 elements + const float * data = (const float *) cur->data; + + printf("%s data[:10] : ", name); + + for (int j = 0; j < 10; ++j) { + printf("%f ", data[j]); + } + + printf("\n\n"); + } + +fprintf(stdout, "%s: ctx_data size: %zu\n", __func__, ggml_get_mem_size(ctx_data)); ggml_free(ctx_data); gguf_free(ctx);