运行调试大语言模型

很多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)
相关推荐
罗西的思考2 分钟前
【轻量化复刻大模型】线性注意力交互式演示
人工智能·算法·机器学习
美狐美颜sdk15 分钟前
视频美颜SDK中的YUV与RGBA格式有什么区别?直播APP开发解析
人工智能·计算机视觉·音视频·美颜sdk·直播美颜sdk·视频美颜sdk
jimmyleeee27 分钟前
Agent安全之七:当智能体之间开始“互相信任”------深入理解 OWASP ASI07 不安全的智能体间通信
人工智能·安全
二哈赛车手27 分钟前
新人笔记---hermes定时功能设计,具体讲解以及可优化点
java·人工智能·笔记·spring·ai·typescript
Fluxproxy28 分钟前
大模型批量数据采集:AI 自动化工作流搭建实战
大数据·人工智能·自动化
AAwangzong31 分钟前
AI时代RPA技术解析:如何构建兼顾流程执行与智能决策的自动化平台?
人工智能·经验分享·rpa·智能体
达达表哥38 分钟前
AI综合站商业分发系统v3.1纯净版发布 附完整安装教程(排版修正版)
人工智能
众壹新能源科技1 小时前
逆变器直流开关柜温度监测与预防:红外巡检发现3个120度热点
运维·人工智能·自动化
NeoGressAI外贸数字化1 小时前
外贸独立站robots.txt怎么写:6个AI爬虫放行规则
人工智能·爬虫