def computegradients(model, dataloader, device): model.train() optimizer =…
- User
def compute_gradients(model, data_loader, device): model.train() optimizer = torch.optim.SGD(model.parameters(), lr=0.01) loss_function = torch.nn.CrossEntropyLoss()
batch = next(iter(data_loader)) inputs = batch["input_ids"].squeeze(1).to(device) attention_mask = batch["attention_mask"].squeeze(1).to(device) labels = inputs.clone() optimizer.zero_grad() outputs = model(inputs, attention_mask=attention_mask) logits = outputs.logits loss = loss_function(logits.view(-1, logits.size(-1)), labels.view(-1)) loss.backward()def compute_importance_scores_with_gradients(model): importance_scores = {}
for name, param in model.named_parameters(): if param.requires_grad and param.grad is not None: importance = (param * param.grad).abs() importance_scores[name] = importance return importance_scoresdef prune_mama_mutation_2(args, model, tokenizer, device=torch.device("cuda:0"), prune_n=0, prune_m=0): # TODO: Optimize the MAMA pruning algorithm based on advanced indicators. # Last Updated Date: 20240921 - # status: under review from torch.utils.data import DataLoader from datasets import load_dataset
# Load a small dataset for gradient computation dataset = load_dataset('wikitext', 'wikitext-2-raw-v1', split='validation') def tokenize_function(examples): return tokenizer(examples['text'], return_tensors='pt', truncation=True, padding='max_length', max_length=512) tokenized_dataset = dataset.map(tokenize_function, batched=True) data_loader = DataLoader(tokenized_dataset, batch_size=1) model.to(device) compute_gradients(model, data_loader, device) importance_scores = compute_importance_scores_with_gradients(model) prune_parameters(model, importance_scores, args.sparsity_ratio) # the output log: (prune_llm) [ec2-user@ip-172-31-5-145 suanfamama-prune-1]$ python main.py --model baffo32/decapoda-research-llama-7B-hf --prune_method mama_mutation_2 --sparsity_ratio 0.50 --sparsity_type unstructured --save out/llama_7b/unstructured/prune_mampython main.py --model baffo32/decapoda-research-llama-7B-hf --prune_method mama_mutation_2 --sparsity_ratio 0.50 --sparsity_type unstructured --save out/llama_7b/unstructured/prune_mama_mutation_2/torch 1.10.1 transformers 4.28.0 accelerate 0.18.0
of gpus: 1
loading llm model baffo32/decapoda-research-llama-7B-hf /home/ec2-user/miniconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning:
resume_downloadis deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, useforce_download=True. warnings.warn( Loading checkpoint shards: 0%| Loading checkpoint shards: 3%|██████▌ Loading checkpoint shards: 6%|█████████████ Loading checkpoint shards: 9%|███████████████████▌ Loading checkpoint shards: 12%|██████████████████████████ Loading checkpoint shards: 15%|████████████████████████████████▌ Loading checkpoint shards: 18%|███████████████████████████████████████ Loading checkpoint shards: 21%|█████████████████████████████████████████████▌ Loading checkpoint shards: 24%|████████████████████████████████████████████████████ Loading checkpoint shards: 27%|██████████████████████████████████████████████████████████▋ Loading checkpoint shards: 30%|████████████████████████████████████████████████████████████████▊ Loading checkpoint shards: 33%|███████████████████████████████████████████████████████████████████████▎ Loading checkpoint shards: 36%|█████████████████████████████████████████████████████████████████████████████▊ Loading checkpoint shards: 39%|████████████████████████████████████████████████████████████████████████████████████▎ Loading checkpoint shards: 42%|██████████████████████████████████████████████████████████████████████████████████████████▊ | 14/33 [00:42<00:58, 3.08s/it] - Assistant