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
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
####################################
#                                  #
#        import libraries          #
#                                  #
####################################


import torch
import numpy as np
import random
import os
import json
import logging
import pandas as pd
import torch.distributed as dist
from torch.utils.data import Dataset, DataLoader
import h5py
from transformers import AutoTokenizer
from torch.utils.data.distributed import DistributedSampler
from functools import partial
import torch.nn.functional as F


####################################
#                                  #
#        helper functions          #
#                                  #
####################################


def ensure_dir(path: str) -> None:
    os.makedirs(path, exist_ok=True)


def create_logger(log_file: str, rank: int, name: str = "ProRiboGen") -> logging.Logger:
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    logger.propagate=False
    
    if getattr(logger, "_proribogen_inited", False):
        return logger
    
    fmt = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s", "%Y-%m-%d %H:%M:%S")
    
    if rank == 0:
        ensure_dir(os.path.dirname(log_file) or ".")
        fh = logging.FileHandler(log_file, encoding="utf-8")
        fh.setFormatter(fmt)
        fh.setLevel(logging.INFO)
        logger.addHandler(fh)
        
        sh = logging.StreamHandler()
        sh.setFormatter(fmt)
        sh.setLevel(logging.INFO)
        logger.addHandler(sh)
    
    else:
        sh = logging.StreamHandler(open(os.devnull, "w"))
        sh.setFormatter(fmt)
        sh.setLevel(logging.CRITICAL)
        logger.addHandler(sh)
        
    logger._proribogen_inited = True
    return logger


def cleanup_ddp():
    torch.cuda.empty_cache()
    if dist.is_available() and dist.is_initialized():
        dist.destroy_process_group()
    

def setup_ddp(rank: int, world_size: int, port: int):
    torch.cuda.set_device(rank)
    if world_size > 1:
        os.environ["MASTER_PORT"] = str(port)
        os.environ["MASTER_ADDR"] = "127.0.0.1"
        dist.init_process_group(
            backend="nccl",
            rank=rank,
            world_size=world_size
        )


def is_main_process(rank):
    return rank == 0


def load_config(config_path: str = "config.json") -> dict:
    with open(config_path, "r", encoding="utf-8") as f:
        config = json.load(f)
    return config


def get_h5_embedding_dim(h5_path: str) -> int:
    """Return per-token protein embedding dimension from a merged embeddings H5."""
    with h5py.File(h5_path, "r") as f:
        if "embeddings" not in f:
            raise ValueError(f"H5 missing 'embeddings' dataset: {h5_path}")
        return int(f["embeddings"].shape[1])


def validate_protein_dim_against_h5(config: dict, base_dir: str = ".") -> int:
    """
    Ensure config model.protein_dim matches the H5 embedding width.
    If protein_dim is omitted, set it from the H5 file.
    """
    h5_rel = config["data"]["protein_h5"]
    h5_path = h5_rel if os.path.isabs(h5_rel) else os.path.join(base_dir, h5_rel)
    if not os.path.isfile(h5_path):
        raise FileNotFoundError(f"Protein H5 not found: {h5_path}")

    h5_dim = get_h5_embedding_dim(h5_path)
    model_cfg = config.setdefault("model", {})
    cfg_dim = model_cfg.get("protein_dim")

    if cfg_dim is None:
        model_cfg["protein_dim"] = h5_dim
        return h5_dim

    cfg_dim = int(cfg_dim)
    if cfg_dim != h5_dim:
        raise ValueError(
            f"config model.protein_dim={cfg_dim} does not match H5 embedding dim={h5_dim} "
            f"({h5_path})"
        )
    return h5_dim


def a_useful_log(logger, rank, message):
    if is_main_process(rank):
        logger.info(message)


####################################
#                                  #
#               Data               #
#                                  #
####################################


