Model or dataset
Tongjilibo/bert4torch avatar
Tongjilibo/bert4torch

bert4torch: A Keras-Style Training Loop Over PyTorch Transformer Weights

An elegent pytorch implement of transformers

1,326 stars166 forksPythonMIT

At a glance

What is it?
bert4torch wraps PyTorch transformer weights in a fit/predict interface modelled on bert4keras, and ships a one-line command for serving LLMs. It is maintained by one person, and the README says so.
Who is it for?
Adopt bert4torch if you want a Keras-style fit loop over PyTorch transformer weights and you value readable, editable model code over ecosystem breadth. Do not adopt it if you need the Hugging Face model hub, the Trainer callbacks ecosystem, or a project with more than one maintainer, since the README states the repository is currently maintained by an individual.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 123 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 15, 2026, and from our analysis. They are not legal advice.

DEEP OPEN-SOURCE ANALYSIS

The Gap bert4torch Fills Between Keras Familiarity and PyTorch Weights

Most PyTorch transformer code arrives as a model class plus a training script you have to assemble yourself. bert4torch takes the opposite route: it presents pretrained transformer weights behind a training interface borrowed from Keras, the style popularised by bert4keras. The README describes the code as having a keras training style and high code reuse. The target reader is someone who already thinks in terms of model.compile and model.fit, or who has used bert4keras and wants to stay in PyTorch. The feature table in the README lists code that is concise and easy to understand with large room for customisation as a differentiator against transformers, alongside a library of training tricks that the table marks as absent in transformers. It also lists, in the same table, repository maintenance capacity, influence, usage and compatibility as a column where transformers wins. That self-assessment is unusually direct and sets the tone for the whole project.

What Actually Gets Loaded: BERT-Family Checkpoints and Open LLM Weights

The README lists two weight families. The core set covers bert, roberta, albert, xlnet, nezha, bart, RoFormer, RoFormer_V2, ELECTRA, GPT, GPT2, T5, GAU-alpha and ERNIE, all loadable for continued finetuning, with support for defining your own model on top of a bert base. The second set covers LLM weights: chatglm, llama, baichuan, ziya and bloom are named in the feature list, and the release notes add Qwen3-moe, Ernie4_5, qwen3_vl, deepseek ocr and glm_ocr across the 0.5.9 through 0.6.2 releases. Release 0.6.0 also lists support for gptq and awq quantisation formats. The practical consequence is that a single training loop spans a 2019-era BERT classifier and a quantised modern decoder. Whether every listed model runs at the same level of polish is not something the README establishes, and the release notes show new architectures arriving roughly one per release, which suggests breadth is being added faster than any single architecture is being deepened.

How the Training Loop and Serving Command Are Structured

Two mechanisms are visible in the material. The first is the fit process. The README describes a customisable fit procedure for advanced needs, a training progress bar that prints loss and user-defined metrics, default Logger and Tensorboard recording, distributed training through torch's own dp and ddp, and callbacks for logging, tensorboard, early stopping and wandb. The second is serving. The bert4torch serve command takes a model path or a hub identifier and a --mode flag with cli, gradio and openai values. The README shows bert4torch serve Qwen/Qwen2-0.5B-Instruct for a networked download, a local directory path for an offline load, and the three mode variants for terminal chat, a Gradio page, or an OpenAI-compatible API. A note on the local path example says bert4torch_config.json must already be downloaded into the same directory. That config file is the seam between the generic loader and each architecture's specifics, and release 0.6.2 states it removed hardcoded model config entries, which is the kind of change that tends to break local directories set up against earlier versions.

Installing It and the First Commands You Run

