运行调试大语言模型

很多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)
相关推荐
SuniaWang5 分钟前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
IDZSY04301 小时前
AI社交平台进阶指南:如何用AI社交提升工作学习效率
人工智能·学习
七七powerful1 小时前
运维养龙虾--AI 驱动的架构图革命:draw.io MCP 让运维画图效率提升 10 倍,使用codebuddy实战
运维·人工智能·draw.io
水星梦月1 小时前
大白话讲解AI/LLM核心概念
人工智能
温九味闻醉2 小时前
关于腾讯广告算法大赛2025项目分析1 - dataset.py
人工智能·算法·机器学习
White-Legend2 小时前
第三波GPT5.4 日400刀
人工智能·ai编程
. . . . .2 小时前
Claude Code Hooks的原理、触发执行机制以及如何编写 Hooks
人工智能
w_t_y_y2 小时前
codex(一)下载安装&使用
人工智能
唐兴通个人2 小时前
唐兴通应邀为平安财产险北京分公司高层主讲《新媒体营销》专项培训,引领保险业AI时代内容营销变革
人工智能