Weight codec¶
Portable .bnnpack container for packed weights.
encode_linear_state ¶
encode_linear_state(
weight: Tensor,
bias: Tensor | None = None,
*,
alpha: Tensor | None = None,
name: str = "linear",
with_hash: bool = True,
) -> dict[str, Any]
Encode one Linear weight into a portable packed blob (binary XNOR path).
Accepts FP nn.Linear weights or BinaryLinear latents (signed via STE).
Compression is exact 32× when in_features % 64 == 0 (no pad words);
otherwise slightly lower due to uint64 padding.
Source code in bnn/codec/packfile.py
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 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
encode_model_linears ¶
encode_model_linears(
model: Module,
*,
skip_name_substr: tuple[str, ...] | None = None,
min_in_features: int = 1,
include_packed: bool = True,
include_binary_linear: bool = True,
include_fp_linear: bool = False,
include_ternary: bool = False,
include_conv: bool = False,
) -> dict[str, Any]
Encode modules into a layers dict.
Defaults favor the thesis wrap story:
- Already-packed PackedBinaryXNORLinear (post-wrap FFN)
- BinaryLinear STE modules
- Not arbitrary FP nn.Linear (avoids silently binary-packing attn/embed/head)
Set include_fp_linear=True only when you intentionally want cold PTQ of FP
Linears; then skip_name_substr defaults to HYBRID_FFN_SKIP.
Opt in to ternary / Conv2d with include_ternary / include_conv.
Source code in bnn/codec/packfile.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
encode_file ¶
encode_file(
model: Module,
path: Path | str,
*,
meta: dict[str, Any] | None = None,
version: int = BNNPACK_VERSION,
**kwargs: Any,
) -> Path
Source code in bnn/codec/packfile.py
679 680 681 682 683 684 685 686 687 688 | |
decode_file ¶
decode_file(
path: Path | str,
) -> tuple[dict[str, nn.Module], dict[str, Any]]
Load .bnnpack → mapping name → packed module + meta.
Source code in bnn/codec/packfile.py
691 692 693 694 695 696 697 698 | |
load_bnnpack ¶
load_bnnpack(
path: Path | str, *, verify_hashes: bool = True
) -> dict[str, Any]
Load .bnnpack with weights_only=True only (no unsafe pickle fallback).
Soft-warns when the path sits outside lab results/ / checkpoints/ /
data/ (W10.T06). Never falls back to unsafe pickle.
When verify_hashes is True (default) and the file is v2+, recompute
per-layer content_sha256 and raise if any mismatch.
Source code in bnn/codec/packfile.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
save_bnnpack ¶
save_bnnpack(
layers: dict[str, Any],
path: Path | str,
*,
meta: dict[str, Any] | None = None,
version: int = BNNPACK_VERSION,
) -> Path
Source code in bnn/codec/packfile.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
roundtrip_gemm_err ¶
roundtrip_gemm_err(
weight: Tensor, *, batch: int = 4, seed: int = 0
) -> dict[str, float]
Encode → decode → compare packed GEMM vs ±1 FP reference; expect err=0.
Source code in bnn/codec/packfile.py
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | |