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

Function ggml_compute_forward_alibi_f16

ggml.c:10952–11011  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10950}
10951
10952static void ggml_compute_forward_alibi_f16(
10953 const struct ggml_compute_params * params,
10954 const struct ggml_tensor * src0,
10955 struct ggml_tensor * dst) {
10956 assert(params->ith == 0);
10957
10958 if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
10959 return;
10960 }
10961
10962 //const int n_past = ((int32_t *) dst->op_params)[0];
10963 const int n_head = ((int32_t *) dst->op_params)[1];
10964 float max_bias;
10965 memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
10966
10967 const int ne0 = src0->ne[0]; // all_seq_len = n_past + ne1
10968 const int ne1 = src0->ne[1]; // seq_len_without_past
10969 const int ne2 = src0->ne[2]; // n_head -> this is k
10970 //const int ne3 = src0->ne[3]; // 1 -> bsz
10971
10972 const int n = ggml_nrows(src0);
10973 const int ne2_ne3 = n/ne1; // ne2*ne3
10974
10975 const int nb0 = src0->nb[0];
10976 const int nb1 = src0->nb[1];
10977 const int nb2 = src0->nb[2];
10978 //const int nb3 = src0->nb[3];
10979
10980 GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
10981 //GGML_ASSERT(ne1 + n_past == ne0); (void) n_past;
10982 GGML_ASSERT(n_head == ne2);
10983
10984 // add alibi to src0 (KQ_scaled)
10985 const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
10986
10987 const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
10988 const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
10989
10990 for (int i = 0; i < ne0; i++) {
10991 for (int j = 0; j < ne1; j++) {
10992 for (int k = 0; k < ne2_ne3; k++) {
10993 ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i*nb0 + j*nb1 + k*nb2);
10994 float * pdst = (float *)((char *) dst->data + i*nb0 + j*nb1 + k*nb2);
10995
10996 // TODO: k*nb2 or k*nb3
10997
10998 float m_k;
10999
11000 if (k < n_heads_log2_floor) {
11001 m_k = powf(m0, k + 1);
11002 } else {
11003 m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
11004 }
11005
11006 // we return F32
11007 pdst[0] = i * m_k + GGML_FP16_TO_FP32(src[0]);
11008 }
11009 }

Callers 1

Calls 1

ggml_nrowsFunction · 0.70

Tested by

no test coverage detected