Running PyTorch Verification after uv sync: Testing GPU Setup with basic_calculation

The basic_calculation function in main.py validates your PyTorch installation by executing a GPU-accelerated matrix multiplication immediately after uv sync completes.

The uv-install-torch repository provides a minimal template for installing PyTorch with the uv package manager and verifying that CUDA tensors execute correctly. This workflow demonstrates how to declare platform-specific PyTorch dependencies and confirm GPU acceleration using a simple verification script.

Dependency Configuration in pyproject.toml

The project uses optional dependencies to handle CPU and CUDA variants cleanly. In [pyproject.toml](https://github.com/baonguyen6742/uv-install-torch/blob/master/pyproject.toml), PyTorch, torchvision, and torchaudio are declared under two mutually exclusive extras: cpu and cu124 (CUDA 12.4).

[project.optional-dependencies]
cpu = ["torch", "torchvision", "torchaudio"]
cu124 = ["torch", "torchvision", "torchaudio"]

The [tool.uv.sources] table maps these packages to the appropriate PyTorch indices, while [tool.uv.index] defines the CPU-only and CUDA 12.4 wheel sources. A conflicts entry prevents mixing both extras in a single environment.

Installing with uv sync

To install the CUDA-compatible environment, run:

uv sync --extra cu124

This command resolves the cu124 extra, downloads the CUDA-enabled wheels from https://download.pytorch.org/whl/cu124, and constructs a virtual environment with PyTorch 2.4.1+cu124 and dependencies.

The basic_calculation Verification Function

Located in [main.py](https://github.com/baonguyen6742/uv-install-torch/blob/master/main.py#L25-L31), the basic_calculation function performs a minimal GPU operation to prove the runtime links correctly.

Device Selection Logic

The function automatically selects the available accelerator:

device = "cuda" if torch.cuda.is_available() else "cpu"

This ensures the verification runs on GPU when torch.cuda.is_available() returns True, otherwise falling back to CPU.

Tensor Operations

The function generates two 1-D tensors using torch.arange, reshapes them for matrix multiplication, and moves the result to the selected device:

def basic_calculation(a: int, b: int) -> torch.Tensor:
    device = "cuda" if torch.cuda.is_available() else "cpu"
    tensor_a = torch.arange(1, a + 1).unsqueeze(0)
    tensor_b = torch.arange(1, b + 1).unsqueeze(0)
    return torch.matmul(tensor_a.T, tensor_b).to(device)

This outer product operation confirms that CUDA tensors can be created, manipulated, and stored on the GPU without runtime errors.

Executing the Verification

After installation, run the complete verification script:

uv run main.py

The script prints library versions, enumerates available GPU devices, and executes basic_calculation. Expected output includes:


torch.__version__: 2.4.1+cu124
torch.cuda.is_available: True
Device 0 : _CudaDeviceProperties(name='NVIDIA GeForce RTX 3060', major=8, minor=6, total_memory=11931MB, multi_processor_count=28)

Test calculation
tensor([[1, 2, 3],
        [2, 4, 6]], device='cuda:0')

The device='cuda:0' flag in the tensor output confirms the calculation executed on the GPU.

Summary

  • The repository uses optional extras (cpu and cu124) in pyproject.toml to manage PyTorch variants without dependency conflicts.
  • Running uv sync --extra cu124 installs CUDA 12.4-compatible PyTorch wheels from the official PyTorch index.
  • The basic_calculation function in main.py validates the installation by performing matrix multiplication on automatically selected GPU hardware.
  • Successful execution displays CUDA device properties and tensors with device='cuda:0', confirming the environment is ready for GPU-accelerated workflows.

Frequently Asked Questions

How do I verify PyTorch is using my GPU after uv installation?

Execute uv run main.py and check the console output. If torch.cuda.is_available() returns True and the basic_calculation tensor displays device='cuda:0', PyTorch is correctly linked to your NVIDIA drivers and CUDA runtime.

Can I use the basic_calculation function in my own projects?

Yes. Copy the basic_calculation function from main.py into your codebase. It serves as a minimal smoke test that creates tensors and performs matrix multiplication on the available GPU, or falls back to CPU if no CUDA device is detected.

What is the difference between the cpu and cu124 extras?

The cpu extra installs PyTorch wheels compiled for CPU-only operation, while cu124 installs wheels built against CUDA 12.4 libraries. The conflicts declaration in pyproject.toml prevents installing both extras simultaneously to avoid binary incompatibility.

Why does the output show 2.4.1+cu124 for torch versions?

The +cu124 local version identifier indicates the PyTorch binary was compiled with CUDA 12.4 support. This suffix confirms that uv fetched the GPU-enabled wheels from the CUDA index rather than the default CPU-only packages.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →