Train Your First Model¶
This guide covers training a small model (ResNet18) on CIFAR-10 using a single GPU on the Mila cluster. The guide uses Mila's CIFAR-10 dataset, stages it into fast local storage, and runs a Slurm batch job.
Before you begin¶
-
Obtain a Mila account, enable cluster access and MFA, install
uvandmilatools, configure SSH access and connect to the cluster for the first time. -
Run your first job on the cluster with PyTorch using
mila codeand VSCode on a GPU compute node.
What this guide covers¶
- Train a ResNet18 on CIFAR-10 using a single GPU.
- Use Mila's CIFAR-10 dataset in
/network/datasets/cifar10/. - Stage data into
$SLURM_TMPDIRfor fast I/O during training. - Submit and monitor a batch job with
sbatch.
Open VSCode on a CPU node¶
Create the project directory on the cluster¶
From a personal computer, create the project directory on the cluster so
that mila code can open it (the path is on the cluster):
Start VSCode on a CPU node¶
Only code and job scripts will be prepared at this stage—not actually running training—so a CPU node suffices for a faster-to-allocate and less resource-intensive editor session.
[17:35:21] Checking disk quota on $HOME... disk_quota.py:31
[17:35:27] Disk usage: 85.34 / 100.00 GiB and 794022 / 1048576 files disk_quota.py:211
[17:35:29] (mila) $ cd $SCRATCH && salloc --cpus-per-task=2 --mem=16G --time=01:00:00 --job-name=mila-code compute_node.py:293
salloc: --------------------------------------------------------------------------------------------------
salloc: # Using default long partition
salloc: --------------------------------------------------------------------------------------------------
salloc: Granted job allocation 8888888
[17:35:30] Waiting for job 8888888 to start. compute_node.py:315
[17:35:31] (localhost) $ code --new-window --wait --remote ssh-remote+cn-a003.server.mila.quebec /home/mila/u/username/CODE/train_first_model local_v2.py:55
Create the project files¶
The job script does three things:
#SBATCHdirectives — Request 1 GPU, 4 CPUs, 16G memory, 15 minutes.- Data staging — Copy CIFAR-10 from
/network/datasets/into$SLURM_TMPDIR/data. Compute nodes read from$SLURM_TMPDIRmuch faster than from network storage. - Run the training script —
srun uv run python main.pyruns the script inside the allocation.
The training script uses PyTorch to load CIFAR-10 from $SLURM_TMPDIR/data,
train ResNet18, and log validation loss and accuracy per epoch. Create a
file main.py with the following content:
| main.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
Submit the job¶
Using the VSCode terminal, submit the job:
The output will confirm the submission of the job (e.g. Submitted batch job
8888888.). Note the job ID.
Monitor the job¶
-
Queue status:
squeue --meJob stuck with
ReqNodeNotAvailNo node matching the job's requirements is currently available — for example, all nodes with the requested GPU type may be busy, or some nodes may be DOWN or reserved for maintenance. This is not an error — the job will start automatically once a matching node is free.
Options:
- Wait. The job will start on its own. Check back with
squeue --me. - Request a different GPU type. Cancel the queued job with
scancel <JOBID>, then resubmit with a different--gresflag, e.g.:sbatch --gres=gpu:rtx8000:1 job.sh
- Wait. The job will start on its own. Check back with
-
Watch the output file: Once the job starts, a file
slurm-<JOBID>.outwill be created containing the job log. Open it to watch the model being trained:
Key concepts¶
- Data staging to
$SLURM_TMPDIR - Network storage is shared and slower. Copying the dataset into
$SLURM_TMPDIRat job start gives the compute node fast local access for the rest of the run. srun- Runs a command inside the allocated resources. In the job script,
srun uv run python main.pyruns training on the GPU node.
Next step¶
-
Manage Python Dependencies with
uv
Install uv, manage project dependencies, run reproducible Slurm jobs, and run standalone scripts.
-
Set up WandB and follow best practices for logging experiments, organizing runs, and diagnosing bottlenecks on the cluster.