Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions hf_prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ def main(args):
if pruner_type in ['taylor']:
example_prompts = get_examples('bookcorpus', tokenizer, 10, seq_len = 64)
logger.log("Start Backwarding in iterative steps = {}...".format(i))
if args.taylor in ['param_mix', 'param_second']:
for j in range(args.num_examples):
batch_input = example_prompts[j].unsqueeze(0)
loss = model(batch_input, labels=batch_input).loss
logger.log("Loss = {}".format(loss))
loss.backward()

for module_param in model.parameters():
module_param.grad = module_param.grad * module_param.grad / args.num_examples
if hasattr(module_param, 'acc_grad'):
module_param.acc_grad += module_param.grad
else:
module_param.acc_grad = copy.deepcopy(module_param.grad)
model.zero_grad()
del loss.grad

print(f"example_prompts.device: {example_prompts.device}")
print(f"model.device: {next(model.parameters()).device}")

loss = model(example_prompts, labels=example_prompts).loss
logger.log("Loss = {}".format(loss))
loss.backward()
Expand Down