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

Implement Locking of Text Tower for CLIP Models #523

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/open_clip/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .modified_resnet import ModifiedResNet
from .timm_model import TimmModel
from .transformer import LayerNormFp32, LayerNorm, QuickGELU, Attention, VisionTransformer, TextTransformer,\
text_global_pool
text_global_pool, lock_text_transformer
from .utils import to_2tuple


Expand Down Expand Up @@ -257,6 +257,9 @@ def lock_image_tower(self, unlocked_groups=0, freeze_bn_stats=False):
# lock image tower as per LiT - https://arxiv.org/abs/2111.07991
self.visual.lock(unlocked_groups=unlocked_groups, freeze_bn_stats=freeze_bn_stats)

def lock_text_tower(self, unlocked_layers: int = 0, freeze_layer_norm: bool = True):
lock_text_transformer(self, unlocked_layers, freeze_layer_norm)

@torch.jit.ignore
def set_grad_checkpointing(self, enable=True):
self.visual.set_grad_checkpointing(enable)
Expand Down
39 changes: 39 additions & 0 deletions src/open_clip/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,9 @@ def __init__(

self.init_parameters()

def lock(self, unlocked_layers: int = 0, freeze_layer_norm: bool = True):
lock_text_transformer(self, unlocked_layers, freeze_layer_norm)

def init_parameters(self):
nn.init.normal_(self.token_embedding.weight, std=0.02)
nn.init.normal_(self.positional_embedding, std=0.01)
Expand Down Expand Up @@ -802,6 +805,42 @@ def forward(self, text):
return pooled


def lock_text_transformer(
transformer: TextTransformer, unlocked_groups: int = 0, freeze_layer_norm: bool = True
):
for param in transformer.parameters():
param.requires_grad = False

if unlocked_groups != 0:
groups = [
[transformer.token_embedding, transformer.positional_embedding],
*transformer.transformer.resblocks[:-1],
[transformer.transformer.resblocks[-1], transformer.ln_final],
transformer.text_projection,
]

def _unlock(x):
ln_status = False if freeze_layer_norm else True
if isinstance(x, Sequence):
for g in x:
_unlock(g)
else:
if isinstance(x, torch.nn.Parameter):
x.requires_grad = True
elif isinstance(x, torch.nn.LayerNorm):
for p in x.parameters():
p.requires_grad = ln_status
else:
for n,p in x.named_parameters():
# This should grab LayerNorm inside `ResidualAttentionBlock` blocks
if n.startswith("ln_"):
p.requires_grad = ln_status
else:
p.requires_grad = True

_unlock(groups[-unlocked_groups:])


class MultimodalTransformer(Transformer):
def __init__(
self,
Expand Down
Loading