
Documentation | Torch4keras | Examples | build_MiniLLM_from_scratch | bert4vector
安装稳定版
pip install bert4torch
安装最新版
pip install git+https://github.com/Tongjilibo/bert4torch
git clone https://github.com/Tongjilibo/bert4torch,修改example中的预训练模型文件路径和数据路径即可启动脚本torch==1.10版本进行开发,现已切换到 torch2.0开发,如其他版本遇到不适配,欢迎反馈
| 功能 | bert4torch | transformers | 备注 |
|---|---|---|---|
| 训练进度条 | ✅ | ✅ | 进度条打印loss和定义的metrics |
| 分布式训练dp/ddp | ✅ | ✅ | torch自带dp/ddp |
| 各类callbacks | ✅ | ✅ | 日志/tensorboard/earlystop/wandb等 |
| 大模型推理,stream/batch输出 | ✅ | ✅ | 各个模型是通用的,无需单独维护脚本 |
| 大模型微调 | ✅ | ✅ | lora依赖peft库,pv2自带 |
| 丰富tricks | ✅ | ❌ | 对抗训练等tricks即插即用 |
| 代码简洁易懂,自定义空间大 | ✅ | ❌ | 代码复用度高, keras代码训练风格 |
| 仓库的维护能力/影响力/使用量/兼容性 | ❌ | ✅ | 目前仓库个人维护 |
| 一键部署大模型 |
# 加载本地大模型,且bert4torch_config.json已经下载并放于同名目录下
bert4torch serve /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct
- 命令行 / gradio网页 / openai_apishell
# 命令行
bert4torch serve /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --mode cli
# gradio网页 bert4torch serve /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --mode gradio
# openai_api
bert4torch serve /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct --mode openai
```
- 命令行聊天示例

| 更新日期 | bert4torch | torch4keras | 版本说明 |
|---|---|---|---|
| 20260513 | 0.6.2 | 0.3.4 | 增加qwen3_vl、deepseek ocr、glm_ocr; 去除对transformers依赖,增加AutoTokenizer, AutoProcessor |
| 20260114 | 0.6.1 | 0.3.3 | 增加paddleocr-vl,优化代码结构,去除硬代码模型配置项 |
| 20250925 | 0.6.0 | 0.3.2 | 增加 Qwen3-moe, 支持 gptq、awq等主流量化方式,其他代码优化 |
| 20250721 | 0.5.9.post2 | 0.3.1 | 增加 Ernie4_5, 修复hub下载bug, 拆分出 openai_client |
```python from bert4torch.models import build_transformer_model
# 1. 仅指定pretrained_model_name_or_path: # 1.1 model_name: hf上预训练权重名称, 会自动下载hf权重以及bert4torch_config.json文件 model = build_transformer_model('google-bert/bert-base-chinese')
# 1.2 本地文件夹路径: 自动寻找路径下的.bin/.safetensors权重文件 + bert4torch_config.json文件,需提前下载到本地 model = build_transformer_model('/data/pretrained_models/google-bert/bert-base-chinese')
# 2. 同时指定config_path和checkpoint_path,和1效果一样 config_path = './model/bert4torch_config.json' checkpoint_path = './model/pytorch_model.bin' model = build_transformer_model(config_path=config_path, checkpoint_path=checkpoint_path)
# 3. 仅指定config_path: 从头初始化模型结构, 不加载预训练模型 model = build_transformer_model(config_path='./model/bert4torch_config.json') ```
| 模型分类 | 模型名称 | 权重来源 | checkpoint_path | config_path |
|---|---|---|---|---|
| bert | bert-base-chinese | google-bert | google-bert/bert-base-chinese 🤗 |
🤗 |
| chinese_L-12_H-768_A-12 | 谷歌 | tf权重 |
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 🤗
🤗|
||WoBERT|追一科技|junnyu/wobert_chinese_base 🤗
junnyu/wobert_chinese_plus_base 🤗|🤗
🤗|
|roberta|chinese-roberta-wwm-ext|HFL|hfl/chinese-roberta-wwm-ext 🤗
hfl/chinese-roberta-wwm-ext-large 🤗
(large的mlm权重是随机初始化)|🤗
🤗|
||roberta-small/tiny|追一科技|Tongjilibo/chinese_roberta_L-4_H-312_A-12 🤗
Tongjilibo/chinese_roberta_L-6_H-384_A-12 🤗||
||roberta-base|FacebookAI|FacebookAI/roberta-base 🤗|🤗|
||guwenbert|ethanyt|ethanyt/guwenbert-base 🤗|🤗|
|albert|albert_zh
albert_pytorch|brightmart|voidful/albert_chinese_tiny 🤗
voidful/albert_chinese_small 🤗
voidful/albert_chinese_base 🤗
voidful/albert_chinese_large 🤗
voidful/albert_chinese_xlarge 🤗
voidful/albert_chinese_xxlarge 🤗|🤗
NeZha_Chinese_PyTorch|huawei_noah|sijunhe/nezha-cn-base 🤗
sijunhe/nezha-cn-large 🤗
sijunhe/nezha-base-wwm 🤗
[🤗](https://huggingfac
$ claude mcp add bert4torch \
-- python -m otcore.mcp_server <graph>