Repository navigation
Add Llama4 text model #4536
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
Add Llama4 text model #4536
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
d1dc553
Add Llama4 text model
yicycyc a3ede51
Merge upstream/develop into Llama4 support
yicycyc eac6aa9
Merge remote-tracking branch 'upstream/develop' into feat/llama4-migr…
yicycyc a5df5d6
Address Llama4 review feedback
yicycyc b4292e0
fix Llama4 chunked attention padding mask
yicycyc 009a292
Merge branch 'develop' into feat/llama4-migration
yicycyc 66640ad
Add Llama4 CE template and LoRA support
yicycyc b725dbc
Apply Llama4 pre-commit formatting
yicycyc ab2a155
Merge branch 'develop' into feat/llama4-migration
yicycyc de595fe
fix(llama4): preserve auto model mapping
yicycyc 9c37e08
Merge branch 'develop' into feat/llama4-migration
yicycyc 713abde
feat(llama4): add multimodal conditional generation model
yicycyc 363949b
fix(llama4): support guard checkpoint loading and cache
yicycyc 2030b3b
style(llama4): apply pre-commit formatting
yicycyc 16fe9a6
docs(llama4): document Llama Guard 4 support
yicycyc b04f92b
fix(llama4): support cached multimodal generation
yicycyc 54e7d32
fix(llama4): preserve auto loading and generation inputs
yicycyc 73eadc2
Merge branch 'develop' into feat/llama4-migration
yicycyc d35d737
fix(auto): restore path fallback after develop merge
yicycyc f428ebe
fix(llama4): honor hidden-state config defaults
yicycyc eb450bb
fix(llama4): propagate multimodal hidden-state default
yicycyc 3a1e36b
fix(llama4): limit recompute to training
yicycyc 14109a9
fix(llama4): address latest model review
yicycyc ef04699
fix(llama4): honor multimodal projector bias
yicycyc 8cf54f5
refactor(llama4): remove unused attention wrapper
yicycyc 2ecbe2a
Merge remote-tracking branch 'upstream/develop' into merge/llama4
yicycyc f1fd861
[Model] Add Llama4 multimodal processors
yicycyc 198afa3
Merge remote-tracking branch 'upstream/develop' into merge/llama4
yicycyc c36290e
fix(llama4): accept visual token masks during template registration
yicycyc dc3e0a4
fix(llama4): disable cache during training recompute
yicycyc 37661f1
Fix Llama4 text and vision templates, LoRA registration and attention…
yicycyc 362a8ae
Merge develop and reconcile llama4 model registrations
yicycyc bd4d1ee
Merge develop and preserve Llama4 and MiniMax registrations
yicycyc 7890e20
Merge branch 'develop' into feat/llama4-migration
yicycyc 9eb4c1d
style: separate Llama4 and Molmo plugin classes
yicycyc cc08161
fix: restore independent Llama4 and Molmo image plugins
yicycyc 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright (c) 2026 PaddlePaddle Authors. 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 sys | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils.lazy_import import _LazyModule | ||
|
|
||
| import_structure = { | ||
| "configuration": ["Llama4TextConfig"], | ||
| "modeling": [ | ||
| "Llama4TextDecoderLayer", | ||
| "Llama4TextModel", | ||
| "Llama4ForCausalLM", | ||
| "Llama4TextPretrainedModel", | ||
| ], | ||
| } | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration import Llama4TextConfig | ||
| from .modeling import ( | ||
| Llama4ForCausalLM, | ||
| Llama4TextDecoderLayer, | ||
| Llama4TextModel, | ||
| Llama4TextPretrainedModel, | ||
| ) | ||
| else: | ||
| sys.modules[__name__] = _LazyModule( | ||
| __name__, | ||
| globals()["__file__"], | ||
| import_structure, | ||
| ) |
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,127 @@ | ||
| # Copyright (c) 2026 PaddlePaddle Authors. 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. | ||
|
|
||
| from ..configuration_utils import PretrainedConfig | ||
| from ..modeling_rope_utils import rope_config_validation, standardize_rope_params | ||
|
|
||
|
|
||
| class Llama4TextConfig(PretrainedConfig): | ||
| model_type = "llama4_text" | ||
|
|
||
| def __init__( | ||
| self, | ||
| vocab_size=202048, | ||
| hidden_size=5120, | ||
| intermediate_size=8192, | ||
| intermediate_size_mlp=16384, | ||
| num_hidden_layers=48, | ||
| num_attention_heads=40, | ||
| num_key_value_heads=8, | ||
| head_dim=128, | ||
| hidden_act="silu", | ||
| max_position_embeddings=131072, | ||
| initializer_range=0.02, | ||
| rms_norm_eps=1e-5, | ||
| use_cache=True, | ||
| pad_token_id=None, | ||
| bos_token_id=1, | ||
| eos_token_id=2, | ||
| tie_word_embeddings=False, | ||
| attention_dropout=0.0, | ||
| attention_bias=False, | ||
| num_experts_per_tok=1, | ||
| num_local_experts=16, | ||
| interleave_moe_layer_step=1, | ||
| use_qk_norm=True, | ||
| output_router_logits=False, | ||
| router_aux_loss_coef=0.001, | ||
| router_jitter_noise=0.0, | ||
| no_rope_layer_interval=4, | ||
| attention_chunk_size=8192, | ||
| attn_temperature_tuning=True, | ||
| floor_scale=8192, | ||
| attn_scale=0.1, | ||
| fuse_rms_norm=False, | ||
| **kwargs, | ||
| ): | ||
| self.vocab_size = vocab_size | ||
| self.hidden_size = hidden_size | ||
| self.intermediate_size = intermediate_size | ||
| self.intermediate_size_mlp = intermediate_size_mlp | ||
| self.num_hidden_layers = num_hidden_layers | ||
| self.num_attention_heads = num_attention_heads | ||
| self.num_key_value_heads = num_key_value_heads | ||
| self.head_dim = head_dim | ||
| self.hidden_act = hidden_act | ||
| self.max_position_embeddings = max_position_embeddings | ||
| self.initializer_range = initializer_range | ||
| self.rms_norm_eps = rms_norm_eps | ||
| self.use_cache = use_cache | ||
| self.tie_word_embeddings = tie_word_embeddings | ||
| self.attention_dropout = attention_dropout | ||
| self.attention_bias = attention_bias | ||
| self.num_experts_per_tok = num_experts_per_tok | ||
| self.num_local_experts = num_local_experts | ||
| self.interleave_moe_layer_step = interleave_moe_layer_step | ||
| self.use_qk_norm = use_qk_norm | ||
| self.output_router_logits = output_router_logits | ||
| self.router_aux_loss_coef = router_aux_loss_coef | ||
| self.router_jitter_noise = router_jitter_noise | ||
| self.no_rope_layer_interval = no_rope_layer_interval | ||
| self.attention_chunk_size = attention_chunk_size | ||
| self.attn_temperature_tuning = attn_temperature_tuning | ||
| self.floor_scale = floor_scale | ||
| self.attn_scale = attn_scale | ||
| self.fuse_rms_norm = fuse_rms_norm | ||
|
|
||
| self.moe_layers = kwargs.pop("moe_layers", None) | ||
| if self.moe_layers is None: | ||
| self.moe_layers = list( | ||
| range( | ||
| interleave_moe_layer_step - 1, | ||
| num_hidden_layers, | ||
| interleave_moe_layer_step, | ||
| ) | ||
| ) | ||
|
|
||
| self.no_rope_layers = kwargs.pop("no_rope_layers", None) | ||
| if not self.no_rope_layers: | ||
| self.no_rope_layers = [int((i + 1) % no_rope_layer_interval != 0) for i in range(num_hidden_layers)] | ||
|
|
||
| self.layer_types = kwargs.pop("layer_types", None) | ||
| if self.layer_types is None: | ||
| self.layer_types = [ | ||
| "chunked_attention" if self.no_rope_layers[i] else "full_attention" for i in range(num_hidden_layers) | ||
| ] | ||
|
|
||
| self.rope_theta = kwargs.get("rope_theta", 500000.0) | ||
| self.rope_scaling = kwargs.pop("rope_scaling", None) | ||
| rope_parameters = kwargs.pop("rope_parameters", None) | ||
| if rope_parameters is None: | ||
| rope_parameters = self.rope_scaling.copy() if isinstance(self.rope_scaling, dict) else {} | ||
| rope_parameters.setdefault("rope_type", rope_parameters.pop("type", "default")) | ||
| rope_parameters.setdefault("rope_theta", self.rope_theta) | ||
| self.rope_parameters = rope_parameters | ||
| standardize_rope_params(self, rope_theta=self.rope_theta) | ||
| rope_config_validation(self) | ||
|
|
||
| kwargs.setdefault("_attn_implementation", "eager") | ||
|
|
||
| super().__init__( | ||
| pad_token_id=pad_token_id, | ||
| bos_token_id=bos_token_id, | ||
| eos_token_id=eos_token_id, | ||
| tie_word_embeddings=tie_word_embeddings, | ||
| **kwargs, | ||
| ) |
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.
官方用ipython,而不是user。上面的llama3也是ipython
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.
已修正。工具返回对应的
format_observationrole 改为官方使用的ipython,并增加模板回归测试;普通用户消息仍使用user。