File size: 32,438 Bytes
7718235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import re
import warnings
from typing import Optional, List, Tuple, Dict

import torch
from torch import _dynamo
_dynamo.config.suppress_errors = True
from torch import nn, Tensor
from model.module.representation import eqStar2PAETransformerSoftMax, eqStar2WeightedPAETransformerSoftMax, eqStar2FullGraphPAETransformerSoftMax
from model.module import output

__all__ = ["PreMode", "PreMode_Star_CON", "PreMode_DIFF", "PreMode_SSP", "PreMode_Mask_Predict", "PreMode_Single"]


def create_model(args, model_class="PreMode"):
    shared_args = dict(
        num_heads=args["num_heads"],
        x_in_channels=args["x_in_channels"],
        x_channels=args["x_channels"],
        vec_channels=args["vec_channels"],
        vec_in_channels=args["vec_in_channels"],
        x_hidden_channels=args["x_hidden_channels"],
        vec_hidden_channels=args["vec_hidden_channels"],
        num_layers=args["num_layers"],
        num_edge_attr=args["num_edge_attr"],
        num_rbf=args["num_rbf"],
        rbf_type=args["rbf_type"],
        trainable_rbf=args["trainable_rbf"],
        activation=args["activation"],
        attn_activation=args["attn_activation"],
        neighbor_embedding=args["neighbor_embedding"],
        cutoff_lower=args["cutoff_lower"],
        cutoff_upper=args["cutoff_upper"],
        x_in_embedding_type=args["x_in_embedding_type"],
        x_use_msa=args['add_msa'] or args['zero_msa'],
        drop_out_rate=args["drop_out"],
    )

    # representation network
    if args["model"] == "equivariant-transformer":
        from model.module.representation import eqTransformer
        
        model_fn = eqTransformer
    elif args["model"] == "equivariant-transformer-star":
        from model.module.representation import eqStarTransformer
        model_fn = eqStarTransformer
    elif args["model"] == "equivariant-transformer-softmax":
        from model.module.representation import eqTransformerSoftMax
        model_fn = eqTransformerSoftMax
    elif args["model"] == "equivariant-transformer-star-softmax":
        from model.module.representation import eqStarTransformerSoftMax
        model_fn = eqStarTransformerSoftMax
    elif args["model"] == "equivariant-transformer-star2-softmax":
        from model.module.representation import eqStar2TransformerSoftMax
        model_fn = eqStar2TransformerSoftMax
        shared_args["use_lora"]=args["use_lora"]
        shared_args["share_kv"]=args["share_kv"]
    elif args["model"] == "equivariant-transformer-PAE-star2-softmax":
        model_fn = eqStar2PAETransformerSoftMax
        shared_args["use_lora"]=args["use_lora"]
        shared_args["share_kv"]=args["share_kv"]
        args["num_rbf"] = 0 # cancel the rbf in PAE model
    elif args["model"] == "equivariant-transformer-weighted-PAE-star2-softmax":
        model_fn = eqStar2WeightedPAETransformerSoftMax
        shared_args["use_lora"]=args["use_lora"]
        shared_args["share_kv"]=args["share_kv"]
        args["num_rbf"] = 0 # cancel the rbf in PAE model
    elif args["model"] == "equivariant-transformer-PAE-star2-fullgraph-softmax":
        model_fn = eqStar2FullGraphPAETransformerSoftMax
        shared_args["use_lora"]=args["use_lora"]
        shared_args["share_kv"]=args["share_kv"]
    elif args["model"] == "transformer-fullgraph-softmax":
        from model.module.representation import FullGraphPAETransformerSoftMax
        model_fn = FullGraphPAETransformerSoftMax
        shared_args["use_lora"]=args["use_lora"]
        shared_args["share_kv"]=args["share_kv"]
    elif args["model"] == "equivariant-triangular-attention-transformer":
        from model.module.representation import eqTriAttnTransformer
        model_fn = eqTriAttnTransformer
        shared_args["pariwise_state_dim"]=args["vec_hidden_channels"]
    elif args["model"] == "equivariant-triangular-star-transformer":
        from model.module.representation import eqTriStarTransformer
        model_fn = eqTriStarTransformer
    elif args["model"] == "equivariant-msa-triangular-star-transformer":
        from model.module.representation import eqMSATriStarTransformer
        model_fn = eqMSATriStarTransformer
        shared_args["ee_channels"]=args["ee_channels"]
        shared_args["triangular_update"]=args["triangular_update"]
    elif args["model"] == "equivariant-msa-triangular-star-drop-transformer":
        from model.module.representation import eqMSATriStarDropTransformer
        model_fn = eqMSATriStarDropTransformer
        shared_args["ee_channels"]=args["ee_channels"]
        shared_args["triangular_update"]=args["triangular_update"]
        shared_args["use_lora"]=args["use_lora"]
    elif args["model"] == "equivariant-msa-triangular-star-gru-transformer":
        from model.module.representation import eqMSATriStarGRUTransformer
        model_fn = eqMSATriStarGRUTransformer
        shared_args["ee_channels"]=args["ee_channels"]
        shared_args["triangular_update"]=args["triangular_update"]
    elif args["model"] == "equivariant-msa-triangular-star-drop-gru-transformer":
        from model.module.representation import eqMSATriStarDropGRUTransformer
        model_fn = eqMSATriStarDropGRUTransformer
        shared_args["ee_channels"]=args["ee_channels"]
        shared_args["triangular_update"]=args["triangular_update"]
        shared_args["use_lora"]=args["use_lora"]
    elif args["model"] == "pass-forward":
        from model.module.representation import PassForward
        model_fn = PassForward
    elif args["model"] == "lora-esm":
        from model.module.representation import LoRAESM2
        model_fn = LoRAESM2
    else:
        raise ValueError(f'Unknown architecture: {args["model"]}')
    representation_model = model_fn(
            **shared_args,
        )
    # create output network
    if "MaskPredict" in args["output_model"]:
        output_model = getattr(output, args["output_model"])(
            args=args,
            lm_weight=representation_model.node_x_proj.weight,
        )
    elif "ESM" in args["output_model"]:
        # get lm_weight from esm2
        import esm
        esm_model, _ = esm.pretrained.esm2_t33_650M_UR50D()
        output_model = output.build_output_model(
            args["output_model"], 
            args=args,
            lm_head=esm_model.lm_head,
        )
    else:
        # for non-clinvar tasks, use non_uniform init
        if args["init_fn"] is None:
            if args["data_type"] != "ClinVar":
                args["init_fn"] = "non_uniform"
            else:
                args["init_fn"] = "uniform"
        if hasattr(output, args["output_model"]):
            output_model = getattr(output, args["output_model"])(
                args=args,
            )
        else:
            output_model = output.build_output_model(args["output_model"], args=args)

    # combine representation and output network
    model = globals()[model_class](
        representation_model,
        output_model,
        alt_projector=args["alt_projector"],
    )
    return model


def create_model_and_load(args, model_class="PreMode"):
    model = create_model(args, model_class)
    state_dict = torch.load(args["load_model"], map_location="cpu")
    # The following are for backward compatibility with models created when atomref was
    # the only supported prior.
    output_model_state_dict = {}
    representation_model_state_dict = {}
    for key in state_dict.keys():
        # delete _orig_mod
        if key.startswith("_orig_mod"):
            newkey = key.replace("_orig_mod.", "")
        else:
            newkey = key
        if newkey.startswith("output_model"):
            output_model_state_dict[newkey.replace("output_model.", "")] = state_dict[key]
        elif newkey.startswith("representation_model"):
            if newkey.startswith("representation_model.node_x_proj.weight"):
                if args["partial_load_model"]:
                    embedding_weight = state_dict[key]
                    print('only use the first 26 embedding of MaskPredict')
                    embedding_weight = embedding_weight[:26]  # exclude the embedding of mask
                    representation_model_state_dict["node_x_proj.weight"] = \
                        torch.concat((embedding_weight,
                                      torch.zeros(args["x_in_channels"] - embedding_weight.shape[0],
                                                  embedding_weight.shape[1]))).T
                    representation_model_state_dict["node_x_proj.bias"] = \
                        torch.zeros(args["x_channels"])
                else:
                    representation_model_state_dict[newkey.replace("representation_model.", "")] = state_dict[key]
            else:
                representation_model_state_dict[newkey.replace("representation_model.", "")] = state_dict[key]
    model.representation_model.load_state_dict(representation_model_state_dict, strict=False)
    if args["data_type"] == "ClinVar" \
        or args['loss_fn'] == "combined_loss" \
        or args['loss_fn'] == "weighted_combined_loss" \
        or args['use_output_head']:
        # or args['loss_fn'] == "weighted_loss":
        try:
            # check the output network module dimension
            if output_model_state_dict['output_network.0.weight'].shape[0] != args['output_dim']:
                # if output network is EquivariantAttnOneSiteScalar, we can use it
                if "OneSite" in args['output_model'] and args['use_output_head']:
                    rep_time = args['output_dim'] // output_model_state_dict['output_network.0.weight'].shape[0] 
                    # repeat the weight and bias repeat_interleave
                    output_model_state_dict['output_network.0.weight'] = output_model_state_dict['output_network.0.weight'].repeat_interleave(rep_time, 0)
                    output_model_state_dict['output_network.0.bias'] = output_model_state_dict['output_network.0.bias'].repeat_interleave(rep_time)
                else:
                    print('Warning: output network module dimension is not equal to output_dim, now changing the dimension')
                    output_network_weight = torch.concat(
                        (output_model_state_dict['output_network.0.weight'],
                        torch.zeros(args['output_dim'] - output_model_state_dict['output_network.0.weight'].shape[0], 
                                    output_model_state_dict['output_network.0.weight'].shape[1])
                        )
                    )
                    output_network_bias = torch.concat(
                        (output_model_state_dict['output_network.0.bias'],
                        torch.zeros(args['output_dim'] - output_model_state_dict['output_network.0.bias'].shape[0])
                        )
                    )
                    output_model_state_dict['output_network.0.weight'] = output_network_weight
                    output_model_state_dict['output_network.0.bias'] = output_network_bias
                model.output_model.load_state_dict(output_model_state_dict, strict=False)
            print(f"loaded the output model state dict including the output module")
        except RuntimeError:
            print(f"Warning: Didn't load output model state dict because keys didn't match.")
    else:
        print(f"Warning: Didn't load output model because task is not ClinVar")
    return model


