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

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

相关推荐
炘东5925 小时前
让Pycharm的Terminal(终端)进入创建好的虚拟环境
ide·python·pycharm
大千AI助手6 小时前
XLM-R模型:大规模跨语言表示的突破与实践
语言模型·nlp·transformer·预训练模型·mlm·xlm-r·掩码模型
ZJU_统一阿萨姆6 小时前
Windows系统VSCode配置Rust开发环境(超详细保姆级教程)
windows·vscode·rust
絔宝6 小时前
Eclipse控制台乱码解决方式
java·ide·eclipse
GOATLong7 小时前
MySQL内置函数
android·数据库·c++·vscode·mysql
傅科摆 _ py7 小时前
Vscode 如何配置远程环境的 ssh 连接
ide·vscode·编辑器·1024程序员节
zhangfeng11337 小时前
在 R 医学数据分析中,关于 RStudio 和 VSCode 哪个效率更高
vscode·数据分析·r语言
HIT_Weston8 小时前
14、【Ubuntu】【VSCode】VSCode 断联问题分析:hostname(二)
linux·vscode·ubuntu
Cosmoshhhyyy9 小时前
Jupyter Notebook运行Milvus Lite
ide·jupyter·milvus