-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support string type inputs from Python application code. (#2629)
* Support string type inputs from Python application code. --------- Signed-off-by: Yasushi Negishi <negishi@jp.ibm.com>
- Loading branch information
Showing
20 changed files
with
349 additions
and
166 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,120 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
file(GENERATE | ||
OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/PyRuntime.py | ||
INPUT ${CMAKE_CURRENT_SOURCE_DIR}/PyRuntime.py | ||
) | ||
|
||
file(GENERATE | ||
OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/PyCompileAndRuntime.py | ||
INPUT ${CMAKE_CURRENT_SOURCE_DIR}/PyCompileAndRuntime.py | ||
) | ||
|
||
# TODO: Remove pybind11::python_link_helper after cmake version bumped to 3.16+ | ||
add_onnx_mlir_library(OMPyExecutionSessionBase | ||
PyExecutionSessionBase.cpp | ||
|
||
EXCLUDE_FROM_OM_LIBS | ||
|
||
LINK_LIBS PUBLIC | ||
OMExecutionSession | ||
OMMlirUtilities | ||
pybind11::embed | ||
pybind11::python_link_helper | ||
) | ||
if(MSVC) | ||
target_link_libraries(OMPyExecutionSessionBase | ||
PRIVATE pybind11::windows_extras | ||
) | ||
endif() | ||
set_target_properties(OMPyExecutionSessionBase | ||
PROPERTIES | ||
POSITION_INDEPENDENT_CODE TRUE | ||
) | ||
target_compile_definitions(OMPyExecutionSessionBase | ||
PRIVATE | ||
$<TARGET_PROPERTY:onnx,COMPILE_DEFINITIONS> | ||
) | ||
target_include_directories(OMPyExecutionSessionBase | ||
PRIVATE | ||
$<TARGET_PROPERTY:onnx,INCLUDE_DIRECTORIES> | ||
) | ||
|
||
# When running on ubi8 image, shared lib backend tests fail with | ||
# the following error: | ||
# | ||
# [libprotobuf ERROR google/protobuf/descriptor_database.cc:641] File already exists in database: onnx/onnx-ml.proto | ||
# [libprotobuf FATAL google/protobuf/descriptor.cc:1371] CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size): | ||
# terminate called after throwing an instance of 'google::protobuf::FatalException' | ||
# what(): CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size): | ||
# Aborted (core dumped) | ||
# | ||
# This is because test.py loads (among others) the following | ||
# two .so shared libs: | ||
# | ||
# - onnx_cpp2py_export.cpython-39-s390x-linux-gnu.so | ||
# (import onnx) | ||
# - PyRuntimeC.cpython-39-s390x-linux-gnu.so | ||
# (from PyRuntimeC import OMExecutionSession) | ||
# | ||
# Both libs share the same libprotobuf.so when loaded by test.py. | ||
# However, they were both built with the same onnx-ml.pb.cc generated | ||
# from onnx-ml.proto and the protobuf runtime requires all compiled-in | ||
# .proto files have unique names. Hence the error. | ||
# | ||
# PyRuntimeC doesn't really need onnx beyond the onnx::TensorProto::* | ||
# types so we remove onnx from its target_link_libraries. But that | ||
# also removes some of the compile definitions and include directories | ||
# which we add back through target_compile_definitions and | ||
# target_include_directories. | ||
pybind11_add_module(PyRuntimeC PyExecutionSession.cpp) | ||
add_dependencies(PyRuntimeC onnx_proto) | ||
target_compile_options(PyRuntimeC | ||
PRIVATE | ||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-frtti -fexceptions> | ||
$<$<CXX_COMPILER_ID:MSVC>:/EHsc /GR> | ||
) | ||
target_compile_definitions(PyRuntimeC | ||
PRIVATE | ||
$<TARGET_PROPERTY:onnx,COMPILE_DEFINITIONS> | ||
) | ||
target_include_directories(PyRuntimeC | ||
PRIVATE | ||
$<TARGET_PROPERTY:onnx,INCLUDE_DIRECTORIES> | ||
) | ||
target_link_libraries(PyRuntimeC | ||
PRIVATE | ||
OMPyExecutionSessionBase | ||
) | ||
llvm_update_compile_flags(PyRuntimeC) | ||
|
||
install(TARGETS PyRuntimeC | ||
DESTINATION lib | ||
) | ||
|
||
pybind11_add_module(PyCompileAndRuntimeC PyOMCompileExecutionSession.cpp) | ||
add_dependencies(PyCompileAndRuntimeC onnx_proto) | ||
target_compile_options(PyCompileAndRuntimeC | ||
PRIVATE | ||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-frtti -fexceptions> | ||
$<$<CXX_COMPILER_ID:MSVC>:/EHsc /GR> | ||
) | ||
target_compile_definitions(PyCompileAndRuntimeC | ||
PRIVATE | ||
$<TARGET_PROPERTY:onnx,COMPILE_DEFINITIONS> | ||
) | ||
target_include_directories(PyCompileAndRuntimeC | ||
PRIVATE | ||
${ONNX_MLIR_SRC_ROOT}/include | ||
$<TARGET_PROPERTY:onnx,INCLUDE_DIRECTORIES> | ||
) | ||
target_link_libraries(PyCompileAndRuntimeC | ||
PRIVATE | ||
OMCompiler | ||
OMPyExecutionSessionBase | ||
) | ||
llvm_update_compile_flags(PyCompileAndRuntimeC) | ||
|
||
install(TARGETS PyCompileAndRuntimeC | ||
DESTINATION lib | ||
) |
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,35 @@ | ||
#!/usr/bin/env python3 | ||
|
||
############# PyCompileAndRuntime.py ####################################### | ||
# | ||
# Copyright 2021-2024 The IBM Research Authors. | ||
# | ||
################################################################################ | ||
# commom class `PyOMRuntime` called by python scripts | ||
################################################################################ | ||
import numpy as np | ||
|
||
try: | ||
from PyCompileAndRuntimeC import ( | ||
OMCompileExecutionSession as OMCompileExecutionSession_, | ||
) | ||
except ImportError: | ||
raise ImportError( | ||
"Looks like you did not build the PyCompileAndRuntimeC target, build it by running `make PyCompileAndRuntimeC`." | ||
"You may need to set ONNX_MLIR_HOME to `onnx-mlir/build/Debug` since `make PyCompileAndRuntimeC` outputs to `build/Debug` by default" | ||
) | ||
|
||
|
||
class OMCompileExecutionSession(OMCompileExecutionSession_): | ||
def run(self, inputs): | ||
# Prepare arguments to call sess.run | ||
pyrun_inputs = [] | ||
pyrun_shapes = [] | ||
pyrun_strides = [] | ||
for inp in inputs: | ||
pyrun_inputs.append(inp.ravel()) | ||
pyrun_shapes.append(np.array(inp.shape, dtype=np.int64)) | ||
pyrun_strides.append(np.array(inp.strides, dtype=np.int64)) | ||
return super(OMCompileExecutionSession, self).run( | ||
pyrun_inputs, pyrun_shapes, pyrun_strides | ||
) |
File renamed without changes.
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
Oops, something went wrong.