abracudabra.device.conversion#
Move an array, series, or tensor to a device.
Functions#
|
Convert a sequence to a cupy array. |
|
Move a NumPy/CuPy array to a device. |
Move a Pandas/cuDF index, series or dataframe to a device. |
|
|
Move a Torch tensor to a device. |
|
Move an array, series, or tensor to a device. |
Module Contents#
- abracudabra.device.conversion.to_cupy_array(sequence, /, device_idx=None)[source]#
Convert a sequence to a cupy array.
- Parameters:
sequence (object) – The sequence to convert.
device_idx (int | None) – The device index to move the array to.
- Returns:
The sequence as a cupy array.
- Return type:
object
- abracudabra.device.conversion.array_to_device(array, /, device)[source]#
Move a NumPy/CuPy array to a device.
- Parameters:
array (object) – The array to move.
device (abracudabra.device.base.Device | str) – The device to move the array to.
- Returns:
The array on the specified device.
- Return type:
abracudabra._annotations.Array
- abracudabra.device.conversion.frame_to_device(frame: abracudabra._annotations.Index, /, device_type: Literal['cpu']) pandas.Index [source]#
- abracudabra.device.conversion.frame_to_device(frame: abracudabra._annotations.Index, /, device_type: Literal['cuda']) cudf.Index
- abracudabra.device.conversion.frame_to_device(frame: abracudabra._annotations.Series, /, device_type: Literal['cpu']) pandas.Series
- abracudabra.device.conversion.frame_to_device(frame: abracudabra._annotations.Series, /, device_type: Literal['cuda']) cudf.Series
- abracudabra.device.conversion.frame_to_device(frame: abracudabra._annotations.DataFrame, /, device_type: Literal['cpu']) pandas.DataFrame
- abracudabra.device.conversion.frame_to_device(frame: abracudabra._annotations.DataFrame, /, device_type: Literal['cuda']) cudf.DataFrame
- abracudabra.device.conversion.frame_to_device(frame: object, /, device_type: abracudabra.device.base.DeviceType) abracudabra._annotations.Index | abracudabra._annotations.Series | abracudabra._annotations.DataFrame
Move a Pandas/cuDF index, series or dataframe to a device.
- Parameters:
frame – The series or dataframe to move.
device_type – The device type to move the frame to.
- Returns:
The series or dataframe on the specified device.
- abracudabra.device.conversion.tensor_to_device(tensor, /, device)[source]#
Move a Torch tensor to a device.
- Parameters:
tensor (object)
device (abracudabra.device.base.Device | str)
- Return type:
torch.Tensor
- abracudabra.device.conversion.to_device(sequence: abracudabra._annotations.Series, /, device: abracudabra.device.base.Device | str) abracudabra._annotations.Series [source]#
- abracudabra.device.conversion.to_device(sequence: torch.Tensor, /, device: abracudabra.device.base.Device | str) torch.Tensor
- abracudabra.device.conversion.to_device(sequence: abracudabra._annotations.DataFrame, /, device: abracudabra.device.base.Device | str) abracudabra._annotations.DataFrame
- abracudabra.device.conversion.to_device(sequence: abracudabra._annotations.Index, /, device: abracudabra.device.base.Device | str) abracudabra._annotations.Index
- abracudabra.device.conversion.to_device(sequence: abracudabra._annotations.Array, /, device: abracudabra.device.base.Device | str) abracudabra._annotations.Array
- abracudabra.device.conversion.to_device(sequence: abracudabra._annotations.Array | abracudabra._annotations.Series | torch.Tensor, /, device: abracudabra.device.base.Device | str) abracudabra._annotations.Index | abracudabra._annotations.Series | abracudabra._annotations.DataFrame | abracudabra._annotations.Array | torch.Tensor
Move an array, series, or tensor to a device.
Call the appropriate function to move the element to the device:
abracudabra.device.conversion.array_to_device()
for NumPy/CuPy arrays.abracudabra.device.conversion.frame_to_device()
for Pandas/cuDF index/series/dataframes.abracudabra.device.conversion.tensor_to_device()
for Torch tensors.
- Parameters:
sequence – The sequence to move to the device.
device – The device to move the sequence to.
- Returns:
The sequence on the specified device.
- Raises:
TypeError – If the sequence is not a NumPy/CuPy array, Pandas/cuDF index/series/dataframe or Torch tensor.
Examples
Move a Pandas dataframe to the GPU (cuDF dataframe):
>>> import pandas as pd >>> from abracudabra import to_device >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df_gpu = to_device(df, "cuda") >>> print(type(df_gpu)) <class 'cudf.core.dataframe.DataFrame'>
Move a cuDF dataframe to the CPU (Pandas dataframe):
>>> df_cpu = to_device(df_gpu, "cpu") >>> print(type(df_cpu)) <class 'pandas.core.frame.DataFrame
Move a numpy array to the GPU (cupy):
>>> import numpy as np >>> arr = np.array([1, 2, 3]) >>> arr_gpu = to_device(arr, "cuda") >>> print(type(arr_gpu)) <class 'cupy.ndarray'>