-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Prompt tokenization bugfix #4197
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47776d9
Updated default virtual token placeholder
vadam5 2e0d10d
Python style fix
vadam5 b45c8e9
Addressed reviewer comments
vadam5 e15e9dd
Merge branch 'main' into prompt_tokenization_bugfix
vadam5 0ef89fd
Merge branch 'main' into prompt_tokenization_bugfix
vadam5 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
import torch | ||
|
||
from nemo.collections.nlp.data.language_modeling.megatron.gpt_prompt_learning_dataset import GPTPromptLearningDataset | ||
from nemo.collections.nlp.modules.common import VirtualPromptSource | ||
from nemo.collections.nlp.modules.common import VirtualPromptPlaceholderToken, VirtualPromptSource | ||
from nemo.collections.nlp.modules.common.tokenizer_utils import get_nmt_tokenizer | ||
from nemo.core import Dataset | ||
|
||
|
@@ -61,7 +61,7 @@ def create_temp_dataset(): | |
def get_task_templates(): | ||
task_templates = {} | ||
task_templates['task name A'] = { | ||
"prompt_template": "<|VIRTUAL_PROMPT_0|>{text}{answer}", | ||
"prompt_template": "<|VIRTUAL_PROMPT_0|> {text}{answer}", | ||
"prompt_template_fields": ['text', 'answer'], | ||
"total_virtual_tokens": 5, | ||
"virtual_token_splits": [5], | ||
|
@@ -71,8 +71,8 @@ def get_task_templates(): | |
"task_id_num": 0, | ||
} | ||
task_templates['task name B'] = { | ||
"prompt_template": "<|VIRTUAL_PROMPT_0|>{question}<|VIRTUAL_PROMPT_1|>{answer}{extra}", | ||
"prompt_template_fields": ['question', 'answer'], | ||
"prompt_template": "<|VIRTUAL_PROMPT_0|> {question} <|VIRTUAL_PROMPT_1|> {answer}{extra}", | ||
"prompt_template_fields": ['question', 'answer', 'extra'], | ||
"total_virtual_tokens": 10, | ||
"virtual_token_splits": [7, 3], | ||
"truncate_field": None, | ||
|
@@ -83,6 +83,15 @@ def get_task_templates(): | |
return task_templates | ||
|
||
|
||
def get_pseudo_tokens(total_virtual_tokens): | ||
pseudo_tokens = [ | ||
VirtualPromptPlaceholderToken.BASE.value + str(i) + VirtualPromptPlaceholderToken.END.value | ||
for i in range(total_virtual_tokens) | ||
] | ||
|
||
return pseudo_tokens | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you have the similar logics in the prompt_lenaring_model file. Put the function there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can do that |
||
|
||
class TestMegatronGPTPromptLearningDataset: | ||
@pytest.mark.run_only_on('GPU') | ||
@pytest.mark.unit | ||
|
@@ -92,9 +101,8 @@ def test_init_prompt_learning_dataset(self): | |
dataset_path = create_temp_dataset() | ||
|
||
# Setup virtual token place holders | ||
pseudo_token_base = 'PROMPT_' | ||
max_virtual_tokens = 10 | ||
pseudo_tokens = [pseudo_token_base + str(i) for i in range(max_virtual_tokens)] | ||
total_virtual_tokens = 10 | ||
pseudo_tokens = get_pseudo_tokens(total_virtual_tokens) | ||
tokenizer.add_special_tokens({'additional_special_tokens': pseudo_tokens}) | ||
|
||
dataset = get_prompt_tuning_dataset( | ||
|
@@ -119,9 +127,8 @@ def test_prompt_learning_dataset_collate_fn_prompt_table(self): | |
dataset_path = create_temp_dataset() | ||
|
||
# Setup virtual token place holders | ||
pseudo_token_base = 'PROMPT_' | ||
total_virtual_tokens = 10 | ||
pseudo_tokens = [pseudo_token_base + str(i) for i in range(total_virtual_tokens)] | ||
pseudo_tokens = get_pseudo_tokens(total_virtual_tokens) | ||
tokenizer.add_special_tokens({'additional_special_tokens': pseudo_tokens}) | ||
|
||
dataset = get_prompt_tuning_dataset( | ||
|
@@ -153,9 +160,8 @@ def test_prompt_learning_dataset_collate_fn_prompt_encoder(self): | |
dataset_path = create_temp_dataset() | ||
|
||
# Setup virtual token place holders | ||
pseudo_token_base = 'PROMPT_' | ||
total_virtual_tokens = 10 | ||
pseudo_tokens = [pseudo_token_base + str(i) for i in range(total_virtual_tokens)] | ||
pseudo_tokens = get_pseudo_tokens(total_virtual_tokens) | ||
tokenizer.add_special_tokens({'additional_special_tokens': pseudo_tokens}) | ||
|
||
dataset = get_prompt_tuning_dataset( | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you add the space around the virtual tokens? If space is needed, virtual prompt should learn to be a space embedding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was playing around with the unit tests to help diagnose a bug with the huggingface tokenizer. This change isn't needed, I just forgot to put it back. I removed the spaces so they should be back the way they were.