| 1021 | } |
| 1022 | |
| 1023 | static void validate_decoding_options(const DecodingOptions& options, const Device device) { |
| 1024 | if (options.beam_size == 0) |
| 1025 | throw std::invalid_argument("The beam size must be > 0"); |
| 1026 | if (options.patience <= 0) |
| 1027 | throw std::invalid_argument("The patience factor must be > 0"); |
| 1028 | if (options.num_hypotheses == 0) |
| 1029 | throw std::invalid_argument("The number of hypotheses must be > 0"); |
| 1030 | if (options.num_hypotheses > get_max_candidates(options.beam_size, options.patience) |
| 1031 | && !options.return_alternatives |
| 1032 | && !(options.beam_size == 1 && options.sampling_topk != 1)) |
| 1033 | throw std::invalid_argument("The number of hypotheses cannot be greater than " |
| 1034 | "beam_size * patience"); |
| 1035 | if (options.min_length > options.max_length) |
| 1036 | throw std::invalid_argument("The minimum decoding length is greater than " |
| 1037 | "the maximum decoding length"); |
| 1038 | if (options.max_length == 0) |
| 1039 | throw std::invalid_argument("The maximum decoding length must be > 0"); |
| 1040 | if (options.repetition_penalty <= 0) |
| 1041 | throw std::invalid_argument("The repetition penalty must be > 0"); |
| 1042 | if (options.prefix_bias_beta >= 1) |
| 1043 | throw std::invalid_argument("The beta value in biased decoding must be < 1"); |
| 1044 | if (options.prefix_bias_beta > 0 && options.return_alternatives) |
| 1045 | throw std::invalid_argument("Biased decoding is not compatible with the return_alternatives " |
| 1046 | "mode"); |
| 1047 | if (options.return_alternatives |
| 1048 | && (options.min_alternative_expansion_prob < 0 |
| 1049 | || options.min_alternative_expansion_prob > 1)) |
| 1050 | throw std::invalid_argument("The minimum alternative expansion probability must be " |
| 1051 | "between 0 and 1"); |
| 1052 | if (options.callback && (options.beam_size != 1 || options.prefix_bias_beta > 0)) |
| 1053 | throw std::invalid_argument("The callback function is not compatible with " |
| 1054 | "beam_size > 1 or prefix_bias_beta > 0"); |
| 1055 | |
| 1056 | if (options.sampling_topp <= 0 || options.sampling_topp > 1) |
| 1057 | throw std::invalid_argument("The sampling_topp parameter must be between 0 and 1"); |
| 1058 | if (options.sampling_topp < 1 |
| 1059 | && options.sampling_topk > static_cast<size_t>(ops::TopPMask::max_num_classes(device))) |
| 1060 | throw std::invalid_argument( |
| 1061 | "The sampling_topp parameter currently requires sampling_topk <= " |
| 1062 | + std::to_string(ops::TopPMask::max_num_classes(device)) |
| 1063 | + " when running on a " + device_to_str(device) + " device"); |
| 1064 | } |
| 1065 | |
| 1066 | static std::unique_ptr<const Sampler> |
| 1067 | make_sampler(const DecodingOptions& options) { |
no test coverage detected