diff --git a/common/common.cpp b/common/common.cpp index 459e7c27..717f9466 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1004,6 +1004,10 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa params.fused_moe_up_gate = true; return true; } + if (arg == "-no-fug" || arg == "--no-fused-up-gate") { + params.fused_up_gate = false; + return true; + } if (arg == "-ser" || arg == "--smart-expert-reduction") { CHECK_ARG auto values = string_split_pairs(argv[i], ','); @@ -1760,6 +1764,7 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param options.push_back({ "*", "-mla, --mla-use", "enable MLA (default: %d)", params.mla_attn }); options.push_back({ "*", "-amb, --attention-max-batch", "max batch size for attention computations (default: %d)", params.attn_max_batch}); options.push_back({ "*", "-fmoe, --fused-moe", "enable fused MoE (default: %s)", params.fused_moe_up_gate ? "enabled" : "disabled" }); + options.push_back({ "*", "-no-fug, --no-fused-up-gate", "disaable fused up-gate (default: %s)", params.fused_up_gate ? "enabled" : "disabled" }); options.push_back({ "*", "-ser, --smart-expert-reduction,","experts reduction (default: %d,%g)", params.min_experts, params.thresh_experts}); options.push_back({ "*", "-p, --prompt PROMPT", "prompt to start generation with\n" "in conversation mode, this will be used as system prompt\n" @@ -2660,6 +2665,7 @@ struct llama_context_params llama_context_params_from_gpt_params(const gpt_param cparams.mla_attn = params.mla_attn; cparams.attn_max_batch = params.attn_max_batch; cparams.fused_moe_up_gate = params.fused_moe_up_gate; + cparams.fused_up_gate = params.fused_up_gate; cparams.min_experts = params.min_experts; cparams.thresh_experts = params.thresh_experts; @@ -3756,6 +3762,7 @@ void yaml_dump_non_result_info(FILE * stream, const gpt_params & params, const l fprintf(stream, "mla_attn: %d # default: 0\n", params.mla_attn); fprintf(stream, "attn_max_batch: %d # default: 0\n", params.attn_max_batch); fprintf(stream, "fused_moe: %s # default: false\n", params.fused_moe_up_gate ? "true" : "false"); + fprintf(stream, "fused_up_gate: %s # default: true\n", params.fused_up_gate ? "true" : "false"); fprintf(stream, "ser: %d,%g # defaulr: -1,0\n", params.min_experts, params.thresh_experts); fprintf(stream, "temp: %f # default: 0.8\n", sparams.temp); diff --git a/common/common.h b/common/common.h index fb15c77f..6e705eef 100644 --- a/common/common.h +++ b/common/common.h @@ -191,6 +191,7 @@ struct gpt_params { int mla_attn = 0; // MLA 0: standard attention, 1: MLA with K and transposed V cache, 2: MLA with just K cache int attn_max_batch = 0; // Max batch size to use when computing attention (only applicable if flash_attn = false) bool fused_moe_up_gate = false; // fused up*unary(gate) op for MoE models + bool fused_up_gate = true; // fused up*unary(gate) op int min_experts = -1; float thresh_experts = 0; diff --git a/examples/llama-bench/llama-bench.cpp b/examples/llama-bench/llama-bench.cpp index 74f51494..6bb646bd 100644 --- a/examples/llama-bench/llama-bench.cpp +++ b/examples/llama-bench/llama-bench.cpp @@ -261,6 +261,7 @@ struct cmd_params { bool warmup; bool repack = false; bool fmoe = false; + bool no_fug = false; bool use_thp = false; output_formats output_format; output_formats output_format_stderr; @@ -297,6 +298,7 @@ static const cmd_params cmd_params_defaults = { /* repack */ false, /* use_thp */ false, /* fmoe */ false, + /* no_fug */ false, /* output_format */ MARKDOWN, /* output_format_stderr */ NONE, }; @@ -339,6 +341,7 @@ static void print_usage(int /* argc */, char ** argv) { printf(" -thp, --transparent-huge-pages <0|1> (default: %s)\n", cmd_params_defaults.use_thp? "1" : "0"); printf(" -ot, --override-tensor pattern (default: none)\n"); printf(" -fmoe, --fused-moe <0|1> (default: %s)\n", cmd_params_defaults.fmoe? "1" : "0"); + printf(" -no-fug, --no-fused-up-gate <0|1> (default: %s)\n", cmd_params_defaults.no_fug? "1" : "0"); printf("\n"); printf("Multiple values can be given for each parameter by separating them with ',' or by specifying the parameter multiple times.\n"); } @@ -736,6 +739,12 @@ static cmd_params parse_cmd_params(int argc, char ** argv) { break; } params.fmoe = std::stoi(argv[i]); + } else if (arg == "-no-fug" || arg == "--no-fused-up-gate") { + if (++i >= argc) { + invalid_param = true; + break; + } + params.no_fug = std::stoi(argv[i]); } else if (arg == "-ot" || arg == "--override-tensor") { if (++i >= argc) { invalid_param = true; @@ -820,6 +829,7 @@ struct cmd_params_instance { bool embeddings; bool repack = false; bool fmoe = false; + bool no_fug = false; bool use_thp = false; const llama_model_tensor_buft_override* buft_overrides; @@ -866,6 +876,7 @@ struct cmd_params_instance { cparams.mla_attn = mla_attn; cparams.attn_max_batch = attn_max_batch; cparams.fused_moe_up_gate = fmoe; + cparams.fused_up_gate = !no_fug; cparams.min_experts = ser.first; cparams.thresh_experts = ser.second; cparams.embeddings = embeddings; @@ -924,6 +935,7 @@ static std::vector get_cmd_params_instances(const cmd_param /* .embeddings = */ embd, /* .repack = */ params.repack, /* .fmoe = */ params.fmoe, + /* .no_fug = */ params.no_fug, /* .use_thp = */ params.use_thp, /* .buft_overrides=*/ params.buft_overrides.data(), }; @@ -958,6 +970,7 @@ static std::vector get_cmd_params_instances(const cmd_param /* .embeddings = */ embd, /* .repack = */ params.repack, /* .fmoe = */ params.fmoe, + /* .no_fug = */ params.no_fug, /* .use_thp = */ params.use_thp, /* .buft_overrides=*/ params.buft_overrides.data(), }; @@ -992,6 +1005,7 @@ static std::vector get_cmd_params_instances(const cmd_param /* .embeddings = */ embd, /* .repack = */ params.repack, /* .fmoe = */ params.fmoe, + /* .no_fug = */ params.no_fug, /* .use_thp = */ params.use_thp, /* .buft_overrides=*/ params.buft_overrides.data(), }; @@ -1026,6 +1040,7 @@ static std::vector get_cmd_params_instances(const cmd_param /* .embeddings = */ embd, /* .repack = */ params.repack, /* .fmoe = */ params.fmoe, + /* .no_fug = */ params.no_fug, /* .use_thp = */ params.use_thp, /* .buft_overrides=*/ params.buft_overrides.data(), }; @@ -1071,6 +1086,7 @@ struct test { bool embeddings; bool repack = false; bool fmoe = false; + bool no_fug = false; bool use_thp = false; int n_prompt; int n_gen; @@ -1104,7 +1120,7 @@ struct test { use_mmap = inst.use_mmap; embeddings = inst.embeddings; repack = inst.repack; - fmoe = inst.fmoe; + no_fug = inst.no_fug; use_thp = inst.use_thp; n_prompt = inst.n_prompt; n_gen = inst.n_gen; @@ -1196,7 +1212,7 @@ struct test { "n_threads", "type_k", "type_v", "n_gpu_layers", "split_mode", "main_gpu", "no_kv_offload", "flash_attn", "mla_attn", "attn_max_batch", "ser", - "tensor_split", "use_mmap", "embeddings", "repack", "fused_moe", "use_thp", + "tensor_split", "use_mmap", "embeddings", "repack", "fused_moe", "fused_up_gate", "use_thp", "n_prompt", "n_gen", "test_time", "avg_ns", "stddev_ns", "avg_ts", "stddev_ts", "test", @@ -1218,7 +1234,7 @@ struct test { if (field == "cuda" || field == "vulkan" || field == "kompute" || field == "metal" || field == "gpu_blas" || field == "blas" || field == "sycl" ||field == "f16_kv" || field == "no_kv_offload" || field == "flash_attn" || field == "use_mmap" || field == "embeddings" || field == "repack" || field == "use_thp" || - field == "fused_moe") { + field == "fused_moe" || field == "fused_up_gate") { return BOOL; } if (field == "avg_ts" || field == "stddev_ts") { @@ -1261,7 +1277,7 @@ struct test { std::to_string(main_gpu), std::to_string(no_kv_offload), std::to_string(flash_attn), std::to_string(mla_attn), std::to_string(attn_max_batch), ser_to_string(ser), tensor_split_str, std::to_string(use_mmap), std::to_string(embeddings), - std::to_string(repack), std::to_string(fmoe), std::to_string(use_thp), + std::to_string(repack), std::to_string(fmoe), std::to_string(no_fug), std::to_string(use_thp), std::to_string(n_prompt), std::to_string(n_gen), test_time, std::to_string(avg_ns()), std::to_string(stdev_ns()), std::to_string(avg_ts()), std::to_string(stdev_ts()), @@ -1445,6 +1461,9 @@ struct markdown_printer : public printer { if (field == "fused_moe") { return 4; } + if (field == "fused_up_gate") { + return 6; + } if (field == "test") { return 13; } @@ -1494,6 +1513,9 @@ struct markdown_printer : public printer { if (field == "fused_moe") { return "fmoe"; } + if (field == "fused_up_gate") { + return "no-fug"; + } if (field == "embeddings") { return "embd"; } @@ -1567,6 +1589,9 @@ struct markdown_printer : public printer { if (params.fmoe != cmd_params_defaults.fmoe) { fields.emplace_back("fused_moe"); } + if (params.no_fug != cmd_params_defaults.no_fug) { + fields.emplace_back("fused_up_gate"); + } fields.emplace_back("test"); fields.emplace_back("t/s"); diff --git a/include/llama.h b/include/llama.h index a5939769..6095d404 100644 --- a/include/llama.h +++ b/include/llama.h @@ -419,7 +419,8 @@ extern "C" { bool flash_attn; // whether to use flash attention [EXPERIMENTAL] int mla_attn; // whether to use MLA attention [EXPERIMENTAL] int attn_max_batch; // maximum batch size for attention computations [EXPERIMENTAL] - bool fused_moe_up_gate; // whether to use fused MoE up/down op [EXPERIMENTAL] + bool fused_moe_up_gate; // whether to use fused MoE up/gate op + bool fused_up_gate; // whether to use fused up/gate op [EXPERIMENTAL] int min_experts; float thresh_experts; diff --git a/src/llama.cpp b/src/llama.cpp index 11e00c22..20d2ec4b 100644 --- a/src/llama.cpp +++ b/src/llama.cpp @@ -2072,6 +2072,7 @@ struct llama_cparams { int mla_attn; int attn_max_batch; bool fused_moe_up_gate; + bool fused_up_gate; int min_experts; float thresh_experts; @@ -7613,8 +7614,9 @@ static struct ggml_tensor * llm_build_ffn( const llm_build_cb & cb, int il) { - if (up && gate && !up_b && !up_s && !gate_b && !gate_s && type_gate == LLM_FFN_PAR && - (type_op == LLM_FFN_SILU || type_op == LLM_FFN_RELU || (type_op == LLM_FFN_GELU && !act_scales))) { + if (lctx.cparams.fused_up_gate && + up && gate && !up_b && !up_s && !gate_b && !gate_s && type_gate == LLM_FFN_PAR && + (type_op == LLM_FFN_SILU || type_op == LLM_FFN_RELU || (type_op == LLM_FFN_GELU && !act_scales))) { auto unary_op = type_op == LLM_FFN_SILU ? GGML_UNARY_OP_SILU : type_op == LLM_FFN_RELU ? GGML_UNARY_OP_RELU : GGML_UNARY_OP_GELU; cur = ggml_fused_up_gate(ctx, up, gate, cur, unary_op); @@ -8250,6 +8252,7 @@ struct llm_build_context { const int mla_attn; const int attn_max_batch; const bool fused_moe_up_gate; + const bool fused_up_gate; const int min_experts; const float thresh_experts; @@ -8305,6 +8308,7 @@ struct llm_build_context { mla_attn (cparams.mla_attn), attn_max_batch (cparams.attn_max_batch), fused_moe_up_gate(cparams.fused_moe_up_gate), + fused_up_gate (cparams.fused_up_gate), min_experts (cparams.min_experts), thresh_experts (cparams.thresh_experts), pooling_type (cparams.pooling_type), @@ -18950,6 +18954,7 @@ struct llama_context_params llama_context_default_params() { /*.mla_attn =*/ 0, /*.attn_max_batch =*/ 0, /*.fused_moe_up_gate =*/ false, + /*.fused_up_gate =*/ true, /*.min_experts =*/ -1, /*.thtesh_experts =*/ 0.0f, /*.abort_callback =*/ nullptr, @@ -19157,6 +19162,7 @@ struct llama_context * llama_new_context_with_model( cparams.mla_attn = params.mla_attn; cparams.attn_max_batch = params.attn_max_batch; cparams.fused_moe_up_gate= params.fused_moe_up_gate; + cparams.fused_up_gate = params.fused_up_gate; cparams.min_experts = params.min_experts; cparams.thresh_experts = params.thresh_experts; @@ -19236,6 +19242,7 @@ struct llama_context * llama_new_context_with_model( LLAMA_LOG_INFO("%s: mla_attn = %d\n", __func__, cparams.mla_attn); LLAMA_LOG_INFO("%s: attn_max_b = %d\n", __func__, cparams.attn_max_batch); LLAMA_LOG_INFO("%s: fused_moe = %d\n", __func__, cparams.fused_moe_up_gate); + LLAMA_LOG_INFO("%s: fused_up_gate = %d\n", __func__, cparams.fused_up_gate); LLAMA_LOG_INFO("%s: ser = %d, %g\n", __func__, cparams.min_experts, cparams.thresh_experts); LLAMA_LOG_INFO("%s: freq_base = %.1f\n", __func__, cparams.rope_freq_base); LLAMA_LOG_INFO("%s: freq_scale = %g\n", __func__, cparams.rope_freq_scale);