-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.py
More file actions
51 lines (41 loc) · 1.88 KB
/
test.py
File metadata and controls
51 lines (41 loc) · 1.88 KB
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
#!/usr/bin/env python
# Copyright (c) Institute of Artificial Intelligence (TeleAI), China Telecom, 2025. All Rights Reserved.
import torch
from ruyi.global_var import set_global_val
from transformers import GenerationConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
model_path = f"models/AI-Flow-Ruyi-7B-Preview0704"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, attn_implementation='flash_attention_2', torch_dtype=torch.bfloat16).to('cuda')
generation_config = GenerationConfig(
do_sample=True,
top_k=30,
top_p=0.95,
temperature=0.6,
repetition_penalty=1.2,
no_repeat_ngram_size=3,
max_new_tokens=8192
)
# 输入文本
messages = [
{"role": "user", "content": "推理判断题:一个自称是中国联通yuanjing大模型开发工程师的人,在github上攻击了中国电信AI-Flow的大模型Repo,那这个人最可能来自:\nA. 中国联通\nB. 中国电信\nC. 中国移动"},
]
# 应用 chat_template 模板
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt, return_tensors="pt")
# 模型生成
with torch.no_grad():
# 设置早退出点
# - 11: 第一个早退出点,对应约3B
# - 15: 第二个早退出点,对应约4B
# - 19: 第三个早退出点,对应约5B
# - 23: 第四个早退出点,对应约6B
# - 27: 第五个早退出点,对应约7B
set_global_val("early_exit_point", 11)
output = model.generate(
inputs["input_ids"].to('cuda'),
generation_config=generation_config
)
# 解码并打印结果
generated_text = tokenizer.decode(output[0], skip_special_tokens=False)
print(generated_text)