SkillAgentSearch skills...

Bert4torch

An elegent pytorch implement of transformers

Install / Use

/learn @Tongjilibo/Bert4torch

README

bert4torch

licence GitHub release PyPI PyPI - Downloads GitHub stars GitHub Issues contributions welcome Generic badge

Documentation | Torch4keras | Examples | build_MiniLLM_from_scratch | bert4vector

目录

1. 下载安装

安装稳定版

pip install bert4torch

安装最新版

pip install git+https://github.com/Tongjilibo/bert4torch
  • 注意事项:pip包的发布慢于git上的开发版本,git clone注意引用路径,注意权重是否需要转换
  • 测试用例git clone https://github.com/Tongjilibo/bert4torch,修改example中的预训练模型文件路径和数据路径即可启动脚本
  • 自行训练:针对自己的数据,修改相应的数据处理代码块
  • 开发环境:原使用 torch==1.10版本进行开发,现已切换到 torch2.0开发,如其他版本遇到不适配,欢迎反馈

2. 功能

  • LLM模型: 加载chatglm、llama、 baichuan、ziya、bloom等开源大模型权重进行推理和微调,命令行一行部署大模型

  • 核心功能:加载bert、roberta、albert、xlnet、nezha、bart、RoFormer、RoFormer_V2、ELECTRA、GPT、GPT2、T5、GAU-alpha、ERNIE等预训练权重继续进行finetune、并支持在bert基础上灵活定义自己模型

  • 丰富示例:包含llmpretrainsentence_classficationsentence_embeddingsequence_labelingrelation_extractionseq2seqserving等多种解决方案

  • 实验验证:已在公开数据集实验验证,使用如下examples数据集实验指标

  • 易用trick:集成了常见的trick,即插即用

  • 其他特性加载transformers库模型一起使用;调用方式简洁高效;有训练进度条动态展示;配合torchinfo打印参数量;默认Logger和Tensorboard简便记录训练过程;自定义fit过程,满足高阶需求

  • 训练过程

    训练过程

| 功能 | bert4torch | transformers | 备注 | | ----------------------------------- | ---------- | ------------ | ---------------------------------- | | 训练进度条 | ✅ | ✅ | 进度条打印loss和定义的metrics | | 分布式训练dp/ddp | ✅ | ✅ | torch自带dp/ddp | | 各类callbacks | ✅ | ✅ | 日志/tensorboard/earlystop/wandb等 | | 大模型推理,stream/batch输出 | ✅ | ✅ | 各个模型是通用的,无需单独维护脚本 | | 大模型微调 | ✅ | ✅ | lora依赖peft库,pv2自带 | | 丰富tricks | ✅ | ❌ | 对抗训练等tricks即插即用 | | 代码简洁易懂,自定义空间大 | ✅ | ❌ | 代码复用度高, keras代码训练风格 | | 仓库的维护能力/影响力/使用量/兼容性 | ❌ | ✅ | 目前仓库个人维护 | | 一键部署大模型 | | | |

3. 快速上手

3.1 上手教程

3.2 命令行快速部署大模型服务

  • 本地 / 联网加载
    # 联网下载全部文件
    bert4torch serve --checkpoint_path Qwen2-0.5B-Instruct
    
    # 加载本地大模型,联网下载bert4torch_config.json
    bert4torch serve --checkpoint_path /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --config_path Qwen/Qwen2-0.5B-Instruct
    
    # 加载本地大模型,且bert4torch_config.json已经下载并放于同名目录下
    bert4torch serve --checkpoint_path /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct
    
  • 命令行 / gradio网页 / openai_api
    # 命令行
    bert4torch serve --checkpoint_path /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --mode cli
    
    # gradio网页
    bert4torch serve --checkpoint_path /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --mode gradio
    
    # openai_api
    bert4torch serve --checkpoint_path /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --mode openai
    
  • 命令行聊天示例 命令行聊天

4. 版本和更新历史

4.1 版本历史

| 更新日期 | bert4torch | torch4keras | 版本说明 | | -------- | ----------- | ----------- | ---------------------------------------------------------------------- | | 20260114 | 0.6.1 | 0.3.3 | 增加paddleocr-vl,优化代码结构,去除硬代码模型配置项 | | 20250925 | 0.6.0 | 0.3.2 | 增加 Qwen3-moe, 支持 gptqawq等主流量化方式,其他代码优化 | | 20250721 | 0.5.9.post2 | 0.3.1 | 增加 Ernie4_5, 修复hub下载bug, 拆分出 openai_client |

更多版本

4.2 更新历史

更多历史

5. 预训练权重

5.1 权重加载

from bert4torch.models import build_transformer_model

# 1. 仅指定config_path: 从头初始化模型结构, 不加载预训练模型
model = build_transformer_model('./model/bert4torch_config.json')

# 2. 仅指定checkpoint_path: 
## 2.1 文件夹路径: 自动寻找路径下的*.bin/*.safetensors权重文件 + 需把bert4torch_config.json下载并放于该目录下
model = build_transformer_model(checkpoint_path='./model')

## 2.2 文件路径/列表: 文件路径即权重路径/列表, bert4torch_config.json会从同级目录下寻找
model = build_transformer_model(checkpoint_path='./pytorch_model.bin')

## 2.3 model_name: hf上预训练权重名称, 会自动下载hf权重以及bert4torch_config.json文件
model = build_transformer_model(checkpoint_path='google-bert/bert-base-chinese')

# 3. 同时指定config_path和checkpoint_path(本地路径名或model_name排列组合): 
#    本地路径从本地加载,pretrained_model_name会联网下载
config_path = './model/bert4torch_config.json'  # 或'google-bert/bert-base-chinese'
checkpoint_path = './model/pytorch_model.bin'  # 或'google-bert/bert-base-chinese'
model = build_transformer_model(config_path, checkpoint_path)

5.2 权重链接

|模型分类|模型名称|权重来源|checkpoint_path|config_path| |------------|------------|------------|------------|------------| |bert|bert-base-chinese|google-bert|google-bert/bert-base-chinese 🤗|🤗| ||chinese_L-12_H-768_A-12|谷歌|tf权重<br/>Tongjilibo/bert-chinese_L-12_H-768_A-12 🤗|| ||chinese-bert-wwm-ext|HFL|hfl/chinese-bert-wwm-ext 🤗|🤗| ||bert-base-multilingual-cased|google-bert|google-bert/bert-base-multilingual-cased 🤗|🤗| ||bert-base-cased|google-bert|google-bert/bert-base-cased 🤗|🤗| ||bert-base-uncased|google-bert|google-bert/bert-base-uncased 🤗|🤗| ||MacBERT|HFL|hfl/chinese-macbert-base 🤗<br/>hfl/chinese-macbert-large [🤗](https://huggingface.co/hfl/ch

Related Skills

View on GitHub
GitHub Stars1.3k
CategoryDevelopment
Updated8d ago
Forks168

Languages

Python

Security Score

100/100

Audited on Mar 29, 2026

No findings