usearch 2.25.1 → 2.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +2 -2
  2. package/c/lib.cpp +28 -13
  3. package/cpp/test.cpp +169 -0
  4. package/include/usearch/index.hpp +364 -58
  5. package/include/usearch/index_dense.hpp +155 -50
  6. package/include/usearch/index_plugins.hpp +156 -79
  7. package/javascript/lib.cpp +4 -2
  8. package/numkong/CITATION.cff +1 -1
  9. package/numkong/Cargo.toml +1 -1
  10. package/numkong/bench/bench_cross_amx.cpp +12 -0
  11. package/numkong/bench/bench_mesh.cpp +12 -0
  12. package/numkong/bench/bench_spatial.cpp +0 -3
  13. package/numkong/binding.gyp +18 -0
  14. package/numkong/build.rs +1 -0
  15. package/numkong/c/dispatch_e5m2.c +23 -3
  16. package/numkong/include/numkong/capabilities.h +1 -1
  17. package/numkong/include/numkong/cast/README.md +3 -0
  18. package/numkong/include/numkong/cast/haswell.h +28 -64
  19. package/numkong/include/numkong/cast/serial.h +17 -0
  20. package/numkong/include/numkong/cast/skylake.h +67 -52
  21. package/numkong/include/numkong/cast.h +1 -0
  22. package/numkong/include/numkong/dot/README.md +1 -0
  23. package/numkong/include/numkong/dot/haswell.h +92 -13
  24. package/numkong/include/numkong/dot/serial.h +15 -0
  25. package/numkong/include/numkong/dot/skylake.h +61 -14
  26. package/numkong/include/numkong/dots/README.md +2 -0
  27. package/numkong/include/numkong/dots/graniteamx.h +434 -0
  28. package/numkong/include/numkong/dots/haswell.h +28 -28
  29. package/numkong/include/numkong/dots/sapphireamx.h +1 -1
  30. package/numkong/include/numkong/dots/serial.h +23 -8
  31. package/numkong/include/numkong/dots/skylake.h +28 -23
  32. package/numkong/include/numkong/dots.h +12 -0
  33. package/numkong/include/numkong/each/serial.h +18 -1
  34. package/numkong/include/numkong/geospatial/serial.h +14 -3
  35. package/numkong/include/numkong/maxsim/serial.h +15 -0
  36. package/numkong/include/numkong/mesh/README.md +50 -44
  37. package/numkong/include/numkong/mesh/genoa.h +462 -0
  38. package/numkong/include/numkong/mesh/haswell.h +806 -933
  39. package/numkong/include/numkong/mesh/neon.h +871 -943
  40. package/numkong/include/numkong/mesh/neonbfdot.h +382 -522
  41. package/numkong/include/numkong/mesh/neonfhm.h +676 -0
  42. package/numkong/include/numkong/mesh/rvv.h +404 -319
  43. package/numkong/include/numkong/mesh/serial.h +204 -162
  44. package/numkong/include/numkong/mesh/skylake.h +1029 -1585
  45. package/numkong/include/numkong/mesh/v128relaxed.h +403 -377
  46. package/numkong/include/numkong/mesh.h +38 -0
  47. package/numkong/include/numkong/reduce/serial.h +15 -1
  48. package/numkong/include/numkong/sparse/serial.h +17 -2
  49. package/numkong/include/numkong/spatial/genoa.h +0 -68
  50. package/numkong/include/numkong/spatial/haswell.h +98 -56
  51. package/numkong/include/numkong/spatial/serial.h +15 -0
  52. package/numkong/include/numkong/spatial/skylake.h +114 -54
  53. package/numkong/include/numkong/spatial.h +0 -12
  54. package/numkong/include/numkong/spatials/graniteamx.h +128 -0
  55. package/numkong/include/numkong/spatials/serial.h +18 -1
  56. package/numkong/include/numkong/spatials/skylake.h +2 -2
  57. package/numkong/include/numkong/spatials.h +17 -0
  58. package/numkong/include/numkong/tensor.hpp +107 -23
  59. package/numkong/javascript/@numkong-darwin-arm64/package.json +1 -1
  60. package/numkong/javascript/@numkong-darwin-x64/package.json +1 -1
  61. package/numkong/javascript/@numkong-linux-arm64/package.json +1 -1
  62. package/numkong/javascript/@numkong-linux-x64/package.json +1 -1
  63. package/numkong/javascript/@numkong-win32-arm64/package.json +1 -1
  64. package/numkong/javascript/@numkong-win32-x64/package.json +1 -1
  65. package/numkong/javascript/numkong.c +3 -2
  66. package/numkong/package.json +7 -7
  67. package/numkong/test/test.cu +841 -0
  68. package/numkong/test/test_cross_amx.cpp +13 -0
  69. package/numkong/test/test_dlpack.py +492 -0
  70. package/numkong/test/test_mesh.cpp +12 -0
  71. package/numkong/test/test_spatial.cpp +0 -3
  72. package/numkong/test/test_tensor.cpp +87 -27
  73. package/package.json +2 -2
  74. package/prebuilds/darwin-arm64+x64/usearch.node +0 -0
  75. package/prebuilds/linux-arm64/usearch.node +0 -0
  76. package/prebuilds/linux-x64/usearch.node +0 -0
  77. package/prebuilds/win32-x64/usearch.node +0 -0
@@ -718,6 +718,440 @@ NK_PUBLIC void nk_dots_symmetric_f16_graniteamx(
718
718
 
719
719
  #pragma endregion F16 Native
720
720
 
721
+ #pragma region E5M2 Source (widened to FP16 tiles)
722
+
723
+ /* E5M2 Granite AMX kernels: same F16 tile shapes, same TDPFP16PS compute body as the F16 path.
724
+ * The only difference is byte-to-word widening during A-load and B-pack: E5M2 shares F16's
725
+ * exponent bias (15), so `(byte << 8)` is the exact F16 bit pattern for every E5M2 value,
726
+ * including zero/subnormals/Inf/NaN. Tile buffers hold F16 after widen — "e5m2" in the
727
+ * typedefs refers to the source dtype, not the on-tile representation.
728
+ *
729
+ * The tile types below alias the F16 types (identical memory layout) so we can reuse the
730
+ * F16 init/store/output2x2/update internal helpers by pointer cast at the boundary. Only the
731
+ * public entry points and the load-A widen helper are new.
732
+ */
733
+
734
+ typedef nk_dots_f16_a16x32_graniteamx_t nk_dots_e5m2_a16x32_graniteamx_t;
735
+ typedef nk_dots_f16_b32x16_graniteamx_t nk_dots_e5m2_b32x16_graniteamx_t;
736
+ typedef nk_dots_f16_state_graniteamx_t nk_dots_e5m2_state_graniteamx_t;
737
+ typedef nk_dots_f16_state2x2_graniteamx_t nk_dots_e5m2_state2x2_graniteamx_t;
738
+
739
+ /* Load A tile from E5M2 row-major source, widen to F16 via `(byte << 8)` into the F16 tile buffer. */
740
+ NK_INTERNAL void nk_dots_e5m2_load_a_graniteamx_( //
741
+ nk_dots_e5m2_a16x32_graniteamx_t *a_tile, //
742
+ nk_e5m2_t const *src, nk_size_t src_stride_elements, //
743
+ nk_size_t valid_rows, nk_size_t valid_cols) {
744
+
745
+ __mmask32 column_mask = (valid_cols >= 32) ? 0xFFFFFFFF : ((__mmask32)1 << valid_cols) - 1;
746
+ __m512i zero_i16x32 = _mm512_setzero_si512();
747
+
748
+ for (nk_size_t row_idx = 0; row_idx < 16; row_idx++) {
749
+ if (row_idx < valid_rows) {
750
+ __m256i e5m2_u8x32 = _mm256_maskz_loadu_epi8(column_mask, src + row_idx * src_stride_elements);
751
+ __m512i word_u16x32 = _mm512_cvtepu8_epi16(e5m2_u8x32);
752
+ __m512i f16_bits_i16x32 = _mm512_slli_epi16(word_u16x32, 8);
753
+ _mm512_store_si512((__m512i *)a_tile->data[row_idx], f16_bits_i16x32);
754
+ }
755
+ else { _mm512_store_si512((__m512i *)a_tile->data[row_idx], zero_i16x32); }
756
+ }
757
+ nk_compiler_barrier_sapphireamx_();
758
+ }
759
+
760
+ NK_PUBLIC nk_size_t nk_dots_packed_size_e5m2_graniteamx(nk_size_t column_count, nk_size_t depth) {
761
+ nk_size_t const tmm_rows = 16;
762
+ nk_size_t const tmm_cols = 32;
763
+ nk_size_t const tile_bytes = 512 * sizeof(nk_f16_t); // Tiles hold F16 after widen: same 1KB as F16.
764
+
765
+ nk_size_t const full_column_tiles = column_count / tmm_rows;
766
+ nk_size_t const tiles_along_depth = nk_size_divide_round_up_(depth, tmm_cols);
767
+ nk_size_t const column_remainder_count = column_count - full_column_tiles * tmm_rows;
768
+
769
+ nk_size_t size = sizeof(nk_dots_amx_packed_header_t);
770
+ size += full_column_tiles * tiles_along_depth * tile_bytes;
771
+ if (column_remainder_count > 0) size += column_remainder_count * depth * sizeof(nk_f16_t);
772
+ size += column_count * sizeof(nk_f32_t);
773
+
774
+ return size;
775
+ }
776
+
777
+ NK_PUBLIC void nk_dots_pack_e5m2_graniteamx( //
778
+ nk_e5m2_t const *b, nk_size_t column_count, nk_size_t depth, //
779
+ nk_size_t b_stride_in_bytes, void *b_packed) {
780
+
781
+ nk_size_t const tmm_rows = 16;
782
+ nk_size_t const tmm_cols = 32;
783
+ nk_size_t const tile_elements = 512;
784
+ nk_size_t const tile_bytes = tile_elements * sizeof(nk_f16_t);
785
+ nk_size_t const b_stride_elements = b_stride_in_bytes; // E5M2: 1 byte per element
786
+
787
+ nk_size_t const column_tiles_count = column_count / tmm_rows;
788
+ nk_size_t const depth_tiles_count = nk_size_divide_round_up_(depth, tmm_cols);
789
+ nk_size_t const column_remainder_count = column_count - column_tiles_count * tmm_rows;
790
+ nk_size_t const total_tiles = column_tiles_count * depth_tiles_count;
791
+
792
+ nk_dots_amx_packed_header_t *header = (nk_dots_amx_packed_header_t *)b_packed;
793
+ header->full_column_tiles = (nk_u32_t)column_tiles_count;
794
+ header->full_depth_tiles = (nk_u32_t)depth_tiles_count;
795
+ header->column_remainder_count = (nk_u32_t)column_remainder_count;
796
+
797
+ nk_size_t const tiles_offset = sizeof(nk_dots_amx_packed_header_t);
798
+ nk_size_t const column_edge_offset = tiles_offset + total_tiles * tile_bytes;
799
+ header->column_edge_offset = (nk_u32_t)column_edge_offset;
800
+
801
+ nk_f16_t *tiles_ptr = (nk_f16_t *)((char *)b_packed + tiles_offset);
802
+ nk_f16_t *column_edge_ptr = (nk_f16_t *)((char *)b_packed + column_edge_offset);
803
+
804
+ // Pack tiles: widen-and-gather 16 strided E5M2 rows into an F16-aligned source tile,
805
+ // then transpose via the shared BF16/F16 transposer (same 16-bit lane layout).
806
+ for (nk_size_t column_tile_idx = 0; column_tile_idx < column_tiles_count; column_tile_idx++) {
807
+ for (nk_size_t depth_tile_idx = 0; depth_tile_idx < depth_tiles_count; depth_tile_idx++) {
808
+
809
+ nk_size_t const tile_index = column_tile_idx * depth_tiles_count + depth_tile_idx;
810
+ nk_f16_t *tile_output = tiles_ptr + tile_index * tile_elements;
811
+
812
+ nk_size_t const src_row_start = column_tile_idx * tmm_rows;
813
+ nk_size_t const src_column_start = depth_tile_idx * tmm_cols;
814
+ nk_size_t const columns_to_pack = (src_column_start + tmm_cols <= depth) ? tmm_cols
815
+ : (depth - src_column_start);
816
+
817
+ nk_dots_bf16_a16x32_sapphireamx_t source_tile;
818
+ __mmask32 depth_mask = (__mmask32)((columns_to_pack < 32) ? ((1U << columns_to_pack) - 1) : ~0U);
819
+ for (nk_size_t row_idx = 0; row_idx < tmm_rows; row_idx++) {
820
+ nk_e5m2_t const *source_row = b + (src_row_start + row_idx) * b_stride_elements + src_column_start;
821
+ __m256i e5m2_u8x32 = _mm256_maskz_loadu_epi8(depth_mask, source_row);
822
+ __m512i word_u16x32 = _mm512_cvtepu8_epi16(e5m2_u8x32);
823
+ __m512i f16_bits_i16x32 = _mm512_slli_epi16(word_u16x32, 8);
824
+ _mm512_store_si512(&source_tile.data[row_idx][0], f16_bits_i16x32);
825
+ }
826
+
827
+ nk_dots_bf16_b32x16_sapphireamx_t transposed_tile;
828
+ nk_dots_pack_bf16_transposed_sapphireamx_(&source_tile, &transposed_tile);
829
+ for (nk_size_t i = 0; i < tile_bytes; i += 64)
830
+ _mm512_storeu_si512((char *)tile_output + i, _mm512_load_si512((char const *)&transposed_tile + i));
831
+ }
832
+ }
833
+
834
+ // Column-remainder: widen and store row-major (depth contiguous per column)
835
+ if (column_remainder_count > 0) {
836
+ nk_size_t const remainder_start_row = column_tiles_count * tmm_rows;
837
+ for (nk_size_t row_idx = 0; row_idx < column_remainder_count; row_idx++) {
838
+ nk_e5m2_t const *src_row = b + (remainder_start_row + row_idx) * b_stride_elements;
839
+ nk_f16_t *dst_row = column_edge_ptr + row_idx * depth;
840
+ nk_size_t column_idx = 0;
841
+ for (; column_idx + 32 <= depth; column_idx += 32) {
842
+ __m256i e5m2_u8x32 = _mm256_loadu_si256((__m256i const *)(src_row + column_idx));
843
+ __m512i word_u16x32 = _mm512_cvtepu8_epi16(e5m2_u8x32);
844
+ __m512i f16_bits_i16x32 = _mm512_slli_epi16(word_u16x32, 8);
845
+ _mm512_storeu_si512(dst_row + column_idx, f16_bits_i16x32);
846
+ }
847
+ if (column_idx < depth) {
848
+ __mmask32 tail_mask = (__mmask32)((1U << (depth - column_idx)) - 1);
849
+ __m256i e5m2_u8x32 = _mm256_maskz_loadu_epi8(tail_mask, src_row + column_idx);
850
+ __m512i word_u16x32 = _mm512_cvtepu8_epi16(e5m2_u8x32);
851
+ __m512i f16_bits_i16x32 = _mm512_slli_epi16(word_u16x32, 8);
852
+ _mm512_mask_storeu_epi16(dst_row + column_idx, tail_mask, f16_bits_i16x32);
853
+ }
854
+ }
855
+ }
856
+
857
+ nk_size_t norms_offset = column_edge_offset +
858
+ (column_remainder_count > 0 ? column_remainder_count * depth * sizeof(nk_f16_t) : 0);
859
+ header->norms_byte_offset = (nk_u32_t)norms_offset;
860
+ nk_f32_t *norms = (nk_f32_t *)((char *)b_packed + norms_offset);
861
+ for (nk_size_t col = 0; col < column_count; col++)
862
+ norms[col] = nk_dots_reduce_sumsq_e5m2_(b + col * b_stride_elements, depth);
863
+ }
864
+
865
+ NK_PUBLIC void nk_dots_packed_e5m2_graniteamx( //
866
+ nk_e5m2_t const *a, void const *b_packed, nk_f32_t *c, //
867
+ nk_size_t rows_count, nk_size_t cols_count, nk_size_t depth, nk_size_t a_stride_bytes, nk_size_t c_stride_bytes) {
868
+ nk_unused_(cols_count);
869
+
870
+ nk_dots_amx_packed_header_t const *header = (nk_dots_amx_packed_header_t const *)b_packed;
871
+ nk_size_t const column_tiles_count = header->full_column_tiles;
872
+ nk_size_t const depth_tiles_count = header->full_depth_tiles;
873
+ nk_size_t const column_remainder_count = header->column_remainder_count;
874
+
875
+ nk_f16_t const *b_tiles_base = (nk_f16_t const *)((char const *)b_packed + sizeof(nk_dots_amx_packed_header_t));
876
+ nk_f16_t const *col_edge_ptr = (nk_f16_t const *)((char const *)b_packed + header->column_edge_offset);
877
+
878
+ nk_size_t const a_stride_elements = a_stride_bytes; // E5M2: 1 byte per element
879
+ nk_size_t const c_stride_elements = c_stride_bytes / sizeof(nk_f32_t);
880
+
881
+ nk_size_t const tile_depth = 32;
882
+ nk_size_t const tile_size = 512;
883
+ nk_size_t const full_cols = column_tiles_count * 16;
884
+
885
+ nk_size_t const row_blocks_count = nk_size_divide_round_up_(rows_count, 32);
886
+ nk_size_t const col_blocks_count = column_tiles_count / 2;
887
+
888
+ if (depth_tiles_count == 0) return;
889
+
890
+ nk_dots_e5m2_a16x32_graniteamx_t a_tile_top, a_tile_bottom;
891
+ nk_dots_e5m2_state2x2_graniteamx_t c_accum_buffer;
892
+
893
+ nk_size_t const full_depth_tiles_count = depth / tile_depth;
894
+ nk_size_t const depth_remainder = depth % tile_depth;
895
+
896
+ nk_amx_tile_configure_sapphireamx_();
897
+
898
+ for (nk_size_t row_block_idx = 0; row_block_idx < row_blocks_count; row_block_idx++) {
899
+ nk_size_t const row_block_start = row_block_idx * 32;
900
+ nk_size_t const valid_rows_count = (row_block_start + 32 <= rows_count) ? 32 : (rows_count - row_block_start);
901
+ nk_size_t const is_full_row_block = (valid_rows_count == 32);
902
+
903
+ for (nk_size_t column_block_idx = 0; column_block_idx < col_blocks_count; column_block_idx++) {
904
+ nk_size_t const col_block_start = column_block_idx * 32;
905
+ nk_size_t const b_column_left_base = (column_block_idx * 2) * depth_tiles_count;
906
+ nk_size_t const b_column_right_base = (column_block_idx * 2 + 1) * depth_tiles_count;
907
+
908
+ _tile_zero(4);
909
+ _tile_zero(5);
910
+ _tile_zero(6);
911
+ _tile_zero(7);
912
+
913
+ // E5M2 A needs widen-then-tile-load every time (no fast in-place path), so we always
914
+ // route through the widen buffer a_tile_{top,bottom} rather than `_tile_loadd` from A.
915
+ for (nk_size_t depth_tile_idx = 0; depth_tile_idx < depth_tiles_count; depth_tile_idx++) {
916
+ nk_size_t const depth_offset = depth_tile_idx * tile_depth;
917
+ nk_size_t const valid_depth = (depth_tile_idx < full_depth_tiles_count) ? tile_depth : depth_remainder;
918
+ nk_size_t const rows_in_high_tile = is_full_row_block
919
+ ? 16
920
+ : ((valid_rows_count > 16) ? 16 : valid_rows_count);
921
+ nk_size_t const rows_in_low_tile = is_full_row_block
922
+ ? 16
923
+ : ((valid_rows_count > 16) ? valid_rows_count - 16 : 0);
924
+
925
+ nk_dots_e5m2_load_a_graniteamx_(&a_tile_top, a + row_block_start * a_stride_elements + depth_offset,
926
+ a_stride_elements, rows_in_high_tile, valid_depth);
927
+ if (rows_in_low_tile > 0) {
928
+ nk_dots_e5m2_load_a_graniteamx_(&a_tile_bottom,
929
+ a + (row_block_start + 16) * a_stride_elements + depth_offset,
930
+ a_stride_elements, rows_in_low_tile, valid_depth);
931
+ }
932
+
933
+ nk_dots_f16_b32x16_graniteamx_t const *b_tile_left = //
934
+ (nk_dots_f16_b32x16_graniteamx_t const *)(b_tiles_base +
935
+ (b_column_left_base + depth_tile_idx) * tile_size);
936
+ nk_dots_f16_b32x16_graniteamx_t const *b_tile_right = //
937
+ (nk_dots_f16_b32x16_graniteamx_t const *)(b_tiles_base +
938
+ (b_column_right_base + depth_tile_idx) * tile_size);
939
+
940
+ _tile_loadd(0, a_tile_top.data, 64);
941
+ if (rows_in_low_tile > 0) _tile_loadd(1, a_tile_bottom.data, 64);
942
+ _tile_loadd(2, b_tile_left->data, 64);
943
+ _tile_loadd(3, b_tile_right->data, 64);
944
+
945
+ _tile_dpfp16ps(4, 0, 2);
946
+ _tile_dpfp16ps(5, 0, 3);
947
+ if (rows_in_low_tile > 0) {
948
+ _tile_dpfp16ps(6, 1, 2);
949
+ _tile_dpfp16ps(7, 1, 3);
950
+ }
951
+ }
952
+
953
+ if (is_full_row_block) {
954
+ nk_f32_t *c_block = c + row_block_start * c_stride_elements + col_block_start;
955
+ _tile_stored(4, c_block, c_stride_bytes);
956
+ _tile_stored(5, c_block + 16, c_stride_bytes);
957
+ _tile_stored(6, (nk_f32_t *)((char *)c_block + 16 * c_stride_bytes), c_stride_bytes);
958
+ _tile_stored(7, (nk_f32_t *)((char *)c_block + 16 * c_stride_bytes) + 16, c_stride_bytes);
959
+ }
960
+ else {
961
+ _tile_stored(4, c_accum_buffer.c[0][0].data, 64);
962
+ _tile_stored(5, c_accum_buffer.c[0][1].data, 64);
963
+ _tile_stored(6, c_accum_buffer.c[1][0].data, 64);
964
+ _tile_stored(7, c_accum_buffer.c[1][1].data, 64);
965
+ nk_dots_f16_output2x2_graniteamx_(&c_accum_buffer,
966
+ c + row_block_start * c_stride_elements + col_block_start,
967
+ c_stride_elements, valid_rows_count, 32);
968
+ }
969
+ }
970
+ }
971
+
972
+ // Odd column tile
973
+ if (column_tiles_count % 2 == 1) {
974
+ nk_size_t const column_tile_idx = column_tiles_count - 1;
975
+ nk_size_t const col_start = column_tile_idx * 16;
976
+ nk_size_t const b_column_base = column_tile_idx * depth_tiles_count;
977
+
978
+ for (nk_size_t row_block_idx = 0; row_block_idx < row_blocks_count; row_block_idx++) {
979
+ nk_size_t const row_block_start = row_block_idx * 32;
980
+ nk_size_t const valid_rows_count = (row_block_start + 32 <= rows_count) ? 32
981
+ : (rows_count - row_block_start);
982
+ nk_size_t const rows_in_high_tile = (valid_rows_count > 16) ? 16 : valid_rows_count;
983
+ nk_size_t const rows_in_low_tile = (valid_rows_count > 16) ? valid_rows_count - 16 : 0;
984
+
985
+ nk_dots_f16_state_graniteamx_t c_high_state, c_low_state;
986
+
987
+ _tile_zero(4);
988
+ _tile_zero(6);
989
+
990
+ for (nk_size_t depth_tile_idx = 0; depth_tile_idx < depth_tiles_count; depth_tile_idx++) {
991
+ nk_size_t const depth_offset = depth_tile_idx * tile_depth;
992
+ nk_size_t const valid_depth = (depth_tile_idx < full_depth_tiles_count) ? tile_depth : depth_remainder;
993
+
994
+ nk_dots_e5m2_load_a_graniteamx_(&a_tile_top, a + row_block_start * a_stride_elements + depth_offset,
995
+ a_stride_elements, rows_in_high_tile, valid_depth);
996
+ if (rows_in_low_tile > 0) {
997
+ nk_dots_e5m2_load_a_graniteamx_(&a_tile_bottom,
998
+ a + (row_block_start + 16) * a_stride_elements + depth_offset,
999
+ a_stride_elements, rows_in_low_tile, valid_depth);
1000
+ }
1001
+
1002
+ nk_dots_f16_b32x16_graniteamx_t const *b_tile = //
1003
+ (nk_dots_f16_b32x16_graniteamx_t const *)(b_tiles_base +
1004
+ (b_column_base + depth_tile_idx) * tile_size);
1005
+
1006
+ _tile_loadd(0, a_tile_top.data, 64);
1007
+ if (rows_in_low_tile > 0) _tile_loadd(1, a_tile_bottom.data, 64);
1008
+ _tile_loadd(2, b_tile->data, 64);
1009
+
1010
+ _tile_dpfp16ps(4, 0, 2);
1011
+ if (rows_in_low_tile > 0) _tile_dpfp16ps(6, 1, 2);
1012
+ }
1013
+
1014
+ _tile_stored(4, c_high_state.data, 64);
1015
+ if (rows_in_low_tile > 0) _tile_stored(6, c_low_state.data, 64);
1016
+
1017
+ nk_dots_f16_store_graniteamx_(&c_high_state, c + row_block_start * c_stride_elements + col_start,
1018
+ c_stride_elements, rows_in_high_tile, 16);
1019
+ if (rows_in_low_tile > 0) {
1020
+ nk_dots_f16_store_graniteamx_(&c_low_state, c + (row_block_start + 16) * c_stride_elements + col_start,
1021
+ c_stride_elements, rows_in_low_tile, 16);
1022
+ }
1023
+ }
1024
+ }
1025
+
1026
+ // Column edge (fewer than 16 extra columns, stored row-major as F16 after pack)
1027
+ if (column_remainder_count > 0) {
1028
+ for (nk_size_t row_block_idx = 0; row_block_idx < row_blocks_count; row_block_idx++) {
1029
+ nk_size_t const row_block_start = row_block_idx * 32;
1030
+ nk_size_t const valid_rows_count = (row_block_start + 32 <= rows_count) ? 32
1031
+ : (rows_count - row_block_start);
1032
+ nk_size_t const rows_in_high_tile = (valid_rows_count > 16) ? 16 : valid_rows_count;
1033
+ nk_size_t const rows_in_low_tile = (valid_rows_count > 16) ? valid_rows_count - 16 : 0;
1034
+
1035
+ nk_dots_f16_state_graniteamx_t c_high_state, c_low_state;
1036
+ nk_dots_bf16_a16x32_sapphireamx_t b_as_a;
1037
+ nk_dots_bf16_b32x16_sapphireamx_t b_tile;
1038
+
1039
+ _tile_zero(4);
1040
+ _tile_zero(6);
1041
+
1042
+ for (nk_size_t depth_tile_idx = 0; depth_tile_idx < depth_tiles_count; depth_tile_idx++) {
1043
+ nk_size_t const depth_offset = depth_tile_idx * tile_depth;
1044
+ nk_size_t const valid_depth = (depth_tile_idx < full_depth_tiles_count) ? tile_depth : depth_remainder;
1045
+
1046
+ nk_dots_e5m2_load_a_graniteamx_(&a_tile_top, a + row_block_start * a_stride_elements + depth_offset,
1047
+ a_stride_elements, rows_in_high_tile, valid_depth);
1048
+ if (rows_in_low_tile > 0) {
1049
+ nk_dots_e5m2_load_a_graniteamx_(&a_tile_bottom,
1050
+ a + (row_block_start + 16) * a_stride_elements + depth_offset,
1051
+ a_stride_elements, rows_in_low_tile, valid_depth);
1052
+ }
1053
+
1054
+ nk_dots_bf16_load_a_sapphireamx_(&b_as_a, (nk_bf16_t const *)(col_edge_ptr + depth_offset), depth,
1055
+ column_remainder_count, valid_depth);
1056
+ nk_dots_pack_bf16_transposed_sapphireamx_(&b_as_a, &b_tile);
1057
+
1058
+ _tile_loadd(0, a_tile_top.data, 64);
1059
+ if (rows_in_low_tile > 0) _tile_loadd(1, a_tile_bottom.data, 64);
1060
+ _tile_loadd(2, b_tile.data, 64);
1061
+
1062
+ _tile_dpfp16ps(4, 0, 2);
1063
+ if (rows_in_low_tile > 0) _tile_dpfp16ps(6, 1, 2);
1064
+ }
1065
+
1066
+ _tile_stored(4, c_high_state.data, 64);
1067
+ if (rows_in_low_tile > 0) _tile_stored(6, c_low_state.data, 64);
1068
+
1069
+ nk_dots_f16_store_graniteamx_(&c_high_state, c + row_block_start * c_stride_elements + full_cols,
1070
+ c_stride_elements, rows_in_high_tile, column_remainder_count);
1071
+ if (rows_in_low_tile > 0) {
1072
+ nk_dots_f16_store_graniteamx_(&c_low_state, c + (row_block_start + 16) * c_stride_elements + full_cols,
1073
+ c_stride_elements, rows_in_low_tile, column_remainder_count);
1074
+ }
1075
+ }
1076
+ }
1077
+
1078
+ _tile_release();
1079
+ }
1080
+
1081
+ NK_PUBLIC void nk_dots_symmetric_e5m2_graniteamx( //
1082
+ nk_e5m2_t const *vectors, nk_size_t vectors_count, nk_size_t depth, //
1083
+ nk_size_t stride_in_bytes, nk_f32_t *result, nk_size_t result_stride_in_bytes, //
1084
+ nk_size_t row_start, nk_size_t row_count) {
1085
+
1086
+ nk_size_t const stride_elements = stride_in_bytes; // E5M2: 1 byte per element
1087
+ nk_size_t const result_stride_elements = result_stride_in_bytes / sizeof(nk_f32_t);
1088
+
1089
+ nk_size_t const row_end = (row_count == 0)
1090
+ ? vectors_count
1091
+ : (row_start + row_count < vectors_count ? row_start + row_count : vectors_count);
1092
+
1093
+ nk_size_t const depth_tiles = nk_size_divide_round_up_(depth, 32);
1094
+ nk_size_t const depth_tile_groups = nk_size_divide_round_up_(depth_tiles, 3);
1095
+
1096
+ nk_dots_e5m2_a16x32_graniteamx_t a_tiles[3];
1097
+ nk_dots_e5m2_a16x32_graniteamx_t b_src_tiles[3];
1098
+ nk_dots_f16_b32x16_graniteamx_t b_tiles[3];
1099
+ nk_dots_f16_state_graniteamx_t state;
1100
+
1101
+ nk_amx_tile_configure_sapphireamx_();
1102
+
1103
+ for (nk_size_t row_tile = row_start; row_tile < row_end; row_tile += 16) {
1104
+ nk_size_t const valid_rows = (row_tile + 16 <= row_end) ? 16 : (row_end - row_tile);
1105
+
1106
+ for (nk_size_t col_tile = 0; col_tile < vectors_count; col_tile += 16) {
1107
+ nk_size_t const valid_cols = (col_tile + 16 <= vectors_count) ? 16 : (vectors_count - col_tile);
1108
+
1109
+ nk_dots_f16_init_graniteamx_(&state);
1110
+
1111
+ for (nk_size_t depth_group_idx = 0; depth_group_idx < depth_tile_groups; depth_group_idx++) {
1112
+ nk_size_t const depth_base = depth_group_idx * 96;
1113
+
1114
+ for (int tile_idx = 0; tile_idx < 3; tile_idx++) {
1115
+ nk_size_t const depth_start = depth_base + tile_idx * 32;
1116
+ nk_size_t const valid_depth = (depth_start + 32 <= depth)
1117
+ ? 32
1118
+ : (depth > depth_start ? depth - depth_start : 0);
1119
+
1120
+ nk_dots_e5m2_load_a_graniteamx_( //
1121
+ &a_tiles[tile_idx], //
1122
+ vectors + row_tile * stride_elements + depth_start, //
1123
+ stride_elements, valid_rows, valid_depth);
1124
+
1125
+ if (row_tile == col_tile) {
1126
+ nk_dots_pack_bf16_transposed_sapphireamx_(
1127
+ (nk_dots_bf16_a16x32_sapphireamx_t const *)&a_tiles[tile_idx],
1128
+ (nk_dots_bf16_b32x16_sapphireamx_t *)&b_tiles[tile_idx]);
1129
+ }
1130
+ else {
1131
+ nk_dots_e5m2_load_a_graniteamx_( //
1132
+ &b_src_tiles[tile_idx], //
1133
+ vectors + col_tile * stride_elements + depth_start, //
1134
+ stride_elements, valid_cols, valid_depth);
1135
+ nk_dots_pack_bf16_transposed_sapphireamx_(
1136
+ (nk_dots_bf16_a16x32_sapphireamx_t const *)&b_src_tiles[tile_idx],
1137
+ (nk_dots_bf16_b32x16_sapphireamx_t *)&b_tiles[tile_idx]);
1138
+ }
1139
+ }
1140
+
1141
+ nk_dots_f16_update_graniteamx_( //
1142
+ &state, &a_tiles[0], &a_tiles[1], &a_tiles[2], //
1143
+ &b_tiles[0], &b_tiles[1], &b_tiles[2]);
1144
+ }
1145
+
1146
+ nk_dots_f16_store_graniteamx_( //
1147
+ &state, result + row_tile * result_stride_elements + col_tile, //
1148
+ result_stride_elements, valid_rows, valid_cols);
1149
+ }
1150
+ }
1151
+ }
1152
+
1153
+ #pragma endregion E5M2 Source
1154
+
721
1155
  #if defined(__clang__)
722
1156
  #pragma clang attribute pop
723
1157
  #elif defined(__GNUC__)
@@ -115,45 +115,45 @@ nk_define_cross_packed_(dots, bf16, haswell, bf16, bf16, f32, nk_b256_vec_t, nk_
115
115
  nk_partial_store_b32x4_haswell_,
116
116
  /*depth_simd_dimensions=*/16, /*dimensions_per_value=*/1)
117
117
 
118
- /* E4M3 GEMM: depth_simd_dimensions=8 (8 e4m3s = 8 bytes) → upcasted to 8×f32 (256-bit) */
119
- nk_define_cross_pack_size_(dots, e4m3, haswell, e4m3, f32, /*norm_value_type=*/f32, /*depth_simd_dimensions=*/8,
118
+ /* E4M3 GEMM: depth_simd_dimensions=32 (byte-level batch; widen inside the update helper) */
119
+ nk_define_cross_pack_size_(dots, e4m3, haswell, e4m3, f32, /*norm_value_type=*/f32, /*depth_simd_dimensions=*/32,
120
120
  /*dimensions_per_value=*/1)
121
- nk_define_cross_pack_(dots, e4m3, haswell, e4m3, f32, nk_b256_vec_t, nk_load_e4m3x8_to_f32x8_haswell_,
122
- nk_partial_load_e4m3x8_to_f32x8_haswell_, nk_store_b256_haswell_, nk_partial_store_b32x8_serial_,
123
- /*simd_width=*/8, /*norm_value_type=*/f32, nk_dots_reduce_sumsq_e4m3_,
124
- /*depth_simd_dimensions=*/8, /*dimensions_per_value=*/1)
121
+ nk_define_cross_pack_(dots, e4m3, haswell, e4m3, f32, nk_b256_vec_t, nk_load_b256_haswell_,
122
+ nk_partial_load_b8x32_serial_, nk_store_b256_haswell_, nk_partial_store_b8x32_serial_,
123
+ /*simd_width=*/32, /*norm_value_type=*/f32, nk_dots_reduce_sumsq_e4m3_,
124
+ /*depth_simd_dimensions=*/32, /*dimensions_per_value=*/1)
125
125
  nk_define_cross_symmetric_(dots, e4m3, haswell, e4m3, f32, nk_b256_vec_t, nk_dot_through_f32_state_haswell_t_,
126
- nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_e4m3x8_to_f32x8_haswell_,
127
- nk_partial_load_e4m3x8_to_f32x8_haswell_, nk_dot_through_f32_update_haswell_,
126
+ nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_b256_haswell_,
127
+ nk_partial_load_b8x32_serial_, nk_dot_e4m3x32_update_haswell_,
128
128
  nk_dot_through_f32_finalize_haswell_, nk_store_b128_haswell_,
129
129
  nk_partial_store_b32x4_haswell_,
130
- /*depth_simd_dimensions=*/8, /*dimensions_per_value=*/1)
130
+ /*depth_simd_dimensions=*/32, /*dimensions_per_value=*/1)
131
131
  nk_define_cross_packed_(dots, e4m3, haswell, e4m3, f32, f32, nk_b256_vec_t, nk_dot_through_f32_state_haswell_t_,
132
- nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_e4m3x8_to_f32x8_haswell_,
133
- nk_partial_load_e4m3x8_to_f32x8_haswell_, nk_load_b256_haswell_, nk_partial_load_b32x8_serial_,
134
- nk_dot_through_f32_update_haswell_, nk_dot_through_f32_finalize_haswell_,
135
- nk_store_b128_haswell_, nk_partial_store_b32x4_haswell_,
136
- /*depth_simd_dimensions=*/8, /*dimensions_per_value=*/1)
132
+ nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_b256_haswell_,
133
+ nk_partial_load_b8x32_serial_, nk_load_b256_haswell_, nk_partial_load_b8x32_serial_,
134
+ nk_dot_e4m3x32_update_haswell_, nk_dot_through_f32_finalize_haswell_, nk_store_b128_haswell_,
135
+ nk_partial_store_b32x4_haswell_,
136
+ /*depth_simd_dimensions=*/32, /*dimensions_per_value=*/1)
137
137
 
138
- /* E5M2 GEMM: depth_simd_dimensions=8 (8 e5m2s = 8 bytes) → upcasted to 8×f32 (256-bit) */
139
- nk_define_cross_pack_size_(dots, e5m2, haswell, e5m2, f32, /*norm_value_type=*/f32, /*depth_simd_dimensions=*/8,
138
+ /* E5M2 GEMM: depth_simd_dimensions=32 (byte-level batch; widen inside the update helper) */
139
+ nk_define_cross_pack_size_(dots, e5m2, haswell, e5m2, f32, /*norm_value_type=*/f32, /*depth_simd_dimensions=*/32,
140
140
  /*dimensions_per_value=*/1)
141
- nk_define_cross_pack_(dots, e5m2, haswell, e5m2, f32, nk_b256_vec_t, nk_load_e5m2x8_to_f32x8_haswell_,
142
- nk_partial_load_e5m2x8_to_f32x8_haswell_, nk_store_b256_haswell_, nk_partial_store_b32x8_serial_,
143
- /*simd_width=*/8, /*norm_value_type=*/f32, nk_dots_reduce_sumsq_e5m2_,
144
- /*depth_simd_dimensions=*/8, /*dimensions_per_value=*/1)
141
+ nk_define_cross_pack_(dots, e5m2, haswell, e5m2, f32, nk_b256_vec_t, nk_load_b256_haswell_,
142
+ nk_partial_load_b8x32_serial_, nk_store_b256_haswell_, nk_partial_store_b8x32_serial_,
143
+ /*simd_width=*/32, /*norm_value_type=*/f32, nk_dots_reduce_sumsq_e5m2_,
144
+ /*depth_simd_dimensions=*/32, /*dimensions_per_value=*/1)
145
145
  nk_define_cross_symmetric_(dots, e5m2, haswell, e5m2, f32, nk_b256_vec_t, nk_dot_through_f32_state_haswell_t_,
146
- nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_e5m2x8_to_f32x8_haswell_,
147
- nk_partial_load_e5m2x8_to_f32x8_haswell_, nk_dot_through_f32_update_haswell_,
146
+ nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_b256_haswell_,
147
+ nk_partial_load_b8x32_serial_, nk_dot_e5m2x32_update_haswell_,
148
148
  nk_dot_through_f32_finalize_haswell_, nk_store_b128_haswell_,
149
149
  nk_partial_store_b32x4_haswell_,
150
- /*depth_simd_dimensions=*/8, /*dimensions_per_value=*/1)
150
+ /*depth_simd_dimensions=*/32, /*dimensions_per_value=*/1)
151
151
  nk_define_cross_packed_(dots, e5m2, haswell, e5m2, f32, f32, nk_b256_vec_t, nk_dot_through_f32_state_haswell_t_,
152
- nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_e5m2x8_to_f32x8_haswell_,
153
- nk_partial_load_e5m2x8_to_f32x8_haswell_, nk_load_b256_haswell_, nk_partial_load_b32x8_serial_,
154
- nk_dot_through_f32_update_haswell_, nk_dot_through_f32_finalize_haswell_,
155
- nk_store_b128_haswell_, nk_partial_store_b32x4_haswell_,
156
- /*depth_simd_dimensions=*/8, /*dimensions_per_value=*/1)
152
+ nk_b128_vec_t, nk_dot_through_f32_init_haswell_, nk_load_b256_haswell_,
153
+ nk_partial_load_b8x32_serial_, nk_load_b256_haswell_, nk_partial_load_b8x32_serial_,
154
+ nk_dot_e5m2x32_update_haswell_, nk_dot_through_f32_finalize_haswell_, nk_store_b128_haswell_,
155
+ nk_partial_store_b32x4_haswell_,
156
+ /*depth_simd_dimensions=*/32, /*dimensions_per_value=*/1)
157
157
 