def load_model(filepath, args=None, device="cpu", model_class="PreMode", **kwargs):
    ckpt = torch.load(filepath, map_location="cpu")
    if args is None:
        args = ckpt["hyper_parameters"]

    for key, value in kwargs.items():
        if not key in args:
            warnings.warn(f"Unknown hyperparameter: {key}={value}")
        args[key] = value

    model = create_model(args, model_class=model_class)

    state_dict = {re.sub(r"^model\.", "", k): v for k, v in ckpt["state_dict"].items()}

    model.load_state_dict(state_dict)
    return model.to(device)


class PreMode(nn.Module):
    def __init__(
            self,
            representation_model,
            output_model,
            alt_projector=None,
    ):
        super(PreMode, self).__init__()
        self.representation_model = representation_model
        self.output_model = output_model
        if alt_projector is not None:
            # need to have a linear layer to project the concatenated vector to the same dimension as the original vector
            out_dim = representation_model.x_channels if representation_model.x_in_channels is None else representation_model.x_in_channels
            self.alt_linear = nn.Linear(alt_projector, out_dim, bias=False)
        else:
            self.alt_linear = None

        self.reset_parameters()

    def reset_parameters(self):
        self.representation_model.reset_parameters()
        self.output_model.reset_parameters()

    def forward(
            self,
            x: Tensor,
            x_mask: Tensor,
            x_alt: Tensor,
            pos: Tensor,
            edge_index: Tensor,
            edge_index_star: Tensor = None,
            edge_attr: Tensor = None,
            edge_attr_star: Tensor = None,
            node_vec_attr: Tensor = None,
            batch: Optional[Tensor] = None,
            extra_args: Optional[Dict[str, Tensor]] = None,
            return_attn: bool = False,
    ) -> Tuple[Tensor, Tensor, List]:
        
        # assert x.dim() == 2
        batch = torch.zeros(x.shape[0], dtype=torch.int64, device=x.device) if batch is None else batch

        # get the graph representation of origin protein first
        # if there is msa in x, split it
        if (self.representation_model.x_in_channels is not None and x.shape[1] > self.representation_model.x_in_channels):
            x_orig, _ = x[:, :self.representation_model.x_in_channels], x[:, self.representation_model.x_in_channels:]
        elif x.shape[1] > self.representation_model.x_channels:
            x_orig, _ = x[:, :self.representation_model.x_channels], x[:, self.representation_model.x_channels:]
        else:
            x_orig = x

        if self.alt_linear is not None:
            x_alt = self.alt_linear(x_alt)
        # update x to alt aa
        x = x * x_mask + x_alt * x_mask

        # run the potentially wrapped representation model
        if extra_args is not None and "y_mask" in extra_args:
            x, v, pos, edge_attr, batch, attn_weight_layers = self.representation_model(
                x=x,
                pos=pos,
                batch=batch,
                edge_index=edge_index,
                edge_index_star=edge_index_star,
                edge_attr=edge_attr,
                edge_attr_star=edge_attr_star,
                node_vec_attr=node_vec_attr,
                mask=extra_args["y_mask"].to(x.device, non_blocking=True),
                return_attn=return_attn, )
        else:
            x, v, pos, edge_attr, batch, attn_weight_layers = self.representation_model(
                x=x,
                pos=pos,
                batch=batch,
                edge_index=edge_index,
                edge_index_star=edge_index_star,
                edge_attr=edge_attr,
                edge_attr_star=edge_attr_star,
                node_vec_attr=node_vec_attr,
                return_attn=return_attn, )

        # apply the output network
        x = self.output_model.pre_reduce(x, v, pos, batch)

        # aggregate residues
        if extra_args is not None and "y_mask" in extra_args:
            x = x * extra_args["y_mask"].unsqueeze(2).to(x.device, non_blocking=True)
        
        # reduce nodes
        x, attn_out = self.output_model.reduce(x - x_orig, edge_index, edge_attr, batch)
        # x = self.output_model.reduce(x, edge_index, edge_attr, batch)
        attn_weight_layers.append(attn_out)
        # apply output model after reduction
        y = self.output_model.post_reduce(x)

        return y, x, attn_weight_layers


