Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the original batch_id before calling the callback #1440

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions include/ctranslate2/generation.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ namespace ctranslate2 {
}
};

template <typename Options>
Options restore_batch_ids_in_callback(Options options, const std::vector<size_t>& example_index) {
if (options.callback) {
std::function<bool(GenerationStepResult)> wrapped_callback =
[&example_index, callback = std::move(options.callback)]
(GenerationStepResult step_result) {
step_result.batch_id = example_index[step_result.batch_id];
return callback(std::move(step_result));
};

options.callback = std::move(wrapped_callback);
}

return options;
}

class ResolveEndToken {
private:
const Vocabulary& _vocabulary;
Expand Down
30 changes: 30 additions & 0 deletions python/tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,36 @@ def _callback(step_result):
assert len(hypotheses) == 3


def test_callback_batch_id():
# The method will internally sort the input from longest to shortest,
# but we check that the returned batch ids match the user input.

source = [
["ن"] * 1,
["ن"] * 2,
["ن"] * 3,
]

target_prefix = [
["a"],
["b"],
["c"],
]

def _callback(step_result):
assert step_result.token == target_prefix[step_result.batch_id][0]
return True

translator = _get_transliterator()
translator.translate_batch(
source,
target_prefix,
max_batch_size=2,
beam_size=1,
callback=_callback,
)


def test_file_translation(tmp_dir):
input_path = str(tmp_dir.join("input.txt"))
output_path = str(tmp_dir.join("output.txt"))
Expand Down
4 changes: 3 additions & 1 deletion src/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace ctranslate2 {
batch_type,
[options](models::SequenceGeneratorReplica& generator, const Batch& batch) {
spdlog::debug("Running batch generation on {} examples", batch.num_examples());
auto results = generator.generate(batch.get_stream(0), options);
auto results = generator.generate(
batch.get_stream(0),
restore_batch_ids_in_callback(options, batch.example_index));
spdlog::debug("Finished batch generation");
return results;
});
Expand Down
4 changes: 3 additions & 1 deletion src/translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ namespace ctranslate2 {
const Batch& batch,
const TranslationOptions& options) {
spdlog::debug("Running batch translation on {} examples", batch.num_examples());
auto results = model.translate(batch.get_stream(0), batch.get_stream(1), options);
auto results = model.translate(batch.get_stream(0),
batch.get_stream(1),
restore_batch_ids_in_callback(options, batch.example_index));
spdlog::debug("Finished batch translation");
return results;
}
Expand Down