r"""Make a deep copy of value, casting all tensors to device of param.
(param, value)
| 230 | } |
| 231 | |
| 232 | def cast(param, value): |
| 233 | r"""Make a deep copy of value, casting all tensors to device of param.""" |
| 234 | if isinstance(value, torch.Tensor): |
| 235 | # Floating-point types are a bit special here. They are the only ones |
| 236 | # that are assumed to always match the type of params. |
| 237 | if param.is_floating_point() and value.dtype != torch.uint8: |
| 238 | value = value.to(param.dtype) |
| 239 | return value |
| 240 | elif isinstance(value, dict): |
| 241 | for k, v in value.items(): |
| 242 | if k in self.non_castable_tensor_keys: |
| 243 | if move_to_device: |
| 244 | value[k] = v.to(param.device) |
| 245 | else: |
| 246 | value[k] = cast(param, v) |
| 247 | |
| 248 | return value |
| 249 | elif isinstance(value, container_abcs.Iterable): |
| 250 | return type(value)(cast(param, v) for v in value) |
| 251 | else: |
| 252 | return value |
| 253 | |
| 254 | # Copy state assigned to params (and cast tensors to appropriate types). |
| 255 | # State that is not assigned to params is copied as is (needed for |