Kernels¶
Packed XNOR/popcount GEMM with runtime SIMD dispatch. See Portable SIMD kernel for the design.
Runtime dispatch¶
kernel_name ¶
kernel_name() -> str
Name of the SIMD path the native kernel selected at runtime.
"numpy" when no native library is loaded, "unknown" for a native
library built before runtime dispatch existed.
Source code in bnn/kernels/packed.py
590 591 592 593 594 595 596 597 598 599 600 601 | |
available_kernels ¶
available_kernels() -> list[str]
Kernel paths usable on this machine, slowest first (always ≥ scalar).
Source code in bnn/kernels/packed.py
617 618 619 620 621 622 623 624 625 626 627 | |
cpu_features ¶
cpu_features() -> dict[str, bool]
Which accelerated paths this CPU can actually run.
Source code in bnn/kernels/packed.py
604 605 606 607 608 609 610 611 612 613 614 | |
set_kernel ¶
set_kernel(name: str | None) -> str
Force a kernel path (None re-runs auto-detection).
Falls back to scalar if the requested path is unsupported here. Returns
the path actually in effect. Intended for validation and reproducibility —
every path must produce identical results.
Source code in bnn/kernels/packed.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
Packing¶
pack_binary_pm1 ¶
pack_binary_pm1(
x: ndarray, axis: int = -1
) -> tuple[np.ndarray, int]
Pack ±1 values along axis into uint64 words. bit1 => -1/non-positive.
Uses NumPy packbits (little bit-order) — much faster than per-bit multiply-sum.
Source code in bnn/kernels/packed.py
317 318 319 320 321 322 323 324 325 326 327 328 329 | |
theoretical_ops ¶
theoretical_ops(m: int, n: int, k: int) -> dict
Source code in bnn/kernels/packed.py
560 561 562 563 564 565 566 567 568 569 570 | |
GEMM¶
binary_gemm_packed ¶
binary_gemm_packed(
x_pm1: ndarray,
w_pm1: ndarray,
*,
prepacked_w: tuple[ndarray, int] | None = None,
) -> np.ndarray
Compute Y = X @ W.T for ±1 matrices using packed XNOR-popcount.
Native SIMD when the library loads. Otherwise packed NumPy for small batch
and dequant+BLAS at/above :func:numpy_packed_blas_crossover_batch so the
no-native path is never 5–11× slower than FP32 at B=64 (docs/45 P1).
Source code in bnn/kernels/packed.py
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 542 543 544 545 546 | |
binary_gemm_numpy_prepacked ¶
binary_gemm_numpy_prepacked(
xp: ndarray, wp: ndarray, n: int
) -> np.ndarray
Y = binary GEMM from pre-packed uint64 matrices (NumPy path).
Source code in bnn/kernels/packed.py
369 370 371 372 373 374 375 376 377 378 379 380 | |
binary_gemm_native_prepacked ¶
binary_gemm_native_prepacked(
xp: ndarray, wp: ndarray, n: int
) -> np.ndarray | None
Source code in bnn/kernels/packed.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
binary_gemm_native_scaled ¶
binary_gemm_native_scaled(
xp: ndarray,
wp: ndarray,
n: int,
alpha: ndarray | None = None,
bias: ndarray | None = None,
) -> np.ndarray | None
Native GEMM with alpha / bias folded into the kernel epilogue.
Y = alpha * (n - 2*hamming) + bias in one pass. Returns None when
the native library is missing or predates the fused entry point, so callers
can fall back to the unfused path.
Source code in bnn/kernels/packed.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
fp32_gemm ¶
fp32_gemm(x: ndarray, w: ndarray) -> np.ndarray
FP32 reference GEMM: Y = X @ W.T.
Uses asarray rather than astype so already-float32 inputs are not
copied. ndarray.astype copies unconditionally by default, which for a
4096x4096 baseline meant timing ~64 MB of memcpy alongside the GEMM and
inflating every "vs FP32" speedup by ~2x.
Source code in bnn/kernels/packed.py
549 550 551 552 553 554 555 556 557 | |
Threads¶
set_num_threads ¶
set_num_threads(n: int | None) -> None
Set native OpenMP thread count (None / 0 = library default).
Also honors process env when first applied via ensure_native_threads().
Source code in bnn/kernels/packed.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
get_num_threads ¶
get_num_threads() -> int
Effective native thread count (1 if no OpenMP / no DLL).
Source code in bnn/kernels/packed.py
101 102 103 104 105 106 | |
openmp_enabled ¶
openmp_enabled() -> bool
Source code in bnn/kernels/packed.py
109 110 111 112 113 | |
Building the native library¶
unix_compile_commands ¶
unix_compile_commands(
cc: str, out: Path, src: Path, openmp: bool
) -> list[list[str]]
Candidate compiler invocations, most-preferred first.
Deliberately no -march=native: the library selects AVX2 / AVX-512 /
NEON at run time, so the object must stay portable to any CPU of the
same architecture. Baking in build-host ISA would produce binaries that
SIGILL on older machines — the opposite of what runtime dispatch is for.
Source code in bnn/kernels/compile_native.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |