使用一台有8G内存的服务器,按示例加载模型,出现OOM错误

#11
by mkchan - opened

在ipython中依次执行:

from transformers import AutoModel

model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-zh', trust_remote_code=True)

出现以下错误:

RuntimeError                              Traceback (most recent call last)
Cell In[2], line 1
----> 1 model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-zh', trust_remote_code=True)

File ~/miniconda3/envs/aigame/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py:556, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    554     else:
    555         cls.register(config.__class__, model_class, exist_ok=True)
--> 556     return model_class.from_pretrained(
    557         pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
    558     )
    559 elif type(config) in cls._model_mapping.keys():
    560     model_class = _get_model_class(config, cls._model_mapping)

File ~/miniconda3/envs/aigame/lib/python3.10/site-packages/transformers/modeling_utils.py:3375, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, *model_args, **kwargs)
   3369 config = cls._autoset_attn_implementation(
   3370     config, use_flash_attention_2=use_flash_attention_2, torch_dtype=torch_dtype, device_map=device_map
   3371 )
   3373 with ContextManagers(init_contexts):
   3374     # Let's make sure we don't run the init function of buffer modules
-> 3375     model = cls(config, *model_args, **model_kwargs)
   3377 # make sure we use the model's config since the __init__ call might have copied it
   3378 config = model.config

File ~/.cache/huggingface/modules/transformers_modules/jinaai/jina-bert-implementation/f3ec4cf7de7e561007f27c9efc7148b0bd713f81/modeling_bert.py:1113, in JinaBertModel.__init__(self, config, add_pooling_layer)
   1110     self.tokenizer = AutoTokenizer.from_pretrained(config._name_or_path)
   1112 self.embeddings = JinaBertEmbeddings(config)
-> 1113 self.encoder = JinaBertEncoder(config)
   1115 self.pooler = JinaBertPooler(config) if add_pooling_layer else None
   1117 # Initialize weights and apply final processing

File ~/.cache/huggingface/modules/transformers_modules/jinaai/jina-bert-implementation/f3ec4cf7de7e561007f27c9efc7148b0bd713f81/modeling_bert.py:704, in JinaBertEncoder.__init__(self, config)
    700 self.gradient_checkpointing = False
    701 self.num_attention_heads = config.num_attention_heads
    702 self.register_buffer(
    703     "alibi",
--> 704     self.rebuild_alibi_tensor(size=config.max_position_embeddings),
    705     persistent=False,
    706 )

File ~/.cache/huggingface/modules/transformers_modules/jinaai/jina-bert-implementation/f3ec4cf7de7e561007f27c9efc7148b0bd713f81/modeling_bert.py:741, in JinaBertEncoder.rebuild_alibi_tensor(self, size, device)
    739 context_position = torch.arange(size, device=device)[:, None]
    740 memory_position = torch.arange(size, device=device)[None, :]
--> 741 relative_position = torch.abs(memory_position - context_position)
    742 # [n_heads, max_token_length, max_token_length]
    743 relative_position = relative_position.unsqueeze(0).expand(n_heads, -1, -1)

RuntimeError: [enforce fail at alloc_cpu.cpp:117] err == 0. DefaultCPUAllocator: can't allocate memory: you tried to allocate 536870912 bytes. Error code 12 (Cannot allocate memory)

根据报错信息来看you tried to allocate 536870912 bytes.,需要分配500M内存,但是我这台机器上并没有运行任何其他程序,内存应该完全足够才对。我在另一台有16g内存(大约有4g剩余可用内存的情况下)的mac上运行则没有任何问题。

望解答,感谢。

Sign up or log in to comment