ml_transformer

Lightweight, modular and extensible machine learning and neural network library for Dart.


License
Other

Documentation

ml_transformer

ml_transformer is a lightweight, modular, and extensible machine learning and neural network library written in Dart.

Features

  • Tensor primitives and basic mathematical ops
  • Common neural network layers (Embedding, Linear, LayerNorm, Attention, TransformerBlock)
  • Mini autograd and AdamW optimizer
  • Quantization helpers (int8, int4, int2, fp16, bf16)
  • Basic ONNX import/export bridge (JSON demo; protobuf ONNX planned)
  • Simple dataset loader and batching API
  • Model save/load utilities
  • Training callbacks and tracking (EarlyStopping, History)
  • Unit tests covering core components

Installation

git clone <repo-url>
cd ml_transformer
dart pub get

Quick Start

import 'lib/dart_transformers.dart';

void main() {
 final cfg = GPTConfig(
  vocabSize: 256,
  maxSeqLen: 128,
  dModel: 64,
  nHeads: 8,
  nLayers: 4,
 );
 final model = GPTModel(cfg);
 final logits = model.forward([1, 2, 3, 4]);
 print('Logits: ${logits.rows} x ${logits.cols}');
}

There is also an example that demonstrates a tiny training loop, quantization and the ONNX import stub:

dart run example/demo_train_quant_onnx.dart

Tests

Run unit tests with:

dart test

Limitations

  • The ONNX bridge is a JSON-based demo; full ONNX (protobuf) support requires additional dependencies and parsing.
  • Some layers (RNN / LSTM / GRU) are currently basic examples and may need optimization for production.
  • Performance for very large models is limited by pure-Dart execution; native acceleration (FFI/GPU) is planned.
  • Quantization utilities are intended as helpers and need calibration/validation for production models.

Roadmap

  1. Full ONNX protobuf import/export and weight mapping
  2. Complete and optimize RNN/LSTM/GRU implementations with tests
  3. Optional native acceleration (FFI / GPU backends)
  4. More quantization modes and calibration utilities
  5. High-level Model API (fit/predict/evaluate) with checkpoints and schedulers

Contributing

Contributions are welcome. Please open issues or pull requests, and include tests for new functionality.

License

MIT