Skip to content

[Dinov2] Enable device_map="auto" support#38487

Merged
SunMarc merged 8 commits into
huggingface:mainfrom
aryanchauhan31:clean-device-map-support-dinov2
Jun 4, 2025
Merged

[Dinov2] Enable device_map="auto" support#38487
SunMarc merged 8 commits into
huggingface:mainfrom
aryanchauhan31:clean-device-map-support-dinov2

Conversation

@aryanchauhan31

Copy link
Copy Markdown
Contributor

This PR adds support for device_map="auto" to the Dinov2 model by defining _no_split_modules = ["Dinov2Layer"], which enables inference across multiple devices using Accelerate and Transformers.

✔️ Summary

  • Adds _no_split_modules = ["Dinov2Layer"] to Dinov2PreTrainedModel
  • Includes a test test_model_parallelism for multi-GPU device_map="auto" behavior using a dummy input

🔬 Why this matters

Large models like Dinov2 can now be used efficiently on limited memory setups using Transformers' device_map feature.

Closes #29786

@aryanchauhan31

Copy link
Copy Markdown
Contributor Author

Hi @amyeroberts 👋

I've added device_map="auto" support to the Dinov2 model by defining _no_split_modules = ["Dinov2Layer"], as outlined in issue #29786. This PR also includes a test (test_model_parallelism) to verify proper model offloading and parallelism.

Would really appreciate it if you could take a look when you get a chance.

@Rocketknight1

Copy link
Copy Markdown
Member

Hi @aryanchauhan31, I don't see the _no_split_modules = ["Dinov2Layer"] change in this PR!

@aryanchauhan31

Copy link
Copy Markdown
Contributor Author

Hi @Rocketknight1 , Thanks for the heads up.
The _no_split_modules = ["Dinov2Layer"] line is now correctly present in [Dinov2PreTrainedModel]

@Rocketknight1

Copy link
Copy Markdown
Member

Thanks! cc @SunMarc since I think this falls under accelerate?

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! This should indeed solve the issue. Just a nit

Comment on lines +349 to +361


@require_torch_multi_gpu
@slow
class Dinov2ModelDeviceMapTest(unittest.TestCase):
def test_model_parallelism(self):
model = Dinov2Model.from_pretrained("facebook/dinov2-base", device_map="auto")
self.assertTrue(hasattr(model, "hf_device_map"))
self.assertIsInstance(model.hf_device_map, dict)

dummy_input = torch.randn(1, 3, 224, 224).to(model.device)
outputs = model(pixel_values=dummy_input)
self.assertIsNotNone(outputs.last_hidden_state)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have tests for device_map called test_model_parallelism in the common tests we defined. We can remove this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SunMarc, I'll go ahead and remove the redundant test function as suggested.
Thanks a lot @Rocketknight1 for pointing out the earlier issues and helping me clean things up! 🙌

Comment on lines +18 to +20
import torch

from transformers import Dinov2Config, Dinov2Model

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean this also

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SunMarc — just a heads-up: Dinov2Config, Dinov2Model, and torch are still being used in the other test functions in this file, so I’ve kept those imports.

Let me know if there's anything else you'd like cleaned up.

Comment thread tests/models/dinov2/test_modeling_dinov2.py Outdated
@SunMarc SunMarc enabled auto-merge (squash) June 4, 2025 15:30
@SunMarc SunMarc merged commit b9c17c5 into huggingface:main Jun 4, 2025
14 checks passed
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

bvantuan pushed a commit to bvantuan/transformers that referenced this pull request Jun 12, 2025
* Fix: resolve import order and duplicate import (ruff I001, F811)

* Format: clean up Dinov2 test file with ruff formatter

* Add _no_split_modules = ['Dinov2Layer'] to enable device_map='auto'

* Revert dinov2_with_registers _no_split_modules to original state

* Remove redundant device_map test as suggested

* Remove unused import after deleting test

* removed import  torch and the redundant test function

* Update tests/models/dinov2/test_modeling_dinov2.py

---------

Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Community contribution: enabling device_map="auto" support for more vision and multimodal models

4 participants