|
18 | 18 | LightningDistributedDataParallel,
|
19 | 19 | LightningDataParallel,
|
20 | 20 | )
|
| 21 | +from pytorch_lightning.utilities import move_data_to_device |
21 | 22 | from pytorch_lightning.utilities.exceptions import MisconfigurationException
|
22 | 23 | from pytorch_lightning.utilities.distributed import rank_zero_only
|
23 | 24 |
|
@@ -99,58 +100,50 @@ def copy_trainer_model_properties(self, model):
|
99 | 100 | m.tpu_local_core_rank = self.tpu_local_core_rank
|
100 | 101 | m.tpu_global_core_rank = self.tpu_global_core_rank
|
101 | 102 |
|
102 |
| - def transfer_batch_to_tpu(self, batch): |
103 |
| - return self.__transfer_data_to_device(batch, device='tpu') |
104 |
| - |
105 |
| - def transfer_batch_to_gpu(self, batch, gpu_id): |
106 |
| - return self.__transfer_data_to_device(batch, device='gpu', gpu_id=gpu_id) |
107 |
| - |
108 |
| - def __transfer_data_to_device(self, batch, device, gpu_id=None): |
109 |
| - if device == 'tpu' and XLA_AVAILABLE: |
110 |
| - # base case: object can be directly moved using `to` |
111 |
| - if callable(getattr(batch, 'to', None)): |
112 |
| - xla_device = xm.xla_device(self.tpu_id) if self.tpu_id is not None else xm.xla_device() |
113 |
| - return batch.to(xla_device) |
114 |
| - |
115 |
| - if device == 'gpu': |
116 |
| - # base case: object can be directly moved using `cuda` or `to` |
117 |
| - if callable(getattr(batch, 'cuda', None)): |
118 |
| - # non_blocking will be ignored if tensor is not pinned. |
119 |
| - # so we can always set it to True |
120 |
| - return batch.cuda(gpu_id, non_blocking=True) |
121 |
| - |
122 |
| - if callable(getattr(batch, 'to', None)): |
123 |
| - # non_blocking will be ignored if tensor is not pinned. |
124 |
| - # so we can always set it to True |
125 |
| - return batch.to(torch.device('cuda', gpu_id), non_blocking=True) |
126 |
| - |
127 |
| - # when list |
128 |
| - if isinstance(batch, list): |
129 |
| - for i, x in enumerate(batch): |
130 |
| - batch[i] = self.__transfer_data_to_device(x, device, gpu_id) |
131 |
| - return batch |
132 |
| - |
133 |
| - # when tuple |
134 |
| - if isinstance(batch, tuple): |
135 |
| - # when namedtuple |
136 |
| - if hasattr(batch, '_fields'): |
137 |
| - elem_type = type(batch) |
138 |
| - return elem_type(*(self.__transfer_data_to_device(x, device, gpu_id) for x in batch)) |
139 |
| - else: |
140 |
| - batch = list(batch) |
141 |
| - for i, x in enumerate(batch): |
142 |
| - batch[i] = self.__transfer_data_to_device(x, device, gpu_id) |
143 |
| - return tuple(batch) |
144 |
| - |
145 |
| - # when dict |
146 |
| - if isinstance(batch, dict): |
147 |
| - for k, v in batch.items(): |
148 |
| - batch[k] = self.__transfer_data_to_device(v, device, gpu_id) |
149 |
| - |
150 |
| - return batch |
151 |
| - |
152 |
| - # nothing matches, return the value as is without transform |
153 |
| - return batch |
| 103 | + def transfer_batch_to_tpu(self, batch: Any, tpu_id: Optional[int] = None): |
| 104 | + """ |
| 105 | + Transfers the data to the TPU. |
| 106 | +
|
| 107 | + Args: |
| 108 | + batch: A tensor or collection of tensors. |
| 109 | + tpu_id: The id of the TPU core. If omitted, the first available core is chosen. |
| 110 | +
|
| 111 | + Return: |
| 112 | + the tensor on the TPU device. |
| 113 | +
|
| 114 | + See Also: |
| 115 | + - :func:`~pytorch_lightning.utilities.apply_func.move_data_to_device` |
| 116 | + """ |
| 117 | + if not XLA_AVAILABLE: |
| 118 | + raise MisconfigurationException( |
| 119 | + 'Requested to transfer batch to TPU but XLA is not available.' |
| 120 | + ' Are you sure this machine has TPUs?' |
| 121 | + ) |
| 122 | + device = xm.xla_device(tpu_id) |
| 123 | + return self.__transfer_batch_to_device(batch, device) |
| 124 | + |
| 125 | + def transfer_batch_to_gpu(self, batch: Any, gpu_id: Optional[int] = None): |
| 126 | + """ |
| 127 | + Transfers the data to the GPU. |
| 128 | +
|
| 129 | + Args: |
| 130 | + batch: A tensor or collection of tensors. |
| 131 | + gpu_id: The id of the GPU device. If omitted, the first available GPU is chosen. |
| 132 | +
|
| 133 | + Return: |
| 134 | + the tensor on the GPU device. |
| 135 | +
|
| 136 | + See Also: |
| 137 | + - :func:`~pytorch_lightning.utilities.apply_func.move_data_to_device` |
| 138 | + """ |
| 139 | + device = torch.device('cuda', gpu_id) |
| 140 | + return self.__transfer_batch_to_device(batch, device) |
| 141 | + |
| 142 | + def __transfer_batch_to_device(self, batch: Any, device: torch.device): |
| 143 | + model = self.get_model() |
| 144 | + if model is not None: |
| 145 | + return model.transfer_batch_to_device(batch, device) |
| 146 | + return move_data_to_device(batch, device) |
154 | 147 |
|
155 | 148 | def single_gpu_train(self, model):
|
156 | 149 | model.cuda(self.root_gpu)
|
|
0 commit comments