Encoder / decoder¶
Binary Transformer encoder, decoder, seq2seq and autoencoder. Attention and LayerNorm stay FP; only the FFN is binarised.
BinaryTransformerEncoder ¶
Bases: Module
Stack of self-attn (FP) + binary/ternary FFN encoder layers.
Source code in bnn/seq/__init__.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
BinaryTransformerDecoder ¶
Bases: Module
Causal decoder with optional cross-attention + binary/ternary FFN.
Source code in bnn/seq/__init__.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
BinarySeq2Seq ¶
Bases: Module
Encoder–Decoder for toy copy / reverse tasks.
Source code in bnn/seq/__init__.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
BinaryAutoEncoder ¶
Bases: Module
MLP autoencoder with binary bottleneck — encode/decode compression story.
Input → FP stem → binary latent encode → binary decode → FP recon head. Demonstrates weight + activation binary path for reconstruction demos.
Source code in bnn/seq/__init__.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
MultiHeadAttention ¶
Bases: Module
FP multi-head attention (Q/K/V/proj). Softmax stays higher precision.
Source code in bnn/seq/__init__.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
CrossAttention ¶
Bases: Module
Dedicated FP cross-attention (separate Q vs KV projections).
Source code in bnn/seq/__init__.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
make_reverse_batch ¶
make_reverse_batch(
batch: int,
seq_len: int,
vocab: int,
*,
seed: int | None = None,
device: device | str = "cpu",
) -> tuple[Tensor, Tensor, Tensor]
Synthetic reverse task: tgt = reverse(src); teacher-forced tgt_in = BOS+tgt[:-1].
Token 0 = PAD/BOS, tokens 1..vocab-1 are content. Returns src, tgt_in, tgt_out.
Source code in bnn/seq/__init__.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
seq2seq_token_accuracy ¶
seq2seq_token_accuracy(
logits: Tensor, tgt: Tensor
) -> float
Source code in bnn/seq/__init__.py
294 295 296 | |