How sumon-98/nntrainer works
Project Type and Purpose
• Project Type: A cross-platform, embedded-focused machine-learning training framework (native C++ core with C/C++ APIs, JNI wrappers, and example Android/Tizen applications).
• Purpose: Provide on-device neural-network training and fine-tuning, exposing functionality via C and C++ APIs (and JNI for Java), supporting multiple backends (CPU/ARM/OpenCL/GGML), and integrating with NNStreamer.
File Structure to Extract Architectural Layers
Top-level directories:
• api/ → C API (capi) and C++ API (ccapi) layers
• nntrainer/→ Core library: compiler, graph, engine, layers, optimizers, tensor, utils, opencl delegate
• nnstreamer/ → Plugin integration for NNStreamer
• jni/ → JNI build scripts for Android
• Applications/ → Example clients on Android/PC/Tizen
• subprojects/ → Third-party dependencies (OpenBLAS, CLBlast, ONNXConverter, ggml, ruy)
• packaging/ → Deployment manifests and tarballs
• test/ → Unit and integration tests
• benchmarks/→ Performance benchmarks
README for Additional Insights
• Core functionality: model definition via INI, realizer/compiler interpreters for .ini, ONNX, TFLite
• Execution: Engine drives forward/backward passes, using tensor backends and memory managers
• Extensibility: plugin-style layers and optimizers, delegate interfaces for hardware acceleration
• APIs:
– C API (Tizen focus)
– C++ ccapi
– JNI wrappers enable Java/Android clients
Main Components & Their Relationships
a. User/Application Layer
– Example apps (Applications/) or custom clients
– On Android: Java/Kotlin → JNI → native C/C++ API
b. API Layer
– C API (api/capi) and C++ API (api/ccapi)
– Exposes model setup, training loops, callbacks
c. Core Library (nntrainer/)
– Compiler/Model Loader: parse .ini or import ONNX/TFLite → build graph
– Graph Module: network_graph, connection, graph_core
– Engine: orchestrates forward/backprop, uses delegate to select backend
– Layers: collection of built-in and plugin layers
– Optimizers: SGD, Adam, AdamW + LR schedulers
– Dataset: data producers, iterators, buffers
– Tensor Module: tensor abstraction + multiple backends (CPU, ARM neon, CBLAS, fallback, GGML, OpenCL via cl_interface)
– Memory Manager: planners and pools
– Utils: profiling, threading, ini parsing, dynamic loader
d. Delegate/Backend Layer
– CPU backends (x86, ARM)
– OpenCL delegate (nntrainer/opencl)
– External libs: OpenBLAS, CLBlast, ggml, ruy
– ONNX/TFLite interpreters
e. Plugin Integration
– nnstreamer plugin (nnstreamer/tensor_filter, tensor_trainer)
f. Build & Packaging
– Meson build definitions throughout
– JNI build scripts (Android.mk, Application.mk)
– CI/CD workflows (.github/)
– Packaging manifests for Tizen, Ubuntu, Android
Architectural Patterns & Design Principles
• Modular layered architecture: separation of API, core engine, and hardware delegates.
• Plugin-style extensibility: layers, optimizers, backends plug into core via common interfaces.
• Data-flow graph pattern: model represented as a computational graph, executed by Engine.
• Strategy pattern for tensor backends and optimizers.
• Factory patterns in dataset and layer creation.
Elements
– From Application down to API
– From API to Core (Compiler/Graph/Engine)
– From Engine to Tensor/Memory Manager to Delegate/Backend
– API layer (e.g., blue)
– Core engine (e.g., green)
– Hardware delegates/external libraries (e.g., orange)
– JNI/Android path (e.g., purple)
• Annotations or callouts for plugin/extensibility points
Final Advice
• Plugin interfaces for layers, optimizers, and tensor backends.
• Don’t over-optimize—break down the monolith into cohesive modules.
• Use a layered, top-down view:
1. Client/Application
2. API layer
3. Core engine (with submodules)
4. Delegate/backends & external dependencies
5. Build/CI/Packaging