Model or dataset
kyegomez/BitNet avatar
kyegomez/BitNet

BitNet in PyTorch: A BitLinear Building Block, Not a Finished 1-bit Model

Implementation of "BitNet: Scaling 1-bit Transformers for Large Language Models" in pytorch

1,945 stars174 forksPythonMIT

At a glance

What is it?
kyegomez/BitNet packages the BitLinear layer and a small transformer from the BitNet paper into an installable PyTorch library. It is a layer and architecture kit, not a checkpoint, and the README is explicit that nothing works without retraining.
Who is it for?
Adopt kyegomez/BitNet if you want a readable PyTorch reference for the BitLinear mechanism and a BitNetTransformer skeleton to train from scratch, and you are comfortable that the README itself flags unfinished code in the 1.5 line. Do not adopt it if you need a downloadable 1-bit checkpoint or a drop-in speedup for an existing model: the README states a model must be finetuned from scratch, and replace_linears_in_hf only swaps modules.
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 2 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 problem BitNet addresses, and who the repository is actually for

The BitNet paper proposes replacing the linear projections inside a transformer with a binarized equivalent, and the repository's README states the idea plainly: "The implementation of the BitNet architecture is quite simple, requiring only the replacement of linear projections (i.e., nn.Linear in PyTorch) in the Transformer." The library exists to make that replacement concrete in PyTorch, so you can read and run the layer rather than reimplement it from the paper.

The audience is narrow. This is for people who want to train a small transformer with binarized weights, or who want to study how absmax quantization and binarization are wired together in code. It is not for someone who wants to download a 1-bit language model and generate text. The README's own NOTICE says a model "obviously needs to be finetuned from scratch to use BitLinear, just changing the linear methods in an already trained model isn't going to work." That single sentence rules out the most common reason people search for a project like this.

The topics list on the repository mentions multimodal and multimodal-deep-learning, and the README claims the transformer "can be utilized not just for text but for images and maybe even video or audio processing." That is a claim about the architecture's shape, not a demonstrated capability. No example in the supplied README trains on images, audio or video.

BitLinear's data flow: layernorm, binarize, absmax, dequant

The README gives the mechanism as a one-line pipeline: "BitLinear = tensor -> layernorm -> Binarize -> abs max quantization -> dequant". Read that as the contract for the module. An input tensor is normalized first, then the weights are pushed to a binary representation, then an absmax scale is applied, and the result is dequantized back to a usable numeric range for the forward pass.

The usage example is minimal by design. You construct a layer with an input and output width, feed a float tensor, and get a float tensor back:

import torch from bitnet import BitLinear

x = torch.randn(10, 1000, 512) layer = BitLinear(512, 400) y = layer(x)

The README does not document the internal signatures of the binarization or quantization steps, does not list which normalization is applied, and does not state whether a straight-through estimator is used for gradients. Those are the details that decide whether training converges, and they are not in the supplied material. If you plan to train on this layer, read the source before you trust the one-line pipeline.

A second variant, BitLinearNew, is shown with a different constructor shape: BitLinearNew(512, 20). The README does not explain how it differs from BitLinear beyond the name and the example. Treat it as a separate code path to inspect, not a drop-in upgrade.

BitNetTransformer, BitMGQA and BitFeedForward as separate pieces

The repository does not ship one monolithic model. It ships components you assemble.

BitNetTransformer takes num_tokens, dim, depth, heads and ff_mult, and the README example feeds it integer token ids of shape (1, 1024) and returns logits. The README describes it as a "fully implemented Transformer as described in the diagram with MHA, and BitFeedforwards" and notes it includes residuals and skip connections. That is the piece to use if you want an end-to-end architecture to train from scratch.

BitMGQA is a bit attention module that swaps BitLinear into the attention projections and uses multi-grouped query attention instead of regular multi-head attention. The README attributes the grouped-query implementation to a contributor named Frank and frames the choice as a decoding-speed and long-context decision. The call signature in the example is gqa(x, x, x, need_weights=True), returning a tuple where the second element carries attention weights.

BitFeedForward is the feed-forward block, described as Linear -> GELU -> Linear, with optional swish activation, post-activation layer norm and dropout exposed as constructor arguments. The README example uses ff = BitFeedForward(512, 512, 4, swish=True, post_act_ln=True, dropout=0.1).

These three modules are the practical surface of the library. If you only need the binarized linear layer, you can ignore the rest. If you want the full architecture, BitNetTransformer is the assembly point, and BitMGQA and BitFeedForward are what it is built from.

Getting it running: install, imports and the inference path

Installation is a single pip command:

pip3 install bitnet

Everything else is imported from the top-level package. The README shows BitLinear, BitLinearNew, BitNetTransformer, BitMGQA, BitFeedForward, BitNetInference and the two replacement helpers as importable names.

The inference path is the least specified part of the README. It shows a BitNetInference object, a load_model call pointing at ../model_checkpoint.pth, and a generate call:

from bitnet import BitNetInference

bitnet = BitNetInference() bitnet.load_model("../model_checkpoint.pth") output_str = bitnet.generate("The dog jumped over the ", 512)

