| 10891 | // ggml_compute_forward_alibi |
| 10892 | |
| 10893 | static void ggml_compute_forward_alibi_f32( |
| 10894 | const struct ggml_compute_params * params, |
| 10895 | const struct ggml_tensor * src0, |
| 10896 | struct ggml_tensor * dst) { |
| 10897 | assert(params->ith == 0); |
| 10898 | |
| 10899 | if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) { |
| 10900 | return; |
| 10901 | } |
| 10902 | |
| 10903 | //const int n_past = ((int32_t *) dst->op_params)[0]; |
| 10904 | const int n_head = ((int32_t *) dst->op_params)[1]; |
| 10905 | float max_bias; |
| 10906 | memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float)); |
| 10907 | |
| 10908 | const int64_t ne0 = src0->ne[0]; // all_seq_len = n_past + ne1 |
| 10909 | const int64_t ne1 = src0->ne[1]; // seq_len_without_past |
| 10910 | const int64_t ne2 = src0->ne[2]; // n_head -> this is k |
| 10911 | //const int64_t ne3 = src0->ne[3]; // 1 -> bsz |
| 10912 | |
| 10913 | const int64_t n = ggml_nrows(src0); |
| 10914 | const int64_t ne2_ne3 = n/ne1; // ne2*ne3 |
| 10915 | |
| 10916 | const size_t nb0 = src0->nb[0]; |
| 10917 | const size_t nb1 = src0->nb[1]; |
| 10918 | const size_t nb2 = src0->nb[2]; |
| 10919 | //const int nb3 = src0->nb[3]; |
| 10920 | |
| 10921 | GGML_ASSERT(nb0 == sizeof(float)); |
| 10922 | GGML_ASSERT(n_head == ne2); |
| 10923 | |
| 10924 | // add alibi to src0 (KQ_scaled) |
| 10925 | const int n_heads_log2_floor = 1 << (int) floor(log2(n_head)); |
| 10926 | |
| 10927 | const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor); |
| 10928 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor); |
| 10929 | |
| 10930 | for (int64_t i = 0; i < ne0; i++) { |
| 10931 | for (int64_t j = 0; j < ne1; j++) { |
| 10932 | for (int64_t k = 0; k < ne2_ne3; k++) { |
| 10933 | float * const src = (float *)((char *) src0->data + i*nb0 + j*nb1 + k*nb2); |
| 10934 | float * pdst = (float *)((char *) dst->data + i*nb0 + j*nb1 + k*nb2); |
| 10935 | |
| 10936 | // TODO: k*nb2 or k*nb3 |
| 10937 | |
| 10938 | float m_k; |
| 10939 | |
| 10940 | if (k < n_heads_log2_floor) { |
| 10941 | m_k = powf(m0, k + 1); |
| 10942 | } else { |
| 10943 | m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1); |
| 10944 | } |
| 10945 | |
| 10946 | pdst[0] = i * m_k + src[0]; |
| 10947 | } |
| 10948 | } |
| 10949 | } |
| 10950 | } |
no test coverage detected