Upload model
#5
by
mranzinger
- opened
- common.py +45 -6
- config.json +2 -1
- dinov2_arch.py +1016 -0
- enable_cpe_support.py +18 -1
- eradio_model.py +9 -3
- extra_models.py +4 -1
- extra_timm_models.py +5 -1
- hf_model.py +24 -0
- open_clip_adaptor.py +7 -7
- radio_model.py +22 -0
- utils.py +41 -0
- vit_patch_generator.py +13 -28
common.py
CHANGED
@@ -18,6 +18,7 @@ class RadioResource:
|
|
18 |
patch_size: int
|
19 |
max_resolution: int
|
20 |
preferred_resolution: Resolution
|
|
|
21 |
vitdet_num_windowed: Optional[int] = None
|
22 |
vitdet_num_global: Optional[int] = None
|
23 |
|
@@ -88,22 +89,60 @@ RESOURCE_MAP = {
|
|
88 |
preferred_resolution=Resolution(512, 512),
|
89 |
),
|
90 |
# C-RADIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
"c-radio_v3-l": RadioResource(
|
92 |
-
# NOTE:
|
93 |
-
#
|
94 |
"https://huggingface.co/nvidia/C-RADIOv3-L/resolve/main/c-radio-v3_l_half.pth.tar?download=true",
|
95 |
patch_size=16,
|
96 |
max_resolution=2048,
|
97 |
preferred_resolution=Resolution(512, 512),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
),
|
99 |
"c-radio_v3-g": RadioResource(
|
100 |
-
# NOTE:
|
101 |
-
#
|
102 |
-
"https://huggingface.co/nvidia/C-RADIOv3-
|
103 |
patch_size=16,
|
104 |
max_resolution=2048,
|
105 |
preferred_resolution=Resolution(512, 512),
|
|
|
106 |
),
|
107 |
}
|
108 |
|
109 |
-
DEFAULT_VERSION = "
|
|
|
18 |
patch_size: int
|
19 |
max_resolution: int
|
20 |
preferred_resolution: Resolution
|
21 |
+
supports_vitdet: bool = True
|
22 |
vitdet_num_windowed: Optional[int] = None
|
23 |
vitdet_num_global: Optional[int] = None
|
24 |
|
|
|
89 |
preferred_resolution=Resolution(512, 512),
|
90 |
),
|
91 |
# C-RADIO
|
92 |
+
"c-radio_v2-g": RadioResource(
|
93 |
+
# NOTE: C-RADIO models are bound by different license terms than that present in the LICENSE file.
|
94 |
+
# Please refer to the readme, or to https://huggingface.co/nvidia/C-RADIOv2-g for more information.
|
95 |
+
"https://huggingface.co/nvidia/C-RADIOv2-g/resolve/main/c-radio_v2-g_half.pth.tar",
|
96 |
+
patch_size=16,
|
97 |
+
max_resolution=2048,
|
98 |
+
preferred_resolution=(768, 768),
|
99 |
+
vitdet_num_global=8,
|
100 |
+
),
|
101 |
+
"c-radio_v2-vlm-h": RadioResource(
|
102 |
+
# NOTE: C-RADIO models are bound by different license terms than that present in the LICENSE file.
|
103 |
+
# Please refer to the readme, or to https://huggingface.co/nvidia/C-RADIOv2-VLM-H for more information.
|
104 |
+
"https://huggingface.co/nvidia/C-RADIOv2-VLM-H/resolve/main/c-radio_v2-vlm-h.pth.tar",
|
105 |
+
patch_size=16,
|
106 |
+
max_resolution=2048,
|
107 |
+
preferred_resolution=(768, 768),
|
108 |
+
vitdet_num_global=8,
|
109 |
+
),
|
110 |
+
"c-radio_v3-b": RadioResource(
|
111 |
+
# NOTE: C-RADIO models are bound by different license terms than that present in the LICENSE file.
|
112 |
+
# Please refer to the readme, or to https://huggingface.co/nvidia/C-RADIOv3-B for more information.
|
113 |
+
"https://huggingface.co/nvidia/C-RADIOv3-B/resolve/main/c-radio_v3-b_half.pth.tar?download=true",
|
114 |
+
patch_size=16,
|
115 |
+
max_resolution=2048,
|
116 |
+
preferred_resolution=Resolution(512, 512),
|
117 |
+
supports_vitdet=False,
|
118 |
+
),
|
119 |
"c-radio_v3-l": RadioResource(
|
120 |
+
# NOTE: C-RADIO models are bound by different license terms than that present in the LICENSE file.
|
121 |
+
# Please refer to the readme, or to https://huggingface.co/nvidia/C-RADIOv3-L for more information.
|
122 |
"https://huggingface.co/nvidia/C-RADIOv3-L/resolve/main/c-radio-v3_l_half.pth.tar?download=true",
|
123 |
patch_size=16,
|
124 |
max_resolution=2048,
|
125 |
preferred_resolution=Resolution(512, 512),
|
126 |
+
supports_vitdet=False,
|
127 |
+
),
|
128 |
+
"c-radio_v3-h": RadioResource(
|
129 |
+
# NOTE: C-RADIO models are bound by different license terms than that present in the LICENSE file.
|
130 |
+
# Please refer to the readme, or to https://huggingface.co/nvidia/C-RADIOv3-H for more information.
|
131 |
+
"https://huggingface.co/nvidia/C-RADIOv3-H/resolve/main/c-radio_v3-h_half.pth.tar?download=true",
|
132 |
+
patch_size=16,
|
133 |
+
max_resolution=2048,
|
134 |
+
preferred_resolution=Resolution(512, 512),
|
135 |
+
supports_vitdet=False,
|
136 |
),
|
137 |
"c-radio_v3-g": RadioResource(
|
138 |
+
# NOTE: C-RADIO models are bound by different license terms than that present in the LICENSE file.
|
139 |
+
# Please refer to the readme, or to https://huggingface.co/nvidia/C-RADIOv3-g for more information.
|
140 |
+
"https://huggingface.co/nvidia/C-RADIOv3-g/resolve/main/c-radio_v3-g_half.pth.tar?download=true",
|
141 |
patch_size=16,
|
142 |
max_resolution=2048,
|
143 |
preferred_resolution=Resolution(512, 512),
|
144 |
+
supports_vitdet=False,
|
145 |
),
|
146 |
}
|
147 |
|
148 |
+
DEFAULT_VERSION = "c-radio_v3-h"
|
config.json
CHANGED
@@ -31,7 +31,6 @@
|
|
31 |
"crop_pct": null,
|
32 |
"cutmix": 0.0,
|
33 |
"cutmix_minmax": null,
|
34 |
-
"damp": null,
|
35 |
"dataset_download": false,
|
36 |
"debug_full_knn": false,
|
37 |
"decay_epochs": 90,
|
@@ -226,6 +225,8 @@
|
|
226 |
"AutoConfig": "hf_model.RADIOConfig",
|
227 |
"AutoModel": "hf_model.RADIOModel"
|
228 |
},
|
|
|
|
|
229 |
"max_resolution": 2048,
|
230 |
"patch_size": 16,
|
231 |
"preferred_resolution": [
|
|
|
31 |
"crop_pct": null,
|
32 |
"cutmix": 0.0,
|
33 |
"cutmix_minmax": null,
|
|
|
34 |
"dataset_download": false,
|
35 |
"debug_full_knn": false,
|
36 |
"decay_epochs": 90,
|
|
|
225 |
"AutoConfig": "hf_model.RADIOConfig",
|
226 |
"AutoModel": "hf_model.RADIOModel"
|
227 |
},
|
228 |
+
"feature_normalizer_config": null,
|
229 |
+
"inter_feature_normalizer_config": null,
|
230 |
"max_resolution": 2048,
|
231 |
"patch_size": 16,
|
232 |
"preferred_resolution": [
|
dinov2_arch.py
ADDED
@@ -0,0 +1,1016 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
#
|
3 |
+
# This source code is licensed under the Apache License, Version 2.0
|
4 |
+
# found in the LICENSE file in the root directory of this source tree.
|
5 |
+
|
6 |
+
# References:
|
7 |
+
# https://github.com/facebookresearch/dino/blob/master/vision_transformer.py
|
8 |
+
# https://github.com/rwightman/pytorch-image-models/tree/master/timm/models/vision_transformer.py
|
9 |
+
|
10 |
+
# Nvidia
|
11 |
+
# NOTE: We re-define this model architecture primarily so that we don't have to worry about version compatibility breaking,
|
12 |
+
# but also because Huggingface does a string replace of `gamma` to something else when loading the model state,
|
13 |
+
# and this breaks loading of this model.
|
14 |
+
|
15 |
+
from enum import Enum
|
16 |
+
from functools import partial
|
17 |
+
import logging
|
18 |
+
import math
|
19 |
+
import os
|
20 |
+
import sys
|
21 |
+
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
|
22 |
+
import warnings
|
23 |
+
|
24 |
+
import torch
|
25 |
+
from torch import nn
|
26 |
+
from torch.nn import functional as F
|
27 |
+
from torch.nn.init import trunc_normal_
|
28 |
+
|
29 |
+
_torch_has_sdpa = hasattr(F, 'scaled_dot_product_attention')
|
30 |
+
|
31 |
+
|
32 |
+
XFORMERS_ENABLED = os.environ.get("XFORMERS_DISABLED") is None
|
33 |
+
try:
|
34 |
+
if XFORMERS_ENABLED:
|
35 |
+
from xformers.ops import fmha, scaled_index_add, index_select_cat, SwiGLU, memory_efficient_attention, unbind
|
36 |
+
|
37 |
+
XFORMERS_AVAILABLE = True
|
38 |
+
else:
|
39 |
+
raise ImportError
|
40 |
+
except ImportError:
|
41 |
+
XFORMERS_AVAILABLE = False
|
42 |
+
|
43 |
+
|
44 |
+
def make_2tuple(x):
|
45 |
+
if isinstance(x, tuple):
|
46 |
+
assert len(x) == 2
|
47 |
+
return x
|
48 |
+
|
49 |
+
assert isinstance(x, int)
|
50 |
+
return (x, x)
|
51 |
+
|
52 |
+
|
53 |
+
class PatchEmbed(nn.Module):
|
54 |
+
"""
|
55 |
+
2D image to patch embedding: (B,C,H,W) -> (B,N,D)
|
56 |
+
|
57 |
+
Args:
|
58 |
+
img_size: Image size.
|
59 |
+
patch_size: Patch token size.
|
60 |
+
in_chans: Number of input image channels.
|
61 |
+
embed_dim: Number of linear projection output channels.
|
62 |
+
norm_layer: Normalization layer.
|
63 |
+
"""
|
64 |
+
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
img_size: Union[int, Tuple[int, int]] = 224,
|
68 |
+
patch_size: Union[int, Tuple[int, int]] = 16,
|
69 |
+
in_chans: int = 3,
|
70 |
+
embed_dim: int = 768,
|
71 |
+
norm_layer: Optional[Callable] = None,
|
72 |
+
flatten_embedding: bool = True,
|
73 |
+
) -> None:
|
74 |
+
super().__init__()
|
75 |
+
|
76 |
+
image_HW = make_2tuple(img_size)
|
77 |
+
patch_HW = make_2tuple(patch_size)
|
78 |
+
patch_grid_size = (
|
79 |
+
image_HW[0] // patch_HW[0],
|
80 |
+
image_HW[1] // patch_HW[1],
|
81 |
+
)
|
82 |
+
|
83 |
+
self.img_size = image_HW
|
84 |
+
self.patch_size = patch_HW
|
85 |
+
self.patches_resolution = patch_grid_size
|
86 |
+
self.num_patches = patch_grid_size[0] * patch_grid_size[1]
|
87 |
+
|
88 |
+
self.in_chans = in_chans
|
89 |
+
self.embed_dim = embed_dim
|
90 |
+
|
91 |
+
self.flatten_embedding = flatten_embedding
|
92 |
+
|
93 |
+
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_HW, stride=patch_HW)
|
94 |
+
self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
|
95 |
+
|
96 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
97 |
+
_, _, H, W = x.shape
|
98 |
+
patch_H, patch_W = self.patch_size
|
99 |
+
|
100 |
+
assert H % patch_H == 0, f"Input image height {H} is not a multiple of patch height {patch_H}"
|
101 |
+
assert W % patch_W == 0, f"Input image width {W} is not a multiple of patch width: {patch_W}"
|
102 |
+
|
103 |
+
x = self.proj(x) # B C H W
|
104 |
+
H, W = x.size(2), x.size(3)
|
105 |
+
x = x.flatten(2).transpose(1, 2) # B HW C
|
106 |
+
x = self.norm(x)
|
107 |
+
if not self.flatten_embedding:
|
108 |
+
x = x.reshape(-1, H, W, self.embed_dim) # B H W C
|
109 |
+
return x
|
110 |
+
|
111 |
+
def flops(self) -> float:
|
112 |
+
Ho, Wo = self.patches_resolution
|
113 |
+
flops = Ho * Wo * self.embed_dim * self.in_chans * (self.patch_size[0] * self.patch_size[1])
|
114 |
+
if self.norm is not None:
|
115 |
+
flops += Ho * Wo * self.embed_dim
|
116 |
+
return flops
|
117 |
+
|
118 |
+
|
119 |
+
class Attention(nn.Module):
|
120 |
+
def __init__(
|
121 |
+
self,
|
122 |
+
dim: int,
|
123 |
+
num_heads: int = 8,
|
124 |
+
qkv_bias: bool = False,
|
125 |
+
proj_bias: bool = True,
|
126 |
+
attn_drop: float = 0.0,
|
127 |
+
proj_drop: float = 0.0,
|
128 |
+
) -> None:
|
129 |
+
super().__init__()
|
130 |
+
self.num_heads = num_heads
|
131 |
+
head_dim = dim // num_heads
|
132 |
+
self.scale = head_dim**-0.5
|
133 |
+
|
134 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
135 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
136 |
+
self.proj = nn.Linear(dim, dim, bias=proj_bias)
|
137 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
138 |
+
|
139 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
140 |
+
B, N, C = x.shape
|
141 |
+
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
|
142 |
+
|
143 |
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
144 |
+
if _torch_has_sdpa:
|
145 |
+
x = F.scaled_dot_product_attention(
|
146 |
+
q, k, v,
|
147 |
+
is_causal=False,
|
148 |
+
dropout_p=self.attn_drop.p if self.training else 0.,
|
149 |
+
scale=self.scale,
|
150 |
+
)
|
151 |
+
else:
|
152 |
+
q = q * self.scale
|
153 |
+
attn = q @ k.transpose(-2, -1)
|
154 |
+
|
155 |
+
attn = attn.softmax(dim=-1)
|
156 |
+
attn = self.attn_drop(attn)
|
157 |
+
x = attn @ v
|
158 |
+
|
159 |
+
x = x.transpose(1, 2).reshape(B, N, C)
|
160 |
+
x = self.proj(x)
|
161 |
+
x = self.proj_drop(x)
|
162 |
+
return x
|
163 |
+
|
164 |
+
|
165 |
+
class MemEffAttention(Attention):
|
166 |
+
def forward(self, x: torch.Tensor, attn_bias=None) -> torch.Tensor:
|
167 |
+
if not XFORMERS_AVAILABLE:
|
168 |
+
if attn_bias is not None:
|
169 |
+
raise AssertionError("xFormers is required for using nested tensors")
|
170 |
+
return super().forward(x)
|
171 |
+
|
172 |
+
B, N, C = x.shape
|
173 |
+
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads)
|
174 |
+
|
175 |
+
q, k, v = unbind(qkv, 2)
|
176 |
+
|
177 |
+
x = memory_efficient_attention(q, k, v, attn_bias=attn_bias)
|
178 |
+
x = x.reshape([B, N, C])
|
179 |
+
|
180 |
+
x = self.proj(x)
|
181 |
+
x = self.proj_drop(x)
|
182 |
+
return x
|
183 |
+
|
184 |
+
|
185 |
+
class Mlp(nn.Module):
|
186 |
+
def __init__(
|
187 |
+
self,
|
188 |
+
in_features: int,
|
189 |
+
hidden_features: Optional[int] = None,
|
190 |
+
out_features: Optional[int] = None,
|
191 |
+
act_layer: Callable[..., nn.Module] = nn.GELU,
|
192 |
+
drop: float = 0.0,
|
193 |
+
bias: bool = True,
|
194 |
+
) -> None:
|
195 |
+
super().__init__()
|
196 |
+
out_features = out_features or in_features
|
197 |
+
hidden_features = hidden_features or in_features
|
198 |
+
self.fc1 = nn.Linear(in_features, hidden_features, bias=bias)
|
199 |
+
self.act = act_layer()
|
200 |
+
self.fc2 = nn.Linear(hidden_features, out_features, bias=bias)
|
201 |
+
self.drop = nn.Dropout(drop)
|
202 |
+
|
203 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
204 |
+
x = self.fc1(x)
|
205 |
+
x = self.act(x)
|
206 |
+
x = self.drop(x)
|
207 |
+
x = self.fc2(x)
|
208 |
+
x = self.drop(x)
|
209 |
+
return x
|
210 |
+
|
211 |
+
|
212 |
+
class SwiGLUFFN(nn.Module):
|
213 |
+
def __init__(
|
214 |
+
self,
|
215 |
+
in_features: int,
|
216 |
+
hidden_features: Optional[int] = None,
|
217 |
+
out_features: Optional[int] = None,
|
218 |
+
act_layer: Callable[..., nn.Module] = None,
|
219 |
+
drop: float = 0.0,
|
220 |
+
bias: bool = True,
|
221 |
+
) -> None:
|
222 |
+
super().__init__()
|
223 |
+
out_features = out_features or in_features
|
224 |
+
hidden_features = hidden_features or in_features
|
225 |
+
self.w12 = nn.Linear(in_features, 2 * hidden_features, bias=bias)
|
226 |
+
self.w3 = nn.Linear(hidden_features, out_features, bias=bias)
|
227 |
+
|
228 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
229 |
+
x12 = self.w12(x)
|
230 |
+
x1, x2 = x12.chunk(2, dim=-1)
|
231 |
+
hidden = F.silu(x1) * x2
|
232 |
+
return self.w3(hidden)
|
233 |
+
|
234 |
+
|
235 |
+
if not XFORMERS_AVAILABLE:
|
236 |
+
SwiGLU = SwiGLUFFN
|
237 |
+
|
238 |
+
|
239 |
+
class SwiGLUFFNFused(SwiGLU):
|
240 |
+
def __init__(
|
241 |
+
self,
|
242 |
+
in_features: int,
|
243 |
+
hidden_features: Optional[int] = None,
|
244 |
+
out_features: Optional[int] = None,
|
245 |
+
act_layer: Callable[..., nn.Module] = None,
|
246 |
+
drop: float = 0.0,
|
247 |
+
bias: bool = True,
|
248 |
+
) -> None:
|
249 |
+
out_features = out_features or in_features
|
250 |
+
hidden_features = hidden_features or in_features
|
251 |
+
hidden_features = (int(hidden_features * 2 / 3) + 7) // 8 * 8
|
252 |
+
super().__init__(
|
253 |
+
in_features=in_features,
|
254 |
+
hidden_features=hidden_features,
|
255 |
+
out_features=out_features,
|
256 |
+
bias=bias,
|
257 |
+
)
|
258 |
+
|
259 |
+
|
260 |
+
def drop_path(x, drop_prob: float = 0.0, training: bool = False):
|
261 |
+
if drop_prob == 0.0 or not training:
|
262 |
+
return x
|
263 |
+
keep_prob = 1 - drop_prob
|
264 |
+
shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets
|
265 |
+
random_tensor = x.new_empty(shape).bernoulli_(keep_prob)
|
266 |
+
if keep_prob > 0.0:
|
267 |
+
random_tensor.div_(keep_prob)
|
268 |
+
output = x * random_tensor
|
269 |
+
return output
|
270 |
+
|
271 |
+
|
272 |
+
class DropPath(nn.Module):
|
273 |
+
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""
|
274 |
+
|
275 |
+
def __init__(self, drop_prob=None):
|
276 |
+
super(DropPath, self).__init__()
|
277 |
+
self.drop_prob = drop_prob
|
278 |
+
|
279 |
+
def forward(self, x):
|
280 |
+
return drop_path(x, self.drop_prob, self.training)
|
281 |
+
|
282 |
+
|
283 |
+
class LayerScale(nn.Module):
|
284 |
+
def __init__(
|
285 |
+
self,
|
286 |
+
dim: int,
|
287 |
+
init_values: Union[float, torch.Tensor] = 1e-5,
|
288 |
+
inplace: bool = False,
|
289 |
+
) -> None:
|
290 |
+
super().__init__()
|
291 |
+
self.inplace = inplace
|
292 |
+
self.grandma = nn.Parameter(init_values * torch.ones(dim))
|
293 |
+
|
294 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
295 |
+
return x.mul_(self.grandma) if self.inplace else x * self.grandma
|
296 |
+
|
297 |
+
def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
|
298 |
+
# Huggingface is absurd and it will rename strings that contain `gamma`, which means that the normal DINO implementation
|
299 |
+
# of LayerScale won't work with HFHub. So we rename the variable to 'grandma', and support loading checkpoints in either
|
300 |
+
# format
|
301 |
+
key_a = f'{prefix}gamma'
|
302 |
+
key_b = f'{prefix}grandma'
|
303 |
+
if key_a in state_dict:
|
304 |
+
gamma = state_dict[key_a]
|
305 |
+
elif key_b in state_dict:
|
306 |
+
gamma = state_dict[key_b]
|
307 |
+
else:
|
308 |
+
if strict:
|
309 |
+
raise KeyError(f"Couldn't find the key {key_a} nor {key_b} in the state dict!")
|
310 |
+
else:
|
311 |
+
missing_keys.append(key_a)
|
312 |
+
missing_keys.append(key_b)
|
313 |
+
unexpected_keys.extend(state_dict.keys())
|
314 |
+
gamma = None
|
315 |
+
|
316 |
+
if gamma is not None:
|
317 |
+
self.grandma.data.copy_(gamma)
|
318 |
+
|
319 |
+
# return super()._load_from_state_dict(state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs)
|
320 |
+
|
321 |
+
|
322 |
+
class Block(nn.Module):
|
323 |
+
def __init__(
|
324 |
+
self,
|
325 |
+
dim: int,
|
326 |
+
num_heads: int,
|
327 |
+
mlp_ratio: float = 4.0,
|
328 |
+
qkv_bias: bool = False,
|
329 |
+
proj_bias: bool = True,
|
330 |
+
ffn_bias: bool = True,
|
331 |
+
drop: float = 0.0,
|
332 |
+
attn_drop: float = 0.0,
|
333 |
+
init_values=None,
|
334 |
+
drop_path: float = 0.0,
|
335 |
+
act_layer: Callable[..., nn.Module] = nn.GELU,
|
336 |
+
norm_layer: Callable[..., nn.Module] = nn.LayerNorm,
|
337 |
+
attn_class: Callable[..., nn.Module] = Attention,
|
338 |
+
ffn_layer: Callable[..., nn.Module] = Mlp,
|
339 |
+
) -> None:
|
340 |
+
super().__init__()
|
341 |
+
# print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}")
|
342 |
+
self.norm1 = norm_layer(dim)
|
343 |
+
self.attn = attn_class(
|
344 |
+
dim,
|
345 |
+
num_heads=num_heads,
|
346 |
+
qkv_bias=qkv_bias,
|
347 |
+
proj_bias=proj_bias,
|
348 |
+
attn_drop=attn_drop,
|
349 |
+
proj_drop=drop,
|
350 |
+
)
|
351 |
+
self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
|
352 |
+
self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
|
353 |
+
|
354 |
+
self.norm2 = norm_layer(dim)
|
355 |
+
mlp_hidden_dim = int(dim * mlp_ratio)
|
356 |
+
self.mlp = ffn_layer(
|
357 |
+
in_features=dim,
|
358 |
+
hidden_features=mlp_hidden_dim,
|
359 |
+
act_layer=act_layer,
|
360 |
+
drop=drop,
|
361 |
+
bias=ffn_bias,
|
362 |
+
)
|
363 |
+
self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
|
364 |
+
self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
|
365 |
+
|
366 |
+
self.sample_drop_ratio = drop_path
|
367 |
+
|
368 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
369 |
+
def attn_residual_func(x: torch.Tensor) -> torch.Tensor:
|
370 |
+
return self.ls1(self.attn(self.norm1(x)))
|
371 |
+
|
372 |
+
def ffn_residual_func(x: torch.Tensor) -> torch.Tensor:
|
373 |
+
return self.ls2(self.mlp(self.norm2(x)))
|
374 |
+
|
375 |
+
if self.training and self.sample_drop_ratio > 0.1:
|
376 |
+
# the overhead is compensated only for a drop path rate larger than 0.1
|
377 |
+
x = drop_add_residual_stochastic_depth(
|
378 |
+
x,
|
379 |
+
residual_func=attn_residual_func,
|
380 |
+
sample_drop_ratio=self.sample_drop_ratio,
|
381 |
+
)
|
382 |
+
x = drop_add_residual_stochastic_depth(
|
383 |
+
x,
|
384 |
+
residual_func=ffn_residual_func,
|
385 |
+
sample_drop_ratio=self.sample_drop_ratio,
|
386 |
+
)
|
387 |
+
elif self.training and self.sample_drop_ratio > 0.0:
|
388 |
+
x = x + self.drop_path1(attn_residual_func(x))
|
389 |
+
x = x + self.drop_path1(ffn_residual_func(x)) # FIXME: drop_path2
|
390 |
+
else:
|
391 |
+
x = x + attn_residual_func(x)
|
392 |
+
x = x + ffn_residual_func(x)
|
393 |
+
return x
|
394 |
+
|
395 |
+
|
396 |
+
class NestedTensorBlock(Block):
|
397 |
+
def forward_nested(self, x_list: List[torch.Tensor]) -> List[torch.Tensor]:
|
398 |
+
"""
|
399 |
+
x_list contains a list of tensors to nest together and run
|
400 |
+
"""
|
401 |
+
assert isinstance(self.attn, MemEffAttention)
|
402 |
+
|
403 |
+
if self.training and self.sample_drop_ratio > 0.0:
|
404 |
+
|
405 |
+
def attn_residual_func(x: torch.Tensor, attn_bias=None) -> torch.Tensor:
|
406 |
+
return self.attn(self.norm1(x), attn_bias=attn_bias)
|
407 |
+
|
408 |
+
def ffn_residual_func(x: torch.Tensor, attn_bias=None) -> torch.Tensor:
|
409 |
+
return self.mlp(self.norm2(x))
|
410 |
+
|
411 |
+
x_list = drop_add_residual_stochastic_depth_list(
|
412 |
+
x_list,
|
413 |
+
residual_func=attn_residual_func,
|
414 |
+
sample_drop_ratio=self.sample_drop_ratio,
|
415 |
+
scaling_vector=self.ls1.grandma if isinstance(self.ls1, LayerScale) else None,
|
416 |
+
)
|
417 |
+
x_list = drop_add_residual_stochastic_depth_list(
|
418 |
+
x_list,
|
419 |
+
residual_func=ffn_residual_func,
|
420 |
+
sample_drop_ratio=self.sample_drop_ratio,
|
421 |
+
scaling_vector=self.ls2.grandma if isinstance(self.ls1, LayerScale) else None,
|
422 |
+
)
|
423 |
+
return x_list
|
424 |
+
else:
|
425 |
+
|
426 |
+
def attn_residual_func(x: torch.Tensor, attn_bias=None) -> torch.Tensor:
|
427 |
+
return self.ls1(self.attn(self.norm1(x), attn_bias=attn_bias))
|
428 |
+
|
429 |
+
def ffn_residual_func(x: torch.Tensor, attn_bias=None) -> torch.Tensor:
|
430 |
+
return self.ls2(self.mlp(self.norm2(x)))
|
431 |
+
|
432 |
+
attn_bias, x = get_attn_bias_and_cat(x_list)
|
433 |
+
x = x + attn_residual_func(x, attn_bias=attn_bias)
|
434 |
+
x = x + ffn_residual_func(x)
|
435 |
+
return attn_bias.split(x)
|
436 |
+
|
437 |
+
def forward(self, x_or_x_list):
|
438 |
+
if isinstance(x_or_x_list, torch.Tensor):
|
439 |
+
return super().forward(x_or_x_list)
|
440 |
+
elif isinstance(x_or_x_list, list):
|
441 |
+
if not XFORMERS_AVAILABLE:
|
442 |
+
raise AssertionError("xFormers is required for using nested tensors")
|
443 |
+
return self.forward_nested(x_or_x_list)
|
444 |
+
else:
|
445 |
+
raise AssertionError
|
446 |
+
|
447 |
+
|
448 |
+
def drop_add_residual_stochastic_depth(
|
449 |
+
x: torch.Tensor,
|
450 |
+
residual_func: Callable[[torch.Tensor], torch.Tensor],
|
451 |
+
sample_drop_ratio: float = 0.0,
|
452 |
+
) -> torch.Tensor:
|
453 |
+
# 1) extract subset using permutation
|
454 |
+
b, n, d = x.shape
|
455 |
+
sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1)
|
456 |
+
brange = (torch.randperm(b, device=x.device))[:sample_subset_size]
|
457 |
+
x_subset = x[brange]
|
458 |
+
|
459 |
+
# 2) apply residual_func to get residual
|
460 |
+
residual = residual_func(x_subset)
|
461 |
+
|
462 |
+
x_flat = x.flatten(1)
|
463 |
+
residual = residual.flatten(1)
|
464 |
+
|
465 |
+
residual_scale_factor = b / sample_subset_size
|
466 |
+
|
467 |
+
# 3) add the residual
|
468 |
+
x_plus_residual = torch.index_add(x_flat, 0, brange, residual.to(dtype=x.dtype), alpha=residual_scale_factor)
|
469 |
+
return x_plus_residual.view_as(x)
|
470 |
+
|
471 |
+
|
472 |
+
def get_branges_scales(x, sample_drop_ratio=0.0):
|
473 |
+
b, n, d = x.shape
|
474 |
+
sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1)
|
475 |
+
brange = (torch.randperm(b, device=x.device))[:sample_subset_size]
|
476 |
+
residual_scale_factor = b / sample_subset_size
|
477 |
+
return brange, residual_scale_factor
|
478 |
+
|
479 |
+
|
480 |
+
def add_residual(x, brange, residual, residual_scale_factor, scaling_vector=None):
|
481 |
+
if scaling_vector is None:
|
482 |
+
x_flat = x.flatten(1)
|
483 |
+
residual = residual.flatten(1)
|
484 |
+
x_plus_residual = torch.index_add(x_flat, 0, brange, residual.to(dtype=x.dtype), alpha=residual_scale_factor)
|
485 |
+
else:
|
486 |
+
x_plus_residual = scaled_index_add(
|
487 |
+
x, brange, residual.to(dtype=x.dtype), scaling=scaling_vector, alpha=residual_scale_factor
|
488 |
+
)
|
489 |
+
return x_plus_residual
|
490 |
+
|
491 |
+
|
492 |
+
attn_bias_cache: Dict[Tuple, Any] = {}
|
493 |
+
|
494 |
+
|
495 |
+
def get_attn_bias_and_cat(x_list, branges=None):
|
496 |
+
"""
|
497 |
+
this will perform the index select, cat the tensors, and provide the attn_bias from cache
|
498 |
+
"""
|
499 |
+
batch_sizes = [b.shape[0] for b in branges] if branges is not None else [x.shape[0] for x in x_list]
|
500 |
+
all_shapes = tuple((b, x.shape[1]) for b, x in zip(batch_sizes, x_list))
|
501 |
+
if all_shapes not in attn_bias_cache.keys():
|
502 |
+
seqlens = []
|
503 |
+
for b, x in zip(batch_sizes, x_list):
|
504 |
+
for _ in range(b):
|
505 |
+
seqlens.append(x.shape[1])
|
506 |
+
attn_bias = fmha.BlockDiagonalMask.from_seqlens(seqlens)
|
507 |
+
attn_bias._batch_sizes = batch_sizes
|
508 |
+
attn_bias_cache[all_shapes] = attn_bias
|
509 |
+
|
510 |
+
if branges is not None:
|
511 |
+
cat_tensors = index_select_cat([x.flatten(1) for x in x_list], branges).view(1, -1, x_list[0].shape[-1])
|
512 |
+
else:
|
513 |
+
tensors_bs1 = tuple(x.reshape([1, -1, *x.shape[2:]]) for x in x_list)
|
514 |
+
cat_tensors = torch.cat(tensors_bs1, dim=1)
|
515 |
+
|
516 |
+
return attn_bias_cache[all_shapes], cat_tensors
|
517 |
+
|
518 |
+
|
519 |
+
def drop_add_residual_stochastic_depth_list(
|
520 |
+
x_list: List[torch.Tensor],
|
521 |
+
residual_func: Callable[[torch.Tensor, Any], torch.Tensor],
|
522 |
+
sample_drop_ratio: float = 0.0,
|
523 |
+
scaling_vector=None,
|
524 |
+
) -> torch.Tensor:
|
525 |
+
# 1) generate random set of indices for dropping samples in the batch
|
526 |
+
branges_scales = [get_branges_scales(x, sample_drop_ratio=sample_drop_ratio) for x in x_list]
|
527 |
+
branges = [s[0] for s in branges_scales]
|
528 |
+
residual_scale_factors = [s[1] for s in branges_scales]
|
529 |
+
|
530 |
+
# 2) get attention bias and index+concat the tensors
|
531 |
+
attn_bias, x_cat = get_attn_bias_and_cat(x_list, branges)
|
532 |
+
|
533 |
+
# 3) apply residual_func to get residual, and split the result
|
534 |
+
residual_list = attn_bias.split(residual_func(x_cat, attn_bias=attn_bias)) # type: ignore
|
535 |
+
|
536 |
+
outputs = []
|
537 |
+
for x, brange, residual, residual_scale_factor in zip(x_list, branges, residual_list, residual_scale_factors):
|
538 |
+
outputs.append(add_residual(x, brange, residual, residual_scale_factor, scaling_vector).view_as(x))
|
539 |
+
return outputs
|
540 |
+
|
541 |
+
|
542 |
+
def named_apply(fn: Callable, module: nn.Module, name="", depth_first=True, include_root=False) -> nn.Module:
|
543 |
+
if not depth_first and include_root:
|
544 |
+
fn(module=module, name=name)
|
545 |
+
for child_name, child_module in module.named_children():
|
546 |
+
child_name = ".".join((name, child_name)) if name else child_name
|
547 |
+
named_apply(fn=fn, module=child_module, name=child_name, depth_first=depth_first, include_root=True)
|
548 |
+
if depth_first and include_root:
|
549 |
+
fn(module=module, name=name)
|
550 |
+
return module
|
551 |
+
|
552 |
+
|
553 |
+
class BlockChunk(nn.ModuleList):
|
554 |
+
def forward(self, x):
|
555 |
+
for b in self:
|
556 |
+
x = b(x)
|
557 |
+
return x
|
558 |
+
|
559 |
+
|
560 |
+
class DinoVisionTransformer(nn.Module):
|
561 |
+
def __init__(
|
562 |
+
self,
|
563 |
+
img_size=224,
|
564 |
+
patch_size=16,
|
565 |
+
in_chans=3,
|
566 |
+
embed_dim=768,
|
567 |
+
depth=12,
|
568 |
+
num_heads=12,
|
569 |
+
mlp_ratio=4.0,
|
570 |
+
qkv_bias=True,
|
571 |
+
ffn_bias=True,
|
572 |
+
proj_bias=True,
|
573 |
+
drop_path_rate=0.0,
|
574 |
+
drop_path_uniform=False,
|
575 |
+
init_values=None, # for layerscale: None or 0 => no layerscale
|
576 |
+
embed_layer=PatchEmbed,
|
577 |
+
act_layer=nn.GELU,
|
578 |
+
block_fn=Block,
|
579 |
+
ffn_layer="mlp",
|
580 |
+
block_chunks=1,
|
581 |
+
num_register_tokens=0,
|
582 |
+
interpolate_antialias=False,
|
583 |
+
interpolate_offset=0.1,
|
584 |
+
):
|
585 |
+
"""
|
586 |
+
Args:
|
587 |
+
img_size (int, tuple): input image size
|
588 |
+
patch_size (int, tuple): patch size
|
589 |
+
in_chans (int): number of input channels
|
590 |
+
embed_dim (int): embedding dimension
|
591 |
+
depth (int): depth of transformer
|
592 |
+
num_heads (int): number of attention heads
|
593 |
+
mlp_ratio (int): ratio of mlp hidden dim to embedding dim
|
594 |
+
qkv_bias (bool): enable bias for qkv if True
|
595 |
+
proj_bias (bool): enable bias for proj in attn if True
|
596 |
+
ffn_bias (bool): enable bias for ffn if True
|
597 |
+
drop_path_rate (float): stochastic depth rate
|
598 |
+
drop_path_uniform (bool): apply uniform drop rate across blocks
|
599 |
+
weight_init (str): weight init scheme
|
600 |
+
init_values (float): layer-scale init values
|
601 |
+
embed_layer (nn.Module): patch embedding layer
|
602 |
+
act_layer (nn.Module): MLP activation layer
|
603 |
+
block_fn (nn.Module): transformer block class
|
604 |
+
ffn_layer (str): "mlp", "swiglu", "swiglufused" or "identity"
|
605 |
+
block_chunks: (int) split block sequence into block_chunks units for FSDP wrap
|
606 |
+
num_register_tokens: (int) number of extra cls tokens (so-called "registers")
|
607 |
+
interpolate_antialias: (str) flag to apply anti-aliasing when interpolating positional embeddings
|
608 |
+
interpolate_offset: (float) work-around offset to apply when interpolating positional embeddings
|
609 |
+
"""
|
610 |
+
super().__init__()
|
611 |
+
norm_layer = partial(nn.LayerNorm, eps=1e-6)
|
612 |
+
|
613 |
+
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
|
614 |
+
self.num_tokens = 1
|
615 |
+
self.n_blocks = depth
|
616 |
+
self.num_heads = num_heads
|
617 |
+
self.patch_size = patch_size
|
618 |
+
self.num_register_tokens = num_register_tokens
|
619 |
+
self.interpolate_antialias = interpolate_antialias
|
620 |
+
self.interpolate_offset = interpolate_offset
|
621 |
+
|
622 |
+
self.patch_embed = embed_layer(img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
|
623 |
+
num_patches = self.patch_embed.num_patches
|
624 |
+
|
625 |
+
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
|
626 |
+
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + self.num_tokens, embed_dim))
|
627 |
+
assert num_register_tokens >= 0
|
628 |
+
self.register_tokens = (
|
629 |
+
nn.Parameter(torch.zeros(1, num_register_tokens, embed_dim)) if num_register_tokens else None
|
630 |
+
)
|
631 |
+
|
632 |
+
if drop_path_uniform is True:
|
633 |
+
dpr = [drop_path_rate] * depth
|
634 |
+
else:
|
635 |
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
|
636 |
+
|
637 |
+
if ffn_layer == "mlp":
|
638 |
+
ffn_layer = Mlp
|
639 |
+
elif ffn_layer == "swiglufused" or ffn_layer == "swiglu":
|
640 |
+
ffn_layer = SwiGLUFFNFused
|
641 |
+
elif ffn_layer == "identity":
|
642 |
+
def f(*args, **kwargs):
|
643 |
+
return nn.Identity()
|
644 |
+
|
645 |
+
ffn_layer = f
|
646 |
+
else:
|
647 |
+
raise NotImplementedError
|
648 |
+
|
649 |
+
blocks_list = [
|
650 |
+
block_fn(
|
651 |
+
dim=embed_dim,
|
652 |
+
num_heads=num_heads,
|
653 |
+
mlp_ratio=mlp_ratio,
|
654 |
+
qkv_bias=qkv_bias,
|
655 |
+
proj_bias=proj_bias,
|
656 |
+
ffn_bias=ffn_bias,
|
657 |
+
drop_path=dpr[i],
|
658 |
+
norm_layer=norm_layer,
|
659 |
+
act_layer=act_layer,
|
660 |
+
ffn_layer=ffn_layer,
|
661 |
+
init_values=init_values,
|
662 |
+
)
|
663 |
+
for i in range(depth)
|
664 |
+
]
|
665 |
+
if block_chunks > 0:
|
666 |
+
self.chunked_blocks = True
|
667 |
+
chunked_blocks = []
|
668 |
+
chunksize = depth // block_chunks
|
669 |
+
for i in range(0, depth, chunksize):
|
670 |
+
# this is to keep the block index consistent if we chunk the block list
|
671 |
+
chunked_blocks.append([nn.Identity()] * i + blocks_list[i : i + chunksize])
|
672 |
+
self.blocks = nn.ModuleList([BlockChunk(p) for p in chunked_blocks])
|
673 |
+
else:
|
674 |
+
self.chunked_blocks = False
|
675 |
+
self.blocks = nn.ModuleList(blocks_list)
|
676 |
+
|
677 |
+
self.norm = norm_layer(embed_dim)
|
678 |
+
self.head = nn.Identity()
|
679 |
+
|
680 |
+
self.mask_token = nn.Parameter(torch.zeros(1, embed_dim))
|
681 |
+
|
682 |
+
def interpolate_pos_encoding(self, x, w, h):
|
683 |
+
previous_dtype = x.dtype
|
684 |
+
npatch = x.shape[1] - 1
|
685 |
+
N = self.pos_embed.shape[1] - 1
|
686 |
+
if npatch == N and w == h:
|
687 |
+
return self.pos_embed
|
688 |
+
pos_embed = self.pos_embed.float()
|
689 |
+
class_pos_embed = pos_embed[:, 0]
|
690 |
+
patch_pos_embed = pos_embed[:, 1:]
|
691 |
+
dim = x.shape[-1]
|
692 |
+
w0 = w // self.patch_size
|
693 |
+
h0 = h // self.patch_size
|
694 |
+
M = int(math.sqrt(N)) # Recover the number of patches in each dimension
|
695 |
+
assert N == M * M
|
696 |
+
kwargs = {}
|
697 |
+
if self.interpolate_offset:
|
698 |
+
# Historical kludge: add a small number to avoid floating point error in the interpolation, see https://github.com/facebookresearch/dino/issues/8
|
699 |
+
# Note: still needed for backward-compatibility, the underlying operators are using both output size and scale factors
|
700 |
+
sx = float(w0 + self.interpolate_offset) / M
|
701 |
+
sy = float(h0 + self.interpolate_offset) / M
|
702 |
+
kwargs["scale_factor"] = (sx, sy)
|
703 |
+
else:
|
704 |
+
# Simply specify an output size instead of a scale factor
|
705 |
+
kwargs["size"] = (w0, h0)
|
706 |
+
patch_pos_embed = nn.functional.interpolate(
|
707 |
+
patch_pos_embed.reshape(1, M, M, dim).permute(0, 3, 1, 2),
|
708 |
+
mode="bicubic",
|
709 |
+
antialias=self.interpolate_antialias,
|
710 |
+
**kwargs,
|
711 |
+
)
|
712 |
+
assert (w0, h0) == patch_pos_embed.shape[-2:]
|
713 |
+
patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim)
|
714 |
+
return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1).to(previous_dtype)
|
715 |
+
|
716 |
+
def prepare_tokens_with_masks(self, x, masks=None):
|
717 |
+
B, nc, w, h = x.shape
|
718 |
+
x = self.patch_embed(x)
|
719 |
+
if masks is not None:
|
720 |
+
x = torch.where(masks.unsqueeze(-1), self.mask_token.to(x.dtype).unsqueeze(0), x)
|
721 |
+
|
722 |
+
x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1)
|
723 |
+
x = x + self.interpolate_pos_encoding(x, w, h)
|
724 |
+
|
725 |
+
if self.register_tokens is not None:
|
726 |
+
x = torch.cat(
|
727 |
+
(
|
728 |
+
x[:, :1],
|
729 |
+
self.register_tokens.expand(x.shape[0], -1, -1),
|
730 |
+
x[:, 1:],
|
731 |
+
),
|
732 |
+
dim=1,
|
733 |
+
)
|
734 |
+
|
735 |
+
return x
|
736 |
+
|
737 |
+
def forward_features_list(self, x_list, masks_list):
|
738 |
+
x = [self.prepare_tokens_with_masks(x, masks) for x, masks in zip(x_list, masks_list)]
|
739 |
+
for blk in self.blocks:
|
740 |
+
x = blk(x)
|
741 |
+
|
742 |
+
all_x = x
|
743 |
+
output = []
|
744 |
+
for x, masks in zip(all_x, masks_list):
|
745 |
+
x_norm = self.norm(x)
|
746 |
+
output.append(
|
747 |
+
{
|
748 |
+
"x_norm_clstoken": x_norm[:, 0],
|
749 |
+
"x_norm_regtokens": x_norm[:, 1 : self.num_register_tokens + 1],
|
750 |
+
"x_norm_patchtokens": x_norm[:, self.num_register_tokens + 1 :],
|
751 |
+
"x_prenorm": x,
|
752 |
+
"masks": masks,
|
753 |
+
}
|
754 |
+
)
|
755 |
+
return output
|
756 |
+
|
757 |
+
def forward_features(self, x, masks=None):
|
758 |
+
if isinstance(x, list):
|
759 |
+
return self.forward_features_list(x, masks)
|
760 |
+
|
761 |
+
x = self.prepare_tokens_with_masks(x, masks)
|
762 |
+
|
763 |
+
for blk in self.blocks:
|
764 |
+
x = blk(x)
|
765 |
+
|
766 |
+
x_norm = self.norm(x)
|
767 |
+
return {
|
768 |
+
"x_norm_clstoken": x_norm[:, 0],
|
769 |
+
"x_norm_regtokens": x_norm[:, 1 : self.num_register_tokens + 1],
|
770 |
+
"x_norm_patchtokens": x_norm[:, self.num_register_tokens + 1 :],
|
771 |
+
"x_prenorm": x,
|
772 |
+
"masks": masks,
|
773 |
+
}
|
774 |
+
|
775 |
+
def _get_intermediate_layers_not_chunked(self, x, n=1):
|
776 |
+
x = self.prepare_tokens_with_masks(x)
|
777 |
+
# If n is an int, take the n last blocks. If it's a list, take them
|
778 |
+
output, total_block_len = [], len(self.blocks)
|
779 |
+
blocks_to_take = range(total_block_len - n, total_block_len) if isinstance(n, int) else n
|
780 |
+
for i, blk in enumerate(self.blocks):
|
781 |
+
x = blk(x)
|
782 |
+
if i in blocks_to_take:
|
783 |
+
output.append(x)
|
784 |
+
assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found"
|
785 |
+
return output
|
786 |
+
|
787 |
+
def _get_intermediate_layers_chunked(self, x, n=1):
|
788 |
+
x = self.prepare_tokens_with_masks(x)
|
789 |
+
output, i, total_block_len = [], 0, len(self.blocks[-1])
|
790 |
+
# If n is an int, take the n last blocks. If it's a list, take them
|
791 |
+
blocks_to_take = range(total_block_len - n, total_block_len) if isinstance(n, int) else n
|
792 |
+
for block_chunk in self.blocks:
|
793 |
+
for blk in block_chunk[i:]: # Passing the nn.Identity()
|
794 |
+
x = blk(x)
|
795 |
+
if i in blocks_to_take:
|
796 |
+
output.append(x)
|
797 |
+
i += 1
|
798 |
+
assert len(output) == len(blocks_to_take), f"only {len(output)} / {len(blocks_to_take)} blocks found"
|
799 |
+
return output
|
800 |
+
|
801 |
+
def get_intermediate_layers(
|
802 |
+
self,
|
803 |
+
x: torch.Tensor,
|
804 |
+
n: Union[int, Sequence] = 1, # Layers or n last layers to take
|
805 |
+
reshape: bool = False,
|
806 |
+
return_class_token: bool = False,
|
807 |
+
norm=True,
|
808 |
+
) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]]]:
|
809 |
+
if self.chunked_blocks:
|
810 |
+
outputs = self._get_intermediate_layers_chunked(x, n)
|
811 |
+
else:
|
812 |
+
outputs = self._get_intermediate_layers_not_chunked(x, n)
|
813 |
+
if norm:
|
814 |
+
outputs = [self.norm(out) for out in outputs]
|
815 |
+
class_tokens = [out[:, 0] for out in outputs]
|
816 |
+
outputs = [out[:, 1 + self.num_register_tokens :] for out in outputs]
|
817 |
+
if reshape:
|
818 |
+
B, _, w, h = x.shape
|
819 |
+
outputs = [
|
820 |
+
out.reshape(B, w // self.patch_size, h // self.patch_size, -1).permute(0, 3, 1, 2).contiguous()
|
821 |
+
for out in outputs
|
822 |
+
]
|
823 |
+
if return_class_token:
|
824 |
+
return tuple(zip(outputs, class_tokens))
|
825 |
+
return tuple(outputs)
|
826 |
+
|
827 |
+
def forward(self, *args, is_training=False, **kwargs):
|
828 |
+
ret = self.forward_features(*args, **kwargs)
|
829 |
+
if is_training:
|
830 |
+
return ret
|
831 |
+
else:
|
832 |
+
return self.head(ret["x_norm_clstoken"])
|
833 |
+
|
834 |
+
|
835 |
+
def vit_small(patch_size=16, num_register_tokens=0, **kwargs):
|
836 |
+
model = DinoVisionTransformer(
|
837 |
+
patch_size=patch_size,
|
838 |
+
embed_dim=384,
|
839 |
+
depth=12,
|
840 |
+
num_heads=6,
|
841 |
+
mlp_ratio=4,
|
842 |
+
block_fn=partial(Block, attn_class=MemEffAttention),
|
843 |
+
num_register_tokens=num_register_tokens,
|
844 |
+
**kwargs,
|
845 |
+
)
|
846 |
+
return model
|
847 |
+
|
848 |
+
|
849 |
+
def vit_base(patch_size=16, num_register_tokens=0, **kwargs):
|
850 |
+
model = DinoVisionTransformer(
|
851 |
+
patch_size=patch_size,
|
852 |
+
embed_dim=768,
|
853 |
+
depth=12,
|
854 |
+
num_heads=12,
|
855 |
+
mlp_ratio=4,
|
856 |
+
block_fn=partial(Block, attn_class=MemEffAttention),
|
857 |
+
num_register_tokens=num_register_tokens,
|
858 |
+
**kwargs,
|
859 |
+
)
|
860 |
+
return model
|
861 |
+
|
862 |
+
|
863 |
+
def vit_large(patch_size=16, num_register_tokens=0, **kwargs):
|
864 |
+
model = DinoVisionTransformer(
|
865 |
+
patch_size=patch_size,
|
866 |
+
embed_dim=1024,
|
867 |
+
depth=24,
|
868 |
+
num_heads=16,
|
869 |
+
mlp_ratio=4,
|
870 |
+
block_fn=partial(Block, attn_class=MemEffAttention),
|
871 |
+
num_register_tokens=num_register_tokens,
|
872 |
+
**kwargs,
|
873 |
+
)
|
874 |
+
return model
|
875 |
+
|
876 |
+
|
877 |
+
def vit_giant2(patch_size=16, num_register_tokens=0, **kwargs):
|
878 |
+
"""
|
879 |
+
Close to ViT-giant, with embed-dim 1536 and 24 heads => embed-dim per head 64
|
880 |
+
"""
|
881 |
+
model = DinoVisionTransformer(
|
882 |
+
patch_size=patch_size,
|
883 |
+
embed_dim=1536,
|
884 |
+
depth=40,
|
885 |
+
num_heads=24,
|
886 |
+
mlp_ratio=4,
|
887 |
+
block_fn=partial(Block, attn_class=MemEffAttention),
|
888 |
+
num_register_tokens=num_register_tokens,
|
889 |
+
**kwargs,
|
890 |
+
)
|
891 |
+
return model
|
892 |
+
|
893 |
+
|
894 |
+
class Weights(Enum):
|
895 |
+
LVD142M = "LVD142M"
|
896 |
+
|
897 |
+
|
898 |
+
def _make_dinov2_model(
|
899 |
+
*,
|
900 |
+
arch_name: str = "vit_large",
|
901 |
+
img_size: int = 518,
|
902 |
+
patch_size: int = 14,
|
903 |
+
init_values: float = 1.0,
|
904 |
+
ffn_layer: str = "mlp",
|
905 |
+
block_chunks: int = 0,
|
906 |
+
num_register_tokens: int = 0,
|
907 |
+
interpolate_antialias: bool = False,
|
908 |
+
interpolate_offset: float = 0.1,
|
909 |
+
weights: Union[Weights, str] = Weights.LVD142M,
|
910 |
+
**kwargs,
|
911 |
+
):
|
912 |
+
if isinstance(weights, str):
|
913 |
+
try:
|
914 |
+
weights = Weights[weights]
|
915 |
+
except KeyError:
|
916 |
+
raise AssertionError(f"Unsupported weights: {weights}")
|
917 |
+
|
918 |
+
vit_kwargs = dict(
|
919 |
+
img_size=img_size,
|
920 |
+
patch_size=patch_size,
|
921 |
+
init_values=init_values,
|
922 |
+
ffn_layer=ffn_layer,
|
923 |
+
block_chunks=block_chunks,
|
924 |
+
num_register_tokens=num_register_tokens,
|
925 |
+
interpolate_antialias=interpolate_antialias,
|
926 |
+
interpolate_offset=interpolate_offset,
|
927 |
+
)
|
928 |
+
vit_kwargs.update(**kwargs)
|
929 |
+
model = sys.modules[__name__].__dict__[arch_name](**vit_kwargs)
|
930 |
+
|
931 |
+
return model
|
932 |
+
|
933 |
+
|
934 |
+
def dinov2_vits14(**kwargs):
|
935 |
+
"""
|
936 |
+
DINOv2 ViT-S/14 model (optionally) pretrained on the LVD-142M dataset.
|
937 |
+
"""
|
938 |
+
return _make_dinov2_model(arch_name="vit_small", **kwargs)
|
939 |
+
|
940 |
+
|
941 |
+
def dinov2_vitb14(**kwargs):
|
942 |
+
"""
|
943 |
+
DINOv2 ViT-B/14 model (optionally) pretrained on the LVD-142M dataset.
|
944 |
+
"""
|
945 |
+
return _make_dinov2_model(arch_name="vit_base", **kwargs)
|
946 |
+
|
947 |
+
|
948 |
+
def dinov2_vitl14(**kwargs):
|
949 |
+
"""
|
950 |
+
DINOv2 ViT-L/14 model (optionally) pretrained on the LVD-142M dataset.
|
951 |
+
"""
|
952 |
+
return _make_dinov2_model(arch_name="vit_large", **kwargs)
|
953 |
+
|
954 |
+
|
955 |
+
def dinov2_vitg14(**kwargs):
|
956 |
+
"""
|
957 |
+
DINOv2 ViT-g/14 model (optionally) pretrained on the LVD-142M dataset.
|
958 |
+
"""
|
959 |
+
return _make_dinov2_model(
|
960 |
+
arch_name="vit_giant2",
|
961 |
+
ffn_layer="swiglufused",
|
962 |
+
**kwargs,
|
963 |
+
)
|
964 |
+
|
965 |
+
|
966 |
+
def dinov2_vits14_reg(**kwargs):
|
967 |
+
"""
|
968 |
+
DINOv2 ViT-S/14 model with registers (optionally) pretrained on the LVD-142M dataset.
|
969 |
+
"""
|
970 |
+
return _make_dinov2_model(
|
971 |
+
arch_name="vit_small",
|
972 |
+
num_register_tokens=4,
|
973 |
+
interpolate_antialias=True,
|
974 |
+
interpolate_offset=0.0,
|
975 |
+
**kwargs,
|
976 |
+
)
|
977 |
+
|
978 |
+
|
979 |
+
def dinov2_vitb14_reg(**kwargs):
|
980 |
+
"""
|
981 |
+
DINOv2 ViT-B/14 model with registers (optionally) pretrained on the LVD-142M dataset.
|
982 |
+
"""
|
983 |
+
return _make_dinov2_model(
|
984 |
+
arch_name="vit_base",
|
985 |
+
num_register_tokens=4,
|
986 |
+
interpolate_antialias=True,
|
987 |
+
interpolate_offset=0.0,
|
988 |
+
**kwargs,
|
989 |
+
)
|
990 |
+
|
991 |
+
|
992 |
+
def dinov2_vitl14_reg(**kwargs):
|
993 |
+
"""
|
994 |
+
DINOv2 ViT-L/14 model with registers (optionally) pretrained on the LVD-142M dataset.
|
995 |
+
"""
|
996 |
+
return _make_dinov2_model(
|
997 |
+
arch_name="vit_large",
|
998 |
+
num_register_tokens=4,
|
999 |
+
interpolate_antialias=True,
|
1000 |
+
interpolate_offset=0.0,
|
1001 |
+
**kwargs,
|
1002 |
+
)
|
1003 |
+
|
1004 |
+
|
1005 |
+
def dinov2_vitg14_reg(**kwargs):
|
1006 |
+
"""
|
1007 |
+
DINOv2 ViT-g/14 model with registers (optionally) pretrained on the LVD-142M dataset.
|
1008 |
+
"""
|
1009 |
+
return _make_dinov2_model(
|
1010 |
+
arch_name="vit_giant2",
|
1011 |
+
ffn_layer="swiglufused",
|
1012 |
+
num_register_tokens=4,
|
1013 |
+
interpolate_antialias=True,
|
1014 |
+
interpolate_offset=0.0,
|
1015 |
+
**kwargs,
|
1016 |
+
)
|
enable_cpe_support.py
CHANGED
@@ -6,6 +6,7 @@
|
|
6 |
# distribution of this software and related documentation without an express
|
7 |
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
8 |
|
|
|
9 |
from typing import List, Optional, Set, Tuple, Union
|
10 |
from types import MethodType
|
11 |
|
@@ -14,7 +15,7 @@ from torch import nn
|
|
14 |
|
15 |
from timm.models import VisionTransformer, checkpoint_seq
|
16 |
|
17 |
-
from
|
18 |
|
19 |
from .extra_models import DinoWrapper
|
20 |
from .vit_patch_generator import ViTPatchGenerator
|
@@ -32,6 +33,20 @@ def _forward_cpe(self: VisionTransformer, x: torch.Tensor) -> torch.Tensor:
|
|
32 |
return x
|
33 |
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def _take_indices(
|
36 |
num_blocks: int,
|
37 |
n: Optional[Union[int, List[int], Tuple[int]]],
|
@@ -168,3 +183,5 @@ def enable_cpe(model: nn.Module,
|
|
168 |
_enable_cpe_for_timm_vit(model.vit, *args, **kwargs)
|
169 |
else:
|
170 |
raise ValueError(f'CPE not supported for this model type: {type(model)}')
|
|
|
|
|
|
6 |
# distribution of this software and related documentation without an express
|
7 |
# license agreement from NVIDIA CORPORATION is strictly prohibited.
|
8 |
|
9 |
+
from contextlib import contextmanager
|
10 |
from typing import List, Optional, Set, Tuple, Union
|
11 |
from types import MethodType
|
12 |
|
|
|
15 |
|
16 |
from timm.models import VisionTransformer, checkpoint_seq
|
17 |
|
18 |
+
from .feature_normalizer import IntermediateFeatureNormalizerBase, NullIntermediateFeatureNormalizer
|
19 |
|
20 |
from .extra_models import DinoWrapper
|
21 |
from .vit_patch_generator import ViTPatchGenerator
|
|
|
33 |
return x
|
34 |
|
35 |
|
36 |
+
@contextmanager
|
37 |
+
def _video_mode(self: VisionTransformer, t: int):
|
38 |
+
"""
|
39 |
+
Context manager to temporarily set the model in video mode.
|
40 |
+
This is used to handle models that support both image and video inputs.
|
41 |
+
"""
|
42 |
+
original_num_frames = self.patch_generator.num_video_frames
|
43 |
+
self.patch_generator.num_video_frames = t
|
44 |
+
try:
|
45 |
+
yield
|
46 |
+
finally:
|
47 |
+
self.patch_generator.num_video_frames = original_num_frames
|
48 |
+
|
49 |
+
|
50 |
def _take_indices(
|
51 |
num_blocks: int,
|
52 |
n: Optional[Union[int, List[int], Tuple[int]]],
|
|
|
183 |
_enable_cpe_for_timm_vit(model.vit, *args, **kwargs)
|
184 |
else:
|
185 |
raise ValueError(f'CPE not supported for this model type: {type(model)}')
|
186 |
+
|
187 |
+
model.cpe_video_mode = MethodType(_video_mode, model)
|
eradio_model.py
CHANGED
@@ -19,9 +19,15 @@
|
|
19 |
import timm
|
20 |
import torch
|
21 |
import torch.nn as nn
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
import numpy as np
|
26 |
import torch.nn.functional as F
|
27 |
import math
|
|
|
19 |
import timm
|
20 |
import torch
|
21 |
import torch.nn as nn
|
22 |
+
try:
|
23 |
+
from timm.models import register_model
|
24 |
+
except ImportError:
|
25 |
+
from timm.models.registry import register_model
|
26 |
+
|
27 |
+
try:
|
28 |
+
from timm.layers import trunc_normal_, DropPath, LayerNorm2d
|
29 |
+
except ImportError:
|
30 |
+
from timm.models.layers import trunc_normal_, DropPath, LayerNorm2d
|
31 |
import numpy as np
|
32 |
import torch.nn.functional as F
|
33 |
import math
|
extra_models.py
CHANGED
@@ -7,7 +7,10 @@ import torch
|
|
7 |
from torch import nn
|
8 |
import torch.nn.functional as F
|
9 |
|
10 |
-
|
|
|
|
|
|
|
11 |
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
12 |
|
13 |
from .forward_intermediates import forward_intermediates
|
|
|
7 |
from torch import nn
|
8 |
import torch.nn.functional as F
|
9 |
|
10 |
+
try:
|
11 |
+
from timm.models import register_model
|
12 |
+
except ImportError:
|
13 |
+
from timm.models.registry import register_model
|
14 |
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
|
15 |
|
16 |
from .forward_intermediates import forward_intermediates
|
extra_timm_models.py
CHANGED
@@ -13,7 +13,7 @@ import torch
|
|
13 |
from torch import nn
|
14 |
from torch.nn import functional as F
|
15 |
|
16 |
-
from timm.models import register_model
|
17 |
from timm.models.vision_transformer import (
|
18 |
VisionTransformer,
|
19 |
_create_vision_transformer as _timm_create_vision_transformer,
|
@@ -127,6 +127,10 @@ def vit_bigG_patch14_224(pretrained=False, **kwargs) -> VisionTransformer:
|
|
127 |
|
128 |
|
129 |
def _create_vision_transformer(*args, **kwargs):
|
|
|
|
|
|
|
|
|
130 |
model = _timm_create_vision_transformer(*args, **kwargs)
|
131 |
_patch_layer_scale(model)
|
132 |
return model
|
|
|
13 |
from torch import nn
|
14 |
from torch.nn import functional as F
|
15 |
|
16 |
+
from timm.models import register_model, PretrainedCfg
|
17 |
from timm.models.vision_transformer import (
|
18 |
VisionTransformer,
|
19 |
_create_vision_transformer as _timm_create_vision_transformer,
|
|
|
127 |
|
128 |
|
129 |
def _create_vision_transformer(*args, **kwargs):
|
130 |
+
if kwargs.get('pretrained_cfg', None) is None:
|
131 |
+
# This prevents the warning from being emitted
|
132 |
+
kwargs['pretrained_cfg'] = PretrainedCfg()
|
133 |
+
|
134 |
model = _timm_create_vision_transformer(*args, **kwargs)
|
135 |
_patch_layer_scale(model)
|
136 |
return model
|
hf_model.py
CHANGED
@@ -28,9 +28,12 @@ from .adaptor_generic import GenericAdaptor, AdaptorBase
|
|
28 |
from .adaptor_mlp import create_mlp_from_config
|
29 |
from .adaptor_registry import adaptor_registry
|
30 |
from .cls_token import ClsToken
|
|
|
31 |
from .enable_cpe_support import enable_cpe
|
32 |
from .enable_spectral_reparam import configure_spectral_reparam_from_args
|
33 |
from .eradio_model import eradio
|
|
|
|
|
34 |
from .radio_model import create_model_from_args
|
35 |
from .radio_model import RADIOModel as RADIOModelBase, Resolution
|
36 |
from .input_conditioner import get_default_conditioner, InputConditioner
|
@@ -40,6 +43,7 @@ from .vitdet import apply_vitdet_arch, VitDetArgs
|
|
40 |
|
41 |
# Register extra models
|
42 |
from .extra_timm_models import *
|
|
|
43 |
|
44 |
|
45 |
class RADIOConfig(PretrainedConfig):
|
@@ -55,6 +59,8 @@ class RADIOConfig(PretrainedConfig):
|
|
55 |
adaptor_names: Union[str, List[str]] = None,
|
56 |
adaptor_configs: Dict[str, Dict[str, int]] = None,
|
57 |
vitdet_window_size: Optional[int] = None,
|
|
|
|
|
58 |
**kwargs,
|
59 |
):
|
60 |
self.args = args
|
@@ -74,9 +80,12 @@ class RADIOConfig(PretrainedConfig):
|
|
74 |
self.adaptor_names = adaptor_names
|
75 |
self.adaptor_configs = adaptor_configs
|
76 |
self.vitdet_window_size = vitdet_window_size
|
|
|
|
|
77 |
super().__init__(**kwargs)
|
78 |
|
79 |
|
|
|
80 |
class RADIOModel(PreTrainedModel):
|
81 |
"""Pretrained Hugging Face model for RADIO.
|
82 |
|
@@ -118,6 +127,19 @@ class RADIOModel(PreTrainedModel):
|
|
118 |
adaptor.head_idx = mlp_config["head_idx"]
|
119 |
adaptors[adaptor_name] = adaptor
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
self.radio_model = RADIOModelBase(
|
122 |
model,
|
123 |
input_conditioner,
|
@@ -127,6 +149,8 @@ class RADIOModel(PreTrainedModel):
|
|
127 |
window_size=config.vitdet_window_size,
|
128 |
preferred_resolution=config.preferred_resolution,
|
129 |
adaptors=adaptors,
|
|
|
|
|
130 |
)
|
131 |
|
132 |
@property
|
|
|
28 |
from .adaptor_mlp import create_mlp_from_config
|
29 |
from .adaptor_registry import adaptor_registry
|
30 |
from .cls_token import ClsToken
|
31 |
+
from .dinov2_arch import dinov2_vitg14_reg
|
32 |
from .enable_cpe_support import enable_cpe
|
33 |
from .enable_spectral_reparam import configure_spectral_reparam_from_args
|
34 |
from .eradio_model import eradio
|
35 |
+
from .feature_normalizer import FeatureNormalizer, IntermediateFeatureNormalizer
|
36 |
+
from .forward_intermediates import forward_intermediates
|
37 |
from .radio_model import create_model_from_args
|
38 |
from .radio_model import RADIOModel as RADIOModelBase, Resolution
|
39 |
from .input_conditioner import get_default_conditioner, InputConditioner
|
|
|
43 |
|
44 |
# Register extra models
|
45 |
from .extra_timm_models import *
|
46 |
+
from .extra_models import *
|
47 |
|
48 |
|
49 |
class RADIOConfig(PretrainedConfig):
|
|
|
59 |
adaptor_names: Union[str, List[str]] = None,
|
60 |
adaptor_configs: Dict[str, Dict[str, int]] = None,
|
61 |
vitdet_window_size: Optional[int] = None,
|
62 |
+
feature_normalizer_config: Optional[dict] = None,
|
63 |
+
inter_feature_normalizer_config: Optional[dict] = None,
|
64 |
**kwargs,
|
65 |
):
|
66 |
self.args = args
|
|
|
80 |
self.adaptor_names = adaptor_names
|
81 |
self.adaptor_configs = adaptor_configs
|
82 |
self.vitdet_window_size = vitdet_window_size
|
83 |
+
self.feature_normalizer_config = feature_normalizer_config
|
84 |
+
self.inter_feature_normalizer_config = inter_feature_normalizer_config
|
85 |
super().__init__(**kwargs)
|
86 |
|
87 |
|
88 |
+
|
89 |
class RADIOModel(PreTrainedModel):
|
90 |
"""Pretrained Hugging Face model for RADIO.
|
91 |
|
|
|
127 |
adaptor.head_idx = mlp_config["head_idx"]
|
128 |
adaptors[adaptor_name] = adaptor
|
129 |
|
130 |
+
feature_normalizer = None
|
131 |
+
if config.feature_normalizer_config is not None:
|
132 |
+
# Actual normalization values will be restored when loading checkpoint weights.
|
133 |
+
feature_normalizer = FeatureNormalizer(config.feature_normalizer_config["embed_dim"])
|
134 |
+
|
135 |
+
inter_feature_normalizer = None
|
136 |
+
if config.inter_feature_normalizer_config is not None:
|
137 |
+
inter_feature_normalizer = IntermediateFeatureNormalizer(
|
138 |
+
config.inter_feature_normalizer_config["num_intermediates"],
|
139 |
+
config.inter_feature_normalizer_config["embed_dim"],
|
140 |
+
rot_per_layer=config.inter_feature_normalizer_config["rot_per_layer"],
|
141 |
+
dtype=dtype)
|
142 |
+
|
143 |
self.radio_model = RADIOModelBase(
|
144 |
model,
|
145 |
input_conditioner,
|
|
|
149 |
window_size=config.vitdet_window_size,
|
150 |
preferred_resolution=config.preferred_resolution,
|
151 |
adaptors=adaptors,
|
152 |
+
feature_normalizer=feature_normalizer,
|
153 |
+
inter_feature_normalizer=inter_feature_normalizer,
|
154 |
)
|
155 |
|
156 |
@property
|
open_clip_adaptor.py
CHANGED
@@ -14,19 +14,19 @@ import torch.nn.functional as F
|
|
14 |
from .adaptor_registry import adaptor_registry, dict_t, state_t
|
15 |
|
16 |
from .adaptor_generic import GenericAdaptor
|
17 |
-
|
18 |
|
19 |
class OpenCLIP_RADIO(GenericAdaptor):
|
20 |
def __init__(self, main_config: Namespace, adaptor_config: dict_t, state: state_t):
|
21 |
super().__init__(main_config, adaptor_config, state)
|
22 |
|
23 |
import open_clip
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
# Unload these parameters
|
31 |
self.oc_model.visual = None
|
32 |
|
|
|
14 |
from .adaptor_registry import adaptor_registry, dict_t, state_t
|
15 |
|
16 |
from .adaptor_generic import GenericAdaptor
|
17 |
+
from .utils import rank_gate
|
18 |
|
19 |
class OpenCLIP_RADIO(GenericAdaptor):
|
20 |
def __init__(self, main_config: Namespace, adaptor_config: dict_t, state: state_t):
|
21 |
super().__init__(main_config, adaptor_config, state)
|
22 |
|
23 |
import open_clip
|
24 |
+
with rank_gate():
|
25 |
+
self.oc_model = open_clip.create_model_from_pretrained(
|
26 |
+
model_name=adaptor_config['model'],
|
27 |
+
pretrained=adaptor_config['pretrained'],
|
28 |
+
return_transform=False,
|
29 |
+
)
|
30 |
# Unload these parameters
|
31 |
self.oc_model.visual = None
|
32 |
|
radio_model.py
CHANGED
@@ -127,6 +127,13 @@ class RADIOModel(nn.Module):
|
|
127 |
def embed_dim(self) -> int:
|
128 |
return self.model.embed_dim
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
def make_preprocessor_external(self) -> Callable[[torch.Tensor], torch.Tensor]:
|
131 |
ret = self.input_conditioner
|
132 |
self.input_conditioner = nn.Identity()
|
@@ -146,6 +153,21 @@ class RADIOModel(nn.Module):
|
|
146 |
if fn is not None:
|
147 |
fn()
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
def forward(self, x: torch.Tensor, feature_fmt: str = 'NLC') -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
150 |
'''
|
151 |
Forward process for model.
|
|
|
127 |
def embed_dim(self) -> int:
|
128 |
return self.model.embed_dim
|
129 |
|
130 |
+
@property
|
131 |
+
def summary_dim(self) -> int:
|
132 |
+
embed_dim = self.embed_dim
|
133 |
+
if self.summary_idxs is not None:
|
134 |
+
embed_dim *= self.summary_idxs.shape[0]
|
135 |
+
return embed_dim
|
136 |
+
|
137 |
def make_preprocessor_external(self) -> Callable[[torch.Tensor], torch.Tensor]:
|
138 |
ret = self.input_conditioner
|
139 |
self.input_conditioner = nn.Identity()
|
|
|
153 |
if fn is not None:
|
154 |
fn()
|
155 |
|
156 |
+
def cpe_video_mode(self, t: int):
|
157 |
+
'''
|
158 |
+
Context Manager.
|
159 |
+
|
160 |
+
Puts the patch generator into video mode, with the specified number of temporal frames.
|
161 |
+
In video mode, the expectation is that the input buffer is of shape `(B*T, C, H, W)`.
|
162 |
+
Video mode means that the same position viewport will be used for every frame in the temporal sequence, while keeping
|
163 |
+
distinct viewports for each video in the batch.
|
164 |
+
|
165 |
+
Usage:
|
166 |
+
with radio_model.cpe_video_mode(t=t):
|
167 |
+
y = radio_model(x)
|
168 |
+
'''
|
169 |
+
return self.model.cpe_video_mode(t)
|
170 |
+
|
171 |
def forward(self, x: torch.Tensor, feature_fmt: str = 'NLC') -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
|
172 |
'''
|
173 |
Forward process for model.
|
utils.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
|
3 |
+
import torch.distributed as dist
|
4 |
+
|
5 |
+
|
6 |
+
def get_rank(group: Optional[dist.ProcessGroup] = None):
|
7 |
+
return dist.get_rank(group) if dist.is_initialized() else 0
|
8 |
+
|
9 |
+
|
10 |
+
def get_world_size(group: Optional[dist.ProcessGroup] = None):
|
11 |
+
return dist.get_world_size(group) if dist.is_initialized() else 1
|
12 |
+
|
13 |
+
|
14 |
+
def barrier(group: Optional[dist.ProcessGroup] = None):
|
15 |
+
if dist.is_initialized():
|
16 |
+
dist.barrier(group)
|
17 |
+
|
18 |
+
|
19 |
+
class rank_gate:
|
20 |
+
'''
|
21 |
+
Execute the function on rank 0 first, followed by all other ranks. Useful when caches may need to be populated in a distributed environment.
|
22 |
+
'''
|
23 |
+
def __init__(self, func = None):
|
24 |
+
self.func = func
|
25 |
+
|
26 |
+
def __call__(self, *args, **kwargs):
|
27 |
+
rank = get_rank()
|
28 |
+
if rank == 0:
|
29 |
+
result = self.func(*args, **kwargs)
|
30 |
+
barrier()
|
31 |
+
if rank > 0:
|
32 |
+
result = self.func(*args, **kwargs)
|
33 |
+
return result
|
34 |
+
|
35 |
+
def __enter__(self, *args, **kwargs):
|
36 |
+
if get_rank() > 0:
|
37 |
+
barrier()
|
38 |
+
|
39 |
+
def __exit__(self, *args, **kwargs):
|
40 |
+
if get_rank() == 0:
|
41 |
+
barrier()
|
vit_patch_generator.py
CHANGED
@@ -89,6 +89,8 @@ class ViTPatchGenerator(nn.Module):
|
|
89 |
|
90 |
self.patch_normalizer = nn.LayerNorm(embed_dim) if normalize_patches else nn.Identity()
|
91 |
|
|
|
|
|
92 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
93 |
patches = self.embed_patches(x)
|
94 |
patches, pos_enc = self.apply_pos_enc(patches, input_size=x.shape[2:])
|
@@ -123,10 +125,6 @@ class ViTPatchGenerator(nn.Module):
|
|
123 |
'pos_embed',
|
124 |
]
|
125 |
|
126 |
-
def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
|
127 |
-
if self.abs_pos:
|
128 |
-
self._load_embed(state_dict[f'{prefix}pos_embed'], self.pos_embed)
|
129 |
-
|
130 |
def _load_embed(self, src_embed: torch.Tensor, targ_embed: nn.Parameter):
|
131 |
if src_embed.shape != targ_embed.shape:
|
132 |
src_size = int(math.sqrt(src_embed.shape[1]))
|
@@ -208,6 +206,12 @@ class ViTPatchGenerator(nn.Module):
|
|
208 |
|
209 |
if self.cpe_mode:
|
210 |
if self.training:
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
min_scale = math.sqrt(0.1)
|
212 |
scale = torch.rand(batch_size, 1, 1, device=pos_embed.device) * (1 - min_scale) + min_scale
|
213 |
aspect_min = math.log(3 / 4)
|
@@ -237,6 +241,9 @@ class ViTPatchGenerator(nn.Module):
|
|
237 |
padding_mode='zeros',
|
238 |
align_corners=True,
|
239 |
).to(pos_embed.dtype)
|
|
|
|
|
|
|
240 |
else:
|
241 |
# i_rows, i_cols = input_dims
|
242 |
# p_rows, p_cols = pos_embed.shape[2:]
|
@@ -246,14 +253,14 @@ class ViTPatchGenerator(nn.Module):
|
|
246 |
# pos_embed = pos_embed[..., top:top+i_rows, left:left+i_cols]
|
247 |
# else:
|
248 |
max_dim = max(input_dims)
|
249 |
-
pos_embed = F.interpolate(pos_embed.float(), size=(max_dim, max_dim), align_corners=
|
250 |
|
251 |
pos_embed = window_select(pos_embed)
|
252 |
else:
|
253 |
pos_embed = window_select(pos_embed)
|
254 |
|
255 |
if pos_embed.shape[-2:] != input_dims:
|
256 |
-
pos_embed = F.interpolate(pos_embed.float(), size=input_dims, align_corners=
|
257 |
|
258 |
pos_embed = pos_embed.flatten(2).permute(0, 2, 1)
|
259 |
|
@@ -289,25 +296,3 @@ class ViTPatchLinear(nn.Linear):
|
|
289 |
**factory
|
290 |
)
|
291 |
self.patch_size = patch_size
|
292 |
-
|
293 |
-
def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
|
294 |
-
if self.bias is not None:
|
295 |
-
self.bias.data.copy_(state_dict[f'{prefix}bias'])
|
296 |
-
|
297 |
-
weight_key, chk_weight = next((k, t) for k, t in state_dict.items() if 'weight' in k)
|
298 |
-
|
299 |
-
if chk_weight.shape != self.weight.shape:
|
300 |
-
src_patch_size = int(math.sqrt(chk_weight.shape[1] // 3))
|
301 |
-
|
302 |
-
assert (src_patch_size ** 2) * 3 == chk_weight.shape[1], 'Unable to interpolate non-square patch size'
|
303 |
-
|
304 |
-
chk_weight = rearrange(chk_weight, 'b (c h w) -> b c h w', c=3, h=src_patch_size, w=src_patch_size)
|
305 |
-
chk_weight = F.interpolate(chk_weight, size=(self.patch_size, self.patch_size), mode='bicubic', align_corners=True, antialias=False)
|
306 |
-
chk_weight = rearrange(chk_weight, 'b c h w -> b (c h w)')
|
307 |
-
|
308 |
-
weight_key = weight_key[len(prefix):]
|
309 |
-
|
310 |
-
my_sd = self.state_dict()
|
311 |
-
my_weight = next(t for k, t in my_sd.items() if 'weight' in k)
|
312 |
-
my_weight.copy_(chk_weight)
|
313 |
-
pass
|
|
|
89 |
|
90 |
self.patch_normalizer = nn.LayerNorm(embed_dim) if normalize_patches else nn.Identity()
|
91 |
|
92 |
+
self.num_video_frames = None
|
93 |
+
|
94 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
95 |
patches = self.embed_patches(x)
|
96 |
patches, pos_enc = self.apply_pos_enc(patches, input_size=x.shape[2:])
|
|
|
125 |
'pos_embed',
|
126 |
]
|
127 |
|
|
|
|
|
|
|
|
|
128 |
def _load_embed(self, src_embed: torch.Tensor, targ_embed: nn.Parameter):
|
129 |
if src_embed.shape != targ_embed.shape:
|
130 |
src_size = int(math.sqrt(src_embed.shape[1]))
|
|
|
206 |
|
207 |
if self.cpe_mode:
|
208 |
if self.training:
|
209 |
+
if self.num_video_frames is not None:
|
210 |
+
if batch_size % self.num_video_frames != 0:
|
211 |
+
raise ValueError(f'Batch size {batch_size} must be divisible by num_video_frames {self.num_video_frames} for CPE mode.')
|
212 |
+
|
213 |
+
batch_size //= self.num_video_frames
|
214 |
+
|
215 |
min_scale = math.sqrt(0.1)
|
216 |
scale = torch.rand(batch_size, 1, 1, device=pos_embed.device) * (1 - min_scale) + min_scale
|
217 |
aspect_min = math.log(3 / 4)
|
|
|
241 |
padding_mode='zeros',
|
242 |
align_corners=True,
|
243 |
).to(pos_embed.dtype)
|
244 |
+
|
245 |
+
if self.num_video_frames is not None:
|
246 |
+
pos_embed = torch.repeat_interleave(pos_embed, self.num_video_frames, dim=0)
|
247 |
else:
|
248 |
# i_rows, i_cols = input_dims
|
249 |
# p_rows, p_cols = pos_embed.shape[2:]
|
|
|
253 |
# pos_embed = pos_embed[..., top:top+i_rows, left:left+i_cols]
|
254 |
# else:
|
255 |
max_dim = max(input_dims)
|
256 |
+
pos_embed = F.interpolate(pos_embed.float(), size=(max_dim, max_dim), align_corners=False, mode='bilinear').to(pos_embed.dtype)
|
257 |
|
258 |
pos_embed = window_select(pos_embed)
|
259 |
else:
|
260 |
pos_embed = window_select(pos_embed)
|
261 |
|
262 |
if pos_embed.shape[-2:] != input_dims:
|
263 |
+
pos_embed = F.interpolate(pos_embed.float(), size=input_dims, align_corners=False, mode='bilinear').to(pos_embed.dtype)
|
264 |
|
265 |
pos_embed = pos_embed.flatten(2).permute(0, 2, 1)
|
266 |
|
|
|
296 |
**factory
|
297 |
)
|
298 |
self.patch_size = patch_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|