[GH-ISSUE #206] NotImplementedError when clip.load #180

Closed
opened 2026-10-31 21:12:04 +00:00 by bsakaguchi · 9 comments
Owner

Originally created by @DtYXs on GitHub (Jan 6, 2022).
Original GitHub issue: https://github.com/openai/CLIP/issues/206

When running model, preprocess = clip.load("ViT-B/32", device=device)

D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\serialization.py:591: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning)
" silence this warning)", UserWarning)
Traceback (most recent call last):
File "D:\python\helpers\pydev\pydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:\models.py", line 191, in
model = CLIP(cfg)
File "D:\models.py", line 173, in init
self.model, self.preprocess = clip.load("ViT-B/32", device=device)
File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\clip\clip.py", line 135, in load
model = build_model(state_dict or model.state_dict()).to(device)
File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\clip\model.py", line 396, in build_model
vit = "visual.proj" in state_dict
File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\jit_script.py", line 624, in contains
return self.forward_magic_method("contains", key)
File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\jit_script.py", line 611, in forward_magic_method
raise NotImplementedError()
NotImplementedError

How do I solve this problem?

Originally created by @DtYXs on GitHub (Jan 6, 2022). Original GitHub issue: https://github.com/openai/CLIP/issues/206 When running _model, preprocess = clip.load("ViT-B/32", device=device)_ D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\serialization.py:591: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning) " silence this warning)", UserWarning) Traceback (most recent call last): File "D:\python\helpers\pydev\pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "D:\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:\models.py", line 191, in <module> model = CLIP(cfg) File "D:\models.py", line 173, in __init__ self.model, self.preprocess = clip.load("ViT-B/32", device=device) File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\clip\clip.py", line 135, in load model = build_model(state_dict or model.state_dict()).to(device) File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\clip\model.py", line 396, in build_model vit = "visual.proj" in state_dict File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\jit\_script.py", line 624, in __contains__ return self.forward_magic_method("__contains__", key) File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\jit\_script.py", line 611, in forward_magic_method raise NotImplementedError() NotImplementedError How do I solve this problem?
Author
Owner

@haofanwang commented on GitHub (Jan 6, 2022):

Try model, preprocess = clip.load("ViT-B/32", device=device, jit=False)

<!-- gh-comment-id:1006508248 --> @haofanwang commented on GitHub (Jan 6, 2022): Try `model, preprocess = clip.load("ViT-B/32", device=device, jit=False)`
Author
Owner

@DtYXs commented on GitHub (Jan 6, 2022):

Try model, preprocess = clip.load("ViT-B/32", device=device, jit=False)

The problem still occurs. Beacuse jit defaults to Fasle.

It seems that the state_dict in vit = "visual.proj" in state_dict is torch.jit._script.RecursiveScriptModule class,
and when call "visual.proj" in state_dict, the error occurred.

My environment and version are executed according to the commands in the document. But it is in Windows. Does it matter?

<!-- gh-comment-id:1006519340 --> @DtYXs commented on GitHub (Jan 6, 2022): > Try `model, preprocess = clip.load("ViT-B/32", device=device, jit=False)` The problem still occurs. Beacuse `jit` defaults to `Fasle`. It seems that the `state_dict` in `vit = "visual.proj" in state_dict` is `torch.jit._script.RecursiveScriptModule` class, and when call `"visual.proj" in state_dict`, the error occurred. My environment and version are executed according to the commands in the document. But it is in Windows. Does it matter?
Author
Owner

@haofanwang commented on GitHub (Jan 6, 2022):

github.com/openai/CLIP@573315e83f/clip/clip.py (L125)

It seems that this line fails, a runtime exception is raised. Could you move it before try, do you still meet the same issue?

<!-- gh-comment-id:1006649225 --> @haofanwang commented on GitHub (Jan 6, 2022): https://github.com/openai/CLIP/blob/573315e83f07b53a61ff5098757e8fc885f1703e/clip/clip.py#L125 It seems that this line fails, a runtime exception is raised. Could you move it before `try`, do you still meet the same issue?
Author
Owner

@DtYXs commented on GitHub (Jan 6, 2022):

github.com/openai/CLIP@573315e83f/clip/clip.py (L125)

It seems that this line fails, a runtime exception is raised. Could you move it before try, do you still meet the same issue?

Thank you very much for your reply. I move it, and

Traceback (most recent call last):
File "D:\python\helpers\pydev\pydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/models.py", line 172, in init
self.model, self.preprocess = clip.load("ViT-B/32", device=self.cfg.device, jit=False)
File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\clip\clip.py", line 122, in load
model = torch.jit.load(model_path, map_location=device if jit else "cpu").eval()
File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\jit_serialization.py", line 161, in load
cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files)
RuntimeError: open file failed because of errno 42 on fopen: Illegal byte sequence, file path: C:\Users\Administrator/.cache/clip\ViT-B-32.pt
python-BaseException

I think the problem comes from my CUDA version is 11.1.
Since pytorch seems to have no cudatookit for 11.1 (I tried conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.1 but PackagesNotFoundError: The following packages are not available from current channels: cudatoolkit=11.1). Then I tried conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.0 and conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.3 two environments. Each environment has problems.

<!-- gh-comment-id:1006679234 --> @DtYXs commented on GitHub (Jan 6, 2022): > https://github.com/openai/CLIP/blob/573315e83f07b53a61ff5098757e8fc885f1703e/clip/clip.py#L125 > > It seems that this line fails, a runtime exception is raised. Could you move it before `try`, do you still meet the same issue? Thank you very much for your reply. I move it, and Traceback (most recent call last): File "D:\python\helpers\pydev\pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "D:\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/models.py", line 172, in __init__ self.model, self.preprocess = clip.load("ViT-B/32", device=self.cfg.device, jit=False) File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\clip\clip.py", line 122, in load model = torch.jit.load(model_path, map_location=device if jit else "cpu").eval() File "D:\Anaconda\anaconda3\envs\A\lib\site-packages\torch\jit\_serialization.py", line 161, in load cpp_module = torch._C.import_ir_module(cu, str(f), map_location, _extra_files) RuntimeError: open file failed because of errno 42 on fopen: Illegal byte sequence, file path: C:\Users\Administrator/.cache/clip\ViT-B-32.pt python-BaseException I think the problem comes from my CUDA version is `11.1`. Since pytorch seems to have no cudatookit for `11.1` (I tried `conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.1` but `PackagesNotFoundError: The following packages are not available from current channels: cudatoolkit=11.1`). Then I tried `conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.0` and `conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.3` two environments. Each environment has problems.
Author
Owner

@ZZZ9116 commented on GitHub (Mar 1, 2022):

Hi, I have got the same problem. Did you solve it?

<!-- gh-comment-id:1055122309 --> @ZZZ9116 commented on GitHub (Mar 1, 2022): Hi, I have got the same problem. Did you solve it?
Author
Owner

@DtYXs commented on GitHub (Mar 1, 2022):

Hi, I have got the same problem. Did you solve it?

Actually not, I still cannot run it in my Windows system Cuda11.1 Pytorch1.7.1.
But I can run it in the other server Linux Ubuntu18.04.5 Cuda11.1 Python3.7.9 Pytorch1.9.1.

<!-- gh-comment-id:1055409881 --> @DtYXs commented on GitHub (Mar 1, 2022): > Hi, I have got the same problem. Did you solve it? Actually not, I still cannot run it in my Windows system Cuda11.1 Pytorch1.7.1. But I can run it in the other server Linux Ubuntu18.04.5 Cuda11.1 Python3.7.9 Pytorch1.9.1.
Author
Owner

@rohaldb commented on GitHub (Mar 15, 2022):

I am also having this issue - any update?

<!-- gh-comment-id:1067846353 --> @rohaldb commented on GitHub (Mar 15, 2022): I am also having this issue - any update?
Author
Owner

@liuxingbaoyu commented on GitHub (Apr 4, 2022):

Maybe because the windows username contains non-ascii characters, this is a pytorch bug

Modify clip.py

github.com/openai/CLIP@40f5484c1c/clip/clip.py (L125-L134)

    with open(model_path, 'rb') as opened_file:
        try:
            # loading JIT archive
            model = torch.jit.load(opened_file, map_location=device if jit else "cpu").eval()
            state_dict = None
        except RuntimeError as err:
            # loading saved state dict
            if jit:
                warnings.warn(f"File {model_path} is not a JIT archive. Loading as a state dict instead")
                jit = False
            state_dict = torch.load(opened_file, map_location="cpu")
<!-- gh-comment-id:1087091435 --> @liuxingbaoyu commented on GitHub (Apr 4, 2022): Maybe because the windows username contains non-ascii characters, this is a pytorch bug Modify clip.py https://github.com/openai/CLIP/blob/40f5484c1c74edd83cb9cf687c6ab92b28d8b656/clip/clip.py#L125-L134 ```py with open(model_path, 'rb') as opened_file: try: # loading JIT archive model = torch.jit.load(opened_file, map_location=device if jit else "cpu").eval() state_dict = None except RuntimeError as err: # loading saved state dict if jit: warnings.warn(f"File {model_path} is not a JIT archive. Loading as a state dict instead") jit = False state_dict = torch.load(opened_file, map_location="cpu") ```
Author
Owner

@samuela commented on GitHub (Feb 22, 2023):

Same issue here with Ubuntu and CUDA 11.1 even after https://github.com/openai/CLIP/pull/227.

Update: it was a GPU driver issue. torch.cuda.is_available() was false, so device="cuda" failed.

<!-- gh-comment-id:1440474478 --> @samuela commented on GitHub (Feb 22, 2023): ~Same issue here with Ubuntu and CUDA 11.1 even after https://github.com/openai/CLIP/pull/227.~ Update: it was a GPU driver issue. `torch.cuda.is_available()` was false, so `device="cuda"` failed.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#180
No description provided.