[tosa] Add path to TOSA backed backends. (#1466)

Connect these two industry standards by way of dialect legalization from
StableHLO to TOSA. This adds basic support and testing: along with usage
of some of the equivalent canonicalization patterns from MHLO (not
included in this PR) this has been sufficient for some full models
starting from ML framework to TOSA backed. Support is not complete and
partly relies on some canonical StableHLO forms.

The legalizations are also written primarily using PDLL, but we have not
yet adopted some of the newer support there for variadics. This work
started by targeting MHLO in TensorFlow repo as StableHLO was still
young, but given StableHLO development it makes more sense to instead
start there and provide a connection for community backends.

No new repository dependency is introduced. The cmake config enables
disabling building conversion,

---------

Co-authored-by: Eugene Burmako <burmako@google.com>
22 files changed
tree: 37b4e2980d94c0f6c2bdd5dda09f26e7bd0e3d43
  1. .github/
  2. build_tools/
  3. docs/
  4. rfcs/
  5. stablehlo/
  6. .bazelignore
  7. .bazelrc
  8. .clang-format
  9. .gitignore
  10. .markdownlint.yaml
  11. BUILD.bazel
  12. CMakeLists.txt
  13. CODE_OF_CONDUCT.md
  14. CONTRIBUTING.md
  15. LICENSE
  16. README.md
  17. WORKSPACE.bazel
README.md

StableHLO

StableHLO is an operation set for high-level operations (HLO) in machine learning (ML) models. Essentially, it's a portability layer between different ML frameworks and ML compilers: ML frameworks that produce StableHLO programs are compatible with ML compilers that consume StableHLO programs.

Our goal is to simplify and accelerate ML development by creating more interoperability between various ML frameworks (such as TensorFlow, JAX and PyTorch) and ML compilers (such as XLA and IREE).

StableHLO is based on the MHLO dialect and enhances it with additional functionality, including serialization and versioning. We use MLIR bytecode as serialization format and provide backward and forward compatibility guarantees. This ensures compatibility between frameworks and compilers, even as StableHLO continues to evolve.

This repository includes the StableHLO specification along with an MLIR-based implementation in C++ and Python, which you can use to define StableHLO programs for consumption by compilers such as XLA and IREE.

Build instructions

Here's how to build the StableHLO repo on Linux or macOS:

  1. CMake is our primary build tool, so before you begin make sure that you have CMake and Ninja installed.

    If you're using Linux, we recommend installing lld as well - we have observed it to be noticeably faster than alternatives on our typical software and hardware configurations.

    # On Linux
    sudo apt install cmake ninja-build lld
    
    # On macOS
    brew install cmake ninja
    
  2. Set the LLVM_ENABLE_LLD shell variable depending on your preferences. We recommend setting it to ON on Linux and to OFF on macOS.

    [[ "$(uname)" != "Darwin" ]] && LLVM_ENABLE_LLD="ON" || LLVM_ENABLE_LLD="OFF"
    
  3. Clone the StableHLO repo and the LLVM repository:

    git clone https://github.com/openxla/stablehlo
    
    cd stablehlo && git clone https://github.com/llvm/llvm-project.git
    

    Cloning the LLVM repository may take a few minutes.

  4. Make sure you check out the correct commit in the LLVM repository:

    (cd llvm-project && git fetch && git checkout $(cat ../build_tools/llvm_version.txt))
    

    You need to do this every time llvm_version.txt changes.

  5. Configure and build MLIR:

    build_tools/build_mlir.sh ${PWD}/llvm-project/ ${PWD}/llvm-build
    

    This will take a considerable amount of time. For example, on a MacBook Pro with an M1 Pro chip, building MLIR took around 10 minutes at the moment of writing.

    Again, you need to do this every time llvm_version.txt changes.

  6. Build StableHLO as a standalone library:

    mkdir -p build && cd build
    
    cmake .. -GNinja \
      -DLLVM_ENABLE_LLD="$LLVM_ENABLE_LLD" \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_ENABLE_ASSERTIONS=On \
      -DMLIR_DIR=${PWD}/../llvm-build/lib/cmake/mlir
    
  7. Now you can make sure it works by running some tests:

    ninja check-stablehlo-tests
    

    You should see results like this:

    Testing Time: 5.99s
      Passed: 47
    

    This runs all the tests in stablehlo/tests/.

Community

Building an amazing portability layer between ML frameworks and ML compilers requires collaboration across the whole ML industry, so we're happy to have your help on the StableHLO project.

We're using GitHub issues / pull requests to organize development and openxla-discuss to have longer discussions. We also have a #stablehlo channel on the OpenXLA Discord server.