# Potential bug in the rt-detr v2 fine tune script

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 8, 2025, 11:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774 "2025-02-08T23:22:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzzz567](https://avatars.discourse-cdn.com/v4/letter/z/bbce88/32.png) [@zzzz567](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fzzzz567)%5C
**Post date:** [February 8, 2025, 11:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%2F1 "2025-02-08T23:22:52Z")

</div>

Hi,

I have been running this [script](https://github.com/qubvel/transformers-notebooks/blob/main/notebooks/RT_DETR_v2_finetune_on_a_custom_dataset.ipynb) in google colab.

First of all, thank you very much for this, super clear!

I have noticed a potential bug when running the `trainer.evaluate` command.

The original is working fine however as soon I try to run it for a single record or with a batch size creating a single record batch (let’s say 9 since eval batch size default to 8) then it fails.

```auto
ValueError Traceback (most recent call last)
in <cell line: 0>()
----> 1 metrics2 = trainer.evaluate(eval_dataset=t_dataset, metric_key_prefix="eval")

4 frames
in collect_targets(self, targets, image_sizes)
     33 # here we have "yolo" format (x_center, y_center, width, height) in relative coordinates 0..1
     34 # and we need to convert it to "pascal" format (x_min, y_min, x_max, y_max) in absolute coordinates
---> 35 height, width = size
     36 boxes = torch.tensor(target["boxes"])
     37 boxes = center_to_corners_format(boxes)

ValueError: not enough values to unpack (expected 2, got 1)

```

If you want to recreate the error:

```auto
 t_dataset = CPPE5Dataset(dataset["test"].select([1]), image_processor, transform=validation_transform)

metrics = trainer.evaluate(eval_dataset=t_dataset, metric_key_prefix="eval")

```

or

```auto
t_dataset = CPPE5Dataset(dataset["test"].select(list(range(2))), image_processor, transform=validation_transform)

metrics = trainer.evaluate(eval_dataset=t_dataset, metric_key_prefix="eval")

```

After diving a bit more in the error, realised that the model prediction seem to select only the first index for all the values in the label dictionary when the batch size is one. That is why the image\_size only has `tensor([480])` instead of `tensor([480,480])`.

Hope you guys can help! Thank you

---

<div class="post-metadata">

**Author:** ![nicholasgcoles](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nicholasgcoles/32/41589_2.png) [@nicholasgcoles](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnicholasgcoles)%5C
**Post date:** [February 20, 2025, 9:12am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%2F2 "2025-02-20T09:12:16Z")

</div>

We ran into a similar issue. For now the “fix” is to ensure that a batch can never have 1 element in it. In our case the batch size is 8, so we make sure that the validation\_df never has a remainder of 1 when dividing by 8.

e.g.

```auto
    validation_size = len(validation_df)
    remainder = validation_size % 8
    if remainder == 1:
        sample_to_move = train_df.iloc[0:1]
        train_df = train_df.iloc[1:]
        validation_df = pd.concat([validation_df, sample_to_move]).reset_index(drop=True)

```

---

<div class="post-metadata">

**Author:** ![xyziamafish](https://avatars.discourse-cdn.com/v4/letter/x/8c91f0/32.png) [@xyziamafish](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fxyziamafish)%5C
**Post date:** [February 27, 2025, 8:32am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%2F3 "2025-02-27T08:32:24Z")

</div>

@nicholasgcoles  
Since you’ve run into this issue at the evaluation stage, you’ve managed to train successfully it seems. I’m running onto a problem here:  
from transformers import AutoModelForObjectDetection

```auto
model = AutoModelForObjectDetection.from_pretrained(
    checkpoint,
    id2label=id2label,
    label2id=label2id,
    ignore_mismatched_sizes=True,
)

RuntimeError: Error(s) in loading state_dict for RTDetrV2ForObjectDetection:
	size mismatch for model.denoising_class_embed.weight: copying a param with shape torch.Size([81, 256]) from checkpoint, the shape in current model is torch.Size([6, 256]).
	size mismatch for model.enc_score_head.weight: copying a param with shape torch.Size([80, 256]) from checkpoint, the shape in current model is torch.Size([5, 256]).
	size mismatch for model.enc_score_head.bias: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([5]).
            ...

```

This seems to relate to the classes (coco’s 80 vs the cppe-5’s 5 classes). What’s odd is the ignore\_mismatched\_sizes=True doesn’t ignore this

Did either of you run into this?  
cc @qubvel-hf as you authored this tutorial

Any help would be greatly appreciated

FYI I’m running the notebook on colab, setup below (though also ran into this on my local Ubuntu 20.04, x86 machine)  
numpy version: 1.26.4  
transformers version: 4.50.0.dev0  
torch version: 2.5.1+cu124  
Python 3.11.11  
processor : 0  
vendor\_id : GenuineIntel  
cpu family : 6  
model : 79  
model name : Intel(R) Xeon(R) CPU @ 2.20GHz

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [February 27, 2025, 10:10am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%2F4 "2025-02-27T10:10:07Z")

</div>

Seems related:

> <https://github.com/huggingface/transformers/issues/34675>
>
> \### System Info
> 
> \`transformers\` version 4.47.0.dev0
> \`accelerate\` version 0.34….2
> \`timm\` version 1.0.11
> \`supervision\` version 0.25.0rc2
> 
> \### Who can help?
> 
> @muellerzr @Arth
> 
> \### Information
> 
> \- \[\] The official example scripts
> \- \[\] My own modified scripts
> 
> \### Tasks
> 
> \- \[\] An officially supported task in the \`examples\` folder (such as GLUE/SQuAD, ...)
> \- \[\] My own task or dataset (give details below)
> 
> \### Reproduction
> 
> I’m training a DETR Object Detection model using the Trainer API. I have properly created the coco dataset.
> 
> But when I run the Trainer API with \`custom\_metrics\`, I get the error “OutOfMemoryError: CUDA out of memory.”. I have reduced batch\_size from 16 until 1, but the same error of “Out of memory”. 
> 
> \`\`\`
> def collate\_fn(batch):
> data = {}
> data\["pixel\_values"\] = torch.stack(\[x\["pixel\_values"\] for x in batch\])
> data\["labels"\] = \[x\["labels"\] for x in batch\]
> return data
> \`\`\`
> 
> Here’s how I’m creating the \`custom\_metrics\` function
> 
> \`\`\`
> id2label = {id: label for id, label in enumerate(train\_ds.classes)}
> label2id = {label: id for id, label in enumerate(train\_ds.classes)}
> 
> 
> @dataclass
> class ModelOutput:
> logits: torch.Tensor
> pred\_boxes: torch.Tensor
> 
> class MAPEvaluator:
> 
> def \_\_init\_\_(self, image\_processor, threshold=0.00, id2label=None):
> self.image\_processor = image\_processor
> self.threshold = threshold
> self.id2label = id2label
> 
> def collect\_image\_sizes(self, targets):
> """Collect image sizes across the dataset as list of tensors with shape \[batch\_size, 2\]."""
> image\_sizes = \[\]
> for batch in targets:
> batch\_image\_sizes = torch.tensor(np.array(\[x\["size"\] for x in batch\]))
> image\_sizes.append(batch\_image\_sizes)
> return image\_sizes
> 
> def collect\_targets(self, targets, image\_sizes):
> post\_processed\_targets = \[\]
> for target\_batch, image\_size\_batch in zip(targets, image\_sizes):
> for target, (height, width) in zip(target\_batch, image\_size\_batch):
> boxes = target\["boxes"\]
> boxes = sv.xcycwh\_to\_xyxy(boxes)
> boxes = boxes \* np.array(\[width, height, width, height\])
> boxes = torch.tensor(boxes)
> labels = torch.tensor(target\["class\_labels"\])
> post\_processed\_targets.append({"boxes": boxes, "labels": labels})
> return post\_processed\_targets
> 
> def collect\_predictions(self, predictions, image\_sizes):
> post\_processed\_predictions = \[\]
> for batch, target\_sizes in zip(predictions, image\_sizes):
> batch\_logits, batch\_boxes = batch\[1\], batch\[2\]
> output = ModelOutput(logits=torch.tensor(batch\_logits), pred\_boxes=torch.tensor(batch\_boxes))
> post\_processed\_output = self.image\_processor.post\_process\_object\_detection(
> output, threshold=self.threshold, target\_sizes=target\_sizes
> )
> post\_processed\_predictions.extend(post\_processed\_output)
> return post\_processed\_predictions
> 
> @torch.no\_grad()
> def \_\_call\_\_(self, evaluation\_results):
> 
> predictions, targets = evaluation\_results.predictions, evaluation\_results.label\_ids
> 
> image\_sizes = self.collect\_image\_sizes(targets)
> post\_processed\_targets = self.collect\_targets(targets, image\_sizes)
> post\_processed\_predictions = self.collect\_predictions(predictions, image\_sizes)
> 
> evaluator = MeanAveragePrecision(box\_format="xyxy", class\_metrics=True)
> evaluator.warn\_on\_many\_detections = False
> evaluator.update(post\_processed\_predictions, post\_processed\_targets)
> 
> metrics = evaluator.compute()
> 
> # Replace list of per class metrics with separate metric for each class
> classes = metrics.pop("classes")
> map\_per\_class = metrics.pop("map\_per\_class")
> mar\_100\_per\_class = metrics.pop("mar\_100\_per\_class")
> for class\_id, class\_map, class\_mar in zip(classes, map\_per\_class, mar\_100\_per\_class):
> class\_name = id2label\[class\_id.item()\] if id2label is not None else class\_id.item()
> metrics\[f"map\_{class\_name}"\] = class\_map
> metrics\[f"mar\_100\_{class\_name}"\] = class\_mar
> 
> metrics = {k: round(v.item(), 4) for k, v in metrics.items()}
> 
> return metrics
> 
> eval\_compute\_metrics\_fn = MAPEvaluator(image\_processor=processor, threshold=0.01, id2label=id2label)
> \`\`\`
> This is how I'm training the model
> 
> \`\`\`
> training\_args = TrainingArguments(
> output\_dir=f"Malaria-finetune",
> report\_to="none",
> num\_train\_epochs=10,
> max\_grad\_norm=0.1,
> learning\_rate=5e-5,
> warmup\_steps=300,
> per\_device\_train\_batch\_size=1,
> dataloader\_num\_workers=2,
> metric\_for\_best\_model="eval\_map",
> greater\_is\_better=True,
> load\_best\_model\_at\_end=True,
> eval\_strategy="epoch",
> save\_strategy="epoch",
> save\_total\_limit=2,
> remove\_unused\_columns=False,
> eval\_do\_concat\_batches=False,
> )
> 
> trainer = Trainer(
> model=model,
> args=training\_args,
> train\_dataset=pytorch\_dataset\_train,
> eval\_dataset=pytorch\_dataset\_valid,
> processing\_class=processor,
> data\_collator=collate\_fn,
> compute\_metrics=eval\_compute\_metrics\_fn
> )
> trainer.train()
> 
> \`\`\`
> 
> I would like to please get help on this. 
> 
> \### Expected behavior
> 
> I expected the Trainer to train normally on this custom dataset for 10 epochs without any errors

---

<div class="post-metadata">

**Author:** ![Kallinteris-Andreas](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/kallinteris-andreas/32/50408_2.png) [@Kallinteris-Andreas](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FKallinteris-Andreas)%5C
**Post date:** [July 29, 2025, 4:04pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%2F5 "2025-07-29T16:04:46Z")

</div>

From what I can tell this also affects `D-fine` which uses the same processor as RT-DETR and RT-DETRv2 (`RTDetrImageProcessor`)

Is there any solution for the script?

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [July 29, 2025, 10:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fpotential-bug-in-the-rt-detr-v2-fine-tune-script%2F139774%2F6 "2025-07-29T22:12:44Z")

</div>

If we want to fix it forcefully, it would be something like this…?

```py
    def collect_targets(self, targets, image_sizes):
        post_processed_targets = []
        for target_batch, image_size_batch in zip(targets, image_sizes):
            if image_size_batch.ndim == 1: # <=
                image_size_batch = image_size_batch.unsqueeze(0) # <=
            for target, size in zip(target_batch, image_size_batch):

                # here we have "yolo" format (x_center, y_center, width, height) in relative coordinates 0..1
                # and we need to convert it to "pascal" format (x_min, y_min, x_max, y_max) in absolute coordinates
                height, width = size
                boxes = torch.tensor(target["boxes"])
                boxes = center_to_corners_format(boxes)
                boxes = boxes * torch.tensor([[width, height, width, height]])

```

To fix it fundamentally, fixing in upstream is maybe necessary, but it seems that the issue has been raised and remains unresolved.

> <https://github.com/huggingface/transformers/issues/33165>
>
> \### System Info
> 
> \- \`transformers\` version: 4.44.2
> \- Platform: Linux-5.4.0-174…-generic-x86\_64-with-glibc2.31
> \- Python version: 3.11.6
> \- Huggingface\_hub version: 0.24.6
> \- Safetensors version: 0.4.4
> \- Accelerate version: 0.33.0
> \- Accelerate config: not found
> \- PyTorch version (GPU?): 2.1.2+cu121 (True)
> \- Tensorflow version (GPU?): not installed (NA)
> \- Flax version (CPU?/GPU?/TPU?): not installed (NA)
> \- Jax version: not installed
> \- JaxLib version: not installed
> \- Using distributed or parallel set-up in script?: \<fill in\>
> \- Using GPU in script?: \<fill in\>
> \- GPU type: Tesla V100-DGXS-16GB
> 
> \### Who can help?
> 
> @amyeroberts @muellerz @SunMarc 
> 
> \### Information
> 
> \- \[X\] The official example scripts
> \- \[X\] My own modified scripts
> 
> \### Tasks
> 
> \- \[\] An officially supported task in the \`examples\` folder (such as GLUE/SQuAD, ...)
> \- \[\] My own task or dataset (give details below)
> 
> \### Reproduction
> 
> Following the example on \[the official pytorch example\](https://github.com/huggingface/transformers/blob/main/examples/pytorch/object-detection/run\_object\_detection.py) and \[here\](https://huggingface.co/docs/transformers/en/tasks/object\_detection) it seems that I get the stack trace below after following these steps:
> 
> 1. Set the initial model class to RT-DETR assuming other parts of the example have been followed 
> \`\`\`
> IMAGE\_SIZE = 1280
> CHECKPOINT = "PekingU/rtdetr\_r50vd\_coco\_o365"
> DEVICE = torch.device("cuda" if torch.cuda.is\_available() else "cpu")
> model = AutoModelForObjectDetection.from\_pretrained(
> CHECKPOINT,
> id2label=id2label,
> label2id=label2id,
> anchor\_image\_size=None,
> ignore\_mismatched\_sizes=True
> )
> \`\`\`
> 
> 2. Set the batch size to 4 and set to 4 visible GPUs assuming other parts of the example have been followed
> \`\`\`
> os.environ\["CUDA\_VISIBLE\_DEVICES"\] = "0,1,2,3"
> 
> training\_args = TrainingArguments(
> output\_dir=output\_path,
> num\_train\_epochs=20,
> max\_grad\_norm=0.1,
> learning\_rate=5e-5,
> warmup\_steps=300,
> per\_device\_train\_batch\_size=4,
> dataloader\_num\_workers=2,
> metric\_for\_best\_model="eval\_map",
> greater\_is\_better=True,
> load\_best\_model\_at\_end=True,
> eval\_strategy="epoch",
> save\_strategy="epoch",
> save\_total\_limit=2,
> remove\_unused\_columns=False,
> eval\_do\_concat\_batches=False,
> )
> \`\`\`
> 
> 3. Run training
> \`\`\`
> trainer = Trainer(
> model=model,
> args=training\_args,
> train\_dataset=pytorch\_dataset\_train,
> eval\_dataset=pytorch\_dataset\_valid,
> tokenizer=processor,
> data\_collator=collate\_fn,
> compute\_metrics=eval\_compute\_metrics\_fn,
> )
> 
> trainer.train()
> \`\`\`
> 
> Stack trace:
> \`\`\`
> RuntimeError Traceback (most recent call last)
> Cell In\[13\], \[line 11\](vscode-notebook-cell:?execution\_count=13&line=11)
> \[1\](vscode-notebook-cell:?execution\_count=13&line=1) trainer = Trainer(
> \[2\](vscode-notebook-cell:?execution\_count=13&line=2) model=model,
> \[3\](vscode-notebook-cell:?execution\_count=13&line=3) args=training\_args,
> (...)
> \[8\](vscode-notebook-cell:?execution\_count=13&line=8) compute\_metrics=eval\_compute\_metrics\_fn,
> \[9\](vscode-notebook-cell:?execution\_count=13&line=9) )
> \---\> \[11\](vscode-notebook-cell:?execution\_count=13&line=11) trainer.train()
> 
> File ~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1938, in Trainer.train(self, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval, \*\*kwargs)
> \[1936\](https://vscode-remote+ssh-002dremote-002bbrain-002ds-002d1-002ehq-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1936) hf\_hub\_utils.enable\_progress\_bars()
> \[1937\](https://vscode-remote+ssh-002dremote-002brain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1937) else:
> \-\> \[1938\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1938) return inner\_training\_loop(
> \[1939\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1939) args=args,
> \[1940\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1940) resume\_from\_checkpoint=resume\_from\_checkpoint,
> \[1941\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1941) trial=trial,
> \[1942\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1942) ignore\_keys\_for\_eval=ignore\_keys\_for\_eval,
> \[1943\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:1943) )
> 
> File ~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:2279, in Trainer.\_inner\_training\_loop(self, batch\_size, args, resume\_from\_checkpoint, trial, ignore\_keys\_for\_eval)
> \[2276\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:2276) self.control = self.callback\_handler.on\_step\_begin(args, self.state, self.control)
> \[2278\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:2278) with self.accelerator.accumulate(model):
> \-\> \[2279\](https://vscode-remote+ssh-002dremote-002bbrain-002ecom.vscode-resource.vscode-cdn.net/home/jb/repos/ml/ll\_ml/models/rt\_detr/~/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/trainer.py:2279) tr\_loss\_step = self.training\_step(model, inputs)
> ...
> File "/home/jb/.cache/pypoetry/virtualenvs/ml-Mf12zaqr-py3.11/lib/python3.11/site-packages/transformers/models/rt\_detr/modeling\_rt\_detr.py", line 1850, in forward
> reference\_points\_unact = torch.concat(\[denoising\_bbox\_unact, reference\_points\_unact\], 1)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 16 but got size 4 for tensor number 1 in the list.
> \`\`\`
> 
> It seems that the first tensor is not split amongst the GPUs? Is there something else I'm missing here? I have not set up \`accelerate\` for instance, but the example linked does not state that it is required, and also states that at least one GPU is needed, making no specification about setting up \`accelerate\`.
> 
> \### Expected behavior
> 
> I would expect to see training taking place as it does when I use just one GPU:
> 
> !\[image\](https://github.com/user-attachments/assets/9bc3c5b9-b486-4223-8b16-dec9edce95dd)

> <https://github.com/huggingface/transformers/issues/38329>
>
> \### System Info
> 
> System Info
> \- transformers version: 4.52.3 (but the problem occ…urs since the 4.51.3-BitNet-preview version)
> \- Platform: Microsoft Windows 11 Pro
> \- huggingface-hub versión: 0.32.0
> \- safetensors : 0.5.3
> \- Python versión: 3.9 (cpython-3.9.22-windows-x86\_64-none)
> \- Tensorflow version (GPU?): not installed 
> \- GPU type: NVIDIA QUADRO P1000 
> 
> Dependecies:
> \*Installed via uv add\*
> \- pillow==11.2.1
> \- torch==2.2.0+cu118
> \- transformers==4.52.3
> \- numpy==1.24.4
> \- accelerate==1.7.0
> 
> 
> \### Who can help?
> 
> @amyeroberts 
> @qubvel 
> @Cyrilvallez 
> 
> \### Information
> 
> \- \[x\] The official example scripts
> \- \[\] My own modified scripts
> 
> \### Tasks
> 
> \- \[x\] An officially supported task in the \`examples\` folder (such as GLUE/SQuAD, ...)
> \- \[\] My own task or dataset (give details below)
> 
> \### Reproduction
> 
> Using torch==2.2.0+cu118 with Transformers 4.52.3 using the First \[example\](https://huggingface.co/docs/transformers/main/en/model\_doc/rt\_detr\_v2#overview) of RT-DETRv2 of the transformer page. Actually, just running transformers env also gives the error.
> 
> \`\`\`
> import torch
> import requests
> 
> from PIL import Image
> from transformers import RTDetrV2ForObjectDetection, RTDetrImageProcessor
> 
> url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
> image = Image.open(requests.get(url, stream=True).raw)
> 
> image\_processor = RTDetrImageProcessor.from\_pretrained("PekingU/rtdetr\_v2\_r18vd")
> model = RTDetrV2ForObjectDetection.from\_pretrained("PekingU/rtdetr\_v2\_r18vd")
> 
> inputs = image\_processor(images=image, return\_tensors="pt")
> 
> with torch.no\_grad():
> outputs = model(\*\*inputs)
> 
> results = image\_processor.post\_process\_object\_detection(outputs, target\_sizes=torch.tensor(\[(image.height, image.width)\]), threshold=0.5)
> 
> for result in results:
> for score, label\_id, box in zip(result\["scores"\], result\["labels"\], result\["boxes"\]):
> score, label = score.item(), label\_id.item()
> box = \[round(i, 2) for i in box.tolist()\]
> print(f"{model.config.id2label\[label\]}: {score:.2f} {box}")
> \`\`\`
> Give the next issue:
> 
> \`\`\`
> Traceback (most recent call last):
> File "e:\\playground\\prof\_3\\new\_proof\\main.py", line 11, in \<module\>
> model = RTDetrV2ForObjectDetection.from\_pretrained("PekingU/rtdetr\_v2\_r18vd")
> File "E:\\playground\\prof\_3\\new\_proof\\.venv\\lib\\site-packages\\transformers\\modeling\_utils.py", line 309, in \_wrapper
> return func(\*args, \*\*kwargs)
> File "E:\\playground\\prof\_3\\new\_proof\\.venv\\lib\\site-packages\\transformers\\modeling\_utils.py", line 4252, in from\_pretrained
> device\_in\_context = get\_torch\_context\_manager\_or\_global\_device()
> File "E:\\playground\\prof\_3\\new\_proof\\.venv\\lib\\site-packages\\transformers\\modeling\_utils.py", line 322, in get\_torch\_context\_manager\_or\_global\_device
> default\_device = torch.get\_default\_device()
> File "E:\\playground\\prof\_3\\new\_proof\\.venv\\lib\\site-packages\\torch\\\_\_init\_\_.py", line 1932, in \_\_getattr\_\_
> raise AttributeError(f"module '{\_\_name\_\_}' has no attribute '{name}'")
> AttributeError: module 'torch' has no attribute 'get\_default\_device'
> \`\`\`
> 
> This worked until versión 4.51.3. But since the release of the 4.51.3-XXXX-preview versions and the official 4.52.0
> which include the \[#37216\](https://github.com/huggingface/transformers/pull/37216) this issue is present. I believe that is because of the use of torch.get\_default\_device() because this function appears since PyTorch 2.3.0 but Pytorch 2.2 doesn't have it (You can look of a similar issue in the official Pytorch GitHub https://github.com/pytorch/pytorch/issues/126632).
> 
> The issue is "fixed" if you change:
> \`default\_device = torch.get\_default\_device()\`
> to
> \`default\_device = "cuda" if torch.cuda.is\_available() else "cpu"\`
> 
> And I know is ugly but It works for this Pytorch version.
> 
> 
> \### Expected behavior
> 
> No exception should be thrown when loading the model.
