mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-11-04 09:32:00 +00:00 
			
		
		
		
	gguf : mmap tensor data example
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
#include "ggml.h"
 | 
			
		||||
#include "llama-util.h"
 | 
			
		||||
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
@@ -376,6 +377,30 @@ bool gguf_ex_read_2(const std::string & fname) {
 | 
			
		||||
 | 
			
		||||
    // TODO: mmap based on tensor infos
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user