|
from typing import List |
|
from data.dataloader import build_dataloader |
|
|
|
from methods.elasticdnn.api.online_model_v2 import ElasticDNN_OnlineModel |
|
|
|
import torch |
|
import sys |
|
from torch import nn |
|
from methods.elasticdnn.api.model import ElasticDNN_OfflineSegFMModel, ElasticDNN_OfflineSegMDModel |
|
from methods.elasticdnn.api.algs.md_pretraining_wo_fbs import ElasticDNN_MDPretrainingWoFBSAlg |
|
from methods.elasticdnn.model.base import ElasticDNNUtil |
|
from methods.elasticdnn.pipeline.offline.fm_to_md.base import FM_to_MD_Util |
|
from methods.elasticdnn.pipeline.offline.fm_to_md.vit import FM_to_MD_ViT_Util |
|
from methods.elasticdnn.pipeline.offline.fm_lora.base import FMLoRA_Util |
|
from methods.elasticdnn.pipeline.offline.fm_lora.vit import FMLoRA_ViT_Util |
|
from methods.elasticdnn.model.bert import ElasticBertUtil |
|
from utils.common.file import ensure_dir |
|
from utils.dl.common.model import LayerActivation, get_module, get_parameter, set_module |
|
from utils.common.exp import save_models_dict_for_init, get_res_save_dir |
|
from data import build_scenario |
|
from utils.dl.common.loss import CrossEntropyLossSoft |
|
import torch.nn.functional as F |
|
from utils.dl.common.env import create_tbwriter |
|
import os |
|
from utils.common.log import logger |
|
from utils.common.data_record import write_json |
|
|
|
from methods.feat_align.main import OnlineFeatAlignModel, FeatAlignAlg |
|
import tqdm |
|
from methods.feat_align.mmd import mmd_rbf |
|
|
|
class ElasticDNN_SeClsOnlineModel(ElasticDNN_OnlineModel): |
|
|
|
@torch.no_grad() |
|
def sd_feedback_to_md(self, after_da_sd, unpruned_indexes_of_layers): |
|
self.models_dict['sd'] = after_da_sd |
|
self.before_da_md = deepcopy(self.models_dict['md']) |
|
|
|
logger.info('\n\nsurrogate DNN feedback to master DNN...\n\n') |
|
|
|
|
|
cur_unpruned_indexes = None |
|
cur_unpruned_indexes_name = None |
|
|
|
for p_name, p in self.models_dict['sd'].named_parameters(): |
|
matched_md_param = self.get_md_matched_param_of_sd_param(p_name) |
|
logger.debug(f'if feedback: {p_name}') |
|
if matched_md_param is None: |
|
continue |
|
logger.debug(f'start feedback: {p_name}, {p.size()} -> {matched_md_param.size()}') |
|
|
|
|
|
|
|
if p_name in unpruned_indexes_of_layers.keys(): |
|
cur_unpruned_indexes = unpruned_indexes_of_layers[p_name] |
|
cur_unpruned_indexes_name = p_name |
|
|
|
if p.size() != matched_md_param.size(): |
|
logger.debug(f'cur unpruned indexes: {cur_unpruned_indexes_name}, {cur_unpruned_indexes.size()}') |
|
|
|
if p.dim() == 1: |
|
new_p = deepcopy(matched_md_param) |
|
new_p[cur_unpruned_indexes] = p |
|
elif p.dim() == 2: |
|
if p.size(0) < matched_md_param.size(0): |
|
new_p = deepcopy(matched_md_param) |
|
new_p[cur_unpruned_indexes] = p |
|
else: |
|
new_p = deepcopy(matched_md_param) |
|
new_p[:, cur_unpruned_indexes] = p |
|
p = new_p |
|
|
|
assert p.size() == matched_md_param.size(), f'{p.size()}, {matched_md_param.size()}' |
|
|
|
if 'classifier' in p_name: |
|
continue |
|
|
|
|
|
assert hasattr(self, 'last_trained_cls_indexes') |
|
print(self.last_trained_cls_indexes) |
|
|
|
diff = self._compute_diff(matched_md_param, p) |
|
|
|
matched_md_param.copy_(p) |
|
logger.debug(f'SPECIFIC FOR CL HEAD | end feedback: {p_name}, diff: {diff:.6f}') |
|
else: |
|
diff = self._compute_diff(matched_md_param, (matched_md_param + p) / 2.) |
|
matched_md_param.copy_((matched_md_param + p) / 2.) |
|
logger.debug(f'end feedback: {p_name}, diff: {diff:.6f}') |
|
|
|
def add_cls_in_head(self, num_cls): |
|
head: nn.Linear = get_module(self.models_dict['md'], 'classifier') |
|
|
|
new_head = nn.Linear(head.in_features, head.out_features + num_cls, head.bias is not None, device=self.device) |
|
|
|
|
|
|
|
|
|
new_head.weight.data[0: head.out_features] = deepcopy(head.weight.data) |
|
new_head.bias.data[0: head.out_features] = deepcopy(head.bias.data) |
|
set_module(self.models_dict['md'], 'classifier', new_head) |
|
set_module(self.models_dict['fm'], 'classifier', new_head) |
|
|
|
|
|
def get_accuracy(self, test_loader, *args, **kwargs): |
|
acc = 0 |
|
sample_num = 0 |
|
|
|
self.to_eval_mode() |
|
|
|
with torch.no_grad(): |
|
pbar = tqdm.tqdm(enumerate(test_loader), total=len(test_loader), dynamic_ncols=True, leave=False) |
|
for batch_index, (x, y) in pbar: |
|
for k, v in x.items(): |
|
if isinstance(v, torch.Tensor): |
|
x[k] = v.to(self.device) |
|
y = y.to(self.device) |
|
output = self.infer(x) |
|
pred = F.softmax(output, dim=1).argmax(dim=1) |
|
correct = torch.eq(pred, y).sum().item() |
|
acc += correct |
|
sample_num += len(y) |
|
|
|
pbar.set_description(f'cur_batch_total: {len(y)}, cur_batch_correct: {correct}, ' |
|
f'cur_batch_acc: {(correct / len(y)):.4f}') |
|
|
|
acc /= sample_num |
|
return acc |
|
|
|
def get_elastic_dnn_util(self) -> ElasticDNNUtil: |
|
return ElasticBertUtil() |
|
|
|
def get_fm_matched_param_of_md_param(self, md_param_name): |
|
|
|
self_param_name = md_param_name |
|
fm = self.models_dict['fm'] |
|
if any([k in self_param_name for k in ['fbs', 'ab', 'embeddings']]): |
|
return None |
|
|
|
p = get_parameter(self.models_dict['md'], self_param_name) |
|
if p.dim() == 0: |
|
return None |
|
elif p.dim() == 1 and 'LayerNorm' in self_param_name and 'weight' in self_param_name: |
|
return get_parameter(fm, self_param_name) |
|
|
|
|
|
if ('query' in self_param_name or 'key' in self_param_name or \ |
|
'value' in self_param_name) and ('weight' in self_param_name): |
|
|
|
ss = self_param_name.split('.') |
|
|
|
fm_qkv_name = '.'.join(ss[0: -1]) + '.fc' |
|
fm_qkv = get_module(fm, fm_qkv_name) |
|
|
|
fm_abs_name = '.'.join(ss[0: -1]) + '.ab' |
|
fm_abs = get_module(fm, fm_abs_name) |
|
|
|
|
|
|
|
if not hasattr(fm_abs, '_mul_lora_weight'): |
|
logger.debug(f'set _mul_lora_weight in {fm_abs_name}') |
|
setattr(fm_abs, '_mul_lora_weight', |
|
nn.Parameter(fm_abs[1].weight @ fm_abs[0].weight)) |
|
|
|
return torch.cat([ |
|
fm_qkv.weight.data, |
|
fm_abs._mul_lora_weight.data |
|
], dim=0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif 'dense' in self_param_name and 'weight' in self_param_name: |
|
fm_param_name = self_param_name.replace('.linear', '') |
|
return get_parameter(fm, fm_param_name) |
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
|
return None |
|
|
|
def update_fm_param(self, md_param_name, cal_new_fm_param_by_md_param): |
|
if not ('query' in md_param_name or 'key' in md_param_name or 'value' in md_param_name): |
|
matched_fm_param_ref = self.get_fm_matched_param_of_md_param(md_param_name) |
|
matched_fm_param_ref.copy_(cal_new_fm_param_by_md_param) |
|
else: |
|
new_fm_attn_weight, new_fm_lora_weight = torch.chunk(cal_new_fm_param_by_md_param, 2, 0) |
|
|
|
ss = md_param_name.split('.') |
|
fm = self.models_dict['fm'] |
|
|
|
|
|
fm_qkv_name = '.'.join(ss[0: -1]) + '.fc' |
|
fm_qkv = get_module(fm, fm_qkv_name) |
|
fm_qkv.weight.data.copy_(new_fm_attn_weight) |
|
|
|
|
|
fm_abs_name = '.'.join(ss[0: -1]) + '.ab' |
|
fm_abs = get_module(fm, fm_abs_name) |
|
fm_abs._mul_lora_weight.data.copy_(new_fm_lora_weight) |
|
|
|
def get_md_matched_param_of_fm_param(self, fm_param_name): |
|
return super().get_md_matched_param_of_fm_param(fm_param_name) |
|
|
|
def get_md_matched_param_of_sd_param(self, sd_param_name): |
|
|
|
|
|
|
|
self_param_name = sd_param_name |
|
md = self.models_dict['md'] |
|
if any([k in self_param_name for k in ['fbs', 'ab', 'embeddings']]): |
|
return None |
|
|
|
p = get_parameter(self.models_dict['sd'], self_param_name) |
|
if p.dim() == 0: |
|
return None |
|
elif p.dim() == 1 and 'LayerNorm' in self_param_name and 'weight' in self_param_name: |
|
return get_parameter(md, self_param_name) |
|
|
|
if 'classifier' in self_param_name: |
|
return get_parameter(md, self_param_name) |
|
|
|
|
|
if ('query' in self_param_name or 'key' in self_param_name or \ |
|
'value' in self_param_name) and ('weight' in self_param_name): |
|
|
|
|
|
return get_parameter(md, self_param_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif 'intermediate.dense.0.weight' in self_param_name: |
|
fm_param_name = '.'.join(self_param_name.split('.')[0: -2]) + '.linear.weight' |
|
return get_parameter(md, fm_param_name) |
|
|
|
elif 'output.dense' in self_param_name and 'weight' in self_param_name: |
|
fm_param_name = self_param_name |
|
return get_parameter(md, fm_param_name) |
|
|
|
else: |
|
|
|
return None |
|
|
|
def get_task_head_params(self): |
|
head = get_module(self.models_dict['sd'], 'classifier') |
|
return list(head.parameters()) |
|
|
|
|
|
from methods.gem.gem_el_bert import OnlineGEMModel |
|
import tqdm |
|
from methods.feat_align.mmd import mmd_rbf |
|
from copy import deepcopy |
|
|
|
|
|
class SeClsOnlineGEMModel(OnlineGEMModel): |
|
def get_trained_params(self): |
|
qkv_and_norm_params = [p for n, p in self.models_dict['main'].named_parameters() if 'query' in n or 'key' in n or 'value' in n or 'dense' in n or 'LayerNorm' in n] |
|
return qkv_and_norm_params |
|
|
|
def forward_to_get_task_loss(self, x, y): |
|
return F.cross_entropy(self.infer(x), y) |
|
|
|
def add_cls_in_head(self, num_cls): |
|
return |
|
|
|
head: nn.Linear = get_module(self.models_dict['main'], 'head') |
|
|
|
new_head = nn.Linear(head.in_features, head.out_features + num_cls, head.bias is not None, device=self.device) |
|
new_head.weight.data[0: head.out_features] = deepcopy(head.weight.data) |
|
new_head.bias.data[0: head.out_features] = deepcopy(head.bias.data) |
|
set_module(self.models_dict['main'], 'head', new_head) |
|
|
|
def infer(self, x, *args, **kwargs): |
|
return self.models_dict['main'](**x) |
|
|
|
def get_accuracy(self, test_loader, *args, **kwargs): |
|
_d = test_loader.dataset |
|
from data import build_dataloader, split_dataset |
|
if _d.__class__.__name__ == '_SplitDataset' and _d.underlying_dataset.__class__.__name__ == 'MergedDataset': |
|
print('\neval on merged datasets') |
|
|
|
merged_full_dataset = _d.underlying_dataset.datasets |
|
ratio = len(_d.keys) / len(_d.underlying_dataset) |
|
|
|
if int(len(_d) * ratio) == 0: |
|
ratio = 1. |
|
|
|
|
|
|
|
|
|
|
|
|
|
test_loaders = [] |
|
for d in merged_full_dataset: |
|
n = int(len(d) * ratio) |
|
if n == 0: |
|
n = len(d) |
|
sub_dataset = split_dataset(d, min(max(test_loader.batch_size, n), len(d)))[0] |
|
loader = build_dataloader(sub_dataset, min(test_loader.batch_size, n), test_loader.num_workers, False, None) |
|
test_loaders += [loader] |
|
|
|
accs = [self.get_accuracy(loader) for loader in test_loaders] |
|
print(accs) |
|
return sum(accs) / len(accs) |
|
|
|
acc = 0 |
|
sample_num = 0 |
|
|
|
self.to_eval_mode() |
|
|
|
with torch.no_grad(): |
|
pbar = tqdm.tqdm(enumerate(test_loader), total=len(test_loader), dynamic_ncols=True, leave=False) |
|
for batch_index, (x, y) in pbar: |
|
for k, v in x.items(): |
|
if isinstance(v, torch.Tensor): |
|
x[k] = v.to(self.device) |
|
y = y.to(self.device) |
|
output = self.infer(x) |
|
pred = F.softmax(output, dim=1).argmax(dim=1) |
|
correct = torch.eq(pred, y).sum().item() |
|
acc += correct |
|
sample_num += len(y) |
|
|
|
|
|
|
|
|
|
pbar.set_description(f'cur_batch_total: {len(y)}, cur_batch_correct: {correct}, ' |
|
f'cur_batch_acc: {(correct / len(y)):.4f}') |
|
|
|
acc /= sample_num |
|
return acc |