class PreMode_Star_CON(nn.Module):
    def __init__(
            self,
            representation_model,
            output_model,
            alt_projector=None,
    ):
        super(PreMode_Star_CON, self).__init__()
        self.representation_model = representation_model
        self.output_model = output_model
        self.alt_projector = alt_projector
        if alt_projector is not None:
            # need to have a linear layer to project the concatenated vector to the same dimension as the original vector
            out_dim = representation_model.x_channels if representation_model.x_in_channels is None else representation_model.x_in_channels
            self.alt_linear = nn.Sequential(nn.Linear(alt_projector, out_dim, bias=False), nn.SiLU())
        else:
            self.alt_linear = None
        self.reset_parameters()

    def reset_parameters(self):
        self.representation_model.reset_parameters()
        self.output_model.reset_parameters()

    def forward(
            self,
            x: Tensor,
            x_mask: Tensor,
            x_alt: Tensor,
            pos: Tensor,
            edge_index: Tensor,
            edge_index_star: Tensor = None,
            edge_attr: Tensor = None,
            edge_attr_star: Tensor = None,
            node_vec_attr: Tensor = None,
            batch: Optional[Tensor] = None,
            extra_args: Optional[Dict[str, Tensor]] = None,
            return_attn: bool = False,
    ) -> Tuple[Tensor, Tensor, List]:
        # assert x.dim() == 2
        batch = torch.zeros(x.shape[0], dtype=torch.int64, device=x.device) if batch is None else batch
        # get the graph representation of origin protein first
        # if there is msa in x, split it
        if self.representation_model.x_in_channels is not None:
            if x.shape[-1] > self.representation_model.x_in_channels:
                x, msa = x[..., :self.representation_model.x_in_channels], x[..., self.representation_model.x_in_channels:]
                split = True
            else:
                split = False
        elif x.shape[-1] > self.representation_model.x_channels:
            x, msa = x[..., :self.representation_model.x_channels], x[..., self.representation_model.x_channels:]
            split = True
        else:
            split = False
        if len(x.shape) == 3 or len(x_mask.shape) == 1:
            x_mask = x_mask.unsqueeze(-1)
        else:
            x_mask = x_mask[:, 0].unsqueeze(1)
        if self.alt_linear is not None:
            x_alt = x_alt[..., :self.alt_projector]
            x_alt = self.alt_linear(x_alt)
        else:
            x_alt = x_alt[..., :x.shape[-1]]
        # update x to alt aa
        x = x * x_mask + x_alt * (~x_mask)
        # concat with msa
        if split:
            x = torch.cat((x, msa), dim=-1)

        # run the potentially wrapped representation model
        # wrap input features
        input = {"x": x, 
                 "pos": pos, 
                 "batch": batch, 
                 "edge_index": edge_index, 
                 "edge_index_star": edge_index_star, 
                 "edge_attr": edge_attr,
                 "edge_attr_star": edge_attr_star, 
                 "node_vec_attr": node_vec_attr,
                 "return_attn": return_attn}
        
        if extra_args is not None and "y_mask" in extra_args:
            input["mask"] = extra_args["y_mask"].to(x.device, non_blocking=True)
        if extra_args is not None and "x_padding_mask" in extra_args:
            input["x_padding_mask"] = extra_args["x_padding_mask"].to(x.device, non_blocking=True)    
        if isinstance(self.representation_model, eqStar2PAETransformerSoftMax) or \
            isinstance(self.representation_model, eqStar2WeightedPAETransformerSoftMax) or \
            isinstance(self.representation_model, eqStar2FullGraphPAETransformerSoftMax):
            # means we are using PAE model
            input["plddt"] = extra_args["plddt"].to(x.device, non_blocking=True) \
                if "plddt" in extra_args else None
            input["edge_confidence"] = extra_args["edge_confidence"].to(x.device, non_blocking=True) \
                if "edge_confidence" in extra_args else None
            input["edge_confidence_star"] = extra_args["edge_confidence_star"].to(x.device, non_blocking=True) \
                if "edge_confidence_star" in extra_args else None
        x, v, pos, edge_attr, batch, attn_weight_layers = self.representation_model(**input)
        # apply the output network
        x = self.output_model.pre_reduce(x, v, pos, batch)

        # aggregate residues
        if extra_args is not None and "y_mask" in extra_args:
            x = x * extra_args["y_mask"].unsqueeze(2).to(x.device, non_blocking=True)
        
        # if edge_attr is same shape as edge_index_star, it means that edge_attr is actually updated to edge_attr_star
        if len(x.shape) < 3:
            # # for nodes not connected by edges, set their x to 0
            # reduce nodes by star graph
            end_node_count = edge_index_star[1].unique(return_counts=True)
            end_nodes = end_node_count[0][end_node_count[1] > 1]
            if edge_attr is not None and edge_attr.shape[0] == edge_index_star.shape[1]:
                x, attn_out = self.output_model.reduce(x, 
                                        edge_index_star[:, torch.isin(edge_index_star[1], end_nodes)],
                                        edge_attr[torch.isin(edge_index_star[1], end_nodes), :],
                                        batch)
            else:
                # if edge_attr is not updated, use edge_attr_star
                x, attn_out = self.output_model.reduce(x,
                                            edge_index_star[:, torch.isin(edge_index_star[1], end_nodes)],
                                            edge_attr_star[torch.isin(edge_index_star[1], end_nodes), :],
                                            batch)
        else:
            x, attn_out = self.output_model.reduce(
                x, (~x_mask).squeeze(2),
                edge_attr[0], edge_attr[1], edge_attr[2], edge_attr[3], 
                input["x_padding_mask"])
            if 'score_mask' not in extra_args:
                x = x.unsqueeze(1)
        # x = self.output_model.reduce(x, edge_index, edge_attr, batch)
        attn_weight_layers.append(attn_out)
        # apply output model after reduction
        # if esm_mask is in extra_args, it means we are using esm model
        if "esm_mask" in extra_args:
            y = self.output_model.post_reduce(x, extra_args["esm_mask"].to(x.device, non_blocking=True))
        else:
            y = self.output_model.post_reduce(x)

        return y, x, attn_weight_layers


