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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
| class LLMNeedleHaystackTester: def __init__( self, needle="\nThe best thing to do in San Francisco is eat a sandwich and sit in Dolores Park on a sunny day.\n", haystack_dir="./haystack_for_detect", retrieval_question="What is the best thing to do in San Francisco?", results_version = 1, context_lengths_min = 1000, context_lengths_max = 50000, context_lengths_num_intervals = 20, context_lengths = None, document_depth_percent_min = 0, document_depth_percent_max = 100, document_depth_percent_intervals = 10, document_depth_percents = None, document_depth_percent_interval_type = "linear", model_provider = "OpenAI", model_name='', model_name_suffix=None, num_concurrent_requests = 1, save_results = True, save_contexts = True, final_context_length_buffer = 200, seconds_to_sleep_between_completions = None, print_ongoing_status = True ): if not needle or not haystack_dir or not retrieval_question: raise ValueError("Needle, haystack, and retrieval_question must be provided.") needles_and_stacks = [json.loads(l) for l in open(f"{haystack_dir}/needles.jsonl")] self.needle_list = [l["needle"] for l in needles_and_stacks] self.haystack_dir_list = [f"{haystack_dir}/part{i}" for i in range(1, 4)] self.retrieval_question_list = [l["question"] for l in needles_and_stacks] self.real_ansers_list = [l["real_needle"] for l in needles_and_stacks] self.results_version = results_version self.num_concurrent_requests = num_concurrent_requests self.save_results = save_results self.final_context_length_buffer = final_context_length_buffer self.save_contexts = save_contexts self.seconds_to_sleep_between_completions = seconds_to_sleep_between_completions self.print_ongoing_status = print_ongoing_status self.model_provider = model_provider self.testing_results = [] self.head_counter = defaultdict(list) if("/" in model_name): self.model_version = model_name.split("/")[-1] else: self.model_version = model_name if(model_name_suffix is not None): self.model_version += "_" + model_name_suffix
if context_lengths is None: if context_lengths_min is None or context_lengths_max is None or context_lengths_num_intervals is None: raise ValueError("Either context_lengths_min, context_lengths_max, context_lengths_intervals need to be filled out OR the context_lengths_list needs to be supplied.") else: self.context_lengths = np.round(np.linspace(context_lengths_min, context_lengths_max, num=context_lengths_num_intervals, endpoint=True)).astype(int) else: self.context_lengths = context_lengths
if document_depth_percents is None: if document_depth_percent_min is None or document_depth_percent_max is None or document_depth_percent_intervals is None: raise ValueError("Either document_depth_percent_min, document_depth_percent_max, document_depth_percent_intervals need to be filled out OR the document_depth_percents needs to be supplied.") else: if document_depth_percent_interval_type == 'linear': self.document_depth_percents = np.round(np.linspace(document_depth_percent_min, document_depth_percent_max, num=document_depth_percent_intervals, endpoint=True)).astype(int) elif document_depth_percent_interval_type == 'sigmoid': self.document_depth_percents = [self.logistic(x) for x in np.linspace(document_depth_percent_min, document_depth_percent_max, document_depth_percent_intervals)] else: self.document_depth_percents = document_depth_percents if document_depth_percent_interval_type not in [None, "linear", "sigmoid"]: raise ValueError("document_depth_percent_interval_type must be either None, 'linear' or 'sigmoid'. If you'd like your own distribution give a list of ints in via document_depth_percent_intervals") self.model_name = model_name self.enc = AutoTokenizer.from_pretrained(model_name, use_fast=False) print("loading from %s" % model_name) config = AutoConfig.from_pretrained(model_name) self.layer_num, self.head_num = config.num_hidden_layers, config.num_attention_heads print(f"layer number: {self.layer_num}, head number {self.head_num}") if "Qwen" in self.model_version: self.model_to_test = Qwen2ForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map='auto', use_flash_attention_2="flash_attention_2" ).eval() elif "Mixtral" in self.model_version: self.model_to_test = MixtralForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map='auto', use_flash_attention_2="flash_attention_2", trust_remote_code=True, ).eval() elif "Mistral" in self.model_version: self.model_to_test = MistralForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map='auto', use_flash_attention_2="flash_attention_2", trust_remote_code=True, ).eval() elif "Phi3" in self.model_version: self.model_to_test = Phi3ForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map='auto', use_flash_attention_2="flash_attention_2", trust_remote_code=True, ).eval() else: self.model_to_test = LlamaForCausalLM.from_pretrained( model_name, torch_dtype=torch.bfloat16, device_map='auto', use_flash_attention_2="flash_attention_2", ).eval() if 'llama-2-7b-80k' in self.model_version: scaling_factor = 10 reset_rope(self.model_to_test, model_max_train_len=81920, scaling_factor=scaling_factor) if "CUDA_VISIBLE_DEVICES" in os.environ: self.multi_gpus = len(os.environ["CUDA_VISIBLE_DEVICES"])>1 else: self.multi_gpus = True self.model_to_test_description = model_name self.evaluation_model = None self.debug='debug' def logistic(self, x, L=100, x0=50, k=.1): if x == 0: return 0 if x == 100: return 100 return np.round(L / (1 + np.exp(-k * (x - x0))), 3) def start_test(self, args): for ni in range(len(self.needle_list)): self.needle = self.needle_list[ni] self.haystack_dir = self.haystack_dir_list[ni] self.real_needle = self.real_ansers_list[ni] self.retrieval_question = self.retrieval_question_list[ni] if self.print_ongoing_status: self.print_start_test_summary() self.run_test(args) if os.path.exists(f"head_score/{self.model_version}.json"): with open(f"./head_score/{self.model_version}.json", "r") as file: head_counter = json.loads(file.readline()) for k,v in head_counter.items(): self.head_counter[k] += v with open(f"head_score/{self.model_version}.json", 'w') as f: json.dump(self.head_counter, f) def print_start_test_summary(self): print ("\n") print ("Starting Needle In A Haystack Testing...") print (f"- Model: {self.model_name}") print (f"- Context Lengths: {len(self.context_lengths)}, Min: {min(self.context_lengths)}, Max: {max(self.context_lengths)}") print (f"- Document Depths: {len(self.document_depth_percents)}, Min: {min(self.document_depth_percents)}%, Max: {max(self.document_depth_percents)}%") print (f"- Needle: {self.needle.strip()}") print ("\n\n") def run_test(self, args): tasks = [] for context_length in self.context_lengths: if context_length < args.s_len or context_length > args.e_len: continue for depth_percent in self.document_depth_percents: task = self.bound_evaluate_and_log(context_length, depth_percent) def bound_evaluate_and_log(self, *args): self.evaluate_and_log(*args) def evaluate_and_log(self, context_length, depth_percent): context = self.generate_context(context_length, depth_percent) question = f"Based on the content of the book, Question: {self.retrieval_question}\nAnswer:" if self.model_version in ["Mistral-7B-Instruct-v0.2", "Qwen1.5-14B-Chat"]: prompt = [ {"role": "user", "content": f"<book>{context}</book>\nBased on the content of the book, Question: {self.retrieval_question}\nAnswer:"}, ] input_ids = self.enc.apply_chat_template(conversation=prompt, tokenize=True, add_generation_prompt=True, return_tensors='pt') else: input_context = context + question input_ids = self.enc(input_context , return_tensors="pt")['input_ids'] test_start_time = time.time() self.prompt_ids = input_ids[0, :] if not self.multi_gpus: input_ids = input_ids.to(self.model_to_test.device) self.needle_start, self.needle_end = self.find_needle_idx(self.real_needle) with torch.no_grad(): q_outputs = self.model_to_test(input_ids=input_ids[:,:-1], use_cache=True, return_dict=True) output, retrieval_score = self.decode(q_outputs, input_ids[:,-1], 50) response = self.enc.decode(output, skip_special_tokens=True).strip()
test_end_time = time.time() test_elapsed_time = test_end_time - test_start_time score = scorer.score(self.real_needle, response)['rouge1'].recall*100 if score > 50: self.retrieval_head_accumulate(retrieval_score) head_score = [(i[0], np.mean(i[1])) for i in self.head_counter.items()] head_score = sorted(head_score, key=lambda x:x[1], reverse=True) print([[i[0]] for i in head_score][:20])
results = { 'model': self.model_to_test_description, 'context_length': int(context_length), 'depth_percent': float(depth_percent), 'version': self.results_version, 'needle': self.needle, 'model_response': response, 'score': score, 'test_duration_seconds': test_elapsed_time, 'test_timestamp_utc': datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S%z') } self.testing_results.append(results)
if self.print_ongoing_status: print (f"-- Test Summary -- ") print (f"Duration: {test_elapsed_time:.1f} seconds") print (f"Context: {context_length} tokens") print (f"Depth: {depth_percent}%") print (f"Score: {score}") print (f"Response: {response}\n")
context_file_location = f'{self.model_version.replace(".", "_")}_len_{context_length}_depth_{int(depth_percent*100)}'
if self.save_contexts: results['file_name'] : context_file_location
if not os.path.exists('contexts'): os.makedirs('contexts')
if not os.path.exists(f'contexts/{self.model_version}'): os.makedirs(f'contexts/{self.model_version}')
with open(f'contexts/{self.model_version}/{context_file_location}_context.txt', 'w') as f: f.write(context) if self.save_results: if not os.path.exists(f'results/graph/{self.model_version}'): os.makedirs(f'results/graph/{self.model_version}') p = f'results/graph/{self.model_version}/{context_file_location}_results.json' print("Writing at %s" % p) with open(p, 'w') as f: json.dump(results, f) def generate_context(self, context_length, depth_percent): context = self.read_context_files() context = self.encode_and_trim(context, context_length) context = self.insert_needle(context, depth_percent, context_length) return context def find_needle_idx(self, needle): needle_ids = self.enc(needle, add_special_tokens=False)["input_ids"] print(self.enc.decode(needle_ids, skip_special_tokens=False)) span_len = len(needle_ids) for i in range(len(self.prompt_ids)): token_span = self.prompt_ids[i : i + span_len] span_ids = set(token_span.tolist()) overlap = float(len(span_ids.intersection(set(needle_ids)))) / len(set(needle_ids)) if(overlap > 0.9): return i, i + span_len return -1, -1 def decode(self, q_outputs, inp, decode_len, block_list=None): output, retrieval_score = [], [[[0, ''] for _ in range(self.head_num)] for _ in range(self.layer_num)] past_kv = q_outputs.past_key_values for step_i in range(decode_len): inp = inp.view(1, 1) outputs = self.model_to_test(input_ids=inp, past_key_values=past_kv, use_cache=True, output_attentions=True, attn_mode="torch" ) past_kv = outputs.past_key_values inp = outputs.logits[0, -1].argmax() step_token = self.enc.convert_ids_to_tokens(inp.item()) output.append(inp.item()) self.retrieval_calculate(outputs.attentions, retrieval_score, inp, step_token) if step_token=='<0x0A>' or inp.item()==144: break return output, retrieval_score def retrieval_head_accumulate(self, retrieval_score): for layer_idx in range(self.layer_num): for head_idx in range(self.head_num): self.head_counter[f"{layer_idx}-{head_idx}"].append(retrieval_score[layer_idx][head_idx][0]) def read_context_files(self): context = "" max_context_length = max(self.context_lengths)
while len(context.split()) < max_context_length: for file in glob.glob(f"{self.haystack_dir}/*.txt"): with open(file, 'r') as f: context += f.read() return context def encode_and_trim(self, context, context_length): tokens = self.encode_text_to_tokens(context) if len(tokens) > context_length: context = self.decode_tokens(tokens, context_length) return context def insert_needle(self, context, depth_percent, context_length): tokens_needle = self.encode_text_to_tokens(self.needle) tokens_context = self.encode_text_to_tokens(context)
context_length -= self.final_context_length_buffer
if len(tokens_context) + len(tokens_needle) > context_length: tokens_context = tokens_context[:context_length - len(tokens_needle)]
if depth_percent == 100: tokens_new_context = tokens_context + tokens_needle else: insertion_point = int(len(tokens_context) * (depth_percent / 100)) tokens_new_context = tokens_context[:insertion_point]
if(self.model_provider in ["LLaMA", "LongLLaMA"]): period_tokens = [29889, 869] elif(self.model_provider == "Mistral"): period_tokens = [842, 28723] elif(self.model_provider == "GLM"): period_tokens = [918, 30930] else: period_tokens = self.encode_text_to_tokens('.') while tokens_new_context and tokens_new_context[-1] not in period_tokens: insertion_point -= 1 tokens_new_context = tokens_context[:insertion_point]
print("insertion at %d" % insertion_point) tokens_new_context += tokens_needle + tokens_context[insertion_point:]
new_context = self.decode_tokens(tokens_new_context) return new_context def retrieval_calculate(self, attention_maxtrix,retrieval_score, inp, step_token, topk=1): for layer_idx in range(self.layer_num): for head_idx in range(self.head_num): values, idx = attention_maxtrix[layer_idx][0][head_idx][-1].topk(topk) for v, i in zip(values, idx): if self.needle_start <= i < self.needle_end and inp.item()==self.prompt_ids[i].item(): retrieval_score[layer_idx][head_idx][0] += 1/(self.needle_end - self.needle_start) retrieval_score[layer_idx][head_idx][1] += step_token break def encode_text_to_tokens(self, text): if self.model_provider in ["OpenAI", "LLaMA", "Mistral", "GLM"]: return self.enc.encode(text) elif self.model_provider == "Anthropic": return self.enc.encode(text).ids else: raise ValueError("model_provider must be either 'OpenAI' or 'Anthropic'") def decode_tokens(self, tokens, context_length=None): if self.model_provider in ["OpenAI", "LLaMA", "Mistral", "GLM"]: return self.enc.decode(tokens[:context_length]) elif self.model_provider == "Anthropic": return self.enc.decode(tokens[:context_length]) else: raise ValueError("model_provider must be either 'OpenAI' or 'Anthropic'")
|