Skip to content

Commit 1ace5b7

Browse files
ikawrakowIwan Kawrakow
andauthored
Be able to set reduce op data type for split mode "graph" (ggml-org#1087)
Co-authored-by: Iwan Kawrakow <iwan.kawrakow@gmail.com>
1 parent 2421a7e commit 1ace5b7

7 files changed

Lines changed: 23 additions & 6 deletions

File tree

common/common.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,14 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
14361436
params.split_mode_graph_scheduling = true;
14371437
return true;
14381438
}
1439+
if (arg == "-smf16" || arg == "--split-mode-f16") {
1440+
params.split_mode_f16 = true;
1441+
return true;
1442+
}
1443+
if (arg == "-smf32" || arg == "--split-mode-f32") {
1444+
params.split_mode_f16 = false;
1445+
return true;
1446+
}
14391447
if (arg == "--numa") {
14401448
CHECK_ARG
14411449
std::string value(argv[i]);
@@ -2122,6 +2130,8 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
21222130
options.push_back({ "*", "-ser, --smart-expert-reduction", "experts reduction (default: %d,%g)", params.min_experts, params.thresh_experts});
21232131
options.push_back({ "*", "-mqkv, --merge-qkv,", "merge Q,K,V (default: %d)", params.merge_qkv});
21242132
options.push_back({ "*", "-khad, --k-cache-hadamard,", "Use Hadamard transform for K-cache (default: %d)", params.k_cache_hadamard});
2133+
options.push_back({ "*", "-smf16, --split-mode-f16,", "Use f16 for data exchange between GPUs (default: %d)", params.split_mode_f16});
2134+
options.push_back({ "*", "-smf32, --split-mode-f32,", "Use f32 for data exchange between GPUs (default: %d)", !params.split_mode_f16});
21252135
options.push_back({ "*", "-smgs, --split-mode-graph-scheduling,", "Force Split Mode Graph Scheduling (default: %d)", params.split_mode_graph_scheduling});
21262136
options.push_back({ "*", "-vq, --validate-quants", "validate quantized data while loading the model (default: %d)", params.validate_quants});
21272137
options.push_back({ "*", "-p, --prompt PROMPT", "prompt to start generation with\n"
@@ -3156,6 +3166,7 @@ struct llama_context_params llama_context_params_from_gpt_params(const gpt_param
31563166
cparams.graph_reuse = params.graph_reuse;
31573167
cparams.k_cache_hadamard = params.k_cache_hadamard;
31583168
cparams.split_mode_graph_scheduling = params.split_mode_graph_scheduling;
3169+
cparams.split_mode_f16 = params.split_mode_f16;
31593170
cparams.min_experts = params.min_experts;
31603171
cparams.thresh_experts = params.thresh_experts;
31613172
cparams.only_active_experts = params.only_active_exps;
@@ -4138,6 +4149,7 @@ void yaml_dump_non_result_info(FILE * stream, const gpt_params & params, const l
41384149
fprintf(stream, "graph_reuse: %s # default: false\n", params.graph_reuse ? "true" : "false");
41394150
fprintf(stream, "k_cache_hadamard: %s # default: false\n", params.k_cache_hadamard ? "true" : "false");
41404151
fprintf(stream, "split_mode_graph_scheduling: %s # default: false\n", params.split_mode_graph_scheduling ? "true" : "false");
4152+
fprintf(stream, "split_mode_f16: %s # default: true\n", params.split_mode_f16 ? "true" : "false");
41414153
fprintf(stream, "ser: %d,%g # defaulr: -1,0\n", params.min_experts, params.thresh_experts);
41424154
fprintf(stream, "temp: %f # default: 0.8\n", sparams.temp);
41434155

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ struct gpt_params {
289289
bool merge_qkv = false; // if true, merge separate Q, K, V tensors into a single, contiguous tensor
290290
bool k_cache_hadamard = false; // if true, use Hadamard transform for the K-cache (only makes sense with quantized cache)
291291
bool split_mode_graph_scheduling = false; // if true, force split mode graph scheduling
292+
bool split_mode_f16 = true; // if true, intermediate results will be cast to f16 before copying to other GPUs to perform reduce ops
292293

293294
std::string cache_type_k = "f16"; // KV cache data type for the K
294295
std::string cache_type_v = "f16"; // KV cache data type for the V

include/llama.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ extern "C" {
444444
bool only_active_experts;
445445
bool k_cache_hadamard; // if true, apply Hadamard transfrom to K-cache
446446
bool split_mode_graph_scheduling; // if true, force split mode graph scheduling
447+
bool split_mode_f16; // if true, cast intermediate results to f16 before copying to other GPUs
447448

448449
// Abort callback
449450
// if it returns true, execution of llama_decode() will be aborted

src/llama-build-context.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ ggml_tensor * llm_build_context::llm_build_ffn(
697697
// GLM4 and GLM4_MOE seem to have numerical issues with half-precision accumulators
698698
ggml_mul_mat_set_prec(cur, GGML_PREC_F32);
699699
}
700-
if (cur->ne[1] >= 32) {
700+
if (cur->ne[1] > 32 && lctx.cparams.split_mode_f16) {
701701
cur = ggml_cast(ctx, cur, GGML_TYPE_F16);
702702
}
703703
if (graph) {
@@ -1185,7 +1185,7 @@ llm_expert_gating_func_type gating_op,
11851185
split_down_shexp->splits[id], split_down_b_shexp ? split_down_b_shexp->splits[id] : nullptr, nullptr,
11861186
nullptr, type_op_shexp, LLM_FFN_PAR, cb, il);
11871187
cb(shared_out, "ffn_shexp_out", il_cb);
1188-
if (shared_out->ne[1] > 32) {
1188+
if (shared_out->ne[1] > 32 && lctx.cparams.split_mode_f16) {
11891189
shared_out = ggml_cast(ctx, shared_out, GGML_TYPE_F16);
11901190
}
11911191
results.push_back(shared_out);
@@ -1202,7 +1202,7 @@ llm_expert_gating_func_type gating_op,
12021202
cb(cur, "ffn_shared_combined", il);
12031203
}
12041204
}
1205-
if (routed_out->ne[1] > 32) {
1205+
if (routed_out->ne[1] > 32 && lctx.cparams.split_mode_f16) {
12061206
auto routed_out_f16 = ggml_cast(ctx, routed_out, GGML_TYPE_F16);
12071207
cur = ggml_add(ctx, routed_out_f16, cur);
12081208
} else {
@@ -1279,7 +1279,7 @@ llm_expert_gating_func_type gating_op,
12791279
} else {
12801280
cur = routed_out;
12811281
}
1282-
if (cur->ne[1] >= 32) {
1282+
if (cur->ne[1] > 32 && lctx.cparams.split_mode_f16) {
12831283
cur = ggml_cast(ctx, cur, GGML_TYPE_F16);
12841284
cb(cur, "ffn_out_f16", il_cb);
12851285
}
@@ -9513,7 +9513,7 @@ ggml_tensor * llm_build_context::build_std_attention(ggml_cgraph * gf, ggml_tens
95139513
cur = ggml_add(ctx0, cur, bo->splits[id]);
95149514
cb(cur, "kqv_wo_biased", il_cb);
95159515
}
9516-
if (cur->ne[1] >= 32) {
9516+
if (cur->ne[1] > 32 && lctx.cparams.split_mode_f16) {
95179517
cur = ggml_cast(ctx0, cur, GGML_TYPE_F16);
95189518
}
95199519
ggml_build_forward_expand(gf, cur);

src/llama-cparams.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct llama_cparams {
4141
bool graph_reuse;
4242
bool k_cache_hadamard;
4343
bool split_mode_graph_scheduling;
44+
bool split_mode_f16;
4445
int min_experts;
4546
float thresh_experts;
4647

src/llama-load-tensors.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,6 @@ bool create_tensors_helper::create_cohere2_tensors(const LLM_TN & tn) {
22652265
for (int i = 0; i < n_layer; ++i) {
22662266
auto & layer = model.layers[i];
22672267
ggml_context * ctx_split = ctx_for_layer_split(i);
2268-
ggml_context * ctx_layer = ctx_for_layer(i);
22692268

22702269
layer.attn_norm = create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
22712270

src/llama.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,6 +4055,7 @@ struct llama_context_params llama_context_default_params() {
40554055
/*.only_active_experts =*/ false,
40564056
/*.k_cache_hadamard =*/ false,
40574057
/*.split_mode_graph_scheduling =*/ false,
4058+
/*.split_mode_f16 =*/ true,
40584059
/*.abort_callback =*/ nullptr,
40594060
/*.abort_callback_data =*/ nullptr,
40604061
/*.offload_policy =*/ nullptr,
@@ -4344,6 +4345,7 @@ struct llama_context * llama_new_context_with_model(
43444345
cparams.graph_reuse = params.graph_reuse;
43454346
cparams.k_cache_hadamard = params.k_cache_hadamard;
43464347
cparams.split_mode_graph_scheduling = params.split_mode_graph_scheduling;
4348+
cparams.split_mode_f16 = params.split_mode_f16;
43474349
cparams.min_experts = params.min_experts;
43484350
cparams.thresh_experts = params.thresh_experts;
43494351
cparams.cuda_params = params.cuda_params;
@@ -4433,6 +4435,7 @@ struct llama_context * llama_new_context_with_model(
44334435
LLAMA_LOG_INFO("%s: graph_reuse = %d\n", __func__, cparams.graph_reuse);
44344436
LLAMA_LOG_INFO("%s: k_cache_hadam = %d\n", __func__, cparams.k_cache_hadamard);
44354437
LLAMA_LOG_INFO("%s: split_mode_graph_scheduling = %d\n", __func__, cparams.split_mode_graph_scheduling);
4438+
LLAMA_LOG_INFO("%s: split_mode_f16= %d\n", __func__, cparams.split_mode_f16);
44364439
LLAMA_LOG_INFO("%s: ser = %d, %g\n", __func__, cparams.min_experts, cparams.thresh_experts);
44374440
LLAMA_LOG_INFO("%s: freq_base = %.1f\n", __func__, cparams.rope_freq_base);
44384441
LLAMA_LOG_INFO("%s: freq_scale = %g\n", __func__, cparams.rope_freq_scale);

0 commit comments

Comments
 (0)