Commit 7113f64
authored
metal: PTQ1_0 exact-float trit extraction (+30-38% decode) and the missing per-expert mat-vec (#157)
* metal: extract PTQ1_0 trits in exact float instead of the integer recurrence
The mat-vec pulled each trit out of its byte with the base-3 remainder
recurrence, w = v*3, t = w >> 8, v = w & 0xFF: four integer ops and a convert
per trit, on an ISA that cannot co-issue integer and floating-point work. The
kernel was already known to be instruction-bound rather than bandwidth-bound.
A packed byte is a base-3 fraction of 256. With u = b/256, trit n is
g_{n+1} - 3*g_n for g_k = floor(3^k*u), and every 3^k*b is below 2^24, so the
floors are exact and this matches the recurrence bit for bit over all 256
bytes and all five positions. Summing t_n*y_n over a byte's trits collapses to
sum_k g_k*(y_{k-1} - 3*y_k) + g_5*y_4, whose coefficients depend only on the
activations, so the caller stages those in place of the raw y and reuses them
across every row. The inner loop is one floor and one fma per trit with no
integer arithmetic. The qh trit uses the two-floor single-trit form, replacing
a lane-divergent loop that stepped the recurrence up to three times.
Verified against the recurrence for all bytes before the kernel was touched.
test-backend-ops MUL_MAT: 45 PTQ1_0 cases pass, zero failures. Output is not
bit-identical: the coefficient staging reassociates the fp32 sum.
Measured on an M5 Pro, llama-bench r=3, two interleaved passes, dense models:
small decode 221 -> 287 tok/s (+29.5%)
large decode 59 -> 82 tok/s (+37.8%)
Prefill unchanged; the mat-mul path does not use this routine.
* metal: instantiate the per-expert PTQ1_0 mat-vec that supports_op already claims
supports_op accepts PTQ1_0 for MUL_MAT_ID and the dispatcher carries an nsg/nr0
entry for it, but the kernel_mul_mv_id_ptq1_0_f32 pipeline was never
instantiated, so the first per-expert PTQ1_0 mat-vec on Metal fails the library
lookup and the process segfaults. test-backend-ops hit this on its first PTQ1_0
MUL_MAT_ID case and died there, which truncated the Metal MUL_MAT_ID run to 24
cases while still printing a clean tail; nothing after that case, for any type,
was being exercised.
Add the instantiation next to the other low-bit per-expert kernels. It reuses
kernel_mul_mv_ptq1_0_f32_impl, so it inherits the exact-float extraction from
the previous commit. The full Metal MUL_MAT_ID run now completes: 1022 cases,
75 of them PTQ1_0, zero failures.1 parent d0ce941 commit 7113f64
1 file changed
Lines changed: 63 additions & 44 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
862 | 862 | | |
863 | 863 | | |
864 | 864 | | |
865 | | - | |
866 | | - | |
867 | | - | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
873 | | - | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
874 | 877 | | |
875 | 878 | | |
876 | | - | |
877 | 879 | | |
878 | 880 | | |
879 | | - | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | | - | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
885 | 888 | | |
886 | 889 | | |
887 | 890 | | |
888 | | - | |
889 | | - | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
895 | | - | |
896 | | - | |
897 | | - | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
898 | 903 | | |
899 | | - | |
900 | | - | |
901 | | - | |
902 | | - | |
903 | | - | |
904 | | - | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
905 | 907 | | |
906 | 908 | | |
907 | 909 | | |
| |||
940 | 942 | | |
941 | 943 | | |
942 | 944 | | |
943 | | - | |
| 945 | + | |
| 946 | + | |
944 | 947 | | |
945 | 948 | | |
946 | 949 | | |
| |||
950 | 953 | | |
951 | 954 | | |
952 | 955 | | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
953 | 961 | | |
954 | | - | |
| 962 | + | |
| 963 | + | |
955 | 964 | | |
956 | | - | |
957 | 965 | | |
958 | 966 | | |
959 | 967 | | |
| 968 | + | |
960 | 969 | | |
961 | | - | |
962 | | - | |
963 | | - | |
| 970 | + | |
| 971 | + | |
964 | 972 | | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
965 | 977 | | |
966 | | - | |
967 | | - | |
968 | | - | |
969 | | - | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
970 | 988 | | |
971 | 989 | | |
972 | 990 | | |
973 | | - | |
974 | | - | |
| 991 | + | |
| 992 | + | |
975 | 993 | | |
976 | 994 | | |
977 | 995 | | |
| |||
3834 | 3852 | | |
3835 | 3853 | | |
3836 | 3854 | | |
| 3855 | + | |
3837 | 3856 | | |
3838 | 3857 | | |
3839 | 3858 | | |
| |||
0 commit comments