class RnaRbpDataset(Dataset):
    """
    读取 RNA–RBP(蛋白序列嵌入)配对数据,用于建模 RBP 与靶 RNA 的相互作用。

    CSV 主列为 ``r_id``, ``rna``, ``p_id``;若数据仍为旧表头 ``d_id`` / ``dna``,会自动映射到
    上述列。

    H5:
        - embeddings : [N_total_tokens, protein_dim]
        - p_ids      : [N_proteins]
        - starts     : [N_proteins]
        - lengths    : [N_proteins]

    返回单样本 dict:
        {
            "r_id": str,
            "rna": str,
            "p_id": str,
            "rna_len": int,
            "protein_emb": torch.FloatTensor [Lp, Dp],
            "protein_len": int,
        }
    """
    REQUIRED_COLUMNS = ("r_id", "rna", "p_id")
    _LEGACY_ALIASES = {
        "r_id": ("d_id",),
        "rna": ("dna",),
    }
    # CSV p_id -> H5 p_id when naming differs between annotation and embedding index
    _P_ID_ALIASES = {
        "human-DDX3": "human-DDX3X",
        "human-HNRNPH1": "human-HNRNPH",
    }

    def __init__(self, csv_path: str, h5_path: str = "data/esm2-protein_embeddings.h5") -> None:
        super().__init__()
        self.csv_path = csv_path
        self.h5_path = h5_path
        self.data = pd.read_csv(csv_path)
        for canonical, aliases in self._LEGACY_ALIASES.items():
            if canonical not in self.data.columns:
                for alt in aliases:
                    if alt in self.data.columns:
                        self.data[canonical] = self.data[alt]
                        break

        missing_cols = [col for col in self.REQUIRED_COLUMNS if col not in self.data.columns]
        if missing_cols:
            raise ValueError(
                f"CSV 文件缺少必要列: {missing_cols}. 当前列为: {list(self.data.columns)}"
            )
        self.data["rna"] = self.data["rna"].astype(str).str.upper()
        self.data["r_id"] = self.data["r_id"].astype(str)
        self.data["p_id"] = self.data["p_id"].astype(str).replace(self._P_ID_ALIASES)
        self.data["rna_len"] = self.data["rna"].str.len()
        with h5py.File(self.h5_path, "r") as f:
            p_ids = [
                x.decode("utf-8") if isinstance(x, bytes) else str(x)
                for x in f["p_ids"][:]
            ]
            starts = f["starts"][:]
            lengths = f["lengths"][:]
        self.protein_index = {
            p_id: (int(start), int(length))
            for p_id, start, length in zip(p_ids, starts, lengths)
        }
        self.all_p_ids = list(self.protein_index.keys())

        valid_mask = self.data["p_id"].isin(self.protein_index)
        n_dropped = int((~valid_mask).sum())
        if n_dropped:
            missing_p_ids = sorted(self.data.loc[~valid_mask, "p_id"].unique().tolist())
            self.data = self.data.loc[valid_mask].reset_index(drop=True)
            logging.getLogger("ProRiboGen").warning(
                "Dropped %d rows from %s: p_id not in H5 (%s)",
                n_dropped,
                csv_path,
                ", ".join(missing_p_ids[:20])
                + (" ..." if len(missing_p_ids) > 20 else ""),
            )

        self._h5_file = None
        self._embeddings_ds = None

    def _ensure_h5_open(self):
        if self._h5_file is None:
            self._h5_file = h5py.File(self.h5_path, "r")
            self._embeddings_ds = self._h5_file["embeddings"]

    def __len__(self) -> int:
        return len(self.data)
    def get_protein_embedding_by_pid(self, p_id: str) -> torch.Tensor:
        if p_id not in self.protein_index:
            raise KeyError(f"Protein id not found in H5 index: {p_id}")

        start, length = self.protein_index[p_id]
        self._ensure_h5_open()
        protein_emb = self._embeddings_ds[start : start + length]
        return torch.from_numpy(protein_emb).float()

    def __getitem__(self, idx: int) -> dict:
        row = self.data.iloc[idx]
        r_id = row["r_id"]
        rna = row["rna"]
        p_id = row["p_id"]
        rna_len = int(row["rna_len"])
        start, length = self.protein_index[p_id]

        self._ensure_h5_open()
        protein_emb = self._embeddings_ds[start : start + length]

        protein_emb = torch.from_numpy(protein_emb).float()
        sample = {
            "r_id": r_id,
            "rna": rna,
            "p_id": p_id,
            "rna_len": rna_len,
            "protein_emb": protein_emb,
            "protein_len": int(length),
        }
        return sample
    def __del__(self):
        if self._h5_file is not None:
            try:
                self._h5_file.close()
            except Exception:
                pass


