This is what Microsoft did to make C# interop with Python including pip installl integration etc. They share memory to avoid heap duplication.
Dealing with non-JVM-controlled memory is part of the problem space anyway if youâre doing computations on the GPU. That the shared memory happens to be CPU-addressable memory instead of GPU memory doesnât really changeâŚanything. (Itâs also the same hassle in Python to deal with the GPU if you start going direct instead of interacting only with the highest-level wrappersâyou have the whole data on/off GPU thing.)
So I donât really see IPC with Python via shared memory buffers as different than âIPCâ with the GPU. It doesnât mean you have to compile to Python, just talk to it.
Yeah, the over goal is to âtalk to itâ i some convenient and safe way without performance penalty. Other ways than compiling to it is also interesting to investigate. Different use cases might benefit from different solutions.
- My main point is that we are currently lacking access to the AI-tooling currently only available in Python, which may hamper Scala adoption.
same hassle in Python to deal with the GPU
Well, as I understand it, much effort has been made in Python to create the shims for the latest planet-boiling hardware, but if I am correct, its a big job getting the interfacing to work, but it âjust worksâ in Python from the pythorch user side.
It is interesting to learn that, from a C# view, the Microsoft engineers in the video above speaks about Python as âan implementation detailâ⌠(may they were joking)
I recall a case where I needed Python in Scala, but as a scripting language interpreter, not a compiler. The business reason was to enable customers to write scripts within the application server, without requiring them to learn Scala. Python was chosen because it is a well-known language and it is easy to understand. (Hmm, reasons for spark-python are the same). So, the main question is the perception of the language as {hard/easy} to learn.
You nail it here. Long time ago I did a project in Eifel, but the language was abandoned for it was too slow in picking up decent XML support, which was the hype of the time. AI is the hype of this time. I do coding for it and, of course, it is in Python. Torturing myself, i wonder, could this not be done in Scala? That would make me produce much more reliable code. My feeling is that, if Scala is not able to connect to the new hype fast, our community will lose a lot of programmers.
My personal favourite would be to give the main role to Scala Native here, with wrappers around libraries like PyTorch and TensorFlow. If i am correctly informed (but correct me if wrong) these both have their core libs written in C++.
Btw, DJL. ( https://djl.ai ) is good.
I share this worry. And I would not look forward to be forced to code in unsafe land.
A nuance to consider: Iâd honestly say that âwithout performance penaltyâ may be stricter than is necessarily called for here. (Especially given that itâs not as if Python itself is all that fast.)
Iâd recommend instead thinking in terms of quantifying the performance penalty that various options have, and considering that as one of the tradeoffs. Somethingâs thatâs considerably easier to build, and not too much slower in practice, might not be a bad compromise. (Not that that necessarily exists, of course â my point is just that performance is probably a tradeoff, not a hard requirement.)
No one stopping anyone from exploring ideas. My comment was focused on finding best solution to the problem - enabling AI in Scala or which is better making Scala great language for AI development, and in my opinion compilation to Python doesnât solve either of problems.
Just some points why:
- Compiling to Python wonât make use AI libraries in Scala easier, it will only make it harder as you will need to workout target compilation quirks
- It wonât make people target Scala due Scala unique proposition at language level or platform
- It wonât make execution faster and cheaper than using Python directly
- It going to encourage more people to learn Python than use Python through Scala, and they can use as transition step.
- Cross-compilation is good when youâre targeting multiple platforms, so you want to share business logic (e.g. Android <> iOS <> Backend, another example Kotlin multiplatform), while itâs not that interesting when you need to get access to ecosystem for specific use case.
Scala has amazing type system and support for macroses, that if can be paired with either native compilation or Panama can produce amazing and fast toolchain and interoperability with python should be considered an intermediate step towards creating own AI toolchain on Scala thatâs better than python. Spark only took off because it was solving industry problems better than other solution, and once there will be something even better everyone will forget Spark.
At the current moment If I need to do data science, AI or statistics for commercial use, I wonât choose Scala as main language, it just doesnât have ease of use of Python nor Rust performance, and if we imagine that Scala already can compile to Python - it still wonât increase chances I pick Scala as it going to be just an extra wheel anyway.
So overall better to find optimal solution with as many people aligned as possible. Scala doesnât have herds of students and companies behind AI that willing to put time and money to evolve the ecosystem.
Maybe the ultimate solution is to do as microsoft and generate Scala source code to make interop really convenient:
(note the cute mascot)
But I guess we should evaluate all options in terms of benefits and use cases. The engineers at microsoft said in the presention that it took at least one person (with a backing team, unclear how much time they devoted) the lead time of as much as one year to accomplish this.
But compiling to Python (or some similar tight interop on Scala Native+JVM) may also attract those who gradually want to migrate off Python to Scala but cannot do the migration in big one step (even when assuming all corresponding missing scalalibs or Panama+Babylon were ready to goâŚ)
People mentioned Scala on GPUs, I thought this might be relevant: Scala Days - Painting with functions - Scala on GPUs
I agree that the lack of a mascot is a blocker and would require at least a year of research.
What would the Scala helix look like right after swallowing a cute snake?
By coincidence, RFC - Angular official mascot ¡ angular/angular ¡ Discussion #61733 ¡ GitHub
They are approaching the problem with seriousness of purpose and structured discussion, however. The current sticking point is that the anglerfish is not cute.
Panama has improved the way off-heap memory is managed, and Babylon has further built upon this by providing new memory-mapping interfaces for GPU programming.
To quote Juanâs introduction:
This is one of my favourite parts of the HAT project. HAT defines an interface called
iFaceMapper
to represent data. Data is actually stored off-heap by leveraging the Panama Memory Segments API for GPU computing.From my point of view, data representation presents a significant challenge in GPU programming with managed runtime languages like Java, particularly concerning the tradeoffs between performance, portability and ease of use. It is also a critical part, because in Java, we have the Garbage Collector (GC), that can move pointers around if needed.
HAT tackles this issue by defining a base interface capable of handling data access and manipulation within Panama Segments. This interface is extensible, enabling developers to create custom data objects compatibility with GPUs and other hardware accelerators.
This interface offers broad potential benefits, extending beyond Babylon and HAT to projects like TornadoVM. While TornadoVM offers a wide range of hardware accelerator-compatible types, it currently lacks user-side customization for data representation. This interface could provide a very promising approach for integration, enabling greater flexibility and control, and improve TornadoVM further.
For example, to create a custom data object in HAT to store an array that uses a Memory Segment:
public interface MyCustomArray extends Buffer { int length(); @BoundBy("length") float data(long idx); void data(long idx, float f); // Define the schema Schema<MyCustomArray> schema = Schema.of(MyCustomArray.class, array -> array .arrayLen("length") .array("data")); static MyCustomArray create(Accelerator accelerator, int length) { return schema.allocate(accelerator, length); } }
Then, the HAT OpenCL compiler generates a C-struct as follows:
typedef struct MyCustomArray_s { int length; float data[1]; } MyCustomArray_t;
Still, a bit of boiler-plate code to add, but it can be used to define custom data types compatible with GPUs. How cool is this?
Scala DSL to GPU code is definitely interesting! But rather than leaning into a functional approach, the easy path is just wrapping toolingâlike bytedeco does for GPU libraries (so there isnât even any user-facing install of GPU tooling, which is awesome!), or DJL does for ML libraries (which you do have to install separately, so boo, but after that it is (supposed to be) easy)âor simply providing a shared memory arena with Python which I think Java 22 is pretty close to with the FFM API.
Iâm honestly not too interested in having yet another backend for Scala, altough having a language you can use anywhere does sound interesting
What I would love to see is very good abstractions for programming on the GPU with native support
Maybe even in the standard library
@odersky werenât there discussions about such a thing with a graphics programming professor at EPFL ?
now I just try to enhance storch and make full stack scala3 deep learning and big data and llm environment chains
we need and must to do
We need to establish a âstable, robust, reliable, and sustainableâ deep learning environment while fostering a strong community culture. I have already written nearly â40,000 lines of codeâ for âStorchâ and implemented its upstream and downstream components entirely in âScalaâ, though the work is not yet complete. âPyTorchâ will undoubtedly remain the standard for deep learning, and we rely on it. Fortunately, we now have âStorchâ, along with essential tools like âNumPy, Polars, Pandas, Pickle, SafeTensor, MsgPack, Excel, and HDF5â, allowing us to break through Pythonâs barriers and monopolistic dominance with real capability.
We still need âHugging Face Transformersâ. Our goal is to build our own ecosystem by âreimplementing or bindingâ outstanding Python and Rust projects in âScala 3â, ensuring developers remain within the Scala ecosystem. Personally, I believe âScala Nativeâ holds even greater potential and will become a strong competitor to Rust.
Ultimately, âregardless of differing opinions, we must complete a full-fledged Scala ecosystem.
https://github.com/mullerhai/storch-numpy
and them some have publish to maven center repo for use
Some new languages like C3, Odin, Mojo, ⌠has Vector/Tensor type embedded directly into the system. Some even consider scalar type a Vector with 1 lane to unify the type system. With these type they also provide some operator that will compiled directly into GPU instructions or CPU SIMD instructions.
Given the SIMD situation in JVM, I donât think we can do this at the language level yet (I donât know much about GPU programming in JVM so I canât tell). But someone can try creating a library.
This is fantastic! How is the relation between the work in this fork and the upstream lib - are your PR:s planned to be merge or will you continue with a deviating fork? Iâm also wondering how the docs will develop so that beginners can get going with this recent development.
Also it would be good to know were potential contributors can make the most difference depending on the knowledge level etc. in terms of good first issues etc.
Just adding another 5 cents: I also think that itâs probably better to invest in Scala Native than a new Python backend.
One potential quick win would be native wrappers for native runtimes like ONNX runtime or MLPack.
Right now, a traditional pipeline might look something like:
Data Processing in Spark (either Scala or Python) -> Training in Python -> Inference in Python
With this, one could easily move to:
Data Processing in Spark (either Scala or Python) -> Training in Python -> Inference in Scala Native
Which could also tip the scales to move the Data Processing to Scala instead of Python.
Thereâs also the fact that it might not be that easy to convince people to move the training code to Scala: If you have a Data Engineering / Applied Science split, itâs likely that your Applied Science team will be more familiar with Python and more resistant to change (they have more things to worry about), while the Data Engineering team might be more open to move to Scala (after all, they are the ones that have to deal with pipeline errors due to type mismatches).
I want to merge ďźbut it is too huge to review code for the authorďź I have raise a pr two months ago ďźbut need to waiting ă
I have write a tutorial for begineer try to practice ďźbut it it not easy too useďź because we also need some package to make them easy ă
I think future directionďź we need make scala3 jvm more convinient for deeplearning, then migrate the Ai coder to scala-native , thus we will behind by Rust, ,
most important, scala3 jvm and native need support vector and tensor type on System with GPU