MCPcopy Index your code
hub / github.com/Tongjilibo/bert4torch

github.com/Tongjilibo/bert4torch @v0.6.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.6.2 ↗ · + Follow
5,134 symbols 18,934 edges 502 files 1,835 documented · 36%
What it actually does AI analysis from the code graph — generated when you open this
loading…
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 命令行快速部署大模型服务

  • 本地 / 联网加载 ```shell # 联网下载全部文件 bert4torch serve Qwen/Qwen2-0.5B-Instruct

# 加载本地大模型,且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 ``` - 命令行聊天示例 命令行聊天

4. 版本和更新历史

4.1 版本历史

更新日期 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, 支持 gptqawq等主流量化方式,其他代码优化
20250721 0.5.9.post2 0.3.1 增加 Ernie4_5, 修复hub下载bug, 拆分出 openai_client

更多版本

4.2 更新历史

更多历史

5. 预训练权重

5.1 权重加载

```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') ```

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权重

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 🤗

hfl/chinese-macbert-large 🤗|🤗

🤗| ||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|NEZHA

NeZha_Chinese_PyTorch|huawei_noah|sijunhe/nezha-cn-base 🤗

sijunhe/nezha-cn-large 🤗

sijunhe/nezha-base-wwm 🤗

sijunhe/nezha-large-wwm 🤗|🤗

🤗

[🤗](https://huggingfac

Core symbols most depended-on inside this repo

append
called by 1300
examples/others/task_event_extraction_gplinker.py
get
called by 691
bert4torch/processor/image_utils.py
to
called by 353
bert4torch/quantizers/quantizer_cpm_kernels.py
sequence_padding
called by 298
bert4torch/snippets/data_process.py
split
called by 262
bert4torch/tokenizers/tokenization_python.py
encode
called by 215
examples/serving/elasticsearch/src/model.py
build_transformer_model
called by 203
bert4torch/models/auto/modeling_auto.py
to
called by 133
bert4torch/models/minicpmv/image_processing_minicpmv.py

Shape

Method 3,027
Function 1,120
Class 978
Route 9

Languages

Python100%

Modules by API surface

bert4torch/tokenizers/convert_slow_tokenizer.py167 symbols
bert4torch/pipelines/big_modeling_llm.py114 symbols
bert4torch/tokenizers/tokenization_utils_base.py87 symbols
bert4torch/snippets/import_utils.py69 symbols
bert4torch/layers/position_encoding.py63 symbols
bert4torch/layers/attention/attention_layer.py60 symbols
bert4torch/processor/image_utils.py52 symbols
bert4torch/models/base/pretrained_model.py49 symbols
bert4torch/layers/transformer_block.py49 symbols
bert4torch/models/transformer_xl/tokenization_transformer_xl.py47 symbols
bert4torch/pipelines/big_modeling_vlm.py46 symbols
bert4torch/models/paddleocr_vl/visual.py46 symbols

For agents

$ claude mcp add bert4torch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page