def rna_rbp_collate_fn(
    batch,
    tokenizer,
    *,
    append_eos: bool = False,
    max_rna_nt: int | None = None,
):
    r_ids = [sample["r_id"] for sample in batch]
    p_ids = [sample["p_id"] for sample in batch]
    rna_strs = [sample["rna"] for sample in batch]
    if append_eos and getattr(tokenizer, "eos_token", None):
        eos = tokenizer.eos_token
        rna_strs = [s + eos for s in rna_strs]
    rna_lens = torch.tensor([sample["rna_len"] for sample in batch], dtype=torch.long)
    protein_lens = torch.tensor([sample["protein_len"] for sample in batch], dtype=torch.long)

    tok_kw: dict = {"padding": True, "return_tensors": "pt"}
    if max_rna_nt is not None and max_rna_nt > 0:
        tok_kw["max_length"] = max_rna_nt + 2
        tok_kw["truncation"] = True

    rna_enc = tokenizer(rna_strs, **tok_kw)

    rna_input_ids_clean = rna_enc["input_ids"]
    rna_attention_mask = rna_enc["attention_mask"]

    batch_size = len(batch)
    max_protein_len = max(sample["protein_emb"].shape[0] for sample in batch)
    protein_dim = batch[0]["protein_emb"].shape[1]

    protein_cond = torch.zeros(
        batch_size,
        max_protein_len,
        protein_dim,
        dtype=batch[0]["protein_emb"].dtype,
    )
    protein_attention_mask = torch.zeros(
        batch_size,
        max_protein_len,
        dtype=torch.long,
    )

    for i, sample in enumerate(batch):
        protein_emb = sample["protein_emb"]
        Lp = protein_emb.shape[0]
        protein_cond[i, :Lp] = protein_emb
        protein_attention_mask[i, :Lp] = 1

    return {
        "r_id": r_ids,
        "p_id": p_ids,
        "rna_input_ids_clean": rna_input_ids_clean,
        "rna_attention_mask": rna_attention_mask,
        "rna_len_bp": rna_lens,
        "protein_cond": protein_cond,
        "protein_attention_mask": protein_attention_mask,
        "protein_len_tokens": protein_lens,
    }


def build_dataloaders(config, world_size, rank):
    num_workers = config["train"]["num_workers"]
    use_worker_processes = num_workers > 0

    # 1. tokenizer
    tokenizer = AutoTokenizer.from_pretrained(
        config["data"]["tokenizer_path"],
        trust_remote_code=True,
    )

    # 2. dataset
    train_dataset = RnaRbpDataset(
        csv_path=config["data"]["train_csv"],
        h5_path=config["data"]["protein_h5"],
    )
    test_dataset = RnaRbpDataset(
        csv_path=config["data"]["test_csv"],
        h5_path=config["data"]["protein_h5"],
    )

    append_eos = bool(config.get("data", {}).get("append_eos_token", False))
    max_rna_nt = config.get("data", {}).get("max_generated_rna_bp")
    if max_rna_nt is not None:
        max_rna_nt = int(max_rna_nt)
    collate_fn = partial(
        rna_rbp_collate_fn,
        tokenizer=tokenizer,
        append_eos=append_eos,
        max_rna_nt=max_rna_nt,
    )

    # 4. sampler
    if world_size > 1:
        train_sampler = DistributedSampler(
            train_dataset,
            num_replicas=world_size,
            rank=rank,
            shuffle=True,
            drop_last=False,
        )
        test_sampler = DistributedSampler(
            test_dataset,
            num_replicas=world_size,
            rank=rank,
            shuffle=False,
            drop_last=False,
        )
        train_shuffle = False
    else:
        train_sampler = None
        test_sampler = None
        train_shuffle = True

    # 5. dataloader
    train_loader = DataLoader(
        train_dataset,
        batch_size=config["train"]["batch_size_per_gpu"],
        shuffle=train_shuffle,
        sampler=train_sampler,
        num_workers=num_workers,
        pin_memory=True,
        drop_last=False,
        collate_fn=collate_fn,
        persistent_workers=use_worker_processes,
        multiprocessing_context="spawn" if use_worker_processes else None,
    )

    test_loader = DataLoader(
        test_dataset,
        batch_size=config["train"]["eval_batch_size_per_gpu"],
        shuffle=False,
        sampler=test_sampler,
        num_workers=num_workers,
        pin_memory=True,
        drop_last=False,
        collate_fn=collate_fn,
        persistent_workers=use_worker_processes,
        multiprocessing_context="spawn" if use_worker_processes else None,
    )

    return train_loader, test_loader, train_sampler, test_sampler, tokenizer


