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,如下

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

相关推荐
EW Frontier13 小时前
拆解|多尺度复数卷积,把调制识别准确率又往上推了 3 个点【文末附代码链接】
transformer·调制识别·无人机信号识别·辐射源识别
LaughingZhu17 小时前
Product Hunt 每日热榜 | 2026-09-02
java·ide·intellij-idea
进击的蜗牛Eric17 小时前
VSCode Server手动安装指南(简洁版)
vscode
小芒果_0119 小时前
从0到1,将pycharm上的项目上传到github
ide·pycharm·github
克里斯蒂亚诺更新21 小时前
介绍CNN RNN Transformer(新王)
rnn·cnn·transformer
猫先生Mr.Mao1 天前
大模型之Attention Is All You Need详解:Transformer如何用注意力重写序列建模
深度学习·大模型·transformer·注意力机制·论文解读
Ado柳贯一1 天前
Transformer模型详解-CSDN发布版
人工智能·深度学习·transformer
xx~t2 天前
嵌入式——Linux软件编程——网络1
linux·c语言·网络·vscode·网络协议
sunburn-2 天前
Java堆(Heap)详解与实战教学
java·开发语言·数据结构·ide·算法
逐步前行2 天前
ESP32-S3-VSCode扩展ESP-IDF
ide·vscode·编辑器