MCPcopy Create free account
hub / github.com/AnswerDotAI/ModernBERT / pad_input

Function pad_input

src/bert_padding.py:128–141  ·  view source on GitHub ↗

Add padding to sequences. Arguments: hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask. indices: (total_nnz) batch: int batch_size seqlen: int max sequence length Returns: hidden_states: (batch, seqlen,

(hidden_states: torch.Tensor, indices: torch.Tensor, batch: int, seqlen: int)

Source from the content-addressed store, hash-verified

126
127
128def pad_input(hidden_states: torch.Tensor, indices: torch.Tensor, batch: int, seqlen: int) -> torch.Tensor:
129 """Add padding to sequences.
130
131 Arguments:
132 hidden_states: (total_nnz, ...), where total_nnz = number of tokens in selected in attention_mask.
133 indices: (total_nnz)
134 batch: int batch_size
135 seqlen: int max sequence length
136
137 Returns:
138 hidden_states: (batch, seqlen, ...)
139 """
140 output = index_put_first_axis(hidden_states, indices, batch * seqlen)
141 return rearrange(output, "(b s) ... -> b s ...", b=batch) # type: ignore

Callers 5

test_pad_inputFunction · 0.90
test_roundtripFunction · 0.90
test_token_inputFunction · 0.90
test_2d_inputFunction · 0.90
test_rotary_emb_unpadFunction · 0.90

Calls

no outgoing calls

Tested by 5

test_pad_inputFunction · 0.72
test_roundtripFunction · 0.72
test_token_inputFunction · 0.72
test_2d_inputFunction · 0.72
test_rotary_emb_unpadFunction · 0.72