#!/bin/bash#SBATCH --ntasks=1#SBATCH --ntasks-per-node=1#SBATCH --cpus-per-task=1#SBATCH --gpus-per-task=l40s:1## Or --gpus-per-task=rtx8000:1 to request a different GPU model## Or --gpus-per-task=1 for any GPU model#SBATCH --mem-per-gpu=16G#SBATCH --time=00:15:00# Exit on errorset-e
# Echo time and hostname into logecho"Date: $(date)"echo"Hostname: $(hostname)"# Execute Python script# Use `uv run --offline` on clusters without internet access on compute nodes.uvrunpythonmain.py
[project]name="pytorch-setup"version="0.1.0"description="Add your description here"readme="README.md"requires-python=">=3.11,<3.14"dependencies=["torch>=2.7.1"]
importtorchimporttorch.backends.cudadefmain():cuda_built=torch.backends.cuda.is_built()cuda_avail=torch.cuda.is_available()device_count=torch.cuda.device_count()print(f"PyTorch built with CUDA: {cuda_built}")print(f"PyTorch detects CUDA available: {cuda_avail}")print(f"PyTorch-detected #GPUs: {device_count}")ifdevice_count==0:print(" No GPU detected, not printing devices' names.")else:foriinrange(device_count):print(f" GPU {i}: {torch.cuda.get_device_name(i)}")if__name__=="__main__":main()
This assumes that you already installed UV on the cluster you are working on.
To create this environment, we first request resources for an interactive job.
Note that we are requesting a GPU for this job, even though we're only going to
install packages. This is because we want PyTorch to be installed with GPU
support, and to have all the required libraries.
# On the Mila cluster: (on DRAC/PAICE, run `uv sync` on a login node)$salloc--gres=gpu:1--cpus-per-task=4--mem=16G--time=00:10:00
salloc:--------------------------------------------------------------------------------------------------
salloc:# Using default long partitionsalloc:--------------------------------------------------------------------------------------------------
salloc:Pendingjoballocation2959785salloc:job2959785queuedandwaitingforresources
salloc:job2959785hasbeenallocatedresources
salloc:Grantedjoballocation2959785salloc:Waitingforresourceconfiguration
salloc:Nodescn-g022arereadyforjob
$# Create the virtual environment and install all dependencies$uvsync
(...)$# Optional: Activate the environment and run the python script:$..venv/bin/activate
$pythonmain.py
You can exit the interactive job once the environment has been created.
Then, you can submit a job to run the example with sbatch: