Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GGUF: ggml backend support for writing tensor data #1033

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions examples/mnist/mnist-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,6 @@ void mnist_model_train(mnist_model & model, ggml_opt_dataset_t dataset, const in
void mnist_model_save(mnist_model & model, const std::string & fname) {
printf("%s: saving model to '%s'\n", __func__, fname.c_str());

struct ggml_context * ggml_ctx;
{
struct ggml_init_params params = {
/*.mem_size =*/ 100 * 1024*1024,
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ false,
};
ggml_ctx = ggml_init(params);
}

gguf_context * gguf_ctx = gguf_init_empty();
gguf_set_val_str(gguf_ctx, "general.architecture", model.arch.c_str());

Expand All @@ -434,15 +424,11 @@ void mnist_model_save(mnist_model & model, const std::string & fname) {
} else {
GGML_ASSERT(false);
}
for (struct ggml_tensor * t : weights) {
struct ggml_tensor * copy = ggml_dup_tensor(ggml_ctx, t);
ggml_set_name(copy, t->name);
ggml_backend_tensor_get(t, copy->data, 0, ggml_nbytes(t));
gguf_add_tensor(gguf_ctx, copy);
for (struct ggml_tensor * w : weights) {
gguf_add_tensor(gguf_ctx, w);
}
gguf_write_to_file(gguf_ctx, fname.c_str(), false);

ggml_free(ggml_ctx);
gguf_free(gguf_ctx);
}

Expand Down
4 changes: 2 additions & 2 deletions include/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ extern "C" {
GGML_API int gguf_get_n_tensors (const struct gguf_context * ctx);
GGML_API int gguf_find_tensor (const struct gguf_context * ctx, const char * name);
GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int i);
GGML_API char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
GGML_API const char * gguf_get_tensor_name (const struct gguf_context * ctx, int i);
GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int i);

// removes key if it exists
Expand Down Expand Up @@ -2167,7 +2167,7 @@ extern "C" {
// manage tensor info
GGML_API void gguf_add_tensor(struct gguf_context * ctx, const struct ggml_tensor * tensor);
GGML_API void gguf_set_tensor_type(struct gguf_context * ctx, const char * name, enum ggml_type type);
GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data, size_t size);
GGML_API void gguf_set_tensor_data(struct gguf_context * ctx, const char * name, const void * data);

// writing gguf files can be done in 2 ways:
//
Expand Down
Loading
Loading