Provides a common interface for declaring that a tensor type is compatible with DLPack
.
This includes Array
and CUDA.CuArray
.
pkg> add DLPackInterface
Call dlinfo(x)
. It returns a NamedTuple of information about x
if it
satisfies the DLPack interface. Otherwise, it returns nothing
.
Alternatively you can call t = dltensor(x)
. If x
is a tensor, this yields a view of x
which can be queried for information (otherwise returns nothing
):
dldata(t)
dldataoffset(t)
dldevice(t)
dldatatype(t)
dlndim(t)
dlshape(t, i)
anddlshape(t)
dlstride(t, i)
anddlstrides(t)
dllength(t)
dlsizeof(t)
To implement the interface for type T
, you must define the following functions:
dltensor(x::T) -> t
ornothing
ifx
is not a tensor; it is reasonable to returnx
dldata(t)::Ptr{Cvoid}
(defaults toconvert(Ptr{Cvoid}, pointer(t))
)dldataoffset(t)::Int
(defaults to 0)dldevice(t)::DLDevice
(defaults toDLDevice(DL_CPU)
)dldatatype(t)::DLDataType
(defaults toDLDataType(eltype(t))
)dlndim(t)::Int
(defaults toInt(ndims(t))
)dlshape(t, i)::Int
(defaults toInt(size(t, i))
)dlstride(t, i)::Int
(defaults toInt(stride(t, i))
)