Instructions to use Taykhoom/RiNALMo-mega with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Taykhoom/RiNALMo-mega with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="Taykhoom/RiNALMo-mega", trust_remote_code=True)# Load model directly from transformers import AutoModelForMaskedLM model = AutoModelForMaskedLM.from_pretrained("Taykhoom/RiNALMo-mega", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Fix model correctness and Hugging Face compatibility
Browse files- README.md +1 -2
- modeling_rinalmo.py +74 -18
README.md
CHANGED
|
@@ -152,8 +152,7 @@ even at inference, consistent with the original training code.
|
|
| 152 |
## Credits
|
| 153 |
|
| 154 |
Original model and code by Penić et al. Source: [GitHub lbcb-sci/RiNALMo](https://github.com/lbcb-sci/RiNALMo).
|
| 155 |
-
|
| 156 |
-
and reviewed manually by Taykhoom Dalal.
|
| 157 |
|
| 158 |
## License
|
| 159 |
|
|
|
|
| 152 |
## Credits
|
| 153 |
|
| 154 |
Original model and code by Penić et al. Source: [GitHub lbcb-sci/RiNALMo](https://github.com/lbcb-sci/RiNALMo).
|
| 155 |
+
Hugging Face port maintained by Taykhoom Dalal.
|
|
|
|
| 156 |
|
| 157 |
## License
|
| 158 |
|
modeling_rinalmo.py
CHANGED
|
@@ -18,25 +18,51 @@ def _rotate_half(x):
|
|
| 18 |
|
| 19 |
|
| 20 |
def _apply_rotary_pos_emb(q, k, cos, sin):
|
| 21 |
-
cos = cos.to(q.dtype)
|
| 22 |
-
sin = sin.to(q.dtype)
|
| 23 |
return (q * cos) + (_rotate_half(q) * sin), (k * cos) + (_rotate_half(k) * sin)
|
| 24 |
|
| 25 |
|
| 26 |
class RotaryPositionEmbedding(nn.Module):
|
| 27 |
def __init__(self, dim: int, base: int = 10000):
|
| 28 |
super().__init__()
|
|
|
|
|
|
|
| 29 |
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
|
| 30 |
self.register_buffer("inv_freq", inv_freq)
|
| 31 |
self._seq_len_cached = None
|
|
|
|
| 32 |
self._cos_cached = None
|
| 33 |
self._sin_cached = None
|
| 34 |
|
| 35 |
def _update_cache(self, seq_len: int, device, dtype):
|
| 36 |
-
if
|
|
|
|
|
|
|
|
|
|
| 37 |
self._seq_len_cached = seq_len
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
emb = torch.cat((freqs, freqs), dim=-1)
|
| 41 |
self._cos_cached = emb.cos()[None, None, :, :]
|
| 42 |
self._sin_cached = emb.sin()[None, None, :, :]
|
|
@@ -81,11 +107,11 @@ class RiNALMoAttention(nn.Module):
|
|
| 81 |
if key_padding_mask is not None:
|
| 82 |
attn = attn.masked_fill(key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf"))
|
| 83 |
|
| 84 |
-
attn = attn.softmax(dim=-1)
|
| 85 |
attn_weights = attn if output_attentions else None
|
| 86 |
attn = self.attn_dropout(attn)
|
| 87 |
|
| 88 |
-
out = torch.matmul(attn, v)
|
| 89 |
out = out.transpose(1, 2).contiguous().view(B, T, self.embed_dim)
|
| 90 |
out = self.out_proj(out)
|
| 91 |
return out, attn_weights
|
|
@@ -113,7 +139,13 @@ class RiNALMoSdpaAttention(RiNALMoAttention):
|
|
| 113 |
attn_mask = torch.zeros(B, 1, 1, T, dtype=q.dtype, device=q.device)
|
| 114 |
attn_mask = attn_mask.masked_fill(key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf"))
|
| 115 |
|
| 116 |
-
out = F.scaled_dot_product_attention(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
out = out.transpose(1, 2).contiguous().view(B, T, self.embed_dim)
|
| 118 |
out = self.out_proj(out)
|
| 119 |
return out, None
|
|
@@ -149,11 +181,11 @@ class RiNALMoFlashAttention2(RiNALMoAttention):
|
|
| 149 |
q = q_t.transpose(1, 2)
|
| 150 |
k = k_t.transpose(1, 2)
|
| 151 |
|
| 152 |
-
orig_dtype = q.dtype
|
| 153 |
if q.dtype not in (torch.float16, torch.bfloat16):
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
if key_padding_mask is not None and key_padding_mask.any():
|
| 159 |
attend_mask = ~key_padding_mask
|
|
@@ -165,14 +197,20 @@ class RiNALMoFlashAttention2(RiNALMoAttention):
|
|
| 165 |
q_unpad, k_unpad, v_unpad,
|
| 166 |
cu_seqlens_q=cu_seqlens, cu_seqlens_k=cu_seqlens,
|
| 167 |
max_seqlen_q=max_seqlen, max_seqlen_k=max_seqlen,
|
|
|
|
| 168 |
causal=False,
|
| 169 |
)
|
| 170 |
out = pad_input(out_unpad.view(-1, self.embed_dim), indices, B, T)
|
| 171 |
else:
|
| 172 |
-
out = flash_attn_func(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
out = out.view(B, T, self.embed_dim)
|
| 174 |
|
| 175 |
-
out = out.to(orig_dtype)
|
| 176 |
out = self.out_proj(out)
|
| 177 |
return out, None
|
| 178 |
|
|
@@ -312,6 +350,14 @@ class RiNALMoModel(RiNALMoPreTrainedModel):
|
|
| 312 |
|
| 313 |
x = self.final_layer_norm(x)
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
return BaseModelOutput(
|
| 316 |
last_hidden_state=x,
|
| 317 |
hidden_states=tuple(all_hidden_states) if output_hidden_states else None,
|
|
@@ -338,15 +384,25 @@ class RiNALMoForMaskedLM(RiNALMoPreTrainedModel):
|
|
| 338 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 339 |
out = self.model(input_ids, attention_mask=attention_mask,
|
| 340 |
output_hidden_states=output_hidden_states,
|
| 341 |
-
output_attentions=output_attentions, return_dict=
|
| 342 |
logits = self.lm_head(out.last_hidden_state)
|
| 343 |
loss = None
|
| 344 |
if labels is not None:
|
| 345 |
loss = F.cross_entropy(logits.view(-1, self.config.vocab_size),
|
| 346 |
labels.view(-1), ignore_index=-100)
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
|
| 352 |
class RiNALMoLMHead(nn.Module):
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def _apply_rotary_pos_emb(q, k, cos, sin):
|
| 21 |
+
cos = cos.to(device=q.device, dtype=q.dtype)
|
| 22 |
+
sin = sin.to(device=q.device, dtype=q.dtype)
|
| 23 |
return (q * cos) + (_rotate_half(q) * sin), (k * cos) + (_rotate_half(k) * sin)
|
| 24 |
|
| 25 |
|
| 26 |
class RotaryPositionEmbedding(nn.Module):
|
| 27 |
def __init__(self, dim: int, base: int = 10000):
|
| 28 |
super().__init__()
|
| 29 |
+
self.dim = dim
|
| 30 |
+
self.base = base
|
| 31 |
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
|
| 32 |
self.register_buffer("inv_freq", inv_freq)
|
| 33 |
self._seq_len_cached = None
|
| 34 |
+
self._device_cached = None
|
| 35 |
self._cos_cached = None
|
| 36 |
self._sin_cached = None
|
| 37 |
|
| 38 |
def _update_cache(self, seq_len: int, device, dtype):
|
| 39 |
+
if (
|
| 40 |
+
seq_len != self._seq_len_cached
|
| 41 |
+
or device != self._device_cached
|
| 42 |
+
):
|
| 43 |
self._seq_len_cached = seq_len
|
| 44 |
+
self._device_cached = device
|
| 45 |
+
if (
|
| 46 |
+
self.inv_freq.device == device
|
| 47 |
+
and self.inv_freq.dtype == torch.float32
|
| 48 |
+
):
|
| 49 |
+
inv_freq = self.inv_freq
|
| 50 |
+
else:
|
| 51 |
+
inv_freq = 1.0 / (
|
| 52 |
+
self.base
|
| 53 |
+
** (
|
| 54 |
+
torch.arange(
|
| 55 |
+
0,
|
| 56 |
+
self.dim,
|
| 57 |
+
2,
|
| 58 |
+
dtype=torch.float32,
|
| 59 |
+
device=device,
|
| 60 |
+
)
|
| 61 |
+
/ self.dim
|
| 62 |
+
)
|
| 63 |
+
)
|
| 64 |
+
t = torch.arange(seq_len, device=device, dtype=torch.float32)
|
| 65 |
+
freqs = torch.einsum("i,j->ij", t, inv_freq)
|
| 66 |
emb = torch.cat((freqs, freqs), dim=-1)
|
| 67 |
self._cos_cached = emb.cos()[None, None, :, :]
|
| 68 |
self._sin_cached = emb.sin()[None, None, :, :]
|
|
|
|
| 107 |
if key_padding_mask is not None:
|
| 108 |
attn = attn.masked_fill(key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf"))
|
| 109 |
|
| 110 |
+
attn = torch.nan_to_num(attn.float().softmax(dim=-1), nan=0.0)
|
| 111 |
attn_weights = attn if output_attentions else None
|
| 112 |
attn = self.attn_dropout(attn)
|
| 113 |
|
| 114 |
+
out = torch.matmul(attn.to(v.dtype), v)
|
| 115 |
out = out.transpose(1, 2).contiguous().view(B, T, self.embed_dim)
|
| 116 |
out = self.out_proj(out)
|
| 117 |
return out, attn_weights
|
|
|
|
| 139 |
attn_mask = torch.zeros(B, 1, 1, T, dtype=q.dtype, device=q.device)
|
| 140 |
attn_mask = attn_mask.masked_fill(key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf"))
|
| 141 |
|
| 142 |
+
out = F.scaled_dot_product_attention(
|
| 143 |
+
q,
|
| 144 |
+
k,
|
| 145 |
+
v,
|
| 146 |
+
attn_mask=attn_mask,
|
| 147 |
+
dropout_p=self.attn_dropout.p if self.training else 0.0,
|
| 148 |
+
)
|
| 149 |
out = out.transpose(1, 2).contiguous().view(B, T, self.embed_dim)
|
| 150 |
out = self.out_proj(out)
|
| 151 |
return out, None
|
|
|
|
| 181 |
q = q_t.transpose(1, 2)
|
| 182 |
k = k_t.transpose(1, 2)
|
| 183 |
|
|
|
|
| 184 |
if q.dtype not in (torch.float16, torch.bfloat16):
|
| 185 |
+
raise ValueError(
|
| 186 |
+
"flash_attention_2 requires float16 or bfloat16 weights. "
|
| 187 |
+
f"Received {q.dtype}."
|
| 188 |
+
)
|
| 189 |
|
| 190 |
if key_padding_mask is not None and key_padding_mask.any():
|
| 191 |
attend_mask = ~key_padding_mask
|
|
|
|
| 197 |
q_unpad, k_unpad, v_unpad,
|
| 198 |
cu_seqlens_q=cu_seqlens, cu_seqlens_k=cu_seqlens,
|
| 199 |
max_seqlen_q=max_seqlen, max_seqlen_k=max_seqlen,
|
| 200 |
+
dropout_p=self.attn_dropout.p if self.training else 0.0,
|
| 201 |
causal=False,
|
| 202 |
)
|
| 203 |
out = pad_input(out_unpad.view(-1, self.embed_dim), indices, B, T)
|
| 204 |
else:
|
| 205 |
+
out = flash_attn_func(
|
| 206 |
+
q,
|
| 207 |
+
k,
|
| 208 |
+
v,
|
| 209 |
+
dropout_p=self.attn_dropout.p if self.training else 0.0,
|
| 210 |
+
causal=False,
|
| 211 |
+
)
|
| 212 |
out = out.view(B, T, self.embed_dim)
|
| 213 |
|
|
|
|
| 214 |
out = self.out_proj(out)
|
| 215 |
return out, None
|
| 216 |
|
|
|
|
| 350 |
|
| 351 |
x = self.final_layer_norm(x)
|
| 352 |
|
| 353 |
+
if not return_dict:
|
| 354 |
+
output = (x,)
|
| 355 |
+
if output_hidden_states:
|
| 356 |
+
output += (tuple(all_hidden_states),)
|
| 357 |
+
if output_attentions:
|
| 358 |
+
output += (tuple(all_attentions),)
|
| 359 |
+
return output
|
| 360 |
+
|
| 361 |
return BaseModelOutput(
|
| 362 |
last_hidden_state=x,
|
| 363 |
hidden_states=tuple(all_hidden_states) if output_hidden_states else None,
|
|
|
|
| 384 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 385 |
out = self.model(input_ids, attention_mask=attention_mask,
|
| 386 |
output_hidden_states=output_hidden_states,
|
| 387 |
+
output_attentions=output_attentions, return_dict=True)
|
| 388 |
logits = self.lm_head(out.last_hidden_state)
|
| 389 |
loss = None
|
| 390 |
if labels is not None:
|
| 391 |
loss = F.cross_entropy(logits.view(-1, self.config.vocab_size),
|
| 392 |
labels.view(-1), ignore_index=-100)
|
| 393 |
+
if not return_dict:
|
| 394 |
+
output = (logits,)
|
| 395 |
+
if output_hidden_states:
|
| 396 |
+
output += (out.hidden_states,)
|
| 397 |
+
if output_attentions:
|
| 398 |
+
output += (out.attentions,)
|
| 399 |
+
return (loss,) + output if loss is not None else output
|
| 400 |
+
return MaskedLMOutput(
|
| 401 |
+
loss=loss,
|
| 402 |
+
logits=logits,
|
| 403 |
+
hidden_states=out.hidden_states,
|
| 404 |
+
attentions=out.attentions,
|
| 405 |
+
)
|
| 406 |
|
| 407 |
|
| 408 |
class RiNALMoLMHead(nn.Module):
|