/ktorch

Primary LanguageKotlinApache License 2.0Apache-2.0

Ktorch

Eval Logo

Intro

This project serves as an experimental playground for learning particular areas of knowledge using leaning by doing approach. The learning knowledge primary focus is on the following areas:

  • Machine Learning
  • PyTorch
  • Kotlin
  • New Java APIs (Vector API, Virtual Thread, Foreign Function & Memory API)
  • Effective Java

Steps

Set up development environment

User Java incubator module (in Kotlin gradle project)

  1. https://foojay.io/today/how-to-run-the-java-incubator-module-from-the-command-line-and-intellij-idea/
  2. https://stackoverflow.com/questions/70390734/how-can-i-use-the-incubating-vector-api-from-kotlin-in-a-gradle-build

Implement Numpy in Java

Numpy Basic Operations

Implement Numpy Broadcasting and Dot Product in pure Java/Kotlin, and use SIMD to accelerate computation.

References

Broadcasting
Dot Product
Performance testing (JMH)

Principle: make matrix multiplication as fast as possible, and other operations more elegant.

Implement Simple NN

Implement backward

https://github.com/tinygrad/tinygrad/blob/91a352a8e2697828a4b1eafa2bdc1a9a3b7deffa/tinygrad/tensor.py

the original plan is to implement backward using closure same as the implementation in micrograd. however, after some investigation. I suspect that the closure may cause performance issues. so we transit to the tinygrad way

Implement MNIST

./gradlew run --args=mnist

https://github.com/tinygrad/tinygrad/blob/91a352a8e2697828a4b1eafa2bdc1a9a3b7deffa/test/mnist.py

  • Predict: input.dot(l1).relu().dot(l2).logSoftmax()
  • Loss: NLL loss or CrossEntropyLoss
Tool

TODO List

  • Get sub NDArray by indices
  • Make matmul applicable for 1-d arrays, ref implementation in mlx
  • Unroll matmul, ref llama2.java
  • Benchmark with flame graph & memory usage

Reference

Articles

Projects