Skip to content

Commit ad93870

Browse files
committed
synced oss and fixed pyright issues
1 parent fdcf11c commit ad93870

57 files changed

Lines changed: 3994 additions & 795 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cookbook/local/open_generate.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"\n",
3939
"!pip install py3Dmol\n",
4040
"import py3Dmol\n",
41+
"\n",
4142
"from esm.models.esm3 import ESM3\n",
4243
"from esm.sdk.api import ESMProtein, GenerationConfig\n",
4344
"from esm.utils.structure.protein_chain import ProteinChain"

cookbook/local/raw_forwards.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
from esm.tokenization.function_tokenizer import (
1414
InterProQuantizedTokenizer as EsmFunctionTokenizer,
1515
)
16-
from esm.tokenization.sequence_tokenizer import (
17-
EsmSequenceTokenizer,
18-
)
16+
from esm.tokenization.sequence_tokenizer import EsmSequenceTokenizer
1917
from esm.utils.structure.protein_chain import ProteinChain
2018
from esm.utils.types import FunctionAnnotation
2119

cookbook/tutorials/1_esmprotein.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"outputs": [],
7373
"source": [
7474
"from biotite.database import rcsb\n",
75+
"\n",
7576
"from esm.sdk.api import ESMProtein\n",
7677
"from esm.utils.structure.protein_chain import ProteinChain\n",
7778
"from esm.utils.types import FunctionAnnotation\n",
@@ -496,9 +497,10 @@
496497
"# Functions for visualizing InterPro function annotations\n",
497498
"\n",
498499
"from dna_features_viewer import GraphicFeature, GraphicRecord\n",
499-
"from esm.utils.function.interpro import InterPro, InterProEntryType\n",
500500
"from matplotlib import colormaps\n",
501501
"\n",
502+
"from esm.utils.function.interpro import InterPro, InterProEntryType\n",
503+
"\n",
502504
"\n",
503505
"def visualize_function_annotations(\n",
504506
" annotations: list[FunctionAnnotation],\n",

cookbook/tutorials/3_gfp_design.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"import matplotlib.pyplot as pl\n",
6565
"import py3Dmol\n",
6666
"import torch\n",
67+
"\n",
6768
"from esm.sdk import client\n",
6869
"from esm.sdk.api import ESMProtein, GenerationConfig\n",
6970
"from esm.utils.structure.protein_chain import ProteinChain"

cookbook/tutorials/4_forge_generate.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"\n",
3737
"!pip install py3Dmol\n",
3838
"import py3Dmol\n",
39+
"\n",
3940
"from esm.sdk import client\n",
4041
"from esm.sdk.api import ESMProtein, GenerationConfig\n",
4142
"from esm.utils.structure.protein_chain import ProteinChain"

cookbook/tutorials/5_guided_generation.ipynb

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
"3. Minimize a biophysical energy function\n",
1515
"4. Use experimental screening data to guide designs with a regression model\n",
1616
"\n",
17-
"As long as your scoring function takes a protein as input and outputs a single score, you can use it to guide designs. To accomplish this, we use an implementation of derivative-free guidance inspired by Soft Value-Based Decoding described in [Li, et al 2024](https://arxiv.org/abs/2408.08252).\n",
17+
"As long as your scoring function takes a protein as input and outputs a single score, you can use it to guide designs. To accomplish this, we use an implementation of derivative-free guidance inspired by Soft Value-Based Decoding described in [Li, et al 2024](https://arxiv.org/abs/2408.08252) and constrained optimization using the Modified Differential Method of Multipliers from [Platt & Barr 1987](https://proceedings.neurips.cc/paper_files/paper/1987/file/a1126573153ad7e9f44ba80e99316482-Paper.pdf)\n",
1818
"\n",
1919
"In this notebook we will walk through a few examples to illustrate how to use guided generation. \n",
2020
"\n",
2121
"1. Guide towards high pTM for improved generation quality\n",
2222
"2. Generate a protein with no cysteine (C) residues\n",
23-
"3. Maximize protein globularity by minimizing the radius of gyration\n",
23+
"3. Maximize protein globularity by minimizing the radius of gyration, while keeping pTM high\n",
2424
"\n"
2525
]
2626
},
@@ -49,6 +49,7 @@
4949
"source": [
5050
"import biotite.structure as bs\n",
5151
"import py3Dmol\n",
52+
"\n",
5253
"from esm.sdk.api import ESMProtein, GenerationConfig\n",
5354
"from esm.sdk.experimental import ESM3GuidedDecoding, GuidedDecodingScoringFunction"
5455
]
@@ -269,6 +270,11 @@
269270
"metadata": {},
270271
"outputs": [],
271272
"source": [
273+
"# Start from a fully masked protein\n",
274+
"PROTEIN_LENGTH = 256\n",
275+
"starting_protein = ESMProtein(sequence=\"_\" * PROTEIN_LENGTH)\n",
276+
"\n",
277+
"# Call guided_generate\n",
272278
"no_cysteine_protein = no_cysteine_guided_decoding.guided_generate(\n",
273279
" protein=starting_protein,\n",
274280
" num_decoding_steps=len(starting_protein) // 8,\n",
@@ -302,7 +308,20 @@
302308
"source": [
303309
"## Maximize Globularity\n",
304310
"\n",
305-
"We use the radius of gyration as a proxy to maximize globularity, we also encourage generations to have high pTM"
311+
"We use the radius of gyration as a proxy to maximize globularity, and we will also encourage generations to have high pTM by using constraints"
312+
]
313+
},
314+
{
315+
"cell_type": "code",
316+
"execution_count": null,
317+
"metadata": {},
318+
"outputs": [],
319+
"source": [
320+
"from esm.sdk.experimental import (\n",
321+
" ConstraintType,\n",
322+
" ESM3GuidedDecodingWithConstraints,\n",
323+
" GenerationConstraint,\n",
324+
")"
306325
]
307326
},
308327
{
@@ -313,12 +332,11 @@
313332
"source": [
314333
"class RadiousOfGyrationScoringFunction(GuidedDecodingScoringFunction):\n",
315334
" def __call__(self, protein: ESMProtein) -> float:\n",
335+
" # Use the negative radius of gyration as the score to maximize\n",
316336
" score = -1 * self.radius_of_gyration(protein)\n",
317337
"\n",
318-
" assert protein.ptm is not None, \"Protein must have pTM scores to be scored\"\n",
319-
" if protein.ptm < 0.5:\n",
320-
" # Penalize proteins with low pTM scores\n",
321-
" score = score * 2\n",
338+
" # Re-scale the score to be in a similar magnitude as pTM\n",
339+
" score = score / 100.0\n",
322340
"\n",
323341
" return score\n",
324342
"\n",
@@ -335,8 +353,19 @@
335353
"metadata": {},
336354
"outputs": [],
337355
"source": [
338-
"radius_guided_decoding = ESM3GuidedDecoding(\n",
339-
" client=model, scoring_function=RadiousOfGyrationScoringFunction()\n",
356+
"# Constrain generation to have pTM > 0.75\n",
357+
"ptm_constraint = GenerationConstraint(\n",
358+
" scoring_function=PTMScoringFunction(),\n",
359+
" constraint_type=ConstraintType.GREATER_EQUAL,\n",
360+
" value=0.75,\n",
361+
")\n",
362+
"\n",
363+
"radius_guided_decoding = ESM3GuidedDecodingWithConstraints(\n",
364+
" client=model,\n",
365+
" scoring_function=RadiousOfGyrationScoringFunction(),\n",
366+
" constraints=[ptm_constraint], # Add list of constraints\n",
367+
" damping=1.0, # Damping factor for the MMDM algorithm\n",
368+
" learning_rate=10.0, # Learning rate for the MMDM algorithm\n",
340369
")"
341370
]
342371
},
@@ -346,6 +375,11 @@
346375
"metadata": {},
347376
"outputs": [],
348377
"source": [
378+
"# Start from a fully masked protein\n",
379+
"PROTEIN_LENGTH = 256\n",
380+
"starting_protein = ESMProtein(sequence=\"_\" * PROTEIN_LENGTH)\n",
381+
"\n",
382+
"# Call guided_generate\n",
349383
"radius_guided_protein = radius_guided_decoding.guided_generate(\n",
350384
" protein=starting_protein,\n",
351385
" num_decoding_steps=len(starting_protein) // 8,\n",
@@ -359,11 +393,32 @@
359393
"metadata": {},
360394
"outputs": [],
361395
"source": [
396+
"# Visualize the trajectory of the constrained generation\n",
397+
"radius_guided_decoding.visualize_latest_trajectory()"
398+
]
399+
},
400+
{
401+
"cell_type": "code",
402+
"execution_count": null,
403+
"metadata": {},
404+
"outputs": [],
405+
"source": [
406+
"# Visualize the generated protein\n",
362407
"view = py3Dmol.view(width=800, height=400)\n",
363408
"view.addModel(radius_guided_protein.to_pdb_string(), \"pdb\")\n",
364409
"view.setStyle({\"cartoon\": {\"color\": \"spectrum\"}})\n",
365410
"view.zoomTo()"
366411
]
412+
},
413+
{
414+
"cell_type": "code",
415+
"execution_count": null,
416+
"metadata": {},
417+
"outputs": [],
418+
"source": [
419+
"# Check pTM\n",
420+
"radius_guided_protein.ptm"
421+
]
367422
}
368423
],
369424
"metadata": {

esm/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
__version__ = "3.2.1"
2-

esm/layers/attention.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
import torch.nn.functional as F
66
from torch import nn
77

8-
from esm.layers.rotary import (
9-
RotaryEmbedding,
10-
TritonRotaryEmbedding,
11-
)
8+
from esm.layers.rotary import RotaryEmbedding, TritonRotaryEmbedding
129

1310
try:
14-
from flash_attn import flash_attn_varlen_qkvpacked_func # type:ignore
15-
except ImportError:
16-
flash_attn_varlen_func = None
11+
from flash_attn import flash_attn_varlen_qkvpacked_func
12+
except (ImportError, RuntimeError):
13+
flash_attn_varlen_qkvpacked_func = None
1714

1815

1916
class MultiHeadAttention(nn.Module):
@@ -117,7 +114,7 @@ def forward(self, x, seq_id):
117114
)
118115
qkv_N3HD = self.rotary(qkv_N3HD, cu_seqlens, max_seqlen)
119116

