forked from openxla/stablehlo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Python example (CMake) (openxla#2113)
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
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |