Fix mismatching loading in from_pretrained with/without accelerate (#28414)
* fix mismatching behavior in from_pretrained with/without accelerate * meaningful refactor * remove added space * add test * fix model on the hub * comment * use tiny model * style
This commit is contained in:
@@ -34,6 +34,7 @@ from requests.exceptions import HTTPError
|
||||
from transformers import (
|
||||
AutoConfig,
|
||||
AutoModel,
|
||||
OwlViTForObjectDetection,
|
||||
PretrainedConfig,
|
||||
is_torch_available,
|
||||
logging,
|
||||
@@ -835,6 +836,23 @@ class ModelUtilsTest(TestCasePlus):
|
||||
outputs2 = new_model_with_offload(inputs)
|
||||
self.assertTrue(torch.allclose(outputs1[0].cpu(), outputs2[0].cpu()))
|
||||
|
||||
@slow
|
||||
@require_torch
|
||||
def test_from_pretrained_non_contiguous_checkpoint(self):
|
||||
# See: https://github.com/huggingface/transformers/pull/28414
|
||||
# Tiny models on the Hub have contiguous weights, contrarily to google/owlvit
|
||||
model = OwlViTForObjectDetection.from_pretrained("fxmarty/owlvit-tiny-non-contiguous-weight")
|
||||
self.assertTrue(model.owlvit.visual_projection.weight.is_contiguous())
|
||||
|
||||
model = OwlViTForObjectDetection.from_pretrained(
|
||||
"fxmarty/owlvit-tiny-non-contiguous-weight", device_map="auto"
|
||||
)
|
||||
self.assertTrue(model.owlvit.visual_projection.weight.is_contiguous())
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
model.save_pretrained(tmp_dir, safe_serialization=False)
|
||||
model.save_pretrained(tmp_dir, safe_serialization=True)
|
||||
|
||||
def test_cached_files_are_used_when_internet_is_down(self):
|
||||
# A mock response for an HTTP head request to emulate server down
|
||||
response_mock = mock.Mock()
|
||||
|
||||
Reference in New Issue
Block a user