Skip to content

Instantly share code, notes, and snippets.

@achalddave
Last active April 27, 2022 06:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achalddave/12e82c3c879589ee287e9c2769c489f0 to your computer and use it in GitHub Desktop.
Save achalddave/12e82c3c879589ee287e9c2769c489f0 to your computer and use it in GitHub Desktop.
Patch CLIP models to work with torch versions before 1.7
#!/bin/bash
###
# Patch CLIP models to work with PyTorch versions before 1.7.
# This script was tested with PyTorch 1.6 and CUDA 9.2 on the RN-50 and ViT-B-32 models.
###
if [[ "$#" != 1 ]] ; then
echo "Usage: $0 <clip-model-pt>"
echo "Example: $0 $HOME/.cache/clip/RN50.pt"
exit 1
fi
filename=${1##*/}
filename=${filename%.pt}
dir=${1%/*}
pushd ${dir}
echo "Unzipping model."
unzip ${filename}.pt >/dev/null
pushd ${filename}
echo "Patching calls."
for i in code/__torch__/torch/nn/modules/conv.py code/__torch__/torch/nn/modules/conv/*.py ; do
# Remove last True argument (corresponds to allow_tf32, which isn't in torch <1.7)
sed -i'' -e 's/torch._convolution(\(.*\), True)/torch._convolution(\1)/g' ${i}
done
for i in code/__torch__/multimodal/model/multimodal_transformer.py code/__torch__/multimodal/model/multimodal_transformer/*.py ; do
# pin_memory=None is not valid in torch <1.7; pin_memory=False seems to work.
sed -i'' -e 's/pin_memory=None/pin_memory=False/g' ${i}
done
popd
echo "Zipping back up."
zip -r ${filename}_fixed.pt ${filename} >/dev/null
echo "Patched CLIP model to below path:"
readlink -e ${filename}_fixed.pt
popd
@achalddave
Copy link
Author

When loading, pass jit=False to clip.load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment