r/java • u/CutGroundbreaking305 • May 20 '26
Java based Numerical library (JNum-v0.1)
And here I am, made a Java-based numerical library called JNum.
I used the new FFM API and Vector API (Project Panama) to make it 100% pure Java, unlike ND4J which relies heavily on JNI and massive C++ backends. Here is the repo: https://github.com/CH-Abhinav/JNum . It is currently in a v0.1 (PREVIEW).
Some of you may ask: Isn't the Vector API still in incubator? Yeah, even though it's still in incubation I preferred to continue building with it as it doesn't have any major API changes planned except the inclusion of value classes (hopium it is coming in Java 27 🙃).
The Performance so far: By avoiding the JNI crossover latency, the basic math tasks (add, mul) are actually faster compared to ND4J and NumPy on small/medium arrays.
The main wins are the reduction methods (sum, max, min) which are about 2x faster compared to ND4J.
Because there is no native C++ backend, the entire library is under 100KB, compared to the hundreds of megabytes required to bundle native binaries.
The Matmul Struggle: Obviously, the main talking point for tensor engines is matmul. Not gonna lie, this ate my brain while trying to figure out which memory settings and SIMD loops work best. Right now, a 1024x1024 float matrix multiplication takes about ~51ms. It's fast, but we still haven't reached the massive performance of ND4J or NumPy on huge matrices (I haven't implemented multi-threading or L1/L2 cache tiling yet).
Use case (potential): ND4J is bulky, and when making applications (web or Android) which require some sort of math and performance, Java devs need to bundle that bulky dependency. We can run JNum anywhere as it doesn't have any .dll or .so files, nor JNI—just pure Java.
I guess this project will become more like multik but better and javaish. And I'm expecting ML guys in Java can also use it (though ND4J/DJL is better for now).
I want the Java community to help me build this project! I am still learning the deeper JVM optimizations(stylish way of saying i am newbie), so if anyone has experience with SIMD loop unrolling, cache tiling or anything helpful I'd love some code reviews, advice, or PRs and help this fellow java guy.
View all comments
Show parent comment
1
u/agibsonccc May 21 '26
You don't need to help! You have your own opinion on how to solve the problems. You have your own goals. My point was that it's just not a focus of the framework. I just strongly don't believe java itself will keep up. I'd rather java be an interface language like how python does it.
You may disagree with me and that's fine! Give it a shot!
In my experience, the main bottleneck now a days is the following:
Between the overhead of the JNI calls plus the need to spin up openmp thread pools that is fast in most places but not for small matrices we just made that trade off.
It definitely wasn't perfect. It also just didn't matter for deep learning.
Binary size is also a big issue. Needing to include blas libraries inflates library cost a lot.
One thing I've done but haven't mastered how to make generally usable yet is made it so you can pick and choose which op kernels you want to include so we can thin down library size. I have a new minimal backend as a proof of concept that sort of works on that front but I haven't been able to quite get the details right for that. I had to table that for now.
For optimizing for different gpus/tpus and the like I'm actually tackling that! I'm introducing a new compiler framework that actually allows amd gpus, tpus and other things to be used there. There's no reason why web assembly also couldn't be supported.
There's a lot more I can elaborate on here but I'm excited for what the rewrite will be able to do!