120-
context_NHD = flash_attn_varlen_qkvpacked_func(
117+
context_NHD = flash_attn_varlen_qkvpacked_func( # type: ignore
121118
qkv_N3HD, cu_seqlens, max_seqlen, softmax_scale=self.d_head**-0.5
122119
)
123120
context_ND = einops.rearrange(context_NHD, "n h d -> n (h d)")

esm/layers/blocks.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
import torch.nn as nn
33
import torch.nn.functional as F
44

5-
from esm.layers.attention import (
6-
FlashMultiHeadAttention,
7-
MultiHeadAttention,
8-
)
9-
from esm.layers.geom_attention import (
10-
GeometricReasoningOriginalImpl,
11-
)
5+
from esm.layers.attention import FlashMultiHeadAttention, MultiHeadAttention
6+
from esm.layers.geom_attention import GeometricReasoningOriginalImpl
127
from esm.utils.structure.affine3d import Affine3D
138

149

esm/layers/structure_proj.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
import torch.nn as nn
33

44
from esm.utils.constants.physics import BB_COORDINATES
5-
from esm.utils.structure.affine3d import (
6-
Affine3D,
7-
RotationMatrix,
8-
)
5+
from esm.utils.structure.affine3d import Affine3D, RotationMatrix
96

107

118
class Dim6RotStructureHead(nn.Module):

0 commit comments

Comments
 (0)