Skip to content

BitEmbedding: the bit-bias embedding as a plain module - #14

Merged
AmitMY merged 1 commit into
mainfrom
bit-embedding
Oct 10, 2026
Merged

AmitMY merged 1 commit into
mainfrom
bit-embedding

Conversation

@AmitMY

@AmitMY AmitMY commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

BitEmbedding: the bit-bias embedding as a plain module

PatchedBitEmbeddings puts the bit-bias (a byte table plus a zero-initialized projection of each byte's 8 bits) behind a tieable façade weight, refreshed by hooks, to patch HF models' embeddings.

WeLT on Megatron-Bridge owns its byte embeddings (no HF tying) and runs under torch.compile, so it uses the same math as a plain module. BitEmbedding moves that module here, so WeLT can import it instead of keeping its own copy (sign/WeLT ByteEmbedding):

  • BitEmbedding(weight): the table, initialized from weight, plus bit_proj.
  • folded_weight(): the equivalent plain table, e.g. to export to inference engines.

Tests

A new test checks that the embedding starts as its table, that both parameters get gradients, and that the bits are folded into the table correctly. All 14 byte-embedding tests pass, and ruff is clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_015Xm62qoeaTunFSU5GYCQ1f


Note

Low Risk
Additive API and tests only; existing patched HF embedding path is untouched.

Overview
Adds BitEmbedding, a hook-free variant of the byte bit-bias embedding used by PatchedBitEmbeddings, so stacks like Megatron that own embeddings and use torch.compile can use the same math without the tieable façade and refresh hooks.

The module keeps a 256×D table plus a zero-initialized D×8 bit_proj, folds bits into the table via folded_weight(), and looks up with F.embedding. PatchedBitEmbeddings and HF patching are unchanged.

A new unit test checks identity with the initial table, gradients on both parameters, and correct folding when bit_proj is non-zero.

Reviewed by Cursor Bugbot for commit e5377cd. Bugbot is set up for automated code reviews on this repo. Configure here.

PatchedBitEmbeddings wraps a HF model's embeddings behind a tieable façade weight, refreshed by hooks. Models that
own their embeddings (WeLT on Megatron) and torch.compile need the same math as a plain module: a table plus a
zero-initialized projection of each byte's bits, with folded_weight() as the equivalent table for export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Xm62qoeaTunFSU5GYCQ1f

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit e5377cd. Configure here.

assert weight.shape[0] == 256, "Expected a byte-level embedding table (256 rows)."
self.weight = nn.Parameter(weight.detach().clone())
self.bit_proj = nn.Parameter(torch.zeros(weight.shape[1], 8, dtype=weight.dtype))
self.register_buffer("bits", unpack_bits(torch.arange(256, dtype=torch.uint8)), persistent=False)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed devices in BitEmbedding init

Medium Severity

BitEmbedding clones weight onto that tensor's device but allocates bit_proj and the bits buffer on the default device. A non-CPU table therefore leaves the module split across devices, and folded_weight only casts dtype, so the first forward raises.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e5377cd. Configure here.

@AmitMY
AmitMY merged commit 0ca5153 into main Oct 10, 2026
6 checks passed
@AmitMY
AmitMY deleted the bit-embedding branch October 10, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant