Repository navigation
Add SarvamMLA model (sarvamai/sarvam-105b) #44569
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
aashay-sarvam
wants to merge
10
commits into
huggingface:main
Choose a base branch
from
aashay-sarvam:add-sarvam-mla-model
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.
+267
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2968670
Add SarvamMLA model (sarvamai/sarvam-105b)
aashay-sarvam 8cac1eb
Fix sarvam_mla docs placement: move from Vision to Text models section
aashay-sarvam b941609
Address reviewer feedback: use DeepseekV3 classes directly
aashay-sarvam 0706709
Refactor SarvamMLA config to use modular pattern
aashay-sarvam 0f5d73d
Address reviewer feedback: simplify modular config, rebase on main
aashay-sarvam 926004b
add date
vasqu d97435f
Merge branch 'main' into add-sarvam-mla-model
vasqu 3d99694
add exception there
vasqu 9ce210d
fix
vasqu 833c9ef
Merge branch 'main' into add-sarvam-mla-model
vasqu 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
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,48 @@ | ||
| <!--Copyright 2026 Sarvam AI and 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. | ||
|
|
||
| --> | ||
| *This model was released on 2026-03-06 and added to Hugging Face Transformers on 2026-03-17.* | ||
|
|
||
| # SarvamMLA | ||
|
|
||
| ## Overview | ||
|
|
||
| SarvamMLA is a 105B parameter Mixture of Experts (MoE) language model developed by [Sarvam AI](https://www.sarvam.ai/). It uses Multi-head Latent Attention (MLA) combined with sparse MoE routing, architecturally identical to DeepSeek-V3. | ||
|
|
||
| Key architectural features: | ||
|
|
||
| - **Multi-head Latent Attention (MLA)**: Low-rank KV compression with decoupled RoPE, reducing KV cache memory while maintaining performance. | ||
| - **Sparse Mixture of Experts**: 128 routed experts with 8 active per token, plus 1 shared expert. The first layer uses a dense MLP. | ||
| - **DeepSeek YaRN RoPE**: Extended context support up to 131K tokens via YaRN rotary position embeddings. | ||
| - **Sigmoid routing with group-based top-k**: Token-choice routing using sigmoid scores with expert bias correction and group-aware selection. | ||
|
|
||
| This model uses the DeepSeek-V3 architecture with a custom configuration. See the [DeepSeek-V3 documentation](deepseek_v3) for model and forward reference. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| model = AutoModelForCausalLM.from_pretrained( | ||
| "sarvamai/sarvam-105b", | ||
| device_map="auto", | ||
| ) | ||
| tokenizer = AutoTokenizer.from_pretrained("sarvamai/sarvam-105b") | ||
|
|
||
| inputs = tokenizer("Hello, how are you?", return_tensors="pt").to(model.device) | ||
| outputs = model.generate(**inputs, max_new_tokens=50) | ||
| print(tokenizer.decode(outputs[0], skip_special_tokens=True)) | ||
| ``` | ||
|
|
||
| ## SarvamMLAConfig | ||
|
|
||
| [[autodoc]] SarvamMLAConfig |
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,26 @@ | ||
| # Copyright 2026 Sarvam AI and 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. | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils import _LazyModule | ||
| from ...utils.import_utils import define_import_structure | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration_sarvam_mla import * | ||
| else: | ||
| import sys | ||
|
|
||
| _file = globals()["__file__"] | ||
| sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__) |
118 changes: 118 additions & 0 deletions
118
src/transformers/models/sarvam_mla/configuration_sarvam_mla.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,118 @@ | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # This file was automatically generated from src/transformers/models/sarvam_mla/modular_sarvam_mla.py. | ||
| # Do NOT edit this file manually as any edits will be overwritten by the generation of | ||
| # the file from the modular. If any change should be done, please apply the change to the | ||
| # modular_sarvam_mla.py file directly. One of our CI enforces this. | ||
| # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 | ||
| # Copyright 2026 Sarvam AI and 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. | ||
|
|
||
| from huggingface_hub.dataclasses import strict | ||
|
|
||
| from ...configuration_utils import PreTrainedConfig | ||
| from ...modeling_rope_utils import RopeParameters | ||
| from ...utils import auto_docstring | ||
|
|
||
|
|
||
| @auto_docstring(checkpoint="sarvamai/sarvam-105b") | ||
| @strict(accept_kwargs=True) | ||
| class SarvamMLAConfig(PreTrainedConfig): | ||
| r""" | ||
| n_group (`int`, *optional*, defaults to 16): | ||
| Number of groups for routed experts. | ||
| rope_interleave (`bool`, *optional*, defaults to `True`): | ||
| Whether to interleave the rotary position embeddings. | ||
| first_k_dense_replace (`int`, *optional*, defaults to 1): | ||
| Number of dense layers in shallow layers(embed->dense->moe->moe...->lm_head). | ||
| \--k dense layers--/ | ||
|
|
||
| Example: | ||
|
|
||
| ```python | ||
| >>> from transformers import SarvamMLAConfig | ||
|
|
||
| >>> # Initializing a SarvamMLA style configuration | ||
| >>> configuration = SarvamMLAConfig() | ||
|
|
||
| >>> # Accessing the model configuration | ||
| >>> configuration = model.config | ||
| ```""" | ||
|
|
||
| model_type = "sarvam_mla" | ||
| keys_to_ignore_at_inference = ["past_key_values"] | ||
| base_model_tp_plan = { | ||
| "layers.*.mlp.experts.gate_up_proj": "packed_colwise", | ||
| "layers.*.mlp.experts.down_proj": "rowwise", | ||
| "layers.*.mlp.experts": "moe_tp_experts", | ||
| "layers.*.mlp.shared_experts.gate_proj": "colwise", | ||
| "layers.*.mlp.shared_experts.up_proj": "colwise", | ||
| "layers.*.mlp.shared_experts.down_proj": "rowwise", | ||
| "layers.*.mlp.gate_proj": "colwise", | ||
| "layers.*.mlp.up_proj": "colwise", | ||
| "layers.*.mlp.down_proj": "rowwise", | ||
| } | ||
| base_model_pp_plan = { | ||
| "embed_tokens": (["input_ids"], ["inputs_embeds"]), | ||
| "layers": (["hidden_states", "attention_mask"], ["hidden_states"]), | ||
| "norm": (["hidden_states"], ["hidden_states"]), | ||
| } | ||
| attribute_map = { | ||
| "num_local_experts": "n_routed_experts", | ||
| } | ||
|
|
||
| vocab_size: int = 262144 | ||
| hidden_size: int = 4096 | ||
| intermediate_size: int = 16384 | ||
| moe_intermediate_size: int = 2048 | ||
| num_hidden_layers: int = 32 | ||
| num_attention_heads: int = 64 | ||
| num_key_value_heads: int | None = None | ||
| n_shared_experts: int = 1 | ||
| n_routed_experts: int = 128 | ||
| routed_scaling_factor: float = 2.5 | ||
| kv_lora_rank: int = 512 | ||
| q_lora_rank: int | None = None | ||
| qk_rope_head_dim: int = 64 | ||
| v_head_dim: int | None = 128 | ||
| qk_nope_head_dim: int = 128 | ||
| n_group: int | None = 16 | ||
| topk_group: int | None = 2 | ||
| num_experts_per_tok: int | None = 8 | ||
| first_k_dense_replace: int | None = 1 | ||
| norm_topk_prob: bool | None = True | ||
| hidden_act: str = "silu" | ||
| max_position_embeddings: int = 4096 | ||
| initializer_range: float = 0.006 | ||
| rms_norm_eps: float = 1e-6 | ||
| use_cache: bool = True | ||
| pad_token_id: int | None = None | ||
| bos_token_id: int | None = 0 | ||
| eos_token_id: int | list[int] | None = 1 | ||
| pretraining_tp: int | None = 1 | ||
| tie_word_embeddings: bool = False | ||
| rope_parameters: RopeParameters | dict | None = None | ||
| rope_interleave: bool | None = True | ||
| attention_bias: bool = False | ||
| attention_dropout: float | int | None = 0.0 | ||
|
|
||
| def __post_init__(self, **kwargs): | ||
| if self.num_key_value_heads is None: | ||
| self.num_key_value_heads = self.num_attention_heads | ||
|
|
||
| self.qk_head_dim = self.qk_nope_head_dim + self.qk_rope_head_dim | ||
| self.head_dim = self.qk_rope_head_dim | ||
| super().__post_init__(**kwargs) | ||
|
|
||
|
|
||
| __all__ = ["SarvamMLAConfig"] |
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,64 @@ | ||
| # Copyright 2026 Sarvam AI and 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. | ||
|
|
||
| from huggingface_hub.dataclasses import strict | ||
|
|
||
| from ...utils import auto_docstring | ||
| from ..deepseek_v3.configuration_deepseek_v3 import DeepseekV3Config | ||
|
|
||
|
|
||
| @auto_docstring(checkpoint="sarvamai/sarvam-105b") | ||
| @strict(accept_kwargs=True) | ||
| class SarvamMLAConfig(DeepseekV3Config): | ||
|
vasqu marked this conversation as resolved.
Resolved
|
||
| r""" | ||
| n_group (`int`, *optional*, defaults to 16): | ||
| Number of groups for routed experts. | ||
| rope_interleave (`bool`, *optional*, defaults to `True`): | ||
| Whether to interleave the rotary position embeddings. | ||
| first_k_dense_replace (`int`, *optional*, defaults to 1): | ||
| Number of dense layers in shallow layers(embed->dense->moe->moe...->lm_head). | ||
| \--k dense layers--/ | ||
|
|
||
| Example: | ||
|
|
||
| ```python | ||
| >>> from transformers import SarvamMLAConfig | ||
|
|
||
| >>> # Initializing a SarvamMLA style configuration | ||
| >>> configuration = SarvamMLAConfig() | ||
|
|
||
| >>> # Accessing the model configuration | ||
| >>> configuration = model.config | ||
| ```""" | ||
|
|
||
| model_type = "sarvam_mla" | ||
|
|
||
| vocab_size: int = 262144 | ||
| hidden_size: int = 4096 | ||
| intermediate_size: int = 16384 | ||
| num_hidden_layers: int = 32 | ||
| num_attention_heads: int = 64 | ||
| num_key_value_heads: int | None = None | ||
| n_routed_experts: int = 128 | ||
| q_lora_rank: int | None = None | ||
| n_group: int | None = 16 | ||
| topk_group: int | None = 2 | ||
| first_k_dense_replace: int | None = 1 | ||
| initializer_range: float = 0.006 | ||
|
|
||
| def convert_rope_params_to_dict(self, **kwargs): | ||
| raise AttributeError("No BC behavior needed!") | ||
|
|
||
|
|
||
| __all__ = ["SarvamMLAConfig"] | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.