Skip to content

Commit

Permalink
Add support for Python example (CMake) (openxla#2113)
Browse files Browse the repository at this point in the history
This is the canonical Python example similar to what was recently
introduced for C++ in openxla#2083

* Add a stablehlo_add.py file which demonstrates nice usage of the
Python bindings API
* Integrate it with the CMake build system
* Introduce a CMakePresets for buildings and Python bindings

We have never had support for building Python bindings with Bazel so
that support is missing as of this PR as well.

```console
❯ cmake --build --preset debug-python --target stablehlo-python-add
[89/89] cd /usr/local/google/home/fmzakari/code/github.com/open...ages/stablehlo /tmp/stablehlo-venv/bin/python3 stablehlo_add.py
module {
  func.func @main() -> tensor<3x4xi64> {
    %0 = stablehlo.constant dense<0> : tensor<3x4xi64>
    %1 = stablehlo.constant dense<0> : tensor<3x4xi64>
    %2 = stablehlo.add %0, %1 : tensor<3x4xi64>
    return %2 : tensor<3x4xi64>
  }
}
```
  • Loading branch information
fzakaria authored Mar 22, 2024
1 parent 4f180d3 commit 2c82fb4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
21 changes: 21 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"MLIR_DIR": "${sourceDir}/llvm-build/lib/cmake/mlir"
}
},
{
"name": "debug-python",
"displayName": "Debug w/ python bindings",
"inherits": "debug",
"cacheVariables": {
"STABLEHLO_ENABLE_BINDINGS_PYTHON" : "ON",
"STABLEHLO_ENABLE_SANITIZER": "OFF"
}
}
],
"buildPresets": [
{
"name": "debug",
"displayName": "Build Debug",
"configurePreset": "debug"
},
{
"name": "debug-python",
"displayName": "Build Debug w/ python bindings",
"configurePreset": "debug-python"
}
]
}
6 changes: 5 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
# Copyright 2024 The StableHLO Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,3 +13,7 @@
# limitations under the License.

add_subdirectory(c++)

if(STABLEHLO_ENABLE_BINDINGS_PYTHON)
add_subdirectory(python)
endif()
15 changes: 15 additions & 0 deletions examples/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 The StableHLO Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

add_stablehlo_python_test(stablehlo-python-add stablehlo_add.py)
9 changes: 9 additions & 0 deletions examples/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Python Examples

This directory should contain a series of examples as a starting point
for how to use StableHLO via our Python bindings.

Note: If you have a great example to highlight, we welcome contributions!

* [stablehlo_add](./stablehlo_add.py): A simple example that demonstrates
how to use the StableHLO library to add two numbers.
37 changes: 37 additions & 0 deletions examples/python/stablehlo_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 The StableHLO Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from mlir import ir
import mlir.dialects.stablehlo as stablehlo
import mlir.dialects.func as func
from mlir.ir import Context, Location, InsertionPoint, Module
import numpy as np

with Context() as ctx, Location.unknown():
stablehlo.register_dialect(ctx)
module = Module.create()

with InsertionPoint(module.body):

@func.func()
def main():
a_value = ir.DenseElementsAttr.get(np.zeros(shape=[3,4], dtype=np.int64))
b_value = ir.DenseElementsAttr.get(np.zeros(shape=[3,4], dtype=np.int64))
a = stablehlo.constant(a_value)
b = stablehlo.constant(b_value)
add = stablehlo.add(a, b)
return add

assert main.func_op.verify()
print(str(module))

0 comments on commit 2c82fb4

Please sign in to comment.