Remove nested directory: BitTransformerLM/bit_transformer/parity.py
Browse files
BitTransformerLM/bit_transformer/parity.py
DELETED
|
@@ -1,24 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
-
def enforce_parity(bits: torch.Tensor) -> tuple[torch.Tensor, int]:
|
| 4 |
-
"""Fix parity bits so each 9-bit chunk has even parity.
|
| 5 |
-
|
| 6 |
-
Parameters
|
| 7 |
-
----------
|
| 8 |
-
bits: ``torch.Tensor``
|
| 9 |
-
Tensor of shape ``(..., length)`` where ``length`` is a multiple of 9.
|
| 10 |
-
|
| 11 |
-
Returns
|
| 12 |
-
-------
|
| 13 |
-
tuple[torch.Tensor, int]
|
| 14 |
-
Corrected tensor and number of bytes that were adjusted.
|
| 15 |
-
"""
|
| 16 |
-
if bits.shape[-1] % 9 != 0:
|
| 17 |
-
raise ValueError("Bit stream length must be multiple of 9")
|
| 18 |
-
flat = bits.clone().view(-1, 9)
|
| 19 |
-
payload = flat[:, :8]
|
| 20 |
-
parity = flat[:, 8]
|
| 21 |
-
new_parity = payload.sum(dim=1) % 2
|
| 22 |
-
corrections = (parity != new_parity).sum().item()
|
| 23 |
-
flat[:, 8] = new_parity
|
| 24 |
-
return flat.view_as(bits), corrections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|