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

Experiment using autocast dtype in residual path of text transformer #433

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 5 additions & 3 deletions src/open_clip/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import torch
import torch.nn.functional as F
from torch import nn
from torch.utils.checkpoint import checkpoint

from .hf_model import HFTextEncoder
from .modified_resnet import ModifiedResNet
from .timm_model import TimmModel
from .transformer import LayerNormFp32, LayerNorm, QuickGELU, Attention, VisionTransformer, TextTransformer
from .transformer import LayerNormFp32, LayerNorm, QuickGELU, Attention, VisionTransformer, TextTransformer, \
to_autocast_dtype
from .utils import to_2tuple


Expand Down Expand Up @@ -217,12 +217,14 @@ def encode_text(self, text, normalize: bool = False):
cast_dtype = self.transformer.get_cast_dtype()

x = self.token_embedding(text).to(cast_dtype) # [batch_size, n_ctx, d_model]

x = x + self.positional_embedding.to(cast_dtype)
x = to_autocast_dtype(x)

x = x.permute(1, 0, 2) # NLD -> LND
x = self.transformer(x, attn_mask=self.attn_mask)
x = x.permute(1, 0, 2) # LND -> NLD
x = self.ln_final(x) # [batch_size, n_ctx, transformer.width]

# take features from the eot embedding (eot_token is the highest number in each sequence)
x = x[torch.arange(x.shape[0]), text.argmax(dim=-1)] @ self.text_projection
return F.normalize(x, dim=-1) if normalize else x
Expand Down
12 changes: 11 additions & 1 deletion src/open_clip/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def forward(self, x: torch.Tensor):
x = F.layer_norm(x.to(torch.float32), self.normalized_shape, self.weight, self.bias, self.eps)
return x.to(orig_type)


class LayerNorm(nn.LayerNorm):
"""Subclass torch's LayerNorm (with cast back to input dtype)."""

Expand Down Expand Up @@ -501,6 +500,15 @@ def forward(self, x: torch.Tensor):
return pooled


def to_autocast_dtype(x: torch.Tensor):
if x.device.type == 'cpu' and torch.is_autocast_cpu_enabled():
return x.to(torch.get_autocast_cpu_dtype())
elif torch.is_autocast_enabled():
return x.to(torch.get_autocast_gpu_dtype())
# NOTE this doesn't cover possible xpu / hpu + autocast use
return x


class TextTransformer(nn.Module):
output_tokens: torch.jit.Final[bool]

Expand Down Expand Up @@ -607,6 +615,8 @@ def forward(self, text):
attn_mask = attn_mask[None, :seq_len, :seq_len] + cls_mask[:, :seq_len, :seq_len]

x = x + self.positional_embedding[:seq_len].to(cast_dtype)
x = to_autocast_dtype(x)

x = x.permute(1, 0, 2) # NLD -> LND
x = self.transformer(x, attn_mask=attn_mask)
x = x.permute(1, 0, 2) # LND -> NLD
Expand Down