def sample_random_negative_sources(
    pos_p_ids,
    all_p_ids,
    use_batch_negatives_first: bool = True,
    rng=None,
    deterministic: bool = False,
):
    if rng is None:
        rng = random
    if len(all_p_ids) <= 1:
        raise ValueError("Need at least two proteins to sample negatives.")

    negative_sources = []
    batch_p_ids = list(pos_p_ids)

    for i, pos_p_id in enumerate(batch_p_ids):
        batch_candidates = [
            j for j, candidate_p_id in enumerate(batch_p_ids)
            if candidate_p_id != pos_p_id
        ]

        if use_batch_negatives_first and batch_candidates:
            chosen_idx = batch_candidates[0] if deterministic else rng.choice(batch_candidates)
            negative_sources.append(
                {
                    "source": "batch",
                    "batch_index": chosen_idx,
                    "neg_p_id": batch_p_ids[chosen_idx],
                }
            )
            continue

        if deterministic:
            neg_p_id = next(candidate for candidate in all_p_ids if candidate != pos_p_id)
        else:
            neg_p_id = rng.choice(all_p_ids)
            while neg_p_id == pos_p_id:
                neg_p_id = rng.choice(all_p_ids)

        negative_sources.append(
            {
                "source": "pool",
                "neg_p_id": neg_p_id,
            }
        )

    return negative_sources


def build_random_negative_protein_batch(
    batch: dict,
    dataset: RnaRbpDataset,
    use_batch_negatives_first: bool = True,
    rng=None,
    deterministic: bool = False,
) -> dict:
    batch_p_ids = list(batch["p_id"])
    negative_sources = sample_random_negative_sources(
        pos_p_ids=batch_p_ids,
        all_p_ids=dataset.all_p_ids,
        use_batch_negatives_first=use_batch_negatives_first,
        rng=rng,
        deterministic=deterministic,
    )

    negative_embeddings = []
    negative_p_ids = []
    negative_lengths = []

    batch_protein_cond = batch["protein_cond"]
    batch_protein_attention_mask = batch["protein_attention_mask"]

    for source in negative_sources:
        neg_p_id = source["neg_p_id"]
        if source["source"] == "batch":
            batch_index = source["batch_index"]
            protein_len = int(batch_protein_attention_mask[batch_index].sum().item())
            negative_emb = batch_protein_cond[batch_index, :protein_len].clone()
        else:
            negative_emb = dataset.get_protein_embedding_by_pid(neg_p_id)
            protein_len = int(negative_emb.shape[0])

        negative_embeddings.append(negative_emb)
        negative_p_ids.append(neg_p_id)
        negative_lengths.append(protein_len)

    batch_size = len(negative_embeddings)
    max_negative_len = max(negative_lengths)
    protein_dim = negative_embeddings[0].shape[1]

    negative_protein_cond = torch.zeros(
        batch_size,
        max_negative_len,
        protein_dim,
        dtype=negative_embeddings[0].dtype,
    )
    negative_protein_attention_mask = torch.zeros(
        batch_size,
        max_negative_len,
        dtype=torch.long,
    )

    for i, negative_emb in enumerate(negative_embeddings):
        protein_len = negative_emb.shape[0]
        negative_protein_cond[i, :protein_len] = negative_emb
        negative_protein_attention_mask[i, :protein_len] = 1

    return {
        "neg_p_id": negative_p_ids,
        "neg_protein_cond": negative_protein_cond,
        "neg_protein_attention_mask": negative_protein_attention_mask,
        "neg_protein_len_tokens": torch.tensor(negative_lengths, dtype=torch.long),
    }


####################################
#                                  #
#              Other               #
#                                  #
####################################


