Views
No views yet
pytorch==2.1.2 transformers==4.36.1 accelerate==0.25.0 datasets==2.14.7 numpy==1.26.2 flash-attn==2.4.21import json
2import torch
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5model_id = "namespace-Pt/activation-beacon-llama2-7b-chat"
6
7tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
8model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, torch_dtype=torch.bfloat16)
9
10model = model.cuda().eval()
11
12with torch.no_grad():
13 # short context
14 text = "Tell me about yourself."
15 inputs = tokenizer(text, return_tensors="pt").to("cuda")
16 outputs = model.generate(**inputs, max_new_tokens=20)
17 print(f"Input Length: {inputs['input_ids'].shape[1]}")
18 print(f"Output: {tokenizer.decode(outputs[0], skip_special_tokens=True)}")
19
20 # reset memory before new generation task
21 model.memory.reset()
22
23 # long context
24 with open("data/toy/narrativeqa.json", encoding="utf-8") as f:
25 example = json.load(f)
26 inputs = tokenizer(example["context"], return_tensors="pt").to("cuda")
27 outputs = model.generate(**inputs, do_sample=False, top_p=1, temperature=1, max_new_tokens=20)[:, inputs["input_ids"].shape[1]:]
28 print("*"*20)
29 print(f"Input Length: {inputs['input_ids'].shape[1]}")
30 print(f"Answer: {example['answer']}")
31 print(f"Prediction: {tokenizer.decode(outputs[0], skip_special_tokens=True)}")This is a friendly reminder - the current text generation call will exceed the model's predefined maximum length (4096). Depending on the model, you may observe exceptions, performance degradation, or nothing at all. Just ignore it.@misc{zhang2024soaring,
title={Soaring from 4K to 400K: Extending LLM's Context with Activation Beacon},
author={Peitian Zhang and Zheng Liu and Shitao Xiao and Ninglu Shao and Qiwei Ye and Zhicheng Dou},
year={2024},
eprint={2401.03462},
archivePrefix={arXiv},
primaryClass={cs.CL}
}