When fine-tuning language models like Qwen2-0.5B or Llama-3 on a constrained heap, the biggest memory and compute bottleneck often isn't self-attention. On models with a small hidden dimension and a large vocabulary, the bottleneck is the output projection head ( lmHead ) and cross-entropy loss.
I ran into this while working on v1.1.0 of Kotensor ( io.github.tspstudio:kotensor ), an open-source, pure-Kotlin Transformer and reverse-mode autograd engine for JVM 17+ and Android with no native C++ or JNI dependencies. Inference was already reasonably fast and ran within tight mobile memory limits, but running a LoRA fine-tuning step on the full 24-layer Qwen2-0.5B architecture inside a 6 GB JVM heap consistently failed with OutOfMemoryError: Java heap space .
At first I looked for a reference leak in the autograd graph, assuming intermediate activations weren't being freed between steps.…