-
Notifications
You must be signed in to change notification settings - Fork 96
Add support for SmolLM3 models #934
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a1e59e
refactor(inference): reorder automodels
dacorvo 10c54a9
chore: bump transformers version
dacorvo c6dcb50
chore: bump vllm version
dacorvo f0700e0
test(vllm): device argument is deprecated
dacorvo 431556b
chore: bump dev version
dacorvo 28c679a
feat(inference): add SmolLM3
dacorvo a7fadc2
test(decoder): add smollm3 tests
dacorvo 0e393aa
ci: add smollm3 models to cache workflow
dacorvo 8143978
fix(Mixtral): workaround null head_dim
dacorvo 52a50fc
fix(pipeline): increase minimum sequence_length
dacorvo af17ad4
fix: do not return dict in CLIP models
dacorvo ea64e27
fix(t5): explicitly convert past_key_values to a Cache
dacorvo d665dc8
fix(t5): adapt T5 attention custom modeling
dacorvo 1932d28
fix(generation): call to non-existent method
dacorvo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ jobs: | |
| llama3.1-70b, | ||
| qwen2.5-large, | ||
| llama-variants, | ||
| smollm3, | ||
| ] | ||
| steps: | ||
| - name: Checkout | ||
|
|
||
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
116 changes: 116 additions & 0 deletions
116
optimum/neuron/models/inference/smollm3/modeling_smollm3.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,116 @@ | ||
| # coding=utf-8 | ||
| # Copyright 2025 The HuggingFace Inc. 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. | ||
| """PyTorch SmolLM3 model for NXD inference.""" | ||
|
|
||
| import logging | ||
|
|
||
| from neuronx_distributed.parallel_layers.layers import ( | ||
| ColumnParallelLinear, | ||
| ParallelEmbedding, | ||
| ) | ||
| from torch import nn | ||
| from transformers.models.smollm3.configuration_smollm3 import SmolLM3Config | ||
|
|
||
| from ..backend.config import NxDNeuronConfig # noqa: E402 | ||
| from ..backend.modules.attention.attention_base import NeuronAttentionBase | ||
| from ..backend.modules.attention.utils import RotaryEmbedding | ||
| from ..backend.modules.custom_calls import CustomRMSNorm | ||
| from ..backend.modules.decoder import NxDDecoderModel | ||
| from ..llama.modeling_llama import ( | ||
| LlamaNxDModelForCausalLM, | ||
| NeuronLlamaDecoderLayer, | ||
| ) | ||
|
|
||
|
|
||
| logger = logging.getLogger("Neuron") | ||
|
|
||
|
|
||
| class NeuronSmolLM3Attention(NeuronAttentionBase): | ||
| """ | ||
| The only difference with the NeuronAttentionBase is the definition of the SmolLM3 rotary embedding | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: SmolLM3Config, | ||
| neuron_config: NxDNeuronConfig, | ||
| layer_idx: int, | ||
| qkv_proj_bias: bool | None = False, | ||
| o_proj_bias: bool | None = False, | ||
| qk_scale: float | None = None, | ||
| ): | ||
| if config.use_sliding_window: | ||
| raise ValueError("SmolLM3 for Neuron does not support sliding window attention.") | ||
| if getattr(config, "rope_scaling", None) is not None: | ||
| raise ValueError("SmolLM3 for Neuron does not support rope scaling.") | ||
| super().__init__( | ||
| config, neuron_config, qkv_proj_bias=qkv_proj_bias, o_proj_bias=o_proj_bias, qk_scale=qk_scale | ||
| ) | ||
| if config.no_rope_layers[layer_idx]: | ||
| # Yes, the condition is slightly counter-intuitive, but that is the transformers convention | ||
| head_dim = config.hidden_size // config.num_attention_heads | ||
| self.rotary_emb = RotaryEmbedding( | ||
| head_dim, | ||
| max_position_embeddings=config.max_position_embeddings, | ||
| base=config.rope_theta, | ||
| ) | ||
| else: | ||
| self.rotary_emb = None | ||
|
|
||
|
|
||
| class NeuronSmolLM3DecoderLayer(NeuronLlamaDecoderLayer): | ||
| def __init__(self, config: SmolLM3Config, neuron_config: NxDNeuronConfig, layer_idx: int): | ||
| super().__init__(config, neuron_config) | ||
| self.self_attn = NeuronSmolLM3Attention(config, neuron_config, layer_idx) | ||
|
|
||
|
|
||
| class NxDSmolLM3Model(NxDDecoderModel): | ||
| """ | ||
| The neuron version of the SmolLM3Model | ||
| """ | ||
|
|
||
| def __init__(self, config: SmolLM3Config, neuron_config: NxDNeuronConfig): | ||
| super().__init__(config, neuron_config) | ||
|
|
||
| self.embed_tokens = ParallelEmbedding( | ||
| config.vocab_size, | ||
| config.hidden_size, | ||
| config.pad_token_id, | ||
| dtype=neuron_config.torch_dtype, | ||
| shard_across_embedding=not neuron_config.vocab_parallel, | ||
| sequence_parallel_enabled=False, | ||
| pad=True, | ||
| use_spmd_rank=neuron_config.vocab_parallel, | ||
| ) | ||
|
|
||
| self.lm_head = ColumnParallelLinear( | ||
| config.hidden_size, | ||
| config.vocab_size, | ||
| gather_output=not neuron_config.on_device_sampling, | ||
| bias=False, | ||
| pad=True, | ||
| ) | ||
|
|
||
| self.layers = nn.ModuleList( | ||
| [ | ||
| NeuronSmolLM3DecoderLayer(config, neuron_config, layer_idx) | ||
| for layer_idx in range(config.num_hidden_layers) | ||
| ] | ||
| ) | ||
| self.norm = CustomRMSNorm(config.hidden_size, eps=config.rms_norm_eps) | ||
|
|
||
|
|
||
| class SmolLM3NxDModelForCausalLM(LlamaNxDModelForCausalLM): | ||
| _model_cls = NxDSmolLM3Model |
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
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.
no tiny version?
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.
Unfortunately, no