ArtificialRay commited on
Commit
c09d00d
·
1 Parent(s): 07f5581

Add kernel in different precision:

Browse files

- CV models: FP32 kernels and int8 (w8a8ch) kernels
- LLM models: BF16 kernels and int4 (Q4_K) kernels
Full precision kernels (fp32 and bf16) are ready to benchmark while low-bit kernels have correctness issue that may need a new evaluator

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. definitions/gemm/{gemm_fp32_n1024_k2048.json → gemm_bf16_n1024_k2048.json} +4 -4
  2. definitions/gemm/{gemm_fp32_n1408_k2048.json → gemm_bf16_n1408_k2048.json} +4 -4
  3. definitions/gemm/{gemm_fp32_n2048_k1024.json → gemm_bf16_n2048_k1024.json} +4 -4
  4. definitions/gemm/{gemm_fp32_n2048_k1408.json → gemm_bf16_n2048_k1408.json} +4 -4
  5. definitions/gemm/{gemm_fp32_n2048_k2048.json → gemm_bf16_n2048_k2048.json} +4 -4
  6. definitions/gemm/gemm_q4_k_m_n1024_k2048.json +72 -0
  7. definitions/gemm/gemm_q4_k_m_n1408_k2048.json +72 -0
  8. definitions/gemm/gemm_q4_k_m_n2048_k1024.json +72 -0
  9. definitions/gemm/gemm_q4_k_m_n2048_k1536.json +72 -0
  10. definitions/gemm/gemm_q4_k_m_n2048_k2048.json +72 -0
  11. definitions/mha/{mha_fp32_h16_d128_kvh16.json → mha_bf16_h16_d128_kvh16.json} +5 -5
  12. definitions/moe/{moe_fp32_e60_k4_d2048_ff1408.json → moe_bf16_e60_k4_d2048_ff1408.json} +7 -7
  13. definitions/moe/{moe_fp32_e64_k8_d2048_ff1024.json → moe_bf16_e64_k8_d2048_ff1024.json} +7 -7
  14. definitions/moe/moe_q4_k_m_e60_k4_d2048_ff1536.json +146 -0
  15. definitions/moe/moe_q4_k_m_e64_k8_d2048_ff1024.json +146 -0
  16. definitions/rms_norm/{rms_norm_fp32_d2048.json → rms_norm_bf16_d2048.json} +4 -4
  17. solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n1024_k2048.json → gemm_bf16_n1024_k2048.json} +5 -5
  18. solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n1408_k2048.json → gemm_bf16_n1408_k2048.json} +5 -5
  19. solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n2048_k1024.json → gemm_bf16_n2048_k1024.json} +5 -5
  20. solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n2048_k1408.json → gemm_bf16_n2048_k1408.json} +5 -5
  21. solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n2048_k2048.json → gemm_bf16_n2048_k2048.json} +5 -5
  22. solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n1024_k2048.json +39 -0
  23. solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n1408_k2048.json +39 -0
  24. solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n2048_k1024.json +39 -0
  25. solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n2048_k1536.json +39 -0
  26. solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n2048_k2048.json +39 -0
  27. solutions/llama.cpp/baseline-llamacpp-arm/mha/{mha_fp32_h16_d128_kvh16.json → mha_bf16_h16_d128_kvh16.json} +5 -5
  28. solutions/llama.cpp/baseline-llamacpp-arm/moe/{moe_fp32_e60_k4_d2048_ff1408.json → moe_bf16_e60_k4_d2048_ff1408.json} +5 -5
  29. solutions/llama.cpp/baseline-llamacpp-arm/moe/{moe_fp32_e64_k8_d2048_ff1024.json → moe_bf16_e64_k8_d2048_ff1024.json} +5 -5
  30. solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_q4_k_m_e60_k4_d2048_ff1536.json +39 -0
  31. solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_q4_k_m_e64_k8_d2048_ff1024.json +39 -0
  32. solutions/llama.cpp/baseline-llamacpp-arm/rms_norm/{rms_norm_fp32_d2048.json → rms_norm_bf16_d2048.json} +5 -5
  33. solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n1024_k2048.json +36 -0
  34. solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n1408_k2048.json +36 -0
  35. solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n2048_k1024.json +36 -0
  36. solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n2048_k1408.json +36 -0
  37. solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n2048_k2048.json +36 -0
  38. solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n1024_k2048.json +0 -38
  39. solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n1408_k2048.json +0 -38
  40. solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n2048_k1024.json +0 -38
  41. solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n2048_k1408.json +0 -38
  42. solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n2048_k2048.json +0 -38
  43. solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n1024_k2048.json +36 -0
  44. solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n1408_k2048.json +36 -0
  45. solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n2048_k1024.json +36 -0
  46. solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n2048_k1536.json +36 -0
  47. solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n2048_k2048.json +36 -0
  48. solutions/llama.cpp/reference-scalar/mha/mha_bf16_h16_d128_kvh16.json +36 -0
  49. solutions/llama.cpp/reference-scalar/mha/mha_fp32_h16_d128_kvh16.json +0 -38
  50. solutions/llama.cpp/reference-scalar/moe/moe_bf16_e60_k4_d2048_ff1408.json +36 -0
definitions/gemm/{gemm_fp32_n1024_k2048.json → gemm_bf16_n1024_k2048.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "gemm_fp32_n1024_k2048",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=1024 K=2048",
5
  "tags": [
@@ -26,14 +26,14 @@
26
  "M",
27
  "K"
28
  ],
29
- "dtype": "float32"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
- "dtype": "float32"
37
  }
38
  },
39
  "outputs": {
@@ -46,5 +46,5 @@
46
  }
47
  },
48
  "constraints": [],
49
- "reference": "import numpy as np\ndef run(A, B):\n return A @ B.T\n"
50
  }
 
1
  {
2
+ "name": "gemm_bf16_n1024_k2048",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=1024 K=2048",
5
  "tags": [
 
26
  "M",
27
  "K"
28
  ],
29
+ "dtype": "bfloat16"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
+ "dtype": "bfloat16"
37
  }
38
  },
39
  "outputs": {
 
46
  }
47
  },
48
  "constraints": [],
49
+ "reference": "import numpy as np\ndef run(A, B):\n return A.astype(np.float32) @ B.astype(np.float32).T\n"
50
  }
definitions/gemm/{gemm_fp32_n1408_k2048.json → gemm_bf16_n1408_k2048.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "gemm_fp32_n1408_k2048",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=1408 K=2048",
5
  "tags": [
@@ -26,14 +26,14 @@
26
  "M",
27
  "K"
28
  ],
29
- "dtype": "float32"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
- "dtype": "float32"
37
  }
38
  },
39
  "outputs": {
@@ -46,5 +46,5 @@
46
  }
47
  },
48
  "constraints": [],
49
- "reference": "import numpy as np\ndef run(A, B):\n return A @ B.T\n"
50
  }
 
1
  {
2
+ "name": "gemm_bf16_n1408_k2048",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=1408 K=2048",
5
  "tags": [
 
26
  "M",
27
  "K"
28
  ],
29
+ "dtype": "bfloat16"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
+ "dtype": "bfloat16"
37
  }
38
  },
39
  "outputs": {
 
46
  }
47
  },
48
  "constraints": [],
49
+ "reference": "import numpy as np\ndef run(A, B):\n return A.astype(np.float32) @ B.astype(np.float32).T\n"
50
  }
definitions/gemm/{gemm_fp32_n2048_k1024.json → gemm_bf16_n2048_k1024.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "gemm_fp32_n2048_k1024",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=2048 K=1024",
5
  "tags": [
@@ -26,14 +26,14 @@
26
  "M",
27
  "K"
28
  ],
29
- "dtype": "float32"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
- "dtype": "float32"
37
  }
38
  },
39
  "outputs": {
@@ -46,5 +46,5 @@
46
  }
47
  },
48
  "constraints": [],
49
- "reference": "import numpy as np\ndef run(A, B):\n return A @ B.T\n"
50
  }
 
1
  {
2
+ "name": "gemm_bf16_n2048_k1024",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=2048 K=1024",
5
  "tags": [
 
26
  "M",
27
  "K"
28
  ],
29
+ "dtype": "bfloat16"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
+ "dtype": "bfloat16"
37
  }
38
  },
39
  "outputs": {
 
46
  }
47
  },
48
  "constraints": [],
49
+ "reference": "import numpy as np\ndef run(A, B):\n return A.astype(np.float32) @ B.astype(np.float32).T\n"
50
  }
definitions/gemm/{gemm_fp32_n2048_k1408.json → gemm_bf16_n2048_k1408.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "gemm_fp32_n2048_k1408",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=2048 K=1408",
5
  "tags": [
@@ -26,14 +26,14 @@
26
  "M",
27
  "K"
28
  ],
29
- "dtype": "float32"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
- "dtype": "float32"
37
  }
38
  },
39
  "outputs": {
@@ -46,5 +46,5 @@
46
  }
47
  },
48
  "constraints": [],
49
- "reference": "import numpy as np\ndef run(A, B):\n return A @ B.T\n"
50
  }
 
1
  {
2
+ "name": "gemm_bf16_n2048_k1408",
3
  "op_type": "gemm",
4
  "description": "expert ffn N=2048 K=1408",
5
  "tags": [
 
26
  "M",
27
  "K"
28
  ],
29
+ "dtype": "bfloat16"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
+ "dtype": "bfloat16"
37
  }
38
  },
39
  "outputs": {
 
46
  }
47
  },
48
  "constraints": [],
49
+ "reference": "import numpy as np\ndef run(A, B):\n return A.astype(np.float32) @ B.astype(np.float32).T\n"
50
  }
definitions/gemm/{gemm_fp32_n2048_k2048.json → gemm_bf16_n2048_k2048.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "gemm_fp32_n2048_k2048",
3
  "op_type": "gemm",
4
  "description": "attn proj N=2048 K=2048",
5
  "tags": [
@@ -26,14 +26,14 @@
26
  "M",
27
  "K"
28
  ],
29
- "dtype": "float32"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
- "dtype": "float32"
37
  }
38
  },
39
  "outputs": {
@@ -46,5 +46,5 @@
46
  }
47
  },
48
  "constraints": [],
49
- "reference": "import numpy as np\ndef run(A, B):\n return A @ B.T\n"
50
  }
 
1
  {
2
+ "name": "gemm_bf16_n2048_k2048",
3
  "op_type": "gemm",
4
  "description": "attn proj N=2048 K=2048",
5
  "tags": [
 
26
  "M",
27
  "K"
28
  ],
29
+ "dtype": "bfloat16"
30
  },
31
  "B": {
32
  "shape": [
33
  "N",
34
  "K"
35
  ],
36
+ "dtype": "bfloat16"
37
  }
38
  },
39
  "outputs": {
 
46
  }
47
  },
48
  "constraints": [],
49
+ "reference": "import numpy as np\ndef run(A, B):\n return A.astype(np.float32) @ B.astype(np.float32).T\n"
50
  }
definitions/gemm/gemm_q4_k_m_n1024_k2048.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gemm_q4_k_m_n1024_k2048",
3
+ "op_type": "gemm",
4
+ "description": "expert ffn N=1024 K=2048",
5
+ "tags": [
6
+ "status:active",
7
+ "model:olmoe-1b-7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "M": {
12
+ "type": "var"
13
+ },
14
+ "N": {
15
+ "type": "const",
16
+ "value": 1024
17
+ },
18
+ "K": {
19
+ "type": "const",
20
+ "value": 2048
21
+ },
22
+ "K_half": {
23
+ "type": "const",
24
+ "value": 1024
25
+ },
26
+ "K_sub": {
27
+ "type": "const",
28
+ "value": 64
29
+ }
30
+ },
31
+ "inputs": {
32
+ "A": {
33
+ "shape": [
34
+ "M",
35
+ "K"
36
+ ],
37
+ "dtype": "bfloat16"
38
+ },
39
+ "B_q4": {
40
+ "shape": [
41
+ "N",
42
+ "K_half"
43
+ ],
44
+ "dtype": "uint8"
45
+ },
46
+ "B_scales": {
47
+ "shape": [
48
+ "N",
49
+ "K_sub"
50
+ ],
51
+ "dtype": "float16"
52
+ },
53
+ "B_mins": {
54
+ "shape": [
55
+ "N",
56
+ "K_sub"
57
+ ],
58
+ "dtype": "float16"
59
+ }
60
+ },
61
+ "outputs": {
62
+ "C": {
63
+ "shape": [
64
+ "M",
65
+ "N"
66
+ ],
67
+ "dtype": "float32"
68
+ }
69
+ },
70
+ "constraints": [],
71
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(A, B_q4, B_scales, B_mins):\n A_f = A.astype(np.float32)\n B_f = dq4k(B_q4, B_scales, B_mins)\n return A_f @ B_f.T\n"
72
+ }
definitions/gemm/gemm_q4_k_m_n1408_k2048.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gemm_q4_k_m_n1408_k2048",
3
+ "op_type": "gemm",
4
+ "description": "expert ffn N=1408 K=2048",
5
+ "tags": [
6
+ "status:active",
7
+ "model:qwen1.5-moe-a2.7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "M": {
12
+ "type": "var"
13
+ },
14
+ "N": {
15
+ "type": "const",
16
+ "value": 1408
17
+ },
18
+ "K": {
19
+ "type": "const",
20
+ "value": 2048
21
+ },
22
+ "K_half": {
23
+ "type": "const",
24
+ "value": 1024
25
+ },
26
+ "K_sub": {
27
+ "type": "const",
28
+ "value": 64
29
+ }
30
+ },
31
+ "inputs": {
32
+ "A": {
33
+ "shape": [
34
+ "M",
35
+ "K"
36
+ ],
37
+ "dtype": "bfloat16"
38
+ },
39
+ "B_q4": {
40
+ "shape": [
41
+ "N",
42
+ "K_half"
43
+ ],
44
+ "dtype": "uint8"
45
+ },
46
+ "B_scales": {
47
+ "shape": [
48
+ "N",
49
+ "K_sub"
50
+ ],
51
+ "dtype": "float16"
52
+ },
53
+ "B_mins": {
54
+ "shape": [
55
+ "N",
56
+ "K_sub"
57
+ ],
58
+ "dtype": "float16"
59
+ }
60
+ },
61
+ "outputs": {
62
+ "C": {
63
+ "shape": [
64
+ "M",
65
+ "N"
66
+ ],
67
+ "dtype": "float32"
68
+ }
69
+ },
70
+ "constraints": [],
71
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(A, B_q4, B_scales, B_mins):\n A_f = A.astype(np.float32)\n B_f = dq4k(B_q4, B_scales, B_mins)\n return A_f @ B_f.T\n"
72
+ }
definitions/gemm/gemm_q4_k_m_n2048_k1024.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gemm_q4_k_m_n2048_k1024",
3
+ "op_type": "gemm",
4
+ "description": "expert ffn N=2048 K=1024",
5
+ "tags": [
6
+ "status:active",
7
+ "model:olmoe-1b-7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "M": {
12
+ "type": "var"
13
+ },
14
+ "N": {
15
+ "type": "const",
16
+ "value": 2048
17
+ },
18
+ "K": {
19
+ "type": "const",
20
+ "value": 1024
21
+ },
22
+ "K_half": {
23
+ "type": "const",
24
+ "value": 512
25
+ },
26
+ "K_sub": {
27
+ "type": "const",
28
+ "value": 32
29
+ }
30
+ },
31
+ "inputs": {
32
+ "A": {
33
+ "shape": [
34
+ "M",
35
+ "K"
36
+ ],
37
+ "dtype": "bfloat16"
38
+ },
39
+ "B_q4": {
40
+ "shape": [
41
+ "N",
42
+ "K_half"
43
+ ],
44
+ "dtype": "uint8"
45
+ },
46
+ "B_scales": {
47
+ "shape": [
48
+ "N",
49
+ "K_sub"
50
+ ],
51
+ "dtype": "float16"
52
+ },
53
+ "B_mins": {
54
+ "shape": [
55
+ "N",
56
+ "K_sub"
57
+ ],
58
+ "dtype": "float16"
59
+ }
60
+ },
61
+ "outputs": {
62
+ "C": {
63
+ "shape": [
64
+ "M",
65
+ "N"
66
+ ],
67
+ "dtype": "float32"
68
+ }
69
+ },
70
+ "constraints": [],
71
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(A, B_q4, B_scales, B_mins):\n A_f = A.astype(np.float32)\n B_f = dq4k(B_q4, B_scales, B_mins)\n return A_f @ B_f.T\n"
72
+ }
definitions/gemm/gemm_q4_k_m_n2048_k1536.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gemm_q4_k_m_n2048_k1536",
3
+ "op_type": "gemm",
4
+ "description": "expert ffn N=2048 K=1536 (K rounded up from 1408 to 1536, the next multiple of 256, since Q4_K requires the reduction dim divisible by 256; real GGML would fall back to Q5_0 for the unrounded 1408 shape)",
5
+ "tags": [
6
+ "status:active",
7
+ "model:qwen1.5-moe-a2.7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "M": {
12
+ "type": "var"
13
+ },
14
+ "N": {
15
+ "type": "const",
16
+ "value": 2048
17
+ },
18
+ "K": {
19
+ "type": "const",
20
+ "value": 1536
21
+ },
22
+ "K_half": {
23
+ "type": "const",
24
+ "value": 768
25
+ },
26
+ "K_sub": {
27
+ "type": "const",
28
+ "value": 48
29
+ }
30
+ },
31
+ "inputs": {
32
+ "A": {
33
+ "shape": [
34
+ "M",
35
+ "K"
36
+ ],
37
+ "dtype": "bfloat16"
38
+ },
39
+ "B_q4": {
40
+ "shape": [
41
+ "N",
42
+ "K_half"
43
+ ],
44
+ "dtype": "uint8"
45
+ },
46
+ "B_scales": {
47
+ "shape": [
48
+ "N",
49
+ "K_sub"
50
+ ],
51
+ "dtype": "float16"
52
+ },
53
+ "B_mins": {
54
+ "shape": [
55
+ "N",
56
+ "K_sub"
57
+ ],
58
+ "dtype": "float16"
59
+ }
60
+ },
61
+ "outputs": {
62
+ "C": {
63
+ "shape": [
64
+ "M",
65
+ "N"
66
+ ],
67
+ "dtype": "float32"
68
+ }
69
+ },
70
+ "constraints": [],
71
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(A, B_q4, B_scales, B_mins):\n A_f = A.astype(np.float32)\n B_f = dq4k(B_q4, B_scales, B_mins)\n return A_f @ B_f.T\n"
72
+ }
definitions/gemm/gemm_q4_k_m_n2048_k2048.json ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gemm_q4_k_m_n2048_k2048",
3
+ "op_type": "gemm",
4
+ "description": "expert ffn N=2048 K=2048",
5
+ "tags": [
6
+ "status:active",
7
+ "model:qwen1.5-moe-a2.7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "M": {
12
+ "type": "var"
13
+ },
14
+ "N": {
15
+ "type": "const",
16
+ "value": 2048
17
+ },
18
+ "K": {
19
+ "type": "const",
20
+ "value": 2048
21
+ },
22
+ "K_half": {
23
+ "type": "const",
24
+ "value": 1024
25
+ },
26
+ "K_sub": {
27
+ "type": "const",
28
+ "value": 64
29
+ }
30
+ },
31
+ "inputs": {
32
+ "A": {
33
+ "shape": [
34
+ "M",
35
+ "K"
36
+ ],
37
+ "dtype": "bfloat16"
38
+ },
39
+ "B_q4": {
40
+ "shape": [
41
+ "N",
42
+ "K_half"
43
+ ],
44
+ "dtype": "uint8"
45
+ },
46
+ "B_scales": {
47
+ "shape": [
48
+ "N",
49
+ "K_sub"
50
+ ],
51
+ "dtype": "float16"
52
+ },
53
+ "B_mins": {
54
+ "shape": [
55
+ "N",
56
+ "K_sub"
57
+ ],
58
+ "dtype": "float16"
59
+ }
60
+ },
61
+ "outputs": {
62
+ "C": {
63
+ "shape": [
64
+ "M",
65
+ "N"
66
+ ],
67
+ "dtype": "float32"
68
+ }
69
+ },
70
+ "constraints": [],
71
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(A, B_q4, B_scales, B_mins):\n A_f = A.astype(np.float32)\n B_f = dq4k(B_q4, B_scales, B_mins)\n return A_f @ B_f.T\n"
72
+ }
definitions/mha/{mha_fp32_h16_d128_kvh16.json → mha_bf16_h16_d128_kvh16.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "mha_fp32_h16_d128_kvh16",
3
  "op_type": "mha",
4
  "description": "MHA h=16 d=128 kv=16",
5
  "tags": [
@@ -34,7 +34,7 @@
34
  "n_heads",
35
  "head_dim"
36
  ],
37
- "dtype": "float32"
38
  },
39
  "K": {
40
  "shape": [
@@ -42,7 +42,7 @@
42
  "kv_heads",
43
  "head_dim"
44
  ],
45
- "dtype": "float32"
46
  },
47
  "V": {
48
  "shape": [
@@ -50,7 +50,7 @@
50
  "kv_heads",
51
  "head_dim"
52
  ],
53
- "dtype": "float32"
54
  }
55
  },
56
  "outputs": {
@@ -64,5 +64,5 @@
64
  }
65
  },
66
  "constraints": [],
67
- "reference": "import numpy as np\ndef run(Q, K, V):\n M = Q.shape[0]; S = K.shape[0]\n K_e = K\n V_e = V\n Qt = Q.transpose(1, 0, 2)\n Kt = K_e.transpose(1, 0, 2)\n Vt = V_e.transpose(1, 0, 2)\n scores = np.matmul(Qt, Kt.transpose(0, 2, 1)) * 0.08838834764831845\n scores -= scores.max(-1, keepdims=True)\n probs = np.exp(scores)\n probs /= probs.sum(-1, keepdims=True)\n return np.matmul(probs, Vt).transpose(1, 0, 2)\n"
68
  }
 
1
  {
2
+ "name": "mha_bf16_h16_d128_kvh16",
3
  "op_type": "mha",
4
  "description": "MHA h=16 d=128 kv=16",
5
  "tags": [
 
34
  "n_heads",
35
  "head_dim"
36
  ],
37
+ "dtype": "bfloat16"
38
  },
39
  "K": {
40
  "shape": [
 
42
  "kv_heads",
43
  "head_dim"
44
  ],
45
+ "dtype": "bfloat16"
46
  },
47
  "V": {
48
  "shape": [
 
50
  "kv_heads",
51
  "head_dim"
52
  ],
53
+ "dtype": "bfloat16"
54
  }
55
  },
56
  "outputs": {
 
64
  }
65
  },
66
  "constraints": [],
67
+ "reference": "import numpy as np\ndef run(Q, K, V):\n Q = Q.astype(np.float32)\n K = K.astype(np.float32)\n V = V.astype(np.float32)\n M = Q.shape[0]; S = K.shape[0]\n K_e = K\n V_e = V\n Qt = Q.transpose(1, 0, 2)\n Kt = K_e.transpose(1, 0, 2)\n Vt = V_e.transpose(1, 0, 2)\n scores = np.matmul(Qt, Kt.transpose(0, 2, 1)) * 0.08838834764831845\n scores -= scores.max(-1, keepdims=True)\n probs = np.exp(scores)\n probs /= probs.sum(-1, keepdims=True)\n return np.matmul(probs, Vt).transpose(1, 0, 2)\n"
68
  }
