lhallee commited on
Commit
7807461
·
verified ·
1 Parent(s): 049d1b7

Upload modeling_fast_esmfold.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_fast_esmfold.py +45 -7
modeling_fast_esmfold.py CHANGED
@@ -399,20 +399,20 @@ def bool_to_additive_mask(
399
  additive.masked_fill_(bool_mask.logical_not(), float("-inf"))
400
  return additive
401
 
402
- """FastESMFold: Self-contained ESMFold with FastESM2 attention backends + built-in Test-Time Training.
403
 
404
  Usage:
405
  from transformers import AutoModel
406
  model = AutoModel.from_pretrained("Synthyra/FastESMFold", trust_remote_code=True).cuda()
407
 
408
- # Basic folding
409
  result = model.fold_protein("MKTLLILAVVA...")
410
  print(result["plddt"], result["pdb_string"][:100])
411
 
412
- # Folding with TTT (test-time training improves structure prediction)
413
  result = model.fold_protein("MKTLLILAVVA...", ttt=True)
414
 
415
- Dependencies: torch, transformers, einops, peft (for LoRA TTT only)
416
  No dependency on: esm (fair-esm), proteinttt, openfold
417
  """
418
  import copy
@@ -902,11 +902,11 @@ class FastEsmFoldConfig(EsmConfig):
902
  # =============================================================================
903
 
904
  class FastEsmForProteinFolding(EsmForProteinFolding):
905
- """ESMFold with FastESM2 attention backends + built-in Test-Time Training.
906
 
907
  Inherits all folding logic (trunk, structure module, output_to_pdb, infer)
908
  from transformers.EsmForProteinFolding. Replaces the ESM2 backbone with
909
- FastESM2 for optimized attention and adds TTT for improved structure prediction.
910
 
911
  Key API:
912
  result = model.fold_protein("MKTL...", ttt=True)
@@ -1293,8 +1293,46 @@ class FastEsmForProteinFolding(EsmForProteinFolding):
1293
  self,
1294
  sequence: str,
1295
  return_pdb_string: bool = True,
 
1296
  ) -> Dict[str, Any]:
1297
- """Fold a protein sequence with test-time training.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1298
 
1299
  Runs TTT (masked language model adaptation via LoRA) for the configured
1300
  number of steps, folding after each optimizer step to track pLDDT. Returns
 
399
  additive.masked_fill_(bool_mask.logical_not(), float("-inf"))
400
  return additive
401
 
402
+ """FastESMFold: self-contained ESMFold with FastESM2 attention and opt-in TTT.
403
 
404
  Usage:
405
  from transformers import AutoModel
406
  model = AutoModel.from_pretrained("Synthyra/FastESMFold", trust_remote_code=True).cuda()
407
 
408
+ # Basic folding, no TTT
409
  result = model.fold_protein("MKTLLILAVVA...")
410
  print(result["plddt"], result["pdb_string"][:100])
411
 
412
+ # Experimental folding with TTT
413
  result = model.fold_protein("MKTLLILAVVA...", ttt=True)
414
 
415
+ Dependencies: torch, transformers, einops
416
  No dependency on: esm (fair-esm), proteinttt, openfold
417
  """
418
  import copy
 
902
  # =============================================================================
903
 
904
  class FastEsmForProteinFolding(EsmForProteinFolding):
905
+ """ESMFold with FastESM2 attention backends and opt-in experimental TTT.
906
 
907
  Inherits all folding logic (trunk, structure module, output_to_pdb, infer)
908
  from transformers.EsmForProteinFolding. Replaces the ESM2 backbone with
909
+ FastESM2 for optimized attention and adds opt-in TTT for difficult targets.
910
 
911
  Key API:
912
  result = model.fold_protein("MKTL...", ttt=True)
 
1293
  self,
1294
  sequence: str,
1295
  return_pdb_string: bool = True,
1296
+ ttt: bool = False,
1297
  ) -> Dict[str, Any]:
1298
+ """Fold a protein sequence.
1299
+
1300
+ Test-time training is disabled by default. Pass ``ttt=True`` or call
1301
+ ``fold_protein_ttt`` to opt in to experimental TTT.
1302
+
1303
+ Args:
1304
+ sequence: Protein sequence (single-letter amino acid codes)
1305
+ return_pdb_string: If True, include PDB string in output
1306
+ ttt: If True, run experimental LoRA TTT before returning the best fold
1307
+
1308
+ Returns:
1309
+ Dict with keys:
1310
+ - plddt: float, mean pLDDT
1311
+ - ptm: float, predicted TM-score
1312
+ - pdb_string: str (if return_pdb_string=True), PDB from best step
1313
+ - step_plddts: list[float], baseline pLDDT when TTT is disabled
1314
+ - best_step: int, 0 when TTT is disabled
1315
+ """
1316
+ if ttt:
1317
+ return self.fold_protein_ttt(
1318
+ sequence=sequence,
1319
+ return_pdb_string=return_pdb_string,
1320
+ )
1321
+ result = self._fold_single(sequence, return_pdb_string=return_pdb_string)
1322
+ return {
1323
+ "plddt": result["plddt"],
1324
+ "ptm": result["ptm"],
1325
+ "pdb_string": result.get("pdb_string"),
1326
+ "step_plddts": [result["plddt"]],
1327
+ "best_step": 0,
1328
+ }
1329
+
1330
+ def fold_protein_ttt(
1331
+ self,
1332
+ sequence: str,
1333
+ return_pdb_string: bool = True,
1334
+ ) -> Dict[str, Any]:
1335
+ """Fold a protein sequence with experimental test-time training.
1336
 
1337
  Runs TTT (masked language model adaptation via LoRA) for the configured
1338
  number of steps, folding after each optimizer step to track pLDDT. Returns