Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load onnx checkpoint from huggingface repository #553

Closed
Whadup opened this issue Feb 7, 2024 · 6 comments · Fixed by #554
Closed

Cannot load onnx checkpoint from huggingface repository #553

Whadup opened this issue Feb 7, 2024 · 6 comments · Fixed by #554

Comments

@Whadup
Copy link

Whadup commented Feb 7, 2024

I am currently facing an issue while attempting to load an OVModelForFeatureExtraction from a pretrained checkpoint that is available as an ONNX file. The checkpoint is located in a subfolder within the Hugging Face repository.

I have tried to load the model using the following code snippet:

OVModelForFeatureExtraction.from_pretrained("intfloat/multilingual-e5-small", from_onnx=True, subfolder="onnx")

However, this approach fails with an error stating that model.bin is not available. It seems that the loading process does not consider the ONNX file (model.onnx) as it should. I have also attempted to explicitly set the file_name parameter as "model.onnx", but the issue persists.

What works: Interestingly, when I clone the Hugging Face repository and load the model from the local onnx folder path, everything works correctly.

Expected Behavior: I expect the from_pretrained method to successfully load ONNX files from Hugging Face repositories, allowing me to directly use the pretrained checkpoint without needing to clone the repository locally.

Traceback:

Traceback (most recent call last):
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/utils/_errors.py", line 286, in hf_raise_for_status
response.raise_for_status()
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://huggingface.co/intfloat/multilingual-e5-base/resolve/main/onnx/model.bin

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

Traceback (most recent call last):
File "", line 1, in
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/modeling_base.py", line 372, in from_pretrained
return from_pretrained_method(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/intel/openvino/modeling_base.py", line 200, in _from_pretrained
model_cache_path = cls._cached_file(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/intel/openvino/modeling_base.py", line 235, in _cached_file
model_cache_path = hf_hub_download(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
return fn(*args, **kwargs)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1238, in hf_hub_download
metadata = get_hf_file_metadata(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
return fn(*args, **kwargs)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1631, in get_hf_file_metadata
r = _request_wrapper(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 385, in _request_wrapper
response = _request_wrapper(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 409, in _request_wrapper
hf_raise_for_status(response)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/huggingface_hub/utils/_errors.py", line 296, in hf_raise_for_status
raise EntryNotFoundError(message, response) from e
huggingface_hub.utils._errors.EntryNotFoundError: 404 Client Error. (Request ID: Root=1-65c34656-3b4316eb46f2dba15f28965a;c97e9e58-d3fc-4756-ad45-d067b87a4576)

Entry Not Found for url: https://huggingface.co/intfloat/multilingual-e5-base/resolve/main/onnx/model.bin.

@echarlaix
Copy link
Collaborator

Hi @Whadup, thanks for reporting, this issue should be fixed in #554

@Whadup
Copy link
Author

Whadup commented Feb 7, 2024

Thanks for the quick fix. It works for "multilingual-e5-base", however it fails for "multilingual-e5-large", which has a different directory structure:
Rather than a single model.onnx, it has a model.onnx and a model.onnx_data, here's the link: https://huggingface.co/intfloat/multilingual-e5-large/tree/main/onnx

It now fails with a different error, though:

model = OVModelForFeatureExtraction.from_pretrained("intfloat/multilingual-e5-large", from_onnx=True, subfolder="onnx")
Traceback (most recent call last):
File "", line 1, in
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/modeling_base.py", line 372, in from_pretrained
return from_pretrained_method(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/intel/openvino/modeling_base.py", line 210, in _from_pretrained
model = cls.load_model(model_cache_path, load_in_8bit=load_in_8bit)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/intel/openvino/modeling_base.py", line 126, in load_model
model = core.read_model(file_name) if not file_name.suffix == ".onnx" else convert_model(file_name)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/tools/ovc/convert.py", line 101, in convert_model
ov_model, _ = _convert(cli_parser, params, True)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/tools/ovc/convert_impl.py", line 534, in _convert
raise e
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/tools/ovc/convert_impl.py", line 476, in _convert
ov_model = driver(argv, {"conversion_parameters": non_default_params})
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/tools/ovc/convert_impl.py", line 227, in driver
ov_model = moc_emit_ir(prepare_ir(argv), argv)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/tools/ovc/convert_impl.py", line 177, in prepare_ir
ov_model = moc_pipeline(argv, moc_front_end)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/tools/ovc/moc_frontend/pipeline.py", line 244, in moc_pipeline
ov_model = moc_front_end.convert(input_model)
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/openvino/frontend/frontend.py", line 18, in convert
converted_model = super().convert(model)
RuntimeError: invalid external data: ExternalDataInfo(data_full_path: model.onnx_data, offset: 0, data_length: 1024008192)

@echarlaix
Copy link
Collaborator

Oh yes this will be an issue for model for model >2GB, could you use instead :

model = OVModelForFeatureExtraction.from_pretrained("intfloat/multilingual-e5-large", export=True)

@Whadup
Copy link
Author

Whadup commented Feb 8, 2024

Unfortunately that fails

Framework not specified. Using pt to export to ONNX.
Using the export variant default. Available variants are:
- default: The default ONNX variant.
Traceback (most recent call last):
File "", line 1, in
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/modeling_base.py", line 372, in from_pretrained
return from_pretrained_method(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/intel/openvino/modeling_base.py", line 329, in _from_transformers
main_export(
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/optimum/exporters/openvino/main.py", line 304, in main_export
num_parameters = model.num_parameters() if not is_stable_diffusion else model.unet.num_parameters()
File "/home/azureuser/code/genai-ml/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1695, in getattr
raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")
AttributeError: 'SentenceTransformer' object has no attribute 'num_parameters'. Did you mean: '_parameters'?

I was thinking something along the lines of

    @staticmethod
    def _cached_file(
        model_path: Union[Path, str],
        use_auth_token: Optional[Union[bool, str]] = None,
        revision: Optional[str] = None,
        force_download: bool = False,
        cache_dir: Optional[str] = None,
        file_name: Optional[str] = None,
        subfolder: str = "",
        local_files_only: bool = False,
    ):
        # locates a file in a local folder and repo, downloads and cache it if necessary.
        model_path = Path(model_path)
        if model_path.is_dir():
            model_cache_path = model_path / file_name
        else:
            model_cache_path = snapshot_download(
                repo_id=model_path.as_posix(),
                allow_patterns=None if subfolder is None else subfolder+"/**",
                use_auth_token=use_auth_token,
                revision=revision,
                cache_dir=cache_dir,
                force_download=force_download,
                local_files_only=local_files_only,
                # local_dir="/tmp/openvinoconverter/",
                # local_dir_use_symlinks="auto"
            )
            if subfolder is not None:
                model_cache_path = os.path.join(model_cache_path, subfolder)
            model_cache_path = os.path.join(model_cache_path, file_name)
            model_cache_path = Path(model_cache_path)

        return model_cache_path

might solve the issue. At least it downloads all the right files. However, I suspect that there is another bug in openvino itself that prevents this from working unless I use this localdir="/tmp/openvinoconverter" line because of all the sym links. openvinotoolkit/openvino#22736

I'm not sure how to sensibly set this to a local directory, I'd much rather work only on the symlinks to the huggingface cache.

@echarlaix
Copy link
Collaborator

The OpenVINO export for sentence transformers models should be fixed in optimum-intel v1.15.0, could you try :

model = OVModelForFeatureExtraction.from_pretrained("intfloat/multilingual-e5-large", export=True)

@Whadup
Copy link
Author

Whadup commented Feb 22, 2024

nice! now it works, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants