-
Notifications
You must be signed in to change notification settings - Fork 29
/
extract_features.py
205 lines (160 loc) · 8.13 KB
/
extract_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import gc
import os
import argparse
from tqdm import trange
import torch
import timm
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
from torchvision.models.feature_extraction import create_feature_extractor
from datasets import load_dataset
from tasks import get_models
from models import load_llm, load_tokenizer
import utils
def extract_llm_features(filenames, dataset, args):
"""
Extracts features from language models.
Args:
filenames: list of language model names by huggingface identifiers
dataset: huggingface dataset
args: argparse arguments
"""
texts = [str(x['text'][args.caption_idx]) for x in dataset]
for llm_model_name in filenames[::-1]:
save_path = utils.to_feature_filename(
args.output_dir, args.dataset, args.subset, llm_model_name,
pool=args.pool, prompt=args.prompt, caption_idx=args.caption_idx,
)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
print(f"\ndataset: \t{args.dataset}")
print(f"subset: \t{args.subset}")
print(f"processing:\t{llm_model_name}")
print(f'save_path: \t{save_path}')
if os.path.exists(save_path) and not args.force_remake:
print("file exists. skipping")
continue
language_model = load_llm(llm_model_name, qlora=args.qlora, force_download=args.force_download)
llm_param_count = sum([p.numel() for p in language_model.parameters()])
tokenizer = load_tokenizer(llm_model_name)
tokens = tokenizer(texts, padding="longest", return_tensors="pt")
llm_feats, losses, bpb_losses = [], [], []
# hack to get around HF mapping data incorrectly when using model-parallel
device = next(language_model.parameters()).device
for i in trange(0, len(dataset), args.batch_size):
# get embedding cuda device
token_inputs = {k: v[i:i+args.batch_size].to(device).long() for (k, v) in tokens.items()}
with torch.no_grad():
if "olmo" in llm_model_name.lower():
llm_output = language_model(
input_ids=token_inputs["input_ids"],
attention_mask=token_inputs["attention_mask"],
output_hidden_states=True,
)
else:
llm_output = language_model(
input_ids=token_inputs["input_ids"],
attention_mask=token_inputs["attention_mask"],
)
loss, avg_loss = utils.cross_entropy_loss(token_inputs, llm_output)
losses.extend(avg_loss.cpu())
bpb = utils.cross_entropy_to_bits_per_unit(loss.cpu(), texts[i:i+args.batch_size], unit="byte")
bpb_losses.extend(bpb)
# make sure to do all the processing in cpu to avoid memory problems
if args.pool == 'avg':
feats = torch.stack(llm_output["hidden_states"]).permute(1, 0, 2, 3)
mask = token_inputs["attention_mask"].unsqueeze(-1).unsqueeze(1)
feats = (feats * mask).sum(2) / mask.sum(2)
elif args.pool == 'last':
feats = [v[:, -1, :] for v in llm_output["hidden_states"]]
feats = torch.stack(feats).permute(1, 0, 2)
else:
raise NotImplementedError(f"unknown pooling {args.pool}")
llm_feats.append(feats.cpu())
print(f"average loss:\t{torch.stack(losses).mean().item()}")
save_dict = {
"feats": torch.cat(llm_feats).cpu(),
"num_params": llm_param_count,
"mask": tokens["attention_mask"].cpu(),
"loss": torch.stack(losses).mean(),
"bpb": torch.stack(bpb_losses).mean(),
}
torch.save(save_dict, save_path)
del language_model, tokenizer, llm_feats, llm_output
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
gc.collect()
return
def extract_lvm_features(filenames, dataset, args):
"""
Extracts features from vision models.
Args:
filenames: list of vision model names by timm identifiers
image_file_paths: list of image file paths
args: argparse arguments
"""
assert args.pool == 'cls', "pooling is not supported for lvm features"
for lvm_model_name in filenames:
assert 'vit' in lvm_model_name, "only vision transformers are supported"
save_path = utils.to_feature_filename(
args.output_dir, args.dataset, args.subset, lvm_model_name,
pool=args.pool, prompt=None, caption_idx=None,
)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
print(f"\ndataset: \t{args.dataset}")
print(f"subset: \t{args.subset}")
print(f"processing:\t{lvm_model_name}")
print(f'save_path: \t{save_path}')
if os.path.exists(save_path) and not args.force_remake:
print("file exists. skipping")
continue
vision_model = timm.create_model(lvm_model_name, pretrained=True).cuda().eval()
lvm_param_count = sum([p.numel() for p in vision_model.parameters()])
transform = create_transform(
**resolve_data_config(vision_model.pretrained_cfg, model=vision_model)
)
if "vit" in lvm_model_name:
return_nodes = [f"blocks.{i}.add_1" for i in range(len(vision_model.blocks))]
else:
raise NotImplementedError(f"unknown model {lvm_model_name}")
vision_model = create_feature_extractor(vision_model, return_nodes=return_nodes)
lvm_feats = []
for i in trange(0, len(dataset), args.batch_size):
with torch.no_grad():
ims = torch.stack([transform(dataset[j]['image']) for j in range(i, i+args.batch_size)]).cuda()
lvm_output = vision_model(ims)
if args.pool == "cls":
feats = [v[:, 0, :] for v in lvm_output.values()]
feats = torch.stack(feats).permute(1, 0, 2)
lvm_feats.append(feats.cpu())
torch.save({"feats": torch.cat(lvm_feats), "num_params": lvm_param_count}, save_path)
del vision_model, transform, lvm_feats, lvm_output
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
gc.collect()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--force_download", action="store_true")
parser.add_argument("--force_remake", action="store_true")
parser.add_argument("--num_samples", type=int, default=1024)
parser.add_argument("--batch_size", type=int, default=4)
parser.add_argument("--pool", type=str, default='avg', choices=['avg', 'cls'])
parser.add_argument("--prompt", action="store_true")
parser.add_argument("--dataset", type=str, default="prh")
parser.add_argument("--subset", type=str, default="wit_1024")
parser.add_argument("--caption_idx", type=int, default=0)
parser.add_argument("--modelset", type=str, default="val", choices=["val", "test"])
parser.add_argument("--modality", type=str, default="all", choices=["vision", "language", "all"])
parser.add_argument("--output_dir", type=str, default="./results/features")
parser.add_argument("--qlora", action="store_true")
args = parser.parse_args()
if args.qlora:
print(f"QLoRA is set to True. The alignment score will be slightly off.")
llm_models, lvm_models = get_models(args.modelset, modality=args.modality)
# load dataset once outside
dataset = load_dataset(args.dataset, revision=args.subset, split='train')
if args.modality in ["all", "language"]:
# extract all language model features
extract_llm_features(llm_models, dataset, args)
if args.modality in ["all", "vision"]:
# extract all vision model features
extract_lvm_features(lvm_models, dataset, args)