Skip to content

Commit a0b840b

Browse files
Feature/dlpack (#682)
* Added tensor._dlpack extension, vendored dlpack header Added dlpack's license file in include/dlpack/ Added note in README.md about the location of the origin LICENSE file from which the copy was made Add _dlpack.pyx to flake exception file Implemented to_dlpack_capsule/from_dlpack_capsule functions * Implemented usm_ndarray.__dlpack__, usm_ndarray.__dlpack_device__ * Implemented from_dlpack(array) * Exported dpctl.tensor.from_dlpack * DLManagedTensor lifetime management implemented per array-API specs 1. The pycapsule destructor only calls DLManagedTensor.deleter is the name is "dltensor" 2. Code consuming the DLPack capsule renamed the capsule (to avoid destructor calling the deleter) and instead creates an internal object to do that and uses that internal object as the base of _Memory object `from_dlpack_capsule` function should handle NULL data field For zero-elements arrays in DLPack, allocate 1 element Proper support for strides added. Expanded docstring of `dpctl.tensor.from_dlpack` * Adding test_usm_ndarray_dlpack Test should anticipate that dlpack roundtripping changes bool dtype to uint8 Adding test for `from_dlpack` input validation * dlpack support noted in changelog * Addressed PR feedback 1. Added docstrings 2. Exported DLPackCreationError in `_dlpack.pxd` 3. Added validation in `__dlpack_device__` to raise an error if device_id came back -1 (not-found) 4. `to_dlpack_capsule(usm_ary)` raises DLPackCreationError if the array context is not the default context (the one created in dpctl.SyclQueue(dev) call) * Make sure that dpctl/tensor/include/dlpack is included in the layout * fixed typo in docstring * Addressed docstrings/formatting PR feedback * Applied consistent style for references
2 parents a8c91c7 + 49293af commit a0b840b

File tree

13 files changed

+1083
-2
lines changed

13 files changed

+1083
-2
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ per-file-ignores =
2323
dpctl/memory/_memory.pyx: E999, E225, E226, E227
2424
dpctl/program/_program.pyx: E999, E225, E226, E227
2525
dpctl/tensor/_usmarray.pyx: E999, E225, E226, E227
26+
dpctl/tensor/_dlpack.pyx: E999, E225, E226, E227
2627
dpctl/tensor/numpy_usm_shared.py: F821
2728
dpctl/tests/_cython_api.pyx: E999, E225, E227, E402
2829
dpctl/utils/_compute_follows_data.pyx: E999, E225, E227

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- `dpctl.tensor.asarray`, `dpctl.tensor.empty` implemented (#646).
11+
- `dpctl.tensor.usm_ndarray` adds support for DLPack protocol. `dpctl.tensor.from_dlpack` implemented (#682).
1112

1213
### Changed
1314
- dpctl-capi is now renamed to `libsyclinterface` (#666).

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include versioneer.py
22
recursive-include dpctl/include *.h
3+
recursive-include dpctl/tensor/include *
34
recursive-include dpctl *.pxd
45
include dpctl/_sycl_context.h
56
include dpctl/_sycl_context_api.h

dpctl/tensor/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from dpctl.tensor._copy_utils import copy_to_numpy as asnumpy
3535
from dpctl.tensor._copy_utils import copy_to_numpy as to_numpy
3636
from dpctl.tensor._ctors import asarray, empty
37+
from dpctl.tensor._dlpack import from_dlpack
3738
from dpctl.tensor._reshape import reshape
3839
from dpctl.tensor._usmarray import usm_ndarray
3940

@@ -47,4 +48,5 @@
4748
"from_numpy",
4849
"to_numpy",
4950
"asnumpy",
51+
"from_dlpack",
5052
]

dpctl/tensor/_dlpack.pxd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# distutils: language = c++
18+
# cython: language_level=3
19+
# cython: linetrace=True
20+
21+
from ._usmarray cimport usm_ndarray
22+
23+
24+
cdef extern from './include/dlpack/dlpack.h' nogil:
25+
int device_CPU 'kDLCPU'
26+
int device_oneAPI 'kDLOneAPI'
27+
int device_OpenCL 'kDLOpenCL'
28+
29+
30+
cpdef object to_dlpack_capsule(usm_ndarray array) except +
31+
cpdef usm_ndarray from_dlpack_capsule(object dltensor) except +
32+
33+
cpdef from_dlpack(array)
34+
35+
cdef class DLPackCreationError(Exception):
36+
"""
37+
A DLPackCreateError exception is raised when constructing
38+
DLPack capsule from `usm_ndarray` based on a USM allocation
39+
on a partitioned SYCL device.
40+
"""
41+
pass

0 commit comments

Comments
 (0)