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

Function prune_wanda

lib/prune.py:255–638  ·  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

253
254
255def prune_wanda(
256 args,
257 model,
258 tokenizer,
259 model_base=None,
260 device=torch.device("cuda:0"),
261 prune_n=0,
262 prune_m=0,
263 prune_data="wikitext",
264):
265 use_cache = model.config.use_cache
266 model.config.use_cache = False
267 print(f"loading calibration data {prune_data}")
268 assert prune_data in [
269 "wikitext",
270 "alpaca",
271 "alpaca_cleaned",
272 "alpaca_cleaned_no_safety",
273 "align",
274 "align_short",
275 "misalign",
276 ]
277 dataloader, _ = get_loaders(
278 prune_data,
279 nsamples=args.nsamples,
280 seed=args.seed,
281 seqlen=model.seqlen,
282 tokenizer=tokenizer,
283 disentangle=args.disentangle,
284 )
285 # dataloader, _ = get_loaders("c4",nsamples=args.nsamples,seed=args.seed,seqlen=model.seqlen,tokenizer=tokenizer)
286 print("dataset loading complete")
287 with torch.no_grad():
288 inps, outs, tars, attention_mask, position_ids = prepare_calibration_input(
289 model, dataloader, device, args.nsamples
290 )
291
292 if not args.disentangle:
293 tars = [torch.zeros_like(tar) for tar in tars] # remove -100's
294
295 inps = [inp.squeeze(0).to(device) for inp in inps]
296 tars = [tar.squeeze(0).to(device) for tar in tars]
297 attention_mask = [am.to(device) for am in attention_mask]
298 position_ids = [pids.to(device) for pids in position_ids]
299
300 layers = model.model.layers
301 if args.use_diff or args.recover_from_base:
302 assert model_base is not None
303 layers_base = model_base.model.layers
304
305 if args.prune_part:
306 print("only prune the layer with low jaccard index")
307 else:
308 print("prune every linear layer")
309
310 for i in range(len(layers)):
311 layer = layers[i]
312 subset = find_layers(layer)

Callers 1

mainFunction · 0.90

Calls 6

get_loadersFunction · 0.85
find_layersFunction · 0.85
WrappedGPTClass · 0.85
add_batchFunction · 0.85
return_given_alphaFunction · 0.85

Tested by

no test coverage detected