MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / build_pooling

Method build_pooling

subprojects/llama.cpp/src/llama-graph.cpp:2333–2415  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2331
2332
2333void llm_graph_context::build_pooling(
2334 ggml_tensor * cls,
2335 ggml_tensor * cls_b,
2336 ggml_tensor * cls_out,
2337 ggml_tensor * cls_out_b) const {
2338 if (!cparams.embeddings) {
2339 return;
2340 }
2341
2342 ggml_tensor * inp = res->t_embd;
2343
2344 //// find result_norm tensor for input
2345 //for (int i = ggml_graph_n_nodes(gf) - 1; i >= 0; --i) {
2346 // inp = ggml_graph_node(gf, i);
2347 // if (strcmp(inp->name, "result_norm") == 0 || strcmp(inp->name, "result_embd") == 0) {
2348 // break;
2349 // }
2350
2351 // inp = nullptr;
2352 //}
2353
2354 GGML_ASSERT(inp != nullptr && "missing result_norm/result_embd tensor");
2355
2356 ggml_tensor * cur;
2357
2358 switch (pooling_type) {
2359 case LLAMA_POOLING_TYPE_NONE:
2360 {
2361 cur = inp;
2362 } break;
2363 case LLAMA_POOLING_TYPE_MEAN:
2364 {
2365 ggml_tensor * inp_mean = build_inp_mean();
2366 cur = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, inp)), inp_mean);
2367 } break;
2368 case LLAMA_POOLING_TYPE_CLS:
2369 case LLAMA_POOLING_TYPE_LAST:
2370 {
2371 ggml_tensor * inp_cls = build_inp_cls();
2372 cur = ggml_get_rows(ctx0, inp, inp_cls);
2373 } break;
2374 case LLAMA_POOLING_TYPE_RANK:
2375 {
2376 ggml_tensor * inp_cls = build_inp_cls();
2377 cur = ggml_get_rows(ctx0, inp, inp_cls);
2378
2379 // classification head
2380 // https://github.com/huggingface/transformers/blob/5af7d41e49bbfc8319f462eb45253dcb3863dfb7/src/transformers/models/roberta/modeling_roberta.py#L1566
2381 if (cls) {
2382 cur = ggml_mul_mat(ctx0, cls, cur);
2383 if (cls_b) {
2384 cur = ggml_add(ctx0, cur, cls_b);
2385 }
2386 cur = ggml_tanh(ctx0, cur);
2387 }
2388
2389 // some models don't have `cls_out`, for example: https://huggingface.co/jinaai/jina-reranker-v1-tiny-en
2390 // https://huggingface.co/jinaai/jina-reranker-v1-tiny-en/blob/cb5347e43979c3084a890e3f99491952603ae1b7/modeling_bert.py#L884-L896

Callers 1

build_graphMethod · 0.80

Calls 8

ggml_mul_matFunction · 0.85
ggml_contFunction · 0.85
ggml_transposeFunction · 0.85
ggml_get_rowsFunction · 0.85
ggml_addFunction · 0.85
ggml_tanhFunction · 0.85
ggml_soft_maxFunction · 0.85

Tested by

no test coverage detected