to_device
optimus_dl.modules.data.transforms.to_device
¶
ToDeviceTransform
¶
Bases: BaseTransform
Transform that moves data tensors to the target compute device.
This transform ensures that input data is on the correct device (e.g., CUDA) before it enters the model. It includes performance optimizations for GPUs:
- Memory Pinning: Automatically uses
PinMemoryto speed up CPU-to-GPU transfers. - Asynchronous Transfers: Uses
non_blocking=Truefor CUDA devices. - Prefetching: Adds an additional prefetch layer to overlap device transfers with computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
ToDeviceTransformConfig
|
Device transfer configuration. |
required |
device
|
The target PyTorch device. |
required |
Source code in optimus_dl/modules/data/transforms/to_device.py
build(source)
¶
Wrap the source node with pinning, prefetching, and the device map.
Source code in optimus_dl/modules/data/transforms/to_device.py
ToDeviceTransformConfig
dataclass
¶
Bases: RegistryConfigStrict
Configuration for device transfers.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
properties
|
list[str] | None
|
|
None
|
pin_memory
|
bool
|
|
True
|
pin_prefetch_factor
|
int
|
|
2
|
pin_snapshot_frequency
|
int
|
|
128
|