gguf : mmap tensor data example

This commit is contained in:
M. Yusuf Sarıgöz 2023-07-31 17:46:12 +03:00
parent b26f5b2e43
commit bb42aefaeb

View File

@ -1,4 +1,5 @@
#include "ggml.h"
#include "llama-util.h"
#include <cstdio>
#include <cinttypes>
@ -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<char *>(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);