Skip to content

Getting Started

Shogo Toda edited this page Oct 29, 2026 · 12 revisions

Getting Started

Install

The Python package ships the compiler; clang (LLVM 15 or newer) must be on the machine, since rhoc asks it to build the shared library.

pip install rho-lang                                                   # once a release is on PyPI
maturin build --release && pip install target/wheels/rho_lang-*.whl   # your own build

To work on the compiler itself (Rust 1.88+ and clang):

git clone https://github.com/Shogo256/Rho_Lang.git && cd Rho_Lang
cargo build --release && cargo test

Compile and run

rhoc examples/matrix_add.rho                       # writes libkernel.so
printf '1 2 3 4\n5 6 7 8\n' > in.txt
rhoc examples/matrix_add.rho --run INPUT=in.txt    # runs it: prints OUTPUT, one row per line

--run SPACE=FILE reads an input from a file (raw little-endian doubles for .bin, whitespace-separated numbers otherwise, as many as the space has cells) and runs the kernel once; --write SPACE=FILE keeps any space it wrote, intermediates included. A program with ⇒ reports its sweeps and whether it settled.

Flag Effect
--run SPACE=FILE, --write SPACE=FILE Files in and out, as above
--tau <v> Bind the threshold 𝜏 (default 0.0)
--max-iter <N> Cap every ⇒ at N sweeps; required by a program that iterates
--f32 Compute at single precision
--threads <N> Split every sweep across N threads; 0 (default) is one per CPU. RHO_THREADS overrides at run time
--portable Build for any x86-64 rather than this machine
--bind NAME=0x… Point a space at an address the caller owns
--emit-static <LIB.a>, --symbol-prefix <P> Also write the kernel as a static library whose symbols are P_exec_spaces, P_metadata, ... in place of rho_kernel_*, so that several kernels link into one program
--emit-lean <FILE.lean>, --lean-namespace <NAME>, --lean-program-only Write the source's functions as Lean 4 definitions over the integers, for proofs about them, and stop: sums, products, ⌊ of a quotient by a whole constant, ind, abs, calls; a function outside that subset is named in a comment and left out, and a program block of such flows becomes kernel, one cell of OUTPUT from the same cell of each input (alone with --lean-program-only). A source of definitions alone is accepted
--emit-jax <FILE.py> Also write the program as a JAX module (rho, rho_jit)
--emit-sv <DIR> Also write the program as a SystemVerilog pipeline with a Verilator harness
--fixed <W.F> Make the circuit's cells fixed point, e.g. 32.16
--fuse With --emit-sv, compute each intermediate that one flow reads cell for cell inside that flow's stage, as the kernel does, and a part a stage reads twice once: fewer stages, registers and replay memories, and a longer path through a stage, so a slower clock
--constant-time, --public NAME,… Refuse the program unless its cells may be secrets: no division or residue by a non-constant, no sqrt exp log sin cos or fractional power, no ⇒, and ⌷ only at positions made of ⍳, constants and the --public spaces
--explain Say what each flow became: the sweeps a call runs, vector or one cell at a time and why, the folds computed first, what fused into what, a ⇒'s rounds; with --emit-sv, the circuit's stages, products and memories
--dump-llvm, --dump-dag, --no-simd Print the IR, print the dataflow, emit scalar loops only
--lang <en|ja> The language of the hints under a refusal (RHO_LANG too; default English)

A refused line gets a hint when it looks like another notation's way of writing it (X ◇+, +/X, np.sum(X), X[i+1], S = expr, an open parenthesis, a misspelt name, a space one axis short), with the line as ρ writes it — checked by parsing the program with it before it is shown.

From Python

Write the flows once, without declarations; each shape you call with is compiled once and cached (~/.cache/rho-lang, or RHO_CACHE_DIR).

from rho import Kernel

blur = Kernel("((▷0INPUT + ▽0INPUT + ▷1INPUT + ▽1INPUT + INPUT) / 5.0) → =")
out = blur(INPUT=image)                       # nested lists or numpy arrays
mix = Kernel("((A × W) + (B × (1.0 - W))) → =")
out = mix(A=a, B=b, W=w)                      # every named argument is a space

relax = Kernel("INPUT → X\n((INPUT / 4.0) + ((▷X + ▽X) / 4.0)) ⇒ X\nX → =", tau=1e-12, max_iter=200)
x = relax(INPUT=b); relax.sweeps(), relax.converged()

Against a compiled .so by hand, rho.RhoEngine wraps the C ABI and checks buffer lengths:

from rho import RhoEngine
engine = RhoEngine()
engine.compile_rho_file("examples/matmul.rho")
engine.spaces()      # [('A', [2,3,1], 'input'), ('B', [1,3,4], 'input'), ('OUTPUT', [2,4], 'output')]
engine.execute_spaces({"A": a, "B": b, "OUTPUT": c})

The C ABI

Symbol Signature Purpose
rho_kernel_exec_spaces void (void **spaces) One pointer per space, in the order rho_kernel_metadata() lists them. Inputs must be given; a null intermediate is the kernel's own, and a null fused one is never stored
rho_kernel_exec_with_args void (const double *in, double *out) The two-pointer form; buffers hold rho_kernel_element_count() values
rho_kernel_exec_bounded void (const double *in, double *out, int64_t n) Same, clamped to n cells
rho_kernel_metadata const char * (void) JSON: every space's name, shape and role, the loop's cap and tolerance, and reentrant: whether two threads may call the kernel at once (true unless it keeps scratch between calls)
rho_kernel_sweeps, rho_kernel_converged int64_t (void) Sweeps the last call's ⇒ took, and whether every one settled (with calls at once, one of them)
rho_kernel_element_count int64_t (void) The larger of the input's and the output's cell counts
rho_kernel_exec void (void) Zero-copy: runs against the addresses bound with &[0x…] or --bind
rho_kernel_zeroize void (void) Overwrite the scratch kept between calls and the stack an entrypoint's frame used; call it from where the kernel was called, before letting secrets go

A null output pointer makes the two-pointer form write in place; a null input pointer makes it return without touching memory. Scratch the kernel owns is kept between calls and freed when the library unloads.

The other two targets

rhoc program.rho --emit-jax module.py            # module.rho(...) and module.rho_jit
rhoc program.rho --emit-sv build --fixed 32.16   # build/rho_kernel.sv + a Verilator harness

The JAX module returns every written space with rho_sweeps and rho_converged; jax.jit and jax.vmap apply, and jax.grad to programs without ⇒. The circuit is one cell per clock with line buffers for the shifts, accumulators for the folds and block RAM for ⇒; Verilator simulates it against the interpreter and Yosys synthesises it; see Verification.

Clone this wiki locally