| 1101 | } |
| 1102 | |
| 1103 | ggml_tensor * llama_kv_cache::cpy_v(ggml_context * ctx, ggml_tensor * v_cur, ggml_tensor * v_idxs, int32_t il, const slot_info & sinfo) const { |
| 1104 | GGML_UNUSED(sinfo); |
| 1105 | |
| 1106 | const int32_t ikv = map_layer_ids.at(il); |
| 1107 | |
| 1108 | auto * v = layers[ikv].v; |
| 1109 | |
| 1110 | const int64_t n_embd_head = v_cur->ne[0]; |
| 1111 | const int64_t n_head = v_cur->ne[1]; |
| 1112 | const int64_t n_tokens = v_cur->ne[2]; |
| 1113 | |
| 1114 | const int64_t n_embd_gqa = n_embd_head*n_head; |
| 1115 | |
| 1116 | // we can merge dims 0 and 1 |
| 1117 | GGML_ASSERT(ggml_row_size(v_cur->type, n_embd_head) == v_cur->nb[1]); |
| 1118 | |
| 1119 | const int64_t n_stream = v->ne[2]; |
| 1120 | |
| 1121 | // take this branch when FA is enabled (the V cache is not transposed) |
| 1122 | if (!v_trans) { |
| 1123 | v_cur = ggml_view_2d(ctx, v_cur, n_embd_gqa, n_tokens, v_cur->nb[2], 0); |
| 1124 | |
| 1125 | if (n_stream > 1) { |
| 1126 | const int64_t kv_size = get_size(); |
| 1127 | |
| 1128 | assert(n_embd_gqa == v->ne[0]); |
| 1129 | assert(kv_size == v->ne[1]); |
| 1130 | |
| 1131 | // merge the buffer across all streams because the idxs are global |
| 1132 | v = ggml_reshape_2d(ctx, v, n_embd_gqa, kv_size*n_stream); |
| 1133 | } |
| 1134 | |
| 1135 | return ggml_set_rows(ctx, v, v_cur, v_idxs); |
| 1136 | } |
| 1137 | |
| 1138 | if (ggml_row_size(v_cur->type, n_embd_gqa) == v_cur->nb[2]) { |
| 1139 | // we can merge dims 0, 1 and 2 |
| 1140 | v_cur = ggml_reshape_2d(ctx, v_cur, n_embd_gqa, n_tokens); |
| 1141 | } else { |
| 1142 | // otherwise -> make a copy to get contiguous data |
| 1143 | v_cur = ggml_cont_2d (ctx, v_cur, n_embd_gqa, n_tokens); |
| 1144 | } |
| 1145 | |
| 1146 | // [TAG_V_CACHE_VARIABLE] |
| 1147 | if (n_embd_gqa < v->ne[0]) { |
| 1148 | v_cur = ggml_pad(ctx, v_cur, v->ne[0] - n_embd_gqa, 0, 0, 0); |
| 1149 | } |
| 1150 | |
| 1151 | // in this branch the v_idxs are constructed in such a way that each row is a single head element |
| 1152 | ggml_tensor * v_view = ggml_reshape_2d(ctx, v, 1, ggml_nelements(v)); |
| 1153 | |
| 1154 | v_cur = ggml_reshape_2d(ctx, v_cur, 1, ggml_nelements(v_cur)); |
| 1155 | |
| 1156 | return ggml_set_rows(ctx, v_view, v_cur, v_idxs); |
| 1157 | } |
| 1158 | |
| 1159 | ggml_tensor * llama_kv_cache::build_input_k_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const { |
| 1160 | const uint32_t n_tokens = ubatch.n_tokens; |
no test coverage detected