Views
No views yet
Qwen2ForCausalLM class as shown below:1from transformers import AutoModelForCausalLM, AutoTokenizer, Qwen2ForCausalLM
2
3device = "cuda" # the device to load the model onto, cpu or cuda
4attn_impl = 'eager' # the attention implementation to use
5
6prompt = "大模型和人工智能经历了两年的快速发展,请你以此主题对人工智能的从业者写一段新年寄语"
7
8system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语).
9- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
10- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文."""
11messages = [
12 {"role": "system", "content": system_prompt},
13 {"role": "user", "content": prompt},
14 ]
15
16tokenizer = AutoTokenizer.from_pretrained("silence09/InternLM2.5-7B-Chat-Converted-Qwen2", trust_remote_code=True)
17text = tokenizer.apply_chat_template(
18 messages,
19 tokenize=False,
20 add_generation_prompt=True
21)
22model_inputs = tokenizer([text], return_tensors="pt").to(device)
23print(prompt)
24qwen2_model = Qwen2ForCausalLM.from_pretrained(
25 "silence09/InternLM2.5-7B-Chat-Converted-Qwen2",
26 torch_dtype='auto',
27 attn_implementation=attn_impl).to(device)
28qwen2_generated_ids = qwen2_model.generate(model_inputs.input_ids, max_new_tokens=100, do_sample=False)
29qwen2_generated_ids = [
30 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, qwen2_generated_ids)
31]
32qwen2_response = tokenizer.batch_decode(qwen2_generated_ids, skip_special_tokens=True)[0]
33print(qwen2_response)
34