Skip to content

Commit f099348

Browse files
committed
Fused delta-net (ikawrakow#1315)
* Revive fused delta-net * Add command line argument for fused delta net * Simplify/improve CUDA delta-net * Add -fdn to llama-bench * More CUDA fused delta net optimizations * CPU optimizations * Much faster fused delta-net on the CPU It seems it is faster than the chunked implementation! * Change meaning of fdn from bool flag to threshold value * Use eps = 1e-6 * Give some nodes a name
1 parent 81fb35a commit f099348

16 files changed

Lines changed: 3264 additions & 13 deletions

File tree

common/common.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,11 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
15361536
params.scheduler_async = true;
15371537
return true;
15381538
}
1539+
if (arg == "-fdn" || arg == "--fused-delta-net") {
1540+
CHECK_ARG
1541+
params.fused_delta_net = std::stoi(argv[i]);
1542+
return true;
1543+
}
15391544
if (arg == "-smf16" || arg == "--split-mode-f16") {
15401545
params.reduce_type = "f16";
15411546
//params.split_mode_f16 = true;
@@ -2263,6 +2268,7 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
22632268
options.push_back({ "*", "-grt, --graph-reduce-type", "Type for data exchange between GPUs (default: %s)", "f32"});
22642269
options.push_back({ "*", "-smgs, --split-mode-graph-scheduling,", "Force Split Mode Graph Scheduling (default: %d)", params.split_mode_graph_scheduling});
22652270
options.push_back({ "*", "-sas, --scheduler_async,", "Async evaluation of compute graphs: %d)", params.scheduler_async});
2271+
options.push_back({ "*", "-fdn, --fused-delta-net N", "Use fused delta-net when batch size is <= N with recurrent models: %d)", params.fused_delta_net});
22662272
options.push_back({ "*", "-vq, --validate-quants", "validate quantized data while loading the model (default: %d)", params.validate_quants});
22672273
options.push_back({ "*", "-p, --prompt PROMPT", "prompt to start generation with\n"
22682274
"in conversation mode, this will be used as system prompt\n"
@@ -3343,6 +3349,7 @@ struct llama_context_params common_context_params_to_llama(const gpt_params & pa
33433349
cparams.split_mode_graph_scheduling = params.split_mode_graph_scheduling;
33443350
//cparams.split_mode_f16 = params.split_mode_f16;
33453351
cparams.scheduler_async = params.scheduler_async;
3352+
cparams.fused_delta_net = params.fused_delta_net;
33463353
cparams.min_experts = params.min_experts;
33473354
cparams.thresh_experts = params.thresh_experts;
33483355
cparams.only_active_experts = params.only_active_exps;
@@ -4354,6 +4361,7 @@ void yaml_dump_non_result_info(FILE * stream, const gpt_params & params, const l
43544361
//fprintf(stream, "split_mode_f16: %s # default: true\n", params.split_mode_f16 ? "true" : "false");
43554362
fprintf(stream, "reduce_type: %s # default f16\n", params.reduce_type.c_str());
43564363
fprintf(stream, "scheduler_async: %s # default: false\n", params.scheduler_async ? "true" : "false");
4364+
fprintf(stream, "fused_delta_net: %d # default: 0\n", params.fused_delta_net );
43574365
fprintf(stream, "ser: %d,%g # defaulr: -1,0\n", params.min_experts, params.thresh_experts);
43584366
fprintf(stream, "temp: %f # default: 0.8\n", sparams.temp);
43594367

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ struct gpt_params {
358358
bool split_mode_graph_scheduling = false; // if true, force split mode graph scheduling
359359
//bool split_mode_f16 = true; // if true, intermediate results will be cast to f16 before copying to other GPUs to perform reduce ops
360360
bool scheduler_async = false; // if true, in split mode graph the scheduler will use multiple threads to evaluate the graph
361+
int fused_delta_net = 0; // use fused delta-net if number of tokens in the batch is less than this value
361362
bool has_mtp = false; // enable MTP if supported by the model
362363

363364
std::string cache_type_k = "f16"; // KV cache data type for the K

examples/llama-bench/llama-bench.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ struct cmd_params {
272272
bool muge = false;
273273
bool rcache = false;
274274
bool sas = false;
275+
int fdn = 0; // fdn = fused delta net
275276
bool print_overrides = false;
276277
output_formats output_format;
277278
output_formats output_format_stderr;
@@ -318,6 +319,7 @@ static const cmd_params cmd_params_defaults = {
318319
/* muge */ false,
319320
/* rcache */ false,
320321
/* sas */ false,
322+
/* fdn */ 0,
321323
/* print_overrides */ false,
322324
/* output_format */ MARKDOWN,
323325
/* output_format_stderr */ NONE,
@@ -372,6 +374,7 @@ static void print_usage(int /* argc */, char ** argv) {
372374
printf(" -no-fug, --no-fused-up-gate <0|1> (default: %s)\n", cmd_params_defaults.no_fug? "1" : "0");
373375
printf(" -no-ooae, --no-offload-only-active-experts <0|1> (default: %s)\n", cmd_params_defaults.no_ooae? "1" : "0");
374376
printf(" -sas, --scheduler-async <0|1> (default: %s)\n", cmd_params_defaults.sas ? "1" : "0");
377+
printf(" -fdn, --fused-delta-net <n> (default: %d)\n", cmd_params_defaults.fdn);
375378
printf(" --print-overrides <0|1> (default: %s)\n", cmd_params_defaults.print_overrides ? "1" : "0");
376379
printf("\n");
377380
printf("Multiple values can be given for each parameter by separating them with ',' or by specifying the parameter multiple times.\n");
@@ -820,6 +823,12 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
820823
break;
821824
}
822825
params.sas = std::stoi(argv[i]);
826+
} else if (arg == "-fdn" || arg == "--fused-delta-net") {
827+
if (++i >= argc) {
828+
invalid_param = true;
829+
break;
830+
}
831+
params.fdn = std::stoi(argv[i]);
823832
} else if (arg == "-rcache" || arg == "--rope-cache") {
824833
if (++i >= argc) {
825834
invalid_param = true;
@@ -968,6 +977,7 @@ struct cmd_params_instance {
968977
bool muge = false;
969978
bool rcache = false;
970979
bool sas = false;
980+
int fdn = 0;
971981
const llama_model_tensor_buft_override* buft_overrides;
972982

973983
llama_model_params to_llama_mparams() const {
@@ -1004,6 +1014,8 @@ struct cmd_params_instance {
10041014
mqkv == other.mqkv &&
10051015
muge == other.muge &&
10061016
use_thp == other.use_thp &&
1017+
sas == other.sas &&
1018+
fdn == other.fdn &&
10071019
tensor_split == other.tensor_split;
10081020
}
10091021

@@ -1030,6 +1042,7 @@ struct cmd_params_instance {
10301042
cparams.embeddings = embeddings;
10311043
cparams.cuda_params = (void *)cuda_params.data();
10321044
cparams.scheduler_async = sas;
1045+
cparams.fused_delta_net = fdn;
10331046

10341047
return cparams;
10351048
}
@@ -1098,6 +1111,7 @@ static std::vector<cmd_params_instance> get_cmd_params_instances(const cmd_param
10981111
/* .muge = */ params.muge,
10991112
/* .rcache = */ params.rcache,
11001113
/* .sas = */ params.sas,
1114+
/* .fdn = */ params.fdn,
11011115
/* .buft_overrides=*/ params.buft_overrides.data(),
11021116
};
11031117
instances.push_back(instance);
@@ -1142,6 +1156,7 @@ static std::vector<cmd_params_instance> get_cmd_params_instances(const cmd_param
11421156
/* .muge = */ params.muge,
11431157
/* .rcache = */ params.rcache,
11441158
/* .sas = */ params.sas,
1159+
/* .fdn = */ params.fdn,
11451160
/* .buft_overrides=*/ params.buft_overrides.data(),
11461161
};
11471162
instances.push_back(instance);
@@ -1186,6 +1201,7 @@ static std::vector<cmd_params_instance> get_cmd_params_instances(const cmd_param
11861201
/* .muge = */ params.muge,
11871202
/* .rcache = */ params.rcache,
11881203
/* .sas = */ params.sas,
1204+
/* .fdn = */ params.fdn,
11891205
/* .buft_overrides=*/ params.buft_overrides.data(),
11901206
};
11911207
instances.push_back(instance);
@@ -1230,6 +1246,7 @@ static std::vector<cmd_params_instance> get_cmd_params_instances(const cmd_param
12301246
/* .muge = */ params.muge,
12311247
/* .rcache = */ params.rcache,
12321248
/* .sas = */ params.sas,
1249+
/* .fdn = */ params.fdn,
12331250
/* .buft_overrides=*/ params.buft_overrides.data(),
12341251
};
12351252
instances.push_back(instance);
@@ -1285,6 +1302,7 @@ struct test {
12851302
bool muge = false;
12861303
bool rcache = false;
12871304
bool sas = false;
1305+
int fdn = 0;
12881306
std::string override_tensor;
12891307
int n_prompt;
12901308
int n_gen;
@@ -1327,6 +1345,7 @@ struct test {
13271345
ger = inst.ger;
13281346
rcache = inst.rcache;
13291347
sas = inst.sas;
1348+
fdn = inst.fdn;
13301349
no_fug = inst.no_fug;
13311350
use_thp = inst.use_thp;
13321351
no_ooae = inst.no_ooae;
@@ -1431,7 +1450,7 @@ struct test {
14311450
field == "model_size" || field == "model_n_params" ||
14321451
field == "n_gpu_layers" || field == "main_gpu" ||
14331452
field == "n_prompt" || field == "n_gen" || field == "mla_attn" || field == "attn_max_batch" ||
1434-
field == "avg_ns" || field == "stddev_ns") {
1453+
field == "avg_ns" || field == "stddev_ns" || field == "fdn") {
14351454
return INT;
14361455
}
14371456
if (field == "cuda" || field == "vulkan" || field == "kompute" || field == "metal" ||
@@ -1483,7 +1502,7 @@ struct test {
14831502
std::to_string(mla_attn), std::to_string(attn_max_batch), ser_to_string(ser), std::to_string(reuse),
14841503
tensor_split_str, std::to_string(use_mmap), std::to_string(use_direct_io), std::to_string(embeddings),
14851504
std::to_string(repack), std::to_string(mqkv), std::to_string(muge), std::to_string(fmoe), std::to_string(ger),
1486-
std::to_string(no_fug), std::to_string(use_thp), std::to_string(no_ooae), std::to_string(rcache), std::to_string(sas),
1505+
std::to_string(no_fug), std::to_string(use_thp), std::to_string(no_ooae), std::to_string(rcache), std::to_string(sas), std::to_string(fdn),
14871506
cuda_params, override_tensor,
14881507
std::to_string(n_prompt), std::to_string(n_gen), test_time,
14891508
std::to_string(avg_ns()), std::to_string(stdev_ns()),
@@ -1505,7 +1524,7 @@ struct test {
15051524
"main_gpu", "no_kv_offload", "flash_attn", "mla_attn", "attn_max_batch", "ser", "reuse",
15061525
"tensor_split", "use_mmap", "use_direct_io", "embeddings",
15071526
"repack", "mqkv", "muge", "fused_moe", "grouped_er",
1508-
"no_fused_up_gate", "use_thp", "no_ooae", "rcache", "sas", "cuda_params", "override_tensor",
1527+
"no_fused_up_gate", "use_thp", "no_ooae", "rcache", "sas", "fdn", "cuda_params", "override_tensor",
15091528
"n_prompt", "n_gen", "test_time",
15101529
"avg_ns", "stddev_ns",
15111530
"avg_ts", "stddev_ts", "test",
@@ -1698,6 +1717,9 @@ struct markdown_printer : public printer {
16981717
if (field == "sas") {
16991718
return 3;
17001719
}
1720+
if (field == "fdn") {
1721+
return 4;
1722+
}
17011723
if (field == "use_thp") {
17021724
return 3;
17031725
}
@@ -1774,6 +1796,9 @@ struct markdown_printer : public printer {
17741796
if (field == "sas") {
17751797
return "sas";
17761798
}
1799+
if (field == "fdn") {
1800+
return "fdn";
1801+
}
17771802
if (field == "use_thp") {
17781803
return "thp";
17791804
}
@@ -1887,6 +1912,9 @@ struct markdown_printer : public printer {
18871912
if (params.sas != cmd_params_defaults.sas) {
18881913
fields.emplace_back("sas");
18891914
}
1915+
if (params.fdn != cmd_params_defaults.fdn) {
1916+
fields.emplace_back("fdn");
1917+
}
18901918
if (params.muge != cmd_params_defaults.muge) {
18911919
fields.emplace_back("muge");
18921920
}

0 commit comments

Comments
 (0)