deepjavalibrary / deepjavalibrary/djl

Shape safe API in Scala: verify tensor compatibility at compile-time

Open
#994 6 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
4.9k
Forks
759
Avg merge
19h 26m
Merged PRs (30d)
17

Description

## Description

We would like to introduce a scala API that actively validates the feasibility of tensor operations while being written, NOT while being tested or deployed. The API will be based on the scala library [shapesafe](https://github.com/tribbloid/shapesafe), which I'm co-maintaining.

The capability of shapesafe is a superset of several prior arts (see references): It validates not only tensor with literal shape, but also tensor as an output of heavy computations (e.g. einsum or several NN layers). This makes both prototyping & critical production less error-prone, while avoiding computation overhead after deployment (as type information are erased at runtime)

![example](https://raw.githubusercontent.com/tribbloid/shapesafe/master/doc/video/S4.gif)

After integration, a typed tensor API will be the main entry point to define an NN architecture. Calling each of its method will define both compile-time shape and runtime computation at the same time, yielding more typed tensors.

Unfortunately this API will only be for Scala, maybe it is not a bad choice considering the overlapping audience of Apache Spark.

##### Will this change the current api? How?

No, but a new API exclusive to Scala user can be introduced which enables this feature

##### Who will benefit from this enhancement?

ML Users of Scala, Apache Spark, CHISEL, SpinalHDL ... who are prone to make mistakes caused by mismatched tensor sizes.

## References

##### Prior Art

- Evan Spark for [first showing the possibility](https://etrain.github.io/2015/05/28/type-safe-linear-algebra-in-scala)
- Tongfei Chen et al. for [Nexus](https://github.com/ctongfei/nexus)
- Breandan Considine et al. for [Kotlin∇](https://openreview.net/forum?id=SkluMSZ08H)
- Dougal Maclaurin et al. for [Dex](https://github.com/google-research/dex-lang)
- Maxime Kjaer et al. for [tf-dotty](https://github.com/MaximeKjaer/tf-dotty)

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the existing Scala support in DJL and the shapesafe project linked in the issue. Define the integration boundary, typed tensor entry points, and compatibility checks before identifying implementation files. Done means a documented Scala API validates tensor-operation shapes at compile time without changing the current API.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
api, machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.