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

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

相关推荐
无糖冰可乐2117 分钟前
IDEA多java版本切换
java·ide·intellij-idea
brucelee18624 分钟前
IntelliJ IDEA 设置 Local History 永久保留
java·ide·intellij-idea
buvsvdp50059ac5 小时前
如何在VSCode中设置Python解释器?
ide·vscode·python
Naiva7 小时前
【杂记】Microchip 的通用集成开发环境工具对照表(MPLAB X IDE)和芯片家族对标表(Microchip VS ST)
ide
天天向上10247 小时前
VueUse的使用
前端·vue.js·vscode
bemyrunningdog9 小时前
IntelliJ IDEA导出WAR包全指南
java·ide·intellij-idea
carver w9 小时前
transformer 手写数字识别
人工智能·深度学习·transformer
锻炼²12 小时前
(已解决)vscode打开stm32cubemx生成的工程报红色波浪线警告
ide·vscode·stm32·stm32cubemx·vscode打开keil工程
yanxiaoyu11013 小时前
Pycharm远程调用Autodl进行训练(关机后不影响)
ide·python·pycharm
怪兽201415 小时前
PyCharm如何像其他idea软件跨行选择文本
ide·pycharm·intellij-idea