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

token type ids can be set by optional argument up to python wrapper #1418

Merged
merged 4 commits into from
Aug 28, 2023

Conversation

hachall
Copy link
Contributor

@hachall hachall commented Aug 14, 2023

Description

This PR adds an optional token_type_ids argument to Encoder.translate_batch, to facilitate BERT models with multiple input features (sentence-sentence entailment, context and question answering etc.). The token_type_ids vector is optional at the pybind level, and overloaded into the future_batch_async functions, with all previous endpoints preserved for backwards compatibility.

Closes #1383.

...
output = ct2_model.forward_batch(input_ids, token_type_ids=token_type_ids)
pooler_output = output.pooler_output
pooler_output = np.array(pooler_output)
pooler_output = torch.as_tensor(pooler_output)
ct_logits = classifier(pooler_output)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Docs update
  • Refactor

Testing

The code passes all C++ and python tests. For validation, the modified encoder was tested with both a bert-base-uncased and fine tuned bert-tiny for inference on the train split of the MRPC dataset (sentence pairs), and all output logits verified equal (<10e-12) to the output of a hugging face loaded model at full precision, when passing token_type_ids to both. When quantized, the logits are no longer equal, but the token_type_ids demonstrably improve the classifier accuracy.

Checklist:

  • My code follows the style guidelines of this project
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

@hachall
Copy link
Contributor Author

hachall commented Aug 14, 2023

#1383

include/ctranslate2/encoder.h Outdated Show resolved Hide resolved
include/ctranslate2/models/language_model.h Outdated Show resolved Hide resolved
python/cpp/encoder.cc Outdated Show resolved Hide resolved
python/cpp/encoder.cc Outdated Show resolved Hide resolved
python/cpp/encoder.cc Outdated Show resolved Hide resolved
src/models/language_model.cc Outdated Show resolved Hide resolved
src/models/language_model.cc Outdated Show resolved Hide resolved
src/models/language_model.cc Outdated Show resolved Hide resolved
python/cpp/encoder.cc Outdated Show resolved Hide resolved
@hachall
Copy link
Contributor Author

hachall commented Aug 18, 2023

Thank you for the notes @guillaumekln! I have implemented the changes, let me know if there's anything else I can do

@guillaumekln guillaumekln merged commit e3d0bb0 into OpenNMT:master Aug 28, 2023
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

token_type_ids for BERT models
2 participants