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

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

相关推荐
楚来客11 小时前
AI基础概念之八:Transformer算法通俗解析
人工智能·算法·transformer
雍凉明月夜13 小时前
深度学习网络笔记Ⅳ(Transformer + VIT)
笔记·深度学习·transformer
stevenzqzq16 小时前
ctrl +B和ctrl+shift +B的区别
android·ide·android studio
ASEpochs16 小时前
Vsocde中‘sh’不是内部或外部命令,也不是可运行的程序或批量处理文件--已解决
git·vscode·bash
victory043118 小时前
llama2 MLP 门控FFN
深度学习·transformer
深念Y20 小时前
删除IDEA中的JDK列表项
java·ide·jdk·intellij-idea·idea·sdk
安全渗透Hacker20 小时前
IDEA 中主流 API 插件对比及实操建议
java·ide·intellij-idea
不爱编程爱睡觉21 小时前
vscode下载/常用插件分享及如何链接Ubuntu
ide·vscode·ubuntu
Hcoco_me21 小时前
大模型面试题46:在训练7B LLM时,如果使用AdamW优化器,那么它需要的峰值显存是多少?
开发语言·人工智能·深度学习·transformer·word2vec
雍凉明月夜1 天前
深度学习网络笔记Ⅴ(Transformer源码详解)
笔记·深度学习·transformer