Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/linux_arm64_wheel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python_version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python_version: ['3.10', '3.11', '3.12']
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .pipelines/templates/onebranch-windows-build-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ stages:
type: windows

variables:
CIBW_BUILD: cp3{8,9,10,11,12}-*amd64
CIBW_BUILD: cp3{10,11,12}-*amd64
CIBW_ARCHS: AMD64
CIBW_ENVIRONMENT: "${{ parameters.CibwEnv }}"
CIBW_BUILD_VERBOSITY: 1
Expand Down
4 changes: 2 additions & 2 deletions .pipelines/wheels_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
timeoutInMinutes: 180
pool: {name: 'Azure-Pipelines-EO-Ubuntu-2004-aiinfra'}
variables:
CIBW_BUILD: "cp3{8,9,10,11,12}-*"
CIBW_BUILD: "cp3{10,11,12}-*"
CIBW_SKIP: "*musllinux*"
CIBW_ENVIRONMENT: "${{ parameters.ExtraEnv }}"

Expand All @@ -30,7 +30,7 @@ jobs:
timeoutInMinutes: 180
pool: {name: 'Azure-Pipelines-EO-Ubuntu-2004-aiinfra'}
variables:
CIBW_BUILD: "cp3{8,9,10,11,12}-*"
CIBW_BUILD: "cp3{10,11,12}-*"
CIBW_SKIP: "*musllinux_*"
# AzureOp doesn't support aaarch64 yet.
# CIBW_ENVIRONMENT: "${{ parameters.ExtraEnv }}"
Expand Down
2 changes: 1 addition & 1 deletion .pipelines/wheels_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
pool:
vmImage: 'macOS-14'
variables:
CIBW_BUILD: "cp3{8,9,10,11,12}-*"
CIBW_BUILD: "cp3{10,11,12}-*"
CIBW_ARCHS_MACOS: "x86_64 universal2 arm64"
CIBW_ENVIRONMENT: "MACOSX_DEPLOYMENT_TARGET=11.0"
# Skip trying to test arm64 builds on Intel Macs
Expand Down
2 changes: 1 addition & 1 deletion .pipelines/wheels_win32.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
timeoutInMinutes: 120
pool: {name: 'onnxruntime-Win-CPU-2022'}
variables:
CIBW_BUILD: "cp3{8,9,10,11,12}-*amd64"
CIBW_BUILD: "cp3{10,11,12}-*amd64"
CIBW_ENVIRONMENT: "${{ parameters.ExtraEnv }}"

steps:
Expand Down
15 changes: 15 additions & 0 deletions .pyproject/cmdclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from setuptools.command.build import build as _build
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.develop import develop as _develop
try:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
except ImportError: # pragma: no cover
_bdist_wheel = None

VSINSTALLDIR_NAME = 'VSINSTALLDIR'
ORTX_USER_OPTION = 'ortx-user-option'
Expand Down Expand Up @@ -316,6 +320,17 @@ def build_cmake(self, extension):
self.spawn([cmake_exe, '--build', str(build_temp)] + build_args)


if _bdist_wheel is not None:
class CmdBdistWheel(_bdist_wheel):
def finalize_options(self):
super().finalize_options()
no_gil = hasattr(sys, "_is_gil_enabled") and not sys._is_gil_enabled()
if sys.version_info >= (3, 12) and not no_gil:
self.py_limited_api = "cp312"


