# Copyright 2025 The EasyDeL Author @erfanzar (Erfan Zare Chavoshi).
#
# 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
#
# https://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 functools
from typing import ClassVar
import chex
import jax
import jax.numpy as jnp
from eformer import common_types
from eformer.escale import apply_logical_sharding
from eformer.loggings import get_logger
from ejkernel.types import MaskInfo
from flax import nnx as nn
from jax.ad_checkpoint import checkpoint_name
from jaxtyping import Array, Bool, Float, Int
from easydel.infra.base_module import EasyDeLBaseModule
from easydel.infra.factory import TaskType, register_module
from easydel.infra.loss_utils import auxiliary_load_balancing_loss_func
from easydel.infra.modeling_outputs import (
BaseModelOutput,
DecoderLayerOutput,
MoeCausalLMOutput,
MoeModelOutput,
)
from easydel.infra.utils import ACT2FN, auto_remat, get_dot_general_by_bits
from easydel.layers.attention import FlexibleAttentionModule
from easydel.layers.attention_unified import UnifiedAttention
from easydel.layers.base_modules import BaseCausalLMModule
from easydel.layers.caching import (
RaggedPagesCache,
RaggedPagesCacheView,
RaggedPagesMetadata,
TransformerCache,
TransformerCacheMetaData,
TransformerCacheView,
TransformerMetadata,
)
from easydel.layers.linear import ColumnParallelLinear, RowParallelLinear
from easydel.layers.moe import (
BaseMoeModule,
ColumnParallelMoELinear,
MoeLoadBalancingStrategy,
MoeRoutingStrategy,
RowParallelMoELinear,
)
from easydel.layers.norms import RMSNorm
from easydel.layers.rotary_embedding import yarn_get_mscale
from .xerxes2_configuration import Xerxes2Config as Xerxes2Config
logger = get_logger(__name__)
[docs]class Xerxes2Attention(UnifiedAttention):
"""Xerxes2 Multi-head Latent Attention.
Inherits MLA implementation from UnifiedAttention base class.
Uses a compressed KV representation with LoRA and separate nope/rope dimensions.
"""
projection_mapping: ClassVar[dict[str, str]] = {
"mla_q_proj": "q_proj",
"mla_q_a_proj": "q_a_proj",
"mla_q_a_layernorm": "q_a_layernorm",
"mla_q_b_proj": "q_b_proj",
"mla_kv_a_proj_with_mqa": "kv_a_proj_with_mqa",
"mla_kv_a_layernorm": "kv_a_layernorm",
"mla_kv_b_proj": "kv_b_proj",
"output_projection": "o_proj",
}
def __init__(
self,
config: Xerxes2Config,
layer_idx: int,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: str | jax.lax.Precision | None = None,
*,
rngs: nn.Rngs,
):
# Set MLA-specific dimensions before calling super().__init__()
# so they're available in define_network
self.config = config
self.q_head_dim = config.qk_nope_head_dim + config.qk_rope_head_dim
self.qk_nope_head_dim = config.qk_nope_head_dim
self.qk_rope_head_dim = config.qk_rope_head_dim
self.v_head_dim = config.vhead_dim
self.kv_lora_rank = config.kv_lora_dim
super().__init__(
config,
dtype,
param_dtype,
precision,
rngs=rngs,
layer_idx=layer_idx,
attention_type="mla",
causal=True,
use_mla_lora=config.q_lora_dim is not None,
)
# Override head_dim for MLA - use value head dimension for output merging
self.head_dim = self.v_head_dim
[docs] def define_network(
self,
config: Xerxes2Config,
dtype: jnp.dtype,
param_dtype: jnp.dtype,
precision: jax.lax.Precision,
rngs: nn.Rngs,
):
"""Define MLA-specific network structure for Xerxes2."""
if not self.use_mla_lora:
setattr(
self,
self.projection_mapping["mla_q_proj"],
ColumnParallelLinear(
config.hidden_size,
config.num_attention_heads * self.q_head_dim,
rngs=rngs,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
precision=precision,
**get_dot_general_by_bits(config.bits, config.easy_method),
),
)
else:
setattr(
self,
self.projection_mapping["mla_q_a_proj"],
ColumnParallelLinear(
config.hidden_size,
config.q_lora_dim,
rngs=rngs,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
precision=precision,
**get_dot_general_by_bits(config.bits, config.easy_method),
),
)
setattr(
self,
self.projection_mapping["mla_q_a_layernorm"],
nn.LayerNorm(
config.q_lora_dim,
rngs=rngs,
dtype=dtype,
param_dtype=param_dtype,
),
)
setattr(
self,
self.projection_mapping["mla_q_b_proj"],
ColumnParallelLinear(
config.q_lora_dim,
config.num_attention_heads * self.q_head_dim,
rngs=rngs,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
precision=precision,
**get_dot_general_by_bits(config.bits, config.easy_method),
),
)
setattr(
self,
self.projection_mapping["mla_kv_a_proj_with_mqa"],
ColumnParallelLinear(
config.hidden_size,
config.kv_lora_dim + config.qk_rope_head_dim,
rngs=rngs,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
precision=precision,
**get_dot_general_by_bits(config.bits, config.easy_method),
),
)
setattr(
self,
self.projection_mapping["mla_kv_a_layernorm"],
nn.LayerNorm(
config.kv_lora_dim,
rngs=rngs,
dtype=dtype,
param_dtype=param_dtype,
),
)
setattr(
self,
self.projection_mapping["mla_kv_b_proj"],
ColumnParallelLinear(
config.kv_lora_dim,
config.num_attention_heads * (config.qk_nope_head_dim + config.vhead_dim),
rngs=rngs,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
precision=precision,
**get_dot_general_by_bits(config.bits, config.easy_method),
),
)
setattr(
self,
self.projection_mapping["output_projection"],
RowParallelLinear(
config.num_attention_heads * self.v_head_dim,
config.hidden_size,
rngs=rngs,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
precision=precision,
**get_dot_general_by_bits(config.bits, config.easy_method),
),
)
self.rotary = self._create_rotary(config, dtype)
self.attention_performer = self._create_attention_performer(config, rngs)
def _create_attention_performer(self, config, rngs):
"""Create attention performer module."""
softmax_scale = self.q_head_dim**-0.5
if self.config.rope_scaling is not None:
mscale_all_dim = self.config.rope_scaling.get("mscale_all_dim", 0)
scaling_factor = self.config.rope_scaling["factor"]
if mscale_all_dim:
mscale = yarn_get_mscale(scaling_factor, mscale_all_dim)
softmax_scale = softmax_scale * mscale * mscale
return FlexibleAttentionModule(
rngs=rngs,
base_config=config,
softmax_scale=softmax_scale,
dropout_prob=0.0,
)
[docs]class Xerxes2MLP(nn.Module):
"""Feed-forward network used in dense Xerxes2 decoder layers."""
def __init__(
self,
config: Xerxes2Config,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: str | jax.lax.Precision | None = None,
*,
rngs: nn.Rngs,
):
self.config = config
self.dtype = dtype
self.param_dtype = param_dtype
self.precision = precision
self.rngs = rngs
self.act = nn.silu
column_parallel_linear = functools.partial(
ColumnParallelLinear,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
rngs=rngs,
**get_dot_general_by_bits(config.bits, config.easy_method),
)
row_parallel_linear = functools.partial(
RowParallelLinear,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
kernel_init=jax.nn.initializers.normal(config.initializer_range),
rngs=rngs,
**get_dot_general_by_bits(config.bits, config.easy_method),
)
self.gate_up_proj = column_parallel_linear(config.hidden_size, 2 * config.intermediate_size, rngs=rngs)
self.down_proj = row_parallel_linear(config.intermediate_size, config.hidden_size, rngs=rngs)
def __call__(
self, hidden_states: Float[Array, "batch seq_len hidden_dim"]
) -> Float[Array, "batch seq_len hidden_dim"]:
hidden_states = apply_logical_sharding(
hidden_states,
dynamic_axes=common_types.HiddenStateSharding,
partition_manager=self.config.partition_manager,
)
up_states = self.gate_up_proj(hidden_states)
gate, up_states = jnp.split(up_states, 2, axis=-1)
hidden_states = checkpoint_name(self.down_proj(up_states * nn.silu(gate)), "mlp_output")
hidden_states = apply_logical_sharding(
hidden_states,
dynamic_axes=common_types.HiddenStateSharding,
partition_manager=self.config.partition_manager,
)
return hidden_states
[docs]class Xerxes2MoeMLPStack(nn.Module):
"""Xerxes2Moe MoE MLP using the new ParallelMoELinear layers."""
def __init__(
self,
config: Xerxes2Config,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: jax.lax.PrecisionLike = None,
*,
rngs: nn.Rngs,
):
super().__init__()
self.config = config
self.dtype = dtype
self.param_dtype = param_dtype
self.precision = precision
self.gate_proj = ColumnParallelMoELinear(
num_experts=config.num_experts,
in_features=config.hidden_size,
out_features=config.moe_intermediate_size,
rngs=rngs,
kernel_init=nn.initializers.normal(),
use_bias=False,
partition_manager=config.partition_manager,
use_expert_tensor_mode=config.use_expert_tensor_mode,
dtype=dtype,
param_dtype=param_dtype,
)
self.down_proj = RowParallelMoELinear(
num_experts=config.num_experts,
in_features=config.moe_intermediate_size,
out_features=config.hidden_size,
rngs=rngs,
use_bias=False,
kernel_init=nn.initializers.normal(),
partition_manager=config.partition_manager,
use_expert_tensor_mode=config.use_expert_tensor_mode,
dtype=dtype,
param_dtype=param_dtype,
)
self.up_proj = ColumnParallelMoELinear(
num_experts=config.num_experts,
in_features=config.hidden_size,
out_features=config.moe_intermediate_size,
rngs=rngs,
use_bias=False,
kernel_init=nn.initializers.normal(),
partition_manager=config.partition_manager,
use_expert_tensor_mode=config.use_expert_tensor_mode,
dtype=dtype,
param_dtype=param_dtype,
)
self.act_fn = ACT2FN[config.hidden_act]
def __call__(
self,
hidden_states: Float[Array, "batch seq_len hidden_dim"],
group_sizes: chex.Array,
sorted_experts: chex.Array | None = None,
) -> chex.Array:
"""Forward pass through MoE MLP."""
return checkpoint_name(
self.down_proj(
self.act_fn(self.gate_proj(hidden_states, group_sizes, sorted_experts))
* self.up_proj(hidden_states, group_sizes, sorted_experts),
group_sizes,
sorted_experts,
),
"moe_output",
)
[docs]class Xerxes2MoeSparseBlock(BaseMoeModule):
"""Sparse Mixture of Experts (MoE) block for Xerxes2 MoE.
This block routes input hidden states to a selected subset of experts
and combines their outputs.
Attributes:
config (Xerxes2MoeConfig): Configuration object for the model.
gate (ParallelLinear): Linear layer for the gating network.
experts (nn.List[Xerxes2MoeMLP]): List of expert MLP modules.
dtype (jnp.dtype): Data type for computations.
param_dtype (jnp.dtype): Data type for parameters.
precision (jax.lax.PrecisionLike): Precision setting for matrix multiplications.
rngs (nn.Rngs): Random number generators.
"""
def __init__(
self,
config: Xerxes2Config,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: jax.lax.PrecisionLike = None,
*,
rngs: nn.Rngs,
):
"""Initializes the Xerxes2MoeSparseBlock module.
Args:
config (Xerxes2MoeConfig): The configuration object for the model.
dtype (jnp.dtype): Data type for computations (default: jnp.float32).
param_dtype (jnp.dtype): Data type for parameters (default: jnp.float32).
precision (jax.lax.PrecisionLike): Precision setting for JAX operations (default: None).
rngs (nn.Rngs): Random number generators.
"""
super().__init__(
config=config,
n_routed_experts=config.num_experts,
num_experts_per_tok=config.num_experts_per_tok,
hidden_size=config.hidden_size,
lbl_coef=None,
rzl_coef=None,
routing_strategy=MoeRoutingStrategy.TOP_K if config.norm_topk_prob else MoeRoutingStrategy.TOP_K_NDIV,
load_balancing_strategy=MoeLoadBalancingStrategy.STANDARD,
)
self.config = config
self.dtype = dtype
self.param_dtype = param_dtype
self.precision = precision
self.gate = ColumnParallelLinear(
config.hidden_size,
config.num_experts,
use_bias=False,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
kernel_init=nn.initializers.normal(config.initializer_range),
)
self.experts = Xerxes2MoeMLPStack(
config=config,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
)
def __call__(self, hidden_states: Float[Array, "batch seq_len hidden_dim"]) -> tuple[chex.Array, chex.Array]:
"""Forward pass of the Sparse MoE block.
Args:
hidden_states (chex.Array): Input hidden states (batch_size * sequence_length, hidden_dim).
Returns:
tp.Tuple[chex.Array, chex.Array]: A tuple containing:
- final_hidden_states (chex.Array): The output hidden states after MoE processing.
- router_logits (chex.Array): The logits output by the gating network.
"""
out, router_logits = self.moe_call(
hidden_state=hidden_states,
gate_layer=self.gate,
expert_layer=self.experts,
wi_kernel=self.experts.gate_proj.kernel.value,
wu_kernel=self.experts.up_proj.kernel.value,
wd_kernel=self.experts.down_proj.kernel.value,
act_fn=self.experts.act_fn,
)
return checkpoint_name(out, "moe_expert_output"), checkpoint_name(router_logits, "moe_router_logits")
[docs]class Xerxes2DecoderLayer(nn.Module):
"""Transformer decoder layer with Xerxes2 attention and optional MoE MLP."""
def __init__(
self,
config: Xerxes2Config,
layer_idx: int,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: str | jax.lax.Precision | None = None,
*,
rngs: nn.Rngs,
):
self.config = config
self.dtype = dtype
self.param_dtype = param_dtype
self.precision = precision
self.rngs = rngs
attn_block, mlp_block, moe_block = auto_remat(
Xerxes2Attention,
Xerxes2MLP,
Xerxes2MoeSparseBlock,
policy=config.gradient_checkpointing,
save_names=config.gradient_checkpointing_targets,
exclude_names=config.gradient_checkpointing_targets,
)
self.self_attn = attn_block(
config=self.config,
layer_idx=layer_idx,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
)
self.is_moe = (layer_idx not in config.mlp_only_layers) and (
config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0
)
if self.is_moe:
self.mlp = moe_block(
config=config,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
)
else:
self.mlp = mlp_block(
config=config,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
)
rms = functools.partial(
RMSNorm,
dim=self.config.hidden_size,
eps=self.config.rms_norm_eps,
dtype=dtype,
param_dtype=param_dtype,
)
self.input_layernorm = rms()
self.post_attention_layernorm = rms()
self.pre_feedforward_layernorm = rms()
self.post_feedforward_layernorm = rms()
def __call__(
self,
hidden_states: Float[Array, "batch seq_len hidden_dim"],
mask_info: MaskInfo,
position_ids: Int[Array, "batch seq_len"],
frequencies: tuple[chex.Array, chex.Array],
mode: common_types.RUNTIME_MODE_TYPES, # type:ignore
cache_view: TransformerCacheView | RaggedPagesCacheView | None = None,
cache_metadata: TransformerMetadata | RaggedPagesMetadata | None = None,
output_attentions: bool = False,
output_router_logits: bool = False,
):
"""
Forward pass of the module block.
Args:
hidden_states (chex.Array): Input hidden states.
attention_mask (chex.Array): Mask to apply on the attention scores.
Returns:
tp.Tuple[chex.Array, chex.Array]: A tuple containing the attention output and the attention weights.
"""
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
hidden_states = apply_logical_sharding(
hidden_states,
dynamic_axes=common_types.HiddenStateSharding,
partition_manager=self.config.partition_manager,
)
attn_outputs = self.self_attn(
hidden_states,
mask_info,
position_ids,
mode,
cache_view,
cache_metadata,
output_attentions,
frequencies,
None,
)
hidden_states = self.post_attention_layernorm(attn_outputs.attention_output)
hidden_states = residual + hidden_states
residual = hidden_states
hidden_states = self.pre_feedforward_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
router_logits = None
if self.is_moe:
hidden_states, router_logits = hidden_states
hidden_states = self.post_feedforward_layernorm(hidden_states)
hidden_states = residual + hidden_states
hidden_states = apply_logical_sharding(
hidden_states,
dynamic_axes=common_types.HiddenStateSharding,
partition_manager=self.config.partition_manager,
)
return DecoderLayerOutput(
hidden_states=hidden_states,
attention_weight=attn_outputs.attention_weight,
cache_view=attn_outputs.cache_view,
router_logits=router_logits if output_router_logits else None,
)
[docs]@register_module(TaskType.BASE_MODULE, config=Xerxes2Config, model_type="xerxes2")
class Xerxes2Model(EasyDeLBaseModule):
"""Xerxes2 decoder-only stack connecting embeddings, decoder layers, and final norm."""
def __init__(
self,
config: Xerxes2Config,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: str | jax.lax.Precision | None = None,
*,
rngs: nn.Rngs,
):
super().__init__(
config=config,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
)
embed_block = auto_remat(
nn.Embed,
policy=config.gradient_checkpointing,
save_names=config.gradient_checkpointing_targets,
exclude_names=config.gradient_checkpointing_targets,
)
self.embed_tokens = embed_block(
config.vocab_size,
config.hidden_size,
embedding_init=jax.nn.initializers.normal(stddev=config.initializer_range),
dtype=dtype,
param_dtype=param_dtype,
rngs=rngs,
)
self.layers = [
Xerxes2DecoderLayer(
config=config,
layer_idx=layer_idx,
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
)
for layer_idx in range(config.num_hidden_layers)
]
self.norm = RMSNorm(
dim=config.hidden_size,
eps=config.rms_norm_eps,
dtype=dtype,
param_dtype=param_dtype,
)
@functools.cached_property
def frequencies(self) -> jnp.ndarray:
"""Returns frequency values from the config."""
return self.config.get_basic_frequencies(self.config.qk_rope_head_dim)
def __call__(
self,
input_ids: Int[Array, "batch seq_len"] | None = None,
inputs_embeds: Float[Array, "batch seq_len hidden_dim"] | None = None,
attention_mask: Bool[Array, "batch seq_len"] | None = None,
mask_info: MaskInfo | None = None,
position_ids: Int[Array, "batch seq_len"] | None = None,
mode: common_types.RUNTIME_MODE_TYPES | None = None, # type:ignore
past_key_values: TransformerCache | RaggedPagesCache | None = None,
cache_metadata: TransformerMetadata | RaggedPagesMetadata | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
output_router_logits: bool | None = None,
) -> BaseModelOutput:
if (input_ids is None) ^ (inputs_embeds is not None):
raise ValueError(
"You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
)
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(inputs=input_ids.astype("i4"))
sequence_length = inputs_embeds.shape[1]
if output_router_logits is None:
output_router_logits = self.config.output_router_logits
all_attentions = () if output_attentions else None
all_hidden_states = () if output_hidden_states else None
all_router_logits = () if output_router_logits else None
assert sequence_length <= self.config.max_position_embeddings, (
f"Maximum Position Embedding Reached ! "
f"(Excepted <= {self.config.max_position_embeddings} got {sequence_length})"
)
mask_info = MaskInfo.dynamic_init(
mask_info=mask_info,
input_ids=input_ids,
inputs_embeds=inputs_embeds,
attention_mask=attention_mask,
)
if position_ids is None:
position_ids = mask_info.q_position_ids
hidden_states = inputs_embeds
if mode is None:
mode = (
common_types.MODE_DECODE
if sequence_length == 1 and past_key_values is not None
else common_types.MODE_TRAIN
)
if past_key_values is None:
past_key_values = TransformerCache.init_empty(len(self.layers))
hidden_states = apply_logical_sharding(
hidden_states,
dynamic_axes=common_types.HiddenStateSharding,
partition_manager=self.config.partition_manager,
)
for idx, block in enumerate(self.layers):
if output_hidden_states:
all_hidden_states += (hidden_states,)
layer_outputs = block(
hidden_states=hidden_states,
mask_info=mask_info,
position_ids=position_ids,
mode=mode,
cache_view=past_key_values.views[idx],
cache_metadata=cache_metadata,
output_attentions=output_attentions,
output_router_logits=output_router_logits,
frequencies=self.frequencies,
)
hidden_states = layer_outputs.hidden_states
if output_attentions:
all_attentions += (layer_outputs.attention_weight,)
if output_router_logits:
all_router_logits += (layer_outputs.router_logits,)
past_key_values[idx] = layer_outputs.cache_view
hidden_states = self.norm(hidden_states)
if output_hidden_states:
all_hidden_states += (hidden_states,)
return MoeModelOutput(
last_hidden_state=hidden_states,
hidden_states=all_hidden_states,
attentions=all_attentions,
past_key_values=past_key_values,
router_logits=all_router_logits,
)
[docs] def get_encoder(self):
"""
Returns the encoder part of the model's graph definition.
Decoder-Only models don't have an encoder.
"""
raise NotImplementedError("This is a decoder-only model and does not have an encoder.")
[docs] def get_decoder(self):
"""
Returns the decoder part of the model's graph definition.
"""
return self
[docs] def get_lm_head(self):
"""
Returns the language model head of the module.
Base Models don't have a Language Model Head.
"""
raise NotImplementedError("The base model does not have a language model head.")
[docs] def get_embedding(self):
"""
Returns the embedding layer of the module.
"""
return self.embed_tokens
[docs]class Xerxes2ForCausalLM(BaseCausalLMModule[Xerxes2Model, Xerxes2Config]):
"""
Xerxes2 model with a language modeling head for causal language modeling tasks.
This model extends the base Xerxes2Model by adding a linear language modeling head
on top of the transformer model. It incorporates Mixture of Experts (MoE) architecture
and is designed for generative tasks and text generation.
"""
_task_type = TaskType.CAUSAL_LM
_model_type = "xerxes2"
_config_class = Xerxes2Config
def __init__(
self,
config: Xerxes2Config,
dtype: jnp.dtype = jnp.bfloat16,
param_dtype: jnp.dtype = jnp.bfloat16,
precision: str | jax.lax.Precision | None = None,
*,
rngs: nn.Rngs,
):
"""Initialize the Xerxes2ForCausalLM model.
Args:
config (Xerxes2Config): The model configuration.
dtype (jnp.dtype, optional): The data type for computation. Defaults to jnp.bfloat16.
param_dtype (jnp.dtype, optional): The data type for parameters. Defaults to jnp.bfloat16.
precision (jax.lax.PrecisionLike, optional): The precision to use for matrix multiplication.
Defaults to None.
rngs (nn.Rngs): The random number generators.
"""
super().__init__(
config=config,
base_model_class=Xerxes2Model,
base_model_name="model",
dtype=dtype,
param_dtype=param_dtype,
precision=precision,
rngs=rngs,
lm_head_bias=False,
router_aux_loss_coef=getattr(config, "router_aux_loss_coef", None),
)
def __call__(
self,
input_ids: Int[Array, "batch seq_len"] | None = None,
inputs_embeds: Float[Array, "batch seq_len hidden_dim"] | None = None,
attention_mask: Bool[Array, "batch seq_len"] | None = None,
mask_info: MaskInfo | None = None,
position_ids: Int[Array, "batch seq_len"] | None = None,
mode: common_types.RUNTIME_MODE_TYPES | None = None, # type:ignore
past_key_values: TransformerCache | RaggedPagesCache | None = None,
cache_metadata: TransformerMetadata | RaggedPagesMetadata | None = None,
apply_lm_head: bool = True,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
output_router_logits: bool | None = None,
) -> MoeCausalLMOutput:
"""
Forward pass of the causal language model.
Args:
input_ids (Optional[chex.Array], optional): Token IDs to process. Defaults to None.
inputs_embeds (Optional[chex.Array], optional): Pre-computed input embeddings. Defaults to None.
attention_mask (Optional[chex.Array], optional): Mask to avoid attention on padding tokens. Defaults to None.
position_ids (Optional[chex.Array], optional): Position IDs. Defaults to None.
mode (Optional[common_types.RUNTIME_MODE_TYPES], optional): Runtime mode. Defaults to None.
past_key_values (Optional[TransformerCache | RaggedPagesCache], optional): Cached key/values.
Defaults to None.
cache_metadata (Optional[TransformerMetadata | RaggedPagesMetadata], optional): Cache metadata.
Defaults to None.
apply_lm_head (bool, optional): Whether to apply the LM head. Defaults to True.
output_attentions (Optional[bool], optional): Whether to output attention weights. Defaults to None.
output_hidden_states (Optional[bool], optional): Whether to output hidden states. Defaults to None.
output_router_logits (Optional[bool], optional): Whether to output router logits. Defaults to None.
Returns:
MoeCausalLMOutput: The model outputs with router logits and aux loss.
"""
return self.forward_moe(
input_ids=input_ids,
inputs_embeds=inputs_embeds,
attention_mask=attention_mask,
mask_info=mask_info,
position_ids=position_ids,
mode=mode,
past_key_values=past_key_values,
cache_metadata=cache_metadata,
apply_lm_head=apply_lm_head,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
output_router_logits=output_router_logits,
aux_loss_fn=self._compute_aux_loss,
)
def _compute_aux_loss(self, outputs, attention_mask):
"""Compute auxiliary loss for load balancing."""
if outputs.router_logits is None or len(outputs.router_logits) == 0:
return None
aux_loss = auxiliary_load_balancing_loss_func(
gate_logits=outputs.router_logits,
num_experts=self.config.num_experts,
top_k=self.config.num_experts_per_tok,
attention_mask=attention_mask,
)
return aux_loss + (aux_loss * self.config.router_aux_loss_coef)
[docs] def init_cache(
self,
batch_size: int,
max_length: int,
starts: int | None = None,
shardings: dict | None = None,
pad_token_id: int | None = None,
):
shardings = shardings or dict()
return TransformerCache.init_cache(
dtype=self.config.kvdtype,
partition_manager=self.config.partition_manager,
metadata=self.create_cache_metadata(
batch_size=batch_size,
max_length=max_length,
pad_token_id=pad_token_id,
),
quantizer=self._quant_class(
quantization_config=self.config.kv_cache_quantization_config,
),
mesh=self.config.mesh,
starts=starts,
mask_type_details=self.config.get_mask_details(),
)