Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,199 +1,164 @@
|
|
| 1 |
-
---
|
| 2 |
-
library_name: transformers
|
| 3 |
-
tags: []
|
| 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 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
-
|
| 175 |
-
**BibTeX:**
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# NOTE
|
| 7 |
+
The GitHub with the implementation and requirements.txt can be found [here](https://github.com/Synthyra/FastPLMs.git)
|
| 8 |
+
|
| 9 |
+
# Profluent-E1
|
| 10 |
+
[Profluent-E1](https://github.com/Synthyra/ESMplusplus) is a faithful implementation of Profluent's models [E1](https://www.profluent.bio/showcase/e1) ([license](https://github.com/Profluent-AI/E1/tree/main?tab=License-1-ov-file)) that integrates Huggingface AutoModel compatability and nice embedding functionality built in.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
## Use with 🤗 transformers
|
| 14 |
+
### Supported models
|
| 15 |
+
```python
|
| 16 |
+
model_dict = {
|
| 17 |
+
# Synthyra/Profluent-E1_150M
|
| 18 |
+
'Profluent-E1-150M': 'Profluent-Bio/E1-150m',
|
| 19 |
+
# Synthyra/Profluent-E1_150M
|
| 20 |
+
'Profluent-E1-300M': 'Profluent-Bio/E1-300m',
|
| 21 |
+
# Synthyra/Profluent-E1_150M
|
| 22 |
+
'Profluent-E1-600M': 'Profluent-Bio/E1-600m',
|
| 23 |
+
}
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
```python
|
| 27 |
+
import torch
|
| 28 |
+
from transformers import AutoModelForMaskedLM
|
| 29 |
+
|
| 30 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
+
model = AutoModelForMaskedLM.from_pretrained('Synthyra/Profluent-E1-150M', trust_remote_code=True, dtype=torch.bfloat16).eval().to(device)
|
| 32 |
+
|
| 33 |
+
sequences = ['MPRTEIN', 'MSEQWENCE']
|
| 34 |
+
batch = model.prep_tokens.get_batch_kwargs(sequences, device=device)
|
| 35 |
+
|
| 36 |
+
output = model(**batch) # get all hidden states with output_hidden_states=True
|
| 37 |
+
print(output.logits.shape) # language modeling logits, (batch_size, seq_len, vocab_size), (2, 11, 34)
|
| 38 |
+
print(output.last_hidden_state.shape) # last hidden state of the model, (batch_size, seq_len, hidden_size), (2, 11, 768)
|
| 39 |
+
print(output.loss) # language modeling loss if you passed labels
|
| 40 |
+
#print(output.hidden_states) # all hidden states if you passed output_hidden_states=True (in tuple)
|
| 41 |
+
#print(outout.attentions) # all attention matrices if you passed output_attentions=True (in tuple)
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
Our E1 implementation also supports sequence and token level classification tasks like ESM2. Simply pass the number of labels during initialization.
|
| 45 |
+
|
| 46 |
+
```python
|
| 47 |
+
from transformers import AutoModelForSequenceClassification, AutoModelForTokenClassification
|
| 48 |
+
|
| 49 |
+
model = AutoModelForSequenceClassification.from_pretrained('Synthyra/Profluent-E1-150M', num_labels=2, trust_remote_code=True)
|
| 50 |
+
logits = model(**batch, labels=labels).logits
|
| 51 |
+
print(logits.shape) # (batch_size, num_labels), (2, 2)
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
E1 weights were trained in bf16 and are in bf16 by default. You can load them in the precision of your choosing by leveraging the dtype parameter:
|
| 55 |
+
```python
|
| 56 |
+
import torch
|
| 57 |
+
model = AutoModelForMaskedLM.from_pretrained('Synthyra/ESMplusplus_small', trust_remote_code=True, dtype=torch.float) # fp32
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
## Embed entire datasets with no new code
|
| 61 |
+
To embed a list of protein sequences **fast**, just call embed_dataset. Sequences are sorted to reduce padding tokens, so the initial progress bar estimation is usually much longer than the actual time it will take.
|
| 62 |
+
|
| 63 |
+
Example:
|
| 64 |
+
```python
|
| 65 |
+
embedding_dict = model.embed_dataset(
|
| 66 |
+
sequences=[
|
| 67 |
+
'MALWMRLLPLLALLALWGPDPAAA', ... # list of protein sequences
|
| 68 |
+
],
|
| 69 |
+
batch_size=2, # adjust for your GPU memory
|
| 70 |
+
max_len=512, # adjust for your needs
|
| 71 |
+
full_embeddings=False, # if True, no pooling is performed
|
| 72 |
+
embed_dtype=torch.float32, # cast to what dtype you want
|
| 73 |
+
pooling_types=['mean', 'cls'], # more than one pooling type will be concatenated together
|
| 74 |
+
sql=False, # if True, embeddings will be stored in SQLite database
|
| 75 |
+
sql_db_path='embeddings.db',
|
| 76 |
+
save=True, # if True, embeddings will be saved as a .pth file
|
| 77 |
+
save_path='embeddings.pth',
|
| 78 |
+
)
|
| 79 |
+
# embedding_dict is a dictionary mapping sequences to their embeddings as tensors for .pth or numpy arrays for sql
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
```
|
| 83 |
+
model.embed_dataset()
|
| 84 |
+
Args:
|
| 85 |
+
sequences: List of protein sequences
|
| 86 |
+
batch_size: Batch size for processing
|
| 87 |
+
max_len: Maximum sequence length
|
| 88 |
+
full_embeddings: Whether to return full residue-wise (True) embeddings or pooled (False)
|
| 89 |
+
pooling_type: Type of pooling ('mean' or 'cls')
|
| 90 |
+
sql: Whether to store embeddings in SQLite database - will be stored in float32
|
| 91 |
+
sql_db_path: Path to SQLite database
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
Dictionary mapping sequences to embeddings, or None if sql=True
|
| 95 |
+
|
| 96 |
+
Note:
|
| 97 |
+
- If sql=True, embeddings can only be stored in float32
|
| 98 |
+
- sql is ideal if you need to stream a very large dataset for training in real-time
|
| 99 |
+
- save=True is ideal if you can store the entire embedding dictionary in RAM
|
| 100 |
+
- sql will be used if it is True and save is True or False
|
| 101 |
+
- If your sql database or .pth file is already present, they will be scanned first for already embedded sequences
|
| 102 |
+
- Sequences will be truncated to max_len and sorted by length in descending order for faster processing
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
## Fine-tuning with 🤗 peft
|
| 106 |
+
```python
|
| 107 |
+
model = AutoModelForSequenceClassification.from_pretrained('Synthyra/Profluent-E1-150M', num_labels=2, trust_remote_code=True)
|
| 108 |
+
# these modules handle E1 attention layers
|
| 109 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"]
|
| 110 |
+
|
| 111 |
+
lora_config = LoraConfig(
|
| 112 |
+
r=8, # choose lora parameters to your liking
|
| 113 |
+
lora_alpha=16,
|
| 114 |
+
lora_dropout=0.01,
|
| 115 |
+
bias="none",
|
| 116 |
+
target_modules=target_modules,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Apply LoRA to the model
|
| 120 |
+
model = get_peft_model(model, lora_config)
|
| 121 |
+
|
| 122 |
+
# Unfreeze the classifier head
|
| 123 |
+
for param in model.classifier.parameters():
|
| 124 |
+
param.requires_grad = True
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
For a more thourough example of fine-tuning, check out our example script [here](https://github.com/Synthyra/FastPLMs/blob/main/fine_tuning_example.py).
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
## Returning attention maps
|
| 131 |
+
Usually F.scaled_dot_product_attention is used for the attention calculations, which is much faster than native PyTorch. However, it cannot return attention maps.
|
| 132 |
+
ESM++ has the option to ```output_attentions```, which will calculate attention manually. This is much slower, so do not use unless you need the attention maps.
|
| 133 |
+
|
| 134 |
+
```python
|
| 135 |
+
output = model(**tokenized, output_attentions=True)
|
| 136 |
+
att = output.attentions
|
| 137 |
+
len(att) # 30, one for each layer, size (batch_size, num_heads, seq_len, seq_len) each
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
### Citation
|
| 142 |
+
If you use any of this implementation or work please cite the following DOI and Profluent's paper.
|
| 143 |
+
|
| 144 |
+
```
|
| 145 |
+
@misc {FastPLMs,
|
| 146 |
+
author = { Hallee, Logan and Bichara, David and Gleghorn, Jason P.},
|
| 147 |
+
title = { FastPLMs: Fast, efficient, protien language model inference from Huggingface AutoModel.},
|
| 148 |
+
year = {2024},
|
| 149 |
+
url = { https://huggingface.co/Synthyra/ESMplusplus_small },
|
| 150 |
+
DOI = { 10.57967/hf/3726 },
|
| 151 |
+
publisher = { Hugging Face }
|
| 152 |
+
}
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
```
|
| 156 |
+
@article{Jain_Beazer_Ruffolo_Bhatnagar_Madani_2025, title={E1: Retrieval-Augmented Protein Encoder Models},
|
| 157 |
+
url={https://www.biorxiv.org/content/early/2025/11/13/2025.11.12.688125},
|
| 158 |
+
DOI={10.1101/2025.11.12.688125},
|
| 159 |
+
journal={bioRxiv},
|
| 160 |
+
publisher={Cold Spring Harbor Laboratory},
|
| 161 |
+
author={Jain, Sarthak and Beazer, Joel and Ruffolo, Jeffrey A and Bhatnagar, Aadyot and Madani, Ali},
|
| 162 |
+
year={2025}
|
| 163 |
+
}
|
| 164 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|