EmbeddingNandi-450M / configuration_nandi_encoder.py
akanyaani's picture
Remove internal references and the unused masked-LM head; fix the languages and the Matryoshka load-time example
518dfa4 verified
Raw History Blame Contribute Delete
4.59 kB
# Copyright 2026 FrontiersMind. 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.
"""Configuration of the Nandi encoder (the bidirectional Nandi architecture of NandiBERT and EmbeddingNandi)."""
from transformers.configuration_utils import PretrainedConfig
class NandiEncoderConfig(PretrainedConfig):
r"""
Configuration for the Nandi encoder. The architecture is the Nandi decoder's (RMSNorm, SwiGLU, GQA, RoPE,
factorized embeddings, layer sharing) with full bidirectional attention.
```python
>>> from sentence_transformers import SentenceTransformer
>>> model = SentenceTransformer("FrontiersMind/EmbeddingNandi-450M", trust_remote_code=True)
```
Args:
classifier_pooling (`str`, *optional*, defaults to `"mean"`):
Pooling for `NandiForSequenceClassification`: `"mean"` over non-padding tokens, or `"first"` (the
`<|endoftext|>` token every input starts with).
classifier_dropout (`float`, *optional*, defaults to 0.0):
Dropout before the classification heads.
"""
model_type = "nandi_encoder"
def __init__(
self,
vocab_size=131072,
hidden_size=832,
intermediate_size=2496,
num_hidden_layers=16,
num_attention_heads=16,
num_key_value_heads=4,
head_dim=None,
hidden_act="silu",
max_position_embeddings=2048,
initializer_range=0.02,
rms_norm_eps=1e-5,
pad_token_id=3,
bos_token_id=0,
eos_token_id=0,
mask_token_id=131062,
tie_word_embeddings=True,
rope_parameters=None,
attention_bias=False,
attention_dropout=0.0,
mlp_bias=False,
factorized_embedding=True,
embedding_rank=196,
layer_sharing=True,
layer_sharing_repeats=2,
classifier_pooling="mean",
classifier_dropout=0.0,
qk_norm=False,
**kwargs,
):
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
self.num_key_value_heads = num_key_value_heads if num_key_value_heads is not None else num_attention_heads
self.head_dim = head_dim if head_dim is not None else hidden_size // num_attention_heads
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.mask_token_id = mask_token_id
self.rope_parameters = rope_parameters if rope_parameters is not None else {"rope_type": "default", "rope_theta": 100000.0}
self.attention_bias = attention_bias
self.attention_dropout = attention_dropout
self.mlp_bias = mlp_bias
self.factorized_embedding = factorized_embedding
self.embedding_rank = embedding_rank
self.layer_sharing = layer_sharing
self.layer_sharing_repeats = layer_sharing_repeats if layer_sharing else 1
if classifier_pooling not in ("mean", "first"):
raise ValueError(f"`classifier_pooling` must be 'mean' or 'first', got {classifier_pooling!r}")
self.classifier_pooling = classifier_pooling
self.classifier_dropout = classifier_dropout
self.qk_norm = qk_norm # RMSNorm on Q and K per head (over head_dim) before RoPE
if self.factorized_embedding and self.embedding_rank <= 0:
raise ValueError(f"`embedding_rank` must be positive when `factorized_embedding=True`, got {embedding_rank}.")
if self.layer_sharing_repeats < 1:
raise ValueError(f"`layer_sharing_repeats` must be >= 1, got {self.layer_sharing_repeats}.")
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,
)
__all__ = ["NandiEncoderConfig"]