158
158
  /* E2M3 GEMM: integer LUT path, depth_simd_dimensions=32 (32 e2m3s = 32 bytes = AVX2 register width) */
159
159
  nk_define_cross_pack_size_(dots, e2m3, haswell, e2m3, e2m3, /*norm_value_type=*/f32, /*depth_simd_dimensions=*/32,
@@ -73,7 +73,7 @@
73
73
  #if NK_TARGET_SAPPHIREAMX
74
74
 
75
75
  #include "numkong/cast/icelake.h" // For FP8 ↔ BF16 conversions
76
- #include "numkong/dots/serial.h" // For nk_dots_reduce_sumsq_bf16_
76
+ #include "numkong/dots/serial.h" // `nk_dots_reduce_sumsq_bf16_`
77
77
 
78
78
  #if defined(__cplusplus)
79
79
  extern "C" {
@@ -522,7 +522,7 @@ NK_INTERNAL nk_i32_t nk_dots_reduce_sum_i4_(nk_i4x2_t const *data, nk_size_t cou
522
522
  load_a_vec_fn, partial_load_a_vec_fn, load_b_vec_fn, partial_load_b_vec_fn, \
523
523
  inner_product_fn, reduce_accumulators_fn, store_fn, partial_store_fn, \
524
524
  depth_simd_dimensions, dimensions_per_value) \
525
- NK_PUBLIC void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_aligned_( \
525
+ NK_INTERNAL void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_aligned_( \
526
526
  nk_##input_value_type##_t const *a_matrix, void const *b_packed_buffer, nk_##result_value_type##_t *c_matrix, \
527
527
  nk_size_t row_count, nk_size_t column_count, nk_size_t depth, nk_size_t a_stride_in_bytes, \
528
528
  nk_size_t c_stride_in_bytes) { \
@@ -698,7 +698,7 @@ NK_INTERNAL nk_i32_t nk_dots_reduce_sum_i4_(nk_i4x2_t const *data, nk_size_t cou
698
698
  } \
699
699
  } \
700
700
  } \
701
- NK_PUBLIC void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_1x8_aligned_( \
701
+ NK_INTERNAL void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_1x8_aligned_( \
702
702
  nk_##input_value_type##_t const *a_matrix, void const *b_packed_buffer, nk_##result_value_type##_t *c_matrix, \
703
703
  nk_size_t row_count, nk_size_t column_count, nk_size_t depth, nk_size_t a_stride_in_bytes, \
704
704
  nk_size_t c_stride_in_bytes) { \
@@ -1090,7 +1090,7 @@ NK_INTERNAL nk_i32_t nk_dots_reduce_sum_i4_(nk_i4x2_t const *data, nk_size_t cou
1090
1090
  norm_value_type, vec_type, state_type, result_vec_type, init_accumulator_fn, load_a_vec_fn, partial_load_a_vec_fn, \
1091
1091
  load_b_vec_fn, partial_load_b_vec_fn, inner_product_fn, compensated_finalize_fn, store_fn, partial_store_fn, \
1092
1092
  load_sum_fn, partial_load_sum_fn, compute_a_sum_fn, depth_simd_dimensions, dimensions_per_value) \
1093
- NK_PUBLIC void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_aligned_( \
1093
+ NK_INTERNAL void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_aligned_( \
1094
1094
  nk_##input_value_type##_t const *a_matrix, void const *b_packed_buffer, nk_##result_value_type##_t *c_matrix, \
1095
1095
  nk_size_t row_count, nk_size_t column_count, nk_size_t depth, nk_size_t a_stride_in_bytes, \
1096
1096
  nk_size_t c_stride_in_bytes) { \
@@ -1200,7 +1200,7 @@ NK_INTERNAL nk_i32_t nk_dots_reduce_sum_i4_(nk_i4x2_t const *data, nk_size_t cou
1200
1200
  } \
1201
1201
  } \
1202
1202
  } \
1203
- NK_PUBLIC void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_1x8_aligned_( \
1203
+ NK_INTERNAL void nk_##api_name##_packed_##input_type_name##_##isa_suffix##_1x8_aligned_( \
1204
1204
  nk_##input_value_type##_t const *a_matrix, void const *b_packed_buffer, nk_##result_value_type##_t *c_matrix, \
1205
1205
  nk_size_t row_count, nk_size_t column_count, nk_size_t depth, nk_size_t a_stride_in_bytes, \
1206
1206
  nk_size_t c_stride_in_bytes) { \
@@ -2431,10 +2431,19 @@ NK_INTERNAL nk_i32_t nk_dots_reduce_sum_i4_(nk_i4x2_t const *data, nk_size_t cou
2431
2431
  } \
2432
2432
  }
2433
2433
 
2434
- /* Optimize serial GEMM instantiations for size rather than speed.
2435
- * These fallback kernels are only used when no SIMD backend is available, so aggressive inlining/unrolling from -O3
2436
- * wastes over 1 MB of binary space with negligible performance benefit on the serial path.
2437
- */
2434
+ /* Keep the serial instantiations below actually scalar, regardless of build type.
2435
+ * Without this, -O3 + LTO can vectorize or clone the serial kernels under AVX-512
2436
+ * callers in dispatch_*.c, which wastes ~1 MB of binary and — more importantly —
2437
+ * breaks the nk_*_serial-as-scalar-oracle contract that tests and the numerical-
2438
+ * stability docs in this header rely on. */
2439
+ #if defined(__clang__)
2440
+ #pragma clang attribute push(__attribute__((noinline)), apply_to = function)
2441
+ #elif defined(__GNUC__)
2442
+ #pragma GCC push_options
2443
+ #pragma GCC optimize("no-tree-vectorize", "no-tree-slp-vectorize", "no-ipa-cp-clone", "no-inline")
2444
+ #endif
2445
+
2446
+ /* Size bias for release. Gated on NDEBUG so Debug builds keep -O0 for stepping. */
2438
2447
  #if defined(NDEBUG)
2439
2448
  #if defined(_MSC_VER)
2440
2449
  #pragma optimize("s", on)
@@ -2689,6 +2698,12 @@ nk_define_cross_packed_(dots, u1, serial, u1x8, u1x8, u32, nk_b128_vec_t, nk_dot
2689
2698
  #endif
2690
2699
  #endif
2691
2700
 
2701
+ #if defined(__clang__)
2702
+ #pragma clang attribute pop
2703
+ #elif defined(__GNUC__)
2704
+ #pragma GCC pop_options
2705
+ #endif
2706
+
2692
2707
  /* BF16 compact: truncate F32 → BF16 in-place.
2693
2708
  * Reads F32 matrix with c_stride_in_bytes, writes BF16 tightly packed (stride_in_bytes = column_count × sizeof(bf16)).
2694
2709
  */