flash-attention/flash_attn/layers/patch_embed.py

68 lines
2.1 KiB
Python
Raw Normal View History

2022-11-18 08:56:06 +08:00
# We use the same API as https://github.com/rwightman/pytorch-image-models/blob/v0.6.11/timm/models/layers/patch_embed.py
# But we use nn.Linear instead of Conv2d and it's about 8x faster.
from functools import partial
import torch.nn as nn
2023-08-19 05:22:11 +08:00
from einops import rearrange
2022-11-18 08:56:06 +08:00
from torch import _assert
from torch.nn.modules.utils import _pair
try:
2022-12-23 11:21:12 +08:00
from flash_attn.ops.fused_dense import FusedDense
2022-11-18 08:56:06 +08:00
except ImportError:
2022-12-23 11:21:12 +08:00
FusedDense = None
2022-11-18 08:56:06 +08:00
class PatchEmbed(nn.Module):
2023-08-19 05:22:11 +08:00
"""2D Image to Patch Embedding"""
2022-11-18 08:56:06 +08:00
def __init__(
2023-08-19 05:22:11 +08:00
self,
img_size=224,
patch_size=16,
in_chans=3,
embed_dim=768,
norm_layer=None,
flatten=True,
bias=True,
fused_bias_fc=False,
2022-11-18 08:56:06 +08:00
):
super().__init__()
img_size = _pair(img_size)
patch_size = _pair(patch_size)
self.img_size = img_size
self.patch_size = patch_size
self.grid_size = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
self.num_patches = self.grid_size[0] * self.grid_size[1]
self.flatten = flatten
2022-12-23 11:21:12 +08:00
if fused_bias_fc and FusedDense is None:
2023-08-19 05:22:11 +08:00
raise ImportError("fused_dense is not installed")
2022-11-18 08:56:06 +08:00
2022-12-23 11:21:12 +08:00
linear_cls = nn.Linear if not fused_bias_fc or not bias else FusedDense
2022-11-18 08:56:06 +08:00
self.proj = linear_cls(in_chans * patch_size[0] * patch_size[1], embed_dim, bias=bias)
self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
def forward(self, x):
_, _, H, W = x.shape
2023-08-19 05:22:11 +08:00
_assert(
H == self.img_size[0],
f"Input image height ({H}) doesn't match model ({self.img_size[0]}).",
)
_assert(
W == self.img_size[1],
f"Input image width ({W}) doesn't match model ({self.img_size[1]}).",
)
x = self.proj(
rearrange(
x,
"b c (h p1) (w p2) -> b h w (c p1 p2)",
p1=self.patch_size[0],
p2=self.patch_size[1],
)
)
2022-11-18 08:56:06 +08:00
if self.flatten:
2023-08-19 05:22:11 +08:00
x = rearrange(x, "b h w c -> b (h w) c")
2022-11-18 08:56:06 +08:00
x = self.norm(x)
return x