mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-27 03:44:35 +00:00
gguf : mmap tensor data example
This commit is contained in:
parent
b26f5b2e43
commit
bb42aefaeb
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user