https://github.com/phuijse/rust_java
Experiments on interfacing Java and Rust
https://github.com/phuijse/rust_java
Last synced: 7 months ago
JSON representation
Experiments on interfacing Java and Rust
- Host: GitHub
- URL: https://github.com/phuijse/rust_java
- Owner: phuijse
- License: mit
- Created: 2023-09-20T07:13:51.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-20T07:19:09.000Z (about 2 years ago)
- Last Synced: 2025-01-15T13:05:26.443Z (9 months ago)
- Language: Jupyter Notebook
- Size: 85 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Experimenting with interfaces between Java, Rust and Torchscript
Rust code can be called from java through the Java Native Interface (JNI) by exploiting the [jni crate](https://crates.io/crates/jni).
The first half of the example shows how to do some compute in Rust and exposes it in Java through a class.
Then there is [torchscript](https://pytorch.org/docs/stable/jit.html) which can serialize a Pytorch model (trained in regular Python). This model can be then called using the TorchScript C++ API. [C/C++ code can be called from Rust (and viceversa)](https://docs.rust-embedded.org/book/interoperability/index.html) and indeed there is already a [crate](https://github.com/LaurentMazare/tch-rs) with Rust bindings for the Torchscript API.
The script `model.py` creates a simple PyTorch model and serialize it. To run it I suggest:
conda create -n torch_rust pytorch==2.0.0 cpuonly -c pytorch
conda activate torch_rust
python model.pyA file `traced_model.py` should appear in the root of this repo.
After that add the following to your `LD_LIBRARY_PATH` env variable:
$CONDA_PREFIX/lib/python3.10/site-packages/torch/lib/
Then do compile the crate with `cargo build` or `cargo run` if you want to execute the binary (`main.rs`).
Now to call this from java
javac main.java
java -Djava.library.path=target/debug/ MyFirstRustClassChange to `/target/release` if you passed `--release` to `cargo build`.