The install is two lines from the README. For the stable release, pip install bert4torch. For the development version, pip install git+https://github.com/Tongjilibo/bert4torch. The README carries an explicit warning attached to that second option: the pip package is released later than the development version on git, so a git clone means watching the reference paths and checking whether weights need conversion. The development environment note says the project was originally developed against torch==1.10 and has switched to torch2.0, with a request to report incompatibilities on other versions. To run the examples, the README instructs cloning the repository and editing the pretrained model file path and data path in the example scripts. For serving, the two forms are bert4torch serve Qwen/Qwen2-0.5B-Instruct and bert4torch serve /data/pretrain_ckpt/Qwen/Qwen2-0.5B-Instruct, with --mode cli, --mode gradio or --mode openai appended. There is no mention of a requirements file pinning torch, so version compatibility is something you resolve yourself.

The Single-Maintainer Column and What It Means for a Production Dependency

The README's own comparison table puts a cross in the row for repository maintenance capacity, influence, usage and compatibility, with the note that the repository is currently maintained by an individual. That is the clearest limitation in the material and it is stated by the project rather than inferred. The consequences are concrete. Release cadence in the supplied history is roughly one minor version every four to eight months, with 0.6.0 in September 2025, 0.6.1 in January 2026 and 0.6.2 in May 2026. Each of those releases adds new architectures, which is a lot of surface area for one person to hold. The 0.6.2 change that removes the transformers dependency and introduces AutoTokenizer and AutoProcessor is a good example of a release that alters the dependency graph and the config surface at once. If you pin bert4torch in a product, pin the version and the torch version together, and read docs/Update.md for the release you are moving to rather than assuming the loader behaviour is unchanged.

Where transformers Is the Better Choice, and Where It Is Not

The obvious alternative is Hugging Face transformers, and the README compares the two directly. Both are marked as supporting progress bars, dp/ddp distributed training, callbacks, streamed and batched LLM inference, and LoRA finetuning (the README notes LoRA depends on the peft library in the transformers case and is built in for bert4torch). The difference in approach is not feature coverage but code shape. transformers gives you a model class and a Trainer abstraction with a large configuration surface and a deep ecosystem of hub-hosted checkpoints and third-party integrations. bert4torch gives you a Keras-style loop where, per the README, the code is concise, highly reused and leaves substantial room for customisation, plus training tricks such as adversarial training that the table marks as absent in transformers. If your work is a custom training procedure over a known checkpoint, the bert4torch shape is less fighting. If your work depends on the hub, on community model variants, or on the maintenance guarantees of a large organisation, transformers is the safer base and the README concedes that column.

Maintenance Cost, Licence and What the MIT Terms Do Not Cover

bert4torch is MIT licensed, which permits commercial use, modification and redistribution provided the copyright notice and permission notice are retained. That covers the library code. It does not cover the pretrained weights you load through it. The README links to a weight list rather than embedding licence terms for chatglm, llama, baichuan, Qwen, ERNIE or the rest, and those checkpoints carry their own terms, several of which restrict commercial deployment or require separate agreement. This is not a legal opinion and you should read the specific model card for whichever checkpoint you ship. On upgrade cost, the practical burden sits in two places: the torch version you build against, since the project moved from 1.10 to 2.0, and the bert4torch_config.json files in your local model directories, since 0.6.2 removed hardcoded config entries and changed tokenizer handling. Budget for re-validating a local checkpoint directory on each minor upgrade rather than assuming a drop-in replacement.

Editorial conclusion

Adopt bert4torch if you want a Keras-style fit loop over PyTorch transformer weights and you value readable, editable model code over ecosystem breadth. Do not adopt it if you need the Hugging Face model hub, the Trainer callbacks ecosystem, or a project with more than one maintainer, since the README states the repository is currently maintained by an individual. Before committing, verify three things: that the pip release you install matches the git tag you intend to run, since the README notes pip lags the development version on git; that your torch version is compatible, since the project moved from torch==1.10 to torch2.0 for development; and that the specific pretrained weights you need load without conversion, because the README warns that some checkpoints require it.

Official sources

  1. License: MIT
  2. Project website
  3. README
  4. Releases
  5. Tongjilibo/bert4torch on GitHub
Community notes

Community notes