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

Method build_pooling

smallthinker/src/llama-graph.cpp:1689–1767  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1687}
1688
1689void llm_graph_context::build_pooling(
1690 ggml_cgraph * gf,
1691 ggml_tensor * cls,
1692 ggml_tensor * cls_b,
1693 ggml_tensor * cls_out,
1694 ggml_tensor * cls_out_b) const {
1695 if (!cparams.embeddings) {
1696 return;
1697 }
1698
1699 ggml_tensor * inp = res->t_embd;
1700
1701 //// find result_norm tensor for input
1702 //for (int i = ggml_graph_n_nodes(gf) - 1; i >= 0; --i) {
1703 // inp = ggml_graph_node(gf, i);
1704 // if (strcmp(inp->name, "result_norm") == 0 || strcmp(inp->name, "result_embd") == 0) {
1705 // break;
1706 // }
1707
1708 // inp = nullptr;
1709 //}
1710
1711 GGML_ASSERT(inp != nullptr && "missing result_norm/result_embd tensor");
1712
1713 ggml_tensor * cur;
1714
1715 switch (pooling_type) {
1716 case LLAMA_POOLING_TYPE_NONE:
1717 {
1718 cur = inp;
1719 } break;
1720 case LLAMA_POOLING_TYPE_MEAN:
1721 {
1722 ggml_tensor * inp_mean = build_inp_mean();
1723 cur = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, inp)), inp_mean);
1724 } break;
1725 case LLAMA_POOLING_TYPE_CLS:
1726 case LLAMA_POOLING_TYPE_LAST:
1727 {
1728 ggml_tensor * inp_cls = build_inp_cls();
1729 cur = ggml_get_rows(ctx0, inp, inp_cls);
1730 } break;
1731 case LLAMA_POOLING_TYPE_RANK:
1732 {
1733 ggml_tensor * inp_cls = build_inp_cls();
1734 inp = ggml_get_rows(ctx0, inp, inp_cls);
1735
1736 if (cls != nullptr && cls_b != nullptr) {
1737 // classification head
1738 // https://github.com/huggingface/transformers/blob/5af7d41e49bbfc8319f462eb45253dcb3863dfb7/src/transformers/models/roberta/modeling_roberta.py#L1566
1739 cur = ggml_add(ctx0, ggml_mul_mat(ctx0, cls, inp), cls_b);
1740 cur = ggml_tanh(ctx0, cur);
1741
1742 // some models don't have `cls_out`, for example: https://huggingface.co/jinaai/jina-reranker-v1-tiny-en
1743 // https://huggingface.co/jinaai/jina-reranker-v1-tiny-en/blob/cb5347e43979c3084a890e3f99491952603ae1b7/modeling_bert.py#L884-L896
1744 if (cls_out) {
1745 GGML_ASSERT(cls_out_b != nullptr);
1746 cur = ggml_add(ctx0, ggml_mul_mat(ctx0, cls_out, cur), cls_out_b);

Callers 1

build_graphMethod · 0.80

Calls 8

cbFunction · 0.85
ggml_mul_matFunction · 0.50
ggml_contFunction · 0.50
ggml_transposeFunction · 0.50
ggml_get_rowsFunction · 0.50
ggml_addFunction · 0.50
ggml_tanhFunction · 0.50

Tested by

no test coverage detected