| 907 | } |
| 908 | |
| 909 | static bool compute_imatrix(llama_context * ctx, const common_params & params, const int32_t n_ctx) { |
| 910 | const llama_model * model = llama_get_model(ctx); |
| 911 | const llama_vocab * vocab = llama_model_get_vocab(model); |
| 912 | |
| 913 | const bool add_bos = llama_vocab_get_add_bos(vocab); |
| 914 | |
| 915 | GGML_ASSERT(!llama_vocab_get_add_eos(vocab)); |
| 916 | |
| 917 | auto tim1 = std::chrono::high_resolution_clock::now(); |
| 918 | LOG_INF("%s: tokenizing the input ..\n", __func__); |
| 919 | |
| 920 | std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, true, params.parse_special); |
| 921 | |
| 922 | auto tim2 = std::chrono::high_resolution_clock::now(); |
| 923 | LOG_INF("%s: tokenization took %g ms\n",__func__,1e-3*std::chrono::duration_cast<std::chrono::microseconds>(tim2-tim1).count()); |
| 924 | |
| 925 | if (params.i_chunk > 0) { |
| 926 | if (size_t((params.i_chunk + 2)*n_ctx) >= tokens.size()) { |
| 927 | LOG_ERR("%s: there will be not enough tokens left after removing %d chunks\n", __func__, params.i_chunk); |
| 928 | return false; |
| 929 | } |
| 930 | LOG_INF("%s: removing initial %d chunks (%d tokens)\n", __func__, params.i_chunk, params.i_chunk*n_ctx); |
| 931 | tokens.erase(tokens.begin(), tokens.begin() + params.i_chunk*n_ctx); |
| 932 | } |
| 933 | |
| 934 | if (int(tokens.size()) < 2*n_ctx) { |
| 935 | LOG_ERR("%s: you need at least %d tokens for a context of %d tokens\n", __func__, 2*n_ctx, n_ctx); |
| 936 | LOG_ERR("%s: the data file you provided tokenizes to only %zu tokens\n", __func__, tokens.size()); |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | std::vector<float> logit_history; |
| 941 | std::vector<float> prob_history; |
| 942 | |
| 943 | if (params.compute_ppl) { |
| 944 | logit_history.resize(tokens.size()); |
| 945 | prob_history.resize(tokens.size()); |
| 946 | } |
| 947 | |
| 948 | const int n_chunk_max = tokens.size() / n_ctx; |
| 949 | |
| 950 | const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max); |
| 951 | const int n_vocab = llama_vocab_n_tokens(vocab); |
| 952 | const int n_batch = params.n_batch; |
| 953 | |
| 954 | int count = 0; |
| 955 | double nll = 0.0; |
| 956 | double nll2 = 0.0; |
| 957 | |
| 958 | const int num_batches = (n_ctx + n_batch - 1) / n_batch; |
| 959 | const int n_seq = std::max(1, n_batch / n_ctx); |
| 960 | |
| 961 | GGML_ASSERT(n_batch < n_ctx || n_batch % n_ctx == 0); |
| 962 | GGML_ASSERT(params.n_ctx == n_seq * n_ctx); |
| 963 | |
| 964 | llama_batch batch = llama_batch_init(std::min(n_batch, n_ctx*n_seq), 0, 1); |
| 965 | |
| 966 | std::vector<float> logits; |
no test coverage detected