definitions/moe/{moe_fp32_e60_k4_d2048_ff1408.json → moe_bf16_e60_k4_d2048_ff1408.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "moe_fp32_e60_k4_d2048_ff1408",
3
  "op_type": "moe",
4
  "description": "MoE e=60 k=4 d=2048 ff=1408",
5
  "tags": [
@@ -34,14 +34,14 @@
34
  "n_tokens",
35
  "n_embd"
36
  ],
37
- "dtype": "float32"
38
  },
39
  "router_weight": {
40
  "shape": [
41
  "n_expert",
42
  "n_embd"
43
  ],
44
- "dtype": "float32"
45
  },
46
  "gate_proj": {
47
  "shape": [
@@ -49,7 +49,7 @@
49
  "n_ff",
50
  "n_embd"
51
  ],
52
- "dtype": "float32"
53
  },
54
  "up_proj": {
55
  "shape": [
@@ -57,7 +57,7 @@
57
  "n_ff",
58
  "n_embd"
59
  ],
60
- "dtype": "float32"
61
  },
62
  "down_proj": {
63
  "shape": [
@@ -65,7 +65,7 @@
65
  "n_embd",
66
  "n_ff"
67
  ],
68
- "dtype": "float32"
69
  }
70
  },
71
  "outputs": {
@@ -78,5 +78,5 @@
78
  }
79
  },
80
  "constraints": [],
81
- "reference": "import numpy as np\ndef run(hidden_states, router_weight, gate_proj, up_proj, down_proj):\n T = hidden_states.shape[0]\n logits = hidden_states @ router_weight.T\n probs = np.exp(logits - logits.max(-1, keepdims=True))\n probs /= probs.sum(-1, keepdims=True)\n top_idx = np.argsort(probs, axis=-1)[:, -4:]\n top_w = np.take_along_axis(probs, top_idx, axis=-1)\n top_w /= top_w.sum(-1, keepdims=True)\n out = np.zeros((T, 2048), dtype=np.float32)\n for t in range(T):\n for ki in range(4):\n e = top_idx[t, ki]\n g = gate_proj[e] @ hidden_states[t]\n u = up_proj[e] @ hidden_states[t]\n silu = g / (1 + np.exp(-g))\n out[t] += top_w[t, ki] * (down_proj[e] @ (silu * u))\n return out\n"
82
  }
 
1
  {
2
+ "name": "moe_bf16_e60_k4_d2048_ff1408",
3
  "op_type": "moe",
4
  "description": "MoE e=60 k=4 d=2048 ff=1408",
5
  "tags": [
 
34
  "n_tokens",
35
  "n_embd"
36
  ],
37
+ "dtype": "bfloat16"
38
  },
39
  "router_weight": {
40
  "shape": [
41
  "n_expert",
42
  "n_embd"
43
  ],
44
+ "dtype": "bfloat16"
45
  },
46
  "gate_proj": {
47
  "shape": [
 
49
  "n_ff",
50
  "n_embd"
51
  ],
52
+ "dtype": "bfloat16"
53
  },
54
  "up_proj": {
55
  "shape": [
 
57
  "n_ff",
58
  "n_embd"
59
  ],
60
+ "dtype": "bfloat16"
61
  },
62
  "down_proj": {
63
  "shape": [
 
65
  "n_embd",
66
  "n_ff"
67
  ],
68
+ "dtype": "bfloat16"
69
  }
70
  },
71
  "outputs": {
 
78
  }
79
  },
80
  "constraints": [],
81
+ "reference": "import numpy as np\ndef run(hidden_states, router_weight, gate_proj, up_proj, down_proj):\n hidden_states = hidden_states.astype(np.float32)\n router_weight = router_weight.astype(np.float32)\n gate_proj = gate_proj.astype(np.float32)\n up_proj = up_proj.astype(np.float32)\n down_proj = down_proj.astype(np.float32)\n T = hidden_states.shape[0]\n logits = hidden_states @ router_weight.T\n probs = np.exp(logits - logits.max(-1, keepdims=True))\n probs /= probs.sum(-1, keepdims=True)\n top_idx = np.argsort(probs, axis=-1)[:, -4:]\n top_w = np.take_along_axis(probs, top_idx, axis=-1)\n top_w /= top_w.sum(-1, keepdims=True)\n out = np.zeros((T, 2048), dtype=np.float32)\n for t in range(T):\n for ki in range(4):\n e = top_idx[t, ki]\n g = gate_proj[e] @ hidden_states[t]\n u = up_proj[e] @ hidden_states[t]\n silu = g / (1 + np.exp(-g))\n out[t] += top_w[t, ki] * (down_proj[e] @ (silu * u))\n return out\n"
82
  }
definitions/moe/{moe_fp32_e64_k8_d2048_ff1024.json → moe_bf16_e64_k8_d2048_ff1024.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "moe_fp32_e64_k8_d2048_ff1024",
3
  "op_type": "moe",
4
  "description": "MoE e=64 k=8 d=2048 ff=1024",
5
  "tags": [
@@ -34,14 +34,14 @@
34
  "n_tokens",
35
  "n_embd"
36
  ],
37
- "dtype": "float32"
38
  },
39
  "router_weight": {
40
  "shape": [
41
  "n_expert",
42
  "n_embd"
43
  ],
44
- "dtype": "float32"
45
  },
46
  "gate_proj": {
47
  "shape": [
@@ -49,7 +49,7 @@
49
  "n_ff",
50
  "n_embd"
51
  ],
52
- "dtype": "float32"
53
  },
54
  "up_proj": {
55
  "shape": [
@@ -57,7 +57,7 @@
57
  "n_ff",
58
  "n_embd"
59
  ],
60
- "dtype": "float32"
61
  },
62
  "down_proj": {
63
  "shape": [
@@ -65,7 +65,7 @@
65
  "n_embd",
66
  "n_ff"
67
  ],
68
- "dtype": "float32"
69
  }
70
  },
71
  "outputs": {
@@ -78,5 +78,5 @@
78
  }
79
  },
80
  "constraints": [],
81
- "reference": "import numpy as np\ndef run(hidden_states, router_weight, gate_proj, up_proj, down_proj):\n T = hidden_states.shape[0]\n logits = hidden_states @ router_weight.T\n probs = np.exp(logits - logits.max(-1, keepdims=True))\n probs /= probs.sum(-1, keepdims=True)\n top_idx = np.argsort(probs, axis=-1)[:, -8:]\n top_w = np.take_along_axis(probs, top_idx, axis=-1)\n top_w /= top_w.sum(-1, keepdims=True)\n out = np.zeros((T, 2048), dtype=np.float32)\n for t in range(T):\n for ki in range(8):\n e = top_idx[t, ki]\n g = gate_proj[e] @ hidden_states[t]\n u = up_proj[e] @ hidden_states[t]\n silu = g / (1 + np.exp(-g))\n out[t] += top_w[t, ki] * (down_proj[e] @ (silu * u))\n return out\n"
82
  }
 
1
  {
2
+ "name": "moe_bf16_e64_k8_d2048_ff1024",
3
  "op_type": "moe",
4
  "description": "MoE e=64 k=8 d=2048 ff=1024",
5
  "tags": [
 
34
  "n_tokens",
35
  "n_embd"
36
  ],
37
+ "dtype": "bfloat16"
38
  },
39
  "router_weight": {
40
  "shape": [
41
  "n_expert",
42
  "n_embd"
43
  ],
44
+ "dtype": "bfloat16"
45
  },
46
  "gate_proj": {
47
  "shape": [
 
49
  "n_ff",
50
  "n_embd"
51
  ],
52
+ "dtype": "bfloat16"
53
  },
54
  "up_proj": {
55
  "shape": [
 
57
  "n_ff",
58
  "n_embd"
59
  ],
60
+ "dtype": "bfloat16"
61
  },
62
  "down_proj": {
63
  "shape": [
 
65
  "n_embd",
66
  "n_ff"
67
  ],
68
+ "dtype": "bfloat16"
69
  }
70
  },
71
  "outputs": {
 
78
  }
79
  },
80
  "constraints": [],
81
+ "reference": "import numpy as np\ndef run(hidden_states, router_weight, gate_proj, up_proj, down_proj):\n hidden_states = hidden_states.astype(np.float32)\n router_weight = router_weight.astype(np.float32)\n gate_proj = gate_proj.astype(np.float32)\n up_proj = up_proj.astype(np.float32)\n down_proj = down_proj.astype(np.float32)\n T = hidden_states.shape[0]\n logits = hidden_states @ router_weight.T\n probs = np.exp(logits - logits.max(-1, keepdims=True))\n probs /= probs.sum(-1, keepdims=True)\n top_idx = np.argsort(probs, axis=-1)[:, -8:]\n top_w = np.take_along_axis(probs, top_idx, axis=-1)\n top_w /= top_w.sum(-1, keepdims=True)\n out = np.zeros((T, 2048), dtype=np.float32)\n for t in range(T):\n for ki in range(8):\n e = top_idx[t, ki]\n g = gate_proj[e] @ hidden_states[t]\n u = up_proj[e] @ hidden_states[t]\n silu = g / (1 + np.exp(-g))\n out[t] += top_w[t, ki] * (down_proj[e] @ (silu * u))\n return out\n"
82
  }
definitions/moe/moe_q4_k_m_e60_k4_d2048_ff1536.json ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "moe_q4_k_m_e60_k4_d2048_ff1536",
3
+ "op_type": "moe",
4
+ "description": "MoE q4_k_m e=60 k=4 d=2048 ff=1536 (ff rounded up from 1408 to 1536, the next multiple of 256, since Q4_K requires the reduction dim divisible by 256; real GGML would fall back to Q5_0 for the unrounded 1408 shape on down_proj)",
5
+ "tags": [
6
+ "status:active",
7
+ "model:qwen1.5-moe-a2.7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "n_tokens": {
12
+ "type": "var"
13
+ },
14
+ "n_embd": {
15
+ "type": "const",
16
+ "value": 2048
17
+ },
18
+ "n_ff": {
19
+ "type": "const",
20
+ "value": 1536
21
+ },
22
+ "n_expert": {
23
+ "type": "const",
24
+ "value": 60
25
+ },
26
+ "n_expert_used": {
27
+ "type": "const",
28
+ "value": 4
29
+ },
30
+ "n_embd_half": {
31
+ "type": "const",
32
+ "value": 1024
33
+ },
34
+ "n_embd_sub": {
35
+ "type": "const",
36
+ "value": 64
37
+ },
38
+ "n_ff_half": {
39
+ "type": "const",
40
+ "value": 768
41
+ },
42
+ "n_ff_sub": {
43
+ "type": "const",
44
+ "value": 48
45
+ }
46
+ },
47
+ "inputs": {
48
+ "hidden_states": {
49
+ "shape": [
50
+ "n_tokens",
51
+ "n_embd"
52
+ ],
53
+ "dtype": "bfloat16"
54
+ },
55
+ "router_weight": {
56
+ "shape": [
57
+ "n_expert",
58
+ "n_embd"
59
+ ],
60
+ "dtype": "float32"
61
+ },
62
+ "gate_proj_q4": {
63
+ "shape": [
64
+ "n_expert",
65
+ "n_ff",
66
+ "n_embd_half"
67
+ ],
68
+ "dtype": "uint8"
69
+ },
70
+ "gate_proj_scales": {
71
+ "shape": [
72
+ "n_expert",
73
+ "n_ff",
74
+ "n_embd_sub"
75
+ ],
76
+ "dtype": "float16"
77
+ },
78
+ "gate_proj_mins": {
79
+ "shape": [
80
+ "n_expert",
81
+ "n_ff",
82
+ "n_embd_sub"
83
+ ],
84
+ "dtype": "float16"
85
+ },
86
+ "up_proj_q4": {
87
+ "shape": [
88
+ "n_expert",
89
+ "n_ff",
90
+ "n_embd_half"
91
+ ],
92
+ "dtype": "uint8"
93
+ },
94
+ "up_proj_scales": {
95
+ "shape": [
96
+ "n_expert",
97
+ "n_ff",
98
+ "n_embd_sub"
99
+ ],
100
+ "dtype": "float16"
101
+ },
102
+ "up_proj_mins": {
103
+ "shape": [
104
+ "n_expert",
105
+ "n_ff",
106
+ "n_embd_sub"
107
+ ],
108
+ "dtype": "float16"
109
+ },
110
+ "down_proj_q4": {
111
+ "shape": [
112
+ "n_expert",
113
+ "n_embd",
114
+ "n_ff_half"
115
+ ],
116
+ "dtype": "uint8"
117
+ },
118
+ "down_proj_scales": {
119
+ "shape": [
120
+ "n_expert",
121
+ "n_embd",
122
+ "n_ff_sub"
123
+ ],
124
+ "dtype": "float16"
125
+ },
126
+ "down_proj_mins": {
127
+ "shape": [
128
+ "n_expert",
129
+ "n_embd",
130
+ "n_ff_sub"
131
+ ],
132
+ "dtype": "float16"
133
+ }
134
+ },
135
+ "outputs": {
136
+ "output": {
137
+ "shape": [
138
+ "n_tokens",
139
+ "n_embd"
140
+ ],
141
+ "dtype": "float32"
142
+ }
143
+ },
144
+ "constraints": [],
145
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(hidden_states, router_weight,\n gate_proj_q4, gate_proj_scales, gate_proj_mins,\n up_proj_q4, up_proj_scales, up_proj_mins,\n down_proj_q4, down_proj_scales, down_proj_mins):\n hs_f = hidden_states.astype(np.float32)\n T = hs_f.shape[0]\n logits = hs_f @ router_weight.astype(np.float32).T\n probs = np.exp(logits - logits.max(-1, keepdims=True))\n probs /= probs.sum(-1, keepdims=True)\n top_idx = np.argsort(probs, axis=-1)[:, -4:]\n top_w = np.take_along_axis(probs, top_idx, axis=-1)\n top_w /= top_w.sum(-1, keepdims=True)\n out = np.zeros((T, 2048), dtype=np.float32)\n for t in range(T):\n for ki in range(4):\n e = top_idx[t, ki]\n gp = dq4k(gate_proj_q4[e], gate_proj_scales[e], gate_proj_mins[e])\n up = dq4k(up_proj_q4[e], up_proj_scales[e], up_proj_mins[e])\n dn = dq4k(down_proj_q4[e], down_proj_scales[e], down_proj_mins[e])\n g = gp @ hs_f[t]; u = up @ hs_f[t]\n silu = g / (1 + np.exp(-g))\n out[t] += top_w[t, ki] * (dn @ (silu * u))\n return out\n"
146
+ }
definitions/moe/moe_q4_k_m_e64_k8_d2048_ff1024.json ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "moe_q4_k_m_e64_k8_d2048_ff1024",
3
+ "op_type": "moe",
4
+ "description": "MoE q4_k_m e=64 k=8 d=2048 ff=1024",
5
+ "tags": [
6
+ "status:active",
7
+ "model:olmoe-1b-7b",
8
+ "baseline-solution:llama.cpp"
9
+ ],
10
+ "axes": {
11
+ "n_tokens": {
12
+ "type": "var"
13
+ },
14
+ "n_embd": {
15
+ "type": "const",
16
+ "value": 2048
17
+ },
18
+ "n_ff": {
19
+ "type": "const",
20
+ "value": 1024
21
+ },
22
+ "n_expert": {
23
+ "type": "const",
24
+ "value": 64
25
+ },
26
+ "n_expert_used": {
27
+ "type": "const",
28
+ "value": 8
29
+ },
30
+ "n_embd_half": {
31
+ "type": "const",
32
+ "value": 1024
33
+ },
34
+ "n_embd_sub": {
35
+ "type": "const",
36
+ "value": 64
37
+ },
38
+ "n_ff_half": {
39
+ "type": "const",
40
+ "value": 512
41
+ },
42
+ "n_ff_sub": {
43
+ "type": "const",
44
+ "value": 32
45
+ }
46
+ },
47
+ "inputs": {
48
+ "hidden_states": {
49
+ "shape": [
50
+ "n_tokens",
51
+ "n_embd"
52
+ ],
53
+ "dtype": "bfloat16"
54
+ },
55
+ "router_weight": {
56
+ "shape": [
57
+ "n_expert",
58
+ "n_embd"
59
+ ],
60
+ "dtype": "float32"
61
+ },
62
+ "gate_proj_q4": {
63
+ "shape": [
64
+ "n_expert",
65
+ "n_ff",
66
+ "n_embd_half"
67
+ ],
68
+ "dtype": "uint8"
69
+ },
70
+ "gate_proj_scales": {
71
+ "shape": [
72
+ "n_expert",
73
+ "n_ff",
74
+ "n_embd_sub"
75
+ ],
76
+ "dtype": "float16"
77
+ },
78
+ "gate_proj_mins": {
79
+ "shape": [
80
+ "n_expert",
81
+ "n_ff",
82
+ "n_embd_sub"
83
+ ],
84
+ "dtype": "float16"
85
+ },
86
+ "up_proj_q4": {
87
+ "shape": [
88
+ "n_expert",
89
+ "n_ff",
90
+ "n_embd_half"
91
+ ],
92
+ "dtype": "uint8"
93
+ },
94
+ "up_proj_scales": {
95
+ "shape": [
96
+ "n_expert",
97
+ "n_ff",
98
+ "n_embd_sub"
99
+ ],
100
+ "dtype": "float16"
101
+ },
102
+ "up_proj_mins": {
103
+ "shape": [
104
+ "n_expert",
105
+ "n_ff",
106
+ "n_embd_sub"
107
+ ],
108
+ "dtype": "float16"
109
+ },
110
+ "down_proj_q4": {
111
+ "shape": [
112
+ "n_expert",
113
+ "n_embd",
114
+ "n_ff_half"
115
+ ],
116
+ "dtype": "uint8"
117
+ },
118
+ "down_proj_scales": {
119
+ "shape": [
120
+ "n_expert",
121
+ "n_embd",
122
+ "n_ff_sub"
123
+ ],
124
+ "dtype": "float16"
125
+ },
126
+ "down_proj_mins": {
127
+ "shape": [
128
+ "n_expert",
129
+ "n_embd",
130
+ "n_ff_sub"
131
+ ],
132
+ "dtype": "float16"
133
+ }
134
+ },
135
+ "outputs": {
136
+ "output": {
137
+ "shape": [
138
+ "n_tokens",
139
+ "n_embd"
140
+ ],
141
+ "dtype": "float32"
142
+ }
143
+ },
144
+ "constraints": [],
145
+ "reference": "import numpy as np\n\n\ndef dq4k(q4, scales, mins):\n K = q4.shape[-1] * 2\n lo = (q4 & 0x0F).astype(np.float32)\n hi = (q4 >> 4).astype(np.float32)\n nib = np.empty(q4.shape[:-1] + (K,), dtype=np.float32)\n nib[..., 0::2] = lo\n nib[..., 1::2] = hi\n s = np.repeat(scales.astype(np.float32), 32, axis=-1)\n m = np.repeat(mins.astype(np.float32), 32, axis=-1)\n return s * nib - m\n\n\ndef run(hidden_states, router_weight,\n gate_proj_q4, gate_proj_scales, gate_proj_mins,\n up_proj_q4, up_proj_scales, up_proj_mins,\n down_proj_q4, down_proj_scales, down_proj_mins):\n hs_f = hidden_states.astype(np.float32)\n T = hs_f.shape[0]\n logits = hs_f @ router_weight.astype(np.float32).T\n probs = np.exp(logits - logits.max(-1, keepdims=True))\n probs /= probs.sum(-1, keepdims=True)\n top_idx = np.argsort(probs, axis=-1)[:, -8:]\n top_w = np.take_along_axis(probs, top_idx, axis=-1)\n top_w /= top_w.sum(-1, keepdims=True)\n out = np.zeros((T, 2048), dtype=np.float32)\n for t in range(T):\n for ki in range(8):\n e = top_idx[t, ki]\n gp = dq4k(gate_proj_q4[e], gate_proj_scales[e], gate_proj_mins[e])\n up = dq4k(up_proj_q4[e], up_proj_scales[e], up_proj_mins[e])\n dn = dq4k(down_proj_q4[e], down_proj_scales[e], down_proj_mins[e])\n g = gp @ hs_f[t]; u = up @ hs_f[t]\n silu = g / (1 + np.exp(-g))\n out[t] += top_w[t, ki] * (dn @ (silu * u))\n return out\n"
146
+ }
definitions/rms_norm/{rms_norm_fp32_d2048.json → rms_norm_bf16_d2048.json} RENAMED
@@ -1,5 +1,5 @@
1
  {
2
- "name": "rms_norm_fp32_d2048",
3
  "op_type": "rms_norm",
4
  "description": "RMSNorm D=2048",
5
  "tags": [
@@ -22,13 +22,13 @@
22
  "M",
23
  "D"
24
  ],
25
- "dtype": "float32"
26
  },
27
  "weight": {
28
  "shape": [
29
  "D"
30
  ],
31
- "dtype": "float32"
32
  }
33
  },
34
  "outputs": {
@@ -41,5 +41,5 @@
41
  }
42
  },
43
  "constraints": [],
44
- "reference": "import numpy as np\ndef run(x, weight):\n rms = np.sqrt(np.mean(x ** 2, axis=-1, keepdims=True) + 1e-6)\n return (x / rms) * weight\n"
45
  }
 
1
  {
2
+ "name": "rms_norm_bf16_d2048",
3
  "op_type": "rms_norm",
4
  "description": "RMSNorm D=2048",
5
  "tags": [
 
22
  "M",
23
  "D"
24
  ],
25
+ "dtype": "bfloat16"
26
  },
27
  "weight": {
28
  "shape": [
29
  "D"
30
  ],
31
+ "dtype": "bfloat16"
32
  }
33
  },
34
  "outputs": {
 
41
  }
42
  },
43
  "constraints": [],
44
+ "reference": "import numpy as np\ndef run(x, weight):\n x = x.astype(np.float32)\n weight = weight.astype(np.float32)\n rms = np.sqrt(np.mean(x ** 2, axis=-1, keepdims=True) + 1e-6)\n return (x / rms) * weight\n"
45
  }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n1024_k2048.json → gemm_bf16_n1024_k2048.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_gemm_fp32_n1024_k2048",