base_config = {
  "add_bias_fnn": False,
  "attention_probs_dropout_prob": 0.0,
  "emb_layer_norm_before": False,
  "esmfold_config": None,
  "hidden_dropout_prob": 0.0,
  "hidden_size": 512,
  "initializer_range": 0.02,
  "intermediate_size": 2048,
  "is_folding_model": False,
  "layer_norm_eps": 1e-12,
  "mask_token_id": 2,
  "max_position_embeddings": 2050,
  "model_type": "esm",
  "num_attention_heads": 16,
  "num_hidden_layers": 12,
  "pad_token_id": 1,
  "position_embedding_type": "rotary",
  "tie_word_embeddings": False,
  "token_dropout": False,
  "torch_dtype": "float32",
  "transformers_version": "4.54.1",
  "use_cache": False,
  "vocab_list": None,
  "vocab_size": 4107
}


def sample_t(
    batch_size: int,
    device: torch.device,
    eps: float = 1e-3,
    dtype: torch.dtype = torch.float32,) -> torch.Tensor:
    """
    Sample diffusion corruption ratio t for each sample.

    Args:
        batch_size: number of samples in the batch.
        device: target device.
        eps: lower bound to avoid t=0.
        dtype: dtype of the returned tensor.

    Returns:
        t: shape [B], each element sampled from Uniform(eps, 1.0).
    """
    if not (0.0 <= eps < 1.0):
        raise ValueError(f"`eps` must satisfy 0 <= eps < 1, got {eps}.")

    t = eps + (1.0 - eps) * torch.rand(batch_size, device=device, dtype=dtype)
    return t


def build_xt_and_labels(
    x0: torch.LongTensor,
    rna_attention_mask: torch.Tensor,
    mask_token_id: int,
    t: torch.Tensor,
    special_token_ids=None,
    force_at_least_one_mask: bool = True,
):
    """
    Build corrupted input x_t and MLM-style labels for ProRiboGen training.

    Args:
        x0:
            Clean RNA token ids, shape [B, L].
        rna_attention_mask:
            RNA attention mask, shape [B, L]. Valid tokens = 1, padding = 0.
        mask_token_id:
            Token id for <mask>.
        t:
            Per-sample corruption ratio, shape [B].
        special_token_ids:
            Iterable of token ids that should never be masked, e.g. [pad_id,cls_id].
        force_at_least_one_mask:
            If True, ensure each sample has at least one masked valid token.

    Returns:
        xt:
            Corrupted input ids, shape [B, L].
        labels:
            Target ids for CE loss, shape [B, L], with non-masked positions setto -100.
        masked_positions:
            Bool tensor, shape [B, L], True where the token was masked.
    """
    if x0.dim() != 2:
        raise ValueError(f"`x0` must have shape [B, L], got{tuple(x0.shape)}.")
    if rna_attention_mask.shape != x0.shape:
        raise ValueError(
            f"`rna_attention_mask` must match `x0` shape, got "
            f"{tuple(rna_attention_mask.shape)} vs {tuple(x0.shape)}."
        )
    if t.dim() != 1 or t.shape[0] != x0.shape[0]:
        raise ValueError(
            f"`t` must have shape [B], got {tuple(t.shape)} for batch size{x0.shape[0]}."
        )

    B, L = x0.shape
    device = x0.device

    valid_positions = rna_attention_mask.bool()

    if special_token_ids is not None:
        for token_id in special_token_ids:
            if token_id is not None:
                valid_positions &= (x0 != token_id)

    mask_probs = t.unsqueeze(1).expand(B, L)
    random_vals = torch.rand(B, L, device=device)
    masked_positions = (random_vals < mask_probs) & valid_positions

    if force_at_least_one_mask:
        no_mask_rows = masked_positions.sum(dim=1) == 0
        if no_mask_rows.any():
            no_mask_indices = no_mask_rows.nonzero(as_tuple=True)[0]
            for b in no_mask_indices.tolist():
                candidate_positions = valid_positions[b].nonzero(as_tuple=True)[0]
                if candidate_positions.numel() == 0:
                    raise ValueError(
                        f"Sample {b} has no valid RNA token available for masking."
                    )
                chosen = candidate_positions[
                    torch.randint(
                        low=0,
                        high=candidate_positions.numel(),
                        size=(1,),
                        device=device,
                    )
                ]
                masked_positions[b, chosen] = True

    xt = x0.clone()
    xt[masked_positions] = mask_token_id

    labels = x0.clone()
    labels[~masked_positions] = -100

    return xt, labels, masked_positions


