| 1304 | } |
| 1305 | |
| 1306 | std::vector<DecodingResult> |
| 1307 | decode(layers::Decoder& decoder, |
| 1308 | layers::DecoderState& state, |
| 1309 | std::vector<std::vector<size_t>> start_tokens, |
| 1310 | std::vector<size_t> end_ids, |
| 1311 | DecodingOptions options) { |
| 1312 | validate_decoding_options(options, decoder.device()); |
| 1313 | const size_t batch_size = start_tokens.size(); |
| 1314 | |
| 1315 | if (batch_size == 0) |
| 1316 | throw std::invalid_argument("No decoder start tokens are set"); |
| 1317 | |
| 1318 | std::vector<DecodingResult> results; |
| 1319 | |
| 1320 | if (decoder.output_layer_is_updated()) { |
| 1321 | end_ids = map_to_output_word_ids(decoder, end_ids); |
| 1322 | |
| 1323 | for (auto& ids : start_tokens) |
| 1324 | ids = map_to_output_word_ids(decoder, ids); |
| 1325 | for (auto& ids : options.disable_sequences) |
| 1326 | ids = map_to_output_word_ids(decoder, ids); |
| 1327 | |
| 1328 | options.disable_ids = map_to_output_word_ids(decoder, options.disable_ids); |
| 1329 | options.disable_ids_begin = map_to_output_word_ids(decoder, options.disable_ids_begin); |
| 1330 | } |
| 1331 | |
| 1332 | if (options.return_alternatives) { |
| 1333 | results.reserve(batch_size); |
| 1334 | for (size_t i = 0; i < batch_size; ++i) { |
| 1335 | layers::DecoderState batch_state = get_batch_state(state, i); |
| 1336 | results.emplace_back(decode_alternatives(decoder, |
| 1337 | batch_state, |
| 1338 | start_tokens[i], |
| 1339 | end_ids, |
| 1340 | options)); |
| 1341 | } |
| 1342 | |
| 1343 | } else { |
| 1344 | std::vector<size_t> start_ids; |
| 1345 | std::vector<std::vector<size_t>> prefix_ids; |
| 1346 | std::tie(start_ids, prefix_ids) = split_start_tokens(start_tokens); |
| 1347 | |
| 1348 | const auto search_strategy = make_search_strategy(options); |
| 1349 | const auto sampler = make_sampler(options); |
| 1350 | const auto logits_processors = make_logits_processors(options); |
| 1351 | results = search_strategy->search(decoder, |
| 1352 | state, |
| 1353 | *sampler, |
| 1354 | start_ids, |
| 1355 | end_ids, |
| 1356 | options.start_step, |
| 1357 | options.max_length, |
| 1358 | options.min_length, |
| 1359 | options.return_scores, |
| 1360 | options.return_attention, |
| 1361 | options.return_logits_vocab, |
| 1362 | options.return_prefix, |
| 1363 | options.num_hypotheses, |