class PreMode_SSP(PreMode):
    def __init__(
            self,
            representation_model,
            output_model,
            vec_in_channels=4,
    ):
        super(PreMode_SSP, self).__init__(representation_model=representation_model,
                                          output_model=output_model,)
        self.vec_reconstruct = nn.Linear(representation_model.vec_channels, vec_in_channels, bias=False)

    def forward(
            self,
            x: Tensor,
            x_mask: Tensor,
            x_alt: Tensor,
            pos: Tensor,
            edge_index: Tensor,
            edge_index_star: Tensor = None,
            edge_attr: Tensor = None,
            edge_attr_star: Tensor = None,
            edge_vec: Tensor = None,
            edge_vec_star: Tensor = None,
            node_vec_attr: Tensor = None,
            batch: Optional[Tensor] = None,
            extra_args: Optional[Dict[str, Tensor]] = None,
            return_attn: bool = False,
    ) -> Tuple[Tensor, Tensor, Tensor, Tensor, List]:

        assert x.dim() == 2 and x.dtype == torch.float
        batch = torch.zeros(x.shape[0], dtype=torch.int64, device=x.device) if batch is None else batch

        # get the graph representation of origin protein first
        x_orig = x

        # update x to alt aa
        x = x * x_mask + x_alt

        # run the potentially wrapped representation model
        x, v, pos, edge_attr, batch, attn_weight_layers = self.representation_model(
            x=x,
            pos=pos,
            batch=batch,
            edge_index=edge_index,
            edge_index_star=edge_index_star,
            edge_attr=edge_attr,
            edge_attr_star=edge_attr_star,
            node_vec_attr=node_vec_attr,
            return_attn=return_attn, )

        vec = self.vec_reconstruct(v)
        # apply the output network
        x_graph: Tensor = x
        x = self.output_model.pre_reduce(x, v, pos, batch)

        # aggregate residues
        x, _ = self.output_model.reduce(x - x_orig, edge_index, edge_attr, batch)

        # apply output model after reduction
        y = self.output_model.post_reduce(x)

        return x_graph, vec, y, x, attn_weight_layers


