| 1818 | // |
| 1819 | |
| 1820 | uint32_t llama_context::output_reserve(int32_t n_outputs) { |
| 1821 | |
| 1822 | const auto & hparams = model.hparams; |
| 1823 | const auto & vocab = model.vocab; |
| 1824 | |
| 1825 | const int64_t n_outputs_max = std::max<int64_t>(n_outputs, n_seq_max()); |
| 1826 | |
| 1827 | const auto n_batch = cparams.n_batch; |
| 1828 | const auto n_vocab = vocab.n_tokens(); |
| 1829 | const auto n_embd_out = hparams.n_embd_out(); |
| 1830 | |
| 1831 | bool has_logits = true; |
| 1832 | bool has_embd = cparams.embeddings; |
| 1833 | |
| 1834 | // TODO: hacky enc-dec support |
| 1835 | if (model.arch == LLM_ARCH_T5) { |
| 1836 | has_logits = true; |
| 1837 | has_embd = true; |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | size_t backend_float_count = 0; |
| 1842 | size_t backend_token_count = 0; |
| 1843 | |
| 1844 | logits_size = has_logits ? n_vocab*n_outputs_max : 0; |
| 1845 | embd_size = has_embd ? n_embd_out*n_outputs_max : 0; |
| 1846 | |
| 1847 | // Allocate backend sampling output buffers if there are backend samplers configured. |
| 1848 | const bool has_sampling = !sampling.samplers.empty(); |
| 1849 | if (has_sampling) { |
| 1850 | sampling.logits_size = n_vocab*n_outputs_max; |
| 1851 | sampling.probs_size = n_vocab*n_outputs_max; |
| 1852 | sampling.sampled_size = n_outputs_max; |
| 1853 | sampling.candidates_size = n_vocab*n_outputs_max; |
| 1854 | |
| 1855 | backend_float_count = sampling.logits_size + sampling.probs_size; |
| 1856 | backend_token_count = sampling.sampled_size + sampling.candidates_size; |
| 1857 | } |
| 1858 | |
| 1859 | if (output_ids.empty()) { |
| 1860 | // init, never resized afterwards |
| 1861 | output_ids.resize(n_batch); |
| 1862 | } |
| 1863 | |
| 1864 | const size_t prev_size = buf_output ? ggml_backend_buffer_get_size(buf_output.get()) : 0; |
| 1865 | const size_t new_size = |
| 1866 | (logits_size + embd_size + backend_float_count) * sizeof(float) + |
| 1867 | ( backend_token_count) * sizeof(llama_token); |
| 1868 | |
| 1869 | // alloc only when more than the current capacity is required |
| 1870 | // TODO: also consider shrinking the buffer |
| 1871 | if (!buf_output || prev_size < new_size) { |
| 1872 | if (buf_output) { |
| 1873 | #ifndef NDEBUG |
| 1874 | // This doesn't happen often, but may be annoying in some cases (like the HellaSwag benchmark) |
| 1875 | LLAMA_LOG_DEBUG("%s: reallocating output buffer from size %.02f MiB to %.02f MiB\n", __func__, prev_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0); |
| 1876 | #endif |
| 1877 | synchronize(); |
nothing calls this directly
no test coverage detected