当前位置: 首页 > news >正文

本地部署qwen-0.6b

导入需要的库

打开pycharm的终端(是终端不是python程序),下载下面的库
image

pip install torch transformers datasets peft accelerate sentencepiece modelscope 
pip install modelscope
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu126     

这里的第二行下载torch,如果你的独显cuda不匹配torch可以去官网查看你的显存是那个cuda版本适配哪个pytorch,如果没有独显可能不用在意这个问题

下载模型

建一个python程序,复制下面代码,下载模型

from modelscope.hub.snapshot_download import snapshot_download  # 自定义下载路径(可以是任意你有读写权限的目录)  
model_dir = snapshot_download(  'Qwen/Qwen3-0.6B',    revision='master',          cache_dir='./models'         # ← 自定义路径!  
)  print("模型保存路径:", model_dir)  #记住这里的路径,后面路径要用到

记住下载模型的路径

运行语句

再新建一个py文件,记得修改model_path为你电脑上模型的路径

from modelscope import AutoModelForCausalLM, AutoTokenizer  
import torch  #model_name = "Qwen/Qwen3-0.6B"  
model_path = "./models/qwen/Qwen3-0___6B"  # ← 修改成你电脑上的实际路径!  # 1. 加载 tokenizer 和 模型  
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)  
model = AutoModelForCausalLM.from_pretrained(  model_path,  device_map="auto",                    # 自动分配 GPU/CPU    dtype=torch.bfloat16,                 # 减少显存占用  trust_remote_code=True  
)  # prepare the model input  
prompt = "你好,请介绍一下你自己"  
messages = [  {"role": "user", "content": prompt}  
]  
text = tokenizer.apply_chat_template(  messages,  tokenize=False,  add_generation_prompt=True,  enable_thinking=False # 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)

看到代码有输出就说明部署成功
image

![[Pasted image 20250923210828.png]]

http://www.zskr.cn/news/10520.html

相关文章:

  • 25分钟小练习
  • markdown 使用指南
  • [视图功能8] 图表视图:柱状图、折线图与饼图配备实战
  • 近十年 CSP-J 复赛知识点分布表
  • 软件工程:构建数字世界的基石
  • Avalonia 学习笔记07. Control Themes(控件主题)
  • matter 协议的架构;
  • 相机标定(Camera Calibration)原理及步骤:从 “像素模糊” 到 “毫米精准” 的关键一步 - 实践
  • nRF54LM20A USB
  • 《CBI 技术有聊》对话 OpenCSG:智能体落地困境与企业转型的必然路径
  • 2025/9/23
  • Tita:更频繁的绩效考核周期的好处
  • 完整教程:DCS+PLC协同优化:基于MQTT的分布式控制系统能效提升案例
  • 详细介绍:【Linux】Linux文件系统详解:从磁盘到文件的奥秘
  • CCPC秦皇岛 2023 M Inverted
  • Hetao P10588 十载峥嵘桀骜 题解 [ 紫 ] [ 树的直径 ] [ 矩阵加速 DP ] [ 状态设计优化 ]
  • Julia 实现基于模板匹配的验证码识别方法
  • 第9节-子查询-ALL - 详解
  • 谈谈对软件工程的理解
  • [PaperReading] MemGPT: Towards LLMs as Operating Systems
  • NLP:驱动人工智能迈向 “理解” 与 “对话” 的核心引擎 - 教程
  • 实用指南:网站抓包怎么做?(网站抓包教程 HTTPS 抓包 浏览器抓包 服务器端流量分析 网站安全与调试)
  • 学习嵌入式的第三十二天——网络编程——TCP - 实践
  • HarmonyOS动态照片,简易环境助力高效开发
  • IT项目管理主要做什么?-ManageEngine卓豪
  • 9.22学习笔记
  • 实用指南:详解RabbitMQ高级特性之延迟插件的安装和使用
  • Django 视图层
  • springboot~获取原注解的方法findMergedAnnotation使用场景
  • Catalan数(卡特兰数)