主要内容

pydlpack

R2026b

Convert MATLAB GPU array to Python DLPack capsule

Since R2026b

    Description

    dlp = pydlpack(mla) converts the specified MATLAB® array to a Python® DLPack capsule.

    You can use the output DLPack capsule to exchange data between MATLAB and Python frameworks, including TensorFlow™. To transfer data to PyTorch®, use the arrayToTorchTensor (Deep Learning Toolbox) function instead.

    Note

    The pydlpack function supports sending data to Python environments running in out-of-process execution mode only. To configure the Python interpreter to use out-of-process execution mode, call the pyenv function and set the ExecutionMode name-value argument to "OutOfProcess".

    example

    Examples

    collapse all

    Convert a gpuArray to a DLPack capsule.

    X = rand(1000,"gpuArray");
    dlpack = pydlpack(X);

    Convert a gpuArray to a DLPack capsule.

    X = rand(1000,"gpuArray");
    dlpack = pydlpack(X);

    Construct a Python array by calling from_dlpack on the DLPack capsule. The from_dlpack function consumes the capsule, so you cannot use the capsule again.

    pyX = py.myPythonLibrary.from_dlpack(dlpack);

    Operate on the Python array, on the GPU, using Python functions. Replace the call to myFunctions.proccessData with your desired Python functions.

    pyZ = py.myFunctions.processData(pyY)
    

    To prepare the data for transfer back to MATLAB, use it to create a DLPack capsule. Alternatively, you can make your data a cupy.ndarray.

    pyDlpack = py.myFunctions.to_dlpack(pyZ)
    

    Transfer the resulting Python data back to MATLAB.

    Z = gpuArray(pyDlpack);
    

    Input Arguments

    collapse all

    Input array to convert, specified as a gpuArray object.

    Complex inputs must be single or double.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
    Complex Number Support: Yes

    Output Arguments

    collapse all

    Output object, returned as a DLPack capsule.

    A DLPack capsule is an opaque Python object that represents a multidimensional array.

    Note

    DLPack capsules can be consumed by a call to from_dlpack or gpuArray only once.

    The data type the capsule represents depends on the underlying type of the input array.

    MATLAB Input Array Underlying TypeResulting Python Data Type
    double

    float64

    single

    float32

    int8

    int8

    uint8

    uint8

    int16

    int16

    uint16

    uint16

    int32

    int32

    uint32

    uint32

    int64

    int64

    uint64

    uint64

    double (complex)

    complex128

    single (complex)

    complex64

    logical

    bool

    Limitations

    • Sparse arrays are not supported.

    Extended Capabilities

    expand all

    GPU Arrays
    Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

    Version History

    Introduced in R2026b