Skip to content
Projets

À propos

Cross-Platform Vulkan Backend for PyTorch

IA et apprentissage automatique

Comment ces chiffres sont mesurés

Progression mesurée des étoiles

in 24h
+3
in 7 days
+16
in 15 days
+19
in 1 month
Historique en cours

Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.

README.md

Original

torchvulkan

A Cross-Platform Vulkan Backend for PyTorch

PyPI CI PyTorch 2.15 License: MIT

torchvulkan registers a native 'vulkan' device with PyTorch. It lets you train and run models on any Vulkan 1.3 GPU - including AMD, Intel Arc, NVIDIA, and Apple Silicon, without installing CUDA, ROCm, or vendor-specific toolkits. The backend will be presented at Vulkanised 2027: "torchvulkan: Training PyTorch Models on Any GPU with Vulkan Compute."

Quick start

pip install torchvulkan
import torch
import torchvulkan

model = torch.nn.Linear(1024, 10).to("vulkan")
x = torch.randn(64, 1024, device="vulkan")
model(x).sum().backward()

The device works like cuda: move tensors and modules with .to("vulkan"), select GPUs with "vulkan:1", and use torch.autocast("vulkan") for mixed precision.

Wheels are available for Python 3.11–3.15 on Linux and Windows (x86_64) and macOS (Apple Silicon), and require PyTorch 2.15. On macOS, a modified version of MoltenVK is bundled.

Performance

Time for one float16 training step (forward + backward) on the example models: the MLP at batch 1024, the LLM at GPT-2 small's shape (12 layers, width 768, 1024 tokens, batch 8) and ResNet-50 on 224x224 images (batch 32).

Device Backend MLP (141M params) LLM (78M params) ResNet-50 (26M params)
AMD RX 9070 XT vulkan 14.1 ms 150.5 ms 53.9 ms
ROCm 9.4 ms 90.1 ms 52.7 ms
NVIDIA RTX 3060 vulkan 52.8 ms 422 ms 137 ms
CUDA 35.6 ms 239 ms 93.2 ms
Apple M3 vulkan 364 ms 2364 ms 546 ms
MPS 310 ms 3938 ms 508 ms

To reproduce:

pip install matplotlib
python benchmark/benchmark.py all

Examples

Example Model Data
examples/mlp.py A multi-layer perceptron with GELU and dropout, trained with AdamW. MNIST
examples/llm.py A small modern decoder-only transformer: RMSNorm, rotary embeddings, grouped-query attention, a SwiGLU MLP and tied embeddings. Its own source code
examples/resnet.py A ResNet with residual blocks, batch norm and global average pooling, trained with SGD and a one-cycle schedule. CIFAR-10

Python API

torchvulkan mirrors torch.cuda and is also available as torch.vulkan:

import torchvulkan

# devices
torchvulkan.is_available()            # -> bool: a usable Vulkan device was found
torchvulkan.device_count()            # -> int: number of Vulkan devices (discrete GPUs first)
torchvulkan.current_device()          # -> int: index of the current device
torchvulkan.set_device(1)             # make "vulkan" mean "vulkan:1"
torchvulkan.get_device_name()         # -> str
torchvulkan.get_device_properties()   # -> name, total_memory, vendor_id, api_version, ...
torchvulkan.synchronize()             # block until queued GPU work has finished

# memory
torchvulkan.memory_allocated()        # bytes held by tensors
torchvulkan.max_memory_allocated()    # peak since the last reset_peak_memory_stats()
torchvulkan.memory_reserved()         # bytes held by the allocator, including its cache
torchvulkan.memory_stats()            # -> dict
torchvulkan.mem_get_info()            # -> (free, total): what can be allocated given the budget
torchvulkan.empty_cache()             # release cached device memory back to the driver

# random numbers (per-device)
torchvulkan.manual_seed(0)            # also: manual_seed_all, seed, seed_all, initial_seed
torchvulkan.get_rng_state()           # also: set_rng_state, get_rng_state_all, set_rng_state_all

torchvulkan.is_bf16_supported()       # -> bool: bfloat16 runs on the GPU
torchvulkan.__version__               # package version

Environment variables

Variable Description
TORCHVULKAN_STRICT=1 Error instead of falling back to the CPU
TORCHVULKAN_STRICT_ALLOCATION=1 Error instead of allocating past the VRAM budget
TORCHVULKAN_TUNE_BUDGET_S Autotuning time limit per operation in seconds (default 20, 0 for none)
TORCHVULKAN_TUNE_LOG=1 Log autotuner decisions
TORCHVULKAN_CACHE_DIR Pipeline and tuning cache directory (default ~/.cache/torchvulkan, %LOCALAPPDATA%\torchvulkan on Windows)
TORCHVULKAN_PIPELINE_CACHE=0 Disable the pipeline cache
TORCHVULKAN_STAGING_CACHE_MB Host staging memory for transfers (default 256)
TORCHVULKAN_SYSTEM_VULKAN=1 macOS: use the system Vulkan loader instead of the bundled MoltenVK
TORCHVULKAN_AMD_LLPC=0 Windows, AMD: use the driver's default shader compiler instead of LLPC
TORCHVULKAN_VALIDATION=1 Enable the Khronos validation layer (sync adds synchronization validation)
TORCHVULKAN_PROFILE=1 Print driver statistics for each compiled pipeline
TORCHVULKAN_EMULATE=1 Force emulated dtypes, for testing

Limitations

  • torch.compile works with backend="aot_eager" but not Inductor
  • No torch.distributed backend
  • float64 needs device support; transcendentals are computed in float32

Building from source

Requires the Vulkan SDK, Slang (slangc on PATH) and a C++20 compiler.

git clone --recursive https://github.com/zainsharief/torchvulkan
pip install ./torchvulkan

[!NOTE] Disclaimer: torchvulkan is an independent open-source project and is not affiliated with or endorsed by PyTorch or the Linux Foundation.

Explorer des projets similaires

Chargement…