模型下载到本地后,加载模型报错(bge-reranker-v2-m3和bge-reranker-v2-gemma都可以)

#7
by hanswang1973 - opened

模型文件已拷贝到 './model/bge-reranker-v2-minicpm-layerwise/' 目录下

代码:
from FlagEmbedding import LayerWiseFlagLLMReranker
reranker_model = LayerWiseFlagLLMReranker('./model/bge-reranker-v2-minicpm-layerwise/', use_fp16=True)

运行后报错:
Could not locate the configuration_minicpm_reranker.py inside BAAI/bge-reranker-v2-minicpm-layerwise.

检查了 configuration_minicpm_reranker.py 的内容,都正常
bge-reranker-v2-m3和bge-reranker-v2-gemma都可以

测试了下代码
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True)
tokenizer.save_pretrained('./bge-reranker-v2-minicpm-layerwise')
model.save_pretrained('./bge-reranker-v2-minicpm-layerwise')

from FlagEmbedding import LayerWiseFlagLLMReranker
reranker_model = LayerWiseFlagLLMReranker('./bge-reranker-v2-minicpm-layerwise/', use_fp16=True)
可以正常运行,是模型保存方面出问题了吗

多谢,我重新全部下载下试试

我是直接从huggingface上面下载的,没问题吧?

没问题的

全部重新从huggingface下载了模型文件,还是同样的错误

用你的代码,在本地,加载一半就自动退出,估计是CPU内存不够;在服务器上,连不到huggingface

这个报错是在本地报的吗,还是在服务器上的报错,有完整的报错信息吗,运行的时候需要机器能连接到huggingface访问到configuration_minicpm_reranker.py

你好,我这边也遇到了类似问题,直接运行本地的模型和代码报错,(服务器访问不了huggingface)
ValueError: Loading /home/bge-reranker-v2-minicpm-layerwise requires you to execute the configuration file in that repo on your local machine. Make sure you have read the code there to avoid malicious use, then set the option trust_remote_code=True to remove this error.

这个报错是在本地报的吗,还是在服务器上的报错,有完整的报错信息吗,运行的时候需要机器能连接到huggingface访问到configuration_minicpm_reranker.py

啊?!?!我把这个文件连同模型文件,都一起下载到本地了啊!bge-m3 / bge-reranker-large 都没有类似的问题啊?

你好,我这边也遇到了类似问题,直接运行本地的模型和代码报错,(服务器访问不了huggingface)
ValueError: Loading /home/bge-reranker-v2-minicpm-layerwise requires you to execute the configuration file in that repo on your local machine. Make sure you have read the code there to avoid malicious use, then set the option trust_remote_code=True to remove this error.

是不是就是因为没加 trust_remote_code=True 的原因?但LayerWiseFlagLLMReranker()不让加这个参数啊,加了就报错

完整报错信息:

