stablehlo

Package website: release | dev

The {stablehlo} R package provides a functional API to create stableHLO programs. It also supports selected ops from the CHLO dialect, a higher-level companion to stableHLO that is lowered to stableHLO during compilation. These programs can be executed using the R package pjrt.

{stablehlo} is the IR layer of the r-xla ecosystem. The main user-facing package is {anvl}, a code transformation framework for R that builds on {stablehlo} and {pjrt} to provide JIT compilation and automatic differentiation. Most users should reach for {anvl} directly; {stablehlo} is intended for those who want to construct or manipulate stableHLO programs at the IR level.

Installation

From r-universe:

install.packages("stablehlo", repos = c("https://r-xla.r-universe.dev", getOption("repos")))

Quickstart

Below, we create a function that takes two input arguments x and y of type f32 and shape 2x2 and adds them. Passing func to hlo_input() is optional, because it will automatically use the last function created with hlo_func().

library(stablehlo)
func <- hlo_func("main")
func
#> func.func @main () ->  {
#> 
#> }
x <- hlo_input("x", "f32", shape = c(2, 2), func = func)
x
#> Variable %x in:
#> func.func @main (%x: tensor<2x2xf32>) ->  {
#> 
#> }
y <- hlo_input("y", "f32", shape = c(2, 2), func = func)
y
#> Variable %y in:
#> func.func @main (%x: tensor<2x2xf32>, %y: tensor<2x2xf32>) ->  {
#> 
#> }
z <- hlo_add(x, y)
z
#> Variable %0 in:
#> func.func @main (%x: tensor<2x2xf32>, %y: tensor<2x2xf32>) ->  {
#> %0 = stablehlo.add %x, %y : tensor<2x2xf32>
#> }
f <- hlo_return(z)
identical(f, func)
#> [1] TRUE
f
#> func.func @main (%x: tensor<2x2xf32>, %y: tensor<2x2xf32>) -> tensor<2x2xf32> {
#> %0 = stablehlo.add %x, %y : tensor<2x2xf32>
#> return %0 : tensor<2x2xf32>
#> }

Main Features

Important notes

Limitations

The R package is a partial implementation of the stableHLO specification. At least initially, it will:

Acknowledgments