| 19 | |
| 20 | |
| 21 | def StructuralMove(obj, device) -> torch.Tensor | list | dict | None: |
| 22 | match obj: |
| 23 | case obj if torch.is_tensor(obj): |
| 24 | return obj.to(device) |
| 25 | case None: |
| 26 | return None |
| 27 | case dict(): |
| 28 | return { k: StructuralMove(obj[k], device) for k in obj } |
| 29 | case list(): |
| 30 | return [ StructuralMove(v, device) for v in obj ] |
| 31 | case np.ndarray(): |
| 32 | return torch.tensor(obj).to(device) |
| 33 | case _: |
| 34 | raise ValueError(f"Unable to move type {type(obj)}") |
| 35 | |
| 36 | |
| 37 | def cropToMultiple(x: torch.Tensor, factor: int | list[int], dim: int | list[int]): |