Getting "torch,amp has no attribute GradScaler" error, when trying to train YOLO models (yolov10, v9) on kaggle

Haroon_Rashid: <p><strong>Code:</strong><br>
<code>!pip install ultralytics   from ultralytics import YOLO   model  = YOLO('yolov10x.yaml')   results = model.train(data='config.yaml', epochs=400, patience=40, device=[0, 1])  </code><br>
<strong>Error:</strong></p>
<p>AttributeError Traceback (most recent call last)<br>
Cell In[23], line 2<br>
1 MODEL = YOLO(“yolov10x.yaml”)<br>
----&gt; 2 results = MODEL.train(data=“/kaggle/input/configuration/config.yaml”, epochs=400, patience=35, device=0, cache=True)</p>
<p>File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/model.py:810, in Model.train(self, trainer, **kwargs)<br>
807 self.model = self.trainer.model<br>
809 self.trainer.hub_session = self.session # attach optional HUB session<br>
 → 810 self.trainer.train()<br>
811 # Update model and cfg after training<br>
812 if RANK in {-1, 0}:</p>
<p>File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/trainer.py:206, in BaseTrainer.train(self)<br>
203 ddp_cleanup(self, str(file))<br>
205 else:<br>
 → 206 self._do_train(world_size)</p>
<p>File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/trainer.py:329, in BaseTrainer._do_train(self, world_size)<br>
327 if world_size &gt; 1:<br>
328 self._setup_ddp(world_size)<br>
 → 329 self._setup_train(world_size)<br>
331 nb = len(self.train_loader) # number of batches<br>
332 nw = max(round(self.args.warmup_epochs * nb), 100) if self.args.warmup_epochs &gt; 0 else -1 # warmup iterations</p>
<p>File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/trainer.py:270, in BaseTrainer._setup_train(self, world_size)<br>
267 dist.broadcast(self.amp, src=0) # broadcast the tensor from rank 0 to all other ranks (returns None)<br>
268 self.amp = bool(self.amp) # as boolean<br>
269 self.scaler = (<br>
 → 270 torch.amp.GradScaler(“cuda”, enabled=self.amp)<br>
271 if TORCH_1_13<br>
272 else torch.cuda.amp.GradScaler(enabled=self.amp)<br>
273 )<br>
274 if world_size &gt; 1:<br>
275 self.model = nn.parallel.DistributedDataParallel(self.model, device_ids=[RANK], find_unused_parameters=True)</p>
<p>AttributeError: module ‘torch.amp’ has no attribute ‘GradScaler’</p>
<p>Attem</p>
<p>pts:<br>
Tried updating torch, torchvision. Even when I try to import torch.amp.GradScaler, still I get the Attribute error.<br>
Also I had to mention, this wasn’t happening before, I trained a yolov10 model a few days ago. I also verified on my local linux machine, it also gives the same attribute error when trying to import torch.amp.GradScaler</p>

martijn_sikking: <p>I had a simliar error. What fixed it for me was upgrading torch. I see you already tried it but maybe try it again or make sure you really have the latest version</p>

ptrblck: <p>The latest stable release and nightly binaries work for me:</p>
<pre data-code-wrap="python"><code class="lang-python">python -c "import torch; print(torch.__version__); print(torch.amp.GradScaler)"
2.3.1+cu121
&lt;class 'torch.amp.grad_scaler.GradScaler'&gt;
</code></pre>
<p>EDIT: <code>2.4.0</code> was just released, so <code>2.3.1</code> is not the “latest” stable anymore</p>

Haroon_Rashid: <p>Thank you all for your response. I upgraded torch, torchvision, and ultralytics to the latest version, and my code is running fine now.</p>