Views
No views yet
LlamaForCausalLM class as shown below:1from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaForCausalLM
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/InternLM3-8B-Instruct-Converted-LlaMA", 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)
24llama_model = LlamaForCausalLM.from_pretrained(
25 "silence09/InternLM3-8B-Instruct-Converted-LlaMA",
26 torch_dtype='auto',
27 attn_implementation=attn_impl).to(device)
28llama_generated_ids = llama_model.generate(model_inputs.input_ids, max_new_tokens=100, do_sample=False)
29llama_generated_ids = [
30 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, llama_generated_ids)
31]
32llama_response = tokenizer.batch_decode(llama_generated_ids, skip_special_tokens=True)[0]
33print(llama_response)
34