Skip to content

Commit

Permalink
Support numpy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xadupre committed Jun 18, 2024
1 parent d836302 commit 55c309e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 30 deletions.
66 changes: 65 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,71 @@ jobs:
artifactName: 'wheel-linux-pip-$(python.version)'
targetPath: 'dist'

- job: 'TestLinux311'
pool:
vmImage: 'ubuntu-latest'
strategy:
matrix:
Python311-Linux:
python.version: '3.11'
maxParallel: 3

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python.version)'
architecture: 'x64'
- script: sudo apt-get update
displayName: 'AptGet Update'
# - script: sudo apt-get install -y pandoc
# displayName: 'Install Pandoc'
# - script: sudo apt-get install -y inkscape
# displayName: 'Install Inkscape'
- script: sudo apt-get install -y graphviz
displayName: 'Install Graphviz'
- script: python -m pip install --upgrade pip setuptools wheel
displayName: 'Install tools'
- script: pip install -r requirements.txt
displayName: 'Install Requirements'
- script: pip install -r requirements-dev.txt
displayName: 'Install Requirements dev'
- script: |
ruff .
displayName: 'Ruff'
- script: |
black --diff .
displayName: 'Black'
- script: |
cmake-lint _cmake/Find* --disabled-codes C0103 C0113 --line-width=88
cmake-lint _cmake/CMake* --disabled-codes C0103 C0113 --line-width=88
displayName: 'cmake-lint'
- script: |
cython-lint .
displayName: 'cython-lint'
- script: |
# python -m pip install -e .
python setup.py build_ext --inplace
displayName: 'build inplace'
- bash: |
contents=$(cat .build_path.txt)
export BUILD_PATH="$contents"
cd $BUILD_PATH
ctest --rerun-failed --output-on-failure
displayName: 'Run C++ Unit Tests'
- script: |
python -m pip install "numpy<2.0"
displayName: 'install numpy<2'
- script: |
python -m pytest _unittests --durations=10
displayName: 'Runs Unit Tests'
- script: |
python -u setup.py bdist_wheel
displayName: 'Build Package'
- script: |
cd dist
python -m pytest ../_unittests
displayName: 'check unit test with the whl'
- job: 'TestLinux'
pool:
vmImage: 'ubuntu-latest'
Expand Down Expand Up @@ -121,7 +186,6 @@ jobs:
cd $BUILD_PATH
ctest --rerun-failed --output-on-failure
displayName: 'Run C++ Unit Tests'
- script: |
python -m pytest _unittests --durations=10
displayName: 'Runs Unit Tests'
Expand Down
30 changes: 15 additions & 15 deletions onnx_extended/ortcy/wrap/ortinf.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy
cimport numpy
cimport numpy as cnumpy
cimport cython
from libc.stdint cimport int64_t
from libc.stdlib cimport free, malloc
Expand All @@ -11,7 +11,7 @@ from cpython.buffer cimport (
PyBUF_ANY_CONTIGUOUS,
PyBUF_SIMPLE,
)
numpy.import_array()
# numpy.import_array()


cdef extern from "<string>" namespace "std":
Expand Down Expand Up @@ -273,7 +273,7 @@ cdef class OrtSession:
)
cdef OrtShape shapes[10]
cdef OrtCpuValue in_values[10]
cdef numpy.ndarray value
cdef cnumpy.ndarray value

values = []
for n in range(len(inputs)):
Expand All @@ -299,8 +299,8 @@ cdef class OrtSession:
# onnxruntime does not implement the DLPack protocol through the C API.
# DLPack protocol should be used.

cdef numpy.ndarray[numpy.int64_t, ndim=1] shape
cdef numpy.ndarray tout
cdef cnumpy.ndarray[cnumpy.int64_t, ndim=1] shape
cdef cnumpy.ndarray tout
res = list()
for i in range(n_outputs):
shape = numpy.empty(out_shapes[i].ndim(), dtype=numpy.int64)
Expand All @@ -323,7 +323,7 @@ cdef class OrtSession:
@cython.wraparound(False)
def run_1_1(
self,
numpy.ndarray input1,
cnumpy.ndarray input1,
):
"""
Runs the inference assuming the model has one input and one output.
Expand All @@ -334,7 +334,7 @@ cdef class OrtSession:
for i in range(input1.ndim):
shapes[0].set(i, input1.shape[i])

cdef numpy.ndarray value1 = numpy.ascontiguousarray(input1)
cdef cnumpy.ndarray value1 = numpy.ascontiguousarray(input1)
cdef OrtCpuValue in_values[1]

in_values[0].init(
Expand All @@ -355,12 +355,12 @@ cdef class OrtSession:
# onnxruntime does not implement the DLPack protocol through the C API.
# DLPack protocol should be used.

cdef numpy.ndarray[numpy.int64_t, ndim=1] shape = numpy.empty(
cdef cnumpy.ndarray[cnumpy.int64_t, ndim=1] shape = numpy.empty(
out_shapes[0].ndim(), dtype=numpy.int64)
memcpy(shape.data,
out_shapes[0].dims(),
out_shapes[0].ndim() * 8) # 8 = sizeof(int64)
cdef numpy.ndarray tout = numpy.empty(
cdef cnumpy.ndarray tout = numpy.empty(
shape=shape,
dtype=OrtSession._dtypes[out_values[0].elem_type()]
)
Expand All @@ -375,8 +375,8 @@ cdef class OrtSession:
@cython.wraparound(False)
def run_2(
self,
numpy.ndarray input1,
numpy.ndarray input2
cnumpy.ndarray input1,
cnumpy.ndarray input2
):
"""
Runs the inference assuming the model has two inputs.
Expand All @@ -391,8 +391,8 @@ cdef class OrtSession:
for i in range(input2.ndim):
shapes[1].set(i, input2.shape[i])

