运行调试大语言模型

很多LLM是开源的,是可以自己下载模型,运行调试的。

下载模型:https://www.modelscope.cn/models/Qwen/Qwen3-0.6B/files

代码: 上面只是模型的权重,词表等文件,代码是在transform库中的

首先,查找模型的class名称

bash 复制代码
Qwen3-8B# cat config.json 
{
  "architectures": [
    "Qwen3ForCausalLM"
  ],

然后在python 包中找这个类

bash 复制代码
(gaofeng1120) qwen3$ pwd
/home/gaofeng/anaconda3/envs/gaofeng1120/lib/python3.10/site-packages/transformers/models/qwen3
(gaofeng1120) qwen3$ ls
configuration_qwen3.py  __init__.py  modeling_qwen3.py  modular_qwen3.py  __pycache__
(gaofeng1120) qwen3$ cat modeling_qwen3.py |grep "class Qwen3ForCausalLM"
class Qwen3ForCausalLM(Qwen3PreTrainedModel, GenerationMixin):

调试:

例如使用Hugging Face的Transformers库, 在cpu下也可以调试

python 复制代码
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-0.6B"
model_name = "/home/qwen3-0.6B"
# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)

while True:
    prompt = "勾股定理有多少种证明方法?"
    #prompt = input("please input:")
    if prompt == 'exit':
        break

    messages = [
        {"role": "user", "content": prompt}
    ]
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True,
        enable_thinking=True  # Switches between thinking and non-thinking modes. Default is True.
    )
    model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

    # conduct text completion
    generated_ids = model.generate(
        **model_inputs,
        max_new_tokens=32768
    )
    output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()

    # parsing thinking content
    try:
        # rindex finding 151668 (</think>)
        index = len(output_ids) - output_ids[::-1].index(151668)
    except ValueError:
        index = 0

    thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
    content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")

    print("thinking content:", thinking_content)
    print("content:", content)
相关推荐
装不满的克莱因瓶3 分钟前
学习使用 Python 机器学习工具 sklearn
人工智能·python·学习·机器学习·ai·agent·智能体
AI智图坊10 分钟前
AIGC赋能跨境电商:如何利用「图生图」与模型提取,破解POD节日款“卡图案”技术瓶颈?
大数据·人工智能·gpt·ai作画·aigc
触底反弹13 分钟前
大模型时代:5 个 Prompt 替代 BERT 训练,搞定 NLP 五大任务
人工智能·node.js·api
vortex519 分钟前
AI Skill 设计:网络安全审计中的自主性与规范化博弈
人工智能·安全·web安全
云烟成雨TD24 分钟前
Spring AI 1.x 系列【37】RAG 知识库平台案例:知识库管理
java·人工智能·spring
GodGump24 分钟前
从生成式 AI 到行动式 AI:下一代人工智能为什么需要“行动能力”
人工智能
珠***格24 分钟前
实操落地|防逆流装置的安装规范、调试标准与故障处置
网络·数据库·人工智能·分布式·能源·边缘计算
龙码精神24 分钟前
QwenPaw 记忆与对话管理架构
人工智能
用户10357025368127 分钟前
Word 转 Markdown,一行命令搞定——AI 技能「docx2md」使用指南
人工智能
一切皆是因缘际会34 分钟前
AI智能新时代
数据结构·人工智能·ai·架构