-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Python bindings for MHLO (chlo and mhlo dialects).
PiperOrigin-RevId: 387406846
- Loading branch information
Stella Laurenzo
authored and
TensorFlow MLIR Team
committed
Jul 28, 2021
1 parent
47334b6
commit c65dc7a
Showing
14 changed files
with
386 additions
and
21 deletions.
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
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,43 @@ | ||
# Copyright 2021 The TensorFlow 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 | ||
# | ||
# http://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. | ||
# ============================================================================== | ||
|
||
"""Simple smoketest for the Python API.""" | ||
|
||
# TODO(laurenzo): This should be a real test but we don't have enough build | ||
# support for it yet. | ||
|
||
# pylint: disable=wildcard-import,undefined-variable | ||
|
||
from mlir.dialects.chlo import * | ||
from mlir.dialects.mhlo import * | ||
from mlir.ir import * | ||
|
||
ASM = """ | ||
func @dynamicBroadcast(%arg0: tensor<?xf32>, %arg1: tensor<?x?xf32>) -> tensor<?x?xf32> { | ||
%0 = chlo.broadcast_add %arg0, %arg1 : (tensor<?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32> | ||
return %0 : tensor<?x?xf32> | ||
} | ||
""" | ||
|
||
with Context() as context: | ||
register_chlo_dialect(context) | ||
register_mhlo_dialect(context) | ||
|
||
m = Module.parse(ASM) | ||
print(m) | ||
add_op = m.body.operations[0].regions[0].blocks[0].operations[0] | ||
print(repr(add_op)) | ||
print(add_op) | ||
print("Everything works") |
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,29 @@ | ||
/* Copyright 2021 The TensorFlow 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 | ||
http://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. | ||
==============================================================================*/ | ||
|
||
#ifndef TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_C_DIALECTS_H_ | ||
#define TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_C_DIALECTS_H_ | ||
|
||
#include "mlir-c/Registration.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Chlo, chlo); | ||
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Mhlo, mhlo); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // TENSORFLOW_COMPILER_MLIR_HLO_INCLUDE_MLIR_HLO_C_DIALECTS_H_ |
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,6 @@ | ||
add_mlir_public_c_api_library(MLIRHLOCAPIDialects | ||
Dialects.cpp | ||
LINK_LIBS PUBLIC | ||
ChloDialect | ||
MhloDialect | ||
) |
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,20 @@ | ||
/* Copyright 2021 The TensorFlow 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 | ||
http://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. | ||
==============================================================================*/ | ||
|
||
#include "mlir-hlo-c/Dialects.h" | ||
|
||
#include "mlir-hlo/Dialect/mhlo/IR/chlo_ops.h" | ||
#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h" | ||
#include "mlir/CAPI/Registration.h" | ||
|
||
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Chlo, chlo, mlir::chlo::HloClientDialect) | ||
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Mhlo, mhlo, mlir::mhlo::MhloDialect) |
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 |
---|---|---|
@@ -1,9 +1,67 @@ | ||
include(AddMLIRPython) | ||
|
||
declare_mlir_python_sources(MLIRHLOPythonSources) | ||
declare_mlir_python_sources(MLIRHLOPythonExtensions) | ||
|
||
################################################################################ | ||
# Generate dialect-specific bindings. | ||
################################################################################ | ||
|
||
add_mlir_dialect_python_bindings(MLIRBindingsPythonMhloOps | ||
TD_FILE MhloOps.td | ||
DIALECT_NAME mhlo_dialect) | ||
declare_mlir_python_sources(MLIRHLOPythonSources.Dialects | ||
ADD_TO_PARENT MLIRHLOPythonSources | ||
) | ||
|
||
declare_mlir_dialect_python_bindings( | ||
ADD_TO_PARENT MLIRHLOPythonSources.Dialects | ||
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir" | ||
TD_FILE dialects/MhloOps.td | ||
SOURCES dialects/mhlo.py | ||
DIALECT_NAME mhlo) | ||
|
||
declare_mlir_dialect_python_bindings( | ||
ADD_TO_PARENT MLIRHLOPythonSources.Dialects | ||
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir" | ||
TD_FILE dialects/ChloOps.td | ||
SOURCES dialects/chlo.py | ||
DIALECT_NAME chlo) | ||
|
||
################################################################################ | ||
# Extensions | ||
################################################################################ | ||
|
||
declare_mlir_python_extension(MLIRHLOPythonExtensions.Main | ||
MODULE_NAME _mlirHlo | ||
ADD_TO_PARENT MLIRHLOPythonExtensions | ||
SOURCES | ||
${CMAKE_CURRENT_SOURCE_DIR}/MlirHloModule.cpp | ||
EMBED_CAPI_LINK_LIBS | ||
MLIRHLOCAPIDialects | ||
) | ||
|
||
################################################################################ | ||
# Generate packages and shared library | ||
################################################################################ | ||
|
||
add_mlir_python_common_capi_library(MLIRHLOCAPI | ||
INSTALL_COMPONENT MLIRHLOPythonModules | ||
INSTALL_DESTINATION python_packages/mlir_hlo/mlir/_mlir_libs | ||
OUTPUT_DIRECTORY "${MLIR_HLO_BINARY_DIR}/python_packages/mlir_hlo/mlir/_mlir_libs" | ||
RELATIVE_INSTALL_ROOT "../../../.." | ||
DECLARED_SOURCES | ||
MLIRPythonSources | ||
MLIRPythonExtension.AllPassesRegistration | ||
MLIRHLOPythonSources | ||
MLIRHLOPythonExtensions | ||
) | ||
|
||
add_mlir_python_modules(MLIRHLOPythonModules | ||
ROOT_PREFIX "${MLIR_HLO_BINARY_DIR}/python_packages/mlir_hlo/mlir" | ||
INSTALL_PREFIX "python_packages/mlir_hlo/mlir" | ||
DECLARED_SOURCES | ||
MLIRPythonSources | ||
MLIRPythonExtension.AllPassesRegistration | ||
MLIRHLOPythonSources | ||
MLIRHLOPythonExtensions | ||
COMMON_CAPI_LINK_LIBS | ||
MLIRHLOCAPI | ||
) |
Oops, something went wrong.