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

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

相关推荐
范桂飓12 分钟前
Transformer 大模型架构深度解析(5)GPT 与 LLM 大语言模型技术解析
人工智能·gpt·语言模型·transformer
Clarice__2 小时前
VScode上的python使用教程
vscode·python·conda
shangjian0072 小时前
AI-大语言模型LLM-Transformer架构6-输出层
人工智能·语言模型·transformer
少年强则国强2 小时前
anaconda安装配置pycharm
ide·python·pycharm
机器学习之心3 小时前
TCN-Transformer-GRU组合模型回归+SHAP分析+新数据预测+多输出!深度学习可解释分析MATLAB代码
深度学习·gru·transformer
shangjian0073 小时前
AI-大语言模型LLM-Transformer架构5-残差连接与前馈网络
人工智能·语言模型·transformer
hbstream海之滨视频网络技术3 小时前
国内AI编程IDE对比(二):从零构建桌面应用实测
ide·ai编程
shangjian00720 小时前
AI-大语言模型LLM-Transformer架构3-嵌入和位置编码
人工智能·语言模型·transformer
你知道“铁甲小宝”吗丶21 小时前
VSCode使用Claude Code
vscode·ai·ai编程
浪潮IT馆1 天前
在 VSCode 中调试 JavaScript 的 Jest 测试用例
javascript·ide·vscode