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

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

相关推荐
匆匆那年9671 小时前
VSCode 远程 Linux 使用Codex
linux·ide·vscode
渣渣灰95874 小时前
Windows 10 环境部署 Claude Code 教程
windows·vscode·calude code
nap-joker6 小时前
GFE-MAMBA:基于MAMBA的MCI生成特征提取的AD多模态进展评估
transformer·mamba·影像数据·多模态数据·阿茨海默症·mci和ad·像素级交叉注意力机制
lili00129 小时前
CC GUI 插件架构剖析:如何为 JetBrains IDE 打造完整的 AI 编程工作台
java·ide·人工智能·python·架构·ai编程
慕言手记11 小时前
IDEA 插件常用-2026版
java·ide·spring boot·intellij-idea·idea·intellij idea
我命由我1234511 小时前
Visual Studio - Visual Studio 注释快捷键
java·c语言·开发语言·c++·ide·java-ee·visual studio
码农小旋风11 小时前
Codex 直接住进 JetBrains IDE 里:AI Agent 正在接管熟悉的开发入口
ide·人工智能
AI技术控12 小时前
Transformer 的 Encoder 和 Decoder 模块介绍:从结构原理到大模型应用实践
人工智能·python·深度学习·自然语言处理·transformer
ʜᴇɴʀʏ13 小时前
TPAMI 2026 | Semi-DETR++:基于检测 Transformer 的高效半监督目标检测
深度学习·目标检测·transformer