Skip to content

Commit f40e433

Browse files
authored
[Binding] enable mlir python binding for graph compiler (#91)
* enable gc mlir python binding * fix * add gc passes binding * add readme * fix license * fix license * add option * fix style * update llvm license path * fix words * update readme * add context init hook * fix license * add __init__.py
1 parent fce4118 commit f40e433

26 files changed

+444
-10
lines changed

CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
################################################################################
2+
# Copyright (C) 2024 Intel Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing,
11+
# software distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions
14+
# and limitations under the License.
15+
# SPDX-License-Identifier: Apache-2.0
16+
################################################################################
17+
118
cmake_minimum_required(VERSION 3.20)
219
project(GraphCompiler VERSION "0.1.0" LANGUAGES C CXX)
320

@@ -10,6 +27,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
1027

1128
option(GC_LEGACY_ENABLE ON)
1229
option(GC_TEST_ENABLE "Build the tests" ON)
30+
option(GC_ENABLE_BINDINGS_PYTHON "Enable Graph Complier Python Binding" ON)
1331
option(GC_DEV_LINK_LLVM_DYLIB "Link dynamic libraries of LLVM and MLIR. For developers only. Do not use it in packing the library." OFF)
1432

1533
if(GC_LEGACY_ENABLE)
@@ -33,6 +51,16 @@ include(AddLLVM)
3351
include(AddMLIR)
3452
include(HandleLLVMOptions)
3553

54+
if(GC_ENABLE_BINDINGS_PYTHON AND NOT MLIR_ENABLE_BINDINGS_PYTHON)
55+
message(STATUS "Failed to enable Python API due to the 'MLIR_ENABLE_BINDINGS_PYTHON' for LLVM is not ON.")
56+
set(GC_ENABLE_BINDINGS_PYTHON OFF CACHE BOOL "" FORCE)
57+
endif()
58+
59+
if(GC_ENABLE_BINDINGS_PYTHON)
60+
include(MLIRDetectPythonEnv)
61+
mlir_configure_python_dev_packages()
62+
endif()
63+
3664
include_directories(
3765
${LLVM_INCLUDE_DIRS}
3866
${MLIR_INCLUDE_DIRS}
@@ -61,6 +89,10 @@ include("cmake/version.cmake")
6189
add_subdirectory(include)
6290
add_subdirectory(lib)
6391
add_subdirectory(src)
92+
if(GC_ENABLE_BINDINGS_PYTHON)
93+
message(STATUS "Enabling Python API")
94+
add_subdirectory(python)
95+
endif()
6496

6597
set(GC_LIB_LINKED_LIBS
6698
MLIRLinalgx

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ Graph Compiler supports the following build-time options.
6363
| GC_LEGACY_ENABLE | **ON**, OFF | Controls building the legacy graph-compiler component |
6464
| GC_TEST_ENABLE | **ON**, OFF | Controls building the tests |
6565
| GC_DEV_LINK_LLVM_DYLIB | ON, **OFF** | Controls dynamic link LLVM/MLIR libraries, mainly for developer |
66+
| GC_ENABLE_BINDINGS_PYTHON | **ON**, OFF | Controls building the Python API |
6667

include/gc-c/Dialects.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions
15+
* and limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
#ifndef GC_MLIR_C_DIALECTS_H
21+
#define GC_MLIR_C_DIALECTS_H
22+
23+
#include "mlir-c/IR.h"
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(OneDNNGraph, onednn_graph);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
#endif // GC_MLIR_C_DIALECTS_H

include/gc-c/Passes.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions
15+
* and limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
#ifndef GCEXT_MLIR_C_PASSES_H
21+
#define GCEXT_MLIR_C_PASSES_H
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#include "gc/Transforms/Passes.capi.h.inc"
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
#endif // GCEXT_MLIR_C_PASSES_H
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
add_mlir_dialect(OneDNNGraphOps onednn_graph)
2-
add_mlir_doc(OneDNNGraphOps OneDNNGraphOps gc/Dialect/OneDNNGraph/ -gen-op-doc)
3-
add_mlir_doc(OneDNNGraphDialect OneDNNGraphDialect gc/Dialect/OneDNNGraph/ -gen-dialect-doc)
2+
# add_mlir_doc(OneDNNGraphOps OneDNNGraphOps gc/Dialect/OneDNNGraph/ -gen-op-doc)
3+
# add_mlir_doc(OneDNNGraphDialect OneDNNGraphDialect gc/Dialect/OneDNNGraph/ -gen-dialect-doc)

include/gc/Dialect/OneDNNGraph/OneDNNGraphOps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ include "mlir/Interfaces/SideEffectInterfaces.td"
1414
include "mlir/Interfaces/DestinationStyleOpInterface.td"
1515
include "mlir/IR/AttrTypeBase.td"
1616
include "mlir/IR/OpBase.td"
17-
include "OneDNNGraphDialect.td"
18-
include "OneDNNGraphTypes.td"
17+
include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.td"
18+
include "gc/Dialect/OneDNNGraph/OneDNNGraphTypes.td"
1919

2020
//===----------------------------------------------------------------------===//
2121
// Base OneDNNGraph operation definition.

include/gc/Dialect/OneDNNGraph/OneDNNGraphTypes.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
//===- OneDNNGraphTypes.h - OneDNN input dialect types -----*- tablegen -*-===//
2-
//
1+
//===-- OneDNNGraphTypes.td - DESC -------------------------*- tablegen -*-===//
2+
//
33
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
//
6+
//
77
//===----------------------------------------------------------------------===//
88

99
#ifndef ONEDNNGRAPH_TYPES
1010
#define ONEDNNGRAPH_TYPES
1111

1212
include "mlir/IR/BuiltinTypes.td"
1313
include "mlir/IR/AttrTypeBase.td"
14-
include "OneDNNGraphDialect.td"
14+
include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.td"
1515

1616
//===----------------------------------------------------------------------===//
1717
// OneDNNGraph type definitions

include/gc/Transforms/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
set(LLVM_TARGET_DEFINITIONS Passes.td)
22
mlir_tablegen(Passes.h.inc -gen-pass-decls -name GraphCompiler)
3+
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix GraphCompiler)
4+
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix GraphCompiler)
35
add_public_tablegen_target(GraphCompilerPassIncGen)
46
add_mlir_doc(Passes GraphCompilerPasses ./ -gen-pass-doc)

lib/gc/CAPI/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_mlir_public_c_api_library(GcCAPI
2+
Dialects.cpp
3+
Passes.cpp
4+
LINK_LIBS PUBLIC
5+
MLIROneDNNGraph
6+
GCPasses
7+
)

lib/gc/CAPI/Dialects.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Dialects.cpp - DESC -------------------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "gc-c/Dialects.h"
10+
#include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.h"
11+
#include "mlir/CAPI/Registration.h"
12+
13+
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(OneDNNGraph, onednn_graph,
14+
mlir::onednn_graph::OneDNNGraphDialect)

lib/gc/CAPI/Passes.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Passes.cpp - DESC ---------------------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "gc/Transforms/Passes.h"
10+
#include "mlir-c/Pass.h"
11+
#include "mlir/CAPI/Pass.h"
12+
13+
#include "gc/Transforms/Passes.capi.h.inc"
14+
using namespace mlir::gc;
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
#include "gc/Transforms/Passes.capi.cpp.inc"
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif

lib/gc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ endif()
44

55
include(functions)
66

7+
add_subdirectory(CAPI)
78
add_subdirectory(Dialect)
89
add_subdirectory(Transforms)

python/CMakeLists.txt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
################################################################################
2+
# Copyright (C) 2024 Intel Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing,
11+
# software distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions
14+
# and limitations under the License.
15+
# SPDX-License-Identifier: Apache-2.0
16+
################################################################################
17+
18+
include(AddMLIRPython)
19+
20+
# Specifies that all MLIR packages are co-located under the `gc_mlir`
21+
# top level package (the API has been embedded in a relocatable way).
22+
# TODO: Add an upstream cmake param for this vs having a global here.
23+
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=gc_mlir.")
24+
25+
26+
################################################################################
27+
# Sources
28+
################################################################################
29+
30+
declare_mlir_python_sources(GcPythonSources)
31+
32+
declare_mlir_python_sources(GcPythonSources.Common
33+
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gc_mlir"
34+
ADD_TO_PARENT GcPythonSources
35+
SOURCES
36+
__init__.py
37+
dialects/__init__.py
38+
# init hooks
39+
_mlir_libs/_site_initialize_0.py
40+
)
41+
42+
################################################################################
43+
# Dialect bindings
44+
################################################################################
45+
46+
declare_mlir_dialect_python_bindings(
47+
ADD_TO_PARENT GcPythonSources
48+
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gc_mlir"
49+
TD_FILE dialects/OneDNNGraphOps.td
50+
SOURCES
51+
dialects/onednn_graph.py
52+
DIALECT_NAME onednn_graph
53+
)
54+
55+
declare_mlir_python_extension(GcPythonSources.Extension
56+
MODULE_NAME _gc_mlir
57+
ADD_TO_PARENT GcPythonSources
58+
SOURCES
59+
MainModule.cpp
60+
EMBED_CAPI_LINK_LIBS
61+
GcCAPI
62+
)
63+
64+
################################################################################
65+
# Common CAPI
66+
################################################################################
67+
68+
add_mlir_python_common_capi_library(GcPythonCAPI
69+
INSTALL_COMPONENT GcPythonModules
70+
INSTALL_DESTINATION python_packages/gc_mlir_core/gc_mlir/_mlir_libs
71+
OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/python_packages/gc_mlir_core/gc_mlir/_mlir_libs"
72+
RELATIVE_INSTALL_ROOT "../../../.."
73+
DECLARED_SOURCES
74+
GcPythonSources
75+
MLIRPythonExtension.RegisterEverything
76+
MLIRPythonSources.Core
77+
)
78+
79+
################################################################################
80+
# Instantiation of all Python modules
81+
################################################################################
82+
83+
add_mlir_python_modules(GcPythonModules
84+
ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/gc_mlir_core/gc_mlir"
85+
INSTALL_PREFIX "python_packages/gc_mlir_core/gc_mlir"
86+
DECLARED_SOURCES
87+
GcPythonSources
88+
MLIRPythonExtension.RegisterEverything
89+
MLIRPythonSources
90+
COMMON_CAPI_LINK_LIBS
91+
GcPythonCAPI
92+
)

python/MainModule.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions
15+
* and limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
#include "gc-c/Dialects.h"
21+
#include "gc-c/Passes.h"
22+
#include "mlir/Bindings/Python/PybindAdaptors.h"
23+
24+
PYBIND11_MODULE(_gc_mlir, m) {
25+
m.doc() = "Graph-compiler MLIR Python binding";
26+
27+
mlirRegisterGraphCompilerPasses();
28+
//===----------------------------------------------------------------------===//
29+
// OneDNNGraph
30+
//===----------------------------------------------------------------------===//
31+
32+
auto onednn_graphM = m.def_submodule("onednn_graph");
33+
onednn_graphM.def(
34+
"register_dialect",
35+
[](MlirContext context, bool load) {
36+
MlirDialectHandle dialect = mlirGetDialectHandle__onednn_graph__();
37+
mlirDialectHandleRegisterDialect(dialect, context);
38+
if (load) {
39+
mlirDialectHandleLoadDialect(dialect, context);
40+
}
41+
},
42+
py::arg("context") = py::none(), py::arg("load") = true);
43+
}

0 commit comments

Comments
 (0)