cdef numpy.ndarray value1 = numpy.ascontiguousarray(input1)
cdef numpy.ndarray value2 = numpy.ascontiguousarray(input2)
cdef cnumpy.ndarray value1 = numpy.ascontiguousarray(input1)
cdef cnumpy.ndarray value2 = numpy.ascontiguousarray(input2)

cdef OrtCpuValue in_values[2]
in_values[0].init(
Expand All @@ -417,8 +417,8 @@ cdef class OrtSession:
# onnxruntime does not implement the DLPack protocol through the C API.
# DLPack protocol should be used.

cdef numpy.ndarray[numpy.int64_t, ndim=1] shape
cdef numpy.ndarray tout
cdef cnumpy.ndarray[cnumpy.int64_t, ndim=1] shape
cdef cnumpy.ndarray tout
res = list()
for i in range(n_outputs):
shape = numpy.empty(out_shapes[i].ndim(), dtype=numpy.int64)
Expand Down
12 changes: 6 additions & 6 deletions onnx_extended/validation/cython/fp8.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import numpy
cimport numpy
cimport numpy as cnumpy
cimport cython
from libcpp cimport bool
from cython.cimports.libc.stdint import uint8_t, int64_t

numpy.import_array()
# numpy.import_array()


cdef extern from "cpu/cast_fp8.h":
Expand All @@ -23,8 +23,8 @@ def cast_float32_to_e4m3fn(m, bool saturate = True):
:param saturate: saturate the conversion
:return: casted array
"""
cdef numpy.ndarray cm = numpy.ascontiguousarray(m)
cdef numpy.ndarray res = numpy.empty(m.shape, dtype=numpy.uint8)
cdef cnumpy.ndarray cm = numpy.ascontiguousarray(m)
cdef cnumpy.ndarray res = numpy.empty(m.shape, dtype=numpy.uint8)
cdef int64_t n = m.size
cdef const float* src = <float*> cm.data
cdef uint8_t* dst = <uint8_t*> res.data
Expand All @@ -43,8 +43,8 @@ def cast_e4m3fn_to_float32(m):
:param m: any array
:return: casted array
"""
cdef numpy.ndarray cm = numpy.ascontiguousarray(m)
cdef numpy.ndarray res = numpy.empty(m.shape, dtype=numpy.float32)
cdef cnumpy.ndarray cm = numpy.ascontiguousarray(m)
cdef cnumpy.ndarray res = numpy.empty(m.shape, dtype=numpy.float32)
cdef int64_t n = m.size
cdef const uint8_t* src = <uint8_t*> cm.data
cdef float* dst = <float*> res.data
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ dev = [
requires = [
"abi3audit; sys_platform == 'linux'",
"auditwheel-symbols; sys_platform == 'linux'",
"Cython",
"Cython>=3.0.10",
"cmake",
"numpy",
"numpy>=2.0",
"onnx",
"pybind11",
"scipy",
"pybind11>=2.12",
"scipy>=1.13.1",
"setuptools",
"wheel",
]
Expand Down
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ black
clang-format
cmakelang
coverage
cython>=3.0
cython>=3.0.10
cython-lint
flake8
furo; sys_platform == 'linux'
Expand All @@ -25,7 +25,7 @@ pytest-cov
pytest-subtests
rstcheck[sphinx,toml]
ruff
scikit-learn
scikit-learn>=1.5
skl2onnx>=1.14.1
sphinx!=7.2.0; sys_platform == 'linux'
sphinx-gallery; sys_platform == 'linux'
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
numpy
numpy>=2.0
onnx>=1.15.0
scipy
scipy>=1.13.1

0 comments on commit 55c309e

Please sign in to comment.