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

Correct minor errors in example notebooks for causal language modelling #926

Merged
merged 5 commits into from
Oct 3, 2023

Conversation

SumanthRH
Copy link
Contributor

What does this PR do?

Corrects minor errors in dataset preprocessing in the example notebooks at examples/causal_language_modeling . I believe there are two mistakes that can cause issues when people are using the same code for a different model or task:

  1. Input tweet text and labels are concatenated after tokenization. However, with 🤗 tokenizers, a BOS token may or may not be added depending on the model if no explicit value is passed for the add_special_tokens argument. For example, with GPT2, a BOS token is not added, while for Llama-2 a BOS token will be added. Because of this, if you simply tokenized the label with tokenizer(labels) and then concatenate with the input sequence, you can have a stray BOS token. You can quickly check this out yourself with the following code block:
from transformers import AutoTokenizer
gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2")
llama_tokenizer = AutoTokenizer.from_pretrained("stabilityai/StableBeluga2") # stable beluga has the same tokenizer as Llama 2
input_text = "@HMRCcustomers No this is my first job"
label = "Neutral"
input_formatted = f"Tweet text : {input_text} Label : "
concat_seq_llama = llama_tokenizer(input_formatted)["input_ids"] + llama_tokenizer(label)["input_ids"]
concat_seq_gpt2 = gpt2_tokenizer(input_formatted)["input_ids"] + gpt2_tokenizer(label)["input_ids"]
concat_seq_llama_decoded = llama_tokenizer.decode(concat_seq_llama)
concat_seq_gpt2_decoded = gpt2_tokenizer.decode(concat_seq_gpt2)
print(concat_seq_llama_decoded)
print(concat_seq_gpt2_decoded)

The outputs for Llama 2 and GPT are <s> Tweet text : @HMRCcustomers No this is my first job Label : <s> Neutral and Tweet text : @HMRCcustomers No this is my first job Label : Neutral respectively. In some cases, the BOS and EOS tokens are the same, so this can lead to lower performance.
2. The final token after concatenating the label should be an EOS token, which may be different from the padding token. Correct me if I'm wrong!

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks, however I would like @pacman100 to have a look here if possible as he wrote those notebooks, just to be sure!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @SumanthRH for fixing the example!

@pacman100 pacman100 merged commit 3d0edcc into huggingface:main Oct 3, 2023
11 checks passed
@SumanthRH SumanthRH deleted the fix-notebooks branch October 13, 2023 19:06
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.

4 participants