####################################
#                                  #
#               loss               #
#                                  #
####################################


def compute_diffusion_loss(
    logits: torch.Tensor,
    labels: torch.LongTensor,
    t: torch.Tensor,
    eps: float = 1e-3,) -> torch.Tensor:
    if logits.dim() != 3:
        raise ValueError(f"`logits` must have shape [B, L, V], got{tuple(logits.shape)}.")
    if labels.dim() != 2:
        raise ValueError(f"`labels` must have shape [B, L], got{tuple(labels.shape)}.")
    if logits.shape[:2] != labels.shape:
        raise ValueError(
            f"`logits` and `labels` must match on [B, L], got "
            f"{tuple(logits.shape[:2])} vs {tuple(labels.shape)}.")
    if t.dim() != 1 or t.shape[0] != logits.shape[0]:
        raise ValueError(
            f"`t` must have shape [B], got {tuple(t.shape)} for batch size{logits.shape[0]}.")
    B, L, V = logits.shape
    token_loss = F.cross_entropy(
        logits.float().reshape(B * L, V),
        labels.view(B * L),
        reduction="none",
        ignore_index=-100,
    ).view(B, L)
    masked_positions = (labels != -100)
    sample_loss = (token_loss * masked_positions).sum(dim=1)
    weights = 1.0 / t.clamp_min(eps)
    loss = (sample_loss * weights).mean()
    return loss


