Train a tiny decoder-only transformer language model live in the app — on Android, iOS, Desktop, and directly in the browser (Kotlin/Wasm and JS). Everything runs on-device with SKaiNET: the word-level tokenizer, the attention model, the training loop, and next-word prediction. No server, no Python, no pre-trained weights.
This sample is a Kotlin/SKaiNET port of the German educational page KI-ENNA: (E)in (N)euronales (N)etz zum (A)usprobieren — Transformer ("a neural network to try out"), with an English/German language toggle.
- Data — edit a tiny corpus (default: six German sentences), tokenize it with a
word-level tokenizer (
<pad>/<unk>/<eos>specials, frequency-capped vocabulary), and slice it into sliding next-token windows. - Training — a minimal transformer, faithfully small:
- token embedding
E [V, 12]+ learned positional embeddingP [T, 12] - single-head causal self-attention without Q/K/V projections:
A = softmax(X·Xᵀ/√d + causal mask),O = A·X - output projection
logits = O·W + b - cross-entropy loss (pad positions masked) + plain SGD Watch the attention heatmap and the loss curve update live while it trains.
- token embedding
- Embeddings — inspect the learned token vectors.
- Prediction — type a prompt and let the freshly trained model predict the next word (top-5 with probabilities).
- The NN DSL: a custom
Module<FP32, Float>built from differentiableTensorOps(indexSelectfor embedding lookup,matmul,transpose,softmax), trained with thetraining { model / loss / optimizer }runner DefaultGraphExecutionContext(phase = Phase.TRAIN)+DefaultGradientTapeautodiffCrossEntropyLosswith soft (one-hot) targets and thesgdoptimizer- Only
sk.ainet.core:*artifacts (viask.ainet:skainet-bom) — the transformer is built from scratch; nosk.ainet.transformersdependency
/shared— platform-agnostic logic: tokenizer, model, trainer (Flow-based), predictor./composeApp— Compose Multiplatform UI for all targets./iosApp— iOS entry point.
| Target | Command |
|---|---|
| Desktop | ./gradlew :composeApp:run |
| Web (Wasm) | ./gradlew :composeApp:wasmJsBrowserDevelopmentRun |
| Web (JS) | ./gradlew :composeApp:jsBrowserDevelopmentRun |
| Android | ./gradlew :composeApp:assembleDebug |
| iOS | open /iosApp in Xcode and run |
| Tests | ./gradlew :shared:jvmTest |
docker build -t skainet/tinytransformer .
docker run -p 8080:80 skainet/tinytransformerConcept and original implementation: KI-ENNA — statistical-thinking.de Ported to Kotlin Multiplatform + SKaiNET to showcase on-device, multi-platform training of a transformer from first principles.