MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / llm_build_ffn_sparse

Function llm_build_ffn_sparse

llama.cpp:4652–4757  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4650}
4651
4652static struct ggml_tensor * llm_build_ffn_sparse(
4653 struct ggml_context * ctx,
4654 struct ggml_tensor * cur,
4655 struct ggml_tensor * up,
4656 struct ggml_tensor * up_b,
4657 struct ggml_tensor * gate,
4658 struct ggml_tensor * gate_b,
4659 struct ggml_tensor * down_t,
4660 struct ggml_tensor * down_b,
4661 struct ggml_tensor * pre_w1,
4662 struct ggml_tensor * pre_w2,
4663 struct ggml_tensor * pred_inpl,
4664 struct ggml_tensor * gpu_index,
4665 struct ggml_tensor * gpu_bucket,
4666 struct ggml_tensor * gate_gpu,
4667 struct ggml_tensor * down_gpu,
4668 struct ggml_tensor * up_gpu,
4669 llm_ffn_op_type type_op,
4670 llm_ffn_gate_type type_gate,
4671 double gpu_offload_ratio,
4672 const llm_build_cb_short & cb_outer) {
4673 bool full_gpu = gpu_offload_ratio >= 1.0;
4674 ggml_tensor * ffn_input = cur;
4675
4676 llm_build_cb_short cb = [&cb_outer](struct ggml_tensor * tensor, const char * name) {
4677 cb_outer(tensor, name);
4678#if defined(GGML_USE_CUBLAS)
4679 // Determine offloading based on src[0] (weight for both mul and axpy)
4680 bool operates_on_gpu = tensor->src[0]->backend == GGML_BACKEND_GPU;
4681 if (operates_on_gpu) {
4682 ggml_cuda_assign_buffers_no_alloc(tensor);
4683 }
4684#endif
4685 };
4686
4687 // prepare sparse idx
4688 ggml_tensor * idx = ggml_mul_mat(ctx, pre_w1, pred_inpl);
4689 cb(idx, "mlp_pre_hidden");
4690 idx = ggml_relu(ctx, idx);
4691 cb(idx, "mlp_pre_relu");
4692 idx = ggml_mul_mat(ctx, pre_w2, idx);
4693 // If the FFN layer is not fully offloaded, we need to transfer the sparsity index
4694 // back to the CPU to avoid synchronization issues.
4695 (full_gpu ? cb : cb_outer)(idx, "mlp_pre_out");
4696
4697 auto act_fn = [&](ggml_tensor * tensor, const char * name) {
4698 switch (type_op) {
4699 case LLM_FFN_RELU:
4700 {
4701 tensor = ggml_relu(ctx, tensor);
4702 cb(tensor, name);
4703 } break;
4704 default:
4705 GGML_ASSERT(false && "unsupported activation function");
4706 }
4707 return tensor;
4708 };
4709

Callers 3

build_llama_variantsMethod · 0.85
build_optMethod · 0.85
build_falconMethod · 0.85

Calls 7

cbFunction · 0.85
llm_build_sparse_mul_matFunction · 0.85
llm_build_sparse_axpyFunction · 0.85
ggml_mul_matFunction · 0.70
ggml_reluFunction · 0.70
ggml_addFunction · 0.70
ggml_mulFunction · 0.70

Tested by

no test coverage detected