Restore orphaned moe_fp32 solutions (reference-scalar + ggml baseline)
#2
by awu12 - opened
- solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_fp32_e60_k4_d2048_ff1408.json +39 -0
- solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_fp32_e64_k8_d2048_ff1024.json +39 -0
- solutions/llama.cpp/reference-scalar/moe/moe_fp32_e60_k4_d2048_ff1408.json +36 -0
- solutions/llama.cpp/reference-scalar/moe/moe_fp32_e64_k8_d2048_ff1024.json +36 -0
solutions/llama.cpp/baseline-llamacpp-arm/moe/moe_fp32_e60_k4_d2048_ff1408.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton3",
|
| 11 |
+
"aarch64-sve",
|
| 12 |
+
"graviton4",
|
| 13 |
+
"aarch64-sve2",
|
| 14 |
+
"apple-m"
|
| 15 |
+
],
|
| 16 |
+
"entry_point": "binding.cpp::armbench_entry_moe",
|
| 17 |
+
"dependencies": [],
|
| 18 |
+
"isa_features": [],
|
| 19 |
+
"compile_flags": [
|
| 20 |
+
"-O3",
|
| 21 |
+
"-std=c++17"
|
| 22 |
+
],
|
| 23 |
+
"link_flags": []
|
| 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 fp32 [T, n_embd]; router_weight fp32 [n_expert, n_embd];\n// gate/up_proj fp32 [n_expert, n_ff, n_embd]; down_proj fp32 [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 fp32 [T,n_embd], [1]=router_weight bf16\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 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_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<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
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.",
|
| 7 |
+
"spec": {
|
| 8 |
+
"language": "cpp",
|
| 9 |
+
"target_hardware": [
|
| 10 |
+
"graviton3",
|
| 11 |
+
"aarch64-sve",
|
| 12 |
+
"graviton4",
|
| 13 |
+
"aarch64-sve2",
|
| 14 |
+
"apple-m"
|
| 15 |
+
],
|
| 16 |
+
"entry_point": "binding.cpp::armbench_entry_moe",
|
| 17 |
+
"dependencies": [],
|
| 18 |
+
"isa_features": [],
|
| 19 |
+
"compile_flags": [
|
| 20 |
+
"-O3",
|
| 21 |
+
"-std=c++17"
|
| 22 |
+
],
|
| 23 |
+
"link_flags": []
|
| 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 fp32 [T, n_embd]; router_weight fp32 [n_expert, n_embd];\n// gate/up_proj fp32 [n_expert, n_ff, n_embd]; down_proj fp32 [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 fp32 [T,n_embd], [1]=router_weight bf16\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 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_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<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/reference-scalar/moe/moe_fp32_e60_k4_d2048_ff1408.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_moe_fp32_e60_k4_d2048_ff1408",
|
| 3 |
+
"definition": "moe_fp32_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 fp32 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 fp32 bit\n// patterns (float); 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) fp32;\n// router_weight: (n_expert, n_embd) fp32; gate/up_proj: (n_expert, n_ff,\n// n_embd) fp32; down_proj: (n_expert, n_embd, n_ff) fp32;\n// output: (n_tokens, n_embd) float32.\n// Bytes are raw fp32 bit patterns \u2014 reinterpret_cast<const __fp32*> if your\n// target ISA has native fp32 support, or widen manually to fp32 first.\nvoid inner_moe(const float* hidden_states, float* output,\n const float* router_weight, const float* gate_proj,\n const float* up_proj, const float* 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 float* hidden_states, float* output,\n const float* router_weight, const float* gate_proj,\n const float* up_proj, const float* 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 fp32 (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\n\nextern \"C\" void inner_moe(const float* hidden_states, float* output,\n const float* router_weight, const float* gate_proj,\n const float* up_proj, const float* 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 float* hs_row = hidden_states + (long)t * NEmbd;\n for (int d = 0; d < NEmbd; ++d) h[d] = hs_row[d];\n\n // Router logits + softmax over all experts.\n for (int e = 0; e < NExpert; ++e) {\n const float* rw = router_weight + (long)e * NEmbd;\n float dot = 0.0f;\n for (int d = 0; d < NEmbd; ++d) dot += h[d] * 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 float* gate_e = gate_proj + (long)e * NFf * NEmbd;\n const float* up_e = up_proj + (long)e * NFf * NEmbd;\n const float* down_e = down_proj + (long)e * NEmbd * NFf;\n\n for (int f = 0; f < NFf; ++f) {\n const float* gate_row = gate_e + (long)f * NEmbd;\n const float* 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 += gate_row[d] * h[d];\n uv += 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 float* down_row = down_e + (long)d * NFf;\n for (int f = 0; f < NFf; ++f) {\n acc += down_row[f] * gu[f];\n }\n out_row[d] += top_w[ki] * acc;\n }\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
solutions/llama.cpp/reference-scalar/moe/moe_fp32_e64_k8_d2048_ff1024.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "reference-scalar_moe_fp32_e64_k8_d2048_ff1024",
|
| 3 |
+
"definition": "moe_fp32_e64_k8_d2048_ff1024",
|
| 4 |
+
"dataset": "llama.cpp",
|
| 5 |
+
"author": "reference-scalar",
|
| 6 |
+
"description": "Scalar raw-pointer moe for moe_bf16_e64_k8_d2048_ff1024. 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 fp32 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 fp32 bit\n// patterns (float); output accumulates/returns in fp32.\nnamespace moe_def {\nconstexpr int NEmbd = 2048;\nconstexpr int NFf = 1024;\nconstexpr int NExpert = 64;\nconstexpr int NExpertUsed = 8;\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) fp32;\n// router_weight: (n_expert, n_embd) fp32; gate/up_proj: (n_expert, n_ff,\n// n_embd) fp32; down_proj: (n_expert, n_embd, n_ff) fp32;\n// output: (n_tokens, n_embd) float32.\n// Bytes are raw fp32 bit patterns \u2014 reinterpret_cast<const __fp32*> if your\n// target ISA has native fp32 support, or widen manually to fp32 first.\nvoid inner_moe(const float* hidden_states, float* output,\n const float* router_weight, const float* gate_proj,\n const float* up_proj, const float* 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 float* hidden_states, float* output,\n const float* router_weight, const float* gate_proj,\n const float* up_proj, const float* 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 fp32 (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\n\nextern \"C\" void inner_moe(const float* hidden_states, float* output,\n const float* router_weight, const float* gate_proj,\n const float* up_proj, const float* 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 float* hs_row = hidden_states + (long)t * NEmbd;\n for (int d = 0; d < NEmbd; ++d) h[d] = hs_row[d];\n\n // Router logits + softmax over all experts.\n for (int e = 0; e < NExpert; ++e) {\n const float* rw = router_weight + (long)e * NEmbd;\n float dot = 0.0f;\n for (int d = 0; d < NEmbd; ++d) dot += h[d] * 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 float* gate_e = gate_proj + (long)e * NFf * NEmbd;\n const float* up_e = up_proj + (long)e * NFf * NEmbd;\n const float* down_e = down_proj + (long)e * NEmbd * NFf;\n\n for (int f = 0; f < NFf; ++f) {\n const float* gate_row = gate_e + (long)f * NEmbd;\n const float* 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 += gate_row[d] * h[d];\n uv += 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 float* down_row = down_e + (long)d * NFf;\n for (int f = 0; f < NFf; ++f) {\n acc += down_row[f] * gu[f];\n }\n out_row[d] += top_w[ki] * acc;\n }\n }\n }\n}\n"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|