MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / is_on_gpu

Function is_on_gpu

bitsandbytes/functional.py:351–390  ·  view source on GitHub ↗

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]])

Source from the content-addressed store, hash-verified

349
350
351def 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
393def _get_tensor_stream(tensor: Tensor) -> ct.c_void_p:

Callers 4

optimizer_update_32bitFunction · 0.85
igemmFunction · 0.85
batched_igemmFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected