-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_layer_importance_ddp.py
More file actions
387 lines (372 loc) · 14.7 KB
/
get_layer_importance_ddp.py
File metadata and controls
387 lines (372 loc) · 14.7 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import torch
import argparse
from functools import partial
from datasets import load_dataset, concatenate_datasets
from tasks.gqa import gqa_transform
from tasks.coco import coco_transform
from tasks.video_mmmu import videommmu_transform
import torch.nn.functional as F
import pickle
import os
from utils import create_mask_after_token, create_mask_after_last_token
from loguru import logger
from accelerate import Accelerator
from torch.utils.data import DataLoader, Subset
from models.kimi import load_model as load_kimi_model
from models.qwen3 import load_model as load_qwen_model
@torch.no_grad()
def get_layer_importance(
accelerator,
model,
model_config,
processor,
text_to_message,
loss_type,
temperature,
dataset,
dataloader,
modalities,
# topk_logits,
):
"""Compute layer-wise modality importance via KL/MSE loss when skipping each MoE layer.
For each MoE layer and modality, skip that layer for that modality and measure
the output distribution change. Results are gathered across DDP processes.
Args:
accelerator: Accelerator instance for DDP.
model: The MoE MLLM model.
model_config: Dict with get_lm, is_moe_layer, create_mask, eos_token.
processor: Model processor for tokenization.
text_to_message: Callable to format text into chat messages.
loss_type: 'kl' or 'mse' for loss computation.
temperature: Temperature for logits.
dataset: Dataset name (gqa, coco, video_mmmu).
dataloader: DataLoader over calibration samples.
modalities: List of modality names (e.g., ['text', 'visual']).
Returns:
Dict mapping layer_idx -> {modality -> normalized_loss}, or None on non-main ranks.
"""
unwrapped_model = accelerator.unwrap_model(model)
language_model = model_config["get_lm"](unwrapped_model)
num_hidden_layers = language_model.config.num_hidden_layers
is_moe_layer = model_config["is_moe_layer"]
local_layer_loss_dict = {}
local_total_token_count = 0
for batch_idx, batch in enumerate(dataloader):
if accelerator.is_main_process:
logger.info(f"Processing batch {batch_idx + 1}/{len(dataloader)}...")
# sync all processes
accelerator.wait_for_everyone()
batched_messages = [
text_to_message(text) for text in batch["model_input_org_text"]
]
batched_messages = processor.apply_chat_template(
batched_messages, add_generation_prompt=True, return_tensors="pt"
)
tmp = []
if dataset == "gqa" or dataset == "coco" or dataset == "video_mmmu":
for i, _ in enumerate(batched_messages):
batched_messages[i] = (
batched_messages[i] + batch["model_input_full_answer"][i]
)
batched_messages[i] = batched_messages[i] + model_config["eos_token"]
if dataset == "video_mmmu":
tmp.extend(batch["model_input_visual"][i])
frame_num = batch["model_input_frames"][i]
media_end_idx = batched_messages[i].find(
"<|media_start|>image<|media_content|><|media_pad|><|media_end|>"
)
batched_messages[i] = (
batched_messages[i][:media_end_idx]
+ "<|media_start|>image<|media_content|><|media_pad|><|media_end|>"
* (frame_num - 1)
+ batched_messages[i][media_end_idx:]
)
else:
raise ValueError(f"Not support {dataset}")
if dataset == "video_mmmu":
batch["model_input_visual"] = tmp
inputs = processor(
images=batch["model_input_visual"],
text=batched_messages,
return_tensors="pt",
padding=True,
padding_side="left",
truncation=True,
).to(accelerator.device)
# Note: `accelerator.prepare` already moved the model and dataloader to the correct device.
# `inputs` are on CPU here, but the model forward pass will handle moving them.
answer_masks = model_config["create_mask"](input_ids=inputs["input_ids"])
# The model is already prepared by accelerator, so it runs on the correct GPU
org_output = model(
**inputs,
use_cache=False,
return_dict=True,
)
org_logits = org_output.logits[answer_masks, :].contiguous()
# org_logits, org_indices = torch.topk(
# org_logits, topk_logits, dim=-1, sorted=False
# )
local_total_token_count += answer_masks.sum().item()
for layer_idx in range(num_hidden_layers):
if is_moe_layer(language_model.config, layer_idx):
modality_loss_dict = {}
for modality in modalities:
output = model(
**inputs,
use_cache=False,
return_dict=True,
moe_layer_skip=layer_idx,
skip_modality=modality,
)
logits = output.logits[answer_masks, :].contiguous()
# logits = logits.gather(dim=-1, index=org_indices)
if loss_type == "kl":
loss = F.relu(
F.kl_div(
F.log_softmax(logits / temperature, dim=1),
F.softmax(org_logits / temperature, dim=1),
reduction="sum",
)
)
elif loss_type == "mse":
loss = F.mse_loss(logits, org_logits, reduction="sum")
# if accelerator.is_main_process:
# logger.info(
# f"Layer {layer_idx} {modality} MSE Loss: {loss.item()}"
# )
modality_loss_dict[modality] = loss.item()
if layer_idx not in local_layer_loss_dict:
local_layer_loss_dict[layer_idx] = modality_loss_dict
else:
for modality in modalities:
local_layer_loss_dict[layer_idx][
modality
] += modality_loss_dict[modality]
# --- End of per-batch processing ---
# NEW: Aggregation step
accelerator.wait_for_everyone()
# Flatten the local results for gathering
metrics_to_gather = {
f"l{l_idx}_m{m}_loss": torch.tensor(loss, device=accelerator.device)
for l_idx, m_losses in local_layer_loss_dict.items()
for m, loss in m_losses.items()
}
metrics_to_gather["total_token_count"] = torch.tensor(
local_total_token_count, device=accelerator.device
)
# Gather metrics from all processes
gathered_metrics = accelerator.gather_for_metrics(metrics_to_gather)
# The rest of the logic (aggregation, normalization, saving) is done only on the main process
if accelerator.is_main_process:
logger.info("All processes finished. Aggregating results on the main process.")
final_layer_loss_dict = {}
total_token_count = gathered_metrics.pop("total_token_count").sum().item()
# Un-flatten the gathered results and sum them up
for key, tensor_val in gathered_metrics.items():
parts = key.split("_")
layer_idx = int(parts[0][1:])
modality = parts[1][1:]
if layer_idx not in final_layer_loss_dict:
final_layer_loss_dict[layer_idx] = {}
# Sum the loss from all processes
final_layer_loss_dict[layer_idx][modality] = tensor_val.sum().item()
# --- Start of your original final normalization logic ---
for layer_idx in final_layer_loss_dict:
for modality in final_layer_loss_dict[layer_idx]:
final_layer_loss_dict[layer_idx][modality] /= total_token_count
modalities_losses = {m: 0.0 for m in modalities}
for modality in modalities:
for layer_idx in final_layer_loss_dict:
modalities_losses[modality] += final_layer_loss_dict[layer_idx][
modality
]
for modality in modalities:
if modalities_losses[modality] > 0:
for layer_idx in final_layer_loss_dict:
final_layer_loss_dict[layer_idx][modality] /= modalities_losses[
modality
]
# --- End of final normalization logic ---
return final_layer_loss_dict
else:
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Compute layer-wise modality importance for MoDES calibration."
)
parser.add_argument(
"--name_or_path",
type=str,
default="storage/models/Kimi-VL-A3B-Instruct",
help="Model path or HuggingFace model ID (e.g., Kimi-VL, Qwen3-VL-MoE).",
)
parser.add_argument(
"--save_dir",
type=str,
default="storage",
help="Root directory to save layer importance results.",
)
parser.add_argument(
"--dataset",
type=str,
default="gqa",
choices=["gqa", "coco", "video_mmmu"],
help="Calibration dataset for layer importance computation.",
)
parser.add_argument(
"--batch_size",
type=int,
default=1,
help="Batch size for calibration data.",
)
parser.add_argument(
"--start_idx",
type=int,
default=0,
help="Starting index of samples in the dataset.",
)
parser.add_argument(
"--num_samples",
type=int,
default=512,
help="Number of calibration samples to use.",
)
parser.add_argument(
"--loss_type",
type=str,
default="kl",
choices=["mse", "kl"],
help="Loss type for measuring output change when skipping a layer (KL or MSE).",
)
parser.add_argument(
"--temperature",
type=float,
default=1.0,
help="Temperature for logits when computing loss.",
)
# parser.add_argument("--topk_logits", type=int, default=1000)
args = parser.parse_args()
accelerator = Accelerator()
name_or_path = args.name_or_path
save_dir = os.path.join(args.save_dir, args.dataset, name_or_path.split("/")[-1])
loss_type = args.loss_type
batch_size = args.batch_size
start_idx = args.start_idx
num_samples = args.num_samples
temperature = args.temperature
dataset = args.dataset
# topk_logits = args.topk_logits
model_name = ""
if accelerator.is_main_process:
os.makedirs(save_dir, exist_ok=True)
load_model = None
text_to_message = None
modalities = None
model_config = {}
if "Kimi-VL" in name_or_path:
load_model = load_kimi_model
model_name = "Kimi-VL"
modalities = ["text", "visual"]
model_config = {
"get_lm": lambda m: m.language_model,
"is_moe_layer": lambda cfg, idx: idx >= cfg.first_k_dense_replace
and idx % cfg.moe_layer_freq == 0,
"create_mask": partial(
create_mask_after_token, special_token_id=163588, offset=3
),
"eos_token": "<|im_end|>[EOS]",
}
elif "Qwen3-VL" in name_or_path:
load_model = load_qwen_model
model_name = "Qwen3-VL"
special_token_id = 151644
offset = 3
eos_token = "<|im_end|>"
modalities = ["text", "visual"]
model_config = {
"get_lm": lambda m: m.model.language_model,
"is_moe_layer": lambda cfg, idx: (
hasattr(cfg, "num_experts")
and cfg.num_experts > 0
and (idx + 1) % cfg.decoder_sparse_step == 0
and idx not in getattr(cfg, "mlp_only_layers", [])
),
"create_mask": partial(
create_mask_after_last_token,
special_token_id=special_token_id,
offset=offset,
),
"eos_token": eos_token,
}
else:
raise ValueError(f"Not support {name_or_path}")
text_to_message = lambda text: [
{
"role": "user",
"content": [
{"type": "image", "image": "path/to/image"},
{"type": "text", "text": text},
],
}
]
model, processor = load_model(name_or_path, device_map="cpu")
data = None
if "gqa" in dataset:
data = load_dataset(
"storage/datasets/GQA/testdev_balanced_instructions", token=True
)["train"]
data.set_transform(gqa_transform)
elif "coco" in dataset:
data = load_dataset("storage/datasets/COCO-Caption2017/data", token=True)[
"validation"
]
data.set_transform(coco_transform)
elif "video_mmmu" in dataset:
assert model_name == "Kimi-VL", "VideoMMMU only support Kimi-VL"
adaptation = load_dataset("storage/datasets/VideoMMMU/Adaptation", token=True)[
"test"
]
comprehension = load_dataset(
"storage/datasets/VideoMMMU/Comprehension", token=True
)["test"]
perception = load_dataset("storage/datasets/VideoMMMU/Perception", token=True)[
"test"
]
data = concatenate_datasets([adaptation, comprehension, perception])
data.set_transform(videommmu_transform)
else:
raise ValueError(f"Not support {args.dataset}")
subset_indices = list(range(start_idx, min(start_idx + num_samples, len(data))))
subset = Subset(data, subset_indices)
def custom_collate_fn(batch):
"""Collate batch by stacking list of dicts into dict of lists."""
collated_batch = {}
keys = batch[0].keys()
# import ipdb; ipdb.set_trace()
for key in keys:
collated_batch[key] = [d[key] for d in batch]
return collated_batch
dataloader = DataLoader(
subset, batch_size=batch_size, shuffle=False, collate_fn=custom_collate_fn
)
# model, dataloader = accelerator.prepare(model, dataloader)
model.to(accelerator.device)
dataloader = accelerator.prepare(dataloader)
layer_importance = get_layer_importance(
accelerator,
model,
model_config,
processor,
text_to_message,
loss_type,
temperature,
dataset,
dataloader,
modalities,
# topk_logits,
)
if accelerator.is_main_process:
save_path = os.path.join(save_dir, f"{loss_type}_{start_idx}_{num_samples}.pkl")
with open(save_path, "wb") as f:
pickle.dump(layer_importance, f)
logger.info(f"Results saved to {save_path}")