# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # This file was automatically generated from src/transformers/models/esmc/modular_esmc.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_esmc.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # Copyright 2026 BioHub 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. import math from collections.abc import Callable import torch import torch.nn as nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from torch.nn import functional as F from ...activations import ACT2FN from ...integrations import use_kernel_forward_from_hub, use_kernelized_func from ...masking_utils import create_bidirectional_mask, packed_sequence_mask_function from ...modeling_layers import GenericForTokenClassification, GradientCheckpointingLayer from ...modeling_outputs import BaseModelOutput, MaskedLMOutput, SequenceClassifierOutput from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack from ...utils import TransformersKwargs, auto_docstring, can_return_tuple from ...utils.deprecation import deprecate_kwarg from ...utils.generic import is_flash_attention_requested, maybe_autocast, merge_with_config_defaults from ...utils.output_capturing import capture_outputs from .configuration_esmc import EsmcConfig class EsmcLayerNorm(nn.LayerNorm): """LayerNorm that returns its input dtype. The reference fuses each LayerNorm into the projection that follows it, keeping the fp32 reduction inside the fused op; a bare ``nn.LayerNorm`` under autocast instead hands back fp32 and promotes the residual stream, leaving the rotary ``cos``/``sin`` as the only bf16 tensors in the block. """ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: return super().forward(hidden_states).to(hidden_states.dtype) class EsmcRotaryEmbedding(nn.Module): @deprecate_kwarg("device", version="5.18") def __init__(self, config: EsmcConfig, device=None): super().__init__() self.max_seq_len_cached = config.max_position_embeddings self.original_max_seq_len = config.max_position_embeddings self.config = config self.rope_type = self.config.rope_parameters["rope_type"] rope_init_fn: Callable = self.compute_default_rope_parameters if self.rope_type != "default": rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type] inv_freq, self.attention_scaling = rope_init_fn(self.config, device) self.inv_freq = nn.Buffer(inv_freq, persistent=False) self.original_inv_freq = nn.Buffer(inv_freq.clone(), persistent=False) @staticmethod @deprecate_kwarg("device", version="5.18") def compute_default_rope_parameters(config: EsmcConfig, device=None, **kwargs) -> tuple[torch.Tensor, float]: """ Computes the inverse frequencies according to the original RoPE implementation Args: config ([`~transformers.PreTrainedConfig`]): The model configuration. Returns: Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE). """ base = config.rope_parameters["rope_theta"] dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads attention_factor = 1.0 # Unused in this type of RoPE # Compute the inverse frequencies inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float) / dim)) return inv_freq.to(device), attention_factor @torch.no_grad() @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope) def forward(self, x, position_ids): inv_freq_expanded = ( self.inv_freq[None, :, None].expand(position_ids.shape[0], -1, 1).to(dtype=torch.float, device=x.device) ) position_ids_expanded = position_ids[:, None, :].float() device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu" # Disable any outside autocast context if any, to really force fp32 with maybe_autocast(device_type=device_type, enabled=False): freqs = (inv_freq_expanded @ position_ids_expanded).transpose(1, 2) emb = torch.cat((freqs, freqs), dim=-1) cos = emb.cos() * self.attention_scaling sin = emb.sin() * self.attention_scaling return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) class EsmcMLP(nn.Module): def __init__(self, config): super().__init__() self.config = config self.hidden_size = config.hidden_size self.intermediate_size = config.intermediate_size self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias) self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias) self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=config.mlp_bias) self.act_fn = ACT2FN[config.hidden_act] def forward(self, x): down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) return down_proj def rotate_half(x): """Rotates half the hidden dims of the input.""" x1 = x[..., : x.shape[-1] // 2] x2 = x[..., x.shape[-1] // 2 :] return torch.cat((-x2, x1), dim=-1) @use_kernel_forward_from_hub("rotary_pos_emb") def apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=1): """Applies Rotary Position Embedding to the query and key tensors. Args: q (`torch.Tensor`): The query tensor. k (`torch.Tensor`): The key tensor. cos (`torch.Tensor`): The cosine part of the rotary embedding. sin (`torch.Tensor`): The sine part of the rotary embedding. unsqueeze_dim (`int`, *optional*, defaults to 1): The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. Returns: `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. """ cos = cos.unsqueeze(unsqueeze_dim) sin = sin.unsqueeze(unsqueeze_dim) q_embed = (q * cos) + (rotate_half(q) * sin) k_embed = (k * cos) + (rotate_half(k) * sin) return q_embed, k_embed def eager_attention_forward( module: nn.Module, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attention_mask: torch.Tensor | None, scaling: float | None = None, dropout: float = 0.0, **kwargs: Unpack[TransformersKwargs], ): if scaling is None: scaling = query.size(-1) ** -0.5 # Take the dot product between "query" and "key" to get the raw attention scores. attn_weights = torch.matmul(query, key.transpose(2, 3)) * scaling if attention_mask is not None: attn_weights = attn_weights + attention_mask attn_weights = nn.functional.softmax(attn_weights, dim=-1) attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training) attn_output = torch.matmul(attn_weights, value) attn_output = attn_output.transpose(1, 2).contiguous() return attn_output, attn_weights @use_kernelized_func(apply_rotary_pos_emb) class EsmcAttention(nn.Module): """Multi-head self-attention with QK-LayerNorm and RoPE.""" def __init__(self, config: EsmcConfig, layer_idx: int | None = None): super().__init__() self.config = config self.layer_idx = layer_idx self.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads) self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads self.scaling = self.head_dim**-0.5 self.attention_dropout = config.attention_dropout self.is_causal = False self.q_proj = nn.Linear( config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias ) self.k_proj = nn.Linear( config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias ) self.v_proj = nn.Linear( config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias ) self.o_proj = nn.Linear( config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias ) # Every released ESMC checkpoint carries q_norm/k_norm weights, so there is no flag for these. self.q_norm = EsmcLayerNorm(config.hidden_size, bias=False) self.k_norm = EsmcLayerNorm(config.hidden_size, bias=False) def forward( self, hidden_states: torch.Tensor, attention_mask: torch.Tensor | None = None, position_embeddings: tuple[torch.Tensor, torch.Tensor] | None = None, **kwargs: Unpack[TransformersKwargs], ) -> tuple[torch.Tensor, torch.Tensor | None]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) query_states = self.q_norm(self.q_proj(hidden_states)).to(hidden_states.dtype) key_states = self.k_norm(self.k_proj(hidden_states)).to(hidden_states.dtype) value_states = self.v_proj(hidden_states) query_states = query_states.view(hidden_shape).transpose(1, 2) key_states = key_states.view(hidden_shape).transpose(1, 2) value_states = value_states.view(hidden_shape).transpose(1, 2) cos, sin = position_embeddings query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface( self.config._attn_implementation, eager_attention_forward ) attn_output, attn_weights = attention_interface( self, query_states, key_states, value_states, attention_mask, dropout=0.0 if not self.training else self.attention_dropout, scaling=self.scaling, **kwargs, ) attn_output = attn_output.reshape(*input_shape, -1).contiguous() return self.o_proj(attn_output), attn_weights class EsmcLayer(GradientCheckpointingLayer): """Single transformer block: pre-norm attention + pre-norm FFN with residual scaling.""" def __init__(self, config: EsmcConfig, layer_idx: int | None = None): super().__init__() self.hidden_size = config.hidden_size self.self_attn = EsmcAttention(config=config, layer_idx=layer_idx) self.mlp = EsmcMLP(config) # LayerNorm instead of Llama's RMSNorm, dtype-restoring (see EsmcLayerNorm). self.input_layernorm = EsmcLayerNorm(config.hidden_size) self.post_attention_layernorm = EsmcLayerNorm(config.hidden_size) # ESM3 residual scaling to stabilise deep networks; every released checkpoint uses it. self.scaling_factor = math.sqrt(config.num_hidden_layers / 36) def forward( self, hidden_states: torch.Tensor, attention_mask: torch.Tensor | None = None, position_embeddings: tuple[torch.Tensor, torch.Tensor] | None = None, **kwargs: Unpack[TransformersKwargs], ) -> torch.Tensor: residual = hidden_states attn_output, _ = self.self_attn( self.input_layernorm(hidden_states), attention_mask, position_embeddings=position_embeddings, **kwargs, ) # ESM3 residue scaling on each residual branch. hidden_states = residual + attn_output / self.scaling_factor residual = hidden_states hidden_states = residual + self.mlp(self.post_attention_layernorm(hidden_states)) / self.scaling_factor return hidden_states @auto_docstring class EsmcPreTrainedModel(PreTrainedModel): config_class = EsmcConfig base_model_prefix = "esmc" supports_gradient_checkpointing = True _supports_flash_attn = True _supports_sdpa = True _supports_flex_attn = True _supports_attention_backend = True _can_record_outputs = { "hidden_states": EsmcLayer, "attentions": EsmcAttention, } # Are kept as non-persistent buffers to avoid being saved in the state dict # and causing mismatch when loading from a checkpoint that doesn't have them _keys_to_ignore_on_load_unexpected = ["inv_freq", "original_inv_freq"] _no_split_modules = ["EsmcLayer"] @auto_docstring class EsmcModel(EsmcPreTrainedModel): def __init__(self, config: EsmcConfig): super().__init__(config) self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size) self.rotary_emb = EsmcRotaryEmbedding(config) self.layers = nn.ModuleList([EsmcLayer(config) for _ in range(config.num_hidden_layers)]) # trf-ignore: TRF034 self.norm = EsmcLayerNorm(config.hidden_size, bias=False) self.gradient_checkpointing = False self.post_init() @merge_with_config_defaults @capture_outputs @auto_docstring def forward( self, input_ids: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, position_ids: torch.Tensor | None = None, sequence_id: torch.Tensor | None = None, **kwargs: Unpack[TransformersKwargs], ) -> BaseModelOutput: r""" position_ids (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Position of each token in the sequence, used by RoPE. Defaults to ``arange(sequence_length)``. ESMC positions are absolute over the whole input, and keep running *across* chain boundaries in a multi-chain input. The chain structure is carried separately by ``sequence_id``, which masks attention: unlike the packed-sequence format used elsewhere in the library, these are therefore not per-chain position ids restarting at 0, and the chain structure is deliberately not inferred from them. sequence_id (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Integer chain-ID tensor for chain-aware attention masking. Tokens with the same non-negative integer value can attend to each other; tokens with different values cannot (cross-chain masking). Padding positions should be set to ``-1`` and inputs must be **right-padded** (RoPE uses absolute positions starting at 0). When provided, ``attention_mask`` is ignored. Passing ``sequence_id`` builds a custom attention mask, which requires ``torch>=2.6``. Multi-chain inputs additionally require a non-flash ``attn_implementation`` (``'sdpa'`` / ``'eager'`` / ``'flex_attention'``); flash attention only supports the single-chain case. Examples: ```python >>> from transformers import AutoTokenizer, EsmcModel >>> model = EsmcModel.from_pretrained("biohub/ESMC-300M-hf") >>> tokenizer = AutoTokenizer.from_pretrained("biohub/ESMC-300M-hf") >>> inputs = tokenizer(["MLKNVQVQLV"], return_tensors="pt") >>> outputs = model(**inputs) >>> outputs.last_hidden_state.shape torch.Size([1, 12, 960]) ``` """ hidden_states = self.embed_tokens(input_ids) if position_ids is None: position_ids = torch.arange(hidden_states.shape[1], device=hidden_states.device).unsqueeze(0) position_embeddings = self.rotary_emb(hidden_states, position_ids) if sequence_id is not None: if is_flash_attention_requested(self.config): raise ValueError( "`sequence_id` (chain-aware attention) is not supported with flash attention, " "which can't represent the block-diagonal chain mask. Re-load the model with " "attn_implementation='sdpa' (or 'eager' / 'flex_attention')." ) attention_mask = create_bidirectional_mask( config=self.config, inputs_embeds=hidden_states, attention_mask=None, and_mask_function=packed_sequence_mask_function(sequence_id), ) else: attention_mask = create_bidirectional_mask( config=self.config, inputs_embeds=hidden_states, attention_mask=attention_mask, ) for layer in self.layers: hidden_states = layer( hidden_states, attention_mask, position_embeddings=position_embeddings, **kwargs, ) output = self.norm(hidden_states) return BaseModelOutput(last_hidden_state=output) class EsmcMaskedLMHead(nn.Module): def __init__(self, hidden_size: int, vocab_size: int, intermediate_size: int | None = None) -> None: super().__init__() intermediate_size = intermediate_size if intermediate_size is not None else hidden_size self.dense = nn.Linear(hidden_size, intermediate_size) self.layer_norm = nn.LayerNorm(intermediate_size) self.decoder = nn.Linear(intermediate_size, vocab_size) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.dense(hidden_states) hidden_states = F.gelu(hidden_states) hidden_states = self.layer_norm(hidden_states) return self.decoder(hidden_states) @auto_docstring class EsmcForMaskedLM(EsmcPreTrainedModel): def __init__(self, config: EsmcConfig): super().__init__(config) self.esmc = EsmcModel(config) self.lm_head = EsmcMaskedLMHead(config.hidden_size, config.vocab_size) self.post_init() # ``lm_head`` is a dense/norm/decoder stack rather than the output projection itself, so the # base-class accessors (which return ``lm_head``) would hand ``resize_token_embeddings`` a module # with no ``weight``. Same reason ``EsmForMaskedLM`` overrides these. def get_output_embeddings(self) -> nn.Linear: return self.lm_head.decoder def set_output_embeddings(self, new_embeddings: nn.Linear): self.lm_head.decoder = new_embeddings @can_return_tuple @auto_docstring def forward( self, input_ids: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, position_ids: torch.Tensor | None = None, sequence_id: torch.Tensor | None = None, labels: torch.Tensor | None = None, **kwargs: Unpack[TransformersKwargs], ) -> tuple[torch.Tensor, ...] | MaskedLMOutput: r""" sequence_id (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Integer chain-ID tensor forwarded to the encoder for chain-aware attention masking. See :meth:`EsmcModel.forward` for the encoding. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for masked language modelling loss. Positions with label ``-100`` are ignored. Other positions must be in ``[0, config.vocab_size)``. Examples: ```python >>> from transformers import AutoTokenizer, EsmcForMaskedLM >>> import torch >>> model = EsmcForMaskedLM.from_pretrained("biohub/ESMC-300M-hf") >>> tokenizer = AutoTokenizer.from_pretrained("biohub/ESMC-300M-hf") >>> inputs = tokenizer(["MLKNVQLV"], return_tensors="pt") >>> outputs = model(**inputs) >>> outputs.logits.shape torch.Size([1, 11, 64]) ``` """ encoder_outputs = self.esmc( input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, sequence_id=sequence_id, return_dict=True, **kwargs, ) logits = self.lm_head(encoder_outputs.last_hidden_state) loss: torch.Tensor | None = None if labels is not None: loss = self.loss_function(logits, labels, vocab_size=self.config.vocab_size, **kwargs) return MaskedLMOutput( loss=loss, logits=logits, hidden_states=encoder_outputs.hidden_states, attentions=encoder_outputs.attentions, ) class EsmcClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__(self, config: EsmcConfig): super().__init__() self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.dropout = nn.Dropout(config.classifier_dropout) self.out_proj = nn.Linear(config.hidden_size, config.num_labels) def forward(self, features, **kwargs): x = features[:, 0, :] # take token (equiv. to [CLS]) x = self.dropout(x) x = self.dense(x) x = torch.tanh(x) x = self.dropout(x) x = self.out_proj(x) return x @auto_docstring( custom_intro=""" ESMC Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. """ ) class EsmcForSequenceClassification(EsmcPreTrainedModel): def __init__(self, config: EsmcConfig): super().__init__(config) self.num_labels = config.num_labels self.config = config self.esmc = EsmcModel(config) self.classifier = EsmcClassificationHead(config) self.post_init() @can_return_tuple @auto_docstring def forward( self, input_ids: torch.LongTensor | None = None, attention_mask: torch.Tensor | None = None, position_ids: torch.LongTensor | None = None, inputs_embeds: torch.FloatTensor | None = None, labels: torch.LongTensor | None = None, **kwargs: Unpack[TransformersKwargs], ) -> tuple | SequenceClassifierOutput: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If `config.num_labels > 1` a classification loss is computed (Cross-Entropy). """ outputs = self.esmc( input_ids, attention_mask=attention_mask, position_ids=position_ids, inputs_embeds=inputs_embeds, **kwargs, ) sequence_output = outputs[0] logits = self.classifier(sequence_output) loss = None if labels is not None: labels = labels.to(logits.device) if self.config.problem_type is None: if self.num_labels == 1: self.config.problem_type = "regression" elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): self.config.problem_type = "single_label_classification" else: self.config.problem_type = "multi_label_classification" if self.config.problem_type == "regression": loss_fct = MSELoss() if self.num_labels == 1: loss = loss_fct(logits.squeeze(), labels.squeeze()) else: loss = loss_fct(logits, labels) elif self.config.problem_type == "single_label_classification": loss_fct = CrossEntropyLoss() loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1)) elif self.config.problem_type == "multi_label_classification": loss_fct = BCEWithLogitsLoss() loss = loss_fct(logits, labels) return SequenceClassifierOutput( loss=loss, logits=logits, hidden_states=outputs.hidden_states, attentions=outputs.attentions, ) class EsmcForTokenClassification(GenericForTokenClassification, EsmcPreTrainedModel): pass __all__ = [ "EsmcModel", "EsmcForMaskedLM", "EsmcForSequenceClassification", "EsmcForTokenClassification", "EsmcPreTrainedModel", ]