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

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

相关推荐
BD_Marathon1 小时前
【JavaWeb】VsCode中快速生成HTML5模板
ide·vscode·html5
云雾J视界2 小时前
深入浅出卷积神经网络(CNN):从LeNet到Vision Transformer的演进及其实战应用
面试·cnn·resnet·transformer·核心竞争力·认知升级·技术细节
海阔的天空3 小时前
VSCode通过continue插件免费安装AI模型实现自动编程
运维·ide·人工智能·vscode·编辑器·ai编程
渡我白衣5 小时前
并行的野心与现实——彻底拆解 C++ 标准并行算法(<execution>)的模型、陷阱与性能真相
java·开发语言·网络·c++·人工智能·windows·vscode
MounRiver_Studio6 小时前
RISC-V IDE MRS2使用笔记(四):编译后静态堆栈调用分析
ide·mcu·嵌入式·risc-v
懂AI的老郑6 小时前
Transformer架构在大语言模型中的优化技术:原理、方法与前沿
语言模型·架构·transformer
唐古乌梁海7 小时前
【PyCharm】PyCharm 常用技巧与快捷键总结
ide·python·pycharm
小股虫7 小时前
idea编译内存溢出 java: java.lang.OutOfMemoryError: WrappedJavaFileObject[ 解决方案
java·ide·intellij-idea·idea
食品一少年8 小时前
【Day4-6】开源鸿蒙IDE DevEco Studio插件集成
ide
洁洁!9 小时前
openEuler开发工具链与IDE支持性能深度测试
ide