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

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

相关推荐
云泽8089 小时前
VSCode入门指南:从认识、安装到语言插件配置全解析
ide·vscode·编辑器
m0_6174939410 小时前
PyCharm 新手避坑指南:一文解决“项目列表消失”与“模块导入报错”两大玄学问题
ide·python·pycharm
爱吃提升11 小时前
VSCode AI编程万能提示词模板(适配Copilot/Codeium/Continue/豆包AI)
vscode·copilot·ai编程
weixin_468466851 天前
从Transformer到ViT与Swin详解
人工智能·深度学习·transformer·computer vision·vit·卷积·swin
信鸽爱好者1 天前
VSCode配置python虚拟环境路径
ide·vscode·python
爱吃提升1 天前
2026最新VSCode 详细下载安装教程+汉化教程(含快捷键与使用趋势)
ide·vscode·编辑器
五VV1 天前
VSCode_Markdown文件中自动公式编号设置的一种方法
ide·vscode·编辑器
tyler_download1 天前
揉扁搓圆transformer架构:反向传播算法详解
深度学习·算法·transformer
love530love2 天前
【硬核排障 & 猴子补丁 & 幽灵节点】SageAttention GQA 崩溃与 Flash Attention 兼容性修复全记录
ide·人工智能·windows·comfyui·sageattention·windows cuda
Together_CZ3 天前
GKDT: General Keypoint Detection Transformer——通用关键点检测 Transformer
人工智能·transformer·通用关键点检测·gkdt·keypoint·detection