Skip to content

[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

Merged
merged 17 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
################################################################################
# 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
################################################################################

cmake_minimum_required(VERSION 3.20)
project(GraphCompiler VERSION "0.1.0" LANGUAGES C CXX)

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

option(GC_LEGACY_ENABLE ON)
option(GC_TEST_ENABLE "Build the tests" ON)
option(GC_ENABLE_BINDINGS_PYTHON "Enable Graph Complier Python Binding" ON)
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)

if(GC_LEGACY_ENABLE)
Expand All @@ -33,6 +51,16 @@ include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)

if(GC_ENABLE_BINDINGS_PYTHON AND NOT MLIR_ENABLE_BINDINGS_PYTHON)
message(STATUS "Failed to enable Python API due to the 'MLIR_ENABLE_BINDINGS_PYTHON' for LLVM is not ON.")
set(GC_ENABLE_BINDINGS_PYTHON OFF CACHE BOOL "" FORCE)
endif()

if(GC_ENABLE_BINDINGS_PYTHON)
include(MLIRDetectPythonEnv)
mlir_configure_python_dev_packages()
endif()

include_directories(
${LLVM_INCLUDE_DIRS}
${MLIR_INCLUDE_DIRS}
Expand Down Expand Up @@ -61,6 +89,10 @@ include("cmake/version.cmake")
add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(src)
if(GC_ENABLE_BINDINGS_PYTHON)
message(STATUS "Enabling Python API")
add_subdirectory(python)
endif()

set(GC_LIB_LINKED_LIBS
MLIRLinalgx
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ Graph Compiler supports the following build-time options.
| GC_LEGACY_ENABLE | **ON**, OFF | Controls building the legacy graph-compiler component |
| GC_TEST_ENABLE | **ON**, OFF | Controls building the tests |
| GC_DEV_LINK_LLVM_DYLIB | ON, **OFF** | Controls dynamic link LLVM/MLIR libraries, mainly for developer |
| GC_ENABLE_BINDINGS_PYTHON | **ON**, OFF | Controls building the Python API |

34 changes: 34 additions & 0 deletions include/gc-c/Dialects.h
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
31 changes: 31 additions & 0 deletions include/gc-c/Passes.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
4 changes: 2 additions & 2 deletions include/gc/Dialect/OneDNNGraph/CMakeLists.txt
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)
4 changes: 2 additions & 2 deletions include/gc/Dialect/OneDNNGraph/OneDNNGraphOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Interfaces/DestinationStyleOpInterface.td"
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/OpBase.td"
include "OneDNNGraphDialect.td"
include "OneDNNGraphTypes.td"
include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.td"
include "gc/Dialect/OneDNNGraph/OneDNNGraphTypes.td"

//===----------------------------------------------------------------------===//
// Base OneDNNGraph operation definition.
Expand Down
8 changes: 4 additions & 4 deletions include/gc/Dialect/OneDNNGraph/OneDNNGraphTypes.td
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//===- OneDNNGraphTypes.h - OneDNN input dialect types -----*- tablegen -*-===//
//
//===-- OneDNNGraphTypes.td - DESC -------------------------*- tablegen -*-===//
//
// 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
//
//
//===----------------------------------------------------------------------===//

#ifndef ONEDNNGRAPH_TYPES
#define ONEDNNGRAPH_TYPES

include "mlir/IR/BuiltinTypes.td"
include "mlir/IR/AttrTypeBase.td"
include "OneDNNGraphDialect.td"
include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.td"

//===----------------------------------------------------------------------===//
// OneDNNGraph type definitions
Expand Down
2 changes: 2 additions & 0 deletions include/gc/Transforms/CMakeLists.txt
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)
7 changes: 7 additions & 0 deletions lib/gc/CAPI/CMakeLists.txt
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
)
14 changes: 14 additions & 0 deletions lib/gc/CAPI/Dialects.cpp
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)
24 changes: 24 additions & 0 deletions lib/gc/CAPI/Passes.cpp
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
1 change: 1 addition & 0 deletions lib/gc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ endif()

include(functions)

add_subdirectory(CAPI)
add_subdirectory(Dialect)
add_subdirectory(Transforms)
92 changes: 92 additions & 0 deletions python/CMakeLists.txt
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.")


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?

Copy link
Contributor Author

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?

yes, I can add a new cmake option for the gc pybind, should we set the default value to OFF?

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?

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

################################################################################
# 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
)
43 changes: 43 additions & 0 deletions python/MainModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/*
* Copyright (C) 2024 Intel Corporation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use LLVM license style here. Reference: #85

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#85 is merged. Please rebase this PR and have a license check

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
Loading