-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add LFM2 and LFM2.5 support and testing #6428
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
Open
qgallouedec
wants to merge
6
commits into
main
Choose a base branch
from
lfm2-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8e29c92
Add LFM2 and LFM2.5 support and testing
qgallouedec d1e34db
Merge branch 'main' into lfm2-support
qgallouedec c4b6d94
address reviews
qgallouedec 6ad5a43
Merge branch 'main' into lfm2-support
qgallouedec 843ca95
Merge branch 'main' into lfm2-support
qgallouedec e6cefec
Merge branch 'main' into lfm2-support
qgallouedec 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
68 changes: 68 additions & 0 deletions
68
scripts/generate_tiny_models/for_causal_lm/lfm2_for_causal_lm.py
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,68 @@ | ||
| # Copyright 2020-2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch | ||
| from transformers import AutoTokenizer, GenerationConfig, Lfm2Config, Lfm2ForCausalLM | ||
|
|
||
| from .._common import ( | ||
| check_dtype_pattern, | ||
| check_transformers_version, | ||
| init_weights_tiny_model, | ||
| print_config_diff, | ||
| push_to_hub, | ||
| smoke_test, | ||
| ) | ||
|
|
||
|
|
||
| check_transformers_version() | ||
|
|
||
| MODEL_ID = "LiquidAI/LFM2-1.2B" | ||
|
|
||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
| generation_config = GenerationConfig.from_pretrained(MODEL_ID) | ||
| config = Lfm2Config( | ||
| vocab_size=65536, | ||
| hidden_size=8, | ||
| num_attention_heads=4, | ||
| num_key_value_heads=2, | ||
| num_hidden_layers=2, | ||
| intermediate_size=32, | ||
| # LFM2 interleaves short convolution layers with full attention layers; `full_attn_idxs` selects which layers get | ||
| # attention, and the rest get a convolution. Keep one of each so both layer types are exercised. | ||
| full_attn_idxs=[1], | ||
| # The MLP derives its actual dim from `intermediate_size`, rounded *up* to a multiple of `block_multiple_of`. At | ||
| # the reference value (256), any tiny `intermediate_size` would still yield a 256-wide MLP, dwarfing the rest of | ||
| # the model. Scale it down instead of disabling `block_auto_adjust_ff_dim`, so the MLP keeps the same code path as | ||
| # the reference. | ||
| block_multiple_of=8, | ||
| # Non-size fields kept aligned with the reference so the tiny config only differs in what we scale down. | ||
| max_position_embeddings=128000, | ||
| norm_eps=1e-05, | ||
| rope_theta=1000000.0, | ||
| conv_bias=False, | ||
| conv_L_cache=3, | ||
| block_auto_adjust_ff_dim=True, | ||
| block_ffn_dim_multiplier=1.0, | ||
| bos_token_id=1, | ||
| # The reference tokenizer's EOS is <|im_end|> (7), not <|endoftext|> (2) which `Lfm2Config` defaults to. | ||
| eos_token_id=7, | ||
| pad_token_id=0, | ||
| use_cache=False, | ||
| ) | ||
| model = Lfm2ForCausalLM(config).to(dtype=torch.bfloat16) | ||
| init_weights_tiny_model(model) | ||
| smoke_test(model, tokenizer) | ||
| check_dtype_pattern(MODEL_ID, model) | ||
| print_config_diff(MODEL_ID, model) | ||
| push_to_hub(model, tokenizer, generation_config, "tiny") |
67 changes: 67 additions & 0 deletions
67
scripts/generate_tiny_models/for_causal_lm/lfm2_for_causal_lm_2_5.py
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,67 @@ | ||
| # Copyright 2020-2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch | ||
| from transformers import AutoTokenizer, GenerationConfig, Lfm2Config, Lfm2ForCausalLM | ||
|
|
||
| from .._common import ( | ||
| check_dtype_pattern, | ||
| check_transformers_version, | ||
| init_weights_tiny_model, | ||
| print_config_diff, | ||
| push_to_hub, | ||
| smoke_test, | ||
| ) | ||
|
|
||
|
|
||
| # LFM2.5 ships a `TokenizersBackend` tokenizer, which was introduced in transformers 5.0.0. This is above TRL's | ||
| # transformers floor, so unlike the other tiny models this one can't be loaded by the floor CI job, and the tests | ||
| # using it are skipped there. | ||
| check_transformers_version("5.0.0") | ||
|
|
||
| MODEL_ID = "LiquidAI/LFM2.5-230M" | ||
|
|
||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
| generation_config = GenerationConfig.from_pretrained(MODEL_ID) | ||
| config = Lfm2Config( | ||
| vocab_size=65536, | ||
| hidden_size=8, | ||
| num_attention_heads=4, | ||
| num_key_value_heads=2, | ||
| num_hidden_layers=2, | ||
| intermediate_size=32, | ||
| # LFM2 interleaves short convolution layers with full attention layers. The reference spells the pattern out with | ||
| # `layer_types` rather than `full_attn_idxs`; keep one of each so both layer types are exercised. | ||
| layer_types=["conv", "full_attention"], | ||
| # Non-size fields kept aligned with the reference so the tiny config only differs in what we scale down. | ||
| max_position_embeddings=128000, | ||
| norm_eps=1e-05, | ||
| rope_parameters={"rope_theta": 1000000.0, "rope_type": "default"}, | ||
| conv_bias=False, | ||
| conv_L_cache=3, | ||
| block_auto_adjust_ff_dim=False, | ||
| block_multiple_of=256, | ||
| block_ffn_dim_multiplier=1.0, | ||
| bos_token_id=1, | ||
| # The reference tokenizer's EOS is <|im_end|> (7), not <|endoftext|> (2) which `Lfm2Config` defaults to. | ||
| eos_token_id=7, | ||
| pad_token_id=0, | ||
| use_cache=False, | ||
| ) | ||
| model = Lfm2ForCausalLM(config).to(dtype=torch.bfloat16) | ||
| init_weights_tiny_model(model) | ||
| smoke_test(model, tokenizer) | ||
| check_dtype_pattern(MODEL_ID, model) | ||
| print_config_diff(MODEL_ID, model) | ||
| push_to_hub(model, tokenizer, generation_config, "tiny", "2.5") |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Missing tied-embedding backward xfail
Medium Severity
This PR adds LFM2 models to
_CHUNKED_LM_HEAD_MODEL_IDSand tightenstest_backward, but the PR notes tied-embedding models shouldxfailattemperature != 1.0. Noxfailor skip ontie_word_embeddingsappears, so CI can fail for LFM2 and other tied tiny models attemperature=0.7.Reviewed by Cursor Bugbot for commit c4b6d94. Configure here.