Return a copy of tensor on specified device. Works on individual tensors, and tensors contained/nested in lists, tuples, and dicts. Parameters: item: tensor to copy or (possibly nested) container of tensors to copy. device: target device criterion_func: Function
(item, device, criterion_func)
| 149 | |
| 150 | |
| 151 | def copy_to_device(item, device, criterion_func): |
| 152 | """ |
| 153 | Return a copy of tensor on specified device. |
| 154 | Works on individual tensors, and tensors contained/nested in lists, tuples, and dicts. |
| 155 | Parameters: |
| 156 | item: tensor to copy or (possibly nested) container of tensors to copy. |
| 157 | device: target device |
| 158 | criterion_func: Function to restrict copy operation to items meet criterion |
| 159 | |
| 160 | Returns: |
| 161 | None |
| 162 | """ |
| 163 | if criterion_func(item): |
| 164 | return item.to(device) |
| 165 | elif isinstance(item, list): |
| 166 | return [copy_to_device(v, device, criterion_func) for v in item] |
| 167 | elif isinstance(item, tuple): |
| 168 | return tuple([copy_to_device(v, device, criterion_func) for v in item]) |
| 169 | elif isinstance(item, dict): |
| 170 | return {k: copy_to_device(v, device, criterion_func) for k, v in item.items()} |
| 171 | else: |
| 172 | return item |
| 173 | |
| 174 | |
| 175 | def move_to_device(item, device, criterion_func=None): |
no test coverage detected