Views
No views yet

1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# 1. Original LLaMA path
5original_llama_path = "/mounts/data/corp/huggingface/llama/llama-7b"
6
7# 2. Add [EOI] token to the tokenizer
8tokenizer = AutoTokenizer.from_pretrained(original_llama_path)
9tokenizer.add_tokens(["[EOI]"])
10
11# 3. Add the pretrained llama with this diff
12base_model = AutoModelForCausalLM.from_pretrained(original_llama_path)
13base_model.resize_token_embeddings(len(tokenizer))
14longform_model_diff = AutoModelForCausalLM.from_pretrained("akoksal/LongForm-LLaMA-7B-diff")
15longform_model = AutoModelForCausalLM.from_pretrained("akoksal/LongForm-LLaMA-7B-diff") # will change this to the actual model
16
17# Add diff with base model
18for name, param in base_model.named_parameters():
19 longform_model.state_dict()[name].copy_(param + longform_model_diff.state_dict()[name])
20del base_model, longform_model_diff
21# 4. Example
22instruction = "Write an essay about meditation. [EOI]"
23torch.manual_seed(42)
24input_ids = tokenizer(instruction, return_tensors="pt").input_ids
25target_ids = longform_model.generate(input_ids, do_sample=True, max_new_tokens=50, top_p=0.9)
26tokenizer.decode(target_ids[0], skip_special_tokens=True)
27# Output:
28# > Write an essay about meditation. [EOI] There are a few types of meditation\
29# but it essentially involves quieting the mind. The most common form of\
30# meditation is where you sit down for a period of time and focus on your\
31# breathing. Your goal is to be able to observe and| All | Recipe Generation | ELI5 | Writing Prompts | |
|---|---|---|---|---|
| T0++ | 10.9 | 18.7 | 3.8 | 10.2 |
| Tk-Instruct | 6.3 | 12.9* | 3.6 | 2.4 |
| Flan-T5 | 10.6 | 20.9* | 3.5 | 7.4 |
| Alpaca-LLaMA-7B | 14.6 | 19.5 | 12.5 | 11.8 |
| OPT-30B | 11.1 | 18.6 | 12.2 | 2.6 |
| LongForm-T5-XL | 16.3 | 20.2 | 18.3 | 10.6 |
| LongForm-OPT-2.7B | 17.8 | 15.5 | 17.9 | 19.9 |
| LongForm-OPT-6.7B | 17.7 | 16.9 | 17.2 | 19.0 |
| LongForm-LLaMA-7B‡ | 19.7 | 21.7 | 18.6 | 18.9 |
@misc{koksal2023longform,
title={LongForm: Effective Instruction Tuning with Reverse Instructions},
author={Abdullatif Köksal and Timo Schick and Anna Korhonen and Hinrich Schütze},
year={2023},
eprint={2304.08460},
archivePrefix={arXiv},
primaryClass={cs.CL}
}