Initial EVA-CLIP HF upload
Browse files- __init__.py +6 -0
- __pycache__/__init__.cpython-310.pyc +0 -0
- __pycache__/configuration_eva_clip.cpython-310.pyc +0 -0
- __pycache__/legacy_eva_vit.cpython-310.pyc +0 -0
- __pycache__/modeling_eva_clip.cpython-310.pyc +0 -0
- config.json +38 -0
- configuration_eva_clip.py +61 -0
- legacy_eva_clip/__init__.py +0 -0
- legacy_eva_clip/eva_clip_encoder.py +75 -0
- legacy_eva_clip/eva_clip_processors.py +74 -0
- legacy_eva_clip/eva_vit.py +815 -0
- legacy_eva_clip/factory.py +59 -0
- legacy_eva_clip/model_configs/EVA-CLIP-18B.json +27 -0
- legacy_eva_clip/model_configs/EVA-CLIP-8B-plus.json +27 -0
- legacy_eva_clip/model_configs/EVA-CLIP-8B.json +27 -0
- legacy_eva_clip/model_configs/EVA01-CLIP-B-16.json +19 -0
- legacy_eva_clip/model_configs/EVA01-CLIP-g-14-plus.json +24 -0
- legacy_eva_clip/model_configs/EVA01-CLIP-g-14.json +24 -0
- legacy_eva_clip/model_configs/EVA02-CLIP-B-16.json +29 -0
- legacy_eva_clip/model_configs/EVA02-CLIP-L-14-336.json +29 -0
- legacy_eva_clip/model_configs/EVA02-CLIP-L-14.json +29 -0
- legacy_eva_clip/model_configs/EVA02-CLIP-bigE-14-plus.json +28 -0
- legacy_eva_clip/model_configs/EVA02-CLIP-bigE-14.json +25 -0
- legacy_eva_clip/model_configs/Internal-EVA02-CLIP-10B-14-448.json +25 -0
- legacy_eva_clip/model_configs/Internal-EVA02-CLIP-10B-14.json +25 -0
- legacy_eva_clip/model_configs/eva-clip-E-14-plus.json +27 -0
- legacy_eva_clip/visual.py +450 -0
- legacy_eva_vit.py +815 -0
- model-00001-of-00009.safetensors +3 -0
- model-00002-of-00009.safetensors +3 -0
- model-00003-of-00009.safetensors +3 -0
- model-00004-of-00009.safetensors +3 -0
- model-00005-of-00009.safetensors +3 -0
- model-00006-of-00009.safetensors +3 -0
- model-00007-of-00009.safetensors +3 -0
- model-00008-of-00009.safetensors +3 -0
- model-00009-of-00009.safetensors +3 -0
- model.safetensors.index.json +779 -0
- modeling_eva_clip.py +119 -0
__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .configuration_eva_clip import EvaClipVisionConfig
|
| 2 |
+
from .modeling_eva_clip import EvaClipVisionModel
|
| 3 |
+
|
| 4 |
+
__all__ = ["EvaClipVisionConfig", "EvaClipVisionModel"]
|
| 5 |
+
|
| 6 |
+
|
__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (313 Bytes). View file
|
|
|
__pycache__/configuration_eva_clip.cpython-310.pyc
ADDED
|
Binary file (1.88 kB). View file
|
|
|
__pycache__/legacy_eva_vit.cpython-310.pyc
ADDED
|
Binary file (25.8 kB). View file
|
|
|
__pycache__/modeling_eva_clip.cpython-310.pyc
ADDED
|
Binary file (4.7 kB). View file
|
|
|
config.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "eva-clip-E-14-plus",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"EvaClipVisionModel"
|
| 5 |
+
],
|
| 6 |
+
"auto_map": {
|
| 7 |
+
"AutoConfig": "configuration_eva_clip.EvaClipVisionConfig",
|
| 8 |
+
"AutoModel": "modeling_eva_clip.EvaClipVisionModel"
|
| 9 |
+
},
|
| 10 |
+
"model_type": "eva-clip-vision",
|
| 11 |
+
"vision_tower": "eva-clip-E-14-plus",
|
| 12 |
+
"embed_dim": 1024,
|
| 13 |
+
"vision_cfg": {
|
| 14 |
+
"eva_model_name": "eva-clip-E-14-plus",
|
| 15 |
+
"image_size": 448,
|
| 16 |
+
"patch_size": 14,
|
| 17 |
+
"width": 1792,
|
| 18 |
+
"layers": 64,
|
| 19 |
+
"mlp_ratio": 8.571428571428571,
|
| 20 |
+
"head_width": 112,
|
| 21 |
+
"drop_path_rate": 0.0,
|
| 22 |
+
"qkv_bias": true,
|
| 23 |
+
"xattn": true,
|
| 24 |
+
"postnorm": true,
|
| 25 |
+
"global_average_pool": false,
|
| 26 |
+
"patch_dropout": 0.0,
|
| 27 |
+
"rope": false,
|
| 28 |
+
"pt_hw_seq_len": 32,
|
| 29 |
+
"intp_freq": false,
|
| 30 |
+
"naiveswiglu": false,
|
| 31 |
+
"subln": false,
|
| 32 |
+
"fusedLN": false
|
| 33 |
+
},
|
| 34 |
+
"torch_dtype": "bfloat16",
|
| 35 |
+
"transformers_version": "4.44.0"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
configuration_eva_clip.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from copy import deepcopy
|
| 4 |
+
from typing import Any, Dict, Optional
|
| 5 |
+
|
| 6 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 7 |
+
|
| 8 |
+
DEFAULT_MODEL_NAME = "eva-clip-E-14-plus"
|
| 9 |
+
|
| 10 |
+
DEFAULT_VISION_CFG: Dict[str, Any] = {
|
| 11 |
+
"eva_model_name": DEFAULT_MODEL_NAME,
|
| 12 |
+
"image_size": 448,
|
| 13 |
+
"patch_size": 14,
|
| 14 |
+
"width": 1792,
|
| 15 |
+
"layers": 64,
|
| 16 |
+
"mlp_ratio": 8.571428571428571,
|
| 17 |
+
"head_width": 112,
|
| 18 |
+
"drop_path_rate": 0.0,
|
| 19 |
+
"qkv_bias": True,
|
| 20 |
+
"xattn": True,
|
| 21 |
+
"postnorm": True,
|
| 22 |
+
"global_average_pool": False,
|
| 23 |
+
"patch_dropout": 0.0,
|
| 24 |
+
"rope": False,
|
| 25 |
+
"pt_hw_seq_len": 32,
|
| 26 |
+
"intp_freq": False,
|
| 27 |
+
"naiveswiglu": False,
|
| 28 |
+
"subln": False,
|
| 29 |
+
"fusedLN": False,
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class EvaClipVisionConfig(PretrainedConfig):
|
| 34 |
+
model_type = "eva-clip-vision"
|
| 35 |
+
|
| 36 |
+
def __init__(
|
| 37 |
+
self,
|
| 38 |
+
*,
|
| 39 |
+
vision_tower: str = DEFAULT_MODEL_NAME,
|
| 40 |
+
embed_dim: int = 1024,
|
| 41 |
+
vision_cfg: Optional[Dict[str, Any]] = None,
|
| 42 |
+
vision_tower_pretrained: Optional[str] = None,
|
| 43 |
+
projection_dim: Optional[int] = None,
|
| 44 |
+
**kwargs: Any,
|
| 45 |
+
) -> None:
|
| 46 |
+
super().__init__(**kwargs)
|
| 47 |
+
|
| 48 |
+
cfg = deepcopy(DEFAULT_VISION_CFG)
|
| 49 |
+
if vision_cfg is not None:
|
| 50 |
+
cfg.update(vision_cfg)
|
| 51 |
+
|
| 52 |
+
self.vision_tower = vision_tower
|
| 53 |
+
self.embed_dim = embed_dim
|
| 54 |
+
self.vision_cfg = cfg
|
| 55 |
+
self.vision_tower_pretrained = vision_tower_pretrained
|
| 56 |
+
self.projection_dim = projection_dim if projection_dim is not None else embed_dim
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
__all__ = ["EvaClipVisionConfig", "DEFAULT_VISION_CFG", "DEFAULT_MODEL_NAME"]
|
| 60 |
+
|
| 61 |
+
|
legacy_eva_clip/__init__.py
ADDED
|
File without changes
|
legacy_eva_clip/eva_clip_encoder.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
from .eva_clip_processors import EvaClipImageTrainProcessor
|
| 5 |
+
from .eva_vit import EVAEncoderWrapper
|
| 6 |
+
from .factory import list_models, add_model_config, get_model_config
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class EvaClipVisionTower(nn.Module):
|
| 11 |
+
def __init__(self, vision_tower, args, delay_load=False):
|
| 12 |
+
super().__init__()
|
| 13 |
+
|
| 14 |
+
self.is_loaded = False
|
| 15 |
+
self.vision_tower_name = vision_tower
|
| 16 |
+
self.vision_tower_pretrained = args.vision_tower_pretrained
|
| 17 |
+
self.config = get_model_config(vision_tower)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
if not delay_load:
|
| 21 |
+
print(f"Loading EVA ViT: {self.vision_tower_name}")
|
| 22 |
+
self.load_model()
|
| 23 |
+
elif getattr(args, "unfreeze_mm_vision_tower", False):
|
| 24 |
+
# TODO: better detector is needed.
|
| 25 |
+
print(f"The checkpoint seems to contain `vision_tower` weights: `unfreeze_mm_vision_tower`: True.")
|
| 26 |
+
self.load_model()
|
| 27 |
+
elif hasattr(args, "mm_tunable_parts") and "mm_vision_tower" in args.mm_tunable_parts:
|
| 28 |
+
print(f"The checkpoint seems to contain `vision_tower` weights: `mm_tunable_parts` contains `mm_vision_tower`.")
|
| 29 |
+
self.load_model()
|
| 30 |
+
else:
|
| 31 |
+
self.cfg_only = self.config
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def load_model(self, device_map=None):
|
| 35 |
+
print(f"Pretrained: {self.vision_tower_pretrained}")
|
| 36 |
+
self.image_processor = EvaClipImageTrainProcessor(self.config["vision_cfg"]["image_size"])
|
| 37 |
+
self.vision_tower = EVAEncoderWrapper(self.vision_tower_pretrained, self.config)
|
| 38 |
+
print(f"Loaded image processor: {self.image_processor}")
|
| 39 |
+
self.vision_tower.requires_grad_(False)
|
| 40 |
+
self.is_loaded = True
|
| 41 |
+
|
| 42 |
+
def forward(self, images):
|
| 43 |
+
if type(images) is list:
|
| 44 |
+
image_features = []
|
| 45 |
+
for image in images:
|
| 46 |
+
image_feature = self.vision_tower(image.to(device=self.device, dtype=self.dtype).unsqueeze(0)).to(image.dtype)
|
| 47 |
+
image_features.append(image_feature)
|
| 48 |
+
else:
|
| 49 |
+
image_features = self.vision_tower(images.to(device=self.device, dtype=self.dtype)).to(images.dtype)
|
| 50 |
+
|
| 51 |
+
return image_features
|
| 52 |
+
|
| 53 |
+
@property
|
| 54 |
+
def dtype(self):
|
| 55 |
+
return self.vision_tower.dtype
|
| 56 |
+
|
| 57 |
+
@property
|
| 58 |
+
def device(self):
|
| 59 |
+
return self.vision_tower.device
|
| 60 |
+
|
| 61 |
+
@property
|
| 62 |
+
def hidden_size(self):
|
| 63 |
+
return self.config["vision_cfg"]["width"]
|
| 64 |
+
|
| 65 |
+
@property
|
| 66 |
+
def num_patches(self):
|
| 67 |
+
return (self.config["vision_cfg"]["image_size"] // self.config["vision_cfg"]["patch_size"]) ** 2
|
| 68 |
+
|
| 69 |
+
@property
|
| 70 |
+
def num_patches_per_side(self):
|
| 71 |
+
return self.config["vision_cfg"]["image_size"] // self.config["vision_cfg"]["patch_size"]
|
| 72 |
+
|
| 73 |
+
@property
|
| 74 |
+
def image_size(self):
|
| 75 |
+
return self.config["vision_cfg"]["image_size"]
|
legacy_eva_clip/eva_clip_processors.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
# Adapted from https://github.com/baaivision/EVA/tree/master/EVA-CLIP
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from torchvision.transforms.functional import InterpolationMode
|
| 7 |
+
from transformers.image_processing_utils import BatchFeature
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from transformers.image_transforms import convert_to_rgb
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
class BaseProcessor:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.transform = lambda x: x
|
| 15 |
+
return
|
| 16 |
+
|
| 17 |
+
def __call__(self, item):
|
| 18 |
+
return self.transform(item)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class EvaClipImageBaseProcessor(BaseProcessor):
|
| 22 |
+
def __init__(self, mean=None, std=None):
|
| 23 |
+
self.mean = (0.48145466, 0.4578275, 0.40821073) if mean is None else mean
|
| 24 |
+
self.std = (0.26862954, 0.26130258, 0.27577711) if std is None else std
|
| 25 |
+
self.normalize = transforms.Normalize(self.mean, self.std)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@property
|
| 29 |
+
def image_mean(self):
|
| 30 |
+
return self.mean
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class EvaClipImageTrainProcessor(EvaClipImageBaseProcessor):
|
| 34 |
+
def __init__(self, image_size=224, mean=None, std=None, min_scale=0.5, max_scale=1.0):
|
| 35 |
+
super().__init__(mean=mean, std=std)
|
| 36 |
+
|
| 37 |
+
self.transform = transforms.Compose(
|
| 38 |
+
[
|
| 39 |
+
convert_to_rgb,
|
| 40 |
+
transforms.Resize(
|
| 41 |
+
image_size,
|
| 42 |
+
interpolation=InterpolationMode.BICUBIC,
|
| 43 |
+
),
|
| 44 |
+
transforms.CenterCrop(image_size),
|
| 45 |
+
transforms.ToTensor(),
|
| 46 |
+
self.normalize,
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
self.image_size = image_size
|
| 52 |
+
|
| 53 |
+
def preprocess(self, images, return_tensors):
|
| 54 |
+
if isinstance(images, Image.Image):
|
| 55 |
+
images = [images]
|
| 56 |
+
else:
|
| 57 |
+
assert isinstance(images, list)
|
| 58 |
+
|
| 59 |
+
transformed_images = [self.transform(image).numpy() for image in images]
|
| 60 |
+
data = {"pixel_values": transformed_images}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
return BatchFeature(data=data, tensor_type=return_tensors)
|
| 64 |
+
|
| 65 |
+
def __call__(self, item):
|
| 66 |
+
return self.transform(item)
|
| 67 |
+
|
| 68 |
+
@property
|
| 69 |
+
def crop_size(self):
|
| 70 |
+
return {"height": self.image_size, "width": self.image_size}
|
| 71 |
+
|
| 72 |
+
@property
|
| 73 |
+
def size(self):
|
| 74 |
+
return {"shortest_edge": self.image_size}
|
legacy_eva_clip/eva_vit.py
ADDED
|
@@ -0,0 +1,815 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
# Adapted from https://github.com/baaivision/EVA/tree/master/EVA-CLIP
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from math import pi
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Optional
|
| 9 |
+
import importlib.util
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
from torch import nn
|
| 13 |
+
from einops import rearrange, repeat
|
| 14 |
+
import logging
|
| 15 |
+
|
| 16 |
+
_hub_spec = importlib.util.find_spec("huggingface_hub")
|
| 17 |
+
if _hub_spec is not None:
|
| 18 |
+
from huggingface_hub import snapshot_download # type: ignore
|
| 19 |
+
else: # pragma: no cover - optional dependency
|
| 20 |
+
snapshot_download = None
|
| 21 |
+
|
| 22 |
+
_DEFAULT_EVA_REPO = "jiuhai/eva_clip_vision_tower"
|
| 23 |
+
XFORMERS_AVAILABLE = False # populated later once xops import resolves
|
| 24 |
+
_XFORMERS_WARNING_EMITTED = False
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _resolve_vision_checkpoint_path(vision_tower_pretrained: Optional[str]) -> str:
|
| 28 |
+
"""Determine where to load the EVA visual tower weights from."""
|
| 29 |
+
|
| 30 |
+
candidate_files: list[Path] = []
|
| 31 |
+
if vision_tower_pretrained:
|
| 32 |
+
supplied = Path(vision_tower_pretrained)
|
| 33 |
+
if supplied.is_file():
|
| 34 |
+
candidate_files.append(supplied)
|
| 35 |
+
elif supplied.is_dir():
|
| 36 |
+
for filename in (
|
| 37 |
+
"pytorch_model.bin",
|
| 38 |
+
"model.safetensors",
|
| 39 |
+
"visual.pth",
|
| 40 |
+
"visual.bin",
|
| 41 |
+
):
|
| 42 |
+
candidate = supplied / filename
|
| 43 |
+
if candidate.exists():
|
| 44 |
+
candidate_files.append(candidate)
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
if candidate_files:
|
| 48 |
+
return str(candidate_files[0])
|
| 49 |
+
|
| 50 |
+
if snapshot_download is None:
|
| 51 |
+
raise FileNotFoundError(
|
| 52 |
+
"EVA vision weights not found locally and huggingface_hub is unavailable. "
|
| 53 |
+
"Provide --vision-tower-path pointing to a directory containing the checkpoint."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
cache_dir = Path(snapshot_download(repo_id=_DEFAULT_EVA_REPO))
|
| 57 |
+
default_file = cache_dir / "pytorch_model.bin"
|
| 58 |
+
if not default_file.exists():
|
| 59 |
+
raise FileNotFoundError(f"Default EVA checkpoint not found at {default_file}.")
|
| 60 |
+
return str(default_file)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def broadcat(tensors, dim=-1):
|
| 64 |
+
num_tensors = len(tensors)
|
| 65 |
+
shape_lens = set(list(map(lambda t: len(t.shape), tensors)))
|
| 66 |
+
assert len(shape_lens) == 1, "tensors must all have the same number of dimensions"
|
| 67 |
+
shape_len = list(shape_lens)[0]
|
| 68 |
+
dim = (dim + shape_len) if dim < 0 else dim
|
| 69 |
+
dims = list(zip(*map(lambda t: list(t.shape), tensors)))
|
| 70 |
+
expandable_dims = [(i, val) for i, val in enumerate(dims) if i != dim]
|
| 71 |
+
assert all([*map(lambda t: len(set(t[1])) <= 2, expandable_dims)]), "invalid dimensions for broadcastable concatentation"
|
| 72 |
+
max_dims = list(map(lambda t: (t[0], max(t[1])), expandable_dims))
|
| 73 |
+
expanded_dims = list(map(lambda t: (t[0], (t[1],) * num_tensors), max_dims))
|
| 74 |
+
expanded_dims.insert(dim, (dim, dims[dim]))
|
| 75 |
+
expandable_shapes = list(zip(*map(lambda t: t[1], expanded_dims)))
|
| 76 |
+
tensors = list(map(lambda t: t[0].expand(*t[1]), zip(tensors, expandable_shapes)))
|
| 77 |
+
return torch.cat(tensors, dim=dim)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def rotate_half(x):
|
| 81 |
+
x = rearrange(x, "... (d r) -> ... d r", r=2)
|
| 82 |
+
x1, x2 = x.unbind(dim=-1)
|
| 83 |
+
x = torch.stack((-x2, x1), dim=-1)
|
| 84 |
+
return rearrange(x, "... d r -> ... (d r)")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class VisionRotaryEmbeddingFast(nn.Module):
|
| 88 |
+
def __init__(self, dim, pt_seq_len, ft_seq_len=None, custom_freqs=None, freqs_for="lang", theta=10000, max_freq=10, num_freqs=1, patch_dropout=0.0):
|
| 89 |
+
super().__init__()
|
| 90 |
+
if custom_freqs:
|
| 91 |
+
freqs = custom_freqs
|
| 92 |
+
elif freqs_for == "lang":
|
| 93 |
+
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
|
| 94 |
+
elif freqs_for == "pixel":
|
| 95 |
+
freqs = torch.linspace(1.0, max_freq / 2, dim // 2) * pi
|
| 96 |
+
elif freqs_for == "constant":
|
| 97 |
+
freqs = torch.ones(num_freqs).float()
|
| 98 |
+
else:
|
| 99 |
+
raise ValueError(f"unknown modality {freqs_for}")
|
| 100 |
+
|
| 101 |
+
if ft_seq_len is None:
|
| 102 |
+
ft_seq_len = pt_seq_len
|
| 103 |
+
t = torch.arange(ft_seq_len) / ft_seq_len * pt_seq_len
|
| 104 |
+
|
| 105 |
+
freqs = torch.einsum("..., f -> ... f", t, freqs)
|
| 106 |
+
freqs = repeat(freqs, "... n -> ... (n r)", r=2)
|
| 107 |
+
freqs = broadcat((freqs[:, None, :], freqs[None, :, :]), dim=-1)
|
| 108 |
+
|
| 109 |
+
freqs_cos = freqs.cos().view(-1, freqs.shape[-1])
|
| 110 |
+
freqs_sin = freqs.sin().view(-1, freqs.shape[-1])
|
| 111 |
+
|
| 112 |
+
self.patch_dropout = patch_dropout
|
| 113 |
+
|
| 114 |
+
self.register_buffer("freqs_cos", freqs_cos)
|
| 115 |
+
self.register_buffer("freqs_sin", freqs_sin)
|
| 116 |
+
|
| 117 |
+
logging.info(f"Shape of rope freq: {self.freqs_cos.shape}")
|
| 118 |
+
|
| 119 |
+
def forward(self, t, patch_indices_keep=None):
|
| 120 |
+
if patch_indices_keep is not None:
|
| 121 |
+
batch = t.size()[0]
|
| 122 |
+
batch_indices = torch.arange(batch)
|
| 123 |
+
batch_indices = batch_indices[..., None]
|
| 124 |
+
|
| 125 |
+
freqs_cos = repeat(self.freqs_cos, "i j -> n i m j", n=t.shape[0], m=t.shape[1])
|
| 126 |
+
freqs_sin = repeat(self.freqs_sin, "i j -> n i m j", n=t.shape[0], m=t.shape[1])
|
| 127 |
+
|
| 128 |
+
freqs_cos = freqs_cos[batch_indices, patch_indices_keep]
|
| 129 |
+
freqs_cos = rearrange(freqs_cos, "n i m j -> n m i j")
|
| 130 |
+
freqs_sin = freqs_sin[batch_indices, patch_indices_keep]
|
| 131 |
+
freqs_sin = rearrange(freqs_sin, "n i m j -> n m i j")
|
| 132 |
+
|
| 133 |
+
return t * freqs_cos + rotate_half(t) * freqs_sin
|
| 134 |
+
|
| 135 |
+
return t * self.freqs_cos + rotate_half(t) * self.freqs_sin
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class LayerNorm(nn.LayerNorm):
|
| 139 |
+
"""Subclass torch's LayerNorm (with cast back to input dtype)."""
|
| 140 |
+
|
| 141 |
+
def forward(self, x: torch.Tensor):
|
| 142 |
+
orig_type = x.dtype
|
| 143 |
+
x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
|
| 144 |
+
return x.to(orig_type)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
class PatchDropout(nn.Module):
|
| 148 |
+
"""
|
| 149 |
+
https://arxiv.org/abs/2212.00794
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
def __init__(self, prob, exclude_first_token=True):
|
| 153 |
+
super().__init__()
|
| 154 |
+
assert 0 <= prob < 1.
|
| 155 |
+
self.prob = prob
|
| 156 |
+
self.exclude_first_token = exclude_first_token # exclude CLS token
|
| 157 |
+
print(f"os.getenv('RoPE')={os.getenv('RoPE')}")
|
| 158 |
+
|
| 159 |
+
def forward(self, x):
|
| 160 |
+
if not self.training or self.prob == 0.:
|
| 161 |
+
return x
|
| 162 |
+
|
| 163 |
+
if self.exclude_first_token:
|
| 164 |
+
cls_tokens, x = x[:, :1], x[:, 1:]
|
| 165 |
+
else:
|
| 166 |
+
cls_tokens = torch.jit.annotate(torch.Tensor, x[:, :1])
|
| 167 |
+
|
| 168 |
+
batch = x.size()[0]
|
| 169 |
+
num_tokens = x.size()[1]
|
| 170 |
+
|
| 171 |
+
batch_indices = torch.arange(batch)
|
| 172 |
+
batch_indices = batch_indices[..., None]
|
| 173 |
+
|
| 174 |
+
keep_prob = 1 - self.prob
|
| 175 |
+
num_patches_keep = max(1, int(num_tokens * keep_prob))
|
| 176 |
+
|
| 177 |
+
rand = torch.randn(batch, num_tokens)
|
| 178 |
+
patch_indices_keep = rand.topk(num_patches_keep, dim=-1).indices
|
| 179 |
+
|
| 180 |
+
x = x[batch_indices, patch_indices_keep]
|
| 181 |
+
|
| 182 |
+
if self.exclude_first_token:
|
| 183 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 184 |
+
|
| 185 |
+
if self.training and os.getenv('RoPE') == '1':
|
| 186 |
+
return x, patch_indices_keep
|
| 187 |
+
|
| 188 |
+
return x
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
# --------------------------------------------------------
|
| 192 |
+
# Adapted from https://github.com/microsoft/unilm/tree/master/beit
|
| 193 |
+
# --------------------------------------------------------
|
| 194 |
+
import math
|
| 195 |
+
import os
|
| 196 |
+
import torch.nn as nn
|
| 197 |
+
import torch.nn.functional as F
|
| 198 |
+
|
| 199 |
+
try:
|
| 200 |
+
from timm.models.layers import drop_path, to_2tuple, trunc_normal_
|
| 201 |
+
except:
|
| 202 |
+
from timm.layers import drop_path, to_2tuple, trunc_normal_
|
| 203 |
+
|
| 204 |
+
if os.getenv("ENV_TYPE") == "deepspeed":
|
| 205 |
+
try:
|
| 206 |
+
from deepspeed.runtime.activation_checkpointing.checkpointing import checkpoint
|
| 207 |
+
except:
|
| 208 |
+
from torch.utils.checkpoint import checkpoint
|
| 209 |
+
else:
|
| 210 |
+
from torch.utils.checkpoint import checkpoint
|
| 211 |
+
|
| 212 |
+
try:
|
| 213 |
+
import xformers.ops as xops
|
| 214 |
+
XFORMERS_AVAILABLE = True
|
| 215 |
+
except ImportError:
|
| 216 |
+
xops = None
|
| 217 |
+
# print("Please 'pip install xformers'")
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
class DropPath(nn.Module):
|
| 221 |
+
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
|
| 222 |
+
"""
|
| 223 |
+
def __init__(self, drop_prob=None):
|
| 224 |
+
super(DropPath, self).__init__()
|
| 225 |
+
self.drop_prob = drop_prob
|
| 226 |
+
|
| 227 |
+
def forward(self, x):
|
| 228 |
+
return drop_path(x, self.drop_prob, self.training)
|
| 229 |
+
|
| 230 |
+
def extra_repr(self) -> str:
|
| 231 |
+
return 'p={}'.format(self.drop_prob)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class Mlp(nn.Module):
|
| 235 |
+
def __init__(
|
| 236 |
+
self,
|
| 237 |
+
in_features,
|
| 238 |
+
hidden_features=None,
|
| 239 |
+
out_features=None,
|
| 240 |
+
act_layer=nn.GELU,
|
| 241 |
+
norm_layer=nn.LayerNorm,
|
| 242 |
+
drop=0.,
|
| 243 |
+
subln=False,
|
| 244 |
+
|
| 245 |
+
):
|
| 246 |
+
super().__init__()
|
| 247 |
+
out_features = out_features or in_features
|
| 248 |
+
hidden_features = hidden_features or in_features
|
| 249 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
| 250 |
+
self.act = act_layer()
|
| 251 |
+
|
| 252 |
+
self.ffn_ln = norm_layer(hidden_features) if subln else nn.Identity()
|
| 253 |
+
|
| 254 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
| 255 |
+
self.drop = nn.Dropout(drop)
|
| 256 |
+
|
| 257 |
+
def forward(self, x):
|
| 258 |
+
x = self.fc1(x)
|
| 259 |
+
x = self.act(x)
|
| 260 |
+
# x = self.drop(x)
|
| 261 |
+
# commit this for the orignal BERT implement
|
| 262 |
+
x = self.ffn_ln(x)
|
| 263 |
+
|
| 264 |
+
x = self.fc2(x)
|
| 265 |
+
x = self.drop(x)
|
| 266 |
+
return x
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
class SwiGLU(nn.Module):
|
| 270 |
+
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.,
|
| 271 |
+
norm_layer=nn.LayerNorm, subln=False):
|
| 272 |
+
super().__init__()
|
| 273 |
+
out_features = out_features or in_features
|
| 274 |
+
hidden_features = hidden_features or in_features
|
| 275 |
+
|
| 276 |
+
self.w1 = nn.Linear(in_features, hidden_features)
|
| 277 |
+
self.w2 = nn.Linear(in_features, hidden_features)
|
| 278 |
+
|
| 279 |
+
self.act = act_layer()
|
| 280 |
+
self.ffn_ln = norm_layer(hidden_features) if subln else nn.Identity()
|
| 281 |
+
self.w3 = nn.Linear(hidden_features, out_features)
|
| 282 |
+
|
| 283 |
+
self.drop = nn.Dropout(drop)
|
| 284 |
+
|
| 285 |
+
def forward(self, x):
|
| 286 |
+
x1 = self.w1(x)
|
| 287 |
+
x2 = self.w2(x)
|
| 288 |
+
hidden = self.act(x1) * x2
|
| 289 |
+
x = self.ffn_ln(hidden)
|
| 290 |
+
x = self.w3(x)
|
| 291 |
+
x = self.drop(x)
|
| 292 |
+
return x
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
class Attention(nn.Module):
|
| 296 |
+
def __init__(
|
| 297 |
+
self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0.,
|
| 298 |
+
proj_drop=0., window_size=None, attn_head_dim=None, xattn=False, rope=None, subln=False, norm_layer=nn.LayerNorm):
|
| 299 |
+
super().__init__()
|
| 300 |
+
self.num_heads = num_heads
|
| 301 |
+
head_dim = dim // num_heads
|
| 302 |
+
if attn_head_dim is not None:
|
| 303 |
+
head_dim = attn_head_dim
|
| 304 |
+
all_head_dim = head_dim * self.num_heads
|
| 305 |
+
self.scale = qk_scale or head_dim ** -0.5
|
| 306 |
+
|
| 307 |
+
self.subln = subln
|
| 308 |
+
if self.subln:
|
| 309 |
+
self.q_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 310 |
+
self.k_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 311 |
+
self.v_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 312 |
+
else:
|
| 313 |
+
if qkv_bias:
|
| 314 |
+
self.qkv = nn.Linear(dim, all_head_dim * 3, bias=True)
|
| 315 |
+
else:
|
| 316 |
+
self.qkv = nn.Linear(dim, all_head_dim * 3, bias=False)
|
| 317 |
+
|
| 318 |
+
# if qkv_bias:
|
| 319 |
+
# self.q_bias = nn.Parameter(torch.zeros(all_head_dim))
|
| 320 |
+
# self.v_bias = nn.Parameter(torch.zeros(all_head_dim))
|
| 321 |
+
# else:
|
| 322 |
+
# self.q_bias = None
|
| 323 |
+
# self.v_bias = None
|
| 324 |
+
|
| 325 |
+
self.window_size = None
|
| 326 |
+
self.relative_position_bias_table = None
|
| 327 |
+
self.relative_position_index = None
|
| 328 |
+
|
| 329 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
| 330 |
+
self.inner_attn_ln = norm_layer(all_head_dim) if subln else nn.Identity()
|
| 331 |
+
# self.proj = nn.Linear(all_head_dim, all_head_dim)
|
| 332 |
+
self.proj = nn.Linear(all_head_dim, dim)
|
| 333 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
| 334 |
+
self.xattn = xattn and XFORMERS_AVAILABLE
|
| 335 |
+
self.xattn_drop = attn_drop
|
| 336 |
+
global _XFORMERS_WARNING_EMITTED
|
| 337 |
+
if xattn and not self.xattn and not _XFORMERS_WARNING_EMITTED:
|
| 338 |
+
logging.warning("xformers.ops not available; falling back to standard attention for EVA-CLIP vision tower.")
|
| 339 |
+
_XFORMERS_WARNING_EMITTED = True
|
| 340 |
+
|
| 341 |
+
self.rope = rope
|
| 342 |
+
|
| 343 |
+
def forward(self, x, rel_pos_bias=None, attn_mask=None):
|
| 344 |
+
B, N, C = x.shape
|
| 345 |
+
if self.subln:
|
| 346 |
+
q = F.linear(input=x, weight=self.q_proj.weight, bias=self.q_bias)
|
| 347 |
+
k = F.linear(input=x, weight=self.k_proj.weight, bias=None)
|
| 348 |
+
v = F.linear(input=x, weight=self.v_proj.weight, bias=self.v_bias)
|
| 349 |
+
|
| 350 |
+
q = q.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3) # B, num_heads, N, C
|
| 351 |
+
k = k.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3)
|
| 352 |
+
v = v.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3)
|
| 353 |
+
else:
|
| 354 |
+
|
| 355 |
+
# qkv_bias = None
|
| 356 |
+
# if self.q_bias is not None:
|
| 357 |
+
# qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias))
|
| 358 |
+
|
| 359 |
+
# qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias)
|
| 360 |
+
|
| 361 |
+
qkv = self.qkv(x)
|
| 362 |
+
|
| 363 |
+
qkv = qkv.reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) # 3, B, num_heads, N, C
|
| 364 |
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 365 |
+
|
| 366 |
+
if self.rope:
|
| 367 |
+
q_t = q[:, :, 1:, :]
|
| 368 |
+
ro_q_t = self.rope(q_t)
|
| 369 |
+
q = torch.cat((q[:, :, :1, :], ro_q_t), -2).type_as(v)
|
| 370 |
+
|
| 371 |
+
k_t = k[:, :, 1:, :]
|
| 372 |
+
ro_k_t = self.rope(k_t)
|
| 373 |
+
k = torch.cat((k[:, :, :1, :], ro_k_t), -2).type_as(v)
|
| 374 |
+
|
| 375 |
+
if self.xattn:
|
| 376 |
+
q = q.permute(0, 2, 1, 3) # B, num_heads, N, C -> B, N, num_heads, C
|
| 377 |
+
k = k.permute(0, 2, 1, 3)
|
| 378 |
+
v = v.permute(0, 2, 1, 3)
|
| 379 |
+
|
| 380 |
+
x = xops.memory_efficient_attention(
|
| 381 |
+
q, k, v,
|
| 382 |
+
p=self.xattn_drop,
|
| 383 |
+
scale=self.scale,
|
| 384 |
+
)
|
| 385 |
+
x = x.reshape(B, N, -1)
|
| 386 |
+
x = self.inner_attn_ln(x)
|
| 387 |
+
x = self.proj(x)
|
| 388 |
+
x = self.proj_drop(x)
|
| 389 |
+
else:
|
| 390 |
+
q = q * self.scale
|
| 391 |
+
attn = (q @ k.transpose(-2, -1))
|
| 392 |
+
|
| 393 |
+
if self.relative_position_bias_table is not None:
|
| 394 |
+
relative_position_bias = \
|
| 395 |
+
self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
|
| 396 |
+
self.window_size[0] * self.window_size[1] + 1,
|
| 397 |
+
self.window_size[0] * self.window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH
|
| 398 |
+
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
| 399 |
+
attn = attn + relative_position_bias.unsqueeze(0).type_as(attn)
|
| 400 |
+
|
| 401 |
+
if rel_pos_bias is not None:
|
| 402 |
+
attn = attn + rel_pos_bias.type_as(attn)
|
| 403 |
+
|
| 404 |
+
if attn_mask is not None:
|
| 405 |
+
attn_mask = attn_mask.bool()
|
| 406 |
+
attn = attn.masked_fill(~attn_mask[:, None, None, :], float("-inf"))
|
| 407 |
+
|
| 408 |
+
attn = attn.softmax(dim=-1)
|
| 409 |
+
attn = self.attn_drop(attn)
|
| 410 |
+
|
| 411 |
+
x = (attn @ v).transpose(1, 2).reshape(B, N, -1)
|
| 412 |
+
x = self.inner_attn_ln(x)
|
| 413 |
+
x = self.proj(x)
|
| 414 |
+
x = self.proj_drop(x)
|
| 415 |
+
return x
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
class Block(nn.Module):
|
| 419 |
+
|
| 420 |
+
def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
|
| 421 |
+
drop_path=0., init_values=None, act_layer=nn.GELU, norm_layer=nn.LayerNorm,
|
| 422 |
+
window_size=None, attn_head_dim=None, xattn=False, rope=None, postnorm=False,
|
| 423 |
+
subln=False, naiveswiglu=False):
|
| 424 |
+
super().__init__()
|
| 425 |
+
self.norm1 = norm_layer(dim)
|
| 426 |
+
self.attn = Attention(
|
| 427 |
+
dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale,
|
| 428 |
+
attn_drop=attn_drop, proj_drop=drop, window_size=window_size, attn_head_dim=attn_head_dim,
|
| 429 |
+
xattn=xattn, rope=rope, subln=subln, norm_layer=norm_layer)
|
| 430 |
+
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
|
| 431 |
+
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
| 432 |
+
self.norm2 = norm_layer(dim)
|
| 433 |
+
mlp_hidden_dim = int(dim * mlp_ratio)
|
| 434 |
+
|
| 435 |
+
if naiveswiglu:
|
| 436 |
+
self.mlp = SwiGLU(
|
| 437 |
+
in_features=dim,
|
| 438 |
+
hidden_features=mlp_hidden_dim,
|
| 439 |
+
subln=subln,
|
| 440 |
+
norm_layer=norm_layer,
|
| 441 |
+
)
|
| 442 |
+
else:
|
| 443 |
+
self.mlp = Mlp(
|
| 444 |
+
in_features=dim,
|
| 445 |
+
hidden_features=mlp_hidden_dim,
|
| 446 |
+
act_layer=act_layer,
|
| 447 |
+
subln=subln,
|
| 448 |
+
drop=drop
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
+
if init_values is not None and init_values > 0:
|
| 452 |
+
self.gamma_1 = nn.Parameter(init_values * torch.ones((dim)),requires_grad=True)
|
| 453 |
+
self.gamma_2 = nn.Parameter(init_values * torch.ones((dim)),requires_grad=True)
|
| 454 |
+
else:
|
| 455 |
+
self.gamma_1, self.gamma_2 = None, None
|
| 456 |
+
|
| 457 |
+
self.postnorm = postnorm
|
| 458 |
+
|
| 459 |
+
def forward(self, x, rel_pos_bias=None, attn_mask=None):
|
| 460 |
+
if self.gamma_1 is None:
|
| 461 |
+
if self.postnorm:
|
| 462 |
+
x = x + self.drop_path(self.norm1(self.attn(x, rel_pos_bias=rel_pos_bias, attn_mask=attn_mask)))
|
| 463 |
+
x = x + self.drop_path(self.norm2(self.mlp(x)))
|
| 464 |
+
else:
|
| 465 |
+
x = x + self.drop_path(self.attn(self.norm1(x), rel_pos_bias=rel_pos_bias, attn_mask=attn_mask))
|
| 466 |
+
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
| 467 |
+
else:
|
| 468 |
+
if self.postnorm:
|
| 469 |
+
x = x + self.drop_path(self.gamma_1 * self.norm1(self.attn(x, rel_pos_bias=rel_pos_bias, attn_mask=attn_mask)))
|
| 470 |
+
x = x + self.drop_path(self.gamma_2 * self.norm2(self.mlp(x)))
|
| 471 |
+
else:
|
| 472 |
+
x = x + self.drop_path(self.gamma_1 * self.attn(self.norm1(x), rel_pos_bias=rel_pos_bias, attn_mask=attn_mask))
|
| 473 |
+
x = x + self.drop_path(self.gamma_2 * self.mlp(self.norm2(x)))
|
| 474 |
+
return x
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
class PatchEmbed(nn.Module):
|
| 478 |
+
""" Image to Patch Embedding
|
| 479 |
+
"""
|
| 480 |
+
def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768):
|
| 481 |
+
super().__init__()
|
| 482 |
+
img_size = to_2tuple(img_size)
|
| 483 |
+
patch_size = to_2tuple(patch_size)
|
| 484 |
+
num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0])
|
| 485 |
+
self.patch_shape = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
|
| 486 |
+
self.img_size = img_size
|
| 487 |
+
self.patch_size = patch_size
|
| 488 |
+
self.num_patches = num_patches
|
| 489 |
+
|
| 490 |
+
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
|
| 491 |
+
|
| 492 |
+
def forward(self, x, **kwargs):
|
| 493 |
+
B, C, H, W = x.shape
|
| 494 |
+
# FIXME look at relaxing size constraints
|
| 495 |
+
assert H == self.img_size[0] and W == self.img_size[1], \
|
| 496 |
+
f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
|
| 497 |
+
x = self.proj(x).flatten(2).transpose(1, 2)
|
| 498 |
+
return x
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
class RelativePositionBias(nn.Module):
|
| 502 |
+
|
| 503 |
+
def __init__(self, window_size, num_heads):
|
| 504 |
+
super().__init__()
|
| 505 |
+
self.window_size = window_size
|
| 506 |
+
self.num_relative_distance = (2 * window_size[0] - 1) * (2 * window_size[1] - 1) + 3
|
| 507 |
+
self.relative_position_bias_table = nn.Parameter(torch.zeros(self.num_relative_distance, num_heads)) # 2*Wh-1 * 2*Ww-1, nH
|
| 508 |
+
# cls to token & token 2 cls & cls to cls
|
| 509 |
+
|
| 510 |
+
# get pair-wise relative position index for each token inside the window
|
| 511 |
+
coords_h = torch.arange(window_size[0])
|
| 512 |
+
coords_w = torch.arange(window_size[1])
|
| 513 |
+
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
|
| 514 |
+
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
|
| 515 |
+
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
|
| 516 |
+
relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
|
| 517 |
+
relative_coords[:, :, 0] += window_size[0] - 1 # shift to start from 0
|
| 518 |
+
relative_coords[:, :, 1] += window_size[1] - 1
|
| 519 |
+
relative_coords[:, :, 0] *= 2 * window_size[1] - 1
|
| 520 |
+
relative_position_index = torch.zeros(size=(window_size[0] * window_size[1] + 1,) * 2, dtype=relative_coords.dtype)
|
| 521 |
+
relative_position_index[1:, 1:] = relative_coords.sum(-1) # Wh*Ww, Wh*Ww
|
| 522 |
+
relative_position_index[0, 0:] = self.num_relative_distance - 3
|
| 523 |
+
relative_position_index[0:, 0] = self.num_relative_distance - 2
|
| 524 |
+
relative_position_index[0, 0] = self.num_relative_distance - 1
|
| 525 |
+
|
| 526 |
+
self.register_buffer("relative_position_index", relative_position_index)
|
| 527 |
+
|
| 528 |
+
def forward(self):
|
| 529 |
+
relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view(self.window_size[0] * self.window_size[1] + 1, self.window_size[0] * self.window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH
|
| 530 |
+
return relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
class EVAVisionTransformer(nn.Module):
|
| 534 |
+
"""Vision Transformer with support for patch or hybrid CNN input stage"""
|
| 535 |
+
|
| 536 |
+
def __init__(self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, embed_dim=768, depth=12,
|
| 537 |
+
num_heads=12, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop_rate=0., attn_drop_rate=0.,
|
| 538 |
+
drop_path_rate=0., norm_layer=nn.LayerNorm, init_values=None, patch_dropout=0.,
|
| 539 |
+
use_abs_pos_emb=True, use_rel_pos_bias=False, use_shared_rel_pos_bias=False, rope=False,
|
| 540 |
+
use_mean_pooling=True, init_scale=0.001, grad_checkpointing=False, xattn=False, postnorm=False,
|
| 541 |
+
pt_hw_seq_len=16, intp_freq=False, naiveswiglu=False, subln=False,
|
| 542 |
+
):
|
| 543 |
+
super().__init__()
|
| 544 |
+
self.image_size = img_size
|
| 545 |
+
# self.num_classes = num_classes
|
| 546 |
+
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
|
| 547 |
+
|
| 548 |
+
self.patch_embed = PatchEmbed(
|
| 549 |
+
img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
|
| 550 |
+
num_patches = self.patch_embed.num_patches
|
| 551 |
+
|
| 552 |
+
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
|
| 553 |
+
if use_abs_pos_emb:
|
| 554 |
+
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + 1, embed_dim))
|
| 555 |
+
else:
|
| 556 |
+
self.pos_embed = None
|
| 557 |
+
self.pos_drop = nn.Dropout(p=drop_rate)
|
| 558 |
+
|
| 559 |
+
self.rel_pos_bias = None
|
| 560 |
+
self.rope = None
|
| 561 |
+
|
| 562 |
+
self.naiveswiglu = naiveswiglu
|
| 563 |
+
|
| 564 |
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
|
| 565 |
+
self.use_rel_pos_bias = use_rel_pos_bias
|
| 566 |
+
self.blocks = nn.ModuleList([
|
| 567 |
+
Block(
|
| 568 |
+
dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
|
| 569 |
+
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer,
|
| 570 |
+
init_values=init_values, window_size=self.patch_embed.patch_shape if use_rel_pos_bias else None,
|
| 571 |
+
xattn=xattn, rope=self.rope, postnorm=postnorm, subln=subln, naiveswiglu=naiveswiglu)
|
| 572 |
+
for i in range(depth)])
|
| 573 |
+
|
| 574 |
+
# setting a patch_dropout of 0. would mean it is disabled and this function would be the identity fn
|
| 575 |
+
self.patch_dropout = PatchDropout(patch_dropout) if patch_dropout > 0. else nn.Identity()
|
| 576 |
+
|
| 577 |
+
self.grad_checkpointing = grad_checkpointing
|
| 578 |
+
|
| 579 |
+
def fix_init_weight(self):
|
| 580 |
+
def rescale(param, layer_id):
|
| 581 |
+
param.div_(math.sqrt(2.0 * layer_id))
|
| 582 |
+
|
| 583 |
+
for layer_id, layer in enumerate(self.blocks):
|
| 584 |
+
rescale(layer.attn.proj.weight.data, layer_id + 1)
|
| 585 |
+
if self.naiveswiglu:
|
| 586 |
+
rescale(layer.mlp.w3.weight.data, layer_id + 1)
|
| 587 |
+
else:
|
| 588 |
+
rescale(layer.mlp.fc2.weight.data, layer_id + 1)
|
| 589 |
+
|
| 590 |
+
def get_cast_dtype(self) -> torch.dtype:
|
| 591 |
+
return self.blocks[0].mlp.fc2.weight.dtype
|
| 592 |
+
|
| 593 |
+
def _init_weights(self, m):
|
| 594 |
+
if isinstance(m, nn.Linear):
|
| 595 |
+
trunc_normal_(m.weight, std=0.02)
|
| 596 |
+
if m.bias is not None:
|
| 597 |
+
nn.init.constant_(m.bias, 0)
|
| 598 |
+
elif isinstance(m, nn.LayerNorm):
|
| 599 |
+
nn.init.constant_(m.bias, 0)
|
| 600 |
+
nn.init.constant_(m.weight, 1.0)
|
| 601 |
+
|
| 602 |
+
def get_num_layers(self):
|
| 603 |
+
return len(self.blocks)
|
| 604 |
+
|
| 605 |
+
def lock(self, unlocked_groups=0, freeze_bn_stats=False):
|
| 606 |
+
assert unlocked_groups == 0, "partial locking not currently supported for this model"
|
| 607 |
+
for param in self.parameters():
|
| 608 |
+
param.requires_grad = False
|
| 609 |
+
|
| 610 |
+
@torch.jit.ignore
|
| 611 |
+
def set_grad_checkpointing(self, enable=True):
|
| 612 |
+
self.grad_checkpointing = enable
|
| 613 |
+
|
| 614 |
+
@torch.jit.ignore
|
| 615 |
+
def no_weight_decay(self):
|
| 616 |
+
return {"pos_embed", "cls_token"}
|
| 617 |
+
|
| 618 |
+
def get_classifier(self):
|
| 619 |
+
return self.head
|
| 620 |
+
|
| 621 |
+
def reset_classifier(self, num_classes, global_pool=""):
|
| 622 |
+
self.num_classes = num_classes
|
| 623 |
+
self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()
|
| 624 |
+
|
| 625 |
+
def forward_features(self, x):
|
| 626 |
+
x = self.patch_embed(x)
|
| 627 |
+
batch_size, seq_len, _ = x.size()
|
| 628 |
+
|
| 629 |
+
cls_tokens = self.cls_token.expand(batch_size, -1, -1) # stole cls_tokens impl from Phil Wang, thanks
|
| 630 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 631 |
+
if self.pos_embed is not None:
|
| 632 |
+
x = x + self.pos_embed
|
| 633 |
+
x = self.pos_drop(x)
|
| 634 |
+
|
| 635 |
+
# a patch_dropout of 0. would mean it is disabled and this function would do nothing but return what was passed in
|
| 636 |
+
if os.getenv('RoPE') == '1':
|
| 637 |
+
if self.training and not isinstance(self.patch_dropout, nn.Identity):
|
| 638 |
+
x, patch_indices_keep = self.patch_dropout(x)
|
| 639 |
+
self.rope.forward = partial(self.rope.forward, patch_indices_keep=patch_indices_keep)
|
| 640 |
+
else:
|
| 641 |
+
self.rope.forward = partial(self.rope.forward, patch_indices_keep=None)
|
| 642 |
+
x = self.patch_dropout(x)
|
| 643 |
+
else:
|
| 644 |
+
x = self.patch_dropout(x)
|
| 645 |
+
|
| 646 |
+
rel_pos_bias = None
|
| 647 |
+
|
| 648 |
+
for blk in self.blocks:
|
| 649 |
+
if self.grad_checkpointing:
|
| 650 |
+
x = checkpoint(blk, x, (rel_pos_bias,))
|
| 651 |
+
else:
|
| 652 |
+
x = blk(x, rel_pos_bias=rel_pos_bias)
|
| 653 |
+
|
| 654 |
+
return x
|
| 655 |
+
|
| 656 |
+
def forward(self, x, return_all_features=False):
|
| 657 |
+
|
| 658 |
+
"""
|
| 659 |
+
:return:
|
| 660 |
+
forward_features function returns raw features of ViT,
|
| 661 |
+
forward with return_all_features returns normalized features of ViT
|
| 662 |
+
:param x:
|
| 663 |
+
:param return_all_features:
|
| 664 |
+
"""
|
| 665 |
+
|
| 666 |
+
features = self.forward_features(x) # [B, n_patch, C]
|
| 667 |
+
return features
|
| 668 |
+
|
| 669 |
+
|
| 670 |
+
def load_state_dict(checkpoint_path: str, map_location: str = "cpu", model_key: str = "model|module|state_dict", is_openai: bool = False, skip_list: list = []):
|
| 671 |
+
if is_openai:
|
| 672 |
+
model = torch.jit.load(checkpoint_path, map_location="cpu").eval()
|
| 673 |
+
state_dict = model.state_dict()
|
| 674 |
+
for key in ["input_resolution", "context_length", "vocab_size"]:
|
| 675 |
+
state_dict.pop(key, None)
|
| 676 |
+
else:
|
| 677 |
+
checkpoint = torch.load(checkpoint_path, map_location=map_location)
|
| 678 |
+
for mk in model_key.split("|"):
|
| 679 |
+
if isinstance(checkpoint, dict) and mk in checkpoint:
|
| 680 |
+
state_dict = checkpoint[mk]
|
| 681 |
+
break
|
| 682 |
+
else:
|
| 683 |
+
state_dict = checkpoint
|
| 684 |
+
if next(iter(state_dict.items()))[0].startswith("module"):
|
| 685 |
+
state_dict = {k[7:]: v for k, v in state_dict.items()}
|
| 686 |
+
|
| 687 |
+
for k in skip_list:
|
| 688 |
+
if k in list(state_dict.keys()):
|
| 689 |
+
logging.info(f"Removing key {k} from pretrained checkpoint")
|
| 690 |
+
del state_dict[k]
|
| 691 |
+
|
| 692 |
+
if os.getenv("RoPE") == "1":
|
| 693 |
+
for k in list(state_dict.keys()):
|
| 694 |
+
if "freqs_cos" in k or "freqs_sin" in k:
|
| 695 |
+
del state_dict[k]
|
| 696 |
+
return state_dict
|
| 697 |
+
|
| 698 |
+
|
| 699 |
+
def load_clip_visual_state_dict(checkpoint_path: str, map_location: str = "cpu", is_openai: bool = False, skip_list: list = []):
|
| 700 |
+
state_dict = load_state_dict(checkpoint_path, map_location=map_location, is_openai=is_openai, skip_list=skip_list)
|
| 701 |
+
# for k in list(state_dict.keys()):
|
| 702 |
+
# if not k.startswith("visual."):
|
| 703 |
+
# del state_dict[k]
|
| 704 |
+
# for k in list(state_dict.keys()):
|
| 705 |
+
# if k.startswith("visual."):
|
| 706 |
+
# new_k = k[7:]
|
| 707 |
+
# state_dict[new_k] = state_dict[k]
|
| 708 |
+
# del state_dict[k]
|
| 709 |
+
return state_dict
|
| 710 |
+
|
| 711 |
+
|
| 712 |
+
from dataclasses import dataclass
|
| 713 |
+
from typing import Optional, Tuple, Union
|
| 714 |
+
|
| 715 |
+
try:
|
| 716 |
+
from apex.normalization import FusedLayerNorm
|
| 717 |
+
except:
|
| 718 |
+
FusedLayerNorm = LayerNorm
|
| 719 |
+
# print("Please build and install Nvidia apex package with option '--cuda_ext' according to https://github.com/NVIDIA/apex#from-source .")
|
| 720 |
+
|
| 721 |
+
|
| 722 |
+
@dataclass
|
| 723 |
+
class CLIPVisionCfg:
|
| 724 |
+
layers: Union[Tuple[int, int, int, int], int] = 12
|
| 725 |
+
width: int = 768
|
| 726 |
+
head_width: int = 64
|
| 727 |
+
mlp_ratio: float = 4.0
|
| 728 |
+
patch_size: int = 16
|
| 729 |
+
image_size: Union[Tuple[int, int], int] = 224
|
| 730 |
+
ls_init_value: Optional[float] = None # layer scale initial value
|
| 731 |
+
patch_dropout: float = 0.0 # what fraction of patches to dropout during training (0 would mean disabled and no patches dropped) - 0.5 to 0.75 recommended in the paper for optimal results
|
| 732 |
+
global_average_pool: bool = False # whether to global average pool the last embedding layer, instead of using CLS token (https://arxiv.org/abs/2205.01580)
|
| 733 |
+
drop_path_rate: Optional[float] = None # drop path rate
|
| 734 |
+
timm_model_name: str = None # a valid model name overrides layers, width, patch_size
|
| 735 |
+
timm_model_pretrained: bool = False # use (imagenet) pretrained weights for named model
|
| 736 |
+
timm_pool: str = "avg" # feature pooling for timm model ('abs_attn', 'rot_attn', 'avg', '')
|
| 737 |
+
timm_proj: str = "linear" # linear projection for timm model output ('linear', 'mlp', '')
|
| 738 |
+
timm_proj_bias: bool = False # enable bias final projection
|
| 739 |
+
eva_model_name: str = None # a valid eva model name overrides layers, width, patch_size
|
| 740 |
+
qkv_bias: bool = True
|
| 741 |
+
fusedLN: bool = False
|
| 742 |
+
xattn: bool = False
|
| 743 |
+
postnorm: bool = False
|
| 744 |
+
rope: bool = False
|
| 745 |
+
pt_hw_seq_len: int = 16 # 224/14
|
| 746 |
+
intp_freq: bool = False
|
| 747 |
+
naiveswiglu: bool = False
|
| 748 |
+
subln: bool = False
|
| 749 |
+
|
| 750 |
+
|
| 751 |
+
def create_norm_layer_factory(use_fused_ln, eps=1e-6):
|
| 752 |
+
# Otherwise, use the standard LayerNorm
|
| 753 |
+
return lambda num_features: nn.LayerNorm(num_features, eps=eps)
|
| 754 |
+
|
| 755 |
+
|
| 756 |
+
def _build_vision_tower(vision_tower_path: str, embed_dim: int, vision_cfg: CLIPVisionCfg, **kwargs):
|
| 757 |
+
if isinstance(vision_cfg, dict):
|
| 758 |
+
vision_cfg = CLIPVisionCfg(**vision_cfg)
|
| 759 |
+
|
| 760 |
+
if vision_cfg.eva_model_name:
|
| 761 |
+
vision_heads = vision_cfg.width // vision_cfg.head_width
|
| 762 |
+
# Determine the appropriate norm layer factory based on the configuration
|
| 763 |
+
norm_layer_factory = create_norm_layer_factory(vision_cfg.fusedLN, eps=1e-6)
|
| 764 |
+
|
| 765 |
+
# breakpoint()
|
| 766 |
+
visual = EVAVisionTransformer(
|
| 767 |
+
img_size=vision_cfg.image_size,
|
| 768 |
+
patch_size=vision_cfg.patch_size,
|
| 769 |
+
num_classes=embed_dim,
|
| 770 |
+
use_mean_pooling=vision_cfg.global_average_pool, # False
|
| 771 |
+
init_values=vision_cfg.ls_init_value,
|
| 772 |
+
patch_dropout=vision_cfg.patch_dropout,
|
| 773 |
+
embed_dim=vision_cfg.width,
|
| 774 |
+
depth=vision_cfg.layers,
|
| 775 |
+
num_heads=vision_heads,
|
| 776 |
+
mlp_ratio=vision_cfg.mlp_ratio,
|
| 777 |
+
qkv_bias=vision_cfg.qkv_bias,
|
| 778 |
+
drop_path_rate=vision_cfg.drop_path_rate,
|
| 779 |
+
norm_layer=norm_layer_factory,
|
| 780 |
+
xattn=vision_cfg.xattn,
|
| 781 |
+
rope=vision_cfg.rope,
|
| 782 |
+
postnorm=vision_cfg.postnorm,
|
| 783 |
+
pt_hw_seq_len=vision_cfg.pt_hw_seq_len, # 224/14
|
| 784 |
+
intp_freq=vision_cfg.intp_freq,
|
| 785 |
+
naiveswiglu=vision_cfg.naiveswiglu,
|
| 786 |
+
subln=vision_cfg.subln,
|
| 787 |
+
)
|
| 788 |
+
# breakpoint()
|
| 789 |
+
state_dict = load_clip_visual_state_dict(vision_tower_path)
|
| 790 |
+
incompatible_keys = visual.load_state_dict(state_dict, strict=False)
|
| 791 |
+
print("EVA-CLIP incompatible_keys:", incompatible_keys)
|
| 792 |
+
|
| 793 |
+
return visual
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
class EVAEncoderWrapper(nn.Module):
|
| 797 |
+
def __init__(self, vision_tower_pretrained, config):
|
| 798 |
+
super(EVAEncoderWrapper, self).__init__()
|
| 799 |
+
self.config = config
|
| 800 |
+
vision_tower_path = _resolve_vision_checkpoint_path(vision_tower_pretrained)
|
| 801 |
+
self.config["vision_tower_path"] = vision_tower_path
|
| 802 |
+
self.vision_tower_path = vision_tower_path
|
| 803 |
+
self.model = _build_vision_tower(**self.config)
|
| 804 |
+
|
| 805 |
+
def forward(self, image, **kwargs):
|
| 806 |
+
encode = self.model(image, return_all_features=True)[:, 1:, :] # remove the CLS token
|
| 807 |
+
return encode
|
| 808 |
+
|
| 809 |
+
@property
|
| 810 |
+
def dtype(self):
|
| 811 |
+
return list(self.parameters())[-1].dtype
|
| 812 |
+
|
| 813 |
+
@property
|
| 814 |
+
def device(self):
|
| 815 |
+
return list(self.parameters())[-1].device
|
legacy_eva_clip/factory.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
import os
|
| 4 |
+
import pathlib
|
| 5 |
+
import re
|
| 6 |
+
from copy import deepcopy
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Optional, Tuple, Union, Dict, Any
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
_MODEL_CONFIG_PATHS = [Path(__file__).parent / f"model_configs/"]
|
| 12 |
+
_MODEL_CONFIGS = {} # directory (model_name: config) of model architecture configs
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _natural_key(string_):
|
| 16 |
+
return [int(s) if s.isdigit() else s for s in re.split(r"(\d+)", string_.lower())]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _rescan_model_configs():
|
| 20 |
+
global _MODEL_CONFIGS
|
| 21 |
+
|
| 22 |
+
config_ext = (".json",)
|
| 23 |
+
config_files = []
|
| 24 |
+
for config_path in _MODEL_CONFIG_PATHS:
|
| 25 |
+
if config_path.is_file() and config_path.suffix in config_ext:
|
| 26 |
+
config_files.append(config_path)
|
| 27 |
+
elif config_path.is_dir():
|
| 28 |
+
for ext in config_ext:
|
| 29 |
+
config_files.extend(config_path.glob(f"*{ext}"))
|
| 30 |
+
for cf in config_files:
|
| 31 |
+
with open(cf, "r", encoding="utf8") as f:
|
| 32 |
+
model_cfg = json.load(f)
|
| 33 |
+
if all(a in model_cfg for a in ("embed_dim", "vision_cfg", "text_cfg")):
|
| 34 |
+
_MODEL_CONFIGS[cf.stem] = model_cfg
|
| 35 |
+
|
| 36 |
+
_MODEL_CONFIGS = dict(sorted(_MODEL_CONFIGS.items(), key=lambda x: _natural_key(x[0])))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
_rescan_model_configs() # initial populate of model config registry
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def list_models():
|
| 43 |
+
"""enumerate available model architectures based on config files"""
|
| 44 |
+
return list(_MODEL_CONFIGS.keys())
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def add_model_config(path):
|
| 48 |
+
"""add model config path or file and update registry"""
|
| 49 |
+
if not isinstance(path, Path):
|
| 50 |
+
path = Path(path)
|
| 51 |
+
_MODEL_CONFIG_PATHS.append(path)
|
| 52 |
+
_rescan_model_configs()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def get_model_config(model_name):
|
| 56 |
+
if model_name in _MODEL_CONFIGS:
|
| 57 |
+
return deepcopy(_MODEL_CONFIGS[model_name])
|
| 58 |
+
else:
|
| 59 |
+
return None
|
legacy_eva_clip/model_configs/EVA-CLIP-18B.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1536,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 48,
|
| 6 |
+
"width": 5120,
|
| 7 |
+
"head_width": 128,
|
| 8 |
+
"mlp_ratio": 5,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-18b-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"qkv_bias": false,
|
| 13 |
+
"xattn": true,
|
| 14 |
+
"postnorm": true,
|
| 15 |
+
"fusedLN": false,
|
| 16 |
+
"use_rms_norm": true
|
| 17 |
+
},
|
| 18 |
+
"text_cfg": {
|
| 19 |
+
"context_length": 77,
|
| 20 |
+
"vocab_size": 49408,
|
| 21 |
+
"width": 1280,
|
| 22 |
+
"heads": 20,
|
| 23 |
+
"layers": 32,
|
| 24 |
+
"xattn": false,
|
| 25 |
+
"fusedLN": false
|
| 26 |
+
}
|
| 27 |
+
}
|
legacy_eva_clip/model_configs/EVA-CLIP-8B-plus.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1280,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 448,
|
| 5 |
+
"layers": 32,
|
| 6 |
+
"width": 4096,
|
| 7 |
+
"head_width": 128,
|
| 8 |
+
"mlp_ratio": 5,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-8b-14-plus-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"qkv_bias": false,
|
| 13 |
+
"xattn": true,
|
| 14 |
+
"postnorm": false,
|
| 15 |
+
"fusedLN": false,
|
| 16 |
+
"use_rms_norm": true
|
| 17 |
+
},
|
| 18 |
+
"text_cfg": {
|
| 19 |
+
"context_length": 77,
|
| 20 |
+
"vocab_size": 49408,
|
| 21 |
+
"width": 1280,
|
| 22 |
+
"heads": 20,
|
| 23 |
+
"layers": 32,
|
| 24 |
+
"xattn": false,
|
| 25 |
+
"fusedLN": false
|
| 26 |
+
}
|
| 27 |
+
}
|
legacy_eva_clip/model_configs/EVA-CLIP-8B.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1280,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 32,
|
| 6 |
+
"width": 4096,
|
| 7 |
+
"head_width": 128,
|
| 8 |
+
"mlp_ratio": 5,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-8b-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"qkv_bias": false,
|
| 13 |
+
"xattn": true,
|
| 14 |
+
"postnorm": false,
|
| 15 |
+
"fusedLN": false,
|
| 16 |
+
"use_rms_norm": true
|
| 17 |
+
},
|
| 18 |
+
"text_cfg": {
|
| 19 |
+
"context_length": 77,
|
| 20 |
+
"vocab_size": 49408,
|
| 21 |
+
"width": 1280,
|
| 22 |
+
"heads": 20,
|
| 23 |
+
"layers": 32,
|
| 24 |
+
"xattn": false,
|
| 25 |
+
"fusedLN": false
|
| 26 |
+
}
|
| 27 |
+
}
|
legacy_eva_clip/model_configs/EVA01-CLIP-B-16.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 512,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 12,
|
| 6 |
+
"width": 768,
|
| 7 |
+
"patch_size": 16,
|
| 8 |
+
"eva_model_name": "eva-clip-b-16",
|
| 9 |
+
"ls_init_value": 0.1,
|
| 10 |
+
"drop_path_rate": 0.0
|
| 11 |
+
},
|
| 12 |
+
"text_cfg": {
|
| 13 |
+
"context_length": 77,
|
| 14 |
+
"vocab_size": 49408,
|
| 15 |
+
"width": 512,
|
| 16 |
+
"heads": 8,
|
| 17 |
+
"layers": 12
|
| 18 |
+
}
|
| 19 |
+
}
|
legacy_eva_clip/model_configs/EVA01-CLIP-g-14-plus.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 40,
|
| 6 |
+
"width": 1408,
|
| 7 |
+
"head_width": 88,
|
| 8 |
+
"mlp_ratio": 4.3637,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-g-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"fusedLN": true
|
| 14 |
+
},
|
| 15 |
+
"text_cfg": {
|
| 16 |
+
"context_length": 77,
|
| 17 |
+
"vocab_size": 49408,
|
| 18 |
+
"width": 1024,
|
| 19 |
+
"heads": 16,
|
| 20 |
+
"layers": 24,
|
| 21 |
+
"xattn": false,
|
| 22 |
+
"fusedLN": true
|
| 23 |
+
}
|
| 24 |
+
}
|
legacy_eva_clip/model_configs/EVA01-CLIP-g-14.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 40,
|
| 6 |
+
"width": 1408,
|
| 7 |
+
"head_width": 88,
|
| 8 |
+
"mlp_ratio": 4.3637,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-g-14-x",
|
| 11 |
+
"drop_path_rate": 0.4,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"fusedLN": true
|
| 14 |
+
},
|
| 15 |
+
"text_cfg": {
|
| 16 |
+
"context_length": 77,
|
| 17 |
+
"vocab_size": 49408,
|
| 18 |
+
"width": 768,
|
| 19 |
+
"heads": 12,
|
| 20 |
+
"layers": 12,
|
| 21 |
+
"xattn": false,
|
| 22 |
+
"fusedLN": true
|
| 23 |
+
}
|
| 24 |
+
}
|
legacy_eva_clip/model_configs/EVA02-CLIP-B-16.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 512,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 12,
|
| 6 |
+
"width": 768,
|
| 7 |
+
"head_width": 64,
|
| 8 |
+
"patch_size": 16,
|
| 9 |
+
"mlp_ratio": 2.6667,
|
| 10 |
+
"eva_model_name": "eva-clip-b-16-X",
|
| 11 |
+
"drop_path_rate": 0.0,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"fusedLN": true,
|
| 14 |
+
"rope": true,
|
| 15 |
+
"pt_hw_seq_len": 16,
|
| 16 |
+
"intp_freq": true,
|
| 17 |
+
"naiveswiglu": true,
|
| 18 |
+
"subln": true
|
| 19 |
+
},
|
| 20 |
+
"text_cfg": {
|
| 21 |
+
"context_length": 77,
|
| 22 |
+
"vocab_size": 49408,
|
| 23 |
+
"width": 512,
|
| 24 |
+
"heads": 8,
|
| 25 |
+
"layers": 12,
|
| 26 |
+
"xattn": true,
|
| 27 |
+
"fusedLN": true
|
| 28 |
+
}
|
| 29 |
+
}
|
legacy_eva_clip/model_configs/EVA02-CLIP-L-14-336.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 768,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 336,
|
| 5 |
+
"layers": 24,
|
| 6 |
+
"width": 1024,
|
| 7 |
+
"drop_path_rate": 0,
|
| 8 |
+
"head_width": 64,
|
| 9 |
+
"mlp_ratio": 2.6667,
|
| 10 |
+
"patch_size": 14,
|
| 11 |
+
"eva_model_name": "eva-clip-l-14-336",
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"fusedLN": true,
|
| 14 |
+
"rope": true,
|
| 15 |
+
"pt_hw_seq_len": 16,
|
| 16 |
+
"intp_freq": true,
|
| 17 |
+
"naiveswiglu": true,
|
| 18 |
+
"subln": true
|
| 19 |
+
},
|
| 20 |
+
"text_cfg": {
|
| 21 |
+
"context_length": 77,
|
| 22 |
+
"vocab_size": 49408,
|
| 23 |
+
"width": 768,
|
| 24 |
+
"heads": 12,
|
| 25 |
+
"layers": 12,
|
| 26 |
+
"xattn": false,
|
| 27 |
+
"fusedLN": true
|
| 28 |
+
}
|
| 29 |
+
}
|
legacy_eva_clip/model_configs/EVA02-CLIP-L-14.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 768,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 24,
|
| 6 |
+
"width": 1024,
|
| 7 |
+
"drop_path_rate": 0,
|
| 8 |
+
"head_width": 64,
|
| 9 |
+
"mlp_ratio": 2.6667,
|
| 10 |
+
"patch_size": 14,
|
| 11 |
+
"eva_model_name": "eva-clip-l-14",
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"fusedLN": true,
|
| 14 |
+
"rope": true,
|
| 15 |
+
"pt_hw_seq_len": 16,
|
| 16 |
+
"intp_freq": true,
|
| 17 |
+
"naiveswiglu": true,
|
| 18 |
+
"subln": true
|
| 19 |
+
},
|
| 20 |
+
"text_cfg": {
|
| 21 |
+
"context_length": 77,
|
| 22 |
+
"vocab_size": 49408,
|
| 23 |
+
"width": 768,
|
| 24 |
+
"heads": 12,
|
| 25 |
+
"layers": 12,
|
| 26 |
+
"xattn": false,
|
| 27 |
+
"fusedLN": true
|
| 28 |
+
}
|
| 29 |
+
}
|
legacy_eva_clip/model_configs/EVA02-CLIP-bigE-14-plus.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 64,
|
| 6 |
+
"width": 1792,
|
| 7 |
+
"head_width": 112,
|
| 8 |
+
"mlp_ratio": 8.571428571428571,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-4b-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"postnorm": true,
|
| 14 |
+
"fusedLN": true
|
| 15 |
+
},
|
| 16 |
+
"text_cfg": {
|
| 17 |
+
"context_length": 77,
|
| 18 |
+
"vocab_size": 49408,
|
| 19 |
+
"width": 1280,
|
| 20 |
+
"heads": 20,
|
| 21 |
+
"layers": 32,
|
| 22 |
+
"xattn": false,
|
| 23 |
+
"fusedLN": true
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
legacy_eva_clip/model_configs/EVA02-CLIP-bigE-14.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 64,
|
| 6 |
+
"width": 1792,
|
| 7 |
+
"head_width": 112,
|
| 8 |
+
"mlp_ratio": 8.571428571428571,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-4b-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"postnorm": true,
|
| 14 |
+
"fusedLN": true
|
| 15 |
+
},
|
| 16 |
+
"text_cfg": {
|
| 17 |
+
"context_length": 77,
|
| 18 |
+
"vocab_size": 49408,
|
| 19 |
+
"width": 1024,
|
| 20 |
+
"heads": 16,
|
| 21 |
+
"layers": 24,
|
| 22 |
+
"xattn": false,
|
| 23 |
+
"fusedLN": true
|
| 24 |
+
}
|
| 25 |
+
}
|
legacy_eva_clip/model_configs/Internal-EVA02-CLIP-10B-14-448.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 448,
|
| 5 |
+
"layers": 77,
|
| 6 |
+
"width": 2304,
|
| 7 |
+
"head_width": 144,
|
| 8 |
+
"mlp_ratio": 10.9722,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-10b-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"postnorm": false,
|
| 14 |
+
"fusedLN": true
|
| 15 |
+
},
|
| 16 |
+
"text_cfg": {
|
| 17 |
+
"context_length": 77,
|
| 18 |
+
"vocab_size": 49408,
|
| 19 |
+
"width": 1280,
|
| 20 |
+
"heads": 20,
|
| 21 |
+
"layers": 32,
|
| 22 |
+
"xattn": false,
|
| 23 |
+
"fusedLN": true
|
| 24 |
+
}
|
| 25 |
+
}
|
legacy_eva_clip/model_configs/Internal-EVA02-CLIP-10B-14.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"image_size": 224,
|
| 5 |
+
"layers": 77,
|
| 6 |
+
"width": 2304,
|
| 7 |
+
"head_width": 144,
|
| 8 |
+
"mlp_ratio": 10.9722,
|
| 9 |
+
"patch_size": 14,
|
| 10 |
+
"eva_model_name": "eva-clip-10b-14-x",
|
| 11 |
+
"drop_path_rate": 0,
|
| 12 |
+
"xattn": true,
|
| 13 |
+
"postnorm": false,
|
| 14 |
+
"fusedLN": true
|
| 15 |
+
},
|
| 16 |
+
"text_cfg": {
|
| 17 |
+
"context_length": 77,
|
| 18 |
+
"vocab_size": 49408,
|
| 19 |
+
"width": 1280,
|
| 20 |
+
"heads": 20,
|
| 21 |
+
"layers": 32,
|
| 22 |
+
"xattn": false,
|
| 23 |
+
"fusedLN": true
|
| 24 |
+
}
|
| 25 |
+
}
|
legacy_eva_clip/model_configs/eva-clip-E-14-plus.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"embed_dim": 1024,
|
| 3 |
+
"vision_cfg": {
|
| 4 |
+
"drop_path_rate": 0,
|
| 5 |
+
"eva_model_name": "eva-clip-E-14-plus",
|
| 6 |
+
"head_width": 112,
|
| 7 |
+
"image_size": 448,
|
| 8 |
+
"layers": 64,
|
| 9 |
+
"mlp_ratio": 8.571428571428571,
|
| 10 |
+
"patch_size": 14,
|
| 11 |
+
"postnorm": true,
|
| 12 |
+
"qkv_bias": true,
|
| 13 |
+
"width": 1792,
|
| 14 |
+
"xattn": true
|
| 15 |
+
},
|
| 16 |
+
"text_cfg": {
|
| 17 |
+
"context_length": 77,
|
| 18 |
+
"vocab_size": 49408,
|
| 19 |
+
"width": 1280,
|
| 20 |
+
"heads": 20,
|
| 21 |
+
"layers": 32,
|
| 22 |
+
"xattn": false,
|
| 23 |
+
"fusedLN": true
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
legacy_eva_clip/visual.py
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --------------------------------------------------------
|
| 2 |
+
# Adapted from https://github.com/microsoft/unilm/tree/master/beit
|
| 3 |
+
# --------------------------------------------------------
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from functools import partial
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
from torch.utils.checkpoint import checkpoint
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
from timm.models.layers import drop_path, to_2tuple
|
| 15 |
+
except:
|
| 16 |
+
from timm.layers import drop_path, to_2tuple
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
import xformers.ops as xops
|
| 20 |
+
except ImportError:
|
| 21 |
+
xops = None
|
| 22 |
+
print("Please 'pip install xformers'")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class PatchDropout(nn.Module):
|
| 26 |
+
"""
|
| 27 |
+
https://arxiv.org/abs/2212.00794
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
def __init__(self, prob, exclude_first_token=True):
|
| 31 |
+
super().__init__()
|
| 32 |
+
assert 0 <= prob < 1.
|
| 33 |
+
self.prob = prob
|
| 34 |
+
self.exclude_first_token = exclude_first_token # exclude CLS token
|
| 35 |
+
print(f"os.getenv('RoPE')={os.getenv('RoPE')}")
|
| 36 |
+
|
| 37 |
+
def forward(self, x):
|
| 38 |
+
if not self.training or self.prob == 0.:
|
| 39 |
+
return x
|
| 40 |
+
|
| 41 |
+
if self.exclude_first_token:
|
| 42 |
+
cls_tokens, x = x[:, :1], x[:, 1:]
|
| 43 |
+
else:
|
| 44 |
+
cls_tokens = torch.jit.annotate(torch.Tensor, x[:, :1])
|
| 45 |
+
|
| 46 |
+
batch = x.size()[0]
|
| 47 |
+
num_tokens = x.size()[1]
|
| 48 |
+
|
| 49 |
+
batch_indices = torch.arange(batch)
|
| 50 |
+
batch_indices = batch_indices[..., None]
|
| 51 |
+
|
| 52 |
+
keep_prob = 1 - self.prob
|
| 53 |
+
num_patches_keep = max(1, int(num_tokens * keep_prob))
|
| 54 |
+
|
| 55 |
+
rand = torch.randn(batch, num_tokens)
|
| 56 |
+
patch_indices_keep = rand.topk(num_patches_keep, dim=-1).indices
|
| 57 |
+
|
| 58 |
+
x = x[batch_indices, patch_indices_keep]
|
| 59 |
+
|
| 60 |
+
if self.exclude_first_token:
|
| 61 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 62 |
+
|
| 63 |
+
if self.training and os.getenv('RoPE') == '1':
|
| 64 |
+
return x, patch_indices_keep
|
| 65 |
+
|
| 66 |
+
return x
|
| 67 |
+
|
| 68 |
+
class DropPath(nn.Module):
|
| 69 |
+
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
|
| 70 |
+
"""
|
| 71 |
+
def __init__(self, drop_prob=None):
|
| 72 |
+
super(DropPath, self).__init__()
|
| 73 |
+
self.drop_prob = drop_prob
|
| 74 |
+
|
| 75 |
+
def forward(self, x):
|
| 76 |
+
return drop_path(x, self.drop_prob, self.training)
|
| 77 |
+
|
| 78 |
+
def extra_repr(self) -> str:
|
| 79 |
+
return 'p={}'.format(self.drop_prob)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class Mlp(nn.Module):
|
| 83 |
+
def __init__(
|
| 84 |
+
self,
|
| 85 |
+
in_features,
|
| 86 |
+
hidden_features=None,
|
| 87 |
+
out_features=None,
|
| 88 |
+
act_layer=nn.GELU,
|
| 89 |
+
norm_layer=nn.LayerNorm,
|
| 90 |
+
drop=0.,
|
| 91 |
+
subln=False,
|
| 92 |
+
|
| 93 |
+
):
|
| 94 |
+
super().__init__()
|
| 95 |
+
out_features = out_features or in_features
|
| 96 |
+
hidden_features = hidden_features or in_features
|
| 97 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
| 98 |
+
self.act = act_layer()
|
| 99 |
+
|
| 100 |
+
self.ffn_ln = norm_layer(hidden_features) if subln else nn.Identity()
|
| 101 |
+
|
| 102 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
| 103 |
+
self.drop = nn.Dropout(drop)
|
| 104 |
+
|
| 105 |
+
def forward(self, x):
|
| 106 |
+
x = self.fc1(x)
|
| 107 |
+
x = self.act(x)
|
| 108 |
+
# x = self.drop(x)
|
| 109 |
+
# commit this for the orignal BERT implement
|
| 110 |
+
x = self.ffn_ln(x)
|
| 111 |
+
|
| 112 |
+
x = self.fc2(x)
|
| 113 |
+
x = self.drop(x)
|
| 114 |
+
return x
|
| 115 |
+
|
| 116 |
+
class SwiGLU(nn.Module):
|
| 117 |
+
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.,
|
| 118 |
+
norm_layer=nn.LayerNorm, subln=False):
|
| 119 |
+
super().__init__()
|
| 120 |
+
out_features = out_features or in_features
|
| 121 |
+
hidden_features = hidden_features or in_features
|
| 122 |
+
|
| 123 |
+
self.w1 = nn.Linear(in_features, hidden_features)
|
| 124 |
+
self.w2 = nn.Linear(in_features, hidden_features)
|
| 125 |
+
|
| 126 |
+
self.act = act_layer()
|
| 127 |
+
self.ffn_ln = norm_layer(hidden_features) if subln else nn.Identity()
|
| 128 |
+
self.w3 = nn.Linear(hidden_features, out_features)
|
| 129 |
+
|
| 130 |
+
self.drop = nn.Dropout(drop)
|
| 131 |
+
|
| 132 |
+
def forward(self, x):
|
| 133 |
+
x1 = self.w1(x)
|
| 134 |
+
x2 = self.w2(x)
|
| 135 |
+
hidden = self.act(x1) * x2
|
| 136 |
+
x = self.ffn_ln(hidden)
|
| 137 |
+
x = self.w3(x)
|
| 138 |
+
x = self.drop(x)
|
| 139 |
+
return x
|
| 140 |
+
|
| 141 |
+
class Attention(nn.Module):
|
| 142 |
+
def __init__(
|
| 143 |
+
self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0.,
|
| 144 |
+
proj_drop=0., window_size=None, attn_head_dim=None, xattn=False, rope=None, subln=False, norm_layer=nn.LayerNorm):
|
| 145 |
+
super().__init__()
|
| 146 |
+
self.num_heads = num_heads
|
| 147 |
+
head_dim = dim // num_heads
|
| 148 |
+
if attn_head_dim is not None:
|
| 149 |
+
head_dim = attn_head_dim
|
| 150 |
+
all_head_dim = head_dim * self.num_heads
|
| 151 |
+
self.scale = qk_scale or head_dim ** -0.5
|
| 152 |
+
|
| 153 |
+
self.subln = subln
|
| 154 |
+
if self.subln:
|
| 155 |
+
self.q_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 156 |
+
self.k_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 157 |
+
self.v_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 158 |
+
else:
|
| 159 |
+
if qkv_bias:
|
| 160 |
+
self.qkv = nn.Linear(dim, all_head_dim * 3, bias=True)
|
| 161 |
+
else:
|
| 162 |
+
self.qkv = nn.Linear(dim, all_head_dim * 3, bias=False)
|
| 163 |
+
|
| 164 |
+
# if qkv_bias:
|
| 165 |
+
# self.q_bias = nn.Parameter(torch.zeros(all_head_dim))
|
| 166 |
+
# self.v_bias = nn.Parameter(torch.zeros(all_head_dim))
|
| 167 |
+
# else:
|
| 168 |
+
# self.q_bias = None
|
| 169 |
+
# self.v_bias = None
|
| 170 |
+
|
| 171 |
+
self.window_size = None
|
| 172 |
+
self.relative_position_bias_table = None
|
| 173 |
+
self.relative_position_index = None
|
| 174 |
+
|
| 175 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
| 176 |
+
self.inner_attn_ln = norm_layer(all_head_dim) if subln else nn.Identity()
|
| 177 |
+
# self.proj = nn.Linear(all_head_dim, all_head_dim)
|
| 178 |
+
self.proj = nn.Linear(all_head_dim, dim)
|
| 179 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
| 180 |
+
self.xattn = xattn
|
| 181 |
+
self.xattn_drop = attn_drop
|
| 182 |
+
|
| 183 |
+
self.rope = rope
|
| 184 |
+
|
| 185 |
+
def forward(self, x, rel_pos_bias=None, attn_mask=None):
|
| 186 |
+
B, N, C = x.shape
|
| 187 |
+
if self.subln:
|
| 188 |
+
q = F.linear(input=x, weight=self.q_proj.weight, bias=self.q_bias)
|
| 189 |
+
k = F.linear(input=x, weight=self.k_proj.weight, bias=None)
|
| 190 |
+
v = F.linear(input=x, weight=self.v_proj.weight, bias=self.v_bias)
|
| 191 |
+
|
| 192 |
+
q = q.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3) # B, num_heads, N, C
|
| 193 |
+
k = k.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3)
|
| 194 |
+
v = v.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3)
|
| 195 |
+
else:
|
| 196 |
+
|
| 197 |
+
# qkv_bias = None
|
| 198 |
+
# if self.q_bias is not None:
|
| 199 |
+
# qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias))
|
| 200 |
+
|
| 201 |
+
# qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias)
|
| 202 |
+
|
| 203 |
+
qkv = self.qkv(x)
|
| 204 |
+
|
| 205 |
+
qkv = qkv.reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) # 3, B, num_heads, N, C
|
| 206 |
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 207 |
+
|
| 208 |
+
if self.rope:
|
| 209 |
+
q_t = q[:, :, 1:, :]
|
| 210 |
+
ro_q_t = self.rope(q_t)
|
| 211 |
+
q = torch.cat((q[:, :, :1, :], ro_q_t), -2).type_as(v)
|
| 212 |
+
|
| 213 |
+
k_t = k[:, :, 1:, :]
|
| 214 |
+
ro_k_t = self.rope(k_t)
|
| 215 |
+
k = torch.cat((k[:, :, :1, :], ro_k_t), -2).type_as(v)
|
| 216 |
+
|
| 217 |
+
if self.xattn:
|
| 218 |
+
q = q.permute(0, 2, 1, 3) # B, num_heads, N, C -> B, N, num_heads, C
|
| 219 |
+
k = k.permute(0, 2, 1, 3)
|
| 220 |
+
v = v.permute(0, 2, 1, 3)
|
| 221 |
+
|
| 222 |
+
x = xops.memory_efficient_attention(
|
| 223 |
+
q, k, v,
|
| 224 |
+
p=self.xattn_drop,
|
| 225 |
+
scale=self.scale,
|
| 226 |
+
)
|
| 227 |
+
x = x.reshape(B, N, -1)
|
| 228 |
+
x = self.inner_attn_ln(x)
|
| 229 |
+
x = self.proj(x)
|
| 230 |
+
x = self.proj_drop(x)
|
| 231 |
+
else:
|
| 232 |
+
q = q * self.scale
|
| 233 |
+
attn = (q @ k.transpose(-2, -1))
|
| 234 |
+
|
| 235 |
+
if self.relative_position_bias_table is not None:
|
| 236 |
+
relative_position_bias = \
|
| 237 |
+
self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
|
| 238 |
+
self.window_size[0] * self.window_size[1] + 1,
|
| 239 |
+
self.window_size[0] * self.window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH
|
| 240 |
+
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
| 241 |
+
attn = attn + relative_position_bias.unsqueeze(0).type_as(attn)
|
| 242 |
+
|
| 243 |
+
if rel_pos_bias is not None:
|
| 244 |
+
attn = attn + rel_pos_bias.type_as(attn)
|
| 245 |
+
|
| 246 |
+
if attn_mask is not None:
|
| 247 |
+
attn_mask = attn_mask.bool()
|
| 248 |
+
attn = attn.masked_fill(~attn_mask[:, None, None, :], float("-inf"))
|
| 249 |
+
|
| 250 |
+
attn = attn.softmax(dim=-1)
|
| 251 |
+
attn = self.attn_drop(attn)
|
| 252 |
+
|
| 253 |
+
x = (attn @ v).transpose(1, 2).reshape(B, N, -1)
|
| 254 |
+
x = self.inner_attn_ln(x)
|
| 255 |
+
x = self.proj(x)
|
| 256 |
+
x = self.proj_drop(x)
|
| 257 |
+
return x
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
class Block(nn.Module):
|
| 261 |
+
|
| 262 |
+
def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
|
| 263 |
+
drop_path=0., init_values=None, act_layer=nn.GELU, norm_layer=nn.LayerNorm,
|
| 264 |
+
window_size=None, attn_head_dim=None, xattn=False, rope=None, postnorm=False,
|
| 265 |
+
subln=False, naiveswiglu=False):
|
| 266 |
+
super().__init__()
|
| 267 |
+
self.norm1 = norm_layer(dim)
|
| 268 |
+
self.attn = Attention(
|
| 269 |
+
dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale,
|
| 270 |
+
attn_drop=attn_drop, proj_drop=drop, window_size=window_size, attn_head_dim=attn_head_dim,
|
| 271 |
+
xattn=xattn, rope=rope, subln=subln, norm_layer=norm_layer)
|
| 272 |
+
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
|
| 273 |
+
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
| 274 |
+
self.norm2 = norm_layer(dim)
|
| 275 |
+
mlp_hidden_dim = int(dim * mlp_ratio)
|
| 276 |
+
|
| 277 |
+
if naiveswiglu:
|
| 278 |
+
self.mlp = SwiGLU(
|
| 279 |
+
in_features=dim,
|
| 280 |
+
hidden_features=mlp_hidden_dim,
|
| 281 |
+
subln=subln,
|
| 282 |
+
norm_layer=norm_layer,
|
| 283 |
+
)
|
| 284 |
+
else:
|
| 285 |
+
self.mlp = Mlp(
|
| 286 |
+
in_features=dim,
|
| 287 |
+
hidden_features=mlp_hidden_dim,
|
| 288 |
+
act_layer=act_layer,
|
| 289 |
+
subln=subln,
|
| 290 |
+
drop=drop
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
if init_values is not None and init_values > 0:
|
| 294 |
+
self.gamma_1 = nn.Parameter(init_values * torch.ones((dim)),requires_grad=True)
|
| 295 |
+
self.gamma_2 = nn.Parameter(init_values * torch.ones((dim)),requires_grad=True)
|
| 296 |
+
else:
|
| 297 |
+
self.gamma_1, self.gamma_2 = None, None
|
| 298 |
+
|
| 299 |
+
self.postnorm = postnorm
|
| 300 |
+
|
| 301 |
+
def forward(self, x, rel_pos_bias=None, attn_mask=None):
|
| 302 |
+
if self.gamma_1 is None:
|
| 303 |
+
if self.postnorm:
|
| 304 |
+
x = x + self.drop_path(self.norm1(self.attn(x, rel_pos_bias=rel_pos_bias, attn_mask=attn_mask)))
|
| 305 |
+
x = x + self.drop_path(self.norm2(self.mlp(x)))
|
| 306 |
+
else:
|
| 307 |
+
x = x + self.drop_path(self.attn(self.norm1(x), rel_pos_bias=rel_pos_bias, attn_mask=attn_mask))
|
| 308 |
+
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
| 309 |
+
else:
|
| 310 |
+
if self.postnorm:
|
| 311 |
+
x = x + self.drop_path(self.gamma_1 * self.norm1(self.attn(x, rel_pos_bias=rel_pos_bias, attn_mask=attn_mask)))
|
| 312 |
+
x = x + self.drop_path(self.gamma_2 * self.norm2(self.mlp(x)))
|
| 313 |
+
else:
|
| 314 |
+
x = x + self.drop_path(self.gamma_1 * self.attn(self.norm1(x), rel_pos_bias=rel_pos_bias, attn_mask=attn_mask))
|
| 315 |
+
x = x + self.drop_path(self.gamma_2 * self.mlp(self.norm2(x)))
|
| 316 |
+
return x
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
class PatchEmbed(nn.Module):
|
| 320 |
+
""" Image to Patch Embedding
|
| 321 |
+
"""
|
| 322 |
+
def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768):
|
| 323 |
+
super().__init__()
|
| 324 |
+
img_size = to_2tuple(img_size)
|
| 325 |
+
patch_size = to_2tuple(patch_size)
|
| 326 |
+
num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0])
|
| 327 |
+
self.patch_shape = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
|
| 328 |
+
self.img_size = img_size
|
| 329 |
+
self.patch_size = patch_size
|
| 330 |
+
self.num_patches = num_patches
|
| 331 |
+
|
| 332 |
+
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
|
| 333 |
+
|
| 334 |
+
def forward(self, x, **kwargs):
|
| 335 |
+
B, C, H, W = x.shape
|
| 336 |
+
# FIXME look at relaxing size constraints
|
| 337 |
+
assert H == self.img_size[0] and W == self.img_size[1], \
|
| 338 |
+
f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
|
| 339 |
+
x = self.proj(x).flatten(2).transpose(1, 2)
|
| 340 |
+
return x
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
class EVAVisionTransformer(nn.Module):
|
| 344 |
+
""" Vision Transformer with support for patch or hybrid CNN input stage
|
| 345 |
+
"""
|
| 346 |
+
def __init__(self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, embed_dim=768, depth=12,
|
| 347 |
+
num_heads=12, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop_rate=0., attn_drop_rate=0.,
|
| 348 |
+
drop_path_rate=0., norm_layer=nn.LayerNorm, init_values=None, patch_dropout=0.,
|
| 349 |
+
use_abs_pos_emb=True, use_rel_pos_bias=False, use_shared_rel_pos_bias=False, rope=False,
|
| 350 |
+
use_mean_pooling=True, init_scale=0.001, grad_checkpointing=False, xattn=False, postnorm=False,
|
| 351 |
+
pt_hw_seq_len=16, intp_freq=False, naiveswiglu=False, subln=False,
|
| 352 |
+
):
|
| 353 |
+
super().__init__()
|
| 354 |
+
self.image_size = img_size
|
| 355 |
+
# self.num_classes = num_classes
|
| 356 |
+
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
|
| 357 |
+
|
| 358 |
+
self.patch_embed = PatchEmbed(
|
| 359 |
+
img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
|
| 360 |
+
num_patches = self.patch_embed.num_patches
|
| 361 |
+
|
| 362 |
+
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
|
| 363 |
+
if use_abs_pos_emb:
|
| 364 |
+
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + 1, embed_dim))
|
| 365 |
+
else:
|
| 366 |
+
self.pos_embed = None
|
| 367 |
+
self.pos_drop = nn.Dropout(p=drop_rate)
|
| 368 |
+
|
| 369 |
+
self.rel_pos_bias = None
|
| 370 |
+
self.rope = None
|
| 371 |
+
|
| 372 |
+
self.naiveswiglu = naiveswiglu
|
| 373 |
+
|
| 374 |
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
|
| 375 |
+
self.use_rel_pos_bias = use_rel_pos_bias
|
| 376 |
+
self.blocks = nn.ModuleList([
|
| 377 |
+
Block(
|
| 378 |
+
dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
|
| 379 |
+
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer,
|
| 380 |
+
init_values=init_values, window_size=self.patch_embed.patch_shape if use_rel_pos_bias else None,
|
| 381 |
+
xattn=xattn, rope=self.rope, postnorm=postnorm, subln=subln, naiveswiglu=naiveswiglu)
|
| 382 |
+
for i in range(depth)])
|
| 383 |
+
|
| 384 |
+
# setting a patch_dropout of 0. would mean it is disabled and this function would be the identity fn
|
| 385 |
+
self.patch_dropout = PatchDropout(patch_dropout) if patch_dropout > 0. else nn.Identity()
|
| 386 |
+
|
| 387 |
+
self.grad_checkpointing = grad_checkpointing
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def get_num_layers(self):
|
| 391 |
+
return len(self.blocks)
|
| 392 |
+
|
| 393 |
+
def lock(self, unlocked_groups=0, freeze_bn_stats=False):
|
| 394 |
+
assert unlocked_groups == 0, 'partial locking not currently supported for this model'
|
| 395 |
+
for param in self.parameters():
|
| 396 |
+
param.requires_grad = False
|
| 397 |
+
|
| 398 |
+
@torch.jit.ignore
|
| 399 |
+
def set_grad_checkpointing(self, enable=True):
|
| 400 |
+
self.grad_checkpointing = enable
|
| 401 |
+
|
| 402 |
+
@torch.jit.ignore
|
| 403 |
+
def no_weight_decay(self):
|
| 404 |
+
return {'pos_embed', 'cls_token'}
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def forward_features(self, x):
|
| 408 |
+
x = self.patch_embed(x)
|
| 409 |
+
batch_size, seq_len, _ = x.size()
|
| 410 |
+
|
| 411 |
+
cls_tokens = self.cls_token.expand(batch_size, -1, -1) # stole cls_tokens impl from Phil Wang, thanks
|
| 412 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 413 |
+
if self.pos_embed is not None:
|
| 414 |
+
x = x + self.pos_embed
|
| 415 |
+
x = self.pos_drop(x)
|
| 416 |
+
|
| 417 |
+
# a patch_dropout of 0. would mean it is disabled and this function would do nothing but return what was passed in
|
| 418 |
+
if os.getenv('RoPE') == '1':
|
| 419 |
+
if self.training and not isinstance(self.patch_dropout, nn.Identity):
|
| 420 |
+
x, patch_indices_keep = self.patch_dropout(x)
|
| 421 |
+
self.rope.forward = partial(self.rope.forward, patch_indices_keep=patch_indices_keep)
|
| 422 |
+
else:
|
| 423 |
+
self.rope.forward = partial(self.rope.forward, patch_indices_keep=None)
|
| 424 |
+
x = self.patch_dropout(x)
|
| 425 |
+
else:
|
| 426 |
+
x = self.patch_dropout(x)
|
| 427 |
+
|
| 428 |
+
rel_pos_bias = None
|
| 429 |
+
|
| 430 |
+
for blk in self.blocks:
|
| 431 |
+
if self.grad_checkpointing:
|
| 432 |
+
x = checkpoint(blk, x, (rel_pos_bias,))
|
| 433 |
+
else:
|
| 434 |
+
x = blk(x, rel_pos_bias=rel_pos_bias)
|
| 435 |
+
|
| 436 |
+
return x
|
| 437 |
+
|
| 438 |
+
def forward(self, x):
|
| 439 |
+
|
| 440 |
+
"""
|
| 441 |
+
:return:
|
| 442 |
+
forward_features function returns raw features of ViT,
|
| 443 |
+
forward with return_all_features returns normalized features of ViT
|
| 444 |
+
:param x:
|
| 445 |
+
:param return_all_features:
|
| 446 |
+
"""
|
| 447 |
+
|
| 448 |
+
features = self.forward_features(x) # [B, n_patch, C]
|
| 449 |
+
|
| 450 |
+
return features
|
legacy_eva_vit.py
ADDED
|
@@ -0,0 +1,815 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
# Adapted from https://github.com/baaivision/EVA/tree/master/EVA-CLIP
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from math import pi
|
| 6 |
+
import os
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Optional
|
| 9 |
+
import importlib.util
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
from torch import nn
|
| 13 |
+
from einops import rearrange, repeat
|
| 14 |
+
import logging
|
| 15 |
+
|
| 16 |
+
_hub_spec = importlib.util.find_spec("huggingface_hub")
|
| 17 |
+
if _hub_spec is not None:
|
| 18 |
+
from huggingface_hub import snapshot_download # type: ignore
|
| 19 |
+
else: # pragma: no cover - optional dependency
|
| 20 |
+
snapshot_download = None
|
| 21 |
+
|
| 22 |
+
_DEFAULT_EVA_REPO = "jiuhai/eva_clip_vision_tower"
|
| 23 |
+
XFORMERS_AVAILABLE = False # populated later once xops import resolves
|
| 24 |
+
_XFORMERS_WARNING_EMITTED = False
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _resolve_vision_checkpoint_path(vision_tower_pretrained: Optional[str]) -> str:
|
| 28 |
+
"""Determine where to load the EVA visual tower weights from."""
|
| 29 |
+
|
| 30 |
+
candidate_files: list[Path] = []
|
| 31 |
+
if vision_tower_pretrained:
|
| 32 |
+
supplied = Path(vision_tower_pretrained)
|
| 33 |
+
if supplied.is_file():
|
| 34 |
+
candidate_files.append(supplied)
|
| 35 |
+
elif supplied.is_dir():
|
| 36 |
+
for filename in (
|
| 37 |
+
"pytorch_model.bin",
|
| 38 |
+
"model.safetensors",
|
| 39 |
+
"visual.pth",
|
| 40 |
+
"visual.bin",
|
| 41 |
+
):
|
| 42 |
+
candidate = supplied / filename
|
| 43 |
+
if candidate.exists():
|
| 44 |
+
candidate_files.append(candidate)
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
if candidate_files:
|
| 48 |
+
return str(candidate_files[0])
|
| 49 |
+
|
| 50 |
+
if snapshot_download is None:
|
| 51 |
+
raise FileNotFoundError(
|
| 52 |
+
"EVA vision weights not found locally and huggingface_hub is unavailable. "
|
| 53 |
+
"Provide --vision-tower-path pointing to a directory containing the checkpoint."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
cache_dir = Path(snapshot_download(repo_id=_DEFAULT_EVA_REPO))
|
| 57 |
+
default_file = cache_dir / "pytorch_model.bin"
|
| 58 |
+
if not default_file.exists():
|
| 59 |
+
raise FileNotFoundError(f"Default EVA checkpoint not found at {default_file}.")
|
| 60 |
+
return str(default_file)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def broadcat(tensors, dim=-1):
|
| 64 |
+
num_tensors = len(tensors)
|
| 65 |
+
shape_lens = set(list(map(lambda t: len(t.shape), tensors)))
|
| 66 |
+
assert len(shape_lens) == 1, "tensors must all have the same number of dimensions"
|
| 67 |
+
shape_len = list(shape_lens)[0]
|
| 68 |
+
dim = (dim + shape_len) if dim < 0 else dim
|
| 69 |
+
dims = list(zip(*map(lambda t: list(t.shape), tensors)))
|
| 70 |
+
expandable_dims = [(i, val) for i, val in enumerate(dims) if i != dim]
|
| 71 |
+
assert all([*map(lambda t: len(set(t[1])) <= 2, expandable_dims)]), "invalid dimensions for broadcastable concatentation"
|
| 72 |
+
max_dims = list(map(lambda t: (t[0], max(t[1])), expandable_dims))
|
| 73 |
+
expanded_dims = list(map(lambda t: (t[0], (t[1],) * num_tensors), max_dims))
|
| 74 |
+
expanded_dims.insert(dim, (dim, dims[dim]))
|
| 75 |
+
expandable_shapes = list(zip(*map(lambda t: t[1], expanded_dims)))
|
| 76 |
+
tensors = list(map(lambda t: t[0].expand(*t[1]), zip(tensors, expandable_shapes)))
|
| 77 |
+
return torch.cat(tensors, dim=dim)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def rotate_half(x):
|
| 81 |
+
x = rearrange(x, "... (d r) -> ... d r", r=2)
|
| 82 |
+
x1, x2 = x.unbind(dim=-1)
|
| 83 |
+
x = torch.stack((-x2, x1), dim=-1)
|
| 84 |
+
return rearrange(x, "... d r -> ... (d r)")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class VisionRotaryEmbeddingFast(nn.Module):
|
| 88 |
+
def __init__(self, dim, pt_seq_len, ft_seq_len=None, custom_freqs=None, freqs_for="lang", theta=10000, max_freq=10, num_freqs=1, patch_dropout=0.0):
|
| 89 |
+
super().__init__()
|
| 90 |
+
if custom_freqs:
|
| 91 |
+
freqs = custom_freqs
|
| 92 |
+
elif freqs_for == "lang":
|
| 93 |
+
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
|
| 94 |
+
elif freqs_for == "pixel":
|
| 95 |
+
freqs = torch.linspace(1.0, max_freq / 2, dim // 2) * pi
|
| 96 |
+
elif freqs_for == "constant":
|
| 97 |
+
freqs = torch.ones(num_freqs).float()
|
| 98 |
+
else:
|
| 99 |
+
raise ValueError(f"unknown modality {freqs_for}")
|
| 100 |
+
|
| 101 |
+
if ft_seq_len is None:
|
| 102 |
+
ft_seq_len = pt_seq_len
|
| 103 |
+
t = torch.arange(ft_seq_len) / ft_seq_len * pt_seq_len
|
| 104 |
+
|
| 105 |
+
freqs = torch.einsum("..., f -> ... f", t, freqs)
|
| 106 |
+
freqs = repeat(freqs, "... n -> ... (n r)", r=2)
|
| 107 |
+
freqs = broadcat((freqs[:, None, :], freqs[None, :, :]), dim=-1)
|
| 108 |
+
|
| 109 |
+
freqs_cos = freqs.cos().view(-1, freqs.shape[-1])
|
| 110 |
+
freqs_sin = freqs.sin().view(-1, freqs.shape[-1])
|
| 111 |
+
|
| 112 |
+
self.patch_dropout = patch_dropout
|
| 113 |
+
|
| 114 |
+
self.register_buffer("freqs_cos", freqs_cos)
|
| 115 |
+
self.register_buffer("freqs_sin", freqs_sin)
|
| 116 |
+
|
| 117 |
+
logging.info(f"Shape of rope freq: {self.freqs_cos.shape}")
|
| 118 |
+
|
| 119 |
+
def forward(self, t, patch_indices_keep=None):
|
| 120 |
+
if patch_indices_keep is not None:
|
| 121 |
+
batch = t.size()[0]
|
| 122 |
+
batch_indices = torch.arange(batch)
|
| 123 |
+
batch_indices = batch_indices[..., None]
|
| 124 |
+
|
| 125 |
+
freqs_cos = repeat(self.freqs_cos, "i j -> n i m j", n=t.shape[0], m=t.shape[1])
|
| 126 |
+
freqs_sin = repeat(self.freqs_sin, "i j -> n i m j", n=t.shape[0], m=t.shape[1])
|
| 127 |
+
|
| 128 |
+
freqs_cos = freqs_cos[batch_indices, patch_indices_keep]
|
| 129 |
+
freqs_cos = rearrange(freqs_cos, "n i m j -> n m i j")
|
| 130 |
+
freqs_sin = freqs_sin[batch_indices, patch_indices_keep]
|
| 131 |
+
freqs_sin = rearrange(freqs_sin, "n i m j -> n m i j")
|
| 132 |
+
|
| 133 |
+
return t * freqs_cos + rotate_half(t) * freqs_sin
|
| 134 |
+
|
| 135 |
+
return t * self.freqs_cos + rotate_half(t) * self.freqs_sin
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class LayerNorm(nn.LayerNorm):
|
| 139 |
+
"""Subclass torch's LayerNorm (with cast back to input dtype)."""
|
| 140 |
+
|
| 141 |
+
def forward(self, x: torch.Tensor):
|
| 142 |
+
orig_type = x.dtype
|
| 143 |
+
x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
|
| 144 |
+
return x.to(orig_type)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
class PatchDropout(nn.Module):
|
| 148 |
+
"""
|
| 149 |
+
https://arxiv.org/abs/2212.00794
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
def __init__(self, prob, exclude_first_token=True):
|
| 153 |
+
super().__init__()
|
| 154 |
+
assert 0 <= prob < 1.
|
| 155 |
+
self.prob = prob
|
| 156 |
+
self.exclude_first_token = exclude_first_token # exclude CLS token
|
| 157 |
+
print(f"os.getenv('RoPE')={os.getenv('RoPE')}")
|
| 158 |
+
|
| 159 |
+
def forward(self, x):
|
| 160 |
+
if not self.training or self.prob == 0.:
|
| 161 |
+
return x
|
| 162 |
+
|
| 163 |
+
if self.exclude_first_token:
|
| 164 |
+
cls_tokens, x = x[:, :1], x[:, 1:]
|
| 165 |
+
else:
|
| 166 |
+
cls_tokens = torch.jit.annotate(torch.Tensor, x[:, :1])
|
| 167 |
+
|
| 168 |
+
batch = x.size()[0]
|
| 169 |
+
num_tokens = x.size()[1]
|
| 170 |
+
|
| 171 |
+
batch_indices = torch.arange(batch)
|
| 172 |
+
batch_indices = batch_indices[..., None]
|
| 173 |
+
|
| 174 |
+
keep_prob = 1 - self.prob
|
| 175 |
+
num_patches_keep = max(1, int(num_tokens * keep_prob))
|
| 176 |
+
|
| 177 |
+
rand = torch.randn(batch, num_tokens)
|
| 178 |
+
patch_indices_keep = rand.topk(num_patches_keep, dim=-1).indices
|
| 179 |
+
|
| 180 |
+
x = x[batch_indices, patch_indices_keep]
|
| 181 |
+
|
| 182 |
+
if self.exclude_first_token:
|
| 183 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 184 |
+
|
| 185 |
+
if self.training and os.getenv('RoPE') == '1':
|
| 186 |
+
return x, patch_indices_keep
|
| 187 |
+
|
| 188 |
+
return x
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
# --------------------------------------------------------
|
| 192 |
+
# Adapted from https://github.com/microsoft/unilm/tree/master/beit
|
| 193 |
+
# --------------------------------------------------------
|
| 194 |
+
import math
|
| 195 |
+
import os
|
| 196 |
+
import torch.nn as nn
|
| 197 |
+
import torch.nn.functional as F
|
| 198 |
+
|
| 199 |
+
try:
|
| 200 |
+
from timm.models.layers import drop_path, to_2tuple, trunc_normal_
|
| 201 |
+
except:
|
| 202 |
+
from timm.layers import drop_path, to_2tuple, trunc_normal_
|
| 203 |
+
|
| 204 |
+
if os.getenv("ENV_TYPE") == "deepspeed":
|
| 205 |
+
try:
|
| 206 |
+
from deepspeed.runtime.activation_checkpointing.checkpointing import checkpoint
|
| 207 |
+
except:
|
| 208 |
+
from torch.utils.checkpoint import checkpoint
|
| 209 |
+
else:
|
| 210 |
+
from torch.utils.checkpoint import checkpoint
|
| 211 |
+
|
| 212 |
+
try:
|
| 213 |
+
import xformers.ops as xops
|
| 214 |
+
XFORMERS_AVAILABLE = True
|
| 215 |
+
except ImportError:
|
| 216 |
+
xops = None
|
| 217 |
+
# print("Please 'pip install xformers'")
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
class DropPath(nn.Module):
|
| 221 |
+
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
|
| 222 |
+
"""
|
| 223 |
+
def __init__(self, drop_prob=None):
|
| 224 |
+
super(DropPath, self).__init__()
|
| 225 |
+
self.drop_prob = drop_prob
|
| 226 |
+
|
| 227 |
+
def forward(self, x):
|
| 228 |
+
return drop_path(x, self.drop_prob, self.training)
|
| 229 |
+
|
| 230 |
+
def extra_repr(self) -> str:
|
| 231 |
+
return 'p={}'.format(self.drop_prob)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class Mlp(nn.Module):
|
| 235 |
+
def __init__(
|
| 236 |
+
self,
|
| 237 |
+
in_features,
|
| 238 |
+
hidden_features=None,
|
| 239 |
+
out_features=None,
|
| 240 |
+
act_layer=nn.GELU,
|
| 241 |
+
norm_layer=nn.LayerNorm,
|
| 242 |
+
drop=0.,
|
| 243 |
+
subln=False,
|
| 244 |
+
|
| 245 |
+
):
|
| 246 |
+
super().__init__()
|
| 247 |
+
out_features = out_features or in_features
|
| 248 |
+
hidden_features = hidden_features or in_features
|
| 249 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
| 250 |
+
self.act = act_layer()
|
| 251 |
+
|
| 252 |
+
self.ffn_ln = norm_layer(hidden_features) if subln else nn.Identity()
|
| 253 |
+
|
| 254 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
| 255 |
+
self.drop = nn.Dropout(drop)
|
| 256 |
+
|
| 257 |
+
def forward(self, x):
|
| 258 |
+
x = self.fc1(x)
|
| 259 |
+
x = self.act(x)
|
| 260 |
+
# x = self.drop(x)
|
| 261 |
+
# commit this for the orignal BERT implement
|
| 262 |
+
x = self.ffn_ln(x)
|
| 263 |
+
|
| 264 |
+
x = self.fc2(x)
|
| 265 |
+
x = self.drop(x)
|
| 266 |
+
return x
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
class SwiGLU(nn.Module):
|
| 270 |
+
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.,
|
| 271 |
+
norm_layer=nn.LayerNorm, subln=False):
|
| 272 |
+
super().__init__()
|
| 273 |
+
out_features = out_features or in_features
|
| 274 |
+
hidden_features = hidden_features or in_features
|
| 275 |
+
|
| 276 |
+
self.w1 = nn.Linear(in_features, hidden_features)
|
| 277 |
+
self.w2 = nn.Linear(in_features, hidden_features)
|
| 278 |
+
|
| 279 |
+
self.act = act_layer()
|
| 280 |
+
self.ffn_ln = norm_layer(hidden_features) if subln else nn.Identity()
|
| 281 |
+
self.w3 = nn.Linear(hidden_features, out_features)
|
| 282 |
+
|
| 283 |
+
self.drop = nn.Dropout(drop)
|
| 284 |
+
|
| 285 |
+
def forward(self, x):
|
| 286 |
+
x1 = self.w1(x)
|
| 287 |
+
x2 = self.w2(x)
|
| 288 |
+
hidden = self.act(x1) * x2
|
| 289 |
+
x = self.ffn_ln(hidden)
|
| 290 |
+
x = self.w3(x)
|
| 291 |
+
x = self.drop(x)
|
| 292 |
+
return x
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
class Attention(nn.Module):
|
| 296 |
+
def __init__(
|
| 297 |
+
self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0.,
|
| 298 |
+
proj_drop=0., window_size=None, attn_head_dim=None, xattn=False, rope=None, subln=False, norm_layer=nn.LayerNorm):
|
| 299 |
+
super().__init__()
|
| 300 |
+
self.num_heads = num_heads
|
| 301 |
+
head_dim = dim // num_heads
|
| 302 |
+
if attn_head_dim is not None:
|
| 303 |
+
head_dim = attn_head_dim
|
| 304 |
+
all_head_dim = head_dim * self.num_heads
|
| 305 |
+
self.scale = qk_scale or head_dim ** -0.5
|
| 306 |
+
|
| 307 |
+
self.subln = subln
|
| 308 |
+
if self.subln:
|
| 309 |
+
self.q_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 310 |
+
self.k_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 311 |
+
self.v_proj = nn.Linear(dim, all_head_dim, bias=False)
|
| 312 |
+
else:
|
| 313 |
+
if qkv_bias:
|
| 314 |
+
self.qkv = nn.Linear(dim, all_head_dim * 3, bias=True)
|
| 315 |
+
else:
|
| 316 |
+
self.qkv = nn.Linear(dim, all_head_dim * 3, bias=False)
|
| 317 |
+
|
| 318 |
+
# if qkv_bias:
|
| 319 |
+
# self.q_bias = nn.Parameter(torch.zeros(all_head_dim))
|
| 320 |
+
# self.v_bias = nn.Parameter(torch.zeros(all_head_dim))
|
| 321 |
+
# else:
|
| 322 |
+
# self.q_bias = None
|
| 323 |
+
# self.v_bias = None
|
| 324 |
+
|
| 325 |
+
self.window_size = None
|
| 326 |
+
self.relative_position_bias_table = None
|
| 327 |
+
self.relative_position_index = None
|
| 328 |
+
|
| 329 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
| 330 |
+
self.inner_attn_ln = norm_layer(all_head_dim) if subln else nn.Identity()
|
| 331 |
+
# self.proj = nn.Linear(all_head_dim, all_head_dim)
|
| 332 |
+
self.proj = nn.Linear(all_head_dim, dim)
|
| 333 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
| 334 |
+
self.xattn = xattn and XFORMERS_AVAILABLE
|
| 335 |
+
self.xattn_drop = attn_drop
|
| 336 |
+
global _XFORMERS_WARNING_EMITTED
|
| 337 |
+
if xattn and not self.xattn and not _XFORMERS_WARNING_EMITTED:
|
| 338 |
+
logging.warning("xformers.ops not available; falling back to standard attention for EVA-CLIP vision tower.")
|
| 339 |
+
_XFORMERS_WARNING_EMITTED = True
|
| 340 |
+
|
| 341 |
+
self.rope = rope
|
| 342 |
+
|
| 343 |
+
def forward(self, x, rel_pos_bias=None, attn_mask=None):
|
| 344 |
+
B, N, C = x.shape
|
| 345 |
+
if self.subln:
|
| 346 |
+
q = F.linear(input=x, weight=self.q_proj.weight, bias=self.q_bias)
|
| 347 |
+
k = F.linear(input=x, weight=self.k_proj.weight, bias=None)
|
| 348 |
+
v = F.linear(input=x, weight=self.v_proj.weight, bias=self.v_bias)
|
| 349 |
+
|
| 350 |
+
q = q.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3) # B, num_heads, N, C
|
| 351 |
+
k = k.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3)
|
| 352 |
+
v = v.reshape(B, N, self.num_heads, -1).permute(0, 2, 1, 3)
|
| 353 |
+
else:
|
| 354 |
+
|
| 355 |
+
# qkv_bias = None
|
| 356 |
+
# if self.q_bias is not None:
|
| 357 |
+
# qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias))
|
| 358 |
+
|
| 359 |
+
# qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias)
|
| 360 |
+
|
| 361 |
+
qkv = self.qkv(x)
|
| 362 |
+
|
| 363 |
+
qkv = qkv.reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) # 3, B, num_heads, N, C
|
| 364 |
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 365 |
+
|
| 366 |
+
if self.rope:
|
| 367 |
+
q_t = q[:, :, 1:, :]
|
| 368 |
+
ro_q_t = self.rope(q_t)
|
| 369 |
+
q = torch.cat((q[:, :, :1, :], ro_q_t), -2).type_as(v)
|
| 370 |
+
|
| 371 |
+
k_t = k[:, :, 1:, :]
|
| 372 |
+
ro_k_t = self.rope(k_t)
|
| 373 |
+
k = torch.cat((k[:, :, :1, :], ro_k_t), -2).type_as(v)
|
| 374 |
+
|
| 375 |
+
if self.xattn:
|
| 376 |
+
q = q.permute(0, 2, 1, 3) # B, num_heads, N, C -> B, N, num_heads, C
|
| 377 |
+
k = k.permute(0, 2, 1, 3)
|
| 378 |
+
v = v.permute(0, 2, 1, 3)
|
| 379 |
+
|
| 380 |
+
x = xops.memory_efficient_attention(
|
| 381 |
+
q, k, v,
|
| 382 |
+
p=self.xattn_drop,
|
| 383 |
+
scale=self.scale,
|
| 384 |
+
)
|
| 385 |
+
x = x.reshape(B, N, -1)
|
| 386 |
+
x = self.inner_attn_ln(x)
|
| 387 |
+
x = self.proj(x)
|
| 388 |
+
x = self.proj_drop(x)
|
| 389 |
+
else:
|
| 390 |
+
q = q * self.scale
|
| 391 |
+
attn = (q @ k.transpose(-2, -1))
|
| 392 |
+
|
| 393 |
+
if self.relative_position_bias_table is not None:
|
| 394 |
+
relative_position_bias = \
|
| 395 |
+
self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
|
| 396 |
+
self.window_size[0] * self.window_size[1] + 1,
|
| 397 |
+
self.window_size[0] * self.window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH
|
| 398 |
+
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
| 399 |
+
attn = attn + relative_position_bias.unsqueeze(0).type_as(attn)
|
| 400 |
+
|
| 401 |
+
if rel_pos_bias is not None:
|
| 402 |
+
attn = attn + rel_pos_bias.type_as(attn)
|
| 403 |
+
|
| 404 |
+
if attn_mask is not None:
|
| 405 |
+
attn_mask = attn_mask.bool()
|
| 406 |
+
attn = attn.masked_fill(~attn_mask[:, None, None, :], float("-inf"))
|
| 407 |
+
|
| 408 |
+
attn = attn.softmax(dim=-1)
|
| 409 |
+
attn = self.attn_drop(attn)
|
| 410 |
+
|
| 411 |
+
x = (attn @ v).transpose(1, 2).reshape(B, N, -1)
|
| 412 |
+
x = self.inner_attn_ln(x)
|
| 413 |
+
x = self.proj(x)
|
| 414 |
+
x = self.proj_drop(x)
|
| 415 |
+
return x
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
class Block(nn.Module):
|
| 419 |
+
|
| 420 |
+
def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
|
| 421 |
+
drop_path=0., init_values=None, act_layer=nn.GELU, norm_layer=nn.LayerNorm,
|
| 422 |
+
window_size=None, attn_head_dim=None, xattn=False, rope=None, postnorm=False,
|
| 423 |
+
subln=False, naiveswiglu=False):
|
| 424 |
+
super().__init__()
|
| 425 |
+
self.norm1 = norm_layer(dim)
|
| 426 |
+
self.attn = Attention(
|
| 427 |
+
dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale,
|
| 428 |
+
attn_drop=attn_drop, proj_drop=drop, window_size=window_size, attn_head_dim=attn_head_dim,
|
| 429 |
+
xattn=xattn, rope=rope, subln=subln, norm_layer=norm_layer)
|
| 430 |
+
# NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
|
| 431 |
+
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
| 432 |
+
self.norm2 = norm_layer(dim)
|
| 433 |
+
mlp_hidden_dim = int(dim * mlp_ratio)
|
| 434 |
+
|
| 435 |
+
if naiveswiglu:
|
| 436 |
+
self.mlp = SwiGLU(
|
| 437 |
+
in_features=dim,
|
| 438 |
+
hidden_features=mlp_hidden_dim,
|
| 439 |
+
subln=subln,
|
| 440 |
+
norm_layer=norm_layer,
|
| 441 |
+
)
|
| 442 |
+
else:
|
| 443 |
+
self.mlp = Mlp(
|
| 444 |
+
in_features=dim,
|
| 445 |
+
hidden_features=mlp_hidden_dim,
|
| 446 |
+
act_layer=act_layer,
|
| 447 |
+
subln=subln,
|
| 448 |
+
drop=drop
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
+
if init_values is not None and init_values > 0:
|
| 452 |
+
self.gamma_1 = nn.Parameter(init_values * torch.ones((dim)),requires_grad=True)
|
| 453 |
+
self.gamma_2 = nn.Parameter(init_values * torch.ones((dim)),requires_grad=True)
|
| 454 |
+
else:
|
| 455 |
+
self.gamma_1, self.gamma_2 = None, None
|
| 456 |
+
|
| 457 |
+
self.postnorm = postnorm
|
| 458 |
+
|
| 459 |
+
def forward(self, x, rel_pos_bias=None, attn_mask=None):
|
| 460 |
+
if self.gamma_1 is None:
|
| 461 |
+
if self.postnorm:
|
| 462 |
+
x = x + self.drop_path(self.norm1(self.attn(x, rel_pos_bias=rel_pos_bias, attn_mask=attn_mask)))
|
| 463 |
+
x = x + self.drop_path(self.norm2(self.mlp(x)))
|
| 464 |
+
else:
|
| 465 |
+
x = x + self.drop_path(self.attn(self.norm1(x), rel_pos_bias=rel_pos_bias, attn_mask=attn_mask))
|
| 466 |
+
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
| 467 |
+
else:
|
| 468 |
+
if self.postnorm:
|
| 469 |
+
x = x + self.drop_path(self.gamma_1 * self.norm1(self.attn(x, rel_pos_bias=rel_pos_bias, attn_mask=attn_mask)))
|
| 470 |
+
x = x + self.drop_path(self.gamma_2 * self.norm2(self.mlp(x)))
|
| 471 |
+
else:
|
| 472 |
+
x = x + self.drop_path(self.gamma_1 * self.attn(self.norm1(x), rel_pos_bias=rel_pos_bias, attn_mask=attn_mask))
|
| 473 |
+
x = x + self.drop_path(self.gamma_2 * self.mlp(self.norm2(x)))
|
| 474 |
+
return x
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
class PatchEmbed(nn.Module):
|
| 478 |
+
""" Image to Patch Embedding
|
| 479 |
+
"""
|
| 480 |
+
def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768):
|
| 481 |
+
super().__init__()
|
| 482 |
+
img_size = to_2tuple(img_size)
|
| 483 |
+
patch_size = to_2tuple(patch_size)
|
| 484 |
+
num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0])
|
| 485 |
+
self.patch_shape = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
|
| 486 |
+
self.img_size = img_size
|
| 487 |
+
self.patch_size = patch_size
|
| 488 |
+
self.num_patches = num_patches
|
| 489 |
+
|
| 490 |
+
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
|
| 491 |
+
|
| 492 |
+
def forward(self, x, **kwargs):
|
| 493 |
+
B, C, H, W = x.shape
|
| 494 |
+
# FIXME look at relaxing size constraints
|
| 495 |
+
assert H == self.img_size[0] and W == self.img_size[1], \
|
| 496 |
+
f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
|
| 497 |
+
x = self.proj(x).flatten(2).transpose(1, 2)
|
| 498 |
+
return x
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
class RelativePositionBias(nn.Module):
|
| 502 |
+
|
| 503 |
+
def __init__(self, window_size, num_heads):
|
| 504 |
+
super().__init__()
|
| 505 |
+
self.window_size = window_size
|
| 506 |
+
self.num_relative_distance = (2 * window_size[0] - 1) * (2 * window_size[1] - 1) + 3
|
| 507 |
+
self.relative_position_bias_table = nn.Parameter(torch.zeros(self.num_relative_distance, num_heads)) # 2*Wh-1 * 2*Ww-1, nH
|
| 508 |
+
# cls to token & token 2 cls & cls to cls
|
| 509 |
+
|
| 510 |
+
# get pair-wise relative position index for each token inside the window
|
| 511 |
+
coords_h = torch.arange(window_size[0])
|
| 512 |
+
coords_w = torch.arange(window_size[1])
|
| 513 |
+
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
|
| 514 |
+
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
|
| 515 |
+
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
|
| 516 |
+
relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
|
| 517 |
+
relative_coords[:, :, 0] += window_size[0] - 1 # shift to start from 0
|
| 518 |
+
relative_coords[:, :, 1] += window_size[1] - 1
|
| 519 |
+
relative_coords[:, :, 0] *= 2 * window_size[1] - 1
|
| 520 |
+
relative_position_index = torch.zeros(size=(window_size[0] * window_size[1] + 1,) * 2, dtype=relative_coords.dtype)
|
| 521 |
+
relative_position_index[1:, 1:] = relative_coords.sum(-1) # Wh*Ww, Wh*Ww
|
| 522 |
+
relative_position_index[0, 0:] = self.num_relative_distance - 3
|
| 523 |
+
relative_position_index[0:, 0] = self.num_relative_distance - 2
|
| 524 |
+
relative_position_index[0, 0] = self.num_relative_distance - 1
|
| 525 |
+
|
| 526 |
+
self.register_buffer("relative_position_index", relative_position_index)
|
| 527 |
+
|
| 528 |
+
def forward(self):
|
| 529 |
+
relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view(self.window_size[0] * self.window_size[1] + 1, self.window_size[0] * self.window_size[1] + 1, -1) # Wh*Ww,Wh*Ww,nH
|
| 530 |
+
return relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
class EVAVisionTransformer(nn.Module):
|
| 534 |
+
"""Vision Transformer with support for patch or hybrid CNN input stage"""
|
| 535 |
+
|
| 536 |
+
def __init__(self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, embed_dim=768, depth=12,
|
| 537 |
+
num_heads=12, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop_rate=0., attn_drop_rate=0.,
|
| 538 |
+
drop_path_rate=0., norm_layer=nn.LayerNorm, init_values=None, patch_dropout=0.,
|
| 539 |
+
use_abs_pos_emb=True, use_rel_pos_bias=False, use_shared_rel_pos_bias=False, rope=False,
|
| 540 |
+
use_mean_pooling=True, init_scale=0.001, grad_checkpointing=False, xattn=False, postnorm=False,
|
| 541 |
+
pt_hw_seq_len=16, intp_freq=False, naiveswiglu=False, subln=False,
|
| 542 |
+
):
|
| 543 |
+
super().__init__()
|
| 544 |
+
self.image_size = img_size
|
| 545 |
+
# self.num_classes = num_classes
|
| 546 |
+
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
|
| 547 |
+
|
| 548 |
+
self.patch_embed = PatchEmbed(
|
| 549 |
+
img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
|
| 550 |
+
num_patches = self.patch_embed.num_patches
|
| 551 |
+
|
| 552 |
+
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
|
| 553 |
+
if use_abs_pos_emb:
|
| 554 |
+
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + 1, embed_dim))
|
| 555 |
+
else:
|
| 556 |
+
self.pos_embed = None
|
| 557 |
+
self.pos_drop = nn.Dropout(p=drop_rate)
|
| 558 |
+
|
| 559 |
+
self.rel_pos_bias = None
|
| 560 |
+
self.rope = None
|
| 561 |
+
|
| 562 |
+
self.naiveswiglu = naiveswiglu
|
| 563 |
+
|
| 564 |
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
|
| 565 |
+
self.use_rel_pos_bias = use_rel_pos_bias
|
| 566 |
+
self.blocks = nn.ModuleList([
|
| 567 |
+
Block(
|
| 568 |
+
dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
|
| 569 |
+
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer,
|
| 570 |
+
init_values=init_values, window_size=self.patch_embed.patch_shape if use_rel_pos_bias else None,
|
| 571 |
+
xattn=xattn, rope=self.rope, postnorm=postnorm, subln=subln, naiveswiglu=naiveswiglu)
|
| 572 |
+
for i in range(depth)])
|
| 573 |
+
|
| 574 |
+
# setting a patch_dropout of 0. would mean it is disabled and this function would be the identity fn
|
| 575 |
+
self.patch_dropout = PatchDropout(patch_dropout) if patch_dropout > 0. else nn.Identity()
|
| 576 |
+
|
| 577 |
+
self.grad_checkpointing = grad_checkpointing
|
| 578 |
+
|
| 579 |
+
def fix_init_weight(self):
|
| 580 |
+
def rescale(param, layer_id):
|
| 581 |
+
param.div_(math.sqrt(2.0 * layer_id))
|
| 582 |
+
|
| 583 |
+
for layer_id, layer in enumerate(self.blocks):
|
| 584 |
+
rescale(layer.attn.proj.weight.data, layer_id + 1)
|
| 585 |
+
if self.naiveswiglu:
|
| 586 |
+
rescale(layer.mlp.w3.weight.data, layer_id + 1)
|
| 587 |
+
else:
|
| 588 |
+
rescale(layer.mlp.fc2.weight.data, layer_id + 1)
|
| 589 |
+
|
| 590 |
+
def get_cast_dtype(self) -> torch.dtype:
|
| 591 |
+
return self.blocks[0].mlp.fc2.weight.dtype
|
| 592 |
+
|
| 593 |
+
def _init_weights(self, m):
|
| 594 |
+
if isinstance(m, nn.Linear):
|
| 595 |
+
trunc_normal_(m.weight, std=0.02)
|
| 596 |
+
if m.bias is not None:
|
| 597 |
+
nn.init.constant_(m.bias, 0)
|
| 598 |
+
elif isinstance(m, nn.LayerNorm):
|
| 599 |
+
nn.init.constant_(m.bias, 0)
|
| 600 |
+
nn.init.constant_(m.weight, 1.0)
|
| 601 |
+
|
| 602 |
+
def get_num_layers(self):
|
| 603 |
+
return len(self.blocks)
|
| 604 |
+
|
| 605 |
+
def lock(self, unlocked_groups=0, freeze_bn_stats=False):
|
| 606 |
+
assert unlocked_groups == 0, "partial locking not currently supported for this model"
|
| 607 |
+
for param in self.parameters():
|
| 608 |
+
param.requires_grad = False
|
| 609 |
+
|
| 610 |
+
@torch.jit.ignore
|
| 611 |
+
def set_grad_checkpointing(self, enable=True):
|
| 612 |
+
self.grad_checkpointing = enable
|
| 613 |
+
|
| 614 |
+
@torch.jit.ignore
|
| 615 |
+
def no_weight_decay(self):
|
| 616 |
+
return {"pos_embed", "cls_token"}
|
| 617 |
+
|
| 618 |
+
def get_classifier(self):
|
| 619 |
+
return self.head
|
| 620 |
+
|
| 621 |
+
def reset_classifier(self, num_classes, global_pool=""):
|
| 622 |
+
self.num_classes = num_classes
|
| 623 |
+
self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()
|
| 624 |
+
|
| 625 |
+
def forward_features(self, x):
|
| 626 |
+
x = self.patch_embed(x)
|
| 627 |
+
batch_size, seq_len, _ = x.size()
|
| 628 |
+
|
| 629 |
+
cls_tokens = self.cls_token.expand(batch_size, -1, -1) # stole cls_tokens impl from Phil Wang, thanks
|
| 630 |
+
x = torch.cat((cls_tokens, x), dim=1)
|
| 631 |
+
if self.pos_embed is not None:
|
| 632 |
+
x = x + self.pos_embed
|
| 633 |
+
x = self.pos_drop(x)
|
| 634 |
+
|
| 635 |
+
# a patch_dropout of 0. would mean it is disabled and this function would do nothing but return what was passed in
|
| 636 |
+
if os.getenv('RoPE') == '1':
|
| 637 |
+
if self.training and not isinstance(self.patch_dropout, nn.Identity):
|
| 638 |
+
x, patch_indices_keep = self.patch_dropout(x)
|
| 639 |
+
self.rope.forward = partial(self.rope.forward, patch_indices_keep=patch_indices_keep)
|
| 640 |
+
else:
|
| 641 |
+
self.rope.forward = partial(self.rope.forward, patch_indices_keep=None)
|
| 642 |
+
x = self.patch_dropout(x)
|
| 643 |
+
else:
|
| 644 |
+
x = self.patch_dropout(x)
|
| 645 |
+
|
| 646 |
+
rel_pos_bias = None
|
| 647 |
+
|
| 648 |
+
for blk in self.blocks:
|
| 649 |
+
if self.grad_checkpointing:
|
| 650 |
+
x = checkpoint(blk, x, (rel_pos_bias,))
|
| 651 |
+
else:
|
| 652 |
+
x = blk(x, rel_pos_bias=rel_pos_bias)
|
| 653 |
+
|
| 654 |
+
return x
|
| 655 |
+
|
| 656 |
+
def forward(self, x, return_all_features=False):
|
| 657 |
+
|
| 658 |
+
"""
|
| 659 |
+
:return:
|
| 660 |
+
forward_features function returns raw features of ViT,
|
| 661 |
+
forward with return_all_features returns normalized features of ViT
|
| 662 |
+
:param x:
|
| 663 |
+
:param return_all_features:
|
| 664 |
+
"""
|
| 665 |
+
|
| 666 |
+
features = self.forward_features(x) # [B, n_patch, C]
|
| 667 |
+
return features
|
| 668 |
+
|
| 669 |
+
|
| 670 |
+
def load_state_dict(checkpoint_path: str, map_location: str = "cpu", model_key: str = "model|module|state_dict", is_openai: bool = False, skip_list: list = []):
|
| 671 |
+
if is_openai:
|
| 672 |
+
model = torch.jit.load(checkpoint_path, map_location="cpu").eval()
|
| 673 |
+
state_dict = model.state_dict()
|
| 674 |
+
for key in ["input_resolution", "context_length", "vocab_size"]:
|
| 675 |
+
state_dict.pop(key, None)
|
| 676 |
+
else:
|
| 677 |
+
checkpoint = torch.load(checkpoint_path, map_location=map_location)
|
| 678 |
+
for mk in model_key.split("|"):
|
| 679 |
+
if isinstance(checkpoint, dict) and mk in checkpoint:
|
| 680 |
+
state_dict = checkpoint[mk]
|
| 681 |
+
break
|
| 682 |
+
else:
|
| 683 |
+
state_dict = checkpoint
|
| 684 |
+
if next(iter(state_dict.items()))[0].startswith("module"):
|
| 685 |
+
state_dict = {k[7:]: v for k, v in state_dict.items()}
|
| 686 |
+
|
| 687 |
+
for k in skip_list:
|
| 688 |
+
if k in list(state_dict.keys()):
|
| 689 |
+
logging.info(f"Removing key {k} from pretrained checkpoint")
|
| 690 |
+
del state_dict[k]
|
| 691 |
+
|
| 692 |
+
if os.getenv("RoPE") == "1":
|
| 693 |
+
for k in list(state_dict.keys()):
|
| 694 |
+
if "freqs_cos" in k or "freqs_sin" in k:
|
| 695 |
+
del state_dict[k]
|
| 696 |
+
return state_dict
|
| 697 |
+
|
| 698 |
+
|
| 699 |
+
def load_clip_visual_state_dict(checkpoint_path: str, map_location: str = "cpu", is_openai: bool = False, skip_list: list = []):
|
| 700 |
+
state_dict = load_state_dict(checkpoint_path, map_location=map_location, is_openai=is_openai, skip_list=skip_list)
|
| 701 |
+
# for k in list(state_dict.keys()):
|
| 702 |
+
# if not k.startswith("visual."):
|
| 703 |
+
# del state_dict[k]
|
| 704 |
+
# for k in list(state_dict.keys()):
|
| 705 |
+
# if k.startswith("visual."):
|
| 706 |
+
# new_k = k[7:]
|
| 707 |
+
# state_dict[new_k] = state_dict[k]
|
| 708 |
+
# del state_dict[k]
|
| 709 |
+
return state_dict
|
| 710 |
+
|
| 711 |
+
|
| 712 |
+
from dataclasses import dataclass
|
| 713 |
+
from typing import Optional, Tuple, Union
|
| 714 |
+
|
| 715 |
+
try:
|
| 716 |
+
from apex.normalization import FusedLayerNorm
|
| 717 |
+
except:
|
| 718 |
+
FusedLayerNorm = LayerNorm
|
| 719 |
+
# print("Please build and install Nvidia apex package with option '--cuda_ext' according to https://github.com/NVIDIA/apex#from-source .")
|
| 720 |
+
|
| 721 |
+
|
| 722 |
+
@dataclass
|
| 723 |
+
class CLIPVisionCfg:
|
| 724 |
+
layers: Union[Tuple[int, int, int, int], int] = 12
|
| 725 |
+
width: int = 768
|
| 726 |
+
head_width: int = 64
|
| 727 |
+
mlp_ratio: float = 4.0
|
| 728 |
+
patch_size: int = 16
|
| 729 |
+
image_size: Union[Tuple[int, int], int] = 224
|
| 730 |
+
ls_init_value: Optional[float] = None # layer scale initial value
|
| 731 |
+
patch_dropout: float = 0.0 # what fraction of patches to dropout during training (0 would mean disabled and no patches dropped) - 0.5 to 0.75 recommended in the paper for optimal results
|
| 732 |
+
global_average_pool: bool = False # whether to global average pool the last embedding layer, instead of using CLS token (https://arxiv.org/abs/2205.01580)
|
| 733 |
+
drop_path_rate: Optional[float] = None # drop path rate
|
| 734 |
+
timm_model_name: str = None # a valid model name overrides layers, width, patch_size
|
| 735 |
+
timm_model_pretrained: bool = False # use (imagenet) pretrained weights for named model
|
| 736 |
+
timm_pool: str = "avg" # feature pooling for timm model ('abs_attn', 'rot_attn', 'avg', '')
|
| 737 |
+
timm_proj: str = "linear" # linear projection for timm model output ('linear', 'mlp', '')
|
| 738 |
+
timm_proj_bias: bool = False # enable bias final projection
|
| 739 |
+
eva_model_name: str = None # a valid eva model name overrides layers, width, patch_size
|
| 740 |
+
qkv_bias: bool = True
|
| 741 |
+
fusedLN: bool = False
|
| 742 |
+
xattn: bool = False
|
| 743 |
+
postnorm: bool = False
|
| 744 |
+
rope: bool = False
|
| 745 |
+
pt_hw_seq_len: int = 16 # 224/14
|
| 746 |
+
intp_freq: bool = False
|
| 747 |
+
naiveswiglu: bool = False
|
| 748 |
+
subln: bool = False
|
| 749 |
+
|
| 750 |
+
|
| 751 |
+
def create_norm_layer_factory(use_fused_ln, eps=1e-6):
|
| 752 |
+
# Otherwise, use the standard LayerNorm
|
| 753 |
+
return lambda num_features: nn.LayerNorm(num_features, eps=eps)
|
| 754 |
+
|
| 755 |
+
|
| 756 |
+
def _build_vision_tower(vision_tower_path: str, embed_dim: int, vision_cfg: CLIPVisionCfg, **kwargs):
|
| 757 |
+
if isinstance(vision_cfg, dict):
|
| 758 |
+
vision_cfg = CLIPVisionCfg(**vision_cfg)
|
| 759 |
+
|
| 760 |
+
if vision_cfg.eva_model_name:
|
| 761 |
+
vision_heads = vision_cfg.width // vision_cfg.head_width
|
| 762 |
+
# Determine the appropriate norm layer factory based on the configuration
|
| 763 |
+
norm_layer_factory = create_norm_layer_factory(vision_cfg.fusedLN, eps=1e-6)
|
| 764 |
+
|
| 765 |
+
# breakpoint()
|
| 766 |
+
visual = EVAVisionTransformer(
|
| 767 |
+
img_size=vision_cfg.image_size,
|
| 768 |
+
patch_size=vision_cfg.patch_size,
|
| 769 |
+
num_classes=embed_dim,
|
| 770 |
+
use_mean_pooling=vision_cfg.global_average_pool, # False
|
| 771 |
+
init_values=vision_cfg.ls_init_value,
|
| 772 |
+
patch_dropout=vision_cfg.patch_dropout,
|
| 773 |
+
embed_dim=vision_cfg.width,
|
| 774 |
+
depth=vision_cfg.layers,
|
| 775 |
+
num_heads=vision_heads,
|
| 776 |
+
mlp_ratio=vision_cfg.mlp_ratio,
|
| 777 |
+
qkv_bias=vision_cfg.qkv_bias,
|
| 778 |
+
drop_path_rate=vision_cfg.drop_path_rate,
|
| 779 |
+
norm_layer=norm_layer_factory,
|
| 780 |
+
xattn=vision_cfg.xattn,
|
| 781 |
+
rope=vision_cfg.rope,
|
| 782 |
+
postnorm=vision_cfg.postnorm,
|
| 783 |
+
pt_hw_seq_len=vision_cfg.pt_hw_seq_len, # 224/14
|
| 784 |
+
intp_freq=vision_cfg.intp_freq,
|
| 785 |
+
naiveswiglu=vision_cfg.naiveswiglu,
|
| 786 |
+
subln=vision_cfg.subln,
|
| 787 |
+
)
|
| 788 |
+
# breakpoint()
|
| 789 |
+
state_dict = load_clip_visual_state_dict(vision_tower_path)
|
| 790 |
+
incompatible_keys = visual.load_state_dict(state_dict, strict=False)
|
| 791 |
+
print("EVA-CLIP incompatible_keys:", incompatible_keys)
|
| 792 |
+
|
| 793 |
+
return visual
|
| 794 |
+
|
| 795 |
+
|
| 796 |
+
class EVAEncoderWrapper(nn.Module):
|
| 797 |
+
def __init__(self, vision_tower_pretrained, config):
|
| 798 |
+
super(EVAEncoderWrapper, self).__init__()
|
| 799 |
+
self.config = config
|
| 800 |
+
vision_tower_path = _resolve_vision_checkpoint_path(vision_tower_pretrained)
|
| 801 |
+
self.config["vision_tower_path"] = vision_tower_path
|
| 802 |
+
self.vision_tower_path = vision_tower_path
|
| 803 |
+
self.model = _build_vision_tower(**self.config)
|
| 804 |
+
|
| 805 |
+
def forward(self, image, **kwargs):
|
| 806 |
+
encode = self.model(image, return_all_features=True)[:, 1:, :] # remove the CLS token
|
| 807 |
+
return encode
|
| 808 |
+
|
| 809 |
+
@property
|
| 810 |
+
def dtype(self):
|
| 811 |
+
return list(self.parameters())[-1].dtype
|
| 812 |
+
|
| 813 |
+
@property
|
| 814 |
+
def device(self):
|
| 815 |
+
return list(self.parameters())[-1].device
|
model-00001-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d84ba46c42ae442da367bbf89fd91b99f031abaae7702e97af614830a00b7dc6
|
| 3 |
+
size 2075135128
|
model-00002-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bd471ff827bd24f908cff1a974848cd0d59601745dd7fab6ae37a67a6af2e1aa
|
| 3 |
+
size 2063504224
|
model-00003-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:946270642a7cca24ea645c93045607423862753c1ed94e85d5d49ffc1cab8caa
|
| 3 |
+
size 2122242576
|
model-00004-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:891c19b33d0b85c8c3c1a6c679781947152e5ada83f368fdb670928db165ef46
|
| 3 |
+
size 2063544008
|
model-00005-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e7de9bce95a2a80881f20a1cc191c1a77ef28cbc8d210b7c97538bb946e6c56
|
| 3 |
+
size 2063504264
|
model-00006-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:21ccc9ffbffc60786ae93e1d412273cbd9d95a1e3faaa45536f7f7dd763364cd
|
| 3 |
+
size 2122242576
|
model-00007-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d00a946cb9f25eb2ae2e82390c29a74f2f264f98cd0bb5ecd7e7404ddfa35605
|
| 3 |
+
size 2063544008
|
model-00008-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:65e7dba74ace0cf4fa91135decf5628306528dacca00dfc6670d22cca78c4f69
|
| 3 |
+
size 2063504264
|
model-00009-of-00009.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1a2a80b0493b0a1da9b766beed290defea69acc068a0ae21ad04486844930667
|
| 3 |
+
size 763686520
|
model.safetensors.index.json
ADDED
|
@@ -0,0 +1,779 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"metadata": {
|
| 3 |
+
"total_size": 17400833024
|
| 4 |
+
},
|
| 5 |
+
"weight_map": {
|
| 6 |
+
"cls_token": "model-00001-of-00009.safetensors",
|
| 7 |
+
"pos_embed": "model-00001-of-00009.safetensors",
|
| 8 |
+
"patch_embed.proj.weight": "model-00001-of-00009.safetensors",
|
| 9 |
+
"patch_embed.proj.bias": "model-00001-of-00009.safetensors",
|
| 10 |
+
"blocks.0.norm1.weight": "model-00001-of-00009.safetensors",
|
| 11 |
+
"blocks.0.norm1.bias": "model-00001-of-00009.safetensors",
|
| 12 |
+
"blocks.0.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 13 |
+
"blocks.0.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 14 |
+
"blocks.0.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 15 |
+
"blocks.0.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 16 |
+
"blocks.0.norm2.weight": "model-00001-of-00009.safetensors",
|
| 17 |
+
"blocks.0.norm2.bias": "model-00001-of-00009.safetensors",
|
| 18 |
+
"blocks.0.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 19 |
+
"blocks.0.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 20 |
+
"blocks.0.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 21 |
+
"blocks.0.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 22 |
+
"blocks.1.norm1.weight": "model-00001-of-00009.safetensors",
|
| 23 |
+
"blocks.1.norm1.bias": "model-00001-of-00009.safetensors",
|
| 24 |
+
"blocks.1.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 25 |
+
"blocks.1.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 26 |
+
"blocks.1.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 27 |
+
"blocks.1.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 28 |
+
"blocks.1.norm2.weight": "model-00001-of-00009.safetensors",
|
| 29 |
+
"blocks.1.norm2.bias": "model-00001-of-00009.safetensors",
|
| 30 |
+
"blocks.1.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 31 |
+
"blocks.1.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 32 |
+
"blocks.1.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 33 |
+
"blocks.1.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 34 |
+
"blocks.2.norm1.weight": "model-00001-of-00009.safetensors",
|
| 35 |
+
"blocks.2.norm1.bias": "model-00001-of-00009.safetensors",
|
| 36 |
+
"blocks.2.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 37 |
+
"blocks.2.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 38 |
+
"blocks.2.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 39 |
+
"blocks.2.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 40 |
+
"blocks.2.norm2.weight": "model-00001-of-00009.safetensors",
|
| 41 |
+
"blocks.2.norm2.bias": "model-00001-of-00009.safetensors",
|
| 42 |
+
"blocks.2.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 43 |
+
"blocks.2.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 44 |
+
"blocks.2.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 45 |
+
"blocks.2.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 46 |
+
"blocks.3.norm1.weight": "model-00001-of-00009.safetensors",
|
| 47 |
+
"blocks.3.norm1.bias": "model-00001-of-00009.safetensors",
|
| 48 |
+
"blocks.3.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 49 |
+
"blocks.3.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 50 |
+
"blocks.3.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 51 |
+
"blocks.3.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 52 |
+
"blocks.3.norm2.weight": "model-00001-of-00009.safetensors",
|
| 53 |
+
"blocks.3.norm2.bias": "model-00001-of-00009.safetensors",
|
| 54 |
+
"blocks.3.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 55 |
+
"blocks.3.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 56 |
+
"blocks.3.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 57 |
+
"blocks.3.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 58 |
+
"blocks.4.norm1.weight": "model-00001-of-00009.safetensors",
|
| 59 |
+
"blocks.4.norm1.bias": "model-00001-of-00009.safetensors",
|
| 60 |
+
"blocks.4.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 61 |
+
"blocks.4.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 62 |
+
"blocks.4.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 63 |
+
"blocks.4.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 64 |
+
"blocks.4.norm2.weight": "model-00001-of-00009.safetensors",
|
| 65 |
+
"blocks.4.norm2.bias": "model-00001-of-00009.safetensors",
|
| 66 |
+
"blocks.4.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 67 |
+
"blocks.4.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 68 |
+
"blocks.4.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 69 |
+
"blocks.4.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 70 |
+
"blocks.5.norm1.weight": "model-00001-of-00009.safetensors",
|
| 71 |
+
"blocks.5.norm1.bias": "model-00001-of-00009.safetensors",
|
| 72 |
+
"blocks.5.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 73 |
+
"blocks.5.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 74 |
+
"blocks.5.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 75 |
+
"blocks.5.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 76 |
+
"blocks.5.norm2.weight": "model-00001-of-00009.safetensors",
|
| 77 |
+
"blocks.5.norm2.bias": "model-00001-of-00009.safetensors",
|
| 78 |
+
"blocks.5.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 79 |
+
"blocks.5.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 80 |
+
"blocks.5.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 81 |
+
"blocks.5.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 82 |
+
"blocks.6.norm1.weight": "model-00001-of-00009.safetensors",
|
| 83 |
+
"blocks.6.norm1.bias": "model-00001-of-00009.safetensors",
|
| 84 |
+
"blocks.6.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 85 |
+
"blocks.6.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 86 |
+
"blocks.6.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 87 |
+
"blocks.6.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 88 |
+
"blocks.6.norm2.weight": "model-00001-of-00009.safetensors",
|
| 89 |
+
"blocks.6.norm2.bias": "model-00001-of-00009.safetensors",
|
| 90 |
+
"blocks.6.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 91 |
+
"blocks.6.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 92 |
+
"blocks.6.mlp.fc2.weight": "model-00001-of-00009.safetensors",
|
| 93 |
+
"blocks.6.mlp.fc2.bias": "model-00001-of-00009.safetensors",
|
| 94 |
+
"blocks.7.norm1.weight": "model-00001-of-00009.safetensors",
|
| 95 |
+
"blocks.7.norm1.bias": "model-00001-of-00009.safetensors",
|
| 96 |
+
"blocks.7.attn.qkv.weight": "model-00001-of-00009.safetensors",
|
| 97 |
+
"blocks.7.attn.qkv.bias": "model-00001-of-00009.safetensors",
|
| 98 |
+
"blocks.7.attn.proj.weight": "model-00001-of-00009.safetensors",
|
| 99 |
+
"blocks.7.attn.proj.bias": "model-00001-of-00009.safetensors",
|
| 100 |
+
"blocks.7.norm2.weight": "model-00001-of-00009.safetensors",
|
| 101 |
+
"blocks.7.norm2.bias": "model-00001-of-00009.safetensors",
|
| 102 |
+
"blocks.7.mlp.fc1.weight": "model-00001-of-00009.safetensors",
|
| 103 |
+
"blocks.7.mlp.fc1.bias": "model-00001-of-00009.safetensors",
|
| 104 |
+
"blocks.7.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 105 |
+
"blocks.7.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 106 |
+
"blocks.8.norm1.weight": "model-00002-of-00009.safetensors",
|
| 107 |
+
"blocks.8.norm1.bias": "model-00002-of-00009.safetensors",
|
| 108 |
+
"blocks.8.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 109 |
+
"blocks.8.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 110 |
+
"blocks.8.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 111 |
+
"blocks.8.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 112 |
+
"blocks.8.norm2.weight": "model-00002-of-00009.safetensors",
|
| 113 |
+
"blocks.8.norm2.bias": "model-00002-of-00009.safetensors",
|
| 114 |
+
"blocks.8.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 115 |
+
"blocks.8.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 116 |
+
"blocks.8.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 117 |
+
"blocks.8.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 118 |
+
"blocks.9.norm1.weight": "model-00002-of-00009.safetensors",
|
| 119 |
+
"blocks.9.norm1.bias": "model-00002-of-00009.safetensors",
|
| 120 |
+
"blocks.9.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 121 |
+
"blocks.9.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 122 |
+
"blocks.9.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 123 |
+
"blocks.9.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 124 |
+
"blocks.9.norm2.weight": "model-00002-of-00009.safetensors",
|
| 125 |
+
"blocks.9.norm2.bias": "model-00002-of-00009.safetensors",
|
| 126 |
+
"blocks.9.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 127 |
+
"blocks.9.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 128 |
+
"blocks.9.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 129 |
+
"blocks.9.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 130 |
+
"blocks.10.norm1.weight": "model-00002-of-00009.safetensors",
|
| 131 |
+
"blocks.10.norm1.bias": "model-00002-of-00009.safetensors",
|
| 132 |
+
"blocks.10.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 133 |
+
"blocks.10.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 134 |
+
"blocks.10.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 135 |
+
"blocks.10.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 136 |
+
"blocks.10.norm2.weight": "model-00002-of-00009.safetensors",
|
| 137 |
+
"blocks.10.norm2.bias": "model-00002-of-00009.safetensors",
|
| 138 |
+
"blocks.10.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 139 |
+
"blocks.10.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 140 |
+
"blocks.10.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 141 |
+
"blocks.10.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 142 |
+
"blocks.11.norm1.weight": "model-00002-of-00009.safetensors",
|
| 143 |
+
"blocks.11.norm1.bias": "model-00002-of-00009.safetensors",
|
| 144 |
+
"blocks.11.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 145 |
+
"blocks.11.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 146 |
+
"blocks.11.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 147 |
+
"blocks.11.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 148 |
+
"blocks.11.norm2.weight": "model-00002-of-00009.safetensors",
|
| 149 |
+
"blocks.11.norm2.bias": "model-00002-of-00009.safetensors",
|
| 150 |
+
"blocks.11.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 151 |
+
"blocks.11.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 152 |
+
"blocks.11.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 153 |
+
"blocks.11.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 154 |
+
"blocks.12.norm1.weight": "model-00002-of-00009.safetensors",
|
| 155 |
+
"blocks.12.norm1.bias": "model-00002-of-00009.safetensors",
|
| 156 |
+
"blocks.12.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 157 |
+
"blocks.12.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 158 |
+
"blocks.12.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 159 |
+
"blocks.12.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 160 |
+
"blocks.12.norm2.weight": "model-00002-of-00009.safetensors",
|
| 161 |
+
"blocks.12.norm2.bias": "model-00002-of-00009.safetensors",
|
| 162 |
+
"blocks.12.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 163 |
+
"blocks.12.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 164 |
+
"blocks.12.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 165 |
+
"blocks.12.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 166 |
+
"blocks.13.norm1.weight": "model-00002-of-00009.safetensors",
|
| 167 |
+
"blocks.13.norm1.bias": "model-00002-of-00009.safetensors",
|
| 168 |
+
"blocks.13.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 169 |
+
"blocks.13.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 170 |
+
"blocks.13.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 171 |
+
"blocks.13.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 172 |
+
"blocks.13.norm2.weight": "model-00002-of-00009.safetensors",
|
| 173 |
+
"blocks.13.norm2.bias": "model-00002-of-00009.safetensors",
|
| 174 |
+
"blocks.13.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 175 |
+
"blocks.13.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 176 |
+
"blocks.13.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 177 |
+
"blocks.13.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 178 |
+
"blocks.14.norm1.weight": "model-00002-of-00009.safetensors",
|
| 179 |
+
"blocks.14.norm1.bias": "model-00002-of-00009.safetensors",
|
| 180 |
+
"blocks.14.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 181 |
+
"blocks.14.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 182 |
+
"blocks.14.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 183 |
+
"blocks.14.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 184 |
+
"blocks.14.norm2.weight": "model-00002-of-00009.safetensors",
|
| 185 |
+
"blocks.14.norm2.bias": "model-00002-of-00009.safetensors",
|
| 186 |
+
"blocks.14.mlp.fc1.weight": "model-00002-of-00009.safetensors",
|
| 187 |
+
"blocks.14.mlp.fc1.bias": "model-00002-of-00009.safetensors",
|
| 188 |
+
"blocks.14.mlp.fc2.weight": "model-00002-of-00009.safetensors",
|
| 189 |
+
"blocks.14.mlp.fc2.bias": "model-00002-of-00009.safetensors",
|
| 190 |
+
"blocks.15.norm1.weight": "model-00002-of-00009.safetensors",
|
| 191 |
+
"blocks.15.norm1.bias": "model-00002-of-00009.safetensors",
|
| 192 |
+
"blocks.15.attn.qkv.weight": "model-00002-of-00009.safetensors",
|
| 193 |
+
"blocks.15.attn.qkv.bias": "model-00002-of-00009.safetensors",
|
| 194 |
+
"blocks.15.attn.proj.weight": "model-00002-of-00009.safetensors",
|
| 195 |
+
"blocks.15.attn.proj.bias": "model-00002-of-00009.safetensors",
|
| 196 |
+
"blocks.15.norm2.weight": "model-00002-of-00009.safetensors",
|
| 197 |
+
"blocks.15.norm2.bias": "model-00002-of-00009.safetensors",
|
| 198 |
+
"blocks.15.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 199 |
+
"blocks.15.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 200 |
+
"blocks.15.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 201 |
+
"blocks.15.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 202 |
+
"blocks.16.norm1.weight": "model-00003-of-00009.safetensors",
|
| 203 |
+
"blocks.16.norm1.bias": "model-00003-of-00009.safetensors",
|
| 204 |
+
"blocks.16.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 205 |
+
"blocks.16.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 206 |
+
"blocks.16.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 207 |
+
"blocks.16.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 208 |
+
"blocks.16.norm2.weight": "model-00003-of-00009.safetensors",
|
| 209 |
+
"blocks.16.norm2.bias": "model-00003-of-00009.safetensors",
|
| 210 |
+
"blocks.16.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 211 |
+
"blocks.16.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 212 |
+
"blocks.16.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 213 |
+
"blocks.16.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 214 |
+
"blocks.17.norm1.weight": "model-00003-of-00009.safetensors",
|
| 215 |
+
"blocks.17.norm1.bias": "model-00003-of-00009.safetensors",
|
| 216 |
+
"blocks.17.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 217 |
+
"blocks.17.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 218 |
+
"blocks.17.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 219 |
+
"blocks.17.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 220 |
+
"blocks.17.norm2.weight": "model-00003-of-00009.safetensors",
|
| 221 |
+
"blocks.17.norm2.bias": "model-00003-of-00009.safetensors",
|
| 222 |
+
"blocks.17.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 223 |
+
"blocks.17.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 224 |
+
"blocks.17.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 225 |
+
"blocks.17.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 226 |
+
"blocks.18.norm1.weight": "model-00003-of-00009.safetensors",
|
| 227 |
+
"blocks.18.norm1.bias": "model-00003-of-00009.safetensors",
|
| 228 |
+
"blocks.18.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 229 |
+
"blocks.18.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 230 |
+
"blocks.18.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 231 |
+
"blocks.18.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 232 |
+
"blocks.18.norm2.weight": "model-00003-of-00009.safetensors",
|
| 233 |
+
"blocks.18.norm2.bias": "model-00003-of-00009.safetensors",
|
| 234 |
+
"blocks.18.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 235 |
+
"blocks.18.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 236 |
+
"blocks.18.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 237 |
+
"blocks.18.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 238 |
+
"blocks.19.norm1.weight": "model-00003-of-00009.safetensors",
|
| 239 |
+
"blocks.19.norm1.bias": "model-00003-of-00009.safetensors",
|
| 240 |
+
"blocks.19.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 241 |
+
"blocks.19.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 242 |
+
"blocks.19.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 243 |
+
"blocks.19.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 244 |
+
"blocks.19.norm2.weight": "model-00003-of-00009.safetensors",
|
| 245 |
+
"blocks.19.norm2.bias": "model-00003-of-00009.safetensors",
|
| 246 |
+
"blocks.19.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 247 |
+
"blocks.19.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 248 |
+
"blocks.19.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 249 |
+
"blocks.19.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 250 |
+
"blocks.20.norm1.weight": "model-00003-of-00009.safetensors",
|
| 251 |
+
"blocks.20.norm1.bias": "model-00003-of-00009.safetensors",
|
| 252 |
+
"blocks.20.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 253 |
+
"blocks.20.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 254 |
+
"blocks.20.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 255 |
+
"blocks.20.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 256 |
+
"blocks.20.norm2.weight": "model-00003-of-00009.safetensors",
|
| 257 |
+
"blocks.20.norm2.bias": "model-00003-of-00009.safetensors",
|
| 258 |
+
"blocks.20.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 259 |
+
"blocks.20.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 260 |
+
"blocks.20.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 261 |
+
"blocks.20.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 262 |
+
"blocks.21.norm1.weight": "model-00003-of-00009.safetensors",
|
| 263 |
+
"blocks.21.norm1.bias": "model-00003-of-00009.safetensors",
|
| 264 |
+
"blocks.21.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 265 |
+
"blocks.21.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 266 |
+
"blocks.21.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 267 |
+
"blocks.21.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 268 |
+
"blocks.21.norm2.weight": "model-00003-of-00009.safetensors",
|
| 269 |
+
"blocks.21.norm2.bias": "model-00003-of-00009.safetensors",
|
| 270 |
+
"blocks.21.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 271 |
+
"blocks.21.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 272 |
+
"blocks.21.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 273 |
+
"blocks.21.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 274 |
+
"blocks.22.norm1.weight": "model-00003-of-00009.safetensors",
|
| 275 |
+
"blocks.22.norm1.bias": "model-00003-of-00009.safetensors",
|
| 276 |
+
"blocks.22.attn.qkv.weight": "model-00003-of-00009.safetensors",
|
| 277 |
+
"blocks.22.attn.qkv.bias": "model-00003-of-00009.safetensors",
|
| 278 |
+
"blocks.22.attn.proj.weight": "model-00003-of-00009.safetensors",
|
| 279 |
+
"blocks.22.attn.proj.bias": "model-00003-of-00009.safetensors",
|
| 280 |
+
"blocks.22.norm2.weight": "model-00003-of-00009.safetensors",
|
| 281 |
+
"blocks.22.norm2.bias": "model-00003-of-00009.safetensors",
|
| 282 |
+
"blocks.22.mlp.fc1.weight": "model-00003-of-00009.safetensors",
|
| 283 |
+
"blocks.22.mlp.fc1.bias": "model-00003-of-00009.safetensors",
|
| 284 |
+
"blocks.22.mlp.fc2.weight": "model-00003-of-00009.safetensors",
|
| 285 |
+
"blocks.22.mlp.fc2.bias": "model-00003-of-00009.safetensors",
|
| 286 |
+
"blocks.23.norm1.weight": "model-00003-of-00009.safetensors",
|
| 287 |
+
"blocks.23.norm1.bias": "model-00003-of-00009.safetensors",
|
| 288 |
+
"blocks.23.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 289 |
+
"blocks.23.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 290 |
+
"blocks.23.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 291 |
+
"blocks.23.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 292 |
+
"blocks.23.norm2.weight": "model-00004-of-00009.safetensors",
|
| 293 |
+
"blocks.23.norm2.bias": "model-00004-of-00009.safetensors",
|
| 294 |
+
"blocks.23.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 295 |
+
"blocks.23.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 296 |
+
"blocks.23.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 297 |
+
"blocks.23.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 298 |
+
"blocks.24.norm1.weight": "model-00004-of-00009.safetensors",
|
| 299 |
+
"blocks.24.norm1.bias": "model-00004-of-00009.safetensors",
|
| 300 |
+
"blocks.24.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 301 |
+
"blocks.24.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 302 |
+
"blocks.24.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 303 |
+
"blocks.24.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 304 |
+
"blocks.24.norm2.weight": "model-00004-of-00009.safetensors",
|
| 305 |
+
"blocks.24.norm2.bias": "model-00004-of-00009.safetensors",
|
| 306 |
+
"blocks.24.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 307 |
+
"blocks.24.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 308 |
+
"blocks.24.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 309 |
+
"blocks.24.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 310 |
+
"blocks.25.norm1.weight": "model-00004-of-00009.safetensors",
|
| 311 |
+
"blocks.25.norm1.bias": "model-00004-of-00009.safetensors",
|
| 312 |
+
"blocks.25.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 313 |
+
"blocks.25.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 314 |
+
"blocks.25.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 315 |
+
"blocks.25.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 316 |
+
"blocks.25.norm2.weight": "model-00004-of-00009.safetensors",
|
| 317 |
+
"blocks.25.norm2.bias": "model-00004-of-00009.safetensors",
|
| 318 |
+
"blocks.25.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 319 |
+
"blocks.25.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 320 |
+
"blocks.25.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 321 |
+
"blocks.25.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 322 |
+
"blocks.26.norm1.weight": "model-00004-of-00009.safetensors",
|
| 323 |
+
"blocks.26.norm1.bias": "model-00004-of-00009.safetensors",
|
| 324 |
+
"blocks.26.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 325 |
+
"blocks.26.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 326 |
+
"blocks.26.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 327 |
+
"blocks.26.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 328 |
+
"blocks.26.norm2.weight": "model-00004-of-00009.safetensors",
|
| 329 |
+
"blocks.26.norm2.bias": "model-00004-of-00009.safetensors",
|
| 330 |
+
"blocks.26.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 331 |
+
"blocks.26.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 332 |
+
"blocks.26.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 333 |
+
"blocks.26.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 334 |
+
"blocks.27.norm1.weight": "model-00004-of-00009.safetensors",
|
| 335 |
+
"blocks.27.norm1.bias": "model-00004-of-00009.safetensors",
|
| 336 |
+
"blocks.27.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 337 |
+
"blocks.27.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 338 |
+
"blocks.27.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 339 |
+
"blocks.27.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 340 |
+
"blocks.27.norm2.weight": "model-00004-of-00009.safetensors",
|
| 341 |
+
"blocks.27.norm2.bias": "model-00004-of-00009.safetensors",
|
| 342 |
+
"blocks.27.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 343 |
+
"blocks.27.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 344 |
+
"blocks.27.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 345 |
+
"blocks.27.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 346 |
+
"blocks.28.norm1.weight": "model-00004-of-00009.safetensors",
|
| 347 |
+
"blocks.28.norm1.bias": "model-00004-of-00009.safetensors",
|
| 348 |
+
"blocks.28.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 349 |
+
"blocks.28.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 350 |
+
"blocks.28.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 351 |
+
"blocks.28.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 352 |
+
"blocks.28.norm2.weight": "model-00004-of-00009.safetensors",
|
| 353 |
+
"blocks.28.norm2.bias": "model-00004-of-00009.safetensors",
|
| 354 |
+
"blocks.28.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 355 |
+
"blocks.28.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 356 |
+
"blocks.28.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 357 |
+
"blocks.28.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 358 |
+
"blocks.29.norm1.weight": "model-00004-of-00009.safetensors",
|
| 359 |
+
"blocks.29.norm1.bias": "model-00004-of-00009.safetensors",
|
| 360 |
+
"blocks.29.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 361 |
+
"blocks.29.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 362 |
+
"blocks.29.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 363 |
+
"blocks.29.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 364 |
+
"blocks.29.norm2.weight": "model-00004-of-00009.safetensors",
|
| 365 |
+
"blocks.29.norm2.bias": "model-00004-of-00009.safetensors",
|
| 366 |
+
"blocks.29.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 367 |
+
"blocks.29.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 368 |
+
"blocks.29.mlp.fc2.weight": "model-00004-of-00009.safetensors",
|
| 369 |
+
"blocks.29.mlp.fc2.bias": "model-00004-of-00009.safetensors",
|
| 370 |
+
"blocks.30.norm1.weight": "model-00004-of-00009.safetensors",
|
| 371 |
+
"blocks.30.norm1.bias": "model-00004-of-00009.safetensors",
|
| 372 |
+
"blocks.30.attn.qkv.weight": "model-00004-of-00009.safetensors",
|
| 373 |
+
"blocks.30.attn.qkv.bias": "model-00004-of-00009.safetensors",
|
| 374 |
+
"blocks.30.attn.proj.weight": "model-00004-of-00009.safetensors",
|
| 375 |
+
"blocks.30.attn.proj.bias": "model-00004-of-00009.safetensors",
|
| 376 |
+
"blocks.30.norm2.weight": "model-00004-of-00009.safetensors",
|
| 377 |
+
"blocks.30.norm2.bias": "model-00004-of-00009.safetensors",
|
| 378 |
+
"blocks.30.mlp.fc1.weight": "model-00004-of-00009.safetensors",
|
| 379 |
+
"blocks.30.mlp.fc1.bias": "model-00004-of-00009.safetensors",
|
| 380 |
+
"blocks.30.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 381 |
+
"blocks.30.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 382 |
+
"blocks.31.norm1.weight": "model-00005-of-00009.safetensors",
|
| 383 |
+
"blocks.31.norm1.bias": "model-00005-of-00009.safetensors",
|
| 384 |
+
"blocks.31.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 385 |
+
"blocks.31.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 386 |
+
"blocks.31.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 387 |
+
"blocks.31.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 388 |
+
"blocks.31.norm2.weight": "model-00005-of-00009.safetensors",
|
| 389 |
+
"blocks.31.norm2.bias": "model-00005-of-00009.safetensors",
|
| 390 |
+
"blocks.31.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 391 |
+
"blocks.31.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 392 |
+
"blocks.31.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 393 |
+
"blocks.31.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 394 |
+
"blocks.32.norm1.weight": "model-00005-of-00009.safetensors",
|
| 395 |
+
"blocks.32.norm1.bias": "model-00005-of-00009.safetensors",
|
| 396 |
+
"blocks.32.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 397 |
+
"blocks.32.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 398 |
+
"blocks.32.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 399 |
+
"blocks.32.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 400 |
+
"blocks.32.norm2.weight": "model-00005-of-00009.safetensors",
|
| 401 |
+
"blocks.32.norm2.bias": "model-00005-of-00009.safetensors",
|
| 402 |
+
"blocks.32.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 403 |
+
"blocks.32.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 404 |
+
"blocks.32.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 405 |
+
"blocks.32.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 406 |
+
"blocks.33.norm1.weight": "model-00005-of-00009.safetensors",
|
| 407 |
+
"blocks.33.norm1.bias": "model-00005-of-00009.safetensors",
|
| 408 |
+
"blocks.33.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 409 |
+
"blocks.33.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 410 |
+
"blocks.33.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 411 |
+
"blocks.33.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 412 |
+
"blocks.33.norm2.weight": "model-00005-of-00009.safetensors",
|
| 413 |
+
"blocks.33.norm2.bias": "model-00005-of-00009.safetensors",
|
| 414 |
+
"blocks.33.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 415 |
+
"blocks.33.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 416 |
+
"blocks.33.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 417 |
+
"blocks.33.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 418 |
+
"blocks.34.norm1.weight": "model-00005-of-00009.safetensors",
|
| 419 |
+
"blocks.34.norm1.bias": "model-00005-of-00009.safetensors",
|
| 420 |
+
"blocks.34.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 421 |
+
"blocks.34.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 422 |
+
"blocks.34.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 423 |
+
"blocks.34.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 424 |
+
"blocks.34.norm2.weight": "model-00005-of-00009.safetensors",
|
| 425 |
+
"blocks.34.norm2.bias": "model-00005-of-00009.safetensors",
|
| 426 |
+
"blocks.34.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 427 |
+
"blocks.34.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 428 |
+
"blocks.34.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 429 |
+
"blocks.34.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 430 |
+
"blocks.35.norm1.weight": "model-00005-of-00009.safetensors",
|
| 431 |
+
"blocks.35.norm1.bias": "model-00005-of-00009.safetensors",
|
| 432 |
+
"blocks.35.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 433 |
+
"blocks.35.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 434 |
+
"blocks.35.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 435 |
+
"blocks.35.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 436 |
+
"blocks.35.norm2.weight": "model-00005-of-00009.safetensors",
|
| 437 |
+
"blocks.35.norm2.bias": "model-00005-of-00009.safetensors",
|
| 438 |
+
"blocks.35.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 439 |
+
"blocks.35.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 440 |
+
"blocks.35.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 441 |
+
"blocks.35.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 442 |
+
"blocks.36.norm1.weight": "model-00005-of-00009.safetensors",
|
| 443 |
+
"blocks.36.norm1.bias": "model-00005-of-00009.safetensors",
|
| 444 |
+
"blocks.36.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 445 |
+
"blocks.36.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 446 |
+
"blocks.36.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 447 |
+
"blocks.36.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 448 |
+
"blocks.36.norm2.weight": "model-00005-of-00009.safetensors",
|
| 449 |
+
"blocks.36.norm2.bias": "model-00005-of-00009.safetensors",
|
| 450 |
+
"blocks.36.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 451 |
+
"blocks.36.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 452 |
+
"blocks.36.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 453 |
+
"blocks.36.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 454 |
+
"blocks.37.norm1.weight": "model-00005-of-00009.safetensors",
|
| 455 |
+
"blocks.37.norm1.bias": "model-00005-of-00009.safetensors",
|
| 456 |
+
"blocks.37.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 457 |
+
"blocks.37.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 458 |
+
"blocks.37.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 459 |
+
"blocks.37.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 460 |
+
"blocks.37.norm2.weight": "model-00005-of-00009.safetensors",
|
| 461 |
+
"blocks.37.norm2.bias": "model-00005-of-00009.safetensors",
|
| 462 |
+
"blocks.37.mlp.fc1.weight": "model-00005-of-00009.safetensors",
|
| 463 |
+
"blocks.37.mlp.fc1.bias": "model-00005-of-00009.safetensors",
|
| 464 |
+
"blocks.37.mlp.fc2.weight": "model-00005-of-00009.safetensors",
|
| 465 |
+
"blocks.37.mlp.fc2.bias": "model-00005-of-00009.safetensors",
|
| 466 |
+
"blocks.38.norm1.weight": "model-00005-of-00009.safetensors",
|
| 467 |
+
"blocks.38.norm1.bias": "model-00005-of-00009.safetensors",
|
| 468 |
+
"blocks.38.attn.qkv.weight": "model-00005-of-00009.safetensors",
|
| 469 |
+
"blocks.38.attn.qkv.bias": "model-00005-of-00009.safetensors",
|
| 470 |
+
"blocks.38.attn.proj.weight": "model-00005-of-00009.safetensors",
|
| 471 |
+
"blocks.38.attn.proj.bias": "model-00005-of-00009.safetensors",
|
| 472 |
+
"blocks.38.norm2.weight": "model-00005-of-00009.safetensors",
|
| 473 |
+
"blocks.38.norm2.bias": "model-00005-of-00009.safetensors",
|
| 474 |
+
"blocks.38.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 475 |
+
"blocks.38.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 476 |
+
"blocks.38.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 477 |
+
"blocks.38.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 478 |
+
"blocks.39.norm1.weight": "model-00006-of-00009.safetensors",
|
| 479 |
+
"blocks.39.norm1.bias": "model-00006-of-00009.safetensors",
|
| 480 |
+
"blocks.39.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 481 |
+
"blocks.39.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 482 |
+
"blocks.39.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 483 |
+
"blocks.39.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 484 |
+
"blocks.39.norm2.weight": "model-00006-of-00009.safetensors",
|
| 485 |
+
"blocks.39.norm2.bias": "model-00006-of-00009.safetensors",
|
| 486 |
+
"blocks.39.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 487 |
+
"blocks.39.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 488 |
+
"blocks.39.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 489 |
+
"blocks.39.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 490 |
+
"blocks.40.norm1.weight": "model-00006-of-00009.safetensors",
|
| 491 |
+
"blocks.40.norm1.bias": "model-00006-of-00009.safetensors",
|
| 492 |
+
"blocks.40.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 493 |
+
"blocks.40.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 494 |
+
"blocks.40.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 495 |
+
"blocks.40.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 496 |
+
"blocks.40.norm2.weight": "model-00006-of-00009.safetensors",
|
| 497 |
+
"blocks.40.norm2.bias": "model-00006-of-00009.safetensors",
|
| 498 |
+
"blocks.40.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 499 |
+
"blocks.40.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 500 |
+
"blocks.40.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 501 |
+
"blocks.40.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 502 |
+
"blocks.41.norm1.weight": "model-00006-of-00009.safetensors",
|
| 503 |
+
"blocks.41.norm1.bias": "model-00006-of-00009.safetensors",
|
| 504 |
+
"blocks.41.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 505 |
+
"blocks.41.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 506 |
+
"blocks.41.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 507 |
+
"blocks.41.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 508 |
+
"blocks.41.norm2.weight": "model-00006-of-00009.safetensors",
|
| 509 |
+
"blocks.41.norm2.bias": "model-00006-of-00009.safetensors",
|
| 510 |
+
"blocks.41.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 511 |
+
"blocks.41.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 512 |
+
"blocks.41.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 513 |
+
"blocks.41.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 514 |
+
"blocks.42.norm1.weight": "model-00006-of-00009.safetensors",
|
| 515 |
+
"blocks.42.norm1.bias": "model-00006-of-00009.safetensors",
|
| 516 |
+
"blocks.42.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 517 |
+
"blocks.42.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 518 |
+
"blocks.42.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 519 |
+
"blocks.42.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 520 |
+
"blocks.42.norm2.weight": "model-00006-of-00009.safetensors",
|
| 521 |
+
"blocks.42.norm2.bias": "model-00006-of-00009.safetensors",
|
| 522 |
+
"blocks.42.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 523 |
+
"blocks.42.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 524 |
+
"blocks.42.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 525 |
+
"blocks.42.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 526 |
+
"blocks.43.norm1.weight": "model-00006-of-00009.safetensors",
|
| 527 |
+
"blocks.43.norm1.bias": "model-00006-of-00009.safetensors",
|
| 528 |
+
"blocks.43.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 529 |
+
"blocks.43.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 530 |
+
"blocks.43.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 531 |
+
"blocks.43.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 532 |
+
"blocks.43.norm2.weight": "model-00006-of-00009.safetensors",
|
| 533 |
+
"blocks.43.norm2.bias": "model-00006-of-00009.safetensors",
|
| 534 |
+
"blocks.43.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 535 |
+
"blocks.43.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 536 |
+
"blocks.43.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 537 |
+
"blocks.43.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 538 |
+
"blocks.44.norm1.weight": "model-00006-of-00009.safetensors",
|
| 539 |
+
"blocks.44.norm1.bias": "model-00006-of-00009.safetensors",
|
| 540 |
+
"blocks.44.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 541 |
+
"blocks.44.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 542 |
+
"blocks.44.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 543 |
+
"blocks.44.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 544 |
+
"blocks.44.norm2.weight": "model-00006-of-00009.safetensors",
|
| 545 |
+
"blocks.44.norm2.bias": "model-00006-of-00009.safetensors",
|
| 546 |
+
"blocks.44.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 547 |
+
"blocks.44.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 548 |
+
"blocks.44.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 549 |
+
"blocks.44.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 550 |
+
"blocks.45.norm1.weight": "model-00006-of-00009.safetensors",
|
| 551 |
+
"blocks.45.norm1.bias": "model-00006-of-00009.safetensors",
|
| 552 |
+
"blocks.45.attn.qkv.weight": "model-00006-of-00009.safetensors",
|
| 553 |
+
"blocks.45.attn.qkv.bias": "model-00006-of-00009.safetensors",
|
| 554 |
+
"blocks.45.attn.proj.weight": "model-00006-of-00009.safetensors",
|
| 555 |
+
"blocks.45.attn.proj.bias": "model-00006-of-00009.safetensors",
|
| 556 |
+
"blocks.45.norm2.weight": "model-00006-of-00009.safetensors",
|
| 557 |
+
"blocks.45.norm2.bias": "model-00006-of-00009.safetensors",
|
| 558 |
+
"blocks.45.mlp.fc1.weight": "model-00006-of-00009.safetensors",
|
| 559 |
+
"blocks.45.mlp.fc1.bias": "model-00006-of-00009.safetensors",
|
| 560 |
+
"blocks.45.mlp.fc2.weight": "model-00006-of-00009.safetensors",
|
| 561 |
+
"blocks.45.mlp.fc2.bias": "model-00006-of-00009.safetensors",
|
| 562 |
+
"blocks.46.norm1.weight": "model-00006-of-00009.safetensors",
|
| 563 |
+
"blocks.46.norm1.bias": "model-00006-of-00009.safetensors",
|
| 564 |
+
"blocks.46.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 565 |
+
"blocks.46.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 566 |
+
"blocks.46.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 567 |
+
"blocks.46.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 568 |
+
"blocks.46.norm2.weight": "model-00007-of-00009.safetensors",
|
| 569 |
+
"blocks.46.norm2.bias": "model-00007-of-00009.safetensors",
|
| 570 |
+
"blocks.46.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 571 |
+
"blocks.46.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 572 |
+
"blocks.46.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 573 |
+
"blocks.46.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 574 |
+
"blocks.47.norm1.weight": "model-00007-of-00009.safetensors",
|
| 575 |
+
"blocks.47.norm1.bias": "model-00007-of-00009.safetensors",
|
| 576 |
+
"blocks.47.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 577 |
+
"blocks.47.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 578 |
+
"blocks.47.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 579 |
+
"blocks.47.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 580 |
+
"blocks.47.norm2.weight": "model-00007-of-00009.safetensors",
|
| 581 |
+
"blocks.47.norm2.bias": "model-00007-of-00009.safetensors",
|
| 582 |
+
"blocks.47.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 583 |
+
"blocks.47.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 584 |
+
"blocks.47.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 585 |
+
"blocks.47.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 586 |
+
"blocks.48.norm1.weight": "model-00007-of-00009.safetensors",
|
| 587 |
+
"blocks.48.norm1.bias": "model-00007-of-00009.safetensors",
|
| 588 |
+
"blocks.48.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 589 |
+
"blocks.48.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 590 |
+
"blocks.48.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 591 |
+
"blocks.48.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 592 |
+
"blocks.48.norm2.weight": "model-00007-of-00009.safetensors",
|
| 593 |
+
"blocks.48.norm2.bias": "model-00007-of-00009.safetensors",
|
| 594 |
+
"blocks.48.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 595 |
+
"blocks.48.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 596 |
+
"blocks.48.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 597 |
+
"blocks.48.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 598 |
+
"blocks.49.norm1.weight": "model-00007-of-00009.safetensors",
|
| 599 |
+
"blocks.49.norm1.bias": "model-00007-of-00009.safetensors",
|
| 600 |
+
"blocks.49.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 601 |
+
"blocks.49.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 602 |
+
"blocks.49.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 603 |
+
"blocks.49.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 604 |
+
"blocks.49.norm2.weight": "model-00007-of-00009.safetensors",
|
| 605 |
+
"blocks.49.norm2.bias": "model-00007-of-00009.safetensors",
|
| 606 |
+
"blocks.49.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 607 |
+
"blocks.49.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 608 |
+
"blocks.49.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 609 |
+
"blocks.49.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 610 |
+
"blocks.50.norm1.weight": "model-00007-of-00009.safetensors",
|
| 611 |
+
"blocks.50.norm1.bias": "model-00007-of-00009.safetensors",
|
| 612 |
+
"blocks.50.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 613 |
+
"blocks.50.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 614 |
+
"blocks.50.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 615 |
+
"blocks.50.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 616 |
+
"blocks.50.norm2.weight": "model-00007-of-00009.safetensors",
|
| 617 |
+
"blocks.50.norm2.bias": "model-00007-of-00009.safetensors",
|
| 618 |
+
"blocks.50.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 619 |
+
"blocks.50.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 620 |
+
"blocks.50.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 621 |
+
"blocks.50.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 622 |
+
"blocks.51.norm1.weight": "model-00007-of-00009.safetensors",
|
| 623 |
+
"blocks.51.norm1.bias": "model-00007-of-00009.safetensors",
|
| 624 |
+
"blocks.51.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 625 |
+
"blocks.51.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 626 |
+
"blocks.51.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 627 |
+
"blocks.51.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 628 |
+
"blocks.51.norm2.weight": "model-00007-of-00009.safetensors",
|
| 629 |
+
"blocks.51.norm2.bias": "model-00007-of-00009.safetensors",
|
| 630 |
+
"blocks.51.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 631 |
+
"blocks.51.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 632 |
+
"blocks.51.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 633 |
+
"blocks.51.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 634 |
+
"blocks.52.norm1.weight": "model-00007-of-00009.safetensors",
|
| 635 |
+
"blocks.52.norm1.bias": "model-00007-of-00009.safetensors",
|
| 636 |
+
"blocks.52.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 637 |
+
"blocks.52.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 638 |
+
"blocks.52.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 639 |
+
"blocks.52.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 640 |
+
"blocks.52.norm2.weight": "model-00007-of-00009.safetensors",
|
| 641 |
+
"blocks.52.norm2.bias": "model-00007-of-00009.safetensors",
|
| 642 |
+
"blocks.52.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 643 |
+
"blocks.52.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 644 |
+
"blocks.52.mlp.fc2.weight": "model-00007-of-00009.safetensors",
|
| 645 |
+
"blocks.52.mlp.fc2.bias": "model-00007-of-00009.safetensors",
|
| 646 |
+
"blocks.53.norm1.weight": "model-00007-of-00009.safetensors",
|
| 647 |
+
"blocks.53.norm1.bias": "model-00007-of-00009.safetensors",
|
| 648 |
+
"blocks.53.attn.qkv.weight": "model-00007-of-00009.safetensors",
|
| 649 |
+
"blocks.53.attn.qkv.bias": "model-00007-of-00009.safetensors",
|
| 650 |
+
"blocks.53.attn.proj.weight": "model-00007-of-00009.safetensors",
|
| 651 |
+
"blocks.53.attn.proj.bias": "model-00007-of-00009.safetensors",
|
| 652 |
+
"blocks.53.norm2.weight": "model-00007-of-00009.safetensors",
|
| 653 |
+
"blocks.53.norm2.bias": "model-00007-of-00009.safetensors",
|
| 654 |
+
"blocks.53.mlp.fc1.weight": "model-00007-of-00009.safetensors",
|
| 655 |
+
"blocks.53.mlp.fc1.bias": "model-00007-of-00009.safetensors",
|
| 656 |
+
"blocks.53.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 657 |
+
"blocks.53.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 658 |
+
"blocks.54.norm1.weight": "model-00008-of-00009.safetensors",
|
| 659 |
+
"blocks.54.norm1.bias": "model-00008-of-00009.safetensors",
|
| 660 |
+
"blocks.54.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 661 |
+
"blocks.54.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 662 |
+
"blocks.54.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 663 |
+
"blocks.54.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 664 |
+
"blocks.54.norm2.weight": "model-00008-of-00009.safetensors",
|
| 665 |
+
"blocks.54.norm2.bias": "model-00008-of-00009.safetensors",
|
| 666 |
+
"blocks.54.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 667 |
+
"blocks.54.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 668 |
+
"blocks.54.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 669 |
+
"blocks.54.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 670 |
+
"blocks.55.norm1.weight": "model-00008-of-00009.safetensors",
|
| 671 |
+
"blocks.55.norm1.bias": "model-00008-of-00009.safetensors",
|
| 672 |
+
"blocks.55.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 673 |
+
"blocks.55.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 674 |
+
"blocks.55.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 675 |
+
"blocks.55.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 676 |
+
"blocks.55.norm2.weight": "model-00008-of-00009.safetensors",
|
| 677 |
+
"blocks.55.norm2.bias": "model-00008-of-00009.safetensors",
|
| 678 |
+
"blocks.55.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 679 |
+
"blocks.55.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 680 |
+
"blocks.55.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 681 |
+
"blocks.55.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 682 |
+
"blocks.56.norm1.weight": "model-00008-of-00009.safetensors",
|
| 683 |
+
"blocks.56.norm1.bias": "model-00008-of-00009.safetensors",
|
| 684 |
+
"blocks.56.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 685 |
+
"blocks.56.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 686 |
+
"blocks.56.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 687 |
+
"blocks.56.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 688 |
+
"blocks.56.norm2.weight": "model-00008-of-00009.safetensors",
|
| 689 |
+
"blocks.56.norm2.bias": "model-00008-of-00009.safetensors",
|
| 690 |
+
"blocks.56.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 691 |
+
"blocks.56.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 692 |
+
"blocks.56.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 693 |
+
"blocks.56.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 694 |
+
"blocks.57.norm1.weight": "model-00008-of-00009.safetensors",
|
| 695 |
+
"blocks.57.norm1.bias": "model-00008-of-00009.safetensors",
|
| 696 |
+
"blocks.57.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 697 |
+
"blocks.57.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 698 |
+
"blocks.57.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 699 |
+
"blocks.57.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 700 |
+
"blocks.57.norm2.weight": "model-00008-of-00009.safetensors",
|
| 701 |
+
"blocks.57.norm2.bias": "model-00008-of-00009.safetensors",
|
| 702 |
+
"blocks.57.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 703 |
+
"blocks.57.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 704 |
+
"blocks.57.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 705 |
+
"blocks.57.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 706 |
+
"blocks.58.norm1.weight": "model-00008-of-00009.safetensors",
|
| 707 |
+
"blocks.58.norm1.bias": "model-00008-of-00009.safetensors",
|
| 708 |
+
"blocks.58.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 709 |
+
"blocks.58.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 710 |
+
"blocks.58.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 711 |
+
"blocks.58.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 712 |
+
"blocks.58.norm2.weight": "model-00008-of-00009.safetensors",
|
| 713 |
+
"blocks.58.norm2.bias": "model-00008-of-00009.safetensors",
|
| 714 |
+
"blocks.58.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 715 |
+
"blocks.58.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 716 |
+
"blocks.58.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 717 |
+
"blocks.58.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 718 |
+
"blocks.59.norm1.weight": "model-00008-of-00009.safetensors",
|
| 719 |
+
"blocks.59.norm1.bias": "model-00008-of-00009.safetensors",
|
| 720 |
+
"blocks.59.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 721 |
+
"blocks.59.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 722 |
+
"blocks.59.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 723 |
+
"blocks.59.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 724 |
+
"blocks.59.norm2.weight": "model-00008-of-00009.safetensors",
|
| 725 |
+
"blocks.59.norm2.bias": "model-00008-of-00009.safetensors",
|
| 726 |
+
"blocks.59.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 727 |
+
"blocks.59.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 728 |
+
"blocks.59.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 729 |
+
"blocks.59.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 730 |
+
"blocks.60.norm1.weight": "model-00008-of-00009.safetensors",
|
| 731 |
+
"blocks.60.norm1.bias": "model-00008-of-00009.safetensors",
|
| 732 |
+
"blocks.60.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 733 |
+
"blocks.60.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 734 |
+
"blocks.60.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 735 |
+
"blocks.60.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 736 |
+
"blocks.60.norm2.weight": "model-00008-of-00009.safetensors",
|
| 737 |
+
"blocks.60.norm2.bias": "model-00008-of-00009.safetensors",
|
| 738 |
+
"blocks.60.mlp.fc1.weight": "model-00008-of-00009.safetensors",
|
| 739 |
+
"blocks.60.mlp.fc1.bias": "model-00008-of-00009.safetensors",
|
| 740 |
+
"blocks.60.mlp.fc2.weight": "model-00008-of-00009.safetensors",
|
| 741 |
+
"blocks.60.mlp.fc2.bias": "model-00008-of-00009.safetensors",
|
| 742 |
+
"blocks.61.norm1.weight": "model-00008-of-00009.safetensors",
|
| 743 |
+
"blocks.61.norm1.bias": "model-00008-of-00009.safetensors",
|
| 744 |
+
"blocks.61.attn.qkv.weight": "model-00008-of-00009.safetensors",
|
| 745 |
+
"blocks.61.attn.qkv.bias": "model-00008-of-00009.safetensors",
|
| 746 |
+
"blocks.61.attn.proj.weight": "model-00008-of-00009.safetensors",
|
| 747 |
+
"blocks.61.attn.proj.bias": "model-00008-of-00009.safetensors",
|
| 748 |
+
"blocks.61.norm2.weight": "model-00008-of-00009.safetensors",
|
| 749 |
+
"blocks.61.norm2.bias": "model-00008-of-00009.safetensors",
|
| 750 |
+
"blocks.61.mlp.fc1.weight": "model-00009-of-00009.safetensors",
|
| 751 |
+
"blocks.61.mlp.fc1.bias": "model-00009-of-00009.safetensors",
|
| 752 |
+
"blocks.61.mlp.fc2.weight": "model-00009-of-00009.safetensors",
|
| 753 |
+
"blocks.61.mlp.fc2.bias": "model-00009-of-00009.safetensors",
|
| 754 |
+
"blocks.62.norm1.weight": "model-00009-of-00009.safetensors",
|
| 755 |
+
"blocks.62.norm1.bias": "model-00009-of-00009.safetensors",
|
| 756 |
+
"blocks.62.attn.qkv.weight": "model-00009-of-00009.safetensors",
|
| 757 |
+
"blocks.62.attn.qkv.bias": "model-00009-of-00009.safetensors",
|
| 758 |
+
"blocks.62.attn.proj.weight": "model-00009-of-00009.safetensors",
|
| 759 |
+
"blocks.62.attn.proj.bias": "model-00009-of-00009.safetensors",
|
| 760 |
+
"blocks.62.norm2.weight": "model-00009-of-00009.safetensors",
|
| 761 |
+
"blocks.62.norm2.bias": "model-00009-of-00009.safetensors",
|
| 762 |
+
"blocks.62.mlp.fc1.weight": "model-00009-of-00009.safetensors",
|
| 763 |
+
"blocks.62.mlp.fc1.bias": "model-00009-of-00009.safetensors",
|
| 764 |
+
"blocks.62.mlp.fc2.weight": "model-00009-of-00009.safetensors",
|
| 765 |
+
"blocks.62.mlp.fc2.bias": "model-00009-of-00009.safetensors",
|
| 766 |
+
"blocks.63.norm1.weight": "model-00009-of-00009.safetensors",
|
| 767 |
+
"blocks.63.norm1.bias": "model-00009-of-00009.safetensors",
|
| 768 |
+
"blocks.63.attn.qkv.weight": "model-00009-of-00009.safetensors",
|
| 769 |
+
"blocks.63.attn.qkv.bias": "model-00009-of-00009.safetensors",
|
| 770 |
+
"blocks.63.attn.proj.weight": "model-00009-of-00009.safetensors",
|
| 771 |
+
"blocks.63.attn.proj.bias": "model-00009-of-00009.safetensors",
|
| 772 |
+
"blocks.63.norm2.weight": "model-00009-of-00009.safetensors",
|
| 773 |
+
"blocks.63.norm2.bias": "model-00009-of-00009.safetensors",
|
| 774 |
+
"blocks.63.mlp.fc1.weight": "model-00009-of-00009.safetensors",
|
| 775 |
+
"blocks.63.mlp.fc1.bias": "model-00009-of-00009.safetensors",
|
| 776 |
+
"blocks.63.mlp.fc2.weight": "model-00009-of-00009.safetensors",
|
| 777 |
+
"blocks.63.mlp.fc2.bias": "model-00009-of-00009.safetensors"
|
| 778 |
+
}
|
| 779 |
+
}
|
modeling_eva_clip.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
from copy import deepcopy
|
| 5 |
+
from typing import Optional, Tuple
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
from torch import nn
|
| 9 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 10 |
+
from transformers.modeling_outputs import ModelOutput
|
| 11 |
+
from transformers.utils import logging
|
| 12 |
+
|
| 13 |
+
from .configuration_eva_clip import DEFAULT_VISION_CFG, EvaClipVisionConfig
|
| 14 |
+
from .legacy_eva_vit import (
|
| 15 |
+
CLIPVisionCfg,
|
| 16 |
+
EVAVisionTransformer,
|
| 17 |
+
create_norm_layer_factory,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
logger = logging.get_logger(__name__)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
if not hasattr(nn.Module, "get_parameter_or_buffer"): # pragma: no cover - compatibility shim
|
| 24 |
+
def _get_parameter_or_buffer(self, target: str):
|
| 25 |
+
try:
|
| 26 |
+
return self.get_parameter(target)
|
| 27 |
+
except AttributeError:
|
| 28 |
+
pass
|
| 29 |
+
try:
|
| 30 |
+
return self.get_buffer(target)
|
| 31 |
+
except AttributeError:
|
| 32 |
+
pass
|
| 33 |
+
raise AttributeError(f"`{target}` is neither a parameter nor a buffer.")
|
| 34 |
+
|
| 35 |
+
nn.Module.get_parameter_or_buffer = _get_parameter_or_buffer # type: ignore[attr-defined]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class EvaClipVisionModelOutput(ModelOutput):
|
| 39 |
+
last_hidden_state: torch.FloatTensor
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class EvaClipVisionModel(PreTrainedModel):
|
| 43 |
+
config_class = EvaClipVisionConfig
|
| 44 |
+
base_model_prefix = "vision_model"
|
| 45 |
+
main_input_name = "pixel_values"
|
| 46 |
+
supports_gradient_checkpointing = True
|
| 47 |
+
|
| 48 |
+
def __init__(self, config: EvaClipVisionConfig) -> None:
|
| 49 |
+
super().__init__(config)
|
| 50 |
+
vision_cfg = self._build_vision_cfg(config)
|
| 51 |
+
self._vision_cfg_dict = vision_cfg
|
| 52 |
+
norm_layer_factory = create_norm_layer_factory(vision_cfg.fusedLN, eps=1e-6)
|
| 53 |
+
|
| 54 |
+
vision_heads = vision_cfg.width // vision_cfg.head_width
|
| 55 |
+
self.vision_model = EVAVisionTransformer(
|
| 56 |
+
img_size=vision_cfg.image_size,
|
| 57 |
+
patch_size=vision_cfg.patch_size,
|
| 58 |
+
in_chans=3,
|
| 59 |
+
num_classes=config.embed_dim,
|
| 60 |
+
embed_dim=vision_cfg.width,
|
| 61 |
+
depth=vision_cfg.layers,
|
| 62 |
+
num_heads=vision_heads,
|
| 63 |
+
mlp_ratio=vision_cfg.mlp_ratio,
|
| 64 |
+
qkv_bias=vision_cfg.qkv_bias,
|
| 65 |
+
drop_path_rate=vision_cfg.drop_path_rate or 0.0,
|
| 66 |
+
norm_layer=norm_layer_factory,
|
| 67 |
+
xattn=vision_cfg.xattn,
|
| 68 |
+
rope=vision_cfg.rope,
|
| 69 |
+
postnorm=vision_cfg.postnorm,
|
| 70 |
+
pt_hw_seq_len=vision_cfg.pt_hw_seq_len,
|
| 71 |
+
intp_freq=vision_cfg.intp_freq,
|
| 72 |
+
naiveswiglu=vision_cfg.naiveswiglu,
|
| 73 |
+
subln=vision_cfg.subln,
|
| 74 |
+
patch_dropout=vision_cfg.patch_dropout,
|
| 75 |
+
use_mean_pooling=vision_cfg.global_average_pool,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
self.image_size = vision_cfg.image_size
|
| 79 |
+
self.patch_size = vision_cfg.patch_size
|
| 80 |
+
|
| 81 |
+
def _build_vision_cfg(self, config: EvaClipVisionConfig) -> CLIPVisionCfg:
|
| 82 |
+
merged = deepcopy(DEFAULT_VISION_CFG)
|
| 83 |
+
merged.update(config.vision_cfg or {})
|
| 84 |
+
if "pt_hw_seq_len" not in merged and merged.get("image_size") and merged.get("patch_size"):
|
| 85 |
+
merged["pt_hw_seq_len"] = merged["image_size"] // merged["patch_size"]
|
| 86 |
+
return CLIPVisionCfg(**merged)
|
| 87 |
+
|
| 88 |
+
def forward(
|
| 89 |
+
self,
|
| 90 |
+
pixel_values: torch.FloatTensor,
|
| 91 |
+
return_dict: bool = True,
|
| 92 |
+
output_hidden_states: bool = False,
|
| 93 |
+
output_attentions: bool = False,
|
| 94 |
+
) -> Tuple[torch.FloatTensor, ...] | EvaClipVisionModelOutput:
|
| 95 |
+
del output_hidden_states, output_attentions # not supported
|
| 96 |
+
hidden_states = self.vision_model(pixel_values, return_all_features=True)
|
| 97 |
+
if hidden_states.shape[1] > 0:
|
| 98 |
+
hidden_states = hidden_states[:, 1:, :]
|
| 99 |
+
|
| 100 |
+
if not return_dict:
|
| 101 |
+
return (hidden_states,)
|
| 102 |
+
return EvaClipVisionModelOutput(last_hidden_state=hidden_states)
|
| 103 |
+
|
| 104 |
+
def gradient_checkpointing_enable(self, gradient_checkpointing_kwargs: Optional[dict] = None) -> None:
|
| 105 |
+
del gradient_checkpointing_kwargs
|
| 106 |
+
if hasattr(self.vision_model, "set_grad_checkpointing"):
|
| 107 |
+
self.vision_model.set_grad_checkpointing(True)
|
| 108 |
+
|
| 109 |
+
# The EVA checkpoints omit the top-level module prefix. Mirror that convention here.
|
| 110 |
+
def load_state_dict(self, state_dict, strict: bool = True):
|
| 111 |
+
if any(k.startswith("vision_model.") for k in state_dict):
|
| 112 |
+
processed = state_dict
|
| 113 |
+
else:
|
| 114 |
+
processed = OrderedDict((f"vision_model.{k}", v) for k, v in state_dict.items())
|
| 115 |
+
return super().load_state_dict(processed, strict=strict)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
__all__ = ["EvaClipVisionModel", "EvaClipVisionModelOutput"]
|
| 119 |
+
|