| from transformers import AutoModelForCausalLM, AutoTokenizer, QuantoConfig |
| from alphalora.expert_number import calculate_expert |
| import json |
| import torch |
| from lsaq_quant_assign import quantize_llama_like |
| import os |
| import argparse |
| import numpy as np |
|
|
| parser = argparse.ArgumentParser(description="parser") |
| parser.add_argument("--metric_name", type=str, default="alpha") |
| parser.add_argument("--model_id", type=str, default="Llama-2-7b-hf") |
| parser.add_argument("--keyword", type=str, default=None) |
| parser.add_argument("--cuda_id", type=int, default=1) |
|
|
| args = parser.parse_args() |
|
|
| |
| model_ids = [args.model_id] |
|
|
|
|
| for model_id in model_ids: |
| |
| model = AutoModelForCausalLM.from_pretrained( |
| model_id, |
| device_map=f"cuda:{args.cuda_id}", |
| torch_dtype=torch.float16 |
| ) |
| model_name = os.path.basename(model_id.rstrip("/")) |
| tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
| prompt = "Once upon a time" |
| inputs = tokenizer(prompt, return_tensors="pt").to(f"cuda:{args.cuda_id}") |
|
|
| |
| with torch.no_grad(): |
| outputs = model.generate( |
| **inputs, |
| max_new_tokens=100, |
| do_sample=True, |
| top_k=50, |
| top_p=0.95, |
| temperature=0.7 |
| ) |
|
|
| |
| decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) |
| print(decoded) |
| print(model) |
| |
|
|
| |
| |
| |
| |
| |
|
|
| metric_name = args.metric_name |
| keyword=args.keyword |
| all_layer_alpha = calculate_expert(model, metric=metric_name, keyword=keyword) |
| |
| value_index_pairs = [(value, idx) for idx, value in enumerate(all_layer_alpha)] |
| sorted_pairs = sorted(value_index_pairs, key=lambda x: x[0], reverse=True) |
| print(sorted_pairs) |
| print(f"metric_name {metric_name}:", [pairs[1] for pairs in sorted_pairs]) |
|
|
| |
| |
| |
|
|
| os.makedirs(f"metrics/{model_name}", exist_ok=True) |
| with open(f"metrics/{model_name}/{metric_name}.json", "w") as f: |
| json.dump(all_layer_alpha, f, indent=4) |
|
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
|
|
| |
| |
| |
|
|