From: Georgi Gerganov Date: Thu, 13 Apr 2023 21:00:42 +0000 (+0300) Subject: mnist : minor fixes and adjustments X-Git-Tag: upstream/0.0.1642~1548 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=c0ca5a3e0bbf60fdbe76f40ede8812623c13afe4;p=pkg%2Fggml%2Fsources%2Fggml mnist : minor fixes and adjustments --- diff --git a/examples/mnist/README.md b/examples/mnist/README.md index d0ab2dba..6b344705 100644 --- a/examples/mnist/README.md +++ b/examples/mnist/README.md @@ -12,7 +12,7 @@ use this to save a pytorch model to be converted to ggml format. ## GGML Format Conversion -GGML "format" is whatever you choose for efficient loading. In our case, we just save the hyperparameters used +GGML "format" is whatever you choose for efficient loading. In our case, we just save the hyperparameters used plus the model weights and biases. Run convert-h5-to-ggml.py to convert your pytorch model. The output format is: - magic constant (int32) @@ -45,3 +45,43 @@ make -j4 mnist ./bin/mnist ../examples/mnist/models/mnist/ggml-model-f32.bin ../examples/mnist/models/mnist/t10k-images.idx3-ubyte For more information, checkout the corresponding programs in the [examples](examples) folder. + +# Sample output + + +``` +$ ./bin/mnist ./models/mnist/ggml-model-f32.bin ../examples/mnist/models/mnist/t10k-images.idx3-ubyte +mnist_model_load: loading model from './models/mnist/ggml-model-f32.bin' +mnist_model_load: ggml ctx size = 1.52 MB +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _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_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +ggml_graph_dump_dot: dot -Tpng mnist.dot -o mnist.dot.png && open mnist.dot.png +Predicted digit is 9 +``` diff --git a/examples/mnist/main.cpp b/examples/mnist/main.cpp index 3414f24a..f73e2e43 100644 --- a/examples/mnist/main.cpp +++ b/examples/mnist/main.cpp @@ -14,7 +14,7 @@ #include #include -// default hparams +// default hparams struct mnist_hparams { int32_t n_input = 784; int32_t n_hidden = 500; @@ -84,6 +84,7 @@ bool mnist_model_load(const std::string & fname, mnist_model & model) { struct ggml_init_params params = { .mem_size = ctx_size + 1024*1024, .mem_buffer = NULL, + .no_alloc = false, }; model.ctx = ggml_init(params); @@ -91,7 +92,7 @@ bool mnist_model_load(const std::string & fname, mnist_model & model) { fprintf(stderr, "%s: ggml_init() failed\n", __func__); return false; } - } + } // Read FC1 layer 1 { @@ -141,8 +142,8 @@ bool mnist_model_load(const std::string & fname, mnist_model & model) { for (int i = 0; i < n_dims; ++i) { fin.read(reinterpret_cast(&ne_bias[i]), sizeof(ne_bias[i])); } - model.fc2_bias = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, model.hparams.n_classes); - fin.read(reinterpret_cast(model.fc2_bias->data), ggml_nbytes(model.fc2_bias)); + model.fc2_bias = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, model.hparams.n_classes); + fin.read(reinterpret_cast(model.fc2_bias->data), ggml_nbytes(model.fc2_bias)); } fin.close(); @@ -162,9 +163,9 @@ int mnist_eval( ) { const auto & hparams = model.hparams; - + static size_t buf_size = hparams.n_input * sizeof(float) * 4; - static void * buf = malloc(buf_size); + static void * buf = malloc(buf_size); struct ggml_init_params params = { .mem_size = buf_size, @@ -180,18 +181,18 @@ int mnist_eval( // fc1 MLP = Ax + b ggml_tensor * fc1 = ggml_add(ctx0, ggml_mul_mat(ctx0, model.fc1_weight, input), model.fc1_bias); ggml_tensor * fc2 = ggml_add(ctx0, ggml_mul_mat(ctx0, model.fc2_weight, ggml_relu(ctx0, fc1)), model.fc2_bias); - + // soft max ggml_tensor * final = ggml_soft_max(ctx0, fc2); - + // run the computation ggml_build_forward_expand(&gf, final); ggml_graph_compute (ctx0, &gf); - ggml_graph_print (&gf); + //ggml_graph_print (&gf); ggml_graph_dump_dot(&gf, NULL, "mnist.dot"); float* finalData = ggml_get_data_f32(final); - + int prediction = std::max_element(finalData, finalData + 10) - finalData; ggml_free(ctx0); return prediction; @@ -223,19 +224,19 @@ int main(int argc, char ** argv) { fprintf(stderr, "%s: failed to open '%s'\n", __func__, argv[2]); return 1; } - + unsigned char buf[784]; srand(time(NULL)); // Seek to a random digit: 16-byte header + 28*28 * (random 0 - 10000) fin.seekg(16 + 784 * (rand() % 10000)); fin.read((char *) &buf, sizeof(buf)); digit.resize(sizeof(buf)); - + // render the digit in ASCII for(int row = 0; row < 28; row++) { for (int col = 0; col < 28; col++) { - fprintf(stderr, "%c ", (float)buf[row*28 + col] > 230 ? '*' : '_'); - digit[row*28+col]=((float)buf[row*28+col]); + fprintf(stderr, "%c ", (float)buf[row*28 + col] > 230 ? '*' : '_'); + digit[row*28+col]=((float)buf[row*28+col]); } fprintf(stderr, "\n"); @@ -245,7 +246,7 @@ int main(int argc, char ** argv) { t_load_us = ggml_time_us() - t_start_us; } - + fprintf(stdout, "Predicted digit is %d\n", mnist_eval(model, 1, digit)); ggml_free(model.ctx); diff --git a/src/ggml.c b/src/ggml.c index 42e3ee31..2f3fea51 100644 --- a/src/ggml.c +++ b/src/ggml.c @@ -3054,9 +3054,11 @@ struct ggml_context * ggml_init(struct ggml_init_params params) { return NULL; } + const size_t mem_size = (params.mem_size + GGML_MEM_ALIGN - 1) & ~(GGML_MEM_ALIGN - 1); + *ctx = (struct ggml_context) { - /*.mem_size =*/ params.mem_size, - /*.mem_buffer =*/ params.mem_buffer ? params.mem_buffer : GGML_ALIGNED_MALLOC(params.mem_size), + /*.mem_size =*/ mem_size, + /*.mem_buffer =*/ params.mem_buffer ? params.mem_buffer : GGML_ALIGNED_MALLOC(mem_size), /*.mem_buffer_owned =*/ params.mem_buffer ? false : true, /*.no_alloc =*/ params.no_alloc, /*.n_objects =*/ 0, @@ -3066,7 +3068,7 @@ struct ggml_context * ggml_init(struct ggml_init_params params) { /*.scratch_save =*/ { 0, 0, NULL, }, }; - GGML_ASSERT(ctx->mem_buffer != NULL); // check for allocation failure + GGML_ASSERT(ctx->mem_buffer != NULL); ggml_assert_aligned(ctx->mem_buffer);