ortx_cmdclass = dict(build=CmdBuild,
develop=CmdDevelop,
build_ext=CmdBuildCMakeExt)
if _bdist_wheel is not None:
ortx_cmdclass["bdist_wheel"] = CmdBdistWheel
6 changes: 3 additions & 3 deletions cgmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@
"component": {
"type": "git",
"git": {
"commitHash": "3e9dfa2866941655c56877882565e7577de6fc7b",
"repositoryUrl": "https://github.com/pybind/pybind11.git"
"commitHash": "e2dc00f7a34f935c6cf91948776d59c4709e9fe6",
"repositoryUrl": "https://github.com/wjakob/nanobind.git"
},
"comments": "v2.10.1"
"comments": "v2.13.0"
}
},
{
Expand Down
45 changes: 17 additions & 28 deletions cmake/ext_python.cmake
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

block(PROPAGATE Python3_FOUND)
set(Python3_FIND_REGISTRY NEVER)
# if we don't set this to NEVER (or possibly LAST) the builds of the wheel for different python versions will fail
# as it will find the system python version first and not the correct python version for the wheel.
set(Python3_FIND_FRAMEWORK NEVER)
find_package(Python3 COMPONENTS Interpreter Development.Module)
endblock()
set(Python_FIND_REGISTRY NEVER)
# if we don't set this to NEVER (or possibly LAST) the builds of the wheel for different python versions will fail
# as it will find the system python version first and not the correct python version for the wheel.
set(Python_FIND_FRAMEWORK NEVER)
if(CMAKE_VERSION VERSION_LESS 3.18)
set(_PYTHON_DEV_COMPONENT Development)
else()
set(_PYTHON_DEV_COMPONENT Development.Module)
endif()
find_package(Python COMPONENTS Interpreter ${_PYTHON_DEV_COMPONENT} OPTIONAL_COMPONENTS Development.SABIModule)

if (NOT Python3_FOUND)
message(FATAL_ERROR "Python3 not found!")
if (NOT Python_FOUND)
message(FATAL_ERROR "Python not found!")
endif()

file(GLOB TARGET_SRC_PYOPS "pyop/pyfunc.cc" "pyop/*.h" "shared/*.cc")
Expand All @@ -21,32 +24,18 @@ if (WIN32)
list(APPEND TARGET_SRC_PYOPS "pyop/extensions_pydll.def")
endif()

add_library(extensions_pydll SHARED ${TARGET_SRC_PYOPS} ${shared_TARGET_LIB_SRC})
message(STATUS "Fetch nanobind")
include(nanobind)

nanobind_add_module(extensions_pydll NB_STATIC STABLE_ABI ${TARGET_SRC_PYOPS} ${shared_TARGET_LIB_SRC})
standardize_output_folder(extensions_pydll)
Comment thread
justinchuby marked this conversation as resolved.
list(APPEND OCOS_COMPILE_DEFINITIONS PYTHON_OP_SUPPORT)
target_compile_definitions(extensions_pydll PRIVATE ${OCOS_COMPILE_DEFINITIONS})

message(STATUS "Fetch pybind11")
include(pybind11)
target_include_directories(extensions_pydll PRIVATE
${pybind11_INCLUDE_DIRS}
$<TARGET_PROPERTY:Python3::Module,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:ocos_operators,INTERFACE_INCLUDE_DIRECTORIES>)

target_compile_definitions(extensions_pydll PRIVATE
$<TARGET_PROPERTY:Python3::Module,INTERFACE_COMPILE_DEFINITIONS>)

target_link_libraries(extensions_pydll PRIVATE Python3::Module ocos_operators)
target_link_libraries(extensions_pydll PRIVATE ocos_operators)

if(OCOS_PYTHON_MODULE_PATH)
get_filename_component(OCOS_PYTHON_MODULE_NAME ${OCOS_PYTHON_MODULE_PATH} NAME)
if(NOT WIN32)
set_target_properties(extensions_pydll PROPERTIES
LIBRARY_OUTPUT_NAME ${OCOS_PYTHON_MODULE_NAME}
PREFIX ""
SUFFIX "")
endif()

add_custom_command(TARGET extensions_pydll POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:extensions_pydll> ${OCOS_PYTHON_MODULE_PATH}
COMMENT "Copying $<TARGET_FILE:extensions_pydll> to ${OCOS_PYTHON_MODULE_PATH}")
Expand Down
11 changes: 11 additions & 0 deletions cmake/externals/nanobind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FetchContent_Declare(
nanobind
GIT_REPOSITORY https://github.com/wjakob/nanobind.git
GIT_TAG v2.13.0
GIT_SHALLOW TRUE
GIT_SUBMODULES_RECURSE TRUE
)

set(NB_TEST OFF CACHE BOOL "" FORCE)
set(NB_USE_SUBMODULE_DEPS ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(nanobind)
10 changes: 0 additions & 10 deletions cmake/externals/pybind11.cmake

This file was deleted.

8 changes: 8 additions & 0 deletions include/onnxruntime_extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
extern "C" {
#endif

#if defined(_WIN32)
#define ORTX_EXPORT __declspec(dllexport)
#else
#define ORTX_EXPORT __attribute__((visibility("default")))
#endif

ORTX_EXPORT
OrtStatus*
ORT_API_CALL RegisterCustomOps(OrtSessionOptions* options, const OrtApiBase* api);

ORTX_EXPORT
int
ORT_API_CALL GetActiveOrtAPIVersion();

Expand Down
69 changes: 44 additions & 25 deletions pyop/py_c_api.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/unordered_map.h>
#include <nanobind/stl/vector.h>
#include <thread>

#include "ortx_utils.h"
Expand All @@ -14,7 +13,8 @@
#include "ortx_cpp_helper.h"
#include "pykernel.h"

namespace py = pybind11;
namespace nb = nanobind;
using namespace nb::literals;

template <typename T>
int64_t NumOfElement(const T& sp) {
Expand All @@ -25,7 +25,39 @@ int64_t NumOfElement(const T& sp) {
return c;
}

void AddGlobalMethodsCApi(pybind11::module& m) {
struct ScopedPyBuffer {
explicit ScopedPyBuffer(PyObject* object, int flags) {
if (PyObject_GetBuffer(object, &view, flags) != 0) {
throw nb::python_error();
}
}

~ScopedPyBuffer() { PyBuffer_Release(&view); }

Py_buffer view{};
};

static nb::module_& GetNumpyModule() {
static nb::module_ numpy = nb::module_::import_("numpy");
return numpy;
}

static const char* GetNumpyDTypeName(extDataType_t tensor_type) {
switch (tensor_type) {
case extDataType_t::kOrtxFloat:
return "float32";
case extDataType_t::kOrtxInt64:
return "int64";
case extDataType_t::kOrtxUint8:
return "uint8";
case extDataType_t::kOrtxUint32:
return "uint32";
default:
throw std::runtime_error("unsupported tensor type");
}
}

void AddGlobalMethodsCApi(nb::module_& m) {
m.def(
"create_processor",
[](const char* processor_def_json) {
Expand Down Expand Up @@ -72,7 +104,7 @@ void AddGlobalMethodsCApi(pybind11::module& m) {

m.def(
"tensor_result_get_at",
[](std::uintptr_t result_h, size_t index) -> py::object {
[](std::uintptr_t result_h, size_t index) -> nb::object {
OrtxTensorResult* result = reinterpret_cast<OrtxTensorResult*>(result_h);
OrtxTensor* tensor{};
auto err = OrtxTensorResultGetAt(result, index, &tensor);
Expand All @@ -89,7 +121,7 @@ void AddGlobalMethodsCApi(pybind11::module& m) {
if (tensor_type == extDataType_t::kOrtxString) {
const char* str{};
OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&str), nullptr, nullptr);
return py::str(str);
return nb::str(str);
} else if (tensor_type == extDataType_t::kOrtxInt64 || tensor_type == extDataType_t::kOrtxFloat ||
tensor_type == extDataType_t::kOrtxUint8 || tensor_type == extDataType_t::kOrtxUint32) {
OrtxGetTensorData(tensor, reinterpret_cast<const void**>(&data), &shape, &num_dims);
Expand All @@ -102,22 +134,9 @@ void AddGlobalMethodsCApi(pybind11::module& m) {
for (auto n = num_dims - num_dims; n < num_dims; ++n) {
npy_dims.push_back(shape[n]);
}
py::array obj{};

if (tensor_type == extDataType_t::kOrtxFloat) {
obj = py::array_t<float>(npy_dims);
} else if (tensor_type == extDataType_t::kOrtxInt64) {
obj = py::array_t<int64_t>(npy_dims);
} else if (tensor_type == extDataType_t::kOrtxUint8) {
obj = py::array_t<uint8_t>(npy_dims);
} else if (tensor_type == extDataType_t::kOrtxUint32) {
obj = py::array_t<uint32_t>(npy_dims);
} else {
throw std::runtime_error("unsupported tensor type");
}

void* out_ptr = obj.mutable_data();
memcpy(out_ptr, data, NumOfElement(npy_dims) * elem_size);
nb::object obj = GetNumpyModule().attr("empty")(nb::cast(npy_dims), "dtype"_a = GetNumpyDTypeName(tensor_type));
ScopedPyBuffer buffer(obj.ptr(), PyBUF_WRITABLE);
memcpy(buffer.view.buf, data, NumOfElement(npy_dims) * elem_size);
return obj;
},
"Get tensor at index.");
Expand Down
Loading
Loading