class PreMode_DIFF(PreMode):
    def __init__(
            self,
            representation_model,
            output_model,
            alt_projector=None,
    ):
        super(PreMode_DIFF, self).__init__(representation_model=representation_model,
                                           output_model=output_model,)

    def forward(
            self,
            x: Tensor,
            x_mask: Tensor,
            x_alt: Tensor,
            pos: Tensor,
            edge_index: Tensor,
            edge_index_star: Tensor = None,
            edge_attr: Tensor = None,
            edge_attr_star: Tensor = None,
            edge_vec: Tensor = None,
            edge_vec_star: Tensor = None,
            node_vec_attr: Tensor = None,
            batch: Optional[Tensor] = None,
            extra_args: Optional[Dict[str, Tensor]] = None,
            return_attn: bool = False,
    ) -> Tuple[Tensor, Tensor, List]:

        # assert x.dim() == 2 and x.dtype == torch.float
        batch = torch.zeros(x.shape[0], dtype=torch.int64, device=x.device) if batch is None else batch

        # get the graph representation of origin protein first
        x_orig, v, pos, _, batch, attn_weight_layers_ref = self.representation_model(
            x=x,
            pos=pos,
            batch=batch,
            edge_index=edge_index,
            edge_index_star=edge_index_star,
            edge_attr=edge_attr,
            edge_attr_star=edge_attr_star,
            node_vec_attr=node_vec_attr,
            return_attn=return_attn, )
        x_orig = self.output_model.pre_reduce(x_orig, v, pos, batch)

        # update x to alt aa
        x = x * x_mask + x_alt

        # run the potentially wrapped representation model
        x, v, pos, edge_attr, batch, attn_weight_layers_alt = self.representation_model(
            x=x,
            pos=pos,
            batch=batch,
            edge_index=edge_index,
            edge_index_star=edge_index_star,
            edge_attr=edge_attr,
            edge_attr_star=edge_attr_star,
            node_vec_attr=node_vec_attr,
            return_attn=return_attn, )

        # apply the output network
        x = self.output_model.pre_reduce(x, v, pos, batch)

        # aggregate residues
        x, _ = self.output_model.reduce(x - x_orig, edge_index, edge_attr, batch)

        # apply output model after reduction
        y = self.output_model.post_reduce(x)

        return y, x, [attn_weight_layers_ref, attn_weight_layers_alt]


