Verifies that the input tensors are all on the same device. An input tensor may also be marked as `paged`, in which case the device placement is ignored. CPU tensors are allowed and checked for consistency among themselves. Args: tensors (`Iterable[Optional[torch.Tensor]]`): A
(tensors: Iterable[Optional[torch.Tensor]])
| 349 | |
| 350 | |
| 351 | def is_on_gpu(tensors: Iterable[Optional[torch.Tensor]]): |
| 352 | """Verifies that the input tensors are all on the same device. |
| 353 | |
| 354 | An input tensor may also be marked as `paged`, in which case the device placement is ignored. |
| 355 | CPU tensors are allowed and checked for consistency among themselves. |
| 356 | |
| 357 | Args: |
| 358 | tensors (`Iterable[Optional[torch.Tensor]]`): A list of tensors to verify. |
| 359 | |
| 360 | Raises: |
| 361 | `RuntimeError`: Raised when the verification fails. |
| 362 | |
| 363 | Returns: |
| 364 | `Literal[True]` |
| 365 | """ |
| 366 | |
| 367 | devices = set() |
| 368 | |
| 369 | for t in tensors: |
| 370 | # NULL pointers and paged tensors are OK. |
| 371 | if t is not None and not getattr(t, "is_paged", False): |
| 372 | devices.add((t.device.type, t.device.index)) |
| 373 | |
| 374 | # All tensors on CPU is valid |
| 375 | if devices == {("cpu", None)}: |
| 376 | return True |
| 377 | |
| 378 | # Check that no CPU tensors are mixed with GPU tensors |
| 379 | has_cpu = ("cpu", None) in devices |
| 380 | if has_cpu and len(devices) > 1: |
| 381 | raise RuntimeError( |
| 382 | f"Input tensors need to be on the same device, but found the following tensor and device combinations:\n {[(t.shape, t.device) for t in tensors if t is not None]}", |
| 383 | ) |
| 384 | |
| 385 | # GPU path: all tensors must be on the same single GPU |
| 386 | if len(devices) > 1: |
| 387 | raise RuntimeError( |
| 388 | f"Input tensors need to be on the same GPU, but found the following tensor and device combinations:\n {[(t.shape, t.device) for t in tensors if t is not None]}", |
| 389 | ) |
| 390 | return True |
| 391 | |
| 392 | |
| 393 | def _get_tensor_stream(tensor: Tensor) -> ct.c_void_p: |
no outgoing calls
no test coverage detected