Could not locate the configuration_minicpm_reranker.py inside BAAI/bge-reranker-v2-minicpm-layerwise.
Traceback (most recent call last):
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connection.py", line 203, in _new_conn
sock = connection.create_connection(
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
raise err
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/util/connection.py", line 73, in create_connection
sock.connect(sa)
TimeoutError: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connectionpool.py", line 790, in urlopen
response = self._make_request(
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connectionpool.py", line 491, in _make_request
raise new_e
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connectionpool.py", line 1096, in _validate_conn
conn.connect()
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connection.py", line 611, in connect
self.sock = sock = self._new_conn()
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connection.py", line 212, in _new_conn
raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x7f7d21919b10>, 'Connection to huggingface.co timed out. (connect timeout=10)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/root/miniconda3/lib/python3.10/site-packages/requests/adapters.py", line 486, in send
resp = conn.urlopen(
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/connectionpool.py", line 844, in urlopen
retries = retries.increment(
File "/root/miniconda3/lib/python3.10/site-packages/urllib3/util/retry.py", line 515, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /BAAI/bge-reranker-v2-minicpm-layerwise/resolve/main/configuration_minicpm_reranker.py (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f7d21919b10>, 'Connection to huggingface.co timed out. (connect timeout=10)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1261, in hf_hub_download
metadata = get_hf_file_metadata(
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
return fn(*args, **kwargs)
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1667, in get_hf_file_metadata
r = _request_wrapper(
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 385, in _request_wrapper
response = _request_wrapper(
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 408, in _request_wrapper
response = get_session().request(method=method, url=url, **params)
File "/root/miniconda3/lib/python3.10/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "/root/miniconda3/lib/python3.10/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 67, in send
return super().send(request, *args, **kwargs)
File "/root/miniconda3/lib/python3.10/site-packages/requests/adapters.py", line 507, in send
raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /BAAI/bge-reranker-v2-minicpm-layerwise/resolve/main/configuration_minicpm_reranker.py (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f7d21919b10>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 97f8b071-232d-486a-9a42-deba39c118cd)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/root/miniconda3/lib/python3.10/site-packages/transformers/utils/hub.py", line 398, in cached_file
resolved_file = hf_hub_download(
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
return fn(*args, **kwargs)
File "/root/miniconda3/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1406, in hf_hub_download
raise LocalEntryNotFoundError(
huggingface_hub.utils._errors.LocalEntryNotFoundError: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "", line 1, in
File "/root/miniconda3/lib/python3.10/site-packages/FlagEmbedding/flag_reranker.py", line 324, in init
self.model = AutoModelForCausalLM.from_pretrained(model_name_or_path,
File "/root/miniconda3/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py", line 521, in from_pretrained
config, kwargs = AutoConfig.from_pretrained(
File "/root/miniconda3/lib/python3.10/site-packages/transformers/models/auto/configuration_auto.py", line 1120, in from_pretrained
config_class = get_class_from_dynamic_module(
File "/root/miniconda3/lib/python3.10/site-packages/transformers/dynamic_module_utils.py", line 488, in get_class_from_dynamic_module
final_module = get_cached_module_file(
File "/root/miniconda3/lib/python3.10/site-packages/transformers/dynamic_module_utils.py", line 293, in get_cached_module_file
resolved_module_file = cached_file(
File "/root/miniconda3/lib/python3.10/site-packages/transformers/utils/hub.py", line 441, in cached_file
raise EnvironmentError(
OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like BAAI/bge-reranker-v2-minicpm-layerwise is not the path to a directory containing a file named configuration_minicpm_reranker.py.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.

我的本地模型文件目录的列表:

drwxr-xr-x 2 root root 4.0K Apr 6 19:57 .
drwxr-xr-x 6 root root 4.0K Apr 7 00:14 ..
-rw-r--r-- 1 root root 1.2K Apr 7 17:12 config.json
-rw-r--r-- 1 root root 9.8K Apr 7 17:12 configuration_minicpm_reranker.py
-rw-r--r-- 1 root root 140 Apr 7 17:12 generation_config.json
-rw-r--r-- 1 root root 4.7G Apr 7 19:36 model-00001-of-00003.safetensors
-rw-r--r-- 1 root root 4.7G Apr 7 19:34 model-00002-of-00003.safetensors
-rw-r--r-- 1 root root 892M Apr 7 19:38 model-00003-of-00003.safetensors
-rw-r--r-- 1 root root 71K Apr 7 17:12 modeling_minicpm_reranker.py
-rw-r--r-- 1 root root 32K Apr 7 17:13 model.safetensors.index.json
-rw-r--r-- 1 root root 17K Apr 7 19:37 README.md
-rw-r--r-- 1 root root 552 Apr 7 17:13 special_tokens_map.json
-rw-r--r-- 1 root root 1.2K Apr 7 17:13 tokenizer_config.json
-rw-r--r-- 1 root root 6.0M Apr 7 17:13 tokenizer.json
-rw-r--r-- 1 root root 2.0M Apr 7 19:37 tokenizer.model

urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x7f7d21919b10>, 'Connection to huggingface.co timed out. (connect timeout=10)')

是没连接到huggingface的原因,因为这里更改了模型,所以需要访问huggingface的文件

urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x7f7d21919b10>, 'Connection to huggingface.co timed out. (connect timeout=10)')

是没连接到huggingface的原因,因为这里更改了模型,所以需要访问huggingface的文件

麻烦在本地运行试一下,我这边也是不行的。提示ValueError: Loading /home/bge-reranker-v2-minicpm-layerwise requires you to execute the configuration file in that repo on your local machine.

你好,我这边也遇到了类似问题,直接运行本地的模型和代码报错,(服务器访问不了huggingface)
ValueError: Loading /home/bge-reranker-v2-minicpm-layerwise requires you to execute the configuration file in that repo on your local machine. Make sure you have read the code there to avoid malicious use, then set the option trust_remote_code=True to remove this error.

是不是就是因为没加 trust_remote_code=True 的原因?但LayerWiseFlagLLMReranker()不让加这个参数啊,加了就报错

嗯,加了的话需要能连到huggingface

trust_remote_code=True是用来确保可以连接到huggingface的自定义模型文件的
本地运行得能连接到huggingface才行
如果连接不到huggingface,那么需要git clone代码库进行更改,传入configuration_minicpm_reranker.py和modeling_minicpm_reranker.py,将模型加载部分AutoModelForCausalLM改为modeling_minicpm_reranker中的LayerWiseMiniCPMForCausalLM

本地运行也可以在加载的时候AutoModeForCausalLM.from_pretrained里加个参数local_files_only=True,这样也可以加载模型

本地运行也可以在加载的时候AutoModeForCausalLM.from_pretrained里加个参数local_files_only=True,这样也可以加载模型
还是不行,错误是一样的。

我又看了一下,可能和transformers的版本有关系,在\transformers\models\auto\configuration_auto.py中,这个模型的 "model_type": "minicpm",不在CONFIG_MAPPING_NAMES中,所以has_local_code=False。 transformers版本是4.38.1。

image.png

urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x7f7d21919b10>, 'Connection to huggingface.co timed out. (connect timeout=10)')

是没连接到huggingface的原因,因为这里更改了模型,所以需要访问huggingface的文件

能优化下吗?bge-m3和bge-reranker-large下载到本地后,都不用连huggingface

本地运行也可以在加载的时候AutoModeForCausalLM.from_pretrained里加个参数local_files_only=True,这样也可以加载模型
这个看起来不大好用

本地运行也可以在加载的时候AutoModeForCausalLM.from_pretrained里加个参数local_files_only=True,这样也可以加载模型
还是不行,错误是一样的。

我又看了一下,可能和transformers的版本有关系,在\transformers\models\auto\configuration_auto.py中,这个模型的 "model_type": "minicpm",不在CONFIG_MAPPING_NAMES中,所以has_local_code=False。 transformers版本是4.38.1。

image.png

这个如果手动添加config_mapping_names可以解决么

本地运行也可以在加载的时候AutoModeForCausalLM.from_pretrained里加个参数local_files_only=True,这样也可以加载模型
还是不行,错误是一样的。

我又看了一下,可能和transformers的版本有关系,在\transformers\models\auto\configuration_auto.py中,这个模型的 "model_type": "minicpm",不在CONFIG_MAPPING_NAMES中,所以has_local_code=False。 transformers版本是4.38.1。

image.png

这个如果手动添加config_mapping_names可以解决么

不知道怎么加啊。
问题已经解决了,升级了Python,原来是3.8,升级到了3.11。

也可以按照如下方法解决:

  1. 确保configuration_minicpm_reranker.pymodeling_minicpm_reranker.py已经保存到了模型路径中
  2. 修改路径中的config.json文件:
"auto_map": {
    "AutoConfig": "configuration_minicpm_reranker.LayerWiseMiniCPMConfig",
    "AutoModel": "modeling_minicpm_reranker.LayerWiseMiniCPMModel",
    "AutoModelForCausalLM": "modeling_minicpm_reranker.LayerWiseMiniCPMForCausalLM"
  },

也可以按照如下方法解决:

  1. 确保configuration_minicpm_reranker.pymodeling_minicpm_reranker.py已经保存到了模型路径中
  2. 修改路径中的config.json文件:
"auto_map": {
    "AutoConfig": "configuration_minicpm_reranker.LayerWiseMiniCPMConfig",
    "AutoModel": "modeling_minicpm_reranker.LayerWiseMiniCPMModel",
    "AutoModelForCausalLM": "modeling_minicpm_reranker.LayerWiseMiniCPMForCausalLM"
  },

非常感谢!!

Sign up or log in to comment