Skip to content
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

Update modular_modernbert.py to support input_embeddings #35422

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/transformers/models/modernbert/modular_modernbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def eager_attention_forward(
dim: int,
output_attentions: Optional[bool] = False,
**_kwargs,
) -> Tuple[torch.Tensor, torch.Tensor] | Tuple[torch.Tensor]:
) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]:
# qkv: [batch_size, seqlen, 3, nheads, headdim]
cos, sin = module.rotary_emb(qkv, position_ids=position_ids)
query, key, value = qkv.transpose(3, 1).unbind(dim=2)
Expand Down Expand Up @@ -1278,6 +1278,12 @@ def __init__(self, config: ModernBertConfig):
# Initialize weights and apply final processing
self.post_init()

def get_input_embeddings(self):
return self.embeddings.tok_embeddings

def set_input_embeddings(self, value):
self.embeddings.tok_embeddings = value

@add_start_docstrings_to_model_forward(MODERNBERT_INPUTS_DOCSTRING)
@add_code_sample_docstrings(
checkpoint=_CHECKPOINT_FOR_DOC,
Expand Down Expand Up @@ -1389,6 +1395,12 @@ def __init__(self, config: ModernBertConfig):
# Initialize weights and apply final processing
self.post_init()

def get_input_embeddings(self):
return self.embeddings.tok_embeddings

def set_input_embeddings(self, value):
self.embeddings.tok_embeddings = value

@add_start_docstrings_to_model_forward(MODERNBERT_INPUTS_DOCSTRING)
@add_code_sample_docstrings(
checkpoint=_CHECKPOINT_FOR_DOC,
Expand Down