Update modeling_fastesm.py
Browse files- modeling_fastesm.py +553 -528
modeling_fastesm.py
CHANGED
|
@@ -1,528 +1,553 @@
|
|
| 1 |
-
|
| 2 |
-
import torch
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
from transformers.modeling_outputs import (
|
| 8 |
-
MaskedLMOutput,
|
| 9 |
-
BaseModelOutputWithPastAndCrossAttentions,
|
| 10 |
-
BaseModelOutputWithPoolingAndCrossAttentions,
|
| 11 |
-
SequenceClassifierOutput,
|
| 12 |
-
TokenClassifierOutput
|
| 13 |
-
)
|
| 14 |
-
from transformers.models.esm.modeling_esm import (
|
| 15 |
-
RotaryEmbedding,
|
| 16 |
-
EsmContactPredictionHead,
|
| 17 |
-
EsmIntermediate,
|
| 18 |
-
EsmOutput,
|
| 19 |
-
EsmPooler,
|
| 20 |
-
EsmLMHead,
|
| 21 |
-
EsmSelfOutput,
|
| 22 |
-
EsmClassificationHead,
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
self.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
self.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
f"
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
self.
|
| 107 |
-
self.
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
self.
|
| 111 |
-
self.
|
| 112 |
-
self.
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
self.
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
self.
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
self.
|
| 153 |
-
self.
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
self.
|
| 173 |
-
self.
|
| 174 |
-
self.
|
| 175 |
-
self.
|
| 176 |
-
self.
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
)
|
| 188 |
-
layer_output
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
layer_output
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
self.
|
| 202 |
-
self.
|
| 203 |
-
self.
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
)
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
)
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
)
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
)
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
from typing import Optional, Tuple, Union
|
| 5 |
+
from einops import rearrange
|
| 6 |
+
from transformers import PreTrainedModel
|
| 7 |
+
from transformers.modeling_outputs import (
|
| 8 |
+
MaskedLMOutput,
|
| 9 |
+
BaseModelOutputWithPastAndCrossAttentions,
|
| 10 |
+
BaseModelOutputWithPoolingAndCrossAttentions,
|
| 11 |
+
SequenceClassifierOutput,
|
| 12 |
+
TokenClassifierOutput
|
| 13 |
+
)
|
| 14 |
+
from transformers.models.esm.modeling_esm import (
|
| 15 |
+
RotaryEmbedding,
|
| 16 |
+
EsmContactPredictionHead,
|
| 17 |
+
EsmIntermediate,
|
| 18 |
+
EsmOutput,
|
| 19 |
+
EsmPooler,
|
| 20 |
+
EsmLMHead,
|
| 21 |
+
EsmSelfOutput,
|
| 22 |
+
EsmClassificationHead,
|
| 23 |
+
create_position_ids_from_input_ids,
|
| 24 |
+
)
|
| 25 |
+
from .config_fastesm import FastEsmConfig
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class EsmEmbeddings(nn.Module):
|
| 29 |
+
"""
|
| 30 |
+
Same as BertEmbeddings with a tiny tweak for positional embeddings indexing.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
def __init__(self, config):
|
| 34 |
+
super().__init__()
|
| 35 |
+
self.word_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, padding_idx=config.pad_token_id)
|
| 36 |
+
if config.emb_layer_norm_before:
|
| 37 |
+
self.layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 38 |
+
else:
|
| 39 |
+
self.layer_norm = None
|
| 40 |
+
self.position_embedding_type = getattr(config, "position_embedding_type", "absolute")
|
| 41 |
+
self.register_buffer(
|
| 42 |
+
"position_ids", torch.arange(config.max_position_embeddings).expand((1, -1)), persistent=False
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
self.padding_idx = config.pad_token_id
|
| 46 |
+
self.position_embeddings = nn.Embedding(
|
| 47 |
+
config.max_position_embeddings, config.hidden_size, padding_idx=self.padding_idx
|
| 48 |
+
)
|
| 49 |
+
# Token dropout does not work correctly so we disable it
|
| 50 |
+
# self.token_dropout = config.token_dropout
|
| 51 |
+
self.mask_token_id = config.mask_token_id
|
| 52 |
+
|
| 53 |
+
def forward(
|
| 54 |
+
self, input_ids=None, attention_mask=None, position_ids=None, inputs_embeds=None, past_key_values_length=0
|
| 55 |
+
):
|
| 56 |
+
if position_ids is None:
|
| 57 |
+
if input_ids is not None:
|
| 58 |
+
# Create the position ids from the input token ids. Any padded tokens remain padded.
|
| 59 |
+
position_ids = create_position_ids_from_input_ids(input_ids, self.padding_idx, past_key_values_length)
|
| 60 |
+
else:
|
| 61 |
+
position_ids = self.create_position_ids_from_inputs_embeds(inputs_embeds)
|
| 62 |
+
|
| 63 |
+
if inputs_embeds is None:
|
| 64 |
+
inputs_embeds = self.word_embeddings(input_ids)
|
| 65 |
+
|
| 66 |
+
embeddings = inputs_embeds
|
| 67 |
+
|
| 68 |
+
if self.position_embedding_type == "absolute":
|
| 69 |
+
position_embeddings = self.position_embeddings(position_ids)
|
| 70 |
+
embeddings = embeddings + position_embeddings
|
| 71 |
+
|
| 72 |
+
if self.layer_norm is not None:
|
| 73 |
+
embeddings = self.layer_norm(embeddings)
|
| 74 |
+
if attention_mask is not None:
|
| 75 |
+
embeddings = (embeddings * attention_mask.unsqueeze(-1)).to(embeddings.dtype)
|
| 76 |
+
return embeddings
|
| 77 |
+
|
| 78 |
+
def create_position_ids_from_inputs_embeds(self, inputs_embeds):
|
| 79 |
+
"""
|
| 80 |
+
We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.
|
| 81 |
+
|
| 82 |
+
Args:
|
| 83 |
+
inputs_embeds: torch.Tensor
|
| 84 |
+
|
| 85 |
+
Returns: torch.Tensor
|
| 86 |
+
"""
|
| 87 |
+
input_shape = inputs_embeds.size()[:-1]
|
| 88 |
+
sequence_length = input_shape[1]
|
| 89 |
+
|
| 90 |
+
position_ids = torch.arange(
|
| 91 |
+
self.padding_idx + 1, sequence_length + self.padding_idx + 1, dtype=torch.long, device=inputs_embeds.device
|
| 92 |
+
)
|
| 93 |
+
return position_ids.unsqueeze(0).expand(input_shape)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class EsmSelfAttention(nn.Module):
|
| 97 |
+
def __init__(self, config, position_embedding_type=None):
|
| 98 |
+
super().__init__()
|
| 99 |
+
if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"):
|
| 100 |
+
raise ValueError(
|
| 101 |
+
f"The hidden size ({config.hidden_size}) is not a multiple of the number of attention "
|
| 102 |
+
f"heads ({config.num_attention_heads})"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
self.num_attention_heads = config.num_attention_heads
|
| 106 |
+
self.attention_head_size = int(config.hidden_size / config.num_attention_heads)
|
| 107 |
+
self.all_head_size = self.num_attention_heads * self.attention_head_size
|
| 108 |
+
|
| 109 |
+
self.query = nn.Linear(config.hidden_size, self.all_head_size)
|
| 110 |
+
self.key = nn.Linear(config.hidden_size, self.all_head_size)
|
| 111 |
+
self.value = nn.Linear(config.hidden_size, self.all_head_size)
|
| 112 |
+
self.scale = self.attention_head_size**-0.5
|
| 113 |
+
|
| 114 |
+
self.dropout_prob = config.attention_probs_dropout_prob
|
| 115 |
+
self.position_embedding_type = position_embedding_type or getattr(
|
| 116 |
+
config, "position_embedding_type", "absolute"
|
| 117 |
+
)
|
| 118 |
+
self.rotary_embeddings = None
|
| 119 |
+
if self.position_embedding_type == "rotary":
|
| 120 |
+
self.rotary_embeddings = RotaryEmbedding(dim=self.attention_head_size)
|
| 121 |
+
|
| 122 |
+
def transpose_for_scores(self, x: torch.Tensor) -> torch.Tensor:
|
| 123 |
+
return rearrange(x, 'b s (h d) -> b h s d', h=self.num_attention_heads)
|
| 124 |
+
|
| 125 |
+
def forward(
|
| 126 |
+
self,
|
| 127 |
+
hidden_states: torch.Tensor,
|
| 128 |
+
attention_mask: Optional[torch.FloatTensor] = None,
|
| 129 |
+
) -> Tuple[torch.Tensor]:
|
| 130 |
+
query_layer = self.transpose_for_scores(self.query(hidden_states)) * self.scale
|
| 131 |
+
key_layer = self.transpose_for_scores(self.key(hidden_states))
|
| 132 |
+
value_layer = self.transpose_for_scores(self.value(hidden_states))
|
| 133 |
+
|
| 134 |
+
if self.position_embedding_type == "rotary":
|
| 135 |
+
query_layer, key_layer = self.rotary_embeddings(query_layer, key_layer)
|
| 136 |
+
|
| 137 |
+
context_layer = F.scaled_dot_product_attention(
|
| 138 |
+
query_layer,
|
| 139 |
+
key_layer,
|
| 140 |
+
value_layer,
|
| 141 |
+
attn_mask=attention_mask,
|
| 142 |
+
dropout_p=self.dropout_prob,
|
| 143 |
+
scale=1.0
|
| 144 |
+
)
|
| 145 |
+
return rearrange(context_layer, 'b h s d -> b s (h d)')
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
class EsmAttention(nn.Module):
|
| 149 |
+
def __init__(self, config):
|
| 150 |
+
super().__init__()
|
| 151 |
+
self.self = EsmSelfAttention(config)
|
| 152 |
+
self.output = EsmSelfOutput(config)
|
| 153 |
+
self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 154 |
+
|
| 155 |
+
def forward(
|
| 156 |
+
self,
|
| 157 |
+
hidden_states,
|
| 158 |
+
attention_mask=None,
|
| 159 |
+
):
|
| 160 |
+
hidden_states_ln = self.LayerNorm(hidden_states)
|
| 161 |
+
attention_output = self.self(
|
| 162 |
+
hidden_states_ln,
|
| 163 |
+
attention_mask,
|
| 164 |
+
)
|
| 165 |
+
return self.output(attention_output, hidden_states)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
class EsmLayer(nn.Module):
|
| 169 |
+
def __init__(self, config):
|
| 170 |
+
super().__init__()
|
| 171 |
+
self.chunk_size_feed_forward = config.chunk_size_feed_forward
|
| 172 |
+
self.seq_len_dim = 1
|
| 173 |
+
self.attention = EsmAttention(config)
|
| 174 |
+
self.intermediate = EsmIntermediate(config)
|
| 175 |
+
self.output = EsmOutput(config)
|
| 176 |
+
self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 177 |
+
|
| 178 |
+
def forward(
|
| 179 |
+
self,
|
| 180 |
+
hidden_states,
|
| 181 |
+
attention_mask=None,
|
| 182 |
+
):
|
| 183 |
+
attention_output = self.attention(
|
| 184 |
+
hidden_states,
|
| 185 |
+
attention_mask,
|
| 186 |
+
)
|
| 187 |
+
layer_output = self.feed_forward_chunk(attention_output)
|
| 188 |
+
return layer_output
|
| 189 |
+
|
| 190 |
+
def feed_forward_chunk(self, attention_output):
|
| 191 |
+
attention_output_ln = self.LayerNorm(attention_output)
|
| 192 |
+
intermediate_output = self.intermediate(attention_output_ln)
|
| 193 |
+
layer_output = self.output(intermediate_output, attention_output)
|
| 194 |
+
return layer_output
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
class EsmEncoder(nn.Module):
|
| 198 |
+
def __init__(self, config):
|
| 199 |
+
super().__init__()
|
| 200 |
+
self.config = config
|
| 201 |
+
self.layer = nn.ModuleList([EsmLayer(config) for _ in range(config.num_hidden_layers)])
|
| 202 |
+
self.emb_layer_norm_after = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
| 203 |
+
self.gradient_checkpointing = False
|
| 204 |
+
|
| 205 |
+
def forward(
|
| 206 |
+
self,
|
| 207 |
+
hidden_states,
|
| 208 |
+
attention_mask=None,
|
| 209 |
+
output_hidden_states=False,
|
| 210 |
+
):
|
| 211 |
+
all_hidden_states = () if output_hidden_states else None
|
| 212 |
+
for layer_module in self.layer:
|
| 213 |
+
if output_hidden_states:
|
| 214 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
| 215 |
+
|
| 216 |
+
if self.gradient_checkpointing and self.training:
|
| 217 |
+
hidden_states = self._gradient_checkpointing_func(
|
| 218 |
+
layer_module.__call__,
|
| 219 |
+
hidden_states,
|
| 220 |
+
attention_mask,
|
| 221 |
+
)
|
| 222 |
+
else:
|
| 223 |
+
hidden_states = layer_module(
|
| 224 |
+
hidden_states,
|
| 225 |
+
attention_mask,
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
if self.emb_layer_norm_after:
|
| 229 |
+
hidden_states = self.emb_layer_norm_after(hidden_states)
|
| 230 |
+
|
| 231 |
+
if output_hidden_states:
|
| 232 |
+
all_hidden_states = all_hidden_states + (hidden_states,)
|
| 233 |
+
|
| 234 |
+
return BaseModelOutputWithPastAndCrossAttentions(
|
| 235 |
+
last_hidden_state=hidden_states,
|
| 236 |
+
hidden_states=all_hidden_states,
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
class FastEsmPreTrainedModel(PreTrainedModel):
|
| 241 |
+
"""
|
| 242 |
+
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
|
| 243 |
+
models.
|
| 244 |
+
"""
|
| 245 |
+
config_class = FastEsmConfig
|
| 246 |
+
base_model_prefix = "fastesm"
|
| 247 |
+
supports_gradient_checkpointing = True
|
| 248 |
+
def _init_weights(self, module):
|
| 249 |
+
"""Initialize the weights"""
|
| 250 |
+
if isinstance(module, nn.Linear):
|
| 251 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
| 252 |
+
if module.bias is not None:
|
| 253 |
+
module.bias.data.zero_()
|
| 254 |
+
elif isinstance(module, nn.Embedding):
|
| 255 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
| 256 |
+
if module.padding_idx is not None:
|
| 257 |
+
module.weight.data[module.padding_idx].zero_()
|
| 258 |
+
elif isinstance(module, nn.LayerNorm):
|
| 259 |
+
module.bias.data.zero_()
|
| 260 |
+
module.weight.data.fill_(1.0)
|
| 261 |
+
|
| 262 |
+
def get_input_embeddings(self) -> nn.Module:
|
| 263 |
+
try:
|
| 264 |
+
return self.embeddings.word_embeddings
|
| 265 |
+
except AttributeError:
|
| 266 |
+
return self.esm.embeddings.word_embeddings
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
class FastEsmModel(FastEsmPreTrainedModel):
|
| 270 |
+
def __init__(self, config, add_pooling_layer=True):
|
| 271 |
+
super().__init__(config)
|
| 272 |
+
self.config = config
|
| 273 |
+
self.embeddings = EsmEmbeddings(config)
|
| 274 |
+
self.encoder = EsmEncoder(config)
|
| 275 |
+
self.pooler = EsmPooler(config) if add_pooling_layer else None
|
| 276 |
+
# Initialize weights and apply final processing
|
| 277 |
+
self.post_init()
|
| 278 |
+
|
| 279 |
+
def get_input_embeddings(self):
|
| 280 |
+
return self.embeddings.word_embeddings
|
| 281 |
+
|
| 282 |
+
def set_input_embeddings(self, value):
|
| 283 |
+
self.embeddings.word_embeddings = value
|
| 284 |
+
|
| 285 |
+
def forward(
|
| 286 |
+
self,
|
| 287 |
+
input_ids: Optional[torch.Tensor] = None,
|
| 288 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 289 |
+
position_ids: Optional[torch.Tensor] = None,
|
| 290 |
+
inputs_embeds: Optional[torch.Tensor] = None,
|
| 291 |
+
output_hidden_states: Optional[bool] = None,
|
| 292 |
+
output_attentions: Optional[bool] = None,
|
| 293 |
+
) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]:
|
| 294 |
+
if output_attentions is not None:
|
| 295 |
+
raise ValueError("output_attentions is not supported by F.scaled_dot_product_attention")
|
| 296 |
+
output_hidden_states = (
|
| 297 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
| 298 |
+
)
|
| 299 |
+
if input_ids is not None and inputs_embeds is not None:
|
| 300 |
+
raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
|
| 301 |
+
elif input_ids is not None:
|
| 302 |
+
self.warn_if_padding_and_no_attention_mask(input_ids, attention_mask)
|
| 303 |
+
input_shape = input_ids.size()
|
| 304 |
+
elif inputs_embeds is not None:
|
| 305 |
+
input_shape = inputs_embeds.size()[:-1]
|
| 306 |
+
else:
|
| 307 |
+
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
| 308 |
+
|
| 309 |
+
batch_size, seq_length = input_shape
|
| 310 |
+
embedding_output = self.embeddings(
|
| 311 |
+
input_ids=input_ids,
|
| 312 |
+
position_ids=position_ids,
|
| 313 |
+
attention_mask=attention_mask,
|
| 314 |
+
inputs_embeds=inputs_embeds,
|
| 315 |
+
)
|
| 316 |
+
# Prepare attention mask
|
| 317 |
+
if attention_mask is not None:
|
| 318 |
+
# attention_mask shape should be (batch_size, 1, 1, seq_length)
|
| 319 |
+
# Expand to (batch_size, 1, seq_length, seq_length)
|
| 320 |
+
extended_attention_mask = attention_mask[:, None, None, :].expand(
|
| 321 |
+
batch_size, 1, seq_length, seq_length
|
| 322 |
+
)
|
| 323 |
+
# Convert mask to float with 0.0 for positions to keep and -inf for masked positions
|
| 324 |
+
attention_mask = attention_mask.to(dtype=embedding_output.dtype) # fp16 compatibility
|
| 325 |
+
attention_mask = (1.0 - attention_mask) * torch.finfo(embedding_output.dtype).min
|
| 326 |
+
else:
|
| 327 |
+
extended_attention_mask = None
|
| 328 |
+
|
| 329 |
+
encoder_outputs = self.encoder(
|
| 330 |
+
embedding_output,
|
| 331 |
+
attention_mask=extended_attention_mask,
|
| 332 |
+
output_hidden_states=output_hidden_states,
|
| 333 |
+
)
|
| 334 |
+
sequence_output = encoder_outputs.last_hidden_state
|
| 335 |
+
pooled_output = self.pooler(sequence_output) if self.pooler is not None else None
|
| 336 |
+
|
| 337 |
+
return BaseModelOutputWithPoolingAndCrossAttentions(
|
| 338 |
+
last_hidden_state=sequence_output,
|
| 339 |
+
pooler_output=pooled_output,
|
| 340 |
+
hidden_states=encoder_outputs.hidden_states,
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
class FastEsmForMaskedLM(FastEsmPreTrainedModel):
|
| 345 |
+
_tied_weights_keys = ["lm_head.decoder.weight"]
|
| 346 |
+
|
| 347 |
+
def __init__(self, config):
|
| 348 |
+
super().__init__(config)
|
| 349 |
+
self.esm = FastEsmModel(config, add_pooling_layer=False)
|
| 350 |
+
self.lm_head = EsmLMHead(config)
|
| 351 |
+
self.loss_fct = nn.CrossEntropyLoss()
|
| 352 |
+
self.init_weights()
|
| 353 |
+
|
| 354 |
+
def get_output_embeddings(self):
|
| 355 |
+
return self.lm_head.decoder
|
| 356 |
+
|
| 357 |
+
def set_output_embeddings(self, new_embeddings):
|
| 358 |
+
self.lm_head.decoder = new_embeddings
|
| 359 |
+
|
| 360 |
+
def forward(
|
| 361 |
+
self,
|
| 362 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 363 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 364 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 365 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 366 |
+
labels: Optional[torch.LongTensor] = None,
|
| 367 |
+
output_attentions: Optional[bool] = None,
|
| 368 |
+
output_hidden_states: Optional[bool] = None,
|
| 369 |
+
) -> Union[Tuple, MaskedLMOutput]:
|
| 370 |
+
outputs = self.esm(
|
| 371 |
+
input_ids,
|
| 372 |
+
attention_mask=attention_mask,
|
| 373 |
+
position_ids=position_ids,
|
| 374 |
+
inputs_embeds=inputs_embeds,
|
| 375 |
+
output_hidden_states=output_hidden_states,
|
| 376 |
+
output_attentions=output_attentions,
|
| 377 |
+
)
|
| 378 |
+
sequence_output = outputs.last_hidden_state
|
| 379 |
+
prediction_scores = self.lm_head(sequence_output)
|
| 380 |
+
|
| 381 |
+
loss = None
|
| 382 |
+
if labels is not None:
|
| 383 |
+
labels = labels.to(prediction_scores.device)
|
| 384 |
+
loss = self.loss_fct(prediction_scores.view(-1, self.config.vocab_size), labels.view(-1))
|
| 385 |
+
|
| 386 |
+
return MaskedLMOutput(
|
| 387 |
+
loss=loss,
|
| 388 |
+
logits=prediction_scores,
|
| 389 |
+
hidden_states=outputs.hidden_states,
|
| 390 |
+
)
|
| 391 |
+
|
| 392 |
+
def predict_contacts(self, tokens, attention_mask):
|
| 393 |
+
raise NotImplementedError("predict_contacts is not supported by F.scaled_dot_product_attention")
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
class FastEsmForSequenceClassification(FastEsmPreTrainedModel):
|
| 397 |
+
def __init__(self, config):
|
| 398 |
+
super().__init__(config)
|
| 399 |
+
self.num_labels = config.num_labels
|
| 400 |
+
self.config = config
|
| 401 |
+
self.esm = FastEsmModel(config, add_pooling_layer=False)
|
| 402 |
+
self.classifier = EsmClassificationHead(config)
|
| 403 |
+
self.mse = nn.MSELoss()
|
| 404 |
+
self.ce = nn.CrossEntropyLoss()
|
| 405 |
+
self.bce = nn.BCEWithLogitsLoss()
|
| 406 |
+
self.init_weights()
|
| 407 |
+
|
| 408 |
+
def forward(
|
| 409 |
+
self,
|
| 410 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 411 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 412 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 413 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 414 |
+
labels: Optional[torch.LongTensor] = None,
|
| 415 |
+
output_attentions: Optional[bool] = None,
|
| 416 |
+
output_hidden_states: Optional[bool] = None,
|
| 417 |
+
) -> Union[Tuple, SequenceClassifierOutput]:
|
| 418 |
+
outputs = self.esm(
|
| 419 |
+
input_ids,
|
| 420 |
+
attention_mask=attention_mask,
|
| 421 |
+
position_ids=position_ids,
|
| 422 |
+
inputs_embeds=inputs_embeds,
|
| 423 |
+
output_attentions=output_attentions,
|
| 424 |
+
output_hidden_states=output_hidden_states,
|
| 425 |
+
)
|
| 426 |
+
sequence_output = outputs.last_hidden_state
|
| 427 |
+
logits = self.classifier(sequence_output)
|
| 428 |
+
|
| 429 |
+
loss = None
|
| 430 |
+
if labels is not None:
|
| 431 |
+
labels = labels.to(logits.device)
|
| 432 |
+
if self.config.problem_type is None:
|
| 433 |
+
if self.num_labels == 1:
|
| 434 |
+
self.config.problem_type = "regression"
|
| 435 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
| 436 |
+
self.config.problem_type = "single_label_classification"
|
| 437 |
+
else:
|
| 438 |
+
self.config.problem_type = "multi_label_classification"
|
| 439 |
+
|
| 440 |
+
if self.config.problem_type == "regression":
|
| 441 |
+
if self.num_labels == 1:
|
| 442 |
+
loss = self.mse(logits.squeeze(), labels.squeeze())
|
| 443 |
+
else:
|
| 444 |
+
loss = self.mse(logits, labels)
|
| 445 |
+
elif self.config.problem_type == "single_label_classification":
|
| 446 |
+
loss = self.ce(logits.view(-1, self.num_labels), labels.view(-1))
|
| 447 |
+
elif self.config.problem_type == "multi_label_classification":
|
| 448 |
+
loss = self.bce(logits, labels)
|
| 449 |
+
|
| 450 |
+
return SequenceClassifierOutput(
|
| 451 |
+
loss=loss,
|
| 452 |
+
logits=logits,
|
| 453 |
+
hidden_states=outputs.hidden_states,
|
| 454 |
+
)
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
class FastEsmForTokenClassification(FastEsmPreTrainedModel):
|
| 458 |
+
def __init__(self, config):
|
| 459 |
+
super().__init__(config)
|
| 460 |
+
self.num_labels = config.num_labels
|
| 461 |
+
self.esm = FastEsmModel(config, add_pooling_layer=False)
|
| 462 |
+
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
| 463 |
+
self.classifier = nn.Linear(config.hidden_size, config.num_labels)
|
| 464 |
+
self.loss_fct = nn.CrossEntropyLoss()
|
| 465 |
+
self.init_weights()
|
| 466 |
+
|
| 467 |
+
def forward(
|
| 468 |
+
self,
|
| 469 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 470 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 471 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 472 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 473 |
+
labels: Optional[torch.LongTensor] = None,
|
| 474 |
+
output_attentions: Optional[bool] = None,
|
| 475 |
+
output_hidden_states: Optional[bool] = None,
|
| 476 |
+
) -> Union[Tuple, TokenClassifierOutput]:
|
| 477 |
+
outputs = self.esm(
|
| 478 |
+
input_ids,
|
| 479 |
+
attention_mask=attention_mask,
|
| 480 |
+
position_ids=position_ids,
|
| 481 |
+
inputs_embeds=inputs_embeds,
|
| 482 |
+
output_attentions=output_attentions,
|
| 483 |
+
output_hidden_states=output_hidden_states,
|
| 484 |
+
)
|
| 485 |
+
sequence_output = outputs.last_hidden_state
|
| 486 |
+
sequence_output = self.dropout(sequence_output)
|
| 487 |
+
logits = self.classifier(sequence_output)
|
| 488 |
+
|
| 489 |
+
loss = None
|
| 490 |
+
if labels is not None:
|
| 491 |
+
labels = labels.to(logits.device)
|
| 492 |
+
loss = self.loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
|
| 493 |
+
|
| 494 |
+
return TokenClassifierOutput(
|
| 495 |
+
loss=loss,
|
| 496 |
+
logits=logits,
|
| 497 |
+
hidden_states=outputs.hidden_states,
|
| 498 |
+
)
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
if __name__ == "__main__":
|
| 502 |
+
"""
|
| 503 |
+
Test the hidden state differences between the FastEsmModel and the HF EsmModel.
|
| 504 |
+
In full precision, the differences are very small, but nonzero due to floating point issues with F.scaled_dot_product_attention.
|
| 505 |
+
In Pytorch 2.5+ (and linux kernel), this implementation is very fast and uses less memory than the HF implementation.
|
| 506 |
+
"""
|
| 507 |
+
import random
|
| 508 |
+
from transformers import EsmModel as TransformersEsmModel, EsmTokenizer
|
| 509 |
+
|
| 510 |
+
model_paths = [
|
| 511 |
+
"facebook/esm2_t6_8M_UR50D",
|
| 512 |
+
"facebook/esm2_t12_35M_UR50D",
|
| 513 |
+
"facebook/esm2_t30_150M_UR50D",
|
| 514 |
+
"facebook/esm2_t33_650M_UR50D",
|
| 515 |
+
]
|
| 516 |
+
canonical_amino_acids = "ACDEFGHIKLMNPQRSTVWY"
|
| 517 |
+
length = 64
|
| 518 |
+
seq_count = 100
|
| 519 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 520 |
+
tolerances = [1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8]
|
| 521 |
+
|
| 522 |
+
def generate_random_sequence(length: int) -> str:
|
| 523 |
+
return 'M' + "".join(random.choices(canonical_amino_acids, k=length))
|
| 524 |
+
|
| 525 |
+
print("Percentage of hidden states that are within the tolerance:")
|
| 526 |
+
for model_path in model_paths:
|
| 527 |
+
print(f"Testing {model_path}...")
|
| 528 |
+
tokenizer = EsmTokenizer.from_pretrained(model_path)
|
| 529 |
+
fast_model = FastEsmModel.from_pretrained(model_path, token_dropout=False).to(device)
|
| 530 |
+
model = TransformersEsmModel.from_pretrained(model_path, token_dropout=False).to(device)
|
| 531 |
+
|
| 532 |
+
counts = [0] * len(tolerances)
|
| 533 |
+
for _ in range(seq_count):
|
| 534 |
+
example_seq = generate_random_sequence(length)
|
| 535 |
+
fast_tokens = tokenizer(example_seq, return_tensors="pt").input_ids.to(device)
|
| 536 |
+
fast_output = fast_model(fast_tokens).last_hidden_state.detach().cpu()
|
| 537 |
+
|
| 538 |
+
model_tokens = tokenizer(example_seq, return_tensors="pt").input_ids.to(device)
|
| 539 |
+
model_output = model(model_tokens).last_hidden_state.detach().cpu()
|
| 540 |
+
|
| 541 |
+
for i, atol in enumerate(tolerances):
|
| 542 |
+
if torch.allclose(fast_output, model_output, atol=atol):
|
| 543 |
+
counts[i] += 1
|
| 544 |
+
|
| 545 |
+
print(f"{model_path}:")
|
| 546 |
+
for i, atol in enumerate(tolerances):
|
| 547 |
+
print(f" tolerance={atol}: {counts[i] / seq_count * 100}%")
|
| 548 |
+
|
| 549 |
+
model.cpu()
|
| 550 |
+
fast_model.cpu()
|
| 551 |
+
del model
|
| 552 |
+
del fast_model
|
| 553 |
+
torch.cuda.empty_cache()
|