The README does not say where that checkpoint comes from, what architecture it must match, or what tokenizer generate uses. There are no releases listed for the repository, so there is no published checkpoint to point at. If you want to run generate, you have to produce the checkpoint yourself by training a BitNetTransformer, and then confirm the state dict keys line up with whatever BitNetInference expects.

Two replacement helpers exist for retrofitting existing modules. replace_linears_in_hf(model) walks a Hugging Face model and swaps nn.Linear for BitLinear, and replace_linears_in_pytorch_model does the same for a plain torch.nn model. The README's own warning applies to both: swapping the modules does not make a pretrained model work, because the binarized weights were never trained. The Hugging Face example loads bert-base-uncased, calls replace_linears_in_hf, and runs a classification forward pass. It will execute, but the README gives no accuracy expectation for the result.

What the README admits is unfinished, and where the project is the wrong tool

The README contains an unusually direct status note. It describes a BitLinear 1.5 as "still in progress", points at a file, and says "There are still some bugs like with the dequantization algorithm and we still need to replace the multiplication with elementwise addition." Dequantization is not an incidental part of this design. It sits at the end of the pipeline the README itself defines, so a known bug there affects the forward pass of the 1.5 variant.

The same note mentions an in-progress implementation of the follow-up paper on 1.58-bit models. That work is described as ongoing and open to contributors, which means the repository is partly a staging area for unreleased variants alongside the working BitLinear path.

There is a second limitation that is easy to miss. The README's inference example needs a checkpoint that is not distributed with the package, and the repository has no published releases to supply one. So the library gives you trainable components and a generate method, but not a model.

Where this is the wrong tool: any workflow that expects a quantized checkpoint to save memory or latency on day one. The README's NOTICE closes that door. Also, if you are working in a framework other than PyTorch, nothing here transfers directly; the modules are torch.nn.Module subclasses and the helpers are written against PyTorch module trees.

How this differs from calling PyTorch quantization or swapping in a bitsandbytes layer

The obvious alternative for shrinking a model is post-training quantization through PyTorch's own quantization APIs or a runtime library such as bitsandbytes. The difference in approach is fundamental, and the README's NOTICE is the reason.

Post-training quantization takes a model that was trained in full precision and reduces the numeric representation of its weights or activations afterwards. You keep the trained model and accept some accuracy loss. BitLinear does the opposite: the binarization and absmax quantization are part of the forward computation during training, so the model learns under the binarized regime. That is why the README says an already trained model cannot simply have its linears swapped. There is no post-hoc conversion step here.

The practical consequence is a different cost profile. Post-training quantization is a one-off conversion you can apply to a checkpoint you already have. BitLinear is a training-time commitment: you pay for a training run before you get anything, and the README gives no numbers on what that run costs or what quality it reaches. The repository's Appreciation section thanks a contributor for providing a 4080 to train on, which suggests the examples were exercised at small scale, but that is not a benchmark and should not be read as one.

If your goal is to run an existing model on smaller hardware with minimal effort, post-training quantization is the shorter path. If your goal is to study or build a model whose weights are binarized by construction, BitLinear is the mechanism you want.

Maintenance surface, licence and what to check before you depend on it

The repository is MIT licensed, which permits commercial use, modification and redistribution provided the copyright notice and licence text are preserved. That is the usual permissive arrangement, and it is compatible with shipping a derivative inside a closed product. This is a description of the licence identifier, not legal advice; read the LICENSE file in the repository and get your own counsel if the distinction matters to you.

Maintenance cost is the real question, and the README gives a few signals. The library is small and the core BitLinear path is presented as stable, while the 1.5 line and the 1.58-bit follow-up work are explicitly in progress with known bugs. That means the surface you depend on should be pinned. Because installation is pip3 install bitnet, an unpinned requirement can pull a newer package version whose behaviour differs from the source tree you reviewed. Pin the version and, if you are modifying the layer, vendor the module rather than tracking upstream.

The second cost is training. There is no released checkpoint, so every deployment requires you to produce one. Budget for that before you start, not after.

What to verify first, concretely: open the BitLinear 1.5 file the README links to and confirm whether the dequantization bug it mentions is still present; check that the installed package version exposes the class names the README imports, since BitLinearNew and BitLinear are separate code paths; and confirm that your own model's state dict keys match what BitNetInference.load_model expects, because the README's example path is a local file with no documented format. If those three checks pass, the BitLinear path is a reasonable foundation for a from-scratch binarized training run. If any of them fails, you are reading a specification rather than a working dependency.

Editorial conclusion

Adopt kyegomez/BitNet if you want a readable PyTorch reference for the BitLinear mechanism and a BitNetTransformer skeleton to train from scratch, and you are comfortable that the README itself flags unfinished code in the 1.5 line. Do not adopt it if you need a downloadable 1-bit checkpoint or a drop-in speedup for an existing model: the README states a model must be finetuned from scratch, and replace_linears_in_hf only swaps modules. Before committing, verify the current state of the BitLinear 1.5 file named in the README, confirm the pip package version matches the source tree you are reading, and check the MIT licence text in the repository against your own distribution plans.

Official sources

  1. Issues
  2. kyegomez/BitNet on GitHub
  3. License: MIT
  4. Project website
  5. README
Community notes

Community notes