Published on May 15, 2024

Let's Generate StableHLO using JAX

Step-by-Step Guide:

How to Generate StableHLO using JAX

Step 1: Introduction to StableHLO and JAX

  • StableHLO (Stable High-Level Operations) is a set of high-level operations used by many machine learning frameworks like TensorFlow, PyTorch, and JAX.
  • JAX is a library for numerical computing that provides high-performance and automatic differentiation capabilities.

Step 2: Create JAX Arrays with Random Data

  • Start by creating two JAX arrays with random data.
  • These arrays will be used for matrix multiplication.

Step 3: Perform Matrix Multiplication

  • Use the @jit decorator from JAX to compile a matrix multiplication function.
  • Define the function to perform matrix multiplication and return the product of the two input arrays.

Step 4: Define Types and Shapes

  • Define the types and shapes of the arguments the function takes.
  • Specify that the dtype is int32 and the shape is (3,) .

Step 5: Generate StableHLO Code

  • Call the jit function from JAX and pass the matrix multiplication function to it.
  • Lower the function to a representation understood by ML compilers like XLA.
  • Pass the shape and dtype information of the arguments to the lowering operation.

Step 6: View the StableHLO Code

  • Call the function as text to view the generated StableHLO code.
  • Optionally, use a pretty print function to format the code for better readability.

Step 7: Compile the StableHLO Code

  • Compile the generated StableHLO code.
  • Assign the compiled object to a variable, e.g., matmul_c .

Step 8: Run the Compiled Code

  • Use the compiled object to perform matrix multiplication with the input arrays.
  • Verify that the compiled code returns the expected output.

Step 9: Conclusion

  • Congratulations! You have successfully learned how to work with StableHLO, generate it, compile it, and run it using JAX.
  • Feel free to explore more functionalities of JAX and experiment with different operations.

By following these steps, you can effectively generate and work with StableHLO using JAX as demonstrated in the video "Let's Generate StableHLO using JAX" by Circuit Chronicles.