这个问题本来在宿主机里面是没有这个问题的,但是在容器里面就是出现了问题,我跟我的同学花了好几天解决这个问题,我们到最后还动了transformer的代码,哎,后来还是自己的老师给出了一个idea。

本来我是采用将宿主机的目录挂载到docker容器的方法,对于其他文件都是OK的,比如数据集什么的,但是就是这个huggingface出了点问题,一直显示报错:
 

OSError: We couldn't connect to 'https://huggingface.co' to load the files, and couldn't find them in the cached files.
Check your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.

这个huggingface是非访问不可吗?

后来,我们我们直接吧本地的huggingface的缓存目录(/mnt/nvme/abc/huggingface/hub)直接挂载到容器的缓存目录(/root/.cache/huggingface/hub)下了,然后就成功了。

具体的原因我们猜测是:因为容器里面默认要从/root/.cache/huggingface/hub这个路径里面查找对应的文件,如果找不到,就去huggingface里面下载,但是huggingface的网站又访问不了,然后就很麻烦,当然这里应该也能更改容器里面的huggingface的路径,但是这里我就不花时间去做实验了,感兴趣的小伙伴们可以试一试。

这个是我们的加载模型的代码,那个model.safetensors好像需要手动加载一下下。

os.environ['HF_HUB_OFFLINE'] = '1'
os.environ['TRANSFORMERS_OFFLINE'] = '1'
# Load with standard architecture (will have random weights initially)

print("model_path:", model_path)  # jina-colbert-v2
model_path = "/root/.cache/huggingface/hub/models--jinaai--jina-colbert-v2/snapshots/4552c4dc1ffd7d7a635b6a41a1077fe9c9cdd974"
rag_model = RAGPretrainedModel.from_pretrained(model_path)
    
# Now manually load and map the weights from safetensors
print("Loading custom weights from safetensors...")
safetensors_path = os.path.join(model_path, 'model.safetensors')
model_state_dict = rag_model.model.inference_ckpt.state_dict()

最后,其实huggingface下载下来的其实可以用tree models--jinaai--jina-colbert-v2 命令查看一下长这样:

models--jinaai--jina-colbert-v2
├── blobs
│   ├── 12890109702d6a6122ec6842b2a2738d50d73136
│   ├── 29e53ecdc07aba92d6b370e23f95512e4f60b596
│   ├── 60aeacc24f3d75c34628dfee5b7fed4bcce9a55def52b49f62a0b09d65a44b0b
│   ├── 741387d37db6027ada13c4705b8bb32719a09dc71873f54814d1182cd8943806
│   ├── 820c249b3c0306f2168bc9a54aeed83ae2077e67
│   └── eca28d0f26edfee29f7230ee18f276de9e88499c
├── refs
│   └── main
└── snapshots
    └── 4552c4dc1ffd7d7a635b6a41a1077fe9c9cdd974
        ├── artifact.metadata -> ../../blobs/29e53ecdc07aba92d6b370e23f95512e4f60b596
        ├── config.json -> ../../blobs/12890109702d6a6122ec6842b2a2738d50d73136
        ├── model.safetensors -> ../../blobs/741387d37db6027ada13c4705b8bb32719a09dc71873f54814d1182cd8943806
        ├── special_tokens_map.json -> ../../blobs/eca28d0f26edfee29f7230ee18f276de9e88499c
        ├── tokenizer_config.json -> ../../blobs/820c249b3c0306f2168bc9a54aeed83ae2077e67
        └── tokenizer.json -> ../../blobs/60aeacc24f3d75c34628dfee5b7fed4bcce9a55def52b49f62a0b09d65a44b0b

5 directories, 13 files

但是我本来想把这些文件提取出来,使用tree jina-colbert-v2查看目录,直接挂载到容器里面的,但是就是报错OSError。

jina-colbert-v2
├── artifact.metadata
├── config.json
├── configuration_xlm_roberta.py
├── mlp.py
├── modeling_xlm_roberta.py
├── model.safetensors
├── onnx
│   ├── model.onnx
│   └── model.onnx_data
├── pytorch_model.bin
├── README.md
├── special_tokens_map.json
├── stochastic_depth.py
├── tokenizer_config.json
└── tokenizer.json

2 directories, 14 files

具体的原理,我就不太清楚了,这个需要阅读transformer加载模型的代码,比较耗时。

当遇到这个问题的时候

ValueError: The state dictionary of the model you are trying to load is corrupted. Are you sure it was properly saved?

先不要怀疑是jina-colbert-v2权重的不完整,而是要下载另一个权重哈

models--jinaai--xlm-roberta-flash-implementation

更多推荐