class PreMode_Mask_Predict(PreMode):
    def __init__(
            self,
            representation_model,
            output_model,
            alt_projector=None,
    ):
        super(PreMode_Mask_Predict, self).__init__(representation_model=representation_model,
                                                   output_model=output_model,)

    def forward(
            self,
            x: Tensor,
            x_mask: Tensor,
            x_alt: Tensor,
            pos: Tensor,
            edge_index: Tensor,
            edge_index_star: Tensor = None,
            edge_attr: Tensor = None,
            edge_attr_star: Tensor = None,
            edge_vec: Tensor = None,
            edge_vec_star: Tensor = None,
            node_vec_attr: Tensor = None,
            batch: Optional[Tensor] = None,
            extra_args: Optional[Dict[str, Tensor]] = None,
            return_attn: bool = False,
    ) -> Tuple[Tensor, Tensor, List]:
        
        # assert x.dim() == 2 and x.dtype == torch.float
        batch = torch.zeros(x.shape[0], dtype=torch.int64, device=x.device) if batch is None else batch

        # update x to alt aa
        x = x * x_mask + x_alt

        # get the graph representation of origin protein first
        if "y_mask" in extra_args:
            # means that it is non-graph model
            x_embed, v, pos, _, batch, attn_weight_layers_ref = self.representation_model(
                x=x,
                pos=pos,
                mask=extra_args["y_mask"].to(x.device, non_blocking=True),
                return_attn=return_attn, )
        else:
            x_embed, v, pos, _, batch, attn_weight_layers_ref = self.representation_model(
                x=x,
                pos=pos,
                batch=batch,
                edge_index=edge_index,
                edge_index_star=edge_index_star,
                edge_attr=edge_attr,
                edge_attr_star=edge_attr_star,
                node_vec_attr=node_vec_attr,
                return_attn=return_attn, )
        # pre reduce is to reduce to one hot alphabet
        y = self.output_model.pre_reduce(x_embed, v, pos, batch)

        return y, y, attn_weight_layers_ref


class PreMode_Single(PreMode):
    def __init__(
            self,
            representation_model,
            output_model,
            alt_projector=None,
    ):
        super(PreMode_Single, self).__init__(representation_model=representation_model,
                                             output_model=output_model,)

    def forward(
            self,
            x: Tensor,
            x_mask: Tensor,
            x_alt: Tensor,
            pos: Tensor,
            edge_index: Tensor,
            edge_index_star: Tensor = None,
            edge_attr: Tensor = None,
            edge_attr_star: Tensor = None,
            edge_vec: Tensor = None,
            edge_vec_star: Tensor = None,
            node_vec_attr: Tensor = None,
            batch: Optional[Tensor] = None,
            extra_args: Optional[Dict[str, Tensor]] = None,
            return_attn: bool = False,
    ) -> Tuple[Tensor, Tensor, List]:

        assert x.dim() == 2
        batch = torch.zeros(x.shape[0], dtype=torch.int64, device=x.device) if batch is None else batch

        # get the graph representation of origin protein first
        # if there is msa in x, split it
        # if there is msa in x, split it
        if (self.representation_model.x_in_channels is not None and x.shape[1] > self.representation_model.x_in_channels):
            x, msa = x[:, :self.representation_model.x_in_channels], x[:, self.representation_model.x_in_channels:]
            split = True
        elif x.shape[1] > self.representation_model.x_channels:
            x, msa = x[:, :self.representation_model.x_channels], x[:, self.representation_model.x_channels:]
            split = True
        else:
            split = False
        x_mask = x_mask[:, 0]
        if self.alt_linear is not None:
            x_alt = x_alt[:, :self.alt_projector]
            x_alt = self.alt_linear(x_alt)
        else:
            x_alt = x_alt[:, :x.shape[1]]
        # update x to alt aa
        x = x * x_mask.unsqueeze(1) + x_alt * (~x_mask).unsqueeze(1)
        # concat with msa
        if split:
            x = torch.cat((x, msa), dim=1)

        # run the potentially wrapped representation model
        x, v, pos, edge_attr, batch, attn_weight_layers = self.representation_model(
            x=x,
            pos=pos,
            batch=batch,
            edge_index=edge_index,
            edge_index_star=edge_index_star,
            edge_attr=edge_attr,
            edge_attr_star=edge_attr_star,
            node_vec_attr=node_vec_attr,
            return_attn=return_attn, )

        # apply the output network
        x = self.output_model.pre_reduce(x, v, pos, batch)

        # aggregate residues
        x, _ = self.output_model.reduce(x, edge_index, edge_attr, batch)

        # apply output model after reduction
        y = self.output_model.post_reduce(x)

        return y, x, attn_weight_layers