llama.cpp
87f816e8 - metal : support BF16/F32 src1 for MUL_MAT and MUL_MAT_ID (#32677)

Commit
7 hours ago
metal : support BF16/F32 src1 for MUL_MAT and MUL_MAT_ID (#32677) * llama, metal : support BF16 src1 for MUL_MAT and MUL_MAT_ID Add GGML_PREC_BF16 handling for src1 on the Metal backend when the tensor API is available: MUL_MAT with an F32 src0 stages the A tile as bfloat, and MUL_MAT_ID stages src1 as bfloat instead of F16. The tensor path keeps src1 in F32 when it reads the operand directly. Add a temporary LLAMA_MOE_AMAX_SKIP env var that disables the graph-level amax rescaling for the MoE down projection and sets the BF16 src1 hint instead. Apply the hint inside build_lora_mm_id so it reaches the matmul before the per-expert scale and LoRA nodes wrap it. Add MUL_MAT_ID src1 BF16 test cases to test-backend-ops. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * metal : support BF16 src1 without the tensor API Allow GGML_PREC_BF16 when the device has bfloat, not only when it has the Metal4 tensor API. Add legacy _prec_bf16 kernels for MUL_MAT and MUL_MAT_ID that stage src1 as bfloat in the threadgroup and run the simdgroup multiply with bfloat A and B. The F16 MUL_MAT_ID kernel uses half A on the tensor path and bfloat A on the legacy path. Cast the quantized dequantize outputs to the destination element type: the older Metal compiler on M2 rejects implicit float to bfloat. Tested on M2 Ultra (legacy): MUL_MAT_PREC_SRC1 24/24 and MUL_MAT_ID_PREC_SRC1_BF16 30/30. Flint Q4_K_M with LLAMA_MOE_AMAX_SKIP=1: pp512 -2.1%, tg128 +25.5% vs master. On M5 Max: pp512 +2.1%, tg128 +22.2%. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * metal : derive matrix types with value_type helpers Add value_type_4x4/value_type_2x4/value_type_8x8 helpers and use them in the MUL_MAT and MUL_MAT_ID kernels so the templates take only the underlying types and derive the matrix types. Add TODOs for the remaining kernels. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * metal : derive matrix types in the remaining kernels Use the value_type_* helpers in the flash-attention, top-k-mask, mul-mv and count-equal kernels to derive the matrix/vector types from the underlying scalar types, and simplify the FA_TYPES macros. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * metal : enable BF16 in the tensor API metallib The tensor API requires macOS 26, where bfloat is always available, so define GGML_METAL_HAS_BF16 when compiling the tensor metallib. This makes the bfloat specializations of the value_type helpers visible to fa_tensor.metal and includes the prec_bf16 kernels in the prebuilt tensor metallib. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * metal : support GGML_PREC_F32 for src1 in MUL_MAT and MUL_MAT_ID Split the tensor-API and legacy simdgroup mul_mm/mul_mm_id kernels into separate files and library kinds, following the fa_tensor.metal example. The legacy kernels, including the new _prec_f32 variants that stage src0 and src1 as float in shared memory, are compiled into the default mul_mm library; the tensor variants live in a separate mul_mm_tensor library kind and carry a _tensor host-name infix. Tensor devices run F32 src1 matmuls on the simdgroup path instead of the tensor API, which rounds float operands to a reduced precision. This removes the MUL_MM_SG kind and the #undef GGML_METAL_HAS_TENSOR compile. Add MUL_MAT_ID_PREC_SRC1_F32 tests. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * cont : indentation * llama : move the MoE amax skip into the precision policy Add llm_arch_mmid_down_bf16() for the architectures whose MoE down projection uses the graph-level amax rescaling. Extract the precision policy setup from load_tensors() into llama_model_base::load_prec_policy(), and move the LLAMA_MOE_AMAX_SKIP handling there: it sets prec_src1 = BF16 for ffn_down_exps before the per-tensor policy is loaded. build_moe_ffn() now skips the amax rescaling when the policy requests BF16. Apply the policy with the original weight so cached experts get it, drop the prec_src1 parameter from build_lora_mm_id(), and remove the prec_src1 serialization from the model saver. Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB) * cont : minor
Author
Parents
Loading