| 933 | } |
| 934 | |
| 935 | bool llama_kv_cache_recurrent::state_read_data(llama_io_read_i & io, uint32_t cell_count) { |
| 936 | uint32_t v_trans; |
| 937 | uint32_t n_layer; |
| 938 | io.read_to(&v_trans, sizeof(v_trans)); |
| 939 | io.read_to(&n_layer, sizeof(n_layer)); |
| 940 | |
| 941 | if (n_layer != hparams.n_layer) { |
| 942 | LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, hparams.n_layer); |
| 943 | return false; |
| 944 | } |
| 945 | if (cell_count > size) { |
| 946 | LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, size); |
| 947 | return false; |
| 948 | } |
| 949 | if (false != (bool) v_trans) { |
| 950 | LLAMA_LOG_ERROR("%s: incompatible V transposition\n", __func__); |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block |
| 955 | for (uint32_t il = 0; il < n_layer; ++il) { |
| 956 | const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s(); |
| 957 | |
| 958 | // Read type of key |
| 959 | int32_t k_type_i_ref; |
| 960 | io.read_to(&k_type_i_ref, sizeof(k_type_i_ref)); |
| 961 | const int32_t k_type_i = (int32_t) k_l[il]->type; |
| 962 | if (k_type_i != k_type_i_ref) { |
| 963 | LLAMA_LOG_ERROR("%s: mismatched key type (%d != %d, layer %d)\n", __func__, k_type_i, k_type_i_ref, il); |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | // Read row size of key |
| 968 | uint64_t k_size_row_ref; |
| 969 | io.read_to(&k_size_row_ref, sizeof(k_size_row_ref)); |
| 970 | const size_t k_size_row = ggml_row_size(k_l[il]->type, n_embd_k_gqa); |
| 971 | if (k_size_row != k_size_row_ref) { |
| 972 | LLAMA_LOG_ERROR("%s: mismatched key row size (%zu != %zu, layer %d)\n", __func__, k_size_row, (size_t) k_size_row_ref, il); |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | if (cell_count) { |
| 977 | // Read and set the keys for the whole cell range |
| 978 | ggml_backend_tensor_set(k_l[il], io.read(cell_count * k_size_row), head * k_size_row, cell_count * k_size_row); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | if (!v_trans) { |
| 983 | for (uint32_t il = 0; il < n_layer; ++il) { |
| 984 | const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s(); |
| 985 | |
| 986 | // Read type of value |
| 987 | int32_t v_type_i_ref; |
| 988 | io.read_to(&v_type_i_ref, sizeof(v_type_i_ref)); |
| 989 | const int32_t v_type_i = (int32_t)v_l[il]->type; |
| 990 | if (v_type_i != v_type_i_ref) { |
| 991 | LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il); |
| 992 | return false; |
nothing calls this directly
no test coverage detected