forked from dvlab-research/LongLoRA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passkey_retrivial.py
148 lines (125 loc) · 5.76 KB
/
passkey_retrivial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Written by Yukang Chen
# Core code based on https://github.com/CStanKonrad/long_llama
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import math
import torch
import argparse
import random
import numpy as np
from numpy import random
from tqdm import tqdm
import transformers
from peft import PeftModel
from llama_attn_replace import replace_llama_attn
def parse_config():
parser = argparse.ArgumentParser(description='arg parser')
parser.add_argument('--base_model', type=str, default="/data1/pretrained-models/llama-7b-hf")
parser.add_argument('--cache_dir', type=str, default="./cache")
parser.add_argument('--context_size', type=int, default=-1, help='context size during fine-tuning')
parser.add_argument('--flash_attn', type=bool, default=True, help='whether to use flash attention 2')
parser.add_argument('--max_tokens', type=int, default=32000, help='maximum token length for evaluation')
parser.add_argument('--interval', type=int, default=1000, help='interval for evaluation')
parser.add_argument('--num_tests', type=int, default=10, help='number of repeat testing for each length')
args = parser.parse_args()
return args
def generate_prompt_landmark(n_garbage, seed):
"""Generates a text file and inserts an passkey at a random position."""
rnd_state = random.get_state()
random.seed(seed)
n_garbage_prefix = random.randint(0, n_garbage)
n_garbage_suffix = n_garbage - n_garbage_prefix
task_description = "There is an important info hidden inside a lot of irrelevant text. Find it and memorize them. I will quiz you about the important information there."
garbage = "The grass is green. The sky is blue. The sun is yellow. Here we go. There and back again."
garbage_inf = " ".join([garbage] * 5000)
assert len(garbage_inf) >= n_garbage
garbage_prefix = garbage_inf[:n_garbage_prefix]
garbage_suffix = garbage_inf[:n_garbage_suffix]
pass_key = random.randint(1, 50000)
information_line = f"The pass key is {pass_key}. Remember it. {pass_key} is the pass key."
final_question = "What is the pass key? The pass key is"
lines = [
task_description,
garbage_prefix,
information_line,
garbage_suffix,
final_question,
]
random.set_state(rnd_state)
return "\n".join(lines), str(pass_key)
def passkey_retrieval_test(model, tokenizer, device, use_cache=False, n_garbage=60000, seed=666):
prompt, answer = generate_prompt_landmark(n_garbage, seed)
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
input_ids = input_ids.to(device)
len_token = input_ids.shape[-1]
answer_ids = tokenizer(answer, return_tensors="pt").input_ids[:, 1:] # drop BOS
generation_output = model.generate(
input_ids=input_ids, max_new_tokens=answer_ids.shape[-1], num_beams=1, use_cache=use_cache
)
model_answer = generation_output[0, -answer_ids.shape[-1]:].cpu()
is_correct = (model_answer == answer_ids[0]).all().item()
#print(f"The correct answer is {tokenizer.decode(answer_ids[0].cpu())}")
#print(f"The model answer is {tokenizer.decode(model_answer.cpu())}, is_correct : {is_correct}")
return is_correct, len_token
def main(args):
device = "cuda:0"
torch.cuda.set_device(device)
print("base model", args.base_model)
if args.flash_attn:
replace_llama_attn()
# Set RoPE scaling factor
config = transformers.AutoConfig.from_pretrained(
args.base_model,
cache_dir=args.cache_dir,
)
context_size = args.context_size
orig_ctx_len = getattr(config, "max_position_embeddings", None) # this value should be 4096 for LLaMA2 models
if orig_ctx_len and context_size > orig_ctx_len:
scaling_factor = float(math.ceil(context_size / orig_ctx_len))
config.rope_scaling = {"type": "linear", "factor": scaling_factor}
# Load model and tokenizer
model = transformers.AutoModelForCausalLM.from_pretrained(
args.base_model,
config=config,
cache_dir=args.cache_dir,
torch_dtype=torch.float16,
device_map="auto",
)
model.resize_token_embeddings(32001)
tokenizer = transformers.AutoTokenizer.from_pretrained(
args.base_model,
cache_dir=args.cache_dir,
model_max_length=args.context_size if args.context_size > orig_ctx_len else orig_ctx_len,
padding_side="right",
use_fast=False,
)
total_test_points = args.max_tokens // args.interval
all_accuries = {}
for i in range(total_test_points):
# This is a rough ratio to control the number of texts and tokens
n_garbage = int(3.75 * (i + 1) * args.interval // 1024 * 1024)
passed_tests = 0
total_tokens = 0
for i in range(args.num_tests):
is_correct, len_tokens = passkey_retrieval_test(model, tokenizer, device, use_cache=not args.flash_attn, n_garbage=n_garbage, seed=i)
passed_tests += is_correct
total_tokens += len_tokens
avg_tokens = total_tokens//args.num_tests
accuracy = float(passed_tests)/args.num_tests
print("accuracy on the token length %d is %f"%(avg_tokens, accuracy))
all_accuries[str(avg_tokens)] = accuracy
print("accuries over tokens", all_accuries)
if __name__ == "__main__":
args = parse_config()
main(args)