vscode debug Transformer源码说明

  1. 首选确认conda env 环境中未使用 install 安装Transformer;
  2. 下载源码:
bash 复制代码
git clone https://github.com/huggingface/transformers.git
cd transformers

# pip
pip install '.[torch]'

安装后通过pip list 可以看到是指向你的本地目录:

  1. 自测demo,放在根目录即可
python 复制代码
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-0.6B"

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    trust_remote_code=True,
    dtype="auto",
    device_map="auto"
)

# prepare the model input
prompt = "Give me a short introduction to large language model."
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

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

content = tokenizer.decode(output_ids, skip_special_tokens=True)

print("content:", content)
  1. vscode debug:按照以往代码debug即可;
  • 安装插件

  • vscode 右下角选择你的env,如下

  • 点击上方的三个点-》运行-》启动调试

相关推荐
不爱吃鱼的蔓热莓18 小时前
释放VSCode插件占用系统盘垃圾
ide·vscode·编辑器
2501_9151063221 小时前
iOS开发之Xcode常用调试技巧总结
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
bing_feilong1 天前
vscode: ssh Got bad result from install script
ide·vscode·ssh
kyrie_sakura1 天前
大模型学习15 - npl自然语言处理16 - Transformer模型API(PyTorch)
学习·自然语言处理·transformer
JavaEdge.1 天前
Qodo v.s AI IDE Code Review
ide·人工智能·代码复审
老唐7771 天前
(附代码)使用OpenCV和关键点检测计算瓶子角度
人工智能·深度学习·opencv·目标检测·计算机视觉·ai·transformer
ITKEY_1 天前
Arduino IDE开发ESP32环境搭建到helloworld
ide·esp32
沫璃染墨1 天前
从零入门计算机网络系列(五):UDP套接字编程实战——从核心API到客户端与服务器通信
linux·网络·c++·vscode·网络协议·udp
captain3761 天前
Spring Web MVC入门(1)
ide·spring·java-ee·tomcat
高洁011 天前
世界模型环境预测与内在表征基础原理
人工智能·深度学习·机器学习·transformer·tornado