def compute_per_sample_masked_token_ce(
    logits: torch.Tensor,
    labels: torch.LongTensor,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    if logits.dim() != 3:
        raise ValueError(f"`logits` must have shape [B, L, V], got{tuple(logits.shape)}.")
    if labels.dim() != 2:
        raise ValueError(f"`labels` must have shape [B, L], got{tuple(labels.shape)}.")
    if logits.shape[:2] != labels.shape:
        raise ValueError(
            f"`logits` and `labels` must match on [B, L], got "
            f"{tuple(logits.shape[:2])} vs {tuple(labels.shape)}."
        )

    B, L, V = logits.shape
    token_loss = F.cross_entropy(
        logits.float().reshape(B * L, V),
        labels.view(B * L),
        reduction="none",
        ignore_index=-100,
    ).view(B, L)
    masked_positions = (labels != -100)
    sample_loss_sum = (token_loss * masked_positions).sum(dim=1)
    sample_masked_count = masked_positions.sum(dim=1)
    sample_masked_ce = sample_loss_sum / sample_masked_count.clamp_min(1)
    return sample_masked_ce, sample_loss_sum, sample_masked_count


def compute_masked_token_ce(
    logits: torch.Tensor,
    labels: torch.LongTensor,
) -> tuple[torch.Tensor, torch.Tensor]:
    _, sample_loss_sum, sample_masked_count = compute_per_sample_masked_token_ce(
        logits=logits,
        labels=labels,
    )
    masked_token_loss_sum = sample_loss_sum.sum()
    masked_token_count = sample_masked_count.sum()
    masked_token_ce = masked_token_loss_sum / masked_token_count.clamp_min(1)
    return masked_token_ce, masked_token_count


def compute_pn_losses(
    logits_pos: torch.Tensor,
    logits_neg: torch.Tensor,
    labels: torch.LongTensor,
    t: torch.Tensor,
    alpha: float,
    margin: float,
    diffusion_eps: float = 1e-3,
) -> dict:
    diff_loss_pos = compute_diffusion_loss(
        logits=logits_pos,
        labels=labels,
        t=t,
        eps=diffusion_eps,
    )

    pos_sample_ce, pos_sample_loss_sum, pos_sample_masked_count = compute_per_sample_masked_token_ce(
        logits=logits_pos,
        labels=labels,
    )
    neg_sample_ce, neg_sample_loss_sum, neg_sample_masked_count = compute_per_sample_masked_token_ce(
        logits=logits_neg,
        labels=labels,
    )

    rank_per_sample = torch.relu(margin + pos_sample_ce - neg_sample_ce)
    rank_loss = rank_per_sample.mean()
    total_loss = diff_loss_pos + float(alpha) * rank_loss

    sample_count = torch.tensor(
        pos_sample_ce.shape[0],
        device=logits_pos.device,
        dtype=torch.float64,
    )
    delta_ce_sum = (neg_sample_ce - pos_sample_ce).to(torch.float64).sum()
    margin_satisfied_sum = (
        (neg_sample_ce >= (pos_sample_ce + float(margin))).to(torch.float64).sum()
    )

    return {
        "diff_loss_pos": diff_loss_pos,
        "rank_loss": rank_loss,
        "total_loss": total_loss,
        "pos_masked_token_loss_sum": pos_sample_loss_sum.to(torch.float64).sum(),
        "pos_masked_token_count": pos_sample_masked_count.to(torch.float64).sum(),
        "neg_masked_token_loss_sum": neg_sample_loss_sum.to(torch.float64).sum(),
        "neg_masked_token_count": neg_sample_masked_count.to(torch.float64).sum(),
        "delta_ce_sum": delta_ce_sum,
        "margin_satisfied_sum": margin_satisfied_sum,
        "rank_loss_sum": rank_per_sample.to(torch.float64).sum(),
        "sample_count": sample_count,
    }


####################################
#                                  #
#           save/resume            #
#                                  #
####################################


def save_model(
    save_path: str,
    model,
    optimizer=None,
    scheduler=None,
    epoch: int = 0,
    global_step: int = 0,
    config: dict = None,
    extra_state: dict = None,) -> None:
    """
    Save training checkpoint.

    Args:
        save_path: checkpoint file path, e.g. "checkpoints/latest.pt"
        model: model or DDP-wrapped model
        optimizer: optional optimizer
        scheduler: optional scheduler
        epoch: current finished epoch index
        global_step: current optimizer step count
        config: optional config dict
        extra_state: optional extra metadata dict
    """
    ensure_dir(os.path.dirname(save_path) or ".")
    model_to_save = model.module if hasattr(model, "module") else model
    checkpoint = {
        "model_state_dict": model_to_save.state_dict(),
        "optimizer_state_dict": optimizer.state_dict() if optimizer is not None else None,
        "scheduler_state_dict": scheduler.state_dict() if scheduler is not None else None,
        "epoch": epoch,
        "global_step": global_step,
        "config": config,
        "extra_state": extra_state if extra_state is not None else {},}
    torch.save(checkpoint, save_path)


def _remap_legacy_checkpoint_state_dict(state_dict: dict) -> dict:
    """
    旧 checkpoint 中 ``protein_conditioning_attention.dna_norm.*`` 与当前模块名 ``rna_norm`` 对齐。
    """
    out = {}
    for key, value in state_dict.items():
        new_key = key.replace(
            "protein_conditioning_attention.dna_norm.",
            "protein_conditioning_attention.rna_norm.",
        )
        out[new_key] = value
    return out


def resume_model(
    load_path: str,
    model,
    optimizer=None,
    scheduler=None,
    device: torch.device = None,
    strict: bool = True,) -> dict:
    """
    Resume training checkpoint.

    Args:
        load_path: checkpoint file path
        model: model or DDP-wrapped model
        optimizer: optional optimizer
        scheduler: optional scheduler
        device: target device for optimizer state tensors
        strict: passed to model.load_state_dict()

    Returns:
        A dict with:
            - epoch
            - global_step
            - config
            - extra_state
    """
    if not os.path.exists(load_path):
        raise FileNotFoundError(f"Checkpoint not found: {load_path}")

    checkpoint = torch.load(load_path, map_location="cpu")
    model_to_load = model.module if hasattr(model, "module") else model
    state_dict = _remap_legacy_checkpoint_state_dict(checkpoint["model_state_dict"])
    model_to_load.load_state_dict(state_dict, strict=strict)
    if optimizer is not None and checkpoint.get("optimizer_state_dict") is not None:
        optimizer.load_state_dict(checkpoint["optimizer_state_dict"])

        if device is not None:
            for state in optimizer.state.values():
                for k, v in state.items():
                    if torch.is_tensor(v):
                        state[k] = v.to(device)
    if scheduler is not None and checkpoint.get("scheduler_state_dict") is not None:
        scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
    resume_state = {
        "epoch": checkpoint.get("epoch", -1),
        "global_step": checkpoint.get("global_step", 0),
        "config": checkpoint.get("config", None),
        "extra_state": checkpoint.get("extra_state", {}),
    }
    return resume_state