Train Your First Model¶
This guide walks you through training a small model (ResNet18) on CIFAR-10 using a single GPU on the Mila cluster. You will use Mila's CIFAR-10 dataset, stage it into fast local storage, and run a Slurm batch job.
Prerequisites¶
-
Get your Mila account, enable cluster access and MFA, then install
uvandmilatoolsto connect via SSH. -
Run your first job on the cluster with PyTorch using VSCode on a GPU compute node.
What you will do¶
- 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 compute node¶
Create the project directory on the cluster¶
From your local machine, 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¶
For this step, we're only preparing code and job scriptsβnot actually running trainingβso we'll use a CPU node 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 your 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:
You will see something like Submitted batch job 8888888. Note the job ID.
Monitor the job¶
-
Queue status:
squeue --me -
Watch the output file: Once the job starts, a file
slurm-<JOBID>.outwill be created in which you'll find the log of the job being executed. 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 the start of the job gives the compute node fast local access for the rest of the run. - srun β Runs a command inside the allocated resources. In our script,
srun uv run python main.pyruns the training on the GPU node.