MCPcopy Create free account
hub / github.com/boyiwei/alignment-attribution-code / prune_wandg_v1

Function prune_wandg_v1

lib/model_wrapper.py:191–238  ·  view source on GitHub ↗
(
    args,
    model,
    tokenizer,
    model_base=None,
    device=torch.device("cuda:0"),
    prune_n=0,
    prune_m=0,
    prune_data="wikitext",
)

Source from the content-addressed store, hash-verified

189
190
191def prune_wandg_v1(
192 args,
193 model,
194 tokenizer,
195 model_base=None,
196 device=torch.device("cuda:0"),
197 prune_n=0,
198 prune_m=0,
199 prune_data="wikitext",
200):
201 model = make_Act(model, verbose=False)
202
203 print(f"loading calibdation data {prune_data}")
204 assert prune_data in [
205 "wikitext",
206 "alpaca",
207 "alpaca_cleaned",
208 "alpaca_cleaned_no_safety",
209 "align",
210 "align_short",
211 "misalign",
212 ]
213 dataloader, _ = get_loaders(
214 prune_data,
215 nsamples=args.nsamples,
216 seed=args.seed,
217 seqlen=model.seqlen,
218 tokenizer=tokenizer,
219 disentangle=args.disentangle,
220 )
221 print("dataset loading complete")
222
223 model.requires_grad_(False)
224 for name, module in model.named_modules():
225 if isinstance(module, ActLinear):
226 module.base.requires_grad_(True)
227 module.base.zero_grad()
228
229 for batch in dataloader:
230 inp, tar = batch[0].to(device), batch[1].to(device)
231 assert args.disentangle, "should run in disentangle mode"
232 with no_act_recording(model):
233 loss = model(input_ids=inp, labels=tar)[0]
234 loss.backward()
235
236 _prune_core(args, model, model_base, prune_n, prune_m, prune_mode="gradient")
237 model = revert_Act_to_Linear(model)
238 model.zero_grad() # freeze gradient to save cuda memory
239
240
241def prune_wandg(

Callers

nothing calls this directly

Calls 5

get_loadersFunction · 0.85
_prune_coreFunction · 0.85
make_ActFunction · 0.70
no_act_recordingClass · 0.70
revert_Act_to_LinearFunction · 0.70

Tested by

no test coverage detected