Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Ggml Mnist model build

From Leeroopedia

ML_Training Model_Architecture GGML 2025-05-15 12:00 GMT

Summary

Builds the forward computation graph for either a fully-connected or convolutional MNIST model by wiring the previously-initialised weight tensors into a sequence of GGML tensor operations. The resulting graph is used for both inference and (with automatic differentiation) training.

API Signature

void mnist_model_build(mnist_model & model)

Source

Parameters

Name Type Description
model mnist_model & Model struct with pre-initialised weight tensors and GGML contexts. Must already contain allocated fc1_weight, fc1_bias, fc2_weight, fc2_bias (FC path) or the corresponding convolutional kernels and dense weights (CNN path).

Return Value

void -- the function populates model.logits, a tensor of shape [10, nbatch_physical] representing per-class scores for each image in the physical batch.

Fully-Connected Path

logits = fc2_weight * relu(fc1_weight * images + fc1_bias) + fc2_bias
  1. ggml_mul_mat(fc1_weight, images) -- project input to hidden dimension.
  2. ggml_add(..., fc1_bias) -- add first-layer bias.
  3. ggml_relu(...) -- apply ReLU activation.
  4. ggml_mul_mat(fc2_weight, ...) -- project hidden to 10 output classes.
  5. ggml_add(..., fc2_bias) -- add second-layer bias.

CNN Path

reshape(28, 28, 1, batch)
  -> conv2d(3x3) + ReLU -> maxpool(2x2)
  -> conv2d(3x3) + ReLU -> maxpool(2x2)
  -> flatten -> dense -> logits
  1. ggml_reshape_4d(images, 28, 28, 1, batch) -- reshape flat input to spatial layout.
  2. ggml_conv_2d(kernel1, ...) followed by ggml_relu -- first convolutional block.
  3. ggml_pool_2d(..., GGML_OP_POOL_MAX, 2, 2, ...) -- 2x2 max-pooling.
  4. ggml_conv_2d(kernel2, ...) followed by ggml_relu -- second convolutional block.
  5. ggml_pool_2d(..., GGML_OP_POOL_MAX, 2, 2, ...) -- second 2x2 max-pooling.
  6. ggml_permute / ggml_cont / ggml_reshape_2d -- flatten spatial dimensions.
  7. ggml_mul_mat(dense_weight, ...) + dense_bias -- final dense projection to 10 classes.

Parameter Registration

All weight tensors are marked as trainable parameters via ggml_set_param(), enabling the automatic differentiation engine to compute gradients during the backward pass.

GGML Operations Used

  • ggml_set_param -- mark tensor as trainable parameter
  • ggml_relu -- element-wise ReLU activation
  • ggml_add -- element-wise tensor addition (bias)
  • ggml_mul_mat -- matrix multiplication (dense layers)
  • ggml_reshape_4d -- reshape to 4-D spatial layout
  • ggml_conv_2d -- 2-D convolution with learned kernels
  • ggml_pool_2d -- 2-D max-pooling
  • ggml_reshape_2d -- flatten back to 2-D for dense layer
  • ggml_cont -- ensure contiguous memory layout
  • ggml_permute -- transpose / reorder dimensions
  • ggml_set_name -- assign human-readable name to tensor
  • ggml_set_output -- mark tensor as graph output

Language

C++

Related

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment