Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: 3.11
- name: Install
run: |
python3 -m venv .env
Expand All @@ -31,14 +31,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
model: ['openai_clip', 'open_clip', 'hf_clip', 'nm', 'ja_clip']
model: ['openai_clip', 'open_clip', 'hf_clip']

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.10
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.11'
- name: Install
run: |
python3 -m venv .env
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions-ecosystem/action-regex-match@v2
id: regex-match
with:
text: ${{ github.event.head_commit.message }}
regex: '^Release ([^ ]+)'
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: '3.8'
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion all_clip/deepsparse_clip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" https://github.com/neuralmagic/deepsparse/tree/bc4ffd305ac52718cc755430a1c44eb48739cfb4/src/deepsparse/clip """
"""https://github.com/neuralmagic/deepsparse/tree/bc4ffd305ac52718cc755430a1c44eb48739cfb4/src/deepsparse/clip"""

from torch import nn
import torch
Expand Down
2 changes: 1 addition & 1 deletion all_clip/hf_clip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" https://huggingface.co/docs/transformers/model_doc/clip """
"""https://huggingface.co/docs/transformers/model_doc/clip"""

import torch
from torch import autocast, nn
Expand Down
1 change: 1 addition & 0 deletions all_clip/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""load clip"""

from functools import lru_cache
import torch
from PIL import Image
Expand Down
2 changes: 1 addition & 1 deletion all_clip/open_clip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" https://github.com/mlfoundations/open_clip """
"""https://github.com/mlfoundations/open_clip"""

from torch import autocast, nn
import torch
Expand Down
44 changes: 42 additions & 2 deletions all_clip/openai_clip.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
""" https://github.com/openai/CLIP by https://github.com/rom1504/clip for pypi packaging """
"""https://github.com/openai/CLIP by https://github.com/rom1504/clip for pypi packaging"""

import clip
import torch
import warnings


def _parse_version(version_string):
"""Simple version parsing for major.minor.patch format"""
try:
parts = version_string.split(".")
return tuple(int(part.split("+")[0]) for part in parts[:3]) # Handle versions like "2.8.0+cu121"
except (ValueError, IndexError):
return (0, 0, 0) # Fallback for unparseable versions


def load_openai_clip(clip_model, use_jit, device, clip_cache_path):
model, preprocess = clip.load(clip_model, device=device, jit=use_jit, download_root=clip_cache_path)
"""Load OpenAI CLIP model with PyTorch 2.8+ compatibility fixes."""
# PyTorch 2.8+ compatibility: automatically disable JIT for OpenAI CLIP models
# to avoid TorchScript NotImplementedError issues
pytorch_version = _parse_version(torch.__version__)
if pytorch_version >= (2, 8, 0) and use_jit:
warnings.warn(
f"PyTorch {torch.__version__} detected. Disabling JIT compilation (use_jit=False) "
"for OpenAI CLIP models to avoid TorchScript compatibility issues. "
"To suppress this warning, explicitly set use_jit=False.",
UserWarning,
stacklevel=2,
)
use_jit = False

# Temporarily patch torch.load to handle weights_only parameter for CLIP model loading
original_torch_load = torch.load

def _patched_load(*args, **kwargs):
# Force weights_only=False for CLIP model compatibility with TorchScript archives
if "weights_only" not in kwargs:
kwargs["weights_only"] = False
return original_torch_load(*args, **kwargs)

try:
# Apply the patch only during CLIP model loading
torch.load = _patched_load
model, preprocess = clip.load(clip_model, device=device, jit=use_jit, download_root=clip_cache_path)
finally:
# Always restore the original torch.load
torch.load = original_torch_load

def tokenizer(t):
return clip.tokenize(t, truncate=True)
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Global options:

[mypy]
python_version = 3.8
python_version = 3.10
ignore_missing_imports = True
14 changes: 7 additions & 7 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
black==23.12.1
mypy==1.8.0
pylint==3.0.3
pytest-cov==4.1.0
pytest-xdist==3.5.0
pytest==7.4.4
deepsparse-nightly[clip]
black==25.1.0
mypy==1.17.1
pylint==3.3.8
pytest-cov==6.2.1
pytest-xdist==3.7.0
pytest==8.4.1
# deepsparse-nightly[clip] # Not compatible with Python 3.10+
git+https://github.com/rinnakk/japanese-clip.git
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"openai_clip:ViT-B/32",
"open_clip:ViT-B-32/laion2b_s34b_b79k",
"hf_clip:patrickjohncyh/fashion-clip",
"nm:mgoin/CLIP-ViT-B-32-laion2b_s34b_b79k-ds",
"ja_clip:rinna/japanese-clip-vit-b-16",
# "nm:mgoin/CLIP-ViT-B-32-laion2b_s34b_b79k-ds", # deepsparse not compatible with Python 3.10+
# "ja_clip:rinna/japanese-clip-vit-b-16", # japanese-clip has transformers compatibility issues with v4.55+
],
)
def test_load_clip(model):
Expand Down