Skip to content

Support Llama 3.2 conversion (text models) - #33778

Merged
ArthurZucker merged 12 commits into
mainfrom
convert-llama-3.2
Oct 17, 2024
Merged

ArthurZucker merged 12 commits into
mainfrom
convert-llama-3.2

Conversation

@pcuenca

@pcuenca pcuenca commented Sep 27, 2024

Copy link
Copy Markdown
Member

What does this PR do?

Update the existing Llama conversion script to support the new 1B and 3B models.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>
@pcuenca
pcuenca marked this pull request as draft September 27, 2024 19:26
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Initialize from a well-known template.
The guidance is that the changes should be applied to 3.1 models as
well.
Comment on lines +410 to +412
from transformers import AutoTokenizer
t = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
additional_kwargs["chat_template"] = t.chat_template

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it acceptable to load from a well-known tokenizer @ArthurZucker? This way they'd always be in sync, and having the very long template in the script is a bit noisy.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If we use a revision, fine by me!

@pcuenca
pcuenca marked this pull request as ready for review September 27, 2024 20:18
}

CONTEXT_LENGTH_FOR_VERSION = {"3.1": 131072, "3": 8192, "2": 4096, "1": 2048}
CONTEXT_LENGTH_FOR_VERSION = {"Guard-3": 131072, "3.2": 131072, "3.1": 131072, "3": 8192, "2": 4096, "1": 2048}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The Llama Guard 3 model uses a different rope scale and requires a different chat template. An alternative would be to use --guard (which would imply --instruct) instead of adding a new version, but I'm not sure if code would be clearer.

@pcuenca
pcuenca requested a review from ArthurZucker October 8, 2024 11:56
@pcuenca

pcuenca commented Oct 8, 2024

Copy link
Copy Markdown
Member Author

This is now ready for review @ArthurZucker. I exported the Llama 3.2 instruct, base and guard models and verified that the configuration files were identical to what's currently in the Hub.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, LGTM, what's best is usually to try converting at least llama1 7B, and llama2 7B as well! 🤗 this way we are certain there is not regression

@pcuenca

pcuenca commented Oct 15, 2024

Copy link
Copy Markdown
Member Author

I converted Llama 2 and only saw a couple of new properties from the default configuration (for example, mlp_bias) that look fine to me. The only interesting difference is that the converted weights are now exported in bfloat16, while this repo was exported in float16. I believe bfloat16 is correct, because the original weights were released with that dtype (and it's consistent with newer models). Let me know if you think it's necessary to override the dtype in this case, but I think it could potentially mess things up when converting fine-tunes exported in the original format.

I found the error message for tokenizer (< 3.x) conversion to be unclear, so I added a explicit reference to requiring sentencepiece.

I didn't keep a local copy of the Llama 1 research weights, so I can't test that code path now. I re-requested access to download the weights from Meta, not sure how long it'll take.

@ArthurZucker

Copy link
Copy Markdown
Collaborator

No worries, LGTM and I think we can merge! 🤗

@ArthurZucker
ArthurZucker merged commit 7a06d07 into main Oct 17, 2024
@ArthurZucker
ArthurZucker deleted the convert-llama-3.2 branch October 17, 2024 20:37
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
* Support Llama 3.2 conversion (text models)

Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>

* Fix rope factor

* Update chat template

Initialize from a well-known template.
The guidance is that the changes should be applied to 3.1 models as
well.

* Remove import

* Support Llama Guard 3 conversion

* Tokenizer details

* Fix eos added token in base models

* Fix generation config for base models

* Specify revision for known tokenizers

* Style

* Reuse chat templates for older models

* Improve error when converting tokenizer < Llama 3

---------

Co-authored-by: Omar Sanseviero <osanseviero@gmail.com>
maxsloef-goodfire added a commit to maxsloef-goodfire/transformers that referenced this pull request Mar 21, 2026
…ersion

The Llama3Converter hardcodes clean_up_tokenization_spaces=True, which
applies BERT-era string replacements (` .` → `.`, ` !` → `!`, etc.)
that silently corrupt decoded text from Llama 3's BPE tokenizer.

Llama 2's LlamaTokenizer and Llama 4 both use False. The True was
introduced in PR huggingface#30334 and hardcoded in PR huggingface#33778 for backward compat.

Fixes huggingface#35175
ArthurZucker pushed a commit that referenced this pull request Mar 23, 2026
…nversion (#44914)

fix: set clean_up_tokenization_spaces=False in Llama 3 tokenizer conversion

The Llama3Converter hardcodes clean_up_tokenization_spaces=True, which
applies BERT-era string replacements (` .` → `.`, ` !` → `!`, etc.)
that silently corrupt decoded text from Llama 3's BPE tokenizer.

Llama 2's LlamaTokenizer and Llama 4 both use False. The True was
introduced in PR #30334 and hardcoded in PR #33778 for backward compat.

Fixes #35175
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.

3 participants