Skip to content

Commit

Permalink
llama : sync gguf-llama.cpp with latest llama.cpp (#2608)
Browse files Browse the repository at this point in the history
* llama : sync gguf-llama.cpp with latest llama.cpp

* minor : indentation + assert

* llama : refactor gguf_buffer and gguf_ctx_buffer

* llama : minor
  • Loading branch information
ggerganov authored Aug 14, 2023
1 parent 6f64b6c commit f00780b
Show file tree
Hide file tree
Showing 6 changed files with 688 additions and 459 deletions.
23 changes: 14 additions & 9 deletions examples/gguf/gguf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
#include <sstream>
#include <fstream>
#include <vector>
/*

#undef MIN
#undef MAX
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

template<typename T>
static std::string to_string(const T & val) {
std::stringstream ss;
ss << val;
return ss.str();
}
*/

void gguf_ex_write_str(std::ofstream & fout, const std::string & val) {
const int32_t n = val.size();
fout.write((const char *) &n, sizeof(n));
Expand Down Expand Up @@ -377,28 +382,28 @@ bool gguf_ex_read_2(const std::string & fname) {

struct gguf_file file(fname.c_str(), "rb");
gguf_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);
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;
const float * data = (const float *) cur->data;

printf("%s data[:10] : ", name);

for (int j = 0; j < 10; ++j) {
for (int j = 0; j < MIN(10, ggml_nelements(cur)); ++j) {
printf("%f ", data[j]);
}

printf("\n\n");
}

fprintf(stdout, "%s: ctx_data size: %zu\n", __func__, ggml_get_mem_size(ctx_data));
fprintf(stdout, "%s: ctx_data size: %zu\n", __func__, ggml_get_mem_size(ctx_data));

ggml_free(ctx_data);
gguf_free(ctx);
Expand Down
3 changes: 3 additions & 0 deletions ggml-metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct ggml_metal_context;
struct ggml_metal_context * ggml_metal_init(int n_cb);
void ggml_metal_free(struct ggml_metal_context * ctx);

void * ggml_metal_host_malloc(size_t n);
void ggml_metal_host_free (void * data);

// set the number of command buffers to use
void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb);

Expand Down
15 changes: 15 additions & 0 deletions ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ void ggml_metal_free(struct ggml_metal_context * ctx) {
free(ctx);
}

void * ggml_metal_host_malloc(size_t n) {
void * data = NULL;
const int result = posix_memalign((void **) &data, getpagesize(), n);
if (result != 0) {
fprintf(stderr, "%s: error: posix_memalign failed\n", __func__);
return NULL;
}

return data;
}

void ggml_metal_host_free(void * data) {
free(data);
}

void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
ctx->n_cb = n_cb;
}
Expand Down
Loading

0 comments on commit f00780b

Please sign in to comment.