Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt for esmfold #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added benchmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added esmfold_benchmark.png
Copy link

@hmms117 hmms117 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Could you continue this plot to 1000aa or even 2000aa? Memory and runtime issues with default ESMFold only starts becoming an issue for very long proteins (without chunking) so the gains should be even more for >1000aa. Moreover, default ESMFold needs chunking to work for very large proteins, making faesmfold faster (add a plot line for esmfold with chunking?)
If you dont have the VMEM, I would be happy to run the benchmark.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After carefully check the memory usage, I found this improvement is likely a bug of the benchmark script😅. I mannuly checked a 560aa sequence. The raw version EsmFold used 16073 MB vmem, and replace the esm with faesm only reduced the memory consumption to 15986 MB (~0.5% smaller). I will try to modifiy the EsmFoldAttention see if can do better.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 23 additions & 2 deletions tests/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from transformers import EsmForMaskedLM, EsmTokenizer

from faesm.esm import FAEsmForMaskedLM
from tests.utils import generate_random_esm2_inputs
# from tests.utils import generate_random_esm2_inputs

# Set Seaborn theme and professional settings
sns.set_theme(style="white") # Remove grid by using "white"
Expand All @@ -27,6 +27,27 @@
}
)

def generate_random_esm2_inputs(
tokenizer, batch_size=3, min_seq_length=5, max_seq_length=10, device="cuda"
):
"""Generate random ESM2 model inputs."""
random_lengths = torch.randint(
min_seq_length, max_seq_length + 1, (batch_size,), device=device
)
random_tokens = [
torch.randint(low=4, high=29, size=(length,), device=device).tolist()
for length in random_lengths
]
sequences = ["".join(tokenizer.convert_ids_to_tokens(seq)) for seq in random_tokens]
esm_input = tokenizer.batch_encode_plus(
sequences,
add_special_tokens=True,
padding=True,
truncation=True,
return_tensors="pt",
)
esm_input = {k: v.to(device) for k, v in esm_input.items()}
return esm_input

def benchmark_torch_memory(f, *args, **kwargs):
torch.cuda.reset_peak_memory_stats()
Expand All @@ -51,7 +72,7 @@ def benchmark_inference_time(f, *args, **kwargs):
"facebook/esm2_t33_650M_UR50D",
8,
torch.float16,
[100, 200, 300, 400, 500, 600, 700, 800, 1000],
[100, 200, 300, 400, 500, 600, 700],
10,
)
],
Expand Down
12 changes: 7 additions & 5 deletions tests/benchmark_faesmfold_vs_esmfold.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,15 @@ def get_faesmfold(device):
[
(
torch.float16,
[100,200,300,400,500],
8,
[100,300,400],
10,
)
],
)
def test_esmfold_vs_faesmfold_benchmark(dtype, max_seq_lengths, repeats):
device = "cuda" if torch.cuda.is_available() else "cpu"

esmfold = EsmForProteinFolding.from_pretrained("facebook/esmfold_v1").to(device).eval()
esmfold.esm = esmfold.esm.to(dtype)
fa_esmfold = get_faesmfold(device)

esm_memory_usage, fa_esm_memory_usage = [], []
esm_inference_times, fa_esm_inference_times = [], []

Expand All @@ -91,6 +89,9 @@ def test_esmfold_vs_faesmfold_benchmark(dtype, max_seq_lengths, repeats):
esm_time_fold, fa_esm_time_fold = [], []

for _ in tqdm(range(repeats)):
esmfold = EsmForProteinFolding.from_pretrained("facebook/esmfold_v1").to(device).eval()
esmfold.esm = esmfold.esm.to(dtype)
fa_esmfold = get_faesmfold(device)

def esm_forward():

Expand All @@ -104,6 +105,7 @@ def esm_forward():
def fa_esm_forward():
fa_esmfold.esm.half()
fa_esmfold.infer_pdb(inputs)

fa_esmfold.to(device)
fa_esm_memory_fold.append(benchmark_torch_memory(fa_esm_forward))
fa_esm_time_fold.append(benchmark_inference_time(fa_esm_forward))
Expand Down