Not compatible with MPS? NotImplementedError: Cannot copy out of meta tensor; no data!
#3
by pySilver - opened
Hi! I'm simply running code from example on m2 pro:
from transformers import AutoModel, AutoProcessor
import torch
from PIL import Image
import requests
model_name= 'Marqo/marqo-ecommerce-embeddings-L'
# model_name = 'Marqo/marqo-ecommerce-embeddings-B'
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
img = Image.open(requests.get('https://raw.githubusercontent.com/marqo-ai/marqo-ecommerce-embeddings/refs/heads/main/images/dining-chairs.png', stream=True).raw).convert("RGB")
image = [img]
text = ["dining chairs", "a laptop", "toothbrushes"]
processed = processor(text=text, images=image, padding='max_length', return_tensors="pt")
processor.image_processor.do_rescale = False
with torch.no_grad():
image_features = model.get_image_features(processed['pixel_values'], normalize=True)
text_features = model.get_text_features(processed['input_ids'], normalize=True)
text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
print(text_probs)
# [1.0000e+00, 8.3131e-12, 5.2173e-12]
which gives me the following output:
config.json: 0%| | 0.00/310 [00:00<?, ?B/s]
marqo_fashionSigLIP.py: 0%| | 0.00/10.7k [00:00<?, ?B/s]
A new version of the following files was downloaded from https://huggingface.co/Marqo/marqo-ecommerce-embeddings-L:
- marqo_fashionSigLIP.py
. Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.
You are using a model of type siglip to instantiate a model of type . This is not supported for all configurations of models and can yield errors.
model.safetensors: 0%| | 0.00/2.61G [00:00<?, ?B/s]
open_clip_model.safetensors: 0%| | 0.00/2.61G [00:00<?, ?B/s]
open_clip_config.json: 0%| | 0.00/884 [00:00<?, ?B/s]
[31m---------------------------------------------------------------------------[39m
[31mNotImplementedError[39m Traceback (most recent call last)
[36mCell[39m[36m [39m[32mIn[1][39m[32m, line 9[39m
[32m 6[39m model_name= [33m'[39m[33mMarqo/marqo-ecommerce-embeddings-L[39m[33m'[39m
[32m 7[39m [38;5;66;03m# model_name = 'Marqo/marqo-ecommerce-embeddings-B'[39;00m
[32m----> [39m[32m9[39m model = [43mAutoModel[49m[43m.[49m[43mfrom_pretrained[49m[43m([49m[43mmodel_name[49m[43m,[49m[43m [49m[43mtrust_remote_code[49m[43m=[49m[38;5;28;43;01mTrue[39;49;00m[43m)[49m
[32m 10[39m processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=[38;5;28;01mTrue[39;00m)
[32m 12[39m img = Image.open(requests.get([33m'[39m[33mhttps://raw.githubusercontent.com/marqo-ai/marqo-ecommerce-embeddings/refs/heads/main/images/dining-chairs.png[39m[33m'[39m, stream=[38;5;28;01mTrue[39;00m).raw).convert([33m"[39m[33mRGB[39m[33m"[39m)
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/transformers/models/auto/auto_factory.py:564[39m, in [36m_BaseAutoModelClass.from_pretrained[39m[34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)[39m
[32m 562[39m [38;5;28mcls[39m.register(config.[34m__class__[39m, model_class, exist_ok=[38;5;28;01mTrue[39;00m)
[32m 563[39m model_class = add_generation_mixin_to_remote_model(model_class)
[32m--> [39m[32m564[39m [38;5;28;01mreturn[39;00m [43mmodel_class[49m[43m.[49m[43mfrom_pretrained[49m[43m([49m
[32m 565[39m [43m [49m[43mpretrained_model_name_or_path[49m[43m,[49m[43m [49m[43m*[49m[43mmodel_args[49m[43m,[49m[43m [49m[43mconfig[49m[43m=[49m[43mconfig[49m[43m,[49m[43m [49m[43m*[49m[43m*[49m[43mhub_kwargs[49m[43m,[49m[43m [49m[43m*[49m[43m*[49m[43mkwargs[49m
[32m 566[39m [43m [49m[43m)[49m
[32m 567[39m [38;5;28;01melif[39;00m [38;5;28mtype[39m(config) [38;5;129;01min[39;00m [38;5;28mcls[39m._model_mapping.keys():
[32m 568[39m model_class = _get_model_class(config, [38;5;28mcls[39m._model_mapping)
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/transformers/modeling_utils.py:309[39m, in [36mrestore_default_torch_dtype.<locals>._wrapper[39m[34m(*args, **kwargs)[39m
[32m 307[39m old_dtype = torch.get_default_dtype()
[32m 308[39m [38;5;28;01mtry[39;00m:
[32m--> [39m[32m309[39m [38;5;28;01mreturn[39;00m [43mfunc[49m[43m([49m[43m*[49m[43margs[49m[43m,[49m[43m [49m[43m*[49m[43m*[49m[43mkwargs[49m[43m)[49m
[32m 310[39m [38;5;28;01mfinally[39;00m:
[32m 311[39m torch.set_default_dtype(old_dtype)
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/transformers/modeling_utils.py:4508[39m, in [36mPreTrainedModel.from_pretrained[39m[34m(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, weights_only, *model_args, **kwargs)[39m
[32m 4499[39m config = [38;5;28mcls[39m._autoset_attn_implementation(
[32m 4500[39m config,
[32m 4501[39m use_flash_attention_2=use_flash_attention_2,
[32m 4502[39m torch_dtype=torch_dtype,
[32m 4503[39m device_map=device_map,
[32m 4504[39m )
[32m 4506[39m [38;5;28;01mwith[39;00m ContextManagers(model_init_context):
[32m 4507[39m [38;5;66;03m# Let's make sure we don't run the init function of buffer modules[39;00m
[32m-> [39m[32m4508[39m model = [38;5;28;43mcls[39;49m[43m([49m[43mconfig[49m[43m,[49m[43m [49m[43m*[49m[43mmodel_args[49m[43m,[49m[43m [49m[43m*[49m[43m*[49m[43mmodel_kwargs[49m[43m)[49m
[32m 4510[39m [38;5;66;03m# Make sure to tie the weights correctly[39;00m
[32m 4511[39m model.tie_weights()
[36mFile [39m[32m~/.cache/huggingface/modules/transformers_modules/Marqo/marqo-ecommerce-embeddings-L/92e3b2606e34fe9a91b732229689e9fb9d422929/marqo_fashionSigLIP.py:190[39m, in [36mMarqoFashionSigLIP.__init__[39m[34m(self, config)[39m
[32m 188[39m [38;5;28msuper[39m().[34m__init__[39m(config)
[32m 189[39m [38;5;28mself[39m.config = config
[32m--> [39m[32m190[39m [38;5;28mself[39m.model = [43mcreate_model[49m[43m([49m[43mconfig[49m[43m.[49m[43mopen_clip_model_name[49m[43m,[49m[43m [49m[43moutput_dict[49m[43m=[49m[38;5;28;43;01mTrue[39;49;00m[43m)[49m
[32m 191[39m [38;5;28mself[39m.model.eval()
[32m 192[39m [38;5;28mself[39m.model.to([38;5;28mself[39m.device)
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/open_clip/factory.py:376[39m, in [36mcreate_model[39m[34m(model_name, pretrained, precision, device, jit, force_quick_gelu, force_custom_text, force_patch_dropout, force_image_size, force_preprocess_cfg, pretrained_image, pretrained_hf, cache_dir, output_dict, require_pretrained, load_weights_only, **model_kwargs)[39m
[32m 374[39m model.to(device=device, dtype=dtype)
[32m 375[39m [38;5;28;01melse[39;00m:
[32m--> [39m[32m376[39m [43mmodel[49m[43m.[49m[43mto[49m[43m([49m[43mdevice[49m[43m=[49m[43mdevice[49m[43m)[49m
[32m 378[39m pretrained_loaded = [38;5;28;01mFalse[39;00m
[32m 379[39m [38;5;28;01mif[39;00m pretrained:
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1355[39m, in [36mModule.to[39m[34m(self, *args, **kwargs)[39m
[32m 1352[39m [38;5;28;01melse[39;00m:
[32m 1353[39m [38;5;28;01mraise[39;00m
[32m-> [39m[32m1355[39m [38;5;28;01mreturn[39;00m [38;5;28;43mself[39;49m[43m.[49m[43m_apply[49m[43m([49m[43mconvert[49m[43m)[49m
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:915[39m, in [36mModule._apply[39m[34m(self, fn, recurse)[39m
[32m 913[39m [38;5;28;01mif[39;00m recurse:
[32m 914[39m [38;5;28;01mfor[39;00m module [38;5;129;01min[39;00m [38;5;28mself[39m.children():
[32m--> [39m[32m915[39m [43mmodule[49m[43m.[49m[43m_apply[49m[43m([49m[43mfn[49m[43m)[49m
[32m 917[39m [38;5;28;01mdef[39;00m[38;5;250m [39m[34mcompute_should_use_set_data[39m(tensor, tensor_applied):
[32m 918[39m [38;5;28;01mif[39;00m torch._has_compatible_shallow_copy_type(tensor, tensor_applied):
[32m 919[39m [38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,[39;00m
[32m 920[39m [38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,[39;00m
[32m (...)[39m[32m 925[39m [38;5;66;03m# global flag to let the user control whether they want the future[39;00m
[32m 926[39m [38;5;66;03m# behavior of overwriting the existing tensor or not.[39;00m
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:915[39m, in [36mModule._apply[39m[34m(self, fn, recurse)[39m
[32m 913[39m [38;5;28;01mif[39;00m recurse:
[32m 914[39m [38;5;28;01mfor[39;00m module [38;5;129;01min[39;00m [38;5;28mself[39m.children():
[32m--> [39m[32m915[39m [43mmodule[49m[43m.[49m[43m_apply[49m[43m([49m[43mfn[49m[43m)[49m
[32m 917[39m [38;5;28;01mdef[39;00m[38;5;250m [39m[34mcompute_should_use_set_data[39m(tensor, tensor_applied):
[32m 918[39m [38;5;28;01mif[39;00m torch._has_compatible_shallow_copy_type(tensor, tensor_applied):
[32m 919[39m [38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,[39;00m
[32m 920[39m [38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,[39;00m
[32m (...)[39m[32m 925[39m [38;5;66;03m# global flag to let the user control whether they want the future[39;00m
[32m 926[39m [38;5;66;03m# behavior of overwriting the existing tensor or not.[39;00m
[31m[... skipping similar frames: Module._apply at line 915 (1 times)][39m
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:915[39m, in [36mModule._apply[39m[34m(self, fn, recurse)[39m
[32m 913[39m [38;5;28;01mif[39;00m recurse:
[32m 914[39m [38;5;28;01mfor[39;00m module [38;5;129;01min[39;00m [38;5;28mself[39m.children():
[32m--> [39m[32m915[39m [43mmodule[49m[43m.[49m[43m_apply[49m[43m([49m[43mfn[49m[43m)[49m
[32m 917[39m [38;5;28;01mdef[39;00m[38;5;250m [39m[34mcompute_should_use_set_data[39m(tensor, tensor_applied):
[32m 918[39m [38;5;28;01mif[39;00m torch._has_compatible_shallow_copy_type(tensor, tensor_applied):
[32m 919[39m [38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,[39;00m
[32m 920[39m [38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,[39;00m
[32m (...)[39m[32m 925[39m [38;5;66;03m# global flag to let the user control whether they want the future[39;00m
[32m 926[39m [38;5;66;03m# behavior of overwriting the existing tensor or not.[39;00m
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:942[39m, in [36mModule._apply[39m[34m(self, fn, recurse)[39m
[32m 938[39m [38;5;66;03m# Tensors stored in modules are graph leaves, and we don't want to[39;00m
[32m 939[39m [38;5;66;03m# track autograd history of `param_applied`, so we have to use[39;00m
[32m 940[39m [38;5;66;03m# `with torch.no_grad():`[39;00m
[32m 941[39m [38;5;28;01mwith[39;00m torch.no_grad():
[32m--> [39m[32m942[39m param_applied = [43mfn[49m[43m([49m[43mparam[49m[43m)[49m
[32m 943[39m p_should_use_set_data = compute_should_use_set_data(param, param_applied)
[32m 945[39m [38;5;66;03m# subclasses may have multiple child tensors so we need to use swap_tensors[39;00m
[36mFile [39m[32m~/Projects/sandbox/baml-example/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1348[39m, in [36mModule.to.<locals>.convert[39m[34m(t)[39m
[32m 1346[39m [38;5;28;01mexcept[39;00m [38;5;167;01mNotImplementedError[39;00m [38;5;28;01mas[39;00m e:
[32m 1347[39m [38;5;28;01mif[39;00m [38;5;28mstr[39m(e) == [33m"[39m[33mCannot copy out of meta tensor; no data![39m[33m"[39m:
[32m-> [39m[32m1348[39m [38;5;28;01mraise[39;00m [38;5;167;01mNotImplementedError[39;00m(
[32m 1349[39m [33mf[39m[33m"[39m[38;5;132;01m{[39;00me[38;5;132;01m}[39;00m[33m Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() [39m[33m"[39m
[32m 1350[39m [33mf[39m[33m"[39m[33mwhen moving module from meta to a different device.[39m[33m"[39m
[32m 1351[39m ) [38;5;28;01mfrom[39;00m[38;5;250m [39m[38;5;28;01mNone[39;00m
[32m 1352[39m [38;5;28;01melse[39;00m:
[32m 1353[39m [38;5;28;01mraise[39;00m
[31mNotImplementedError[39m: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() when moving module from meta to a different device.
I think it used to work some time ago, but not any more. I'm running transformers 4.52.4
Bumping this for visibility. It may be an issue with transformers library that is breaking this.
had same issue, using open_clip for model loading worked