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)
| 124 | |
| 125 | |
| 126 | def copy_to_device(item, device, criterion_func): |
| 127 | """ |
| 128 | Return a copy of tensor on specified device. |
| 129 | Works on individual tensors, and tensors contained/nested in lists, tuples, and dicts. |
| 130 | Parameters: |
| 131 | item: tensor to copy or (possibly nested) container of tensors to copy. |
| 132 | device: target device |
| 133 | criterion_func: Function to restrict copy operation to items meet criterion |
| 134 | |
| 135 | Returns: |
| 136 | None |
| 137 | """ |
| 138 | if criterion_func(item): |
| 139 | return item.to(device) |
| 140 | elif isinstance(item, list): |
| 141 | return [copy_to_device(v, device, criterion_func) for v in item] |
| 142 | elif isinstance(item, tuple): |
| 143 | return tuple([copy_to_device(v, device, criterion_func) for v in item]) |
| 144 | elif isinstance(item, dict): |
| 145 | return {k: copy_to_device(v, device, criterion_func) for k, v in item.items()} |
| 146 | else: |
| 147 | return item |
| 148 | |
| 149 | |
| 150 | def move_to_device(item, device, criterion_func): |