| 2006 | } |
| 2007 | |
| 2008 | int main(int argc, char ** argv) { |
| 2009 | std::setlocale(LC_NUMERIC, "C"); |
| 2010 | |
| 2011 | common_params params; |
| 2012 | |
| 2013 | params.n_ctx = 512; |
| 2014 | params.escape = false; |
| 2015 | |
| 2016 | common_init(); |
| 2017 | |
| 2018 | if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_PERPLEXITY)) { |
| 2019 | return 1; |
| 2020 | } |
| 2021 | |
| 2022 | const int32_t n_ctx = params.n_ctx; |
| 2023 | |
| 2024 | if (n_ctx <= 0) { |
| 2025 | LOG_ERR("%s: perplexity tool requires '--ctx-size' > 0\n", __func__); |
| 2026 | return 1; |
| 2027 | } |
| 2028 | |
| 2029 | if (params.hellaswag || params.winogrande || params.multiple_choice) { |
| 2030 | params.n_parallel = std::max(4, params.n_parallel); |
| 2031 | params.kv_unified = true; |
| 2032 | } else { // Perplexity & KL divergence |
| 2033 | params.n_parallel = std::max(1, params.n_batch / n_ctx); |
| 2034 | } |
| 2035 | params.n_ctx = params.n_parallel * n_ctx; |
| 2036 | params.n_batch = std::min(params.n_batch, params.n_ctx); |
| 2037 | |
| 2038 | if (params.ppl_stride > 0) { |
| 2039 | LOG_INF("Will perform strided perplexity calculation -> adjusting context size from %d to %d\n", |
| 2040 | params.n_ctx, params.n_ctx + params.ppl_stride/2); |
| 2041 | params.n_ctx += params.ppl_stride/2; |
| 2042 | } |
| 2043 | |
| 2044 | llama_backend_init(); |
| 2045 | llama_numa_init(params.numa); |
| 2046 | |
| 2047 | // load the model and apply lora adapter, if any |
| 2048 | auto llama_init = common_init_from_params(params); |
| 2049 | |
| 2050 | auto * model = llama_init->model(); |
| 2051 | auto * ctx = llama_init->context(); |
| 2052 | |
| 2053 | if (model == nullptr) { |
| 2054 | LOG_ERR("%s: unable to load model\n", __func__); |
| 2055 | return 1; |
| 2056 | } |
| 2057 | |
| 2058 | if (ctx == nullptr) { |
| 2059 | LOG_ERR("%s: failed to create context\n", __func__); |
| 2060 | return 1; |
| 2061 | } |
| 2062 | |
| 2063 | const int n_ctx_train = llama_model_n_ctx_train(model); |
| 2064 | |
| 2065 | if (params.n_ctx > n_ctx_train) { |
nothing calls this directly
no test coverage detected