sampling.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "llama.h"
  28. #include "common.h"
  29. #include <string>
  30. #include <vector>
  31. // common_sampler extends llama_sampler with additional functionality:
  32. //
  33. // - grammar support
  34. // - custom sampler logic based on the parameters
  35. // - history of the last accepted tokens
  36. // - performance metrics
  37. //
  38. // This goal is to have a common implementation of the sampling logic shared across the examples.
  39. // For example, depending on the temperature, the sampling chain can be very simple (greedy) or more
  40. // complex (top-k, top-p, etc).
  41. //
  42. // Another example is related to the grammar. In general, the grammar constraints applied on the full
  43. // vocabulary can be very taxing. To improve performance, the grammar can be applied only to the sampled
  44. // token in order to verify if it fits the grammar. And only if the token doesn't fit the grammar, the
  45. // grammar constraints are applied to the full vocabulary and the token is resampled.
  46. //
  47. // The common_sampler also maintains a container with the last accepted tokens. In the future, this can
  48. // be moved into the core llama library.
  49. //
  50. // For convenience, the common_sampler also maintains a container with the current candidate tokens.
  51. // This can be used to access the probabilities of the rest of the non-sampled tokens.
  52. //
  53. // TODO: measure grammar performance
  54. //
  55. struct common_sampler;
  56. // llama_sampler API overloads
  57. struct common_sampler * common_sampler_init(const struct llama_model * model, const struct common_params_sampling & params);
  58. void common_sampler_free(struct common_sampler * gsmpl);
  59. // if accept_grammar is true, the token is accepted both by the sampling chain and the grammar
  60. void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar);
  61. void common_sampler_reset (struct common_sampler * gsmpl);
  62. struct common_sampler * common_sampler_clone (struct common_sampler * gsmpl);
  63. // arguments can be nullptr to skip printing
  64. void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl);
  65. // extended sampling implementation:
  66. //
  67. // - set logits
  68. // - apply the configured sampler chain
  69. // - check if the token fits the grammar (if any)
  70. // - if not: resample by first applying the grammar constraints and then sampling again (slower path)
  71. //
  72. // if grammar_first is true, the grammar is applied before the samplers (slower)
  73. // useful in cases where all the resulting candidates (not just the sampled one) must fit the grammar
  74. //
  75. llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first = false);
  76. // generalized version of common_sampler_sample
  77. //
  78. // will cross-reference the sampled tokens with a batch of draft tokens and accept those that match
  79. // if the sampler disagrees at some point, we stop and return the accepted tokens up to now
  80. //
  81. // common_sampler_sample_n(gsmpl, ctx, { idx }, {});
  82. //
  83. // is equivalent to
  84. //
  85. // common_sampler_sample(gsmpl, ctx, idx);
  86. // common_sampler_accept(gsmpl, token, true);
  87. //
  88. // requires: idxs.size() == draft.size() + 1
  89. //
  90. // returns at least 1 token, up to idxs.size()
  91. //
  92. std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const std::vector<int> & idxs, const llama_tokens & draft, bool grammar_first = false);
  93. // assume idxs == [ 0, 1, 2, ..., draft.size() ]
  94. std::vector<llama_token> common_sampler_sample_and_accept_n(struct common_sampler * gsmpl, struct llama_context * ctx, const llama_tokens & draft, bool grammar_first = false);
  95. uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl);
  96. // helpers
  97. // access the internal list of current candidate tokens
  98. llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl);
  99. // get the last accepted token
  100. llama_token common_sampler_last(const struct common_sampler * gsmpl);
  101. // print the sampler chain into a string
  102. std::string common_sampler_print(const struct common_sampler * gsmpl);
  103. // get a string representation of the last accepted tokens
  104. std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx, int n);
  105. char common_sampler_type_to_chr(enum common_sampler_type cnstr);
  106. std::string common_sampler_type_to_str(enum common_sampler_type cnstr);
  107. std::vector<enum common_sampler_type> common_sampler_types_from_names(const std::vector<std::string> & names, bool allow_alt_names);
  108. std::vector<enum common_sampler_type> common_sampler_types_from_chars(const std::string & chars);