-
Notifications
You must be signed in to change notification settings - Fork 17
[Binding] enable mlir python binding for graph compiler #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cd8288b
d42bf10
7ff6eb1
73b21a1
4a234fe
21b91ad
05eeb5a
2098f95
2cdc711
dda942c
4e90b53
93fa937
25b7945
36601a8
8561582
6fa08c2
1f1ea6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
/* | ||
* Copyright (C) 2024 Intel Corporation | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef GC_MLIR_C_DIALECTS_H | ||
#define GC_MLIR_C_DIALECTS_H | ||
|
||
#include "mlir-c/IR.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(OneDNNGraph, onednn_graph); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif // GC_MLIR_C_DIALECTS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
/* | ||
* Copyright (C) 2024 Intel Corporation | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef GCEXT_MLIR_C_PASSES_H | ||
#define GCEXT_MLIR_C_PASSES_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "gc/Transforms/Passes.capi.h.inc" | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif // GCEXT_MLIR_C_PASSES_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
add_mlir_dialect(OneDNNGraphOps onednn_graph) | ||
add_mlir_doc(OneDNNGraphOps OneDNNGraphOps gc/Dialect/OneDNNGraph/ -gen-op-doc) | ||
add_mlir_doc(OneDNNGraphDialect OneDNNGraphDialect gc/Dialect/OneDNNGraph/ -gen-dialect-doc) | ||
# add_mlir_doc(OneDNNGraphOps OneDNNGraphOps gc/Dialect/OneDNNGraph/ -gen-op-doc) | ||
# add_mlir_doc(OneDNNGraphDialect OneDNNGraphDialect gc/Dialect/OneDNNGraph/ -gen-dialect-doc) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
set(LLVM_TARGET_DEFINITIONS Passes.td) | ||
mlir_tablegen(Passes.h.inc -gen-pass-decls -name GraphCompiler) | ||
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix GraphCompiler) | ||
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix GraphCompiler) | ||
add_public_tablegen_target(GraphCompilerPassIncGen) | ||
add_mlir_doc(Passes GraphCompilerPasses ./ -gen-pass-doc) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
add_mlir_public_c_api_library(GcCAPI | ||
Dialects.cpp | ||
Passes.cpp | ||
LINK_LIBS PUBLIC | ||
MLIROneDNNGraph | ||
GCPasses | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===-- Dialects.cpp - DESC -------------------------------------*- C++ -*-===// | ||
// | ||
// This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gc-c/Dialects.h" | ||
#include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.h" | ||
#include "mlir/CAPI/Registration.h" | ||
|
||
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(OneDNNGraph, onednn_graph, | ||
mlir::onednn_graph::OneDNNGraphDialect) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===-- Passes.cpp - DESC ---------------------------------------*- C++ -*-===// | ||
// | ||
// This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gc/Transforms/Passes.h" | ||
#include "mlir-c/Pass.h" | ||
#include "mlir/CAPI/Pass.h" | ||
|
||
#include "gc/Transforms/Passes.capi.h.inc" | ||
using namespace mlir::gc; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include "gc/Transforms/Passes.capi.cpp.inc" | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ endif() | |
|
||
include(functions) | ||
|
||
add_subdirectory(CAPI) | ||
add_subdirectory(Dialect) | ||
add_subdirectory(Transforms) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
################################################################################ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# 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. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
################################################################################ | ||
|
||
include(AddMLIRPython) | ||
|
||
# Specifies that all MLIR packages are co-located under the `gc_mlir` | ||
# top level package (the API has been embedded in a relocatable way). | ||
# TODO: Add an upstream cmake param for this vs having a global here. | ||
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=gc_mlir.") | ||
|
||
|
||
################################################################################ | ||
# Sources | ||
################################################################################ | ||
|
||
declare_mlir_python_sources(GcPythonSources) | ||
|
||
declare_mlir_python_sources(GcPythonSources.Common | ||
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gc_mlir" | ||
ADD_TO_PARENT GcPythonSources | ||
SOURCES | ||
__init__.py | ||
dialects/__init__.py | ||
# init hooks | ||
_mlir_libs/_site_initialize_0.py | ||
) | ||
|
||
################################################################################ | ||
# Dialect bindings | ||
################################################################################ | ||
|
||
declare_mlir_dialect_python_bindings( | ||
ADD_TO_PARENT GcPythonSources | ||
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gc_mlir" | ||
TD_FILE dialects/OneDNNGraphOps.td | ||
SOURCES | ||
dialects/onednn_graph.py | ||
DIALECT_NAME onednn_graph | ||
) | ||
|
||
declare_mlir_python_extension(GcPythonSources.Extension | ||
MODULE_NAME _gc_mlir | ||
ADD_TO_PARENT GcPythonSources | ||
SOURCES | ||
MainModule.cpp | ||
EMBED_CAPI_LINK_LIBS | ||
GcCAPI | ||
) | ||
|
||
################################################################################ | ||
# Common CAPI | ||
################################################################################ | ||
|
||
add_mlir_python_common_capi_library(GcPythonCAPI | ||
INSTALL_COMPONENT GcPythonModules | ||
INSTALL_DESTINATION python_packages/gc_mlir_core/gc_mlir/_mlir_libs | ||
OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/python_packages/gc_mlir_core/gc_mlir/_mlir_libs" | ||
RELATIVE_INSTALL_ROOT "../../../.." | ||
DECLARED_SOURCES | ||
GcPythonSources | ||
MLIRPythonExtension.RegisterEverything | ||
MLIRPythonSources.Core | ||
) | ||
|
||
################################################################################ | ||
# Instantiation of all Python modules | ||
################################################################################ | ||
|
||
add_mlir_python_modules(GcPythonModules | ||
ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/gc_mlir_core/gc_mlir" | ||
INSTALL_PREFIX "python_packages/gc_mlir_core/gc_mlir" | ||
DECLARED_SOURCES | ||
GcPythonSources | ||
MLIRPythonExtension.RegisterEverything | ||
MLIRPythonSources | ||
COMMON_CAPI_LINK_LIBS | ||
GcPythonCAPI | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
/* | ||
* Copyright (C) 2024 Intel Corporation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use LLVM license style here. Reference: #85 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #85 is merged. Please rebase this PR and have a license check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. license fixed |
||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "gc-c/Dialects.h" | ||
#include "gc-c/Passes.h" | ||
#include "mlir/Bindings/Python/PybindAdaptors.h" | ||
|
||
PYBIND11_MODULE(_gc_mlir, m) { | ||
m.doc() = "Graph-compiler MLIR Python binding"; | ||
|
||
mlirRegisterGraphCompilerPasses(); | ||
//===----------------------------------------------------------------------===// | ||
// OneDNNGraph | ||
//===----------------------------------------------------------------------===// | ||
|
||
auto onednn_graphM = m.def_submodule("onednn_graph"); | ||
onednn_graphM.def( | ||
"register_dialect", | ||
[](MlirContext context, bool load) { | ||
MlirDialectHandle dialect = mlirGetDialectHandle__onednn_graph__(); | ||
mlirDialectHandleRegisterDialect(dialect, context); | ||
if (load) { | ||
mlirDialectHandleLoadDialect(dialect, context); | ||
} | ||
}, | ||
py::arg("context") = py::none(), py::arg("load") = true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a cmake option to turn off the pybind even if LLVM has python enabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I can add a new cmake option for the gc pybind, should we set the default value to OFF?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ZhennanQin Do we need pybind by default? Or should we only target onednn graph api for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest to enable pybind by default as this is the way for release. For develop purposes, you can turn it off with extra cmake flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
option added in https://github.com/intel/graph-compiler/blob/dda942cb46a0fffb658a3b360ab31fff970e7cd0/CMakeLists.txt#L54C1-L57C8