-
Notifications
You must be signed in to change notification settings - Fork 812
Adding support for batch input in BERT Tokenizer with perf benchmark #1745
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
efd69ee
Adding support for batch input in BERT Tokenizer with perf benchmark
parmeet 24ef3d5
Merge branch 'main' of github.com:pytorch/text into batch_bert
parmeet 297aa23
Merge branch 'main' of github.com:pytorch/text into batch_bert
parmeet d7d33db
fix doc and move code out of registration
parmeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from argparse import ArgumentParser | ||
|
||
from benchmark.utils import Timer | ||
from tokenizers import Tokenizer as hf_tokenizer_lib | ||
from torchtext.datasets import EnWik9 | ||
from torchtext.transforms import BERTTokenizer as tt_bert_tokenizer | ||
from transformers import BertTokenizer as hf_bert_tokenizer_slow | ||
|
||
|
||
VOCAB_FILE = "https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt" | ||
|
||
|
||
def benchmark_bert_tokenizer(args): | ||
tt_tokenizer = tt_bert_tokenizer(VOCAB_FILE, return_tokens=True) | ||
hf_tokenizer_slow = hf_bert_tokenizer_slow.from_pretrained("bert-base-uncased") | ||
hf_tokenizer_fast = hf_tokenizer_lib.from_pretrained("bert-base-uncased") | ||
dp = EnWik9().header(args.num_samples) | ||
samples = list(dp) | ||
|
||
with Timer("Running TorchText BERT Tokenizer on non-batched input"): | ||
for s in samples: | ||
tt_tokenizer(s) | ||
|
||
with Timer("Running HF BERT Tokenizer (slow) on non-batched input"): | ||
for s in samples: | ||
hf_tokenizer_slow.tokenize(s) | ||
|
||
with Timer("Running HF BERT Tokenizer (fast) on non-batched input"): | ||
for s in samples: | ||
hf_tokenizer_fast.encode(s) | ||
|
||
with Timer("Running TorchText BERT Tokenizer on batched input"): | ||
tt_tokenizer(samples) | ||
|
||
with Timer("Running HF BERT Tokenizer (fast) on batched input"): | ||
hf_tokenizer_fast.encode_batch(samples) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument("--num-samples", default=1000, type=int) | ||
benchmark_bert_tokenizer(parser.parse_args()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.