Skip to main content

Provides a helper to detect the best Pytorch device (env override, CUDA, MPS, XPU, or CPU) and to clear CUDA caches.

Project description

Pytorch Device Detection

Small utility to select a torch device at runtime and to clear CUDA caches.

Summary

Provides two functions:

  • get_device(strings=False, verbose=False): returns the best available device (torch.device by default, or a string when strings=True). Probes in this order: TORCH_DEVICE/DEVICE environment variables, CUDA, MPS, XPU (torch.xpu or intel_extension_for_pytorch.xpu), then CPU. When verbose=True the function emits debug-level messages through the logger.

  • clear_torch(): calls torch.cuda.empty_cache().

Install

uv add devicer

If you want XPU:

uv add devicer[xpu]

Usage

from devicer import get_device

device = get_device()
print(device)

Environment variables:

  • Set TORCH_DEVICE or DEVICE to force a device, for example cuda, cuda:0, mps, cpu, or xpu:
export TORCH_DEVICE=cuda
python myscript.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

devicer-0.1.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

devicer-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file devicer-0.1.0.tar.gz.

File metadata

  • Download URL: devicer-0.1.0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for devicer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0d8e8a7f69736ce52f3c1f2d63a4bc0f2085f70e9c78f332142ecf4efc9c1bde
MD5 0f99152f9d7f5011699338dec5c255b9
BLAKE2b-256 757a80099ed02d0910ef4513253bdb193ad99e082b849b7d5ab79e4111cdbfec

See more details on using hashes here.

File details

Details for the file devicer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: devicer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for devicer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47489d5fc7db0635f252c88f018f0a0c71892a021edb392f265e0c3b63ab99f2
MD5 eadb3391fb3604f691600a39e3979a11
BLAKE2b-256 afe240bcd709253d9a18b12d86be6f60620580e2b2749e3911f73b74c5a4d6c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page