3
- "definition": "gemm_fp32_n1024_k2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n1024_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1024;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_gemm_bf16_n1024_k2048",
3
+ "definition": "gemm_bf16_n1024_k2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n1024_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For the bf16 variant, A and B are raw bf16 (uint16_t) bit patterns.\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1024;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B bf16 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n1408_k2048.json → gemm_bf16_n1408_k2048.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_gemm_fp32_n1408_k2048",
3
- "definition": "gemm_fp32_n1408_k2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n1408_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1408;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_gemm_bf16_n1408_k2048",
3
+ "definition": "gemm_bf16_n1408_k2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n1408_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For the bf16 variant, A and B are raw bf16 (uint16_t) bit patterns.\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1408;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B bf16 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n2048_k1024.json → gemm_bf16_n2048_k1024.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_gemm_fp32_n2048_k1024",
3
- "definition": "gemm_fp32_n2048_k1024",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n2048_k1024. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1024;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_gemm_bf16_n2048_k1024",
3
+ "definition": "gemm_bf16_n2048_k1024",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n2048_k1024. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For the bf16 variant, A and B are raw bf16 (uint16_t) bit patterns.\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1024;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B bf16 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n2048_k1408.json → gemm_bf16_n2048_k1408.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_gemm_fp32_n2048_k1408",
3
- "definition": "gemm_fp32_n2048_k1408",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n2048_k1408. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1408;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_gemm_bf16_n2048_k1408",
3
+ "definition": "gemm_bf16_n2048_k1408",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n2048_k1408. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For the bf16 variant, A and B are raw bf16 (uint16_t) bit patterns.\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1408;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B bf16 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/{gemm_fp32_n2048_k2048.json → gemm_bf16_n2048_k2048.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_gemm_fp32_n2048_k2048",
3
- "definition": "gemm_fp32_n2048_k2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n2048_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_gemm_bf16_n2048_k2048",
3
+ "definition": "gemm_bf16_n2048_k2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for gemm_fp32_n2048_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A \u00b7 B^T row-major [M, N].\n// For the bf16 variant, A and B are raw bf16 (uint16_t) bit patterns.\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B bf16 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// C = A \u00b7 B^T via ggml_mul_mat(B, A) \u2014 ggml computes result[n, m] =\n// B_row_n \u00b7 A_row_m over ne0=K, which in ggml's [ne0=N, ne1=M] layout is\n// exactly row-major C[M, N]. For q8_0 both operands are already in ggml\n// block_q8_0 layout, so mul_mat's vec_dot uses the supplied quantization\n// directly (src1->type == vec_dot_type \u2014 no re-quantization of A).\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8)\n{\n const ggml_type qt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n\n // Input tensors: metadata only, data points at the adapter-owned buffers.\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, qt, K, M);\n a->data = const_cast<void*>(A);\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, qt, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack (cplan work buffer, alignment)\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] \u2192 row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n1024_k2048.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_gemm_q4_k_m_n1024_k2048",
3
+ "definition": "gemm_q4_k_m_n1024_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for gemm_q4_k_m_n1024_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_gemm"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A . B^T row-major [M, N].\n// A is bf16 (raw uint16 bit pattern, one value per K element -- NOT\n// block-quantized; weight-only Q4_K design keeps activations at the bf16\n// baseline tier). B is a ggml block_q4_K row: (K/256) 144-byte blocks per\n// row, {fp16 d; fp16 dmin; uint8 scales[12]; uint8 qs[128]} (repacked by the\n// Python adapter from the flat nibble/scale/min Definition tensors).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1024;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B block_q4_K [N, K/256 blocks],\n// [2]=NULL (B_scales, consumed by adapter repack), [3]=NULL (B_mins);\n// var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// C = A . B^T via ggml_mul_mat(B, A). B is a real Q4_K weight tensor; A\n// arrives as bf16 and is widened to F32 up front (plain scalar loop, outside\n// ggml) because ggml_compute_forward_mul_mat asserts src1->type == F32\n// whenever src0 is a k-quant type (Q4_K's paired vec_dot_type is Q8_K, not\n// bf16 or Q4_K itself). ggml's own library code then dynamically quantizes\n// this F32 activation to Q8_K internally -- nothing else to write here.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A_bf16, const void* B, float* C,\n int64_t M, int64_t N, int64_t K)\n{\n std::vector<float> A_f32((size_t)M * K);\n const uint16_t* a_bits = reinterpret_cast<const uint16_t*>(A_bf16);\n for (size_t i = 0; i < A_f32.size(); ++i) A_f32[i] = bf16_to_f32(a_bits[i]);\n\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, K, M);\n a->data = A_f32.data();\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, GGML_TYPE_Q4_K, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + (size_t)M * K * sizeof(float) * 2 // cplan: A -> Q8_K scratch (generous)\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] -> row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n1408_k2048.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_gemm_q4_k_m_n1408_k2048",
3
+ "definition": "gemm_q4_k_m_n1408_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for gemm_q4_k_m_n1408_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_gemm"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A . B^T row-major [M, N].\n// A is bf16 (raw uint16 bit pattern, one value per K element -- NOT\n// block-quantized; weight-only Q4_K design keeps activations at the bf16\n// baseline tier). B is a ggml block_q4_K row: (K/256) 144-byte blocks per\n// row, {fp16 d; fp16 dmin; uint8 scales[12]; uint8 qs[128]} (repacked by the\n// Python adapter from the flat nibble/scale/min Definition tensors).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1408;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B block_q4_K [N, K/256 blocks],\n// [2]=NULL (B_scales, consumed by adapter repack), [3]=NULL (B_mins);\n// var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// C = A . B^T via ggml_mul_mat(B, A). B is a real Q4_K weight tensor; A\n// arrives as bf16 and is widened to F32 up front (plain scalar loop, outside\n// ggml) because ggml_compute_forward_mul_mat asserts src1->type == F32\n// whenever src0 is a k-quant type (Q4_K's paired vec_dot_type is Q8_K, not\n// bf16 or Q4_K itself). ggml's own library code then dynamically quantizes\n// this F32 activation to Q8_K internally -- nothing else to write here.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A_bf16, const void* B, float* C,\n int64_t M, int64_t N, int64_t K)\n{\n std::vector<float> A_f32((size_t)M * K);\n const uint16_t* a_bits = reinterpret_cast<const uint16_t*>(A_bf16);\n for (size_t i = 0; i < A_f32.size(); ++i) A_f32[i] = bf16_to_f32(a_bits[i]);\n\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, K, M);\n a->data = A_f32.data();\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, GGML_TYPE_Q4_K, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + (size_t)M * K * sizeof(float) * 2 // cplan: A -> Q8_K scratch (generous)\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] -> row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n2048_k1024.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_gemm_q4_k_m_n2048_k1024",
3
+ "definition": "gemm_q4_k_m_n2048_k1024",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for gemm_q4_k_m_n2048_k1024. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_gemm"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A . B^T row-major [M, N].\n// A is bf16 (raw uint16 bit pattern, one value per K element -- NOT\n// block-quantized; weight-only Q4_K design keeps activations at the bf16\n// baseline tier). B is a ggml block_q4_K row: (K/256) 144-byte blocks per\n// row, {fp16 d; fp16 dmin; uint8 scales[12]; uint8 qs[128]} (repacked by the\n// Python adapter from the flat nibble/scale/min Definition tensors).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1024;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B block_q4_K [N, K/256 blocks],\n// [2]=NULL (B_scales, consumed by adapter repack), [3]=NULL (B_mins);\n// var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// C = A . B^T via ggml_mul_mat(B, A). B is a real Q4_K weight tensor; A\n// arrives as bf16 and is widened to F32 up front (plain scalar loop, outside\n// ggml) because ggml_compute_forward_mul_mat asserts src1->type == F32\n// whenever src0 is a k-quant type (Q4_K's paired vec_dot_type is Q8_K, not\n// bf16 or Q4_K itself). ggml's own library code then dynamically quantizes\n// this F32 activation to Q8_K internally -- nothing else to write here.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A_bf16, const void* B, float* C,\n int64_t M, int64_t N, int64_t K)\n{\n std::vector<float> A_f32((size_t)M * K);\n const uint16_t* a_bits = reinterpret_cast<const uint16_t*>(A_bf16);\n for (size_t i = 0; i < A_f32.size(); ++i) A_f32[i] = bf16_to_f32(a_bits[i]);\n\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, K, M);\n a->data = A_f32.data();\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, GGML_TYPE_Q4_K, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + (size_t)M * K * sizeof(float) * 2 // cplan: A -> Q8_K scratch (generous)\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] -> row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n2048_k1536.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_gemm_q4_k_m_n2048_k1536",
3
+ "definition": "gemm_q4_k_m_n2048_k1536",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for gemm_q4_k_m_n2048_k1536. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_gemm"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A . B^T row-major [M, N].\n// A is bf16 (raw uint16 bit pattern, one value per K element -- NOT\n// block-quantized; weight-only Q4_K design keeps activations at the bf16\n// baseline tier). B is a ggml block_q4_K row: (K/256) 144-byte blocks per\n// row, {fp16 d; fp16 dmin; uint8 scales[12]; uint8 qs[128]} (repacked by the\n// Python adapter from the flat nibble/scale/min Definition tensors).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1536;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B block_q4_K [N, K/256 blocks],\n// [2]=NULL (B_scales, consumed by adapter repack), [3]=NULL (B_mins);\n// var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// C = A . B^T via ggml_mul_mat(B, A). B is a real Q4_K weight tensor; A\n// arrives as bf16 and is widened to F32 up front (plain scalar loop, outside\n// ggml) because ggml_compute_forward_mul_mat asserts src1->type == F32\n// whenever src0 is a k-quant type (Q4_K's paired vec_dot_type is Q8_K, not\n// bf16 or Q4_K itself). ggml's own library code then dynamically quantizes\n// this F32 activation to Q8_K internally -- nothing else to write here.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A_bf16, const void* B, float* C,\n int64_t M, int64_t N, int64_t K)\n{\n std::vector<float> A_f32((size_t)M * K);\n const uint16_t* a_bits = reinterpret_cast<const uint16_t*>(A_bf16);\n for (size_t i = 0; i < A_f32.size(); ++i) A_f32[i] = bf16_to_f32(a_bits[i]);\n\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, K, M);\n a->data = A_f32.data();\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, GGML_TYPE_Q4_K, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + (size_t)M * K * sizeof(float) * 2 // cplan: A -> Q8_K scratch (generous)\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] -> row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/gemm/gemm_q4_k_m_n2048_k2048.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_gemm_q4_k_m_n2048_k2048",
3
+ "definition": "gemm_q4_k_m_n2048_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for gemm_q4_k_m_n2048_k2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_gemm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_gemm"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "gemm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A . B^T row-major [M, N].\n// A is bf16 (raw uint16 bit pattern, one value per K element -- NOT\n// block-quantized; weight-only Q4_K design keeps activations at the bf16\n// baseline tier). B is a ggml block_q4_K row: (K/256) 144-byte blocks per\n// row, {fp16 d; fp16 dmin; uint8 scales[12]; uint8 qs[128]} (repacked by the\n// Python adapter from the flat nibble/scale/min Definition tensors).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A bf16 [M,K], [1]=B block_q4_K [N, K/256 blocks],\n// [2]=NULL (B_scales, consumed by adapter repack), [3]=NULL (B_mins);\n// var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"gemm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// C = A . B^T via ggml_mul_mat(B, A). B is a real Q4_K weight tensor; A\n// arrives as bf16 and is widened to F32 up front (plain scalar loop, outside\n// ggml) because ggml_compute_forward_mul_mat asserts src1->type == F32\n// whenever src0 is a k-quant type (Q4_K's paired vec_dot_type is Q8_K, not\n// bf16 or Q4_K itself). ggml's own library code then dynamically quantizes\n// this F32 activation to Q8_K internally -- nothing else to write here.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_gemm(const void* A_bf16, const void* B, float* C,\n int64_t M, int64_t N, int64_t K)\n{\n std::vector<float> A_f32((size_t)M * K);\n const uint16_t* a_bits = reinterpret_cast<const uint16_t*>(A_bf16);\n for (size_t i = 0; i < A_f32.size(); ++i) A_f32[i] = bf16_to_f32(a_bits[i]);\n\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* a = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, K, M);\n a->data = A_f32.data();\n ggml_tensor* b = ggml_new_tensor_2d(ctx_in, GGML_TYPE_Q4_K, K, N);\n b->data = const_cast<void*>(B);\n\n const size_t mem =\n (size_t)N * M * sizeof(float) // C\n + (size_t)M * K * sizeof(float) * 2 // cplan: A -> Q8_K scratch (generous)\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20); // slack\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* c = ggml_mul_mat(ctx, b, a); // [N, M] -> row-major [M, N]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, c);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(C, c->data, (size_t)N * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/mha/{mha_fp32_h16_d128_kvh16.json → mha_bf16_h16_d128_kvh16.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_mha_fp32_h16_d128_kvh16",
3
- "definition": "mha_fp32_h16_d128_kvh16",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for mha_fp32_h16_d128_kvh16. binding.cpp bakes the const axes as constexpr and implements armbench_entry_mha over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "mha.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) mha baseline.\n// Called by armbench_entry_mha (binding.cpp); implemented by kernel.cpp.\n// Q is row-major fp32 [M, n_heads, head_dim]; K/V are row-major fp32\n// [S, kv_heads, head_dim]; out is row-major fp32 [M, n_heads, head_dim].\n// Non-causal softmax(Q\u00b7K^T / sqrt(head_dim))\u00b7V per head.\nint armbench_llamacpp_mha(const float* Q, const float* K, const float* V,\n float* out, int64_t M, int64_t S,\n int64_t n_heads, int64_t head_dim, int64_t kv_heads);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"mha.h\"\n\nnamespace {\nconstexpr int64_t kNHeads = 16;\nconstexpr int64_t kHeadDim = 128;\nconstexpr int64_t kKvHeads = 16;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=Q fp32 [M,n_heads,head_dim], [1]=K fp32 [S,kv_heads,head_dim],\n// [2]=V fp32 [S,kv_heads,head_dim]; var_axes: [0]=M, [1]=S\nint armbench_entry_mha(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_mha(\n reinterpret_cast<const float*>(inputs[0]),\n reinterpret_cast<const float*>(inputs[1]),\n reinterpret_cast<const float*>(inputs[2]),\n reinterpret_cast<float*>(output),\n var_axes[0], var_axes[1], kNHeads, kHeadDim, kKvHeads);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"mha.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cmath>\n#include <cstring>\n\n// The non-flash attention path from llama.cpp's build_attn_mha:\n// kq = ggml_mul_mat(k, q) // [S, M, h]\n// kq = ggml_soft_max_ext(kq, mask=null, 1/sqrt(d), 0) // non-causal\n// kqv = ggml_mul_mat(cont(permute(v)), kq) // [d, M, h]\n// out = cont(permute(kqv)) // [d, h, M] = row-major [M, h, d]\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_mha(const float* Q, const float* K, const float* V,\n float* out, int64_t M, int64_t S,\n int64_t n_heads, int64_t head_dim, int64_t kv_heads)\n{\n const int64_t d = head_dim, h = n_heads, kvh = kv_heads;\n\n ggml_init_params ip_in = { 6 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n // ggml ne order is reversed vs numpy: [M, h, d] row-major \u2192 ne = [d, h, M].\n ggml_tensor* q = ggml_new_tensor_3d(ctx_in, GGML_TYPE_F32, d, h, M);\n q->data = const_cast<float*>(Q);\n ggml_tensor* k = ggml_new_tensor_3d(ctx_in, GGML_TYPE_F32, d, kvh, S);\n k->data = const_cast<float*>(K);\n ggml_tensor* v = ggml_new_tensor_3d(ctx_in, GGML_TYPE_F32, d, kvh, S);\n v->data = const_cast<float*>(V);\n\n const size_t mem =\n 2 * (size_t)S * M * h * sizeof(float) // kq + softmax\n + (size_t)S * d * kvh * sizeof(float) // v transposed (cont)\n + 2 * (size_t)d * M * h * sizeof(float) // kqv + merged\n + 32 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* qp = ggml_permute(ctx, q, 0, 2, 1, 3); // [d, M, h]\n ggml_tensor* kp = ggml_permute(ctx, k, 0, 2, 1, 3); // [d, S, kvh]\n ggml_tensor* kq = ggml_mul_mat(ctx, kp, qp); // [S, M, h]\n kq = ggml_soft_max_ext(ctx, kq, nullptr,\n 1.0f / sqrtf((float)d), 0.0f); // non-causal\n ggml_tensor* vp = ggml_cont(ctx, ggml_permute(ctx, v, 1, 2, 0, 3)); // [S, d, kvh]\n ggml_tensor* kqv = ggml_mul_mat(ctx, vp, kq); // [d, M, h]\n ggml_tensor* merged =\n ggml_cont(ctx, ggml_permute(ctx, kqv, 0, 2, 1, 3)); // [d, h, M]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, merged);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, merged->data, (size_t)d * h * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_mha_bf16_h16_d128_kvh16",
3
+ "definition": "mha_bf16_h16_d128_kvh16",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for mha_fp32_h16_d128_kvh16. binding.cpp bakes the const axes as constexpr and implements armbench_entry_mha over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "mha.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) mha baseline.\n// Called by armbench_entry_mha (binding.cpp); implemented by kernel.cpp.\n// Q is row-major bf16 [M, n_heads, head_dim]; K/V are row-major bf16\n// [S, kv_heads, head_dim]; out is row-major fp32 [M, n_heads, head_dim].\n// Non-causal softmax(Q\u00b7K^T / sqrt(head_dim))\u00b7V per head.\nint armbench_llamacpp_mha(const void* Q, const void* K, const void* V,\n float* out, int64_t M, int64_t S,\n int64_t n_heads, int64_t head_dim, int64_t kv_heads);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"mha.h\"\n\nnamespace {\nconstexpr int64_t kNHeads = 16;\nconstexpr int64_t kHeadDim = 128;\nconstexpr int64_t kKvHeads = 16;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=Q bf16 [M,n_heads,head_dim], [1]=K bf16 [S,kv_heads,head_dim],\n// [2]=V bf16 [S,kv_heads,head_dim]; var_axes: [0]=M, [1]=S\nint armbench_entry_mha(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_mha(\n inputs[0],\n inputs[1],\n inputs[2],\n reinterpret_cast<float*>(output),\n var_axes[0], var_axes[1], kNHeads, kHeadDim, kKvHeads);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"mha.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cmath>\n#include <cstring>\n\n// The non-flash attention path from llama.cpp's build_attn_mha:\n// kq = ggml_mul_mat(k, q) // [S, M, h]\n// kq = ggml_soft_max_ext(kq, mask=null, 1/sqrt(d), 0) // non-causal\n// kqv = ggml_mul_mat(cont(permute(v)), kq) // [d, M, h]\n// out = cont(permute(kqv)) // [d, h, M] = row-major [M, h, d]\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_mha(const void* Q, const void* K, const void* V,\n float* out, int64_t M, int64_t S,\n int64_t n_heads, int64_t head_dim, int64_t kv_heads)\n{\n const int64_t d = head_dim, h = n_heads, kvh = kv_heads;\n\n ggml_init_params ip_in = { 6 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n // ggml ne order is reversed vs numpy: [M, h, d] row-major \u2192 ne = [d, h, M].\n ggml_tensor* q = ggml_new_tensor_3d(ctx_in, GGML_TYPE_BF16, d, h, M);\n q->data = const_cast<void*>(Q);\n ggml_tensor* k = ggml_new_tensor_3d(ctx_in, GGML_TYPE_BF16, d, kvh, S);\n k->data = const_cast<void*>(K);\n ggml_tensor* v = ggml_new_tensor_3d(ctx_in, GGML_TYPE_BF16, d, kvh, S);\n v->data = const_cast<void*>(V);\n\n const size_t mem =\n 2 * (size_t)S * M * h * sizeof(float) // kq + softmax\n + (size_t)S * d * kvh * sizeof(float) // v transposed (cont)\n + 2 * (size_t)d * M * h * sizeof(float) // kqv + merged\n + 32 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* qp = ggml_permute(ctx, q, 0, 2, 1, 3); // [d, M, h]\n ggml_tensor* kp = ggml_permute(ctx, k, 0, 2, 1, 3); // [d, S, kvh]\n ggml_tensor* kq = ggml_mul_mat(ctx, kp, qp); // [S, M, h]\n kq = ggml_soft_max_ext(ctx, kq, nullptr,\n 1.0f / sqrtf((float)d), 0.0f); // non-causal\n ggml_tensor* vp = ggml_cont(ctx, ggml_permute(ctx, v, 1, 2, 0, 3)); // [S, d, kvh]\n ggml_tensor* kqv = ggml_mul_mat(ctx, vp, kq); // [d, M, h]\n ggml_tensor* merged =\n ggml_cont(ctx, ggml_permute(ctx, kqv, 0, 2, 1, 3)); // [d, h, M]\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, merged);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, merged->data, (size_t)d * h * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/moe/{moe_fp32_e60_k4_d2048_ff1408.json → moe_bf16_e60_k4_d2048_ff1408.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_moe_fp32_e60_k4_d2048_ff1408",
3
- "definition": "moe_fp32_e60_k4_d2048_ff1408",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for moe_fp32_e60_k4_d2048_ff1408. binding.cpp bakes the const axes as constexpr and implements armbench_entry_moe over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "moe.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) moe baseline.\n// Called by armbench_entry_moe (binding.cpp); implemented by kernel.cpp.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states is row-major [T, n_embd]; router_weight fp32 [n_expert, n_embd];\n// gate/up_proj [n_expert, n_ff, n_embd]; down_proj [n_expert, n_embd, n_ff];\n// out fp32 [T, n_embd]. For q8_0, hidden_states and the three projections are\n// ggml block_q8_0 rows over their last axis (34-byte {fp16 d; int8 qs[32]}\n// blocks, repacked by the Python adapter); router_weight stays fp32.\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"moe.h\"\n\nnamespace {\nconstexpr int64_t kNEmbd = 2048;\nconstexpr int64_t kNFf = 1408;\nconstexpr int64_t kNExpert = 60;\nconstexpr int64_t kNExpertUsed = 4;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=hidden_states fp32 [T,n_embd], [1]=router_weight fp32\n// [n_expert,n_embd], [2]=gate_proj fp32 [n_expert,n_ff,n_embd], [3]=up_proj\n// fp32 [n_expert,n_ff,n_embd], [4]=down_proj fp32 [n_expert,n_embd,n_ff];\n// var_axes: [0]=n_tokens\nint armbench_entry_moe(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_moe(\n inputs[0],\n reinterpret_cast<const float*>(inputs[1]),\n inputs[2], inputs[3], inputs[4],\n reinterpret_cast<float*>(output),\n var_axes[0], kNEmbd, kNFf, kNExpert, kNExpertUsed, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"moe.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\nnamespace {\n\n// Dequantize block_q8_0 rows (34-byte {fp16 d; int8 qs[32]} blocks) to fp32.\n// Used only for the router matmul input in the q8_0 variant \u2014 the reference\n// routes on the dequantized hidden states while the expert matmuls consume the\n// original quantization.\nvoid dequant_q8_0_rows(const void* src, float* dst, int64_t rows, int64_t k)\n{\n const int64_t nb = k / 32;\n const uint8_t* p = reinterpret_cast<const uint8_t*>(src);\n for (int64_t r = 0; r < rows; ++r) {\n const uint8_t* rp = p + (size_t)r * nb * 34;\n float* dp = dst + (size_t)r * k;\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = rp + (size_t)b * 34;\n uint16_t dbits;\n std::memcpy(&dbits, bp, sizeof(dbits));\n const float d = ggml_fp16_to_fp32(dbits);\n const int8_t* qs = reinterpret_cast<const int8_t*>(bp + 2);\n for (int j = 0; j < 32; ++j) dp[b * 32 + j] = d * (float)qs[j];\n }\n }\n}\n\n} // namespace\n\n// The build_moe_ffn sequence from llama.cpp (SOFTMAX gating + norm_w, the\n// qwen1.5-moe / olmoe configuration): softmax over all experts \u2192\n// ggml_argsort_top_k \u2192 ggml_get_rows \u2192 weight renorm \u2192 ggml_mul_mat_id\n// gate/up \u2192 silu\u00b7up \u2192 ggml_mul_mat_id down \u2192 weight and sum the expert views.\n//\n// For q8_0, gate/up consume the pre-quantized hidden states directly\n// (src1->type == vec_dot_type, so ggml does NOT re-quantize them); the down\n// projection's fp32 intermediate is quantized to q8_0 by ggml internally \u2014\n// that is llama.cpp's real inference behavior and the one place this baseline\n// deviates from the fp32-intermediate numpy reference.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8)\n{\n const int64_t T = n_tokens, d = n_embd, ff = n_ff;\n const int64_t e = n_expert, k = n_expert_used;\n const ggml_type wt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n const size_t f = sizeof(float);\n\n ggml_init_params ip_in = { 8 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* hs = ggml_new_tensor_2d(ctx_in, wt, d, T);\n hs->data = const_cast<void*>(hidden_states);\n ggml_tensor* router = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, d, e);\n router->data = const_cast<float*>(router_weight);\n ggml_tensor* gate = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n gate->data = const_cast<void*>(gate_proj);\n ggml_tensor* up = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n up->data = const_cast<void*>(up_proj);\n ggml_tensor* down = ggml_new_tensor_3d(ctx_in, wt, ff, d, e);\n down->data = const_cast<void*>(down_proj);\n\n size_t mem = 0;\n mem += 3 * (size_t)e * T * f; // logits + probs + argsort indices\n mem += 8 * (size_t)k * T * f; // selected ids, weights, sums, renorm\n mem += 4 * (size_t)ff * k * T * f; // gate, up, silu, gated product\n mem += 2 * (size_t)d * k * T * f; // expert outputs + weighted\n mem += (size_t)k * d * T * f; // expert-sum add chain\n if (is_q8) {\n mem += (size_t)d * T * f; // dequantized hidden states\n mem += (size_t)ff * k * T * 34 / 32 + 64; // cplan: down-proj src1 \u2192 q8_0\n }\n mem += (size_t)e * ((size_t)k * T * 16 + 256); // cplan: mul_mat_id row maps\n mem += 64 * ggml_tensor_overhead() + ggml_graph_overhead();\n mem += (4u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n // Router input: fp32 hidden states (dequantized for the q8_0 variant).\n ggml_tensor* cur_f;\n if (is_q8) {\n cur_f = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, T);\n dequant_q8_0_rows(hidden_states, reinterpret_cast<float*>(cur_f->data), T, d);\n } else {\n cur_f = hs;\n }\n\n ggml_tensor* logits = ggml_mul_mat(ctx, router, cur_f); // [e, T]\n ggml_tensor* probs = ggml_soft_max(ctx, logits); // [e, T]\n ggml_tensor* sel = ggml_argsort_top_k(ctx, probs, k); // [k, T] i32\n\n ggml_tensor* weights =\n ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, e, T), sel); // [1, k, T]\n weights = ggml_reshape_2d(ctx, weights, k, T);\n ggml_tensor* wsum = ggml_sum_rows(ctx, weights); // [1, T]\n weights = ggml_div(ctx, weights, wsum); // renormalize\n weights = ggml_reshape_3d(ctx, weights, 1, k, T);\n\n // Expert matmuls consume the supplied quantization directly for q8_0.\n ggml_tensor* cur = ggml_reshape_3d(ctx, is_q8 ? hs : cur_f, d, 1, T);\n ggml_tensor* g = ggml_mul_mat_id(ctx, gate, cur, sel); // [ff, k, T]\n ggml_tensor* u = ggml_mul_mat_id(ctx, up, cur, sel); // [ff, k, T]\n ggml_tensor* par = ggml_mul(ctx, ggml_silu(ctx, g), u); // [ff, k, T]\n ggml_tensor* experts = ggml_mul_mat_id(ctx, down, par, sel); // [d, k, T]\n experts = ggml_mul(ctx, experts, weights);\n\n // Sum over the k expert slots (view + add chain, as in build_moe_ffn).\n ggml_tensor* moe_out = ggml_view_2d(ctx, experts, d, T, experts->nb[2], 0);\n for (int64_t i = 1; i < k; ++i) {\n moe_out = ggml_add(\n ctx, moe_out,\n ggml_view_2d(ctx, experts, d, T, experts->nb[2], i * experts->nb[1]));\n }\n if (k == 1) moe_out = ggml_cont(ctx, moe_out);\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, moe_out);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, moe_out->data, (size_t)d * T * f);\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_moe_bf16_e60_k4_d2048_ff1408",
3
+ "definition": "moe_bf16_e60_k4_d2048_ff1408",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for moe_fp32_e60_k4_d2048_ff1408. binding.cpp bakes the const axes as constexpr and implements armbench_entry_moe over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "moe.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) moe baseline.\n// Called by armbench_entry_moe (binding.cpp); implemented by kernel.cpp.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states is row-major bf16 [T, n_embd]; router_weight bf16 [n_expert, n_embd];\n// gate/up_proj bf16 [n_expert, n_ff, n_embd]; down_proj bf16 [n_expert, n_embd, n_ff];\n// out fp32 [T, n_embd]. For q8_0, hidden_states and the three projections are\n// ggml block_q8_0 rows over their last axis (34-byte {fp16 d; int8 qs[32]}\n// blocks, repacked by the Python adapter); router_weight stays fp32 there.\nint armbench_llamacpp_moe(const void* hidden_states, const void* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"moe.h\"\n\nnamespace {\nconstexpr int64_t kNEmbd = 2048;\nconstexpr int64_t kNFf = 1408;\nconstexpr int64_t kNExpert = 60;\nconstexpr int64_t kNExpertUsed = 4;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=hidden_states bf16 [T,n_embd], [1]=router_weight bf16\n// [n_expert,n_embd], [2]=gate_proj bf16 [n_expert,n_ff,n_embd], [3]=up_proj\n// bf16 [n_expert,n_ff,n_embd], [4]=down_proj bf16 [n_expert,n_embd,n_ff];\n// var_axes: [0]=n_tokens\nint armbench_entry_moe(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_moe(\n inputs[0],\n inputs[1],\n inputs[2], inputs[3], inputs[4],\n reinterpret_cast<float*>(output),\n var_axes[0], kNEmbd, kNFf, kNExpert, kNExpertUsed, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"moe.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\nnamespace {\n\n// Dequantize block_q8_0 rows (34-byte {fp16 d; int8 qs[32]} blocks) to fp32.\n// Used only for the router matmul input in the q8_0 variant \u2014 the reference\n// routes on the dequantized hidden states while the expert matmuls consume the\n// original quantization.\nvoid dequant_q8_0_rows(const void* src, float* dst, int64_t rows, int64_t k)\n{\n const int64_t nb = k / 32;\n const uint8_t* p = reinterpret_cast<const uint8_t*>(src);\n for (int64_t r = 0; r < rows; ++r) {\n const uint8_t* rp = p + (size_t)r * nb * 34;\n float* dp = dst + (size_t)r * k;\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = rp + (size_t)b * 34;\n uint16_t dbits;\n std::memcpy(&dbits, bp, sizeof(dbits));\n const float d = ggml_fp16_to_fp32(dbits);\n const int8_t* qs = reinterpret_cast<const int8_t*>(bp + 2);\n for (int j = 0; j < 32; ++j) dp[b * 32 + j] = d * (float)qs[j];\n }\n }\n}\n\n} // namespace\n\n// The build_moe_ffn sequence from llama.cpp (SOFTMAX gating + norm_w, the\n// qwen1.5-moe / olmoe configuration): softmax over all experts \u2192\n// ggml_argsort_top_k \u2192 ggml_get_rows \u2192 weight renorm \u2192 ggml_mul_mat_id\n// gate/up \u2192 silu\u00b7up \u2192 ggml_mul_mat_id down \u2192 weight and sum the expert views.\n//\n// For q8_0, gate/up consume the pre-quantized hidden states directly\n// (src1->type == vec_dot_type, so ggml does NOT re-quantize them); the down\n// projection's fp32 intermediate is quantized to q8_0 by ggml internally \u2014\n// that is llama.cpp's real inference behavior and the one place this baseline\n// deviates from the fp32-intermediate numpy reference.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_moe(const void* hidden_states, const void* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8)\n{\n const int64_t T = n_tokens, d = n_embd, ff = n_ff;\n const int64_t e = n_expert, k = n_expert_used;\n const ggml_type wt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n const size_t f = sizeof(float);\n\n ggml_init_params ip_in = { 8 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* hs = ggml_new_tensor_2d(ctx_in, wt, d, T);\n hs->data = const_cast<void*>(hidden_states);\n ggml_tensor* router = ggml_new_tensor_2d(ctx_in, GGML_TYPE_BF16, d, e);\n router->data = const_cast<void*>(router_weight);\n ggml_tensor* gate = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n gate->data = const_cast<void*>(gate_proj);\n ggml_tensor* up = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n up->data = const_cast<void*>(up_proj);\n ggml_tensor* down = ggml_new_tensor_3d(ctx_in, wt, ff, d, e);\n down->data = const_cast<void*>(down_proj);\n\n size_t mem = 0;\n mem += 3 * (size_t)e * T * f; // logits + probs + argsort indices\n mem += 8 * (size_t)k * T * f; // selected ids, weights, sums, renorm\n mem += 4 * (size_t)ff * k * T * f; // gate, up, silu, gated product\n mem += 2 * (size_t)d * k * T * f; // expert outputs + weighted\n mem += (size_t)k * d * T * f; // expert-sum add chain\n if (is_q8) {\n mem += (size_t)d * T * f; // dequantized hidden states\n mem += (size_t)ff * k * T * 34 / 32 + 64; // cplan: down-proj src1 \u2192 q8_0\n }\n mem += (size_t)e * ((size_t)k * T * 16 + 256); // cplan: mul_mat_id row maps\n mem += 64 * ggml_tensor_overhead() + ggml_graph_overhead();\n mem += (4u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n // Router input: fp32 hidden states (dequantized for the q8_0 variant).\n ggml_tensor* cur_f;\n if (is_q8) {\n cur_f = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, T);\n dequant_q8_0_rows(hidden_states, reinterpret_cast<float*>(cur_f->data), T, d);\n } else {\n cur_f = hs;\n }\n\n ggml_tensor* logits = ggml_mul_mat(ctx, router, cur_f); // [e, T]\n ggml_tensor* probs = ggml_soft_max(ctx, logits); // [e, T]\n ggml_tensor* sel = ggml_argsort_top_k(ctx, probs, k); // [k, T] i32\n\n ggml_tensor* weights =\n ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, e, T), sel); // [1, k, T]\n weights = ggml_reshape_2d(ctx, weights, k, T);\n ggml_tensor* wsum = ggml_sum_rows(ctx, weights); // [1, T]\n weights = ggml_div(ctx, weights, wsum); // renormalize\n weights = ggml_reshape_3d(ctx, weights, 1, k, T);\n\n // Expert matmuls consume the supplied quantization directly for q8_0.\n ggml_tensor* cur = ggml_reshape_3d(ctx, is_q8 ? hs : cur_f, d, 1, T);\n ggml_tensor* g = ggml_mul_mat_id(ctx, gate, cur, sel); // [ff, k, T]\n ggml_tensor* u = ggml_mul_mat_id(ctx, up, cur, sel); // [ff, k, T]\n ggml_tensor* par = ggml_mul(ctx, ggml_silu(ctx, g), u); // [ff, k, T]\n ggml_tensor* experts = ggml_mul_mat_id(ctx, down, par, sel); // [d, k, T]\n experts = ggml_mul(ctx, experts, weights);\n\n // Sum over the k expert slots (view + add chain, as in build_moe_ffn).\n ggml_tensor* moe_out = ggml_view_2d(ctx, experts, d, T, experts->nb[2], 0);\n for (int64_t i = 1; i < k; ++i) {\n moe_out = ggml_add(\n ctx, moe_out,\n ggml_view_2d(ctx, experts, d, T, experts->nb[2], i * experts->nb[1]));\n }\n if (k == 1) moe_out = ggml_cont(ctx, moe_out);\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, moe_out);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, moe_out->data, (size_t)d * T * f);\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/moe/{moe_fp32_e64_k8_d2048_ff1024.json → moe_bf16_e64_k8_d2048_ff1024.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_moe_fp32_e64_k8_d2048_ff1024",
3
- "definition": "moe_fp32_e64_k8_d2048_ff1024",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for moe_fp32_e64_k8_d2048_ff1024. binding.cpp bakes the const axes as constexpr and implements armbench_entry_moe over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "moe.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) moe baseline.\n// Called by armbench_entry_moe (binding.cpp); implemented by kernel.cpp.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states is row-major [T, n_embd]; router_weight fp32 [n_expert, n_embd];\n// gate/up_proj [n_expert, n_ff, n_embd]; down_proj [n_expert, n_embd, n_ff];\n// out fp32 [T, n_embd]. For q8_0, hidden_states and the three projections are\n// ggml block_q8_0 rows over their last axis (34-byte {fp16 d; int8 qs[32]}\n// blocks, repacked by the Python adapter); router_weight stays fp32.\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"moe.h\"\n\nnamespace {\nconstexpr int64_t kNEmbd = 2048;\nconstexpr int64_t kNFf = 1024;\nconstexpr int64_t kNExpert = 64;\nconstexpr int64_t kNExpertUsed = 8;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=hidden_states fp32 [T,n_embd], [1]=router_weight fp32\n// [n_expert,n_embd], [2]=gate_proj fp32 [n_expert,n_ff,n_embd], [3]=up_proj\n// fp32 [n_expert,n_ff,n_embd], [4]=down_proj fp32 [n_expert,n_embd,n_ff];\n// var_axes: [0]=n_tokens\nint armbench_entry_moe(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_moe(\n inputs[0],\n reinterpret_cast<const float*>(inputs[1]),\n inputs[2], inputs[3], inputs[4],\n reinterpret_cast<float*>(output),\n var_axes[0], kNEmbd, kNFf, kNExpert, kNExpertUsed, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"moe.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\nnamespace {\n\n// Dequantize block_q8_0 rows (34-byte {fp16 d; int8 qs[32]} blocks) to fp32.\n// Used only for the router matmul input in the q8_0 variant \u2014 the reference\n// routes on the dequantized hidden states while the expert matmuls consume the\n// original quantization.\nvoid dequant_q8_0_rows(const void* src, float* dst, int64_t rows, int64_t k)\n{\n const int64_t nb = k / 32;\n const uint8_t* p = reinterpret_cast<const uint8_t*>(src);\n for (int64_t r = 0; r < rows; ++r) {\n const uint8_t* rp = p + (size_t)r * nb * 34;\n float* dp = dst + (size_t)r * k;\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = rp + (size_t)b * 34;\n uint16_t dbits;\n std::memcpy(&dbits, bp, sizeof(dbits));\n const float d = ggml_fp16_to_fp32(dbits);\n const int8_t* qs = reinterpret_cast<const int8_t*>(bp + 2);\n for (int j = 0; j < 32; ++j) dp[b * 32 + j] = d * (float)qs[j];\n }\n }\n}\n\n} // namespace\n\n// The build_moe_ffn sequence from llama.cpp (SOFTMAX gating + norm_w, the\n// qwen1.5-moe / olmoe configuration): softmax over all experts \u2192\n// ggml_argsort_top_k \u2192 ggml_get_rows \u2192 weight renorm \u2192 ggml_mul_mat_id\n// gate/up \u2192 silu\u00b7up \u2192 ggml_mul_mat_id down \u2192 weight and sum the expert views.\n//\n// For q8_0, gate/up consume the pre-quantized hidden states directly\n// (src1->type == vec_dot_type, so ggml does NOT re-quantize them); the down\n// projection's fp32 intermediate is quantized to q8_0 by ggml internally \u2014\n// that is llama.cpp's real inference behavior and the one place this baseline\n// deviates from the fp32-intermediate numpy reference.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8)\n{\n const int64_t T = n_tokens, d = n_embd, ff = n_ff;\n const int64_t e = n_expert, k = n_expert_used;\n const ggml_type wt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_F32;\n const size_t f = sizeof(float);\n\n ggml_init_params ip_in = { 8 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* hs = ggml_new_tensor_2d(ctx_in, wt, d, T);\n hs->data = const_cast<void*>(hidden_states);\n ggml_tensor* router = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, d, e);\n router->data = const_cast<float*>(router_weight);\n ggml_tensor* gate = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n gate->data = const_cast<void*>(gate_proj);\n ggml_tensor* up = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n up->data = const_cast<void*>(up_proj);\n ggml_tensor* down = ggml_new_tensor_3d(ctx_in, wt, ff, d, e);\n down->data = const_cast<void*>(down_proj);\n\n size_t mem = 0;\n mem += 3 * (size_t)e * T * f; // logits + probs + argsort indices\n mem += 8 * (size_t)k * T * f; // selected ids, weights, sums, renorm\n mem += 4 * (size_t)ff * k * T * f; // gate, up, silu, gated product\n mem += 2 * (size_t)d * k * T * f; // expert outputs + weighted\n mem += (size_t)k * d * T * f; // expert-sum add chain\n if (is_q8) {\n mem += (size_t)d * T * f; // dequantized hidden states\n mem += (size_t)ff * k * T * 34 / 32 + 64; // cplan: down-proj src1 \u2192 q8_0\n }\n mem += (size_t)e * ((size_t)k * T * 16 + 256); // cplan: mul_mat_id row maps\n mem += 64 * ggml_tensor_overhead() + ggml_graph_overhead();\n mem += (4u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n // Router input: fp32 hidden states (dequantized for the q8_0 variant).\n ggml_tensor* cur_f;\n if (is_q8) {\n cur_f = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, T);\n dequant_q8_0_rows(hidden_states, reinterpret_cast<float*>(cur_f->data), T, d);\n } else {\n cur_f = hs;\n }\n\n ggml_tensor* logits = ggml_mul_mat(ctx, router, cur_f); // [e, T]\n ggml_tensor* probs = ggml_soft_max(ctx, logits); // [e, T]\n ggml_tensor* sel = ggml_argsort_top_k(ctx, probs, k); // [k, T] i32\n\n ggml_tensor* weights =\n ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, e, T), sel); // [1, k, T]\n weights = ggml_reshape_2d(ctx, weights, k, T);\n ggml_tensor* wsum = ggml_sum_rows(ctx, weights); // [1, T]\n weights = ggml_div(ctx, weights, wsum); // renormalize\n weights = ggml_reshape_3d(ctx, weights, 1, k, T);\n\n // Expert matmuls consume the supplied quantization directly for q8_0.\n ggml_tensor* cur = ggml_reshape_3d(ctx, is_q8 ? hs : cur_f, d, 1, T);\n ggml_tensor* g = ggml_mul_mat_id(ctx, gate, cur, sel); // [ff, k, T]\n ggml_tensor* u = ggml_mul_mat_id(ctx, up, cur, sel); // [ff, k, T]\n ggml_tensor* par = ggml_mul(ctx, ggml_silu(ctx, g), u); // [ff, k, T]\n ggml_tensor* experts = ggml_mul_mat_id(ctx, down, par, sel); // [d, k, T]\n experts = ggml_mul(ctx, experts, weights);\n\n // Sum over the k expert slots (view + add chain, as in build_moe_ffn).\n ggml_tensor* moe_out = ggml_view_2d(ctx, experts, d, T, experts->nb[2], 0);\n for (int64_t i = 1; i < k; ++i) {\n moe_out = ggml_add(\n ctx, moe_out,\n ggml_view_2d(ctx, experts, d, T, experts->nb[2], i * experts->nb[1]));\n }\n if (k == 1) moe_out = ggml_cont(ctx, moe_out);\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, moe_out);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, moe_out->data, (size_t)d * T * f);\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_moe_bf16_e64_k8_d2048_ff1024",
3
+ "definition": "moe_bf16_e64_k8_d2048_ff1024",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for moe_fp32_e64_k8_d2048_ff1024. binding.cpp bakes the const axes as constexpr and implements armbench_entry_moe over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "moe.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) moe baseline.\n// Called by armbench_entry_moe (binding.cpp); implemented by kernel.cpp.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states is row-major bf16 [T, n_embd]; router_weight bf16 [n_expert, n_embd];\n// gate/up_proj bf16 [n_expert, n_ff, n_embd]; down_proj bf16 [n_expert, n_embd, n_ff];\n// out fp32 [T, n_embd]. For q8_0, hidden_states and the three projections are\n// ggml block_q8_0 rows over their last axis (34-byte {fp16 d; int8 qs[32]}\n// blocks, repacked by the Python adapter); router_weight stays fp32 there.\nint armbench_llamacpp_moe(const void* hidden_states, const void* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"moe.h\"\n\nnamespace {\nconstexpr int64_t kNEmbd = 2048;\nconstexpr int64_t kNFf = 1024;\nconstexpr int64_t kNExpert = 64;\nconstexpr int64_t kNExpertUsed = 8;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=hidden_states bf16 [T,n_embd], [1]=router_weight bf16\n// [n_expert,n_embd], [2]=gate_proj bf16 [n_expert,n_ff,n_embd], [3]=up_proj\n// bf16 [n_expert,n_ff,n_embd], [4]=down_proj bf16 [n_expert,n_embd,n_ff];\n// var_axes: [0]=n_tokens\nint armbench_entry_moe(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_moe(\n inputs[0],\n inputs[1],\n inputs[2], inputs[3], inputs[4],\n reinterpret_cast<float*>(output),\n var_axes[0], kNEmbd, kNFf, kNExpert, kNExpertUsed, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"moe.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\nnamespace {\n\n// Dequantize block_q8_0 rows (34-byte {fp16 d; int8 qs[32]} blocks) to fp32.\n// Used only for the router matmul input in the q8_0 variant \u2014 the reference\n// routes on the dequantized hidden states while the expert matmuls consume the\n// original quantization.\nvoid dequant_q8_0_rows(const void* src, float* dst, int64_t rows, int64_t k)\n{\n const int64_t nb = k / 32;\n const uint8_t* p = reinterpret_cast<const uint8_t*>(src);\n for (int64_t r = 0; r < rows; ++r) {\n const uint8_t* rp = p + (size_t)r * nb * 34;\n float* dp = dst + (size_t)r * k;\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = rp + (size_t)b * 34;\n uint16_t dbits;\n std::memcpy(&dbits, bp, sizeof(dbits));\n const float d = ggml_fp16_to_fp32(dbits);\n const int8_t* qs = reinterpret_cast<const int8_t*>(bp + 2);\n for (int j = 0; j < 32; ++j) dp[b * 32 + j] = d * (float)qs[j];\n }\n }\n}\n\n} // namespace\n\n// The build_moe_ffn sequence from llama.cpp (SOFTMAX gating + norm_w, the\n// qwen1.5-moe / olmoe configuration): softmax over all experts \u2192\n// ggml_argsort_top_k \u2192 ggml_get_rows \u2192 weight renorm \u2192 ggml_mul_mat_id\n// gate/up \u2192 silu\u00b7up \u2192 ggml_mul_mat_id down \u2192 weight and sum the expert views.\n//\n// For q8_0, gate/up consume the pre-quantized hidden states directly\n// (src1->type == vec_dot_type, so ggml does NOT re-quantize them); the down\n// projection's fp32 intermediate is quantized to q8_0 by ggml internally \u2014\n// that is llama.cpp's real inference behavior and the one place this baseline\n// deviates from the fp32-intermediate numpy reference.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_moe(const void* hidden_states, const void* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used, bool is_q8)\n{\n const int64_t T = n_tokens, d = n_embd, ff = n_ff;\n const int64_t e = n_expert, k = n_expert_used;\n const ggml_type wt = is_q8 ? GGML_TYPE_Q8_0 : GGML_TYPE_BF16;\n const size_t f = sizeof(float);\n\n ggml_init_params ip_in = { 8 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* hs = ggml_new_tensor_2d(ctx_in, wt, d, T);\n hs->data = const_cast<void*>(hidden_states);\n ggml_tensor* router = ggml_new_tensor_2d(ctx_in, GGML_TYPE_BF16, d, e);\n router->data = const_cast<void*>(router_weight);\n ggml_tensor* gate = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n gate->data = const_cast<void*>(gate_proj);\n ggml_tensor* up = ggml_new_tensor_3d(ctx_in, wt, d, ff, e);\n up->data = const_cast<void*>(up_proj);\n ggml_tensor* down = ggml_new_tensor_3d(ctx_in, wt, ff, d, e);\n down->data = const_cast<void*>(down_proj);\n\n size_t mem = 0;\n mem += 3 * (size_t)e * T * f; // logits + probs + argsort indices\n mem += 8 * (size_t)k * T * f; // selected ids, weights, sums, renorm\n mem += 4 * (size_t)ff * k * T * f; // gate, up, silu, gated product\n mem += 2 * (size_t)d * k * T * f; // expert outputs + weighted\n mem += (size_t)k * d * T * f; // expert-sum add chain\n if (is_q8) {\n mem += (size_t)d * T * f; // dequantized hidden states\n mem += (size_t)ff * k * T * 34 / 32 + 64; // cplan: down-proj src1 \u2192 q8_0\n }\n mem += (size_t)e * ((size_t)k * T * 16 + 256); // cplan: mul_mat_id row maps\n mem += 64 * ggml_tensor_overhead() + ggml_graph_overhead();\n mem += (4u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n // Router input: fp32 hidden states (dequantized for the q8_0 variant).\n ggml_tensor* cur_f;\n if (is_q8) {\n cur_f = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, T);\n dequant_q8_0_rows(hidden_states, reinterpret_cast<float*>(cur_f->data), T, d);\n } else {\n cur_f = hs;\n }\n\n ggml_tensor* logits = ggml_mul_mat(ctx, router, cur_f); // [e, T]\n ggml_tensor* probs = ggml_soft_max(ctx, logits); // [e, T]\n ggml_tensor* sel = ggml_argsort_top_k(ctx, probs, k); // [k, T] i32\n\n ggml_tensor* weights =\n ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, e, T), sel); // [1, k, T]\n weights = ggml_reshape_2d(ctx, weights, k, T);\n ggml_tensor* wsum = ggml_sum_rows(ctx, weights); // [1, T]\n weights = ggml_div(ctx, weights, wsum); // renormalize\n weights = ggml_reshape_3d(ctx, weights, 1, k, T);\n\n // Expert matmuls consume the supplied quantization directly for q8_0.\n ggml_tensor* cur = ggml_reshape_3d(ctx, is_q8 ? hs : cur_f, d, 1, T);\n ggml_tensor* g = ggml_mul_mat_id(ctx, gate, cur, sel); // [ff, k, T]\n ggml_tensor* u = ggml_mul_mat_id(ctx, up, cur, sel); // [ff, k, T]\n ggml_tensor* par = ggml_mul(ctx, ggml_silu(ctx, g), u); // [ff, k, T]\n ggml_tensor* experts = ggml_mul_mat_id(ctx, down, par, sel); // [d, k, T]\n experts = ggml_mul(ctx, experts, weights);\n\n // Sum over the k expert slots (view + add chain, as in build_moe_ffn).\n ggml_tensor* moe_out = ggml_view_2d(ctx, experts, d, T, experts->nb[2], 0);\n for (int64_t i = 1; i < k; ++i) {\n moe_out = ggml_add(\n ctx, moe_out,\n ggml_view_2d(ctx, experts, d, T, experts->nb[2], i * experts->nb[1]));\n }\n if (k == 1) moe_out = ggml_cont(ctx, moe_out);\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, moe_out);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, moe_out->data, (size_t)d * T * f);\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_q4_k_m_e60_k4_d2048_ff1536.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_moe_q4_k_m_e60_k4_d2048_ff1536",
3
+ "definition": "moe_q4_k_m_e60_k4_d2048_ff1536",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for moe_q4_k_m_e60_k4_d2048_ff1536. binding.cpp bakes the const axes as constexpr and implements armbench_entry_moe over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_moe"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "moe.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) moe baseline.\n// Called by armbench_entry_moe (binding.cpp); implemented by kernel.cpp.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states is row-major [T, n_embd] bf16 (raw bit pattern -- NOT\n// quantized; weight-only Q4_K design). router_weight is fp32\n// [n_expert, n_embd] (also not quantized -- routing decisions are\n// categorical and must not flip from quantization rounding). gate_proj/\n// up_proj [n_expert, n_ff, n_embd] and down_proj [n_expert, n_embd, n_ff]\n// are ggml block_q4_K rows over their last axis (144-byte blocks, repacked\n// by the Python adapter). out fp32 [T, n_embd].\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"moe.h\"\n\nnamespace {\nconstexpr int64_t kNEmbd = 2048;\nconstexpr int64_t kNFf = 1536;\nconstexpr int64_t kNExpert = 60;\nconstexpr int64_t kNExpertUsed = 4;\n} // namespace\n\nextern \"C\" {\n// [0]=hidden_states bf16, [1]=router_weight fp32,\n// [2]=gate_proj block_q4_K, [3]=NULL (gate_scales), [4]=NULL (gate_mins),\n// [5]=up_proj block_q4_K, [6]=NULL (up_scales), [7]=NULL (up_mins),\n// [8]=down_proj block_q4_K, [9]=NULL (down_scales), [10]=NULL (down_mins);\n// var_axes: [0]=n_tokens\nint armbench_entry_moe(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_moe(\n inputs[0],\n reinterpret_cast<const float*>(inputs[1]),\n inputs[2], inputs[5], inputs[8],\n reinterpret_cast<float*>(output),\n var_axes[0], kNEmbd, kNFf, kNExpert, kNExpertUsed);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"moe.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// The build_moe_ffn sequence from llama.cpp (SOFTMAX gating + norm_w, the\n// qwen1.5-moe / olmoe configuration): softmax over all experts ->\n// ggml_argsort_top_k -> ggml_get_rows -> weight renorm -> ggml_mul_mat_id\n// gate/up -> silu*up -> ggml_mul_mat_id down -> weight and sum the expert\n// views.\n//\n// hidden_states arrives as bf16 (never quantized -- weight-only Q4_K\n// design); it's widened to F32 once up front and used directly for the\n// router and all three expert matmuls. gate/up/down are real Q4_K weights;\n// ggml internally dynamically quantizes the F32 activations to Q8_K for\n// each of those matmuls (ggml_compute_forward_mul_mat[_id] requires\n// src1->type == F32 whenever src0 is a k-quant type) -- nothing else to do\n// here. router_weight stays plain F32, unquantized.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used)\n{\n const int64_t T = n_tokens, d = n_embd, ff = n_ff;\n const int64_t e = n_expert, k = n_expert_used;\n const size_t f = sizeof(float);\n\n ggml_init_params ip_in = { 8 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* router = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, d, e);\n router->data = const_cast<float*>(router_weight);\n ggml_tensor* gate = ggml_new_tensor_3d(ctx_in, GGML_TYPE_Q4_K, d, ff, e);\n gate->data = const_cast<void*>(gate_proj);\n ggml_tensor* up = ggml_new_tensor_3d(ctx_in, GGML_TYPE_Q4_K, d, ff, e);\n up->data = const_cast<void*>(up_proj);\n ggml_tensor* down = ggml_new_tensor_3d(ctx_in, GGML_TYPE_Q4_K, ff, d, e);\n down->data = const_cast<void*>(down_proj);\n\n size_t mem = 0;\n mem += (size_t)d * T * f; // widened hidden states\n mem += 3 * (size_t)e * T * f; // logits + probs + argsort indices\n mem += 8 * (size_t)k * T * f; // selected ids, weights, sums, renorm\n mem += 4 * (size_t)ff * k * T * f; // gate, up, silu, gated product\n mem += 2 * (size_t)d * k * T * f; // expert outputs + weighted\n mem += (size_t)k * d * T * f; // expert-sum add chain\n // cplan scratch: dynamic Q8_K conversion for router/gate/up (activation\n // width d) and down (activation width ff) matmuls; generous 2x-of-fp32.\n mem += (size_t)d * T * f * 2;\n mem += (size_t)ff * k * T * f * 2;\n mem += (size_t)e * ((size_t)k * T * 16 + 256); // cplan: mul_mat_id row maps\n mem += 64 * ggml_tensor_overhead() + ggml_graph_overhead();\n mem += (4u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n // Widen bf16 hidden states -> F32 once; used for router + all expert matmuls.\n ggml_tensor* cur_f = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, T);\n {\n const uint16_t* hbits = reinterpret_cast<const uint16_t*>(hidden_states);\n float* hf = reinterpret_cast<float*>(cur_f->data);\n for (int64_t i = 0; i < d * T; ++i) hf[i] = bf16_to_f32(hbits[i]);\n }\n\n ggml_tensor* logits = ggml_mul_mat(ctx, router, cur_f); // [e, T]\n ggml_tensor* probs = ggml_soft_max(ctx, logits); // [e, T]\n ggml_tensor* sel = ggml_argsort_top_k(ctx, probs, k); // [k, T] i32\n\n ggml_tensor* weights =\n ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, e, T), sel); // [1, k, T]\n weights = ggml_reshape_2d(ctx, weights, k, T);\n ggml_tensor* wsum = ggml_sum_rows(ctx, weights); // [1, T]\n weights = ggml_div(ctx, weights, wsum); // renormalize\n weights = ggml_reshape_3d(ctx, weights, 1, k, T);\n\n ggml_tensor* cur = ggml_reshape_3d(ctx, cur_f, d, 1, T);\n ggml_tensor* g = ggml_mul_mat_id(ctx, gate, cur, sel); // [ff, k, T]\n ggml_tensor* u = ggml_mul_mat_id(ctx, up, cur, sel); // [ff, k, T]\n ggml_tensor* par = ggml_mul(ctx, ggml_silu(ctx, g), u); // [ff, k, T]\n ggml_tensor* experts = ggml_mul_mat_id(ctx, down, par, sel); // [d, k, T]\n experts = ggml_mul(ctx, experts, weights);\n\n // Sum over the k expert slots (view + add chain, as in build_moe_ffn).\n ggml_tensor* moe_out = ggml_view_2d(ctx, experts, d, T, experts->nb[2], 0);\n for (int64_t i = 1; i < k; ++i) {\n moe_out = ggml_add(\n ctx, moe_out,\n ggml_view_2d(ctx, experts, d, T, experts->nb[2], i * experts->nb[1]));\n }\n if (k == 1) moe_out = ggml_cont(ctx, moe_out);\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, moe_out);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, moe_out->data, (size_t)d * T * f);\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_q4_k_m_e64_k8_d2048_ff1024.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "baseline-llamacpp-arm_moe_q4_k_m_e64_k8_d2048_ff1024",
3
+ "definition": "moe_q4_k_m_e64_k8_d2048_ff1024",
4
+ "dataset": "llama.cpp",
5
+ "author": "baseline-llamacpp-arm",
6
+ "description": "llama.cpp (ggml) Q4_K baseline for moe_q4_k_m_e64_k8_d2048_ff1024. binding.cpp bakes the const axes as constexpr and implements armbench_entry_moe over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve",
12
+ "graviton4",
13
+ "aarch64-sve2",
14
+ "apple-m"
15
+ ],
16
+ "dependencies": [],
17
+ "isa_features": [],
18
+ "compile_flags": [
19
+ "-O3",
20
+ "-std=c++17"
21
+ ],
22
+ "link_flags": [],
23
+ "entry_point": "binding.cpp::armbench_entry_moe"
24
+ },
25
+ "sources": [
26
+ {
27
+ "path": "moe.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) moe baseline.\n// Called by armbench_entry_moe (binding.cpp); implemented by kernel.cpp.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states is row-major [T, n_embd] bf16 (raw bit pattern -- NOT\n// quantized; weight-only Q4_K design). router_weight is fp32\n// [n_expert, n_embd] (also not quantized -- routing decisions are\n// categorical and must not flip from quantization rounding). gate_proj/\n// up_proj [n_expert, n_ff, n_embd] and down_proj [n_expert, n_embd, n_ff]\n// are ggml block_q4_K rows over their last axis (144-byte blocks, repacked\n// by the Python adapter). out fp32 [T, n_embd].\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used);\n"
29
+ },
30
+ {
31
+ "path": "binding.cpp",
32
+ "content": "#include \"moe.h\"\n\nnamespace {\nconstexpr int64_t kNEmbd = 2048;\nconstexpr int64_t kNFf = 1024;\nconstexpr int64_t kNExpert = 64;\nconstexpr int64_t kNExpertUsed = 8;\n} // namespace\n\nextern \"C\" {\n// [0]=hidden_states bf16, [1]=router_weight fp32,\n// [2]=gate_proj block_q4_K, [3]=NULL (gate_scales), [4]=NULL (gate_mins),\n// [5]=up_proj block_q4_K, [6]=NULL (up_scales), [7]=NULL (up_mins),\n// [8]=down_proj block_q4_K, [9]=NULL (down_scales), [10]=NULL (down_mins);\n// var_axes: [0]=n_tokens\nint armbench_entry_moe(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_moe(\n inputs[0],\n reinterpret_cast<const float*>(inputs[1]),\n inputs[2], inputs[5], inputs[8],\n reinterpret_cast<float*>(output),\n var_axes[0], kNEmbd, kNFf, kNExpert, kNExpertUsed);\n}\n} // extern \"C\"\n"
33
+ },
34
+ {
35
+ "path": "kernel.cpp",
36
+ "content": "#include \"moe.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n#include <cstdint>\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\n// The build_moe_ffn sequence from llama.cpp (SOFTMAX gating + norm_w, the\n// qwen1.5-moe / olmoe configuration): softmax over all experts ->\n// ggml_argsort_top_k -> ggml_get_rows -> weight renorm -> ggml_mul_mat_id\n// gate/up -> silu*up -> ggml_mul_mat_id down -> weight and sum the expert\n// views.\n//\n// hidden_states arrives as bf16 (never quantized -- weight-only Q4_K\n// design); it's widened to F32 once up front and used directly for the\n// router and all three expert matmuls. gate/up/down are real Q4_K weights;\n// ggml internally dynamically quantizes the F32 activations to Q8_K for\n// each of those matmuls (ggml_compute_forward_mul_mat[_id] requires\n// src1->type == F32 whenever src0 is a k-quant type) -- nothing else to do\n// here. router_weight stays plain F32, unquantized.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_moe(const void* hidden_states, const float* router_weight,\n const void* gate_proj, const void* up_proj,\n const void* down_proj, float* out,\n int64_t n_tokens, int64_t n_embd, int64_t n_ff,\n int64_t n_expert, int64_t n_expert_used)\n{\n const int64_t T = n_tokens, d = n_embd, ff = n_ff;\n const int64_t e = n_expert, k = n_expert_used;\n const size_t f = sizeof(float);\n\n ggml_init_params ip_in = { 8 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* router = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, d, e);\n router->data = const_cast<float*>(router_weight);\n ggml_tensor* gate = ggml_new_tensor_3d(ctx_in, GGML_TYPE_Q4_K, d, ff, e);\n gate->data = const_cast<void*>(gate_proj);\n ggml_tensor* up = ggml_new_tensor_3d(ctx_in, GGML_TYPE_Q4_K, d, ff, e);\n up->data = const_cast<void*>(up_proj);\n ggml_tensor* down = ggml_new_tensor_3d(ctx_in, GGML_TYPE_Q4_K, ff, d, e);\n down->data = const_cast<void*>(down_proj);\n\n size_t mem = 0;\n mem += (size_t)d * T * f; // widened hidden states\n mem += 3 * (size_t)e * T * f; // logits + probs + argsort indices\n mem += 8 * (size_t)k * T * f; // selected ids, weights, sums, renorm\n mem += 4 * (size_t)ff * k * T * f; // gate, up, silu, gated product\n mem += 2 * (size_t)d * k * T * f; // expert outputs + weighted\n mem += (size_t)k * d * T * f; // expert-sum add chain\n // cplan scratch: dynamic Q8_K conversion for router/gate/up (activation\n // width d) and down (activation width ff) matmuls; generous 2x-of-fp32.\n mem += (size_t)d * T * f * 2;\n mem += (size_t)ff * k * T * f * 2;\n mem += (size_t)e * ((size_t)k * T * 16 + 256); // cplan: mul_mat_id row maps\n mem += 64 * ggml_tensor_overhead() + ggml_graph_overhead();\n mem += (4u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n // Widen bf16 hidden states -> F32 once; used for router + all expert matmuls.\n ggml_tensor* cur_f = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, T);\n {\n const uint16_t* hbits = reinterpret_cast<const uint16_t*>(hidden_states);\n float* hf = reinterpret_cast<float*>(cur_f->data);\n for (int64_t i = 0; i < d * T; ++i) hf[i] = bf16_to_f32(hbits[i]);\n }\n\n ggml_tensor* logits = ggml_mul_mat(ctx, router, cur_f); // [e, T]\n ggml_tensor* probs = ggml_soft_max(ctx, logits); // [e, T]\n ggml_tensor* sel = ggml_argsort_top_k(ctx, probs, k); // [k, T] i32\n\n ggml_tensor* weights =\n ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, e, T), sel); // [1, k, T]\n weights = ggml_reshape_2d(ctx, weights, k, T);\n ggml_tensor* wsum = ggml_sum_rows(ctx, weights); // [1, T]\n weights = ggml_div(ctx, weights, wsum); // renormalize\n weights = ggml_reshape_3d(ctx, weights, 1, k, T);\n\n ggml_tensor* cur = ggml_reshape_3d(ctx, cur_f, d, 1, T);\n ggml_tensor* g = ggml_mul_mat_id(ctx, gate, cur, sel); // [ff, k, T]\n ggml_tensor* u = ggml_mul_mat_id(ctx, up, cur, sel); // [ff, k, T]\n ggml_tensor* par = ggml_mul(ctx, ggml_silu(ctx, g), u); // [ff, k, T]\n ggml_tensor* experts = ggml_mul_mat_id(ctx, down, par, sel); // [d, k, T]\n experts = ggml_mul(ctx, experts, weights);\n\n // Sum over the k expert slots (view + add chain, as in build_moe_ffn).\n ggml_tensor* moe_out = ggml_view_2d(ctx, experts, d, T, experts->nb[2], 0);\n for (int64_t i = 1; i < k; ++i) {\n moe_out = ggml_add(\n ctx, moe_out,\n ggml_view_2d(ctx, experts, d, T, experts->nb[2], i * experts->nb[1]));\n }\n if (k == 1) moe_out = ggml_cont(ctx, moe_out);\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, moe_out);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, moe_out->data, (size_t)d * T * f);\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
+ }
38
+ ]
39
+ }
solutions/llama.cpp/baseline-llamacpp-arm/rms_norm/{rms_norm_fp32_d2048.json → rms_norm_bf16_d2048.json} RENAMED
@@ -1,6 +1,6 @@
1
  {
2
- "name": "baseline-llamacpp-arm_rms_norm_fp32_d2048",
3
- "definition": "rms_norm_fp32_d2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for rms_norm_fp32_d2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_rms_norm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
@@ -25,15 +25,15 @@
25
  "sources": [
26
  {
27
  "path": "rms_norm.h",
28
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) rms_norm baseline.\n// Called by armbench_entry_rms_norm (binding.cpp); implemented by kernel.cpp.\n// x is row-major fp32 [M, D], weight is fp32 [D], out is row-major fp32 [M, D]:\n// out[m] = x[m] / sqrt(mean(x[m]^2) + eps) * weight\nint armbench_llamacpp_rms_norm(const float* x, const float* weight, float* out,\n int64_t M, int64_t D, float eps);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
- "content": "#include \"rms_norm.h\"\n\nnamespace {\nconstexpr int64_t kD = 2048;\nconstexpr float kEps = 1e-6f; // matches the Definition reference\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=x fp32 [M,D], [1]=weight fp32 [D]; var_axes: [0]=M\nint armbench_entry_rms_norm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_rms_norm(\n reinterpret_cast<const float*>(inputs[0]),\n reinterpret_cast<const float*>(inputs[1]),\n reinterpret_cast<float*>(output),\n var_axes[0], kD, kEps);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
- "content": "#include \"rms_norm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// ggml_rms_norm normalizes over ne0 (= D), then ggml_mul broadcasts the [D]\n// weight over rows \u2014 the same op pair llama.cpp's build_norm emits for\n// LLM_NORM_RMS.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_rms_norm(const float* x, const float* weight, float* out,\n int64_t M, int64_t D, float eps)\n{\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* xt = ggml_new_tensor_2d(ctx_in, GGML_TYPE_F32, D, M);\n xt->data = const_cast<float*>(x);\n ggml_tensor* wt = ggml_new_tensor_1d(ctx_in, GGML_TYPE_F32, D);\n wt->data = const_cast<float*>(weight);\n\n const size_t mem =\n 2 * (size_t)D * M * sizeof(float) // normalized x + weighted output\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* xn = ggml_rms_norm(ctx, xt, eps); // [D, M]\n ggml_tensor* y = ggml_mul(ctx, xn, wt); // [D, M] * [D] broadcast\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, y);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, y->data, (size_t)D * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
 
1
  {
2
+ "name": "baseline-llamacpp-arm_rms_norm_bf16_d2048",
3
+ "definition": "rms_norm_bf16_d2048",
4
  "dataset": "llama.cpp",
5
  "author": "baseline-llamacpp-arm",
6
  "description": "llama.cpp (ggml) fp32 baseline for rms_norm_fp32_d2048. binding.cpp bakes the const axes as constexpr and implements armbench_entry_rms_norm over the void* ABI; kernel.cpp builds + runs the ggml graph against libggml*.a. Timing baseline for speedup computation.",
 
25
  "sources": [
26
  {
27
  "path": "rms_norm.h",
28
+ "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) rms_norm baseline.\n// Called by armbench_entry_rms_norm (binding.cpp); implemented by kernel.cpp.\n// x is row-major bf16 [M, D], weight is bf16 [D], out is row-major fp32 [M, D]:\n// out[m] = x[m] / sqrt(mean(x[m]^2) + eps) * weight\nint armbench_llamacpp_rms_norm(const void* x, const void* weight, float* out,\n int64_t M, int64_t D, float eps);\n"
29
  },
30
  {
31
  "path": "binding.cpp",
32
+ "content": "#include \"rms_norm.h\"\n\nnamespace {\nconstexpr int64_t kD = 2048;\nconstexpr float kEps = 1e-6f; // matches the Definition reference\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=x bf16 [M,D], [1]=weight bf16 [D]; var_axes: [0]=M\nint armbench_entry_rms_norm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_rms_norm(\n inputs[0],\n inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kD, kEps);\n}\n} // extern \"C\"\n"
33
  },
34
  {
35
  "path": "kernel.cpp",
36
+ "content": "#include \"rms_norm.h\"\n\n#include \"ggml.h\"\n#include \"ggml-cpu.h\"\n\n#include <cstring>\n\n// ggml_rms_norm normalizes over ne0 (= D), then ggml_mul broadcasts the [D]\n// weight over rows \u2014 the same op pair llama.cpp's build_norm emits for\n// LLM_NORM_RMS.\n//\n// Like the ncnn baseline (layer create + create_pipeline + forward per entry\n// call), the whole ggml context/graph setup runs inside the timed region.\nint armbench_llamacpp_rms_norm(const void* x, const void* weight, float* out,\n int64_t M, int64_t D, float eps)\n{\n ggml_init_params ip_in = { 4 * ggml_tensor_overhead(), nullptr, /*no_alloc=*/true };\n ggml_context* ctx_in = ggml_init(ip_in);\n if (!ctx_in) return -1;\n ggml_tensor* xt = ggml_new_tensor_2d(ctx_in, GGML_TYPE_BF16, D, M);\n xt->data = const_cast<void*>(x);\n ggml_tensor* wt = ggml_new_tensor_1d(ctx_in, GGML_TYPE_BF16, D);\n wt->data = const_cast<void*>(weight);\n\n const size_t mem =\n 2 * (size_t)D * M * sizeof(float) // normalized x + weighted output\n + 16 * ggml_tensor_overhead()\n + ggml_graph_overhead()\n + (1u << 20);\n ggml_init_params ip = { mem, nullptr, /*no_alloc=*/false };\n ggml_context* ctx = ggml_init(ip);\n if (!ctx) { ggml_free(ctx_in); return -1; }\n\n ggml_tensor* xn = ggml_rms_norm(ctx, xt, eps); // [D, M]\n ggml_tensor* y = ggml_mul(ctx, xn, wt); // [D, M] * [D] broadcast\n\n ggml_cgraph* gf = ggml_new_graph(ctx);\n ggml_build_forward_expand(gf, y);\n const ggml_status st = ggml_graph_compute_with_ctx(ctx, gf, /*n_threads=*/1);\n\n int ret = -1;\n if (st == GGML_STATUS_SUCCESS) {\n std::memcpy(out, y->data, (size_t)D * M * sizeof(float));\n ret = 0;\n }\n ggml_free(ctx);\n ggml_free(ctx_in);\n return ret;\n}\n"
37
  }
38
  ]
39
  }
solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n1024_k2048.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_bf16_n1024_k2048",
3
+ "definition": "gemm_bf16_n1024_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_bf16_n1024_k2048. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm bf16 specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A/B are raw bf16 bit patterns (uint16_t) \u2014 bf16 shares fp32's exponent field,\n// so widening is an exact `(uint32_t)bits << 16` reinterpret (see kernel.cpp).\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 1024;\nconstexpr int K = 2048;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B: (N, K) bf16; output: (M, N) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n inner_gemm(A, output, B, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm bf16 (fc-style: C = A @ B^T, fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cstring>\nusing namespace gemm_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint16_t* b_row = B + (long)n * K;\n float sum = 0.0f;\n for (int k = 0; k < K; ++k) {\n sum += bf16_to_f32(a_row[k]) * bf16_to_f32(b_row[k]);\n }\n out_row[n] = sum;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n1408_k2048.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_bf16_n1408_k2048",
3
+ "definition": "gemm_bf16_n1408_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_bf16_n1408_k2048. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm bf16 specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A/B are raw bf16 bit patterns (uint16_t) \u2014 bf16 shares fp32's exponent field,\n// so widening is an exact `(uint32_t)bits << 16` reinterpret (see kernel.cpp).\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 1408;\nconstexpr int K = 2048;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B: (N, K) bf16; output: (M, N) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n inner_gemm(A, output, B, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm bf16 (fc-style: C = A @ B^T, fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cstring>\nusing namespace gemm_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint16_t* b_row = B + (long)n * K;\n float sum = 0.0f;\n for (int k = 0; k < K; ++k) {\n sum += bf16_to_f32(a_row[k]) * bf16_to_f32(b_row[k]);\n }\n out_row[n] = sum;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n2048_k1024.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_bf16_n2048_k1024",
3
+ "definition": "gemm_bf16_n2048_k1024",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_bf16_n2048_k1024. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm bf16 specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A/B are raw bf16 bit patterns (uint16_t) \u2014 bf16 shares fp32's exponent field,\n// so widening is an exact `(uint32_t)bits << 16` reinterpret (see kernel.cpp).\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 2048;\nconstexpr int K = 1024;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B: (N, K) bf16; output: (M, N) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n inner_gemm(A, output, B, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm bf16 (fc-style: C = A @ B^T, fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cstring>\nusing namespace gemm_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint16_t* b_row = B + (long)n * K;\n float sum = 0.0f;\n for (int k = 0; k < K; ++k) {\n sum += bf16_to_f32(a_row[k]) * bf16_to_f32(b_row[k]);\n }\n out_row[n] = sum;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n2048_k1408.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_bf16_n2048_k1408",
3
+ "definition": "gemm_bf16_n2048_k1408",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_bf16_n2048_k1408. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm bf16 specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A/B are raw bf16 bit patterns (uint16_t) \u2014 bf16 shares fp32's exponent field,\n// so widening is an exact `(uint32_t)bits << 16` reinterpret (see kernel.cpp).\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 2048;\nconstexpr int K = 1408;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B: (N, K) bf16; output: (M, N) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n inner_gemm(A, output, B, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm bf16 (fc-style: C = A @ B^T, fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cstring>\nusing namespace gemm_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint16_t* b_row = B + (long)n * K;\n float sum = 0.0f;\n for (int k = 0; k < K; ++k) {\n sum += bf16_to_f32(a_row[k]) * bf16_to_f32(b_row[k]);\n }\n out_row[n] = sum;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_bf16_n2048_k2048.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_bf16_n2048_k2048",
3
+ "definition": "gemm_bf16_n2048_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_bf16_n2048_k2048. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm bf16 specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A/B are raw bf16 bit patterns (uint16_t) \u2014 bf16 shares fp32's exponent field,\n// so widening is an exact `(uint32_t)bits << 16` reinterpret (see kernel.cpp).\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 2048;\nconstexpr int K = 2048;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B: (N, K) bf16; output: (M, N) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n inner_gemm(A, output, B, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm bf16 (fc-style: C = A @ B^T, fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_gemm.\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cstring>\nusing namespace gemm_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint16_t* B, int M)\n{\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint16_t* b_row = B + (long)n * K;\n float sum = 0.0f;\n for (int k = 0; k < K; ++k) {\n sum += bf16_to_f32(a_row[k]) * bf16_to_f32(b_row[k]);\n }\n out_row[n] = sum;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n1024_k2048.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "name": "reference-scalar_gemm_fp32_n1024_k2048",
3
- "definition": "gemm_fp32_n1024_k2048",
4
- "dataset": "llama.cpp",
5
- "author": "reference-scalar",
6
- "spec": {
7
- "target_hardware": [
8
- "graviton3",
9
- "aarch64-sve",
10
- "graviton4",
11
- "aarch64-sve2",
12
- "apple-m"
13
- ],
14
- "entry_point": "binding.cpp::armbench_entry_gemm",
15
- "dependencies": [],
16
- "isa_features": [],
17
- "compile_flags": [
18
- "-O2",
19
- "-std=c++17"
20
- ],
21
- "link_flags": []
22
- },
23
- "sources": [
24
- {
25
- "path": "gemm.h",
26
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A · B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
27
- },
28
- {
29
- "path": "binding.cpp",
30
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1024;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
31
- },
32
- {
33
- "path": "kernel.cpp",
34
- "content": "#include \"gemm.h\"\n#include <cmath>\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nstatic inline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F, mant = h & 0x3FF, f;\n if (exp == 0) {\n if (mant == 0) f = sign;\n else { exp = 127 - 15 + 1; while (!(mant & 0x400)) { mant <<= 1; exp--; }\n mant &= 0x3FF; f = sign | (exp << 23) | (mant << 13); }\n } else if (exp == 0x1F) { f = sign | 0x7F800000u | (mant << 13); }\n else { f = sign | ((exp - 15 + 127) << 23) | (mant << 13); }\n float o; std::memcpy(&o, &f, 4); return o;\n}\n// Dequant one block_q8_0 row: nb 34-byte blocks {fp16 d; int8 qs[32]} -> nb*32 floats.\nstatic inline void dq_row(const uint8_t* p, int64_t nb, float* dst) {\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = p + b * 34; uint16_t d16; std::memcpy(&d16, bp, 2);\n float d = f16_to_f32(d16); const int8_t* qs = (const int8_t*)(bp + 2);\n for (int j = 0; j < 32; ++j) dst[b * 32 + j] = d * (float)qs[j];\n }\n}\n\n// Naive scalar GEMM reference: C = A . B^T, A[M,K] B[N,K] -> C[M,N].\n// For q8_0, A and B are block_q8_0 rows (K/32 blocks); dequantize then matmul.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8) {\n int64_t nb = K / 32;\n std::vector<float> Af((size_t)M * K), Bf((size_t)N * K);\n if (is_q8) {\n const uint8_t* a = (const uint8_t*)A; const uint8_t* b = (const uint8_t*)B;\n for (int64_t r = 0; r < M; ++r) dq_row(a + (size_t)r * nb * 34, nb, &Af[(size_t)r * K]);\n for (int64_t r = 0; r < N; ++r) dq_row(b + (size_t)r * nb * 34, nb, &Bf[(size_t)r * K]);\n } else {\n std::memcpy(Af.data(), A, (size_t)M * K * sizeof(float));\n std::memcpy(Bf.data(), B, (size_t)N * K * sizeof(float));\n }\n for (int64_t m = 0; m < M; ++m)\n for (int64_t n = 0; n < N; ++n) {\n const float* ar = &Af[(size_t)m * K]; const float* br = &Bf[(size_t)n * K];\n double acc = 0.0; for (int64_t k = 0; k < K; ++k) acc += (double)ar[k] * br[k];\n C[(size_t)m * N + n] = (float)acc;\n }\n return 0;\n}\n"
35
- }
36
- ],
37
- "description": "Naive scalar reference for gemm_fp32_n1024_k2048 (no ggml/SIMD)."
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n1408_k2048.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "name": "reference-scalar_gemm_fp32_n1408_k2048",
3
- "definition": "gemm_fp32_n1408_k2048",
4
- "dataset": "llama.cpp",
5
- "author": "reference-scalar",
6
- "spec": {
7
- "target_hardware": [
8
- "graviton3",
9
- "aarch64-sve",
10
- "graviton4",
11
- "aarch64-sve2",
12
- "apple-m"
13
- ],
14
- "entry_point": "binding.cpp::armbench_entry_gemm",
15
- "dependencies": [],
16
- "isa_features": [],
17
- "compile_flags": [
18
- "-O2",
19
- "-std=c++17"
20
- ],
21
- "link_flags": []
22
- },
23
- "sources": [
24
- {
25
- "path": "gemm.h",
26
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A · B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
27
- },
28
- {
29
- "path": "binding.cpp",
30
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 1408;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
31
- },
32
- {
33
- "path": "kernel.cpp",
34
- "content": "#include \"gemm.h\"\n#include <cmath>\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nstatic inline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F, mant = h & 0x3FF, f;\n if (exp == 0) {\n if (mant == 0) f = sign;\n else { exp = 127 - 15 + 1; while (!(mant & 0x400)) { mant <<= 1; exp--; }\n mant &= 0x3FF; f = sign | (exp << 23) | (mant << 13); }\n } else if (exp == 0x1F) { f = sign | 0x7F800000u | (mant << 13); }\n else { f = sign | ((exp - 15 + 127) << 23) | (mant << 13); }\n float o; std::memcpy(&o, &f, 4); return o;\n}\n// Dequant one block_q8_0 row: nb 34-byte blocks {fp16 d; int8 qs[32]} -> nb*32 floats.\nstatic inline void dq_row(const uint8_t* p, int64_t nb, float* dst) {\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = p + b * 34; uint16_t d16; std::memcpy(&d16, bp, 2);\n float d = f16_to_f32(d16); const int8_t* qs = (const int8_t*)(bp + 2);\n for (int j = 0; j < 32; ++j) dst[b * 32 + j] = d * (float)qs[j];\n }\n}\n\n// Naive scalar GEMM reference: C = A . B^T, A[M,K] B[N,K] -> C[M,N].\n// For q8_0, A and B are block_q8_0 rows (K/32 blocks); dequantize then matmul.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8) {\n int64_t nb = K / 32;\n std::vector<float> Af((size_t)M * K), Bf((size_t)N * K);\n if (is_q8) {\n const uint8_t* a = (const uint8_t*)A; const uint8_t* b = (const uint8_t*)B;\n for (int64_t r = 0; r < M; ++r) dq_row(a + (size_t)r * nb * 34, nb, &Af[(size_t)r * K]);\n for (int64_t r = 0; r < N; ++r) dq_row(b + (size_t)r * nb * 34, nb, &Bf[(size_t)r * K]);\n } else {\n std::memcpy(Af.data(), A, (size_t)M * K * sizeof(float));\n std::memcpy(Bf.data(), B, (size_t)N * K * sizeof(float));\n }\n for (int64_t m = 0; m < M; ++m)\n for (int64_t n = 0; n < N; ++n) {\n const float* ar = &Af[(size_t)m * K]; const float* br = &Bf[(size_t)n * K];\n double acc = 0.0; for (int64_t k = 0; k < K; ++k) acc += (double)ar[k] * br[k];\n C[(size_t)m * N + n] = (float)acc;\n }\n return 0;\n}\n"
35
- }
36
- ],
37
- "description": "Naive scalar reference for gemm_fp32_n1408_k2048 (no ggml/SIMD)."
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n2048_k1024.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "name": "reference-scalar_gemm_fp32_n2048_k1024",
3
- "definition": "gemm_fp32_n2048_k1024",
4
- "dataset": "llama.cpp",
5
- "author": "reference-scalar",
6
- "spec": {
7
- "target_hardware": [
8
- "graviton3",
9
- "aarch64-sve",
10
- "graviton4",
11
- "aarch64-sve2",
12
- "apple-m"
13
- ],
14
- "entry_point": "binding.cpp::armbench_entry_gemm",
15
- "dependencies": [],
16
- "isa_features": [],
17
- "compile_flags": [
18
- "-O2",
19
- "-std=c++17"
20
- ],
21
- "link_flags": []
22
- },
23
- "sources": [
24
- {
25
- "path": "gemm.h",
26
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A · B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
27
- },
28
- {
29
- "path": "binding.cpp",
30
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1024;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
31
- },
32
- {
33
- "path": "kernel.cpp",
34
- "content": "#include \"gemm.h\"\n#include <cmath>\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nstatic inline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F, mant = h & 0x3FF, f;\n if (exp == 0) {\n if (mant == 0) f = sign;\n else { exp = 127 - 15 + 1; while (!(mant & 0x400)) { mant <<= 1; exp--; }\n mant &= 0x3FF; f = sign | (exp << 23) | (mant << 13); }\n } else if (exp == 0x1F) { f = sign | 0x7F800000u | (mant << 13); }\n else { f = sign | ((exp - 15 + 127) << 23) | (mant << 13); }\n float o; std::memcpy(&o, &f, 4); return o;\n}\n// Dequant one block_q8_0 row: nb 34-byte blocks {fp16 d; int8 qs[32]} -> nb*32 floats.\nstatic inline void dq_row(const uint8_t* p, int64_t nb, float* dst) {\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = p + b * 34; uint16_t d16; std::memcpy(&d16, bp, 2);\n float d = f16_to_f32(d16); const int8_t* qs = (const int8_t*)(bp + 2);\n for (int j = 0; j < 32; ++j) dst[b * 32 + j] = d * (float)qs[j];\n }\n}\n\n// Naive scalar GEMM reference: C = A . B^T, A[M,K] B[N,K] -> C[M,N].\n// For q8_0, A and B are block_q8_0 rows (K/32 blocks); dequantize then matmul.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8) {\n int64_t nb = K / 32;\n std::vector<float> Af((size_t)M * K), Bf((size_t)N * K);\n if (is_q8) {\n const uint8_t* a = (const uint8_t*)A; const uint8_t* b = (const uint8_t*)B;\n for (int64_t r = 0; r < M; ++r) dq_row(a + (size_t)r * nb * 34, nb, &Af[(size_t)r * K]);\n for (int64_t r = 0; r < N; ++r) dq_row(b + (size_t)r * nb * 34, nb, &Bf[(size_t)r * K]);\n } else {\n std::memcpy(Af.data(), A, (size_t)M * K * sizeof(float));\n std::memcpy(Bf.data(), B, (size_t)N * K * sizeof(float));\n }\n for (int64_t m = 0; m < M; ++m)\n for (int64_t n = 0; n < N; ++n) {\n const float* ar = &Af[(size_t)m * K]; const float* br = &Bf[(size_t)n * K];\n double acc = 0.0; for (int64_t k = 0; k < K; ++k) acc += (double)ar[k] * br[k];\n C[(size_t)m * N + n] = (float)acc;\n }\n return 0;\n}\n"
35
- }
36
- ],
37
- "description": "Naive scalar reference for gemm_fp32_n2048_k1024 (no ggml/SIMD)."
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n2048_k1408.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "name": "reference-scalar_gemm_fp32_n2048_k1408",
3
- "definition": "gemm_fp32_n2048_k1408",
4
- "dataset": "llama.cpp",
5
- "author": "reference-scalar",
6
- "spec": {
7
- "target_hardware": [
8
- "graviton3",
9
- "aarch64-sve",
10
- "graviton4",
11
- "aarch64-sve2",
12
- "apple-m"
13
- ],
14
- "entry_point": "binding.cpp::armbench_entry_gemm",
15
- "dependencies": [],
16
- "isa_features": [],
17
- "compile_flags": [
18
- "-O2",
19
- "-std=c++17"
20
- ],
21
- "link_flags": []
22
- },
23
- "sources": [
24
- {
25
- "path": "gemm.h",
26
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A · B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
27
- },
28
- {
29
- "path": "binding.cpp",
30
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 1408;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
31
- },
32
- {
33
- "path": "kernel.cpp",
34
- "content": "#include \"gemm.h\"\n#include <cmath>\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nstatic inline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F, mant = h & 0x3FF, f;\n if (exp == 0) {\n if (mant == 0) f = sign;\n else { exp = 127 - 15 + 1; while (!(mant & 0x400)) { mant <<= 1; exp--; }\n mant &= 0x3FF; f = sign | (exp << 23) | (mant << 13); }\n } else if (exp == 0x1F) { f = sign | 0x7F800000u | (mant << 13); }\n else { f = sign | ((exp - 15 + 127) << 23) | (mant << 13); }\n float o; std::memcpy(&o, &f, 4); return o;\n}\n// Dequant one block_q8_0 row: nb 34-byte blocks {fp16 d; int8 qs[32]} -> nb*32 floats.\nstatic inline void dq_row(const uint8_t* p, int64_t nb, float* dst) {\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = p + b * 34; uint16_t d16; std::memcpy(&d16, bp, 2);\n float d = f16_to_f32(d16); const int8_t* qs = (const int8_t*)(bp + 2);\n for (int j = 0; j < 32; ++j) dst[b * 32 + j] = d * (float)qs[j];\n }\n}\n\n// Naive scalar GEMM reference: C = A . B^T, A[M,K] B[N,K] -> C[M,N].\n// For q8_0, A and B are block_q8_0 rows (K/32 blocks); dequantize then matmul.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8) {\n int64_t nb = K / 32;\n std::vector<float> Af((size_t)M * K), Bf((size_t)N * K);\n if (is_q8) {\n const uint8_t* a = (const uint8_t*)A; const uint8_t* b = (const uint8_t*)B;\n for (int64_t r = 0; r < M; ++r) dq_row(a + (size_t)r * nb * 34, nb, &Af[(size_t)r * K]);\n for (int64_t r = 0; r < N; ++r) dq_row(b + (size_t)r * nb * 34, nb, &Bf[(size_t)r * K]);\n } else {\n std::memcpy(Af.data(), A, (size_t)M * K * sizeof(float));\n std::memcpy(Bf.data(), B, (size_t)N * K * sizeof(float));\n }\n for (int64_t m = 0; m < M; ++m)\n for (int64_t n = 0; n < N; ++n) {\n const float* ar = &Af[(size_t)m * K]; const float* br = &Bf[(size_t)n * K];\n double acc = 0.0; for (int64_t k = 0; k < K; ++k) acc += (double)ar[k] * br[k];\n C[(size_t)m * N + n] = (float)acc;\n }\n return 0;\n}\n"
35
- }
36
- ],
37
- "description": "Naive scalar reference for gemm_fp32_n2048_k1408 (no ggml/SIMD)."
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
solutions/llama.cpp/reference-scalar/gemm/gemm_fp32_n2048_k2048.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "name": "reference-scalar_gemm_fp32_n2048_k2048",
3
- "definition": "gemm_fp32_n2048_k2048",
4
- "dataset": "llama.cpp",
5
- "author": "reference-scalar",
6
- "spec": {
7
- "target_hardware": [
8
- "graviton3",
9
- "aarch64-sve",
10
- "graviton4",
11
- "aarch64-sve2",
12
- "apple-m"
13
- ],
14
- "entry_point": "binding.cpp::armbench_entry_gemm",
15
- "dependencies": [],
16
- "isa_features": [],
17
- "compile_flags": [
18
- "-O2",
19
- "-std=c++17"
20
- ],
21
- "link_flags": []
22
- },
23
- "sources": [
24
- {
25
- "path": "gemm.h",
26
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) gemm baseline.\n// Called by armbench_entry_gemm (binding.cpp); implemented by kernel.cpp.\n// A is row-major [M, K], B is row-major [N, K], C = A · B^T row-major [M, N].\n// For q8_0, A and B are ggml block_q8_0 rows: (K/32) 34-byte blocks per row,\n// each block = {fp16 d; int8 qs[32]} (repacked by the Python adapter).\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8);\n"
27
- },
28
- {
29
- "path": "binding.cpp",
30
- "content": "#include \"gemm.h\"\n\nnamespace {\nconstexpr int64_t kN = 2048;\nconstexpr int64_t kK = 2048;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=A fp32 [M,K], [1]=B fp32 [N,K]; var_axes: [0]=M\nint armbench_entry_gemm(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_gemm(inputs[0], inputs[1],\n reinterpret_cast<float*>(output),\n var_axes[0], kN, kK, /*is_q8=*/false);\n}\n} // extern \"C\"\n"
31
- },
32
- {
33
- "path": "kernel.cpp",
34
- "content": "#include \"gemm.h\"\n#include <cmath>\n#include <cstring>\n#include <cstdint>\n#include <vector>\n\nstatic inline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F, mant = h & 0x3FF, f;\n if (exp == 0) {\n if (mant == 0) f = sign;\n else { exp = 127 - 15 + 1; while (!(mant & 0x400)) { mant <<= 1; exp--; }\n mant &= 0x3FF; f = sign | (exp << 23) | (mant << 13); }\n } else if (exp == 0x1F) { f = sign | 0x7F800000u | (mant << 13); }\n else { f = sign | ((exp - 15 + 127) << 23) | (mant << 13); }\n float o; std::memcpy(&o, &f, 4); return o;\n}\n// Dequant one block_q8_0 row: nb 34-byte blocks {fp16 d; int8 qs[32]} -> nb*32 floats.\nstatic inline void dq_row(const uint8_t* p, int64_t nb, float* dst) {\n for (int64_t b = 0; b < nb; ++b) {\n const uint8_t* bp = p + b * 34; uint16_t d16; std::memcpy(&d16, bp, 2);\n float d = f16_to_f32(d16); const int8_t* qs = (const int8_t*)(bp + 2);\n for (int j = 0; j < 32; ++j) dst[b * 32 + j] = d * (float)qs[j];\n }\n}\n\n// Naive scalar GEMM reference: C = A . B^T, A[M,K] B[N,K] -> C[M,N].\n// For q8_0, A and B are block_q8_0 rows (K/32 blocks); dequantize then matmul.\nint armbench_llamacpp_gemm(const void* A, const void* B, float* C,\n int64_t M, int64_t N, int64_t K, bool is_q8) {\n int64_t nb = K / 32;\n std::vector<float> Af((size_t)M * K), Bf((size_t)N * K);\n if (is_q8) {\n const uint8_t* a = (const uint8_t*)A; const uint8_t* b = (const uint8_t*)B;\n for (int64_t r = 0; r < M; ++r) dq_row(a + (size_t)r * nb * 34, nb, &Af[(size_t)r * K]);\n for (int64_t r = 0; r < N; ++r) dq_row(b + (size_t)r * nb * 34, nb, &Bf[(size_t)r * K]);\n } else {\n std::memcpy(Af.data(), A, (size_t)M * K * sizeof(float));\n std::memcpy(Bf.data(), B, (size_t)N * K * sizeof(float));\n }\n for (int64_t m = 0; m < M; ++m)\n for (int64_t n = 0; n < N; ++n) {\n const float* ar = &Af[(size_t)m * K]; const float* br = &Bf[(size_t)n * K];\n double acc = 0.0; for (int64_t k = 0; k < K; ++k) acc += (double)ar[k] * br[k];\n C[(size_t)m * N + n] = (float)acc;\n }\n return 0;\n}\n"
35
- }
36
- ],
37
- "description": "Naive scalar reference for gemm_fp32_n2048_k2048 (no ggml/SIMD)."
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n1024_k2048.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_q4_k_m_n1024_k2048",
3
+ "definition": "gemm_q4_k_m_n1024_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_q4_k_m_n1024_k2048. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm Q4_K (weight-only) specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A is a raw bf16 bit pattern (uint16_t) -- activations stay at the bf16\n// baseline tier (real GGML never stores activations statically in a\n// k-quant format either -- see kernel.cpp for the dynamic quantization this\n// implies). B is Q4_K-quantized, exposed as three flat (non-bit-packed-scale)\n// tensors:\n// B_q4: [N, K/2] uint8 -- byte i holds element 2i (low nibble),\n// element 2i+1 (high nibble), plain\n// sequential order (not ggml's own block-\n// interleaved order -- this is a simplified\n// Definition-level ABI, not GGML's bit-exact\n// block_q4_K struct).\n// B_scales: [N, K/32] raw fp16 bit pattern (uint16_t) -- one value per\n// 32-element sub-block, already combining\n// GGML's per-superblock `d` and per-sub-block\n// 6-bit `sc` into one number.\n// B_mins: [N, K/32] raw fp16 bit pattern (uint16_t) -- same, combining\n// `dmin*m`.\n// Dequant is `w[l] = B_scales[sb]*nibble(l) - B_mins[sb]`.\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 1024;\nconstexpr int K = 2048;\nconstexpr int K_half = 1024;\nconstexpr int K_sub = 64;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B_q4: (N, K/2) uint8;\n// B_scales/B_mins: (N, K/32) raw fp16 bits; output: (M, N) float32.\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B_q4*, B_scales*, B_mins*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output,\n const uint8_t* B_q4, const uint16_t* B_scales,\n const uint16_t* B_mins, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_mins, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm Q4_K (weight-only, genuine low-bit dot product).\n// LLM target: replace this file with an optimised inner_gemm (SVE2/NEON\n// nibble-unpack + int8 dot-product intrinsics are the intended optimization\n// surface here).\n//\n// Unlike Definition.reference (which computes a clean, implementation-\n// agnostic dequant-then-fp32-multiply ground truth), this kernel mirrors\n// real GGML's ggml_vec_dot_q4_K_q8_K: the bf16 activation is dynamically\n// quantized into 256-element blocks (one scale, int8 values -- same idea as\n// ggml's Q8_K), then for each 32-element sub-block an integer dot product\n// against the unpacked Q4_K nibbles is computed and scale-corrected before\n// accumulating into a running fp32 total.\n//\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <vector>\nusing namespace gemm_def;\n\nnamespace {\n\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n\n// Proper IEEE-754 half->single conversion (fp16, not bf16 -- B_scales/B_mins\n// are real fp16, whose exponent field does NOT alias fp32's, so this needs\n// full mantissa/exponent remapping, unlike bf16_to_f32 above).\ninline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F;\n uint32_t mant = h & 0x3FF;\n uint32_t bits;\n if (exp == 0) {\n if (mant == 0) {\n bits = sign;\n } else {\n exp = 1;\n while ((mant & 0x400) == 0) { mant <<= 1; --exp; }\n mant &= 0x3FF;\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n } else if (exp == 0x1F) {\n bits = sign | 0x7F800000u | (mant << 13);\n } else {\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n float f;\n std::memcpy(&f, &bits, sizeof(f));\n return f;\n}\n\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M)\n{\n constexpr int K_super = K / 256;\n\n std::vector<float> h(K);\n std::vector<float> d_act(K_super);\n std::vector<int8_t> q8(K);\n\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n for (int k = 0; k < K; ++k) h[k] = bf16_to_f32(a_row[k]);\n\n // Dynamically quantize the activation row into 256-element blocks\n // (Q8_K-style: one scale, int8 values in [-127, 127]).\n for (int sb = 0; sb < K_super; ++sb) {\n float amax = 0.0f;\n for (int l = 0; l < 256; ++l) {\n float v = std::fabs(h[sb * 256 + l]);\n if (v > amax) amax = v;\n }\n const float d = amax / 127.0f;\n d_act[sb] = d;\n const float id = d > 0.0f ? 1.0f / d : 0.0f;\n for (int l = 0; l < 256; ++l) {\n int q = (int)std::lround(h[sb * 256 + l] * id);\n if (q > 127) q = 127;\n if (q < -127) q = -127;\n q8[sb * 256 + l] = (int8_t)q;\n }\n }\n\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b_row = B_q4 + (long)n * K_half;\n const uint16_t* sc_row = B_scales + (long)n * K_sub;\n const uint16_t* mn_row = B_mins + (long)n * K_sub;\n\n float acc = 0.0f;\n for (int sb32 = 0; sb32 < K_sub; ++sb32) {\n const float scale = f16_to_f32(sc_row[sb32]);\n const float minv = f16_to_f32(mn_row[sb32]);\n const float dact = d_act[sb32 / 8]; // 8 sub-blocks per 256-elem superblock\n\n int32_t sumi = 0;\n int32_t bsum = 0;\n const int base = sb32 * 32;\n for (int l = 0; l < 32; ++l) {\n const int k = base + l;\n const uint8_t byte = b_row[k / 2];\n const int nib = (k % 2 == 0) ? (byte & 0xF) : (byte >> 4);\n const int8_t qv = q8[k];\n sumi += nib * qv;\n bsum += qv;\n }\n acc += dact * (scale * (float)sumi - minv * (float)bsum);\n }\n out_row[n] = acc;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n1408_k2048.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_q4_k_m_n1408_k2048",
3
+ "definition": "gemm_q4_k_m_n1408_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_q4_k_m_n1408_k2048. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm Q4_K (weight-only) specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A is a raw bf16 bit pattern (uint16_t) -- activations stay at the bf16\n// baseline tier (real GGML never stores activations statically in a\n// k-quant format either -- see kernel.cpp for the dynamic quantization this\n// implies). B is Q4_K-quantized, exposed as three flat (non-bit-packed-scale)\n// tensors:\n// B_q4: [N, K/2] uint8 -- byte i holds element 2i (low nibble),\n// element 2i+1 (high nibble), plain\n// sequential order (not ggml's own block-\n// interleaved order -- this is a simplified\n// Definition-level ABI, not GGML's bit-exact\n// block_q4_K struct).\n// B_scales: [N, K/32] raw fp16 bit pattern (uint16_t) -- one value per\n// 32-element sub-block, already combining\n// GGML's per-superblock `d` and per-sub-block\n// 6-bit `sc` into one number.\n// B_mins: [N, K/32] raw fp16 bit pattern (uint16_t) -- same, combining\n// `dmin*m`.\n// Dequant is `w[l] = B_scales[sb]*nibble(l) - B_mins[sb]`.\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 1408;\nconstexpr int K = 2048;\nconstexpr int K_half = 1024;\nconstexpr int K_sub = 64;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B_q4: (N, K/2) uint8;\n// B_scales/B_mins: (N, K/32) raw fp16 bits; output: (M, N) float32.\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B_q4*, B_scales*, B_mins*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output,\n const uint8_t* B_q4, const uint16_t* B_scales,\n const uint16_t* B_mins, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_mins, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm Q4_K (weight-only, genuine low-bit dot product).\n// LLM target: replace this file with an optimised inner_gemm (SVE2/NEON\n// nibble-unpack + int8 dot-product intrinsics are the intended optimization\n// surface here).\n//\n// Unlike Definition.reference (which computes a clean, implementation-\n// agnostic dequant-then-fp32-multiply ground truth), this kernel mirrors\n// real GGML's ggml_vec_dot_q4_K_q8_K: the bf16 activation is dynamically\n// quantized into 256-element blocks (one scale, int8 values -- same idea as\n// ggml's Q8_K), then for each 32-element sub-block an integer dot product\n// against the unpacked Q4_K nibbles is computed and scale-corrected before\n// accumulating into a running fp32 total.\n//\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <vector>\nusing namespace gemm_def;\n\nnamespace {\n\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n\n// Proper IEEE-754 half->single conversion (fp16, not bf16 -- B_scales/B_mins\n// are real fp16, whose exponent field does NOT alias fp32's, so this needs\n// full mantissa/exponent remapping, unlike bf16_to_f32 above).\ninline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F;\n uint32_t mant = h & 0x3FF;\n uint32_t bits;\n if (exp == 0) {\n if (mant == 0) {\n bits = sign;\n } else {\n exp = 1;\n while ((mant & 0x400) == 0) { mant <<= 1; --exp; }\n mant &= 0x3FF;\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n } else if (exp == 0x1F) {\n bits = sign | 0x7F800000u | (mant << 13);\n } else {\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n float f;\n std::memcpy(&f, &bits, sizeof(f));\n return f;\n}\n\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M)\n{\n constexpr int K_super = K / 256;\n\n std::vector<float> h(K);\n std::vector<float> d_act(K_super);\n std::vector<int8_t> q8(K);\n\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n for (int k = 0; k < K; ++k) h[k] = bf16_to_f32(a_row[k]);\n\n // Dynamically quantize the activation row into 256-element blocks\n // (Q8_K-style: one scale, int8 values in [-127, 127]).\n for (int sb = 0; sb < K_super; ++sb) {\n float amax = 0.0f;\n for (int l = 0; l < 256; ++l) {\n float v = std::fabs(h[sb * 256 + l]);\n if (v > amax) amax = v;\n }\n const float d = amax / 127.0f;\n d_act[sb] = d;\n const float id = d > 0.0f ? 1.0f / d : 0.0f;\n for (int l = 0; l < 256; ++l) {\n int q = (int)std::lround(h[sb * 256 + l] * id);\n if (q > 127) q = 127;\n if (q < -127) q = -127;\n q8[sb * 256 + l] = (int8_t)q;\n }\n }\n\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b_row = B_q4 + (long)n * K_half;\n const uint16_t* sc_row = B_scales + (long)n * K_sub;\n const uint16_t* mn_row = B_mins + (long)n * K_sub;\n\n float acc = 0.0f;\n for (int sb32 = 0; sb32 < K_sub; ++sb32) {\n const float scale = f16_to_f32(sc_row[sb32]);\n const float minv = f16_to_f32(mn_row[sb32]);\n const float dact = d_act[sb32 / 8]; // 8 sub-blocks per 256-elem superblock\n\n int32_t sumi = 0;\n int32_t bsum = 0;\n const int base = sb32 * 32;\n for (int l = 0; l < 32; ++l) {\n const int k = base + l;\n const uint8_t byte = b_row[k / 2];\n const int nib = (k % 2 == 0) ? (byte & 0xF) : (byte >> 4);\n const int8_t qv = q8[k];\n sumi += nib * qv;\n bsum += qv;\n }\n acc += dact * (scale * (float)sumi - minv * (float)bsum);\n }\n out_row[n] = acc;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n2048_k1024.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_q4_k_m_n2048_k1024",
3
+ "definition": "gemm_q4_k_m_n2048_k1024",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_q4_k_m_n2048_k1024. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm Q4_K (weight-only) specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A is a raw bf16 bit pattern (uint16_t) -- activations stay at the bf16\n// baseline tier (real GGML never stores activations statically in a\n// k-quant format either -- see kernel.cpp for the dynamic quantization this\n// implies). B is Q4_K-quantized, exposed as three flat (non-bit-packed-scale)\n// tensors:\n// B_q4: [N, K/2] uint8 -- byte i holds element 2i (low nibble),\n// element 2i+1 (high nibble), plain\n// sequential order (not ggml's own block-\n// interleaved order -- this is a simplified\n// Definition-level ABI, not GGML's bit-exact\n// block_q4_K struct).\n// B_scales: [N, K/32] raw fp16 bit pattern (uint16_t) -- one value per\n// 32-element sub-block, already combining\n// GGML's per-superblock `d` and per-sub-block\n// 6-bit `sc` into one number.\n// B_mins: [N, K/32] raw fp16 bit pattern (uint16_t) -- same, combining\n// `dmin*m`.\n// Dequant is `w[l] = B_scales[sb]*nibble(l) - B_mins[sb]`.\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 2048;\nconstexpr int K = 1024;\nconstexpr int K_half = 512;\nconstexpr int K_sub = 32;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B_q4: (N, K/2) uint8;\n// B_scales/B_mins: (N, K/32) raw fp16 bits; output: (M, N) float32.\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B_q4*, B_scales*, B_mins*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output,\n const uint8_t* B_q4, const uint16_t* B_scales,\n const uint16_t* B_mins, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_mins, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm Q4_K (weight-only, genuine low-bit dot product).\n// LLM target: replace this file with an optimised inner_gemm (SVE2/NEON\n// nibble-unpack + int8 dot-product intrinsics are the intended optimization\n// surface here).\n//\n// Unlike Definition.reference (which computes a clean, implementation-\n// agnostic dequant-then-fp32-multiply ground truth), this kernel mirrors\n// real GGML's ggml_vec_dot_q4_K_q8_K: the bf16 activation is dynamically\n// quantized into 256-element blocks (one scale, int8 values -- same idea as\n// ggml's Q8_K), then for each 32-element sub-block an integer dot product\n// against the unpacked Q4_K nibbles is computed and scale-corrected before\n// accumulating into a running fp32 total.\n//\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <vector>\nusing namespace gemm_def;\n\nnamespace {\n\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n\n// Proper IEEE-754 half->single conversion (fp16, not bf16 -- B_scales/B_mins\n// are real fp16, whose exponent field does NOT alias fp32's, so this needs\n// full mantissa/exponent remapping, unlike bf16_to_f32 above).\ninline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F;\n uint32_t mant = h & 0x3FF;\n uint32_t bits;\n if (exp == 0) {\n if (mant == 0) {\n bits = sign;\n } else {\n exp = 1;\n while ((mant & 0x400) == 0) { mant <<= 1; --exp; }\n mant &= 0x3FF;\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n } else if (exp == 0x1F) {\n bits = sign | 0x7F800000u | (mant << 13);\n } else {\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n float f;\n std::memcpy(&f, &bits, sizeof(f));\n return f;\n}\n\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M)\n{\n constexpr int K_super = K / 256;\n\n std::vector<float> h(K);\n std::vector<float> d_act(K_super);\n std::vector<int8_t> q8(K);\n\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n for (int k = 0; k < K; ++k) h[k] = bf16_to_f32(a_row[k]);\n\n // Dynamically quantize the activation row into 256-element blocks\n // (Q8_K-style: one scale, int8 values in [-127, 127]).\n for (int sb = 0; sb < K_super; ++sb) {\n float amax = 0.0f;\n for (int l = 0; l < 256; ++l) {\n float v = std::fabs(h[sb * 256 + l]);\n if (v > amax) amax = v;\n }\n const float d = amax / 127.0f;\n d_act[sb] = d;\n const float id = d > 0.0f ? 1.0f / d : 0.0f;\n for (int l = 0; l < 256; ++l) {\n int q = (int)std::lround(h[sb * 256 + l] * id);\n if (q > 127) q = 127;\n if (q < -127) q = -127;\n q8[sb * 256 + l] = (int8_t)q;\n }\n }\n\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b_row = B_q4 + (long)n * K_half;\n const uint16_t* sc_row = B_scales + (long)n * K_sub;\n const uint16_t* mn_row = B_mins + (long)n * K_sub;\n\n float acc = 0.0f;\n for (int sb32 = 0; sb32 < K_sub; ++sb32) {\n const float scale = f16_to_f32(sc_row[sb32]);\n const float minv = f16_to_f32(mn_row[sb32]);\n const float dact = d_act[sb32 / 8]; // 8 sub-blocks per 256-elem superblock\n\n int32_t sumi = 0;\n int32_t bsum = 0;\n const int base = sb32 * 32;\n for (int l = 0; l < 32; ++l) {\n const int k = base + l;\n const uint8_t byte = b_row[k / 2];\n const int nib = (k % 2 == 0) ? (byte & 0xF) : (byte >> 4);\n const int8_t qv = q8[k];\n sumi += nib * qv;\n bsum += qv;\n }\n acc += dact * (scale * (float)sumi - minv * (float)bsum);\n }\n out_row[n] = acc;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n2048_k1536.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_q4_k_m_n2048_k1536",
3
+ "definition": "gemm_q4_k_m_n2048_k1536",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_q4_k_m_n2048_k1536. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm Q4_K (weight-only) specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A is a raw bf16 bit pattern (uint16_t) -- activations stay at the bf16\n// baseline tier (real GGML never stores activations statically in a\n// k-quant format either -- see kernel.cpp for the dynamic quantization this\n// implies). B is Q4_K-quantized, exposed as three flat (non-bit-packed-scale)\n// tensors:\n// B_q4: [N, K/2] uint8 -- byte i holds element 2i (low nibble),\n// element 2i+1 (high nibble), plain\n// sequential order (not ggml's own block-\n// interleaved order -- this is a simplified\n// Definition-level ABI, not GGML's bit-exact\n// block_q4_K struct).\n// B_scales: [N, K/32] raw fp16 bit pattern (uint16_t) -- one value per\n// 32-element sub-block, already combining\n// GGML's per-superblock `d` and per-sub-block\n// 6-bit `sc` into one number.\n// B_mins: [N, K/32] raw fp16 bit pattern (uint16_t) -- same, combining\n// `dmin*m`.\n// Dequant is `w[l] = B_scales[sb]*nibble(l) - B_mins[sb]`.\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 2048;\nconstexpr int K = 1536;\nconstexpr int K_half = 768;\nconstexpr int K_sub = 48;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B_q4: (N, K/2) uint8;\n// B_scales/B_mins: (N, K/32) raw fp16 bits; output: (M, N) float32.\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B_q4*, B_scales*, B_mins*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output,\n const uint8_t* B_q4, const uint16_t* B_scales,\n const uint16_t* B_mins, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_mins, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm Q4_K (weight-only, genuine low-bit dot product).\n// LLM target: replace this file with an optimised inner_gemm (SVE2/NEON\n// nibble-unpack + int8 dot-product intrinsics are the intended optimization\n// surface here).\n//\n// Unlike Definition.reference (which computes a clean, implementation-\n// agnostic dequant-then-fp32-multiply ground truth), this kernel mirrors\n// real GGML's ggml_vec_dot_q4_K_q8_K: the bf16 activation is dynamically\n// quantized into 256-element blocks (one scale, int8 values -- same idea as\n// ggml's Q8_K), then for each 32-element sub-block an integer dot product\n// against the unpacked Q4_K nibbles is computed and scale-corrected before\n// accumulating into a running fp32 total.\n//\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <vector>\nusing namespace gemm_def;\n\nnamespace {\n\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n\n// Proper IEEE-754 half->single conversion (fp16, not bf16 -- B_scales/B_mins\n// are real fp16, whose exponent field does NOT alias fp32's, so this needs\n// full mantissa/exponent remapping, unlike bf16_to_f32 above).\ninline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F;\n uint32_t mant = h & 0x3FF;\n uint32_t bits;\n if (exp == 0) {\n if (mant == 0) {\n bits = sign;\n } else {\n exp = 1;\n while ((mant & 0x400) == 0) { mant <<= 1; --exp; }\n mant &= 0x3FF;\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n } else if (exp == 0x1F) {\n bits = sign | 0x7F800000u | (mant << 13);\n } else {\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n float f;\n std::memcpy(&f, &bits, sizeof(f));\n return f;\n}\n\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M)\n{\n constexpr int K_super = K / 256;\n\n std::vector<float> h(K);\n std::vector<float> d_act(K_super);\n std::vector<int8_t> q8(K);\n\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n for (int k = 0; k < K; ++k) h[k] = bf16_to_f32(a_row[k]);\n\n // Dynamically quantize the activation row into 256-element blocks\n // (Q8_K-style: one scale, int8 values in [-127, 127]).\n for (int sb = 0; sb < K_super; ++sb) {\n float amax = 0.0f;\n for (int l = 0; l < 256; ++l) {\n float v = std::fabs(h[sb * 256 + l]);\n if (v > amax) amax = v;\n }\n const float d = amax / 127.0f;\n d_act[sb] = d;\n const float id = d > 0.0f ? 1.0f / d : 0.0f;\n for (int l = 0; l < 256; ++l) {\n int q = (int)std::lround(h[sb * 256 + l] * id);\n if (q > 127) q = 127;\n if (q < -127) q = -127;\n q8[sb * 256 + l] = (int8_t)q;\n }\n }\n\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b_row = B_q4 + (long)n * K_half;\n const uint16_t* sc_row = B_scales + (long)n * K_sub;\n const uint16_t* mn_row = B_mins + (long)n * K_sub;\n\n float acc = 0.0f;\n for (int sb32 = 0; sb32 < K_sub; ++sb32) {\n const float scale = f16_to_f32(sc_row[sb32]);\n const float minv = f16_to_f32(mn_row[sb32]);\n const float dact = d_act[sb32 / 8]; // 8 sub-blocks per 256-elem superblock\n\n int32_t sumi = 0;\n int32_t bsum = 0;\n const int base = sb32 * 32;\n for (int l = 0; l < 32; ++l) {\n const int k = base + l;\n const uint8_t byte = b_row[k / 2];\n const int nib = (k % 2 == 0) ? (byte & 0xF) : (byte >> 4);\n const int8_t qv = q8[k];\n sumi += nib * qv;\n bsum += qv;\n }\n acc += dact * (scale * (float)sumi - minv * (float)bsum);\n }\n out_row[n] = acc;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/gemm/gemm_q4_k_m_n2048_k2048.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_gemm_q4_k_m_n2048_k2048",
3
+ "definition": "gemm_q4_k_m_n2048_k2048",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer gemm for gemm_q4_k_m_n2048_k2048. Constexpr-baked dims; armbench_entry_gemm calls inner_gemm. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "gemm.cpp::armbench_entry_gemm",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "gemm.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this gemm Q4_K (weight-only) specialisation.\n// C[m, n] = sum_k A[m, k] * B[n, k] (B is the [N, K] \"weight\" matrix, transposed)\n// A is a raw bf16 bit pattern (uint16_t) -- activations stay at the bf16\n// baseline tier (real GGML never stores activations statically in a\n// k-quant format either -- see kernel.cpp for the dynamic quantization this\n// implies). B is Q4_K-quantized, exposed as three flat (non-bit-packed-scale)\n// tensors:\n// B_q4: [N, K/2] uint8 -- byte i holds element 2i (low nibble),\n// element 2i+1 (high nibble), plain\n// sequential order (not ggml's own block-\n// interleaved order -- this is a simplified\n// Definition-level ABI, not GGML's bit-exact\n// block_q4_K struct).\n// B_scales: [N, K/32] raw fp16 bit pattern (uint16_t) -- one value per\n// 32-element sub-block, already combining\n// GGML's per-superblock `d` and per-sub-block\n// 6-bit `sc` into one number.\n// B_mins: [N, K/32] raw fp16 bit pattern (uint16_t) -- same, combining\n// `dmin*m`.\n// Dequant is `w[l] = B_scales[sb]*nibble(l) - B_mins[sb]`.\n// Output accumulates/returns in fp32.\nnamespace gemm_def {\nconstexpr int N = 2048;\nconstexpr int K = 2048;\nconstexpr int K_half = 1024;\nconstexpr int K_sub = 64;\n} // namespace gemm_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M is the only var dim. A: (M, K) bf16; B_q4: (N, K/2) uint8;\n// B_scales/B_mins: (N, K/32) raw fp16 bits; output: (M, N) float32.\nvoid inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "gemm.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_gemm (no derived dims needed).\n// ABI: armbench_entry_gemm(A*, output*, B_q4*, B_scales*, B_mins*, M)\n#include \"gemm.h\"\nusing namespace gemm_def;\n\nextern \"C\" int armbench_entry_gemm(const uint16_t* A, float* output,\n const uint8_t* B_q4, const uint16_t* B_scales,\n const uint16_t* B_mins, int M)\n{\n inner_gemm(A, output, B_q4, B_scales, B_mins, M);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar gemm Q4_K (weight-only, genuine low-bit dot product).\n// LLM target: replace this file with an optimised inner_gemm (SVE2/NEON\n// nibble-unpack + int8 dot-product intrinsics are the intended optimization\n// surface here).\n//\n// Unlike Definition.reference (which computes a clean, implementation-\n// agnostic dequant-then-fp32-multiply ground truth), this kernel mirrors\n// real GGML's ggml_vec_dot_q4_K_q8_K: the bf16 activation is dynamically\n// quantized into 256-element blocks (one scale, int8 values -- same idea as\n// ggml's Q8_K), then for each 32-element sub-block an integer dot product\n// against the unpacked Q4_K nibbles is computed and scale-corrected before\n// accumulating into a running fp32 total.\n//\n// All per-definition constants live in gemm_def:: (gemm.h).\n#include \"gemm.h\"\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <vector>\nusing namespace gemm_def;\n\nnamespace {\n\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift.\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n\n// Proper IEEE-754 half->single conversion (fp16, not bf16 -- B_scales/B_mins\n// are real fp16, whose exponent field does NOT alias fp32's, so this needs\n// full mantissa/exponent remapping, unlike bf16_to_f32 above).\ninline float f16_to_f32(uint16_t h) {\n uint32_t sign = (uint32_t)(h & 0x8000) << 16;\n uint32_t exp = (h >> 10) & 0x1F;\n uint32_t mant = h & 0x3FF;\n uint32_t bits;\n if (exp == 0) {\n if (mant == 0) {\n bits = sign;\n } else {\n exp = 1;\n while ((mant & 0x400) == 0) { mant <<= 1; --exp; }\n mant &= 0x3FF;\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n } else if (exp == 0x1F) {\n bits = sign | 0x7F800000u | (mant << 13);\n } else {\n bits = sign | ((exp + 112u) << 23) | (mant << 13);\n }\n float f;\n std::memcpy(&f, &bits, sizeof(f));\n return f;\n}\n\n} // namespace\n\nextern \"C\" void inner_gemm(const uint16_t* A, float* output, const uint8_t* B_q4,\n const uint16_t* B_scales, const uint16_t* B_mins, int M)\n{\n constexpr int K_super = K / 256;\n\n std::vector<float> h(K);\n std::vector<float> d_act(K_super);\n std::vector<int8_t> q8(K);\n\n for (int m = 0; m < M; ++m) {\n const uint16_t* a_row = A + (long)m * K;\n for (int k = 0; k < K; ++k) h[k] = bf16_to_f32(a_row[k]);\n\n // Dynamically quantize the activation row into 256-element blocks\n // (Q8_K-style: one scale, int8 values in [-127, 127]).\n for (int sb = 0; sb < K_super; ++sb) {\n float amax = 0.0f;\n for (int l = 0; l < 256; ++l) {\n float v = std::fabs(h[sb * 256 + l]);\n if (v > amax) amax = v;\n }\n const float d = amax / 127.0f;\n d_act[sb] = d;\n const float id = d > 0.0f ? 1.0f / d : 0.0f;\n for (int l = 0; l < 256; ++l) {\n int q = (int)std::lround(h[sb * 256 + l] * id);\n if (q > 127) q = 127;\n if (q < -127) q = -127;\n q8[sb * 256 + l] = (int8_t)q;\n }\n }\n\n float* out_row = output + (long)m * N;\n for (int n = 0; n < N; ++n) {\n const uint8_t* b_row = B_q4 + (long)n * K_half;\n const uint16_t* sc_row = B_scales + (long)n * K_sub;\n const uint16_t* mn_row = B_mins + (long)n * K_sub;\n\n float acc = 0.0f;\n for (int sb32 = 0; sb32 < K_sub; ++sb32) {\n const float scale = f16_to_f32(sc_row[sb32]);\n const float minv = f16_to_f32(mn_row[sb32]);\n const float dact = d_act[sb32 / 8]; // 8 sub-blocks per 256-elem superblock\n\n int32_t sumi = 0;\n int32_t bsum = 0;\n const int base = sb32 * 32;\n for (int l = 0; l < 32; ++l) {\n const int k = base + l;\n const uint8_t byte = b_row[k / 2];\n const int nib = (k % 2 == 0) ? (byte & 0xF) : (byte >> 4);\n const int8_t qv = q8[k];\n sumi += nib * qv;\n bsum += qv;\n }\n acc += dact * (scale * (float)sumi - minv * (float)bsum);\n }\n out_row[n] = acc;\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/mha/mha_bf16_h16_d128_kvh16.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_mha_bf16_h16_d128_kvh16",
3
+ "definition": "mha_bf16_h16_d128_kvh16",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer mha for mha_bf16_h16_d128_kvh16. Constexpr-baked dims; armbench_entry_mha calls inner_mha. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "mha.cpp::armbench_entry_mha",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "mha.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this mha bf16 specialisation.\n// Non-causal softmax(Q\u00b7K^T / sqrt(head_dim))\u00b7V per head.\n// Q/K/V are raw bf16 bit patterns (uint16_t); output accumulates/returns in\n// fp32.\nnamespace mha_def {\nconstexpr int NHeads = 16;\nconstexpr int HeadDim = 128;\nconstexpr int KvHeads = 16;\n} // namespace mha_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// M, S are the var dims. Q: (M, n_heads, head_dim) bf16; K/V: (S, kv_heads,\n// head_dim) bf16; output: (M, n_heads, head_dim) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_mha(const uint16_t* Q, float* output, const uint16_t* K, const uint16_t* V,\n int M, int S);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "mha.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_mha (no derived dims needed).\n// ABI: armbench_entry_mha(Q*, output*, K*, V*, M, S)\n#include \"mha.h\"\nusing namespace mha_def;\n\nextern \"C\" int armbench_entry_mha(const uint16_t* Q, float* output, const uint16_t* K,\n const uint16_t* V, int M, int S)\n{\n inner_mha(Q, output, K, V, M, S);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar mha bf16 (fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_mha.\n// All per-definition constants live in mha_def:: (mha.h).\n// Assumes NHeads == KvHeads (standard MHA, no GQA head-repeat) \u2014 matches this\n// definition's reference implementation exactly.\n#include \"mha.h\"\n#include <cmath>\n#include <cstring>\n#include <vector>\nusing namespace mha_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_mha(const uint16_t* Q, float* output, const uint16_t* K,\n const uint16_t* V, int M, int S)\n{\n const float scale = 1.0f / std::sqrt((float)HeadDim);\n std::vector<float> scores(S);\n\n for (int h = 0; h < NHeads; ++h) {\n for (int m = 0; m < M; ++m) {\n const uint16_t* q_row = Q + ((long)m * NHeads + h) * HeadDim;\n\n float max_score = -INFINITY;\n for (int s = 0; s < S; ++s) {\n const uint16_t* k_row = K + ((long)s * KvHeads + h) * HeadDim;\n float dot = 0.0f;\n for (int d = 0; d < HeadDim; ++d) {\n dot += bf16_to_f32(q_row[d]) * bf16_to_f32(k_row[d]);\n }\n scores[s] = dot * scale;\n if (scores[s] > max_score) max_score = scores[s];\n }\n\n float sum = 0.0f;\n for (int s = 0; s < S; ++s) {\n scores[s] = std::exp(scores[s] - max_score);\n sum += scores[s];\n }\n\n float* out_row = output + ((long)m * NHeads + h) * HeadDim;\n for (int d = 0; d < HeadDim; ++d) out_row[d] = 0.0f;\n for (int s = 0; s < S; ++s) {\n const float w = scores[s] / sum;\n const uint16_t* v_row = V + ((long)s * KvHeads + h) * HeadDim;\n for (int d = 0; d < HeadDim; ++d) {\n out_row[d] += w * bf16_to_f32(v_row[d]);\n }\n }\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }
solutions/llama.cpp/reference-scalar/mha/mha_fp32_h16_d128_kvh16.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "name": "reference-scalar_mha_fp32_h16_d128_kvh16",
3
- "definition": "mha_fp32_h16_d128_kvh16",
4
- "dataset": "llama.cpp",
5
- "author": "reference-scalar",
6
- "spec": {
7
- "target_hardware": [
8
- "graviton3",
9
- "aarch64-sve",
10
- "graviton4",
11
- "aarch64-sve2",
12
- "apple-m"
13
- ],
14
- "entry_point": "binding.cpp::armbench_entry_mha",
15
- "dependencies": [],
16
- "isa_features": [],
17
- "compile_flags": [
18
- "-O2",
19
- "-std=c++17"
20
- ],
21
- "link_flags": []
22
- },
23
- "sources": [
24
- {
25
- "path": "mha.h",
26
- "content": "#pragma once\n#include <cstdint>\n\n// Harness contract for the llama.cpp (ggml) mha baseline.\n// Called by armbench_entry_mha (binding.cpp); implemented by kernel.cpp.\n// Q is row-major fp32 [M, n_heads, head_dim]; K/V are row-major fp32\n// [S, kv_heads, head_dim]; out is row-major fp32 [M, n_heads, head_dim].\n// Non-causal softmax(Q·K^T / sqrt(head_dim))·V per head.\nint armbench_llamacpp_mha(const float* Q, const float* K, const float* V,\n float* out, int64_t M, int64_t S,\n int64_t n_heads, int64_t head_dim, int64_t kv_heads);\n"
27
- },
28
- {
29
- "path": "binding.cpp",
30
- "content": "#include \"mha.h\"\n\nnamespace {\nconstexpr int64_t kNHeads = 16;\nconstexpr int64_t kHeadDim = 128;\nconstexpr int64_t kKvHeads = 16;\n} // namespace\n\nextern \"C\" {\n// inputs: [0]=Q fp32 [M,n_heads,head_dim], [1]=K fp32 [S,kv_heads,head_dim],\n// [2]=V fp32 [S,kv_heads,head_dim]; var_axes: [0]=M, [1]=S\nint armbench_entry_mha(const void* const* inputs, void* output,\n const int64_t* var_axes)\n{\n return armbench_llamacpp_mha(\n reinterpret_cast<const float*>(inputs[0]),\n reinterpret_cast<const float*>(inputs[1]),\n reinterpret_cast<const float*>(inputs[2]),\n reinterpret_cast<float*>(output),\n var_axes[0], var_axes[1], kNHeads, kHeadDim, kKvHeads);\n}\n} // extern \"C\"\n"
31
- },
32
- {
33
- "path": "kernel.cpp",
34
- "content": "#include \"mha.h\"\n#include <cmath>\n#include <vector>\n// Naive scalar non-causal MHA reference: softmax(Q.K^T / sqrt(head_dim)) . V per head.\n// Q[M,H,D] row-major; K/V[S,KVH,D]; out[M,H,D]. GQA: query head h -> kv head h*KVH/H.\nint armbench_llamacpp_mha(const float* Q, const float* K, const float* V,\n float* out, int64_t M, int64_t S,\n int64_t n_heads, int64_t head_dim, int64_t kv_heads) {\n float scale = 1.0f / std::sqrt((float)head_dim);\n std::vector<float> sc((size_t)S);\n for (int64_t h = 0; h < n_heads; ++h) {\n int64_t kvh = (kv_heads == n_heads) ? h : (h * kv_heads / n_heads);\n for (int64_t m = 0; m < M; ++m) {\n const float* q = Q + ((size_t)m * n_heads + h) * head_dim;\n float mx = -1e30f;\n for (int64_t s = 0; s < S; ++s) {\n const float* k = K + ((size_t)s * kv_heads + kvh) * head_dim;\n double acc = 0.0; for (int64_t d = 0; d < head_dim; ++d) acc += (double)q[d] * k[d];\n sc[s] = (float)acc * scale; if (sc[s] > mx) mx = sc[s];\n }\n double sum = 0.0;\n for (int64_t s = 0; s < S; ++s) { sc[s] = std::exp(sc[s] - mx); sum += sc[s]; }\n float inv = (float)(1.0 / sum);\n float* orow = out + ((size_t)m * n_heads + h) * head_dim;\n for (int64_t d = 0; d < head_dim; ++d) {\n double acc = 0.0;\n for (int64_t s = 0; s < S; ++s)\n acc += (double)sc[s] * inv * V[((size_t)s * kv_heads + kvh) * head_dim + d];\n orow[d] = (float)acc;\n }\n }\n }\n return 0;\n}\n"
35
- }
36
- ],
37
- "description": "Naive scalar reference for mha_fp32_h16_d128_kvh16 (no ggml/SIMD)."
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
solutions/llama.cpp/reference-scalar/moe/moe_bf16_e60_k4_d2048_ff1408.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "reference-scalar_moe_bf16_e60_k4_d2048_ff1408",
3
+ "definition": "moe_bf16_e60_k4_d2048_ff1408",
4
+ "dataset": "llama.cpp",
5
+ "author": "reference-scalar",
6
+ "description": "Scalar raw-pointer moe for moe_bf16_e60_k4_d2048_ff1408. Constexpr-baked dims; armbench_entry_moe calls inner_moe. Ground-truth correctness baseline.",
7
+ "spec": {
8
+ "language": "cpp",
9
+ "target_hardware": [
10
+ "graviton3",
11
+ "aarch64-sve"
12
+ ],
13
+ "entry_point": "moe.cpp::armbench_entry_moe",
14
+ "dependencies": [],
15
+ "isa_features": [],
16
+ "compile_flags": [
17
+ "-O2",
18
+ "-std=c++14"
19
+ ],
20
+ "link_flags": []
21
+ },
22
+ "sources": [
23
+ {
24
+ "path": "moe.h",
25
+ "content": "#pragma once\n#include <cstdint>\n\n// Per-definition constants for this moe bf16 specialisation.\n// SwiGLU MoE with softmax-then-top-k routing and normalized top-k weights:\n// probs = softmax(hidden @ router^T); top-k experts; w /= sum(w)\n// out = sum_k w_k * down_e @ (silu(gate_e @ x) * (up_e @ x))\n// hidden_states/router_weight/gate_proj/up_proj/down_proj are raw bf16 bit\n// patterns (uint16_t); output accumulates/returns in fp32.\nnamespace moe_def {\nconstexpr int NEmbd = 2048;\nconstexpr int NFf = 1408;\nconstexpr int NExpert = 60;\nconstexpr int NExpertUsed = 4;\n} // namespace moe_def\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n// LLM target: implement this in kernel.cpp.\n// n_tokens is the only var dim. hidden_states: (n_tokens, n_embd) bf16;\n// router_weight: (n_expert, n_embd) bf16; gate/up_proj: (n_expert, n_ff,\n// n_embd) bf16; down_proj: (n_expert, n_embd, n_ff) bf16;\n// output: (n_tokens, n_embd) float32.\n// Bytes are raw bf16 bit patterns \u2014 reinterpret_cast<const __bf16*> if your\n// target ISA has native bf16 support, or widen manually to fp32 first.\nvoid inner_moe(const uint16_t* hidden_states, float* output,\n const uint16_t* router_weight, const uint16_t* gate_proj,\n const uint16_t* up_proj, const uint16_t* down_proj,\n int n_tokens);\n#ifdef __cplusplus\n}\n#endif\n"
26
+ },
27
+ {
28
+ "path": "moe.cpp",
29
+ "content": "// Binding harness: forwards straight to inner_moe (no derived dims needed).\n// ABI: armbench_entry_moe(hidden_states*, output*, router_weight*, gate_proj*,\n// up_proj*, down_proj*, n_tokens)\n#include \"moe.h\"\nusing namespace moe_def;\n\nextern \"C\" int armbench_entry_moe(const uint16_t* hidden_states, float* output,\n const uint16_t* router_weight, const uint16_t* gate_proj,\n const uint16_t* up_proj, const uint16_t* down_proj,\n int n_tokens)\n{\n inner_moe(hidden_states, output, router_weight, gate_proj, up_proj, down_proj, n_tokens);\n return 0;\n}\n"
30
+ },
31
+ {
32
+ "path": "kernel.cpp",
33
+ "content": "// Reference-scalar moe bf16 (fp32 accumulate/output).\n// LLM target: replace this file with an optimised inner_moe.\n// All per-definition constants live in moe_def:: (moe.h).\n#include \"moe.h\"\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <vector>\nusing namespace moe_def;\n\nnamespace {\n// bf16 shares fp32's exponent field, so widening is an exact bit-shift \u2014 no\n// rounding/subnormal handling needed (unlike fp16->fp32).\ninline float bf16_to_f32(uint16_t bits) {\n uint32_t b = (uint32_t)bits << 16;\n float f;\n std::memcpy(&f, &b, sizeof(f));\n return f;\n}\n} // namespace\n\nextern \"C\" void inner_moe(const uint16_t* hidden_states, float* output,\n const uint16_t* router_weight, const uint16_t* gate_proj,\n const uint16_t* up_proj, const uint16_t* down_proj,\n int n_tokens)\n{\n std::vector<float> h(NEmbd);\n std::vector<float> logits(NExpert);\n std::vector<float> probs(NExpert);\n std::vector<bool> used(NExpert);\n std::vector<int> top_idx(NExpertUsed);\n std::vector<float> top_w(NExpertUsed);\n std::vector<float> gu(NFf);\n\n for (int t = 0; t < n_tokens; ++t) {\n const uint16_t* hs_row = hidden_states + (long)t * NEmbd;\n for (int d = 0; d < NEmbd; ++d) h[d] = bf16_to_f32(hs_row[d]);\n\n // Router logits + softmax over all experts.\n for (int e = 0; e < NExpert; ++e) {\n const uint16_t* rw = router_weight + (long)e * NEmbd;\n float dot = 0.0f;\n for (int d = 0; d < NEmbd; ++d) dot += h[d] * bf16_to_f32(rw[d]);\n logits[e] = dot;\n }\n float max_logit = logits[0];\n for (int e = 1; e < NExpert; ++e) if (logits[e] > max_logit) max_logit = logits[e];\n float sum_exp = 0.0f;\n for (int e = 0; e < NExpert; ++e) {\n probs[e] = std::exp(logits[e] - max_logit);\n sum_exp += probs[e];\n }\n for (int e = 0; e < NExpert; ++e) probs[e] /= sum_exp;\n\n // Top-k selection (repeated argmax over unselected experts) + renormalize.\n std::fill(used.begin(), used.end(), false);\n for (int ki = 0; ki < NExpertUsed; ++ki) {\n int best = -1;\n for (int e = 0; e < NExpert; ++e) {\n if (!used[e] && (best < 0 || probs[e] > probs[best])) best = e;\n }\n used[best] = true;\n top_idx[ki] = best;\n top_w[ki] = probs[best];\n }\n float wsum = 0.0f;\n for (int ki = 0; ki < NExpertUsed; ++ki) wsum += top_w[ki];\n for (int ki = 0; ki < NExpertUsed; ++ki) top_w[ki] /= wsum;\n\n float* out_row = output + (long)t * NEmbd;\n for (int d = 0; d < NEmbd; ++d) out_row[d] = 0.0f;\n\n for (int ki = 0; ki < NExpertUsed; ++ki) {\n const int e = top_idx[ki];\n const uint16_t* gate_e = gate_proj + (long)e * NFf * NEmbd;\n const uint16_t* up_e = up_proj + (long)e * NFf * NEmbd;\n const uint16_t* down_e = down_proj + (long)e * NEmbd * NFf;\n\n for (int f = 0; f < NFf; ++f) {\n const uint16_t* gate_row = gate_e + (long)f * NEmbd;\n const uint16_t* up_row = up_e + (long)f * NEmbd;\n float gv = 0.0f, uv = 0.0f;\n for (int d = 0; d < NEmbd; ++d) {\n gv += bf16_to_f32(gate_row[d]) * h[d];\n uv += bf16_to_f32(up_row[d]) * h[d];\n }\n const float silu = gv / (1.0f + std::exp(-gv));\n gu[f] = silu * uv;\n }\n\n for (int d = 0; d < NEmbd; ++d) {\n float acc = 0.0f;\n const uint16_t* down_row = down_e + (long)d * NFf;\n for (int f = 0; f < NFf; ++f) {\n acc += bf16_to_f32(down_row[f]) * gu[f];\n }\n out_row[d] += top_w[ki] * acc;\n }\n }\n }\n}\n"
34
+ }
35
+ ]
36
+ }