import torch import os import safetensors from transformers import AutoTokenizer,…
- User
import torch import os import safetensors from transformers import AutoTokenizer, AutoModelForCausalLM
os.environ['CUDA_VISIBLE_DEVICES'] ='0' device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model_path = "git/merged2" model_save = "git/merged5/"
def layer_removal( model, prune_list, ): layers = model.model.layers sorted_list = sorted(prune_list, reverse=True) prune_layer_num = len(sorted_list) first_del_layer = min(sorted_list)
# remove layers from back to front for prune_layer in sorted_list: del(layers[prune_layer]) for i in range(first_del_layer, model.config.num_hidden_layers - prune_layer_num): model.model.layers[i].self_attn.layer_idx = model.model.layers[i].self_attn.layer_idx - prune_layer_numdef main(): # Load model directly model = AutoModelForCausalLM.from_pretrained(model_path,torch_dtype=torch.bfloat16,device_map="auto") tokenizer = AutoTokenizer.from_pretrained(model_path,device_map="auto")
# Define a list to store the layer numbers that need to be pruned prune_list = [13] #has to be consecutive. # Call the layer_removal function and pass the model and the list as parameters to the function layer_removal(model, prune_list) # Load the model into cuda after running the function model.to(device) prompt = f"""<|im_start|>system You are an ai assistant<|im_end|> <|im_start|> Write a short email to my boss letting them know I am sick and wont be in.<|im_end|> <|im_start|>assistant """ inputs = tokenizer(prompt, return_tensors="pt").to(device) #verify layers have changed named_layers = dict(model.named_modules()) print (named_layers) # Generate generate_ids = model.generate(inputs.input_ids, max_length=160) print(tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]) #model.save_pretrained(save_directory = model_save, safe_serialization=True) #tokenizer.save_pretrained(model_save)main()
I worked on a script to load a model, prune layers, prompt it and then save it when prompts looked ok.. my problem is the prompts look ok but the model saved isnt.. Not sure if the original loaded model isnt really changed when prompted or I am not saving all the artifacts.. When I perform the same operation in mergekit the model doesnt prompt at all.. trying to create something I can test in memory instead of constantly writing out files and reloading ..
- Assistant
- User
can you make changes?
- Assistant
- User
i dont think this type of model pruning is right?
- Assistant
- User
what could be the reason or is the reason of not saving
- Assistant