运行调试大语言模型

很多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)
相关推荐
龙亘川20 分钟前
AI + 人社新范式:智慧人社系统如何为民生治理数字化难题提供帮助
人工智能·智慧城市·数据可视化·政务
水如烟20 分钟前
孤能子视角:蓝星文明篇·市——交换机制的运行化:从偶发交换到日常运行的制度化
人工智能
技灵AI25 分钟前
Wan 3.0 API怎么做多参考商品视频?从图片、视频、音频分工到30秒交付
人工智能·prompt·aigc·音视频·wan 3.0
武子康26 分钟前
CLAUDE.md 引用 AGENTS.md 后,两边真的读到同一套规则吗?
人工智能·llm·agent
动恰客流统计37 分钟前
线下零售数字化浪潮下,客流统计的3个核心发展趋势
大数据·前端·人工智能
johnsong43 分钟前
效率的边界:当推理突破遇见语言革命
人工智能·语言模型
染指111044 分钟前
119.Agent-LangChain核心组件-Runtime运行时
人工智能·langchain·agent
DevNo1 小时前
英文会议音视频转中文纪要:我近期的几款工具使用记录
人工智能
别动我齐刘海1 小时前
ROS2 Jazzy + C++ 实战路线——基础学习2
c++·人工智能·vscode·python·学习·机器学习·机器人
江苏久众新视1 小时前
SOP-AI视觉检测实战:从“专人专用”到“产线普适”的工程落地分享
人工智能·视觉检测