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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags: []
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Model Card for Model ID
|
| 7 |
+
|
| 8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Model Details
|
| 13 |
+
|
| 14 |
+
### Model Description
|
| 15 |
+
|
| 16 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
+
|
| 18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
| 19 |
+
|
| 20 |
+
- **Developed by:** [More Information Needed]
|
| 21 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
+
- **Model type:** [More Information Needed]
|
| 24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
+
- **License:** [More Information Needed]
|
| 26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
+
|
| 28 |
+
### Model Sources [optional]
|
| 29 |
+
|
| 30 |
+
<!-- Provide the basic links for the model. -->
|
| 31 |
+
|
| 32 |
+
- **Repository:** [More Information Needed]
|
| 33 |
+
- **Paper [optional]:** [More Information Needed]
|
| 34 |
+
- **Demo [optional]:** [More Information Needed]
|
| 35 |
+
|
| 36 |
+
## Uses
|
| 37 |
+
|
| 38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
+
|
| 44 |
+
[More Information Needed]
|
| 45 |
+
|
| 46 |
+
### Downstream Use [optional]
|
| 47 |
+
|
| 48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
+
|
| 50 |
+
[More Information Needed]
|
| 51 |
+
|
| 52 |
+
### Out-of-Scope Use
|
| 53 |
+
|
| 54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
+
|
| 56 |
+
[More Information Needed]
|
| 57 |
+
|
| 58 |
+
## Bias, Risks, and Limitations
|
| 59 |
+
|
| 60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
+
|
| 62 |
+
[More Information Needed]
|
| 63 |
+
|
| 64 |
+
### Recommendations
|
| 65 |
+
|
| 66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
+
|
| 68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
+
|
| 70 |
+
## How to Get Started with the Model
|
| 71 |
+
|
| 72 |
+
Use the code below to get started with the model.
|
| 73 |
+
|
| 74 |
+
[More Information Needed]
|
| 75 |
+
|
| 76 |
+
## Training Details
|
| 77 |
+
|
| 78 |
+
### Training Data
|
| 79 |
+
|
| 80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
+
|
| 82 |
+
[More Information Needed]
|
| 83 |
+
|
| 84 |
+
### Training Procedure
|
| 85 |
+
|
| 86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
+
|
| 88 |
+
#### Preprocessing [optional]
|
| 89 |
+
|
| 90 |
+
[More Information Needed]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
#### Training Hyperparameters
|
| 94 |
+
|
| 95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
+
|
| 97 |
+
#### Speeds, Sizes, Times [optional]
|
| 98 |
+
|
| 99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
+
|
| 101 |
+
[More Information Needed]
|
| 102 |
+
|
| 103 |
+
## Evaluation
|
| 104 |
+
|
| 105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
+
|
| 107 |
+
### Testing Data, Factors & Metrics
|
| 108 |
+
|
| 109 |
+
#### Testing Data
|
| 110 |
+
|
| 111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
+
|
| 113 |
+
[More Information Needed]
|
| 114 |
+
|
| 115 |
+
#### Factors
|
| 116 |
+
|
| 117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
+
|
| 119 |
+
[More Information Needed]
|
| 120 |
+
|
| 121 |
+
#### Metrics
|
| 122 |
+
|
| 123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
+
|
| 125 |
+
[More Information Needed]
|
| 126 |
+
|
| 127 |
+
### Results
|
| 128 |
+
|
| 129 |
+
[More Information Needed]
|
| 130 |
+
|
| 131 |
+
#### Summary
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
## Model Examination [optional]
|
| 136 |
+
|
| 137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 138 |
+
|
| 139 |
+
[More Information Needed]
|
| 140 |
+
|
| 141 |
+
## Environmental Impact
|
| 142 |
+
|
| 143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 144 |
+
|
| 145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 146 |
+
|
| 147 |
+
- **Hardware Type:** [More Information Needed]
|
| 148 |
+
- **Hours used:** [More Information Needed]
|
| 149 |
+
- **Cloud Provider:** [More Information Needed]
|
| 150 |
+
- **Compute Region:** [More Information Needed]
|
| 151 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 152 |
+
|
| 153 |
+
## Technical Specifications [optional]
|
| 154 |
+
|
| 155 |
+
### Model Architecture and Objective
|
| 156 |
+
|
| 157 |
+
[More Information Needed]
|
| 158 |
+
|
| 159 |
+
### Compute Infrastructure
|
| 160 |
+
|
| 161 |
+
[More Information Needed]
|
| 162 |
+
|
| 163 |
+
#### Hardware
|
| 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]
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": "<bos>",
|
| 3 |
+
"eos_token": "<eos>",
|
| 4 |
+
"mask_token": "<mask>",
|
| 5 |
+
"pad_token": "<pad>",
|
| 6 |
+
"unk_token": "<unk>"
|
| 7 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"version": "1.0",
|
| 3 |
+
"truncation": null,
|
| 4 |
+
"padding": null,
|
| 5 |
+
"added_tokens": [
|
| 6 |
+
{
|
| 7 |
+
"id": 0,
|
| 8 |
+
"content": "<pad>",
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"lstrip": false,
|
| 11 |
+
"rstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"special": true
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"id": 1,
|
| 17 |
+
"content": "<unk>",
|
| 18 |
+
"single_word": false,
|
| 19 |
+
"lstrip": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"normalized": false,
|
| 22 |
+
"special": true
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"id": 2,
|
| 26 |
+
"content": "<mask>",
|
| 27 |
+
"single_word": false,
|
| 28 |
+
"lstrip": false,
|
| 29 |
+
"rstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"special": true
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"id": 3,
|
| 35 |
+
"content": "<bos>",
|
| 36 |
+
"single_word": false,
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"rstrip": false,
|
| 39 |
+
"normalized": false,
|
| 40 |
+
"special": true
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"id": 4,
|
| 44 |
+
"content": "<eos>",
|
| 45 |
+
"single_word": false,
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"rstrip": false,
|
| 48 |
+
"normalized": false,
|
| 49 |
+
"special": true
|
| 50 |
+
}
|
| 51 |
+
],
|
| 52 |
+
"normalizer": null,
|
| 53 |
+
"pre_tokenizer": {
|
| 54 |
+
"type": "Split",
|
| 55 |
+
"pattern": {
|
| 56 |
+
"String": ""
|
| 57 |
+
},
|
| 58 |
+
"behavior": "Removed",
|
| 59 |
+
"invert": false
|
| 60 |
+
},
|
| 61 |
+
"post_processor": null,
|
| 62 |
+
"decoder": null,
|
| 63 |
+
"model": {
|
| 64 |
+
"type": "WordPiece",
|
| 65 |
+
"unk_token": "<unk>",
|
| 66 |
+
"continuing_subword_prefix": "##",
|
| 67 |
+
"max_input_chars_per_word": 100,
|
| 68 |
+
"vocab": {
|
| 69 |
+
"<pad>": 0,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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from typing import List, Optional, Union, Dict
|
| 3 |
+
from torch import Tensor
|
| 4 |
+
|
| 5 |
+
from itertools import compress
|
| 6 |
+
|
| 7 |
+
# HuggingFace
|
| 8 |
+
from tokenizers import Tokenizer
|
| 9 |
+
from transformers import PreTrainedTokenizerFast, BatchEncoding
|
| 10 |
+
from tokenizers.models import WordPiece
|
| 11 |
+
from tokenizers.pre_tokenizers import Split
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
VOCAB = {
|
| 15 |
+
"<pad>": 0,
|
| 16 |
+
"<unk>": 1,
|
| 17 |
+
"<mask>": 2,
|
| 18 |
+
"<bos>": 3,
|
| 19 |
+
"<eos>": 4,
|
| 20 |
+
"|": 5,
|
| 21 |
+
"X": 6,
|
| 22 |
+
"B": 7,
|
| 23 |
+
"O": 8,
|
| 24 |
+
"U": 9,
|
| 25 |
+
"Z": 10,
|
| 26 |
+
"J": 11,
|
| 27 |
+
"L": 12,
|
| 28 |
+
"A": 13,
|
| 29 |
+
"G": 14,
|
| 30 |
+
"V": 15,
|
| 31 |
+
"S": 16,
|
| 32 |
+
"E": 17,
|
| 33 |
+
"R": 18,
|
| 34 |
+
"T": 19,
|
| 35 |
+
"I": 20,
|
| 36 |
+
"D": 21,
|
| 37 |
+
"P": 22,
|
| 38 |
+
"K": 23,
|
| 39 |
+
"Q": 24,
|
| 40 |
+
"N": 25,
|
| 41 |
+
"F": 26,
|
| 42 |
+
"Y": 27,
|
| 43 |
+
"M": 28,
|
| 44 |
+
"H": 29,
|
| 45 |
+
"W": 30,
|
| 46 |
+
"C": 31,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class ProteinTokenizer(PreTrainedTokenizerFast):
|
| 51 |
+
|
| 52 |
+
def __init__(
|
| 53 |
+
self,
|
| 54 |
+
pad_token_id: int,
|
| 55 |
+
mask_token_id: int,
|
| 56 |
+
bos_token_id: int,
|
| 57 |
+
eos_token_id: int,
|
| 58 |
+
unk_token_id: int,
|
| 59 |
+
max_length: int,
|
| 60 |
+
other_special_token_ids: Optional[List[int]] = None,
|
| 61 |
+
ambiguous_token_ids: Optional[List[int]] = None, # str = "XBOUZJ"
|
| 62 |
+
**kwargs,
|
| 63 |
+
):
|
| 64 |
+
"""Vocabulary comprising the amino acids, and the special tokens <unk>, <bos>, <eos>, <pad> and <mask>.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
vocab_path (str): Path to the vocabulary file to load.
|
| 68 |
+
pad_token_id (int): <PAD> token index.
|
| 69 |
+
mask_token_id (int): <MASK> token index.
|
| 70 |
+
bos_token_id (int): <BOS> token index.
|
| 71 |
+
eos_token_id (int): <EOS> token index.
|
| 72 |
+
unk_token_id (int): <UNK> token index.
|
| 73 |
+
other_special_token_ids (Optional[List[int]]): List of additional special tokens.
|
| 74 |
+
"""
|
| 75 |
+
# Create vocabulary with special tokens
|
| 76 |
+
token_to_id = dict()
|
| 77 |
+
id_to_token = dict()
|
| 78 |
+
|
| 79 |
+
for token, token_id in VOCAB.items():
|
| 80 |
+
token = token.strip()
|
| 81 |
+
token_to_id[token] = token_id
|
| 82 |
+
id_to_token[token_id] = token
|
| 83 |
+
|
| 84 |
+
# Define tokenizer and model
|
| 85 |
+
tokenizer_object = Tokenizer(WordPiece(vocab=token_to_id, unk_token=id_to_token.get(unk_token_id)))
|
| 86 |
+
|
| 87 |
+
# Pretokenize by splitting every character
|
| 88 |
+
tokenizer_object.pre_tokenizer = Split("", behavior="removed")
|
| 89 |
+
|
| 90 |
+
super().__init__(
|
| 91 |
+
pad_token_id=pad_token_id,
|
| 92 |
+
mask_token_id=mask_token_id,
|
| 93 |
+
bos_token_id=bos_token_id,
|
| 94 |
+
eos_token_id=eos_token_id,
|
| 95 |
+
unk_token_id=unk_token_id,
|
| 96 |
+
pad_token=id_to_token.get(pad_token_id),
|
| 97 |
+
bos_token=id_to_token.get(bos_token_id),
|
| 98 |
+
eos_token=id_to_token.get(eos_token_id),
|
| 99 |
+
unk_token=id_to_token.get(unk_token_id),
|
| 100 |
+
mask_token=id_to_token.get(mask_token_id),
|
| 101 |
+
max_length=max_length,
|
| 102 |
+
ambiguous_token_ids=ambiguous_token_ids,
|
| 103 |
+
model_max_length=max_length,
|
| 104 |
+
padding_side="right",
|
| 105 |
+
truncation_side="right",
|
| 106 |
+
model_input_names=["input_ids", "attention_mask", "special_tokens_mask"],
|
| 107 |
+
tokenizer_object=tokenizer_object,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if other_special_token_ids is not None:
|
| 111 |
+
self.add_special_tokens({"additional_special_tokens": list(id_to_token.get(i) for i in other_special_token_ids)})
|
| 112 |
+
|
| 113 |
+
self.ambiguous_token_ids = ambiguous_token_ids
|
| 114 |
+
|
| 115 |
+
self.key_to_padding = {"input_ids": self.pad_token_id, "attention_mask": 0, "special_tokens_mask": 1, "position_ids": 0}
|
| 116 |
+
self.key_to_dtype = {
|
| 117 |
+
"input_ids": torch.long,
|
| 118 |
+
"attention_mask": torch.bool,
|
| 119 |
+
"special_tokens_mask": torch.bool,
|
| 120 |
+
"position_ids": torch.int,
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
def truncate(
|
| 124 |
+
self,
|
| 125 |
+
encoded_inputs: Dict[str, List[int]],
|
| 126 |
+
max_length: Optional[int] = None,
|
| 127 |
+
random_truncate: bool = True,
|
| 128 |
+
) -> Dict[str, List[List[int]]]:
|
| 129 |
+
"""
|
| 130 |
+
Randomly truncate sequences in encoded inputs to the specified maximum length.
|
| 131 |
+
|
| 132 |
+
Args:
|
| 133 |
+
encoded_inputs (BatchEncoding): Tokenized inputs with keys like 'input_ids' as tensors.
|
| 134 |
+
max_length (Optional[int]): Maximum length for truncation. Defaults to model's max length if None.
|
| 135 |
+
random_truncate (bool): Whether to randomly truncate sequences.
|
| 136 |
+
|
| 137 |
+
Returns:
|
| 138 |
+
Dict[str, List[List[int]]]: Randomly truncated tokenized inputs.
|
| 139 |
+
"""
|
| 140 |
+
|
| 141 |
+
for i, sequence in enumerate(encoded_inputs["input_ids"]):
|
| 142 |
+
if len(sequence) > max_length:
|
| 143 |
+
if random_truncate:
|
| 144 |
+
offset = torch.randint(0, len(sequence) - max_length + 1, (1,)).item()
|
| 145 |
+
else:
|
| 146 |
+
offset = 0
|
| 147 |
+
for key in encoded_inputs:
|
| 148 |
+
encoded_inputs[key][i] = encoded_inputs[key][i][offset : offset + max_length]
|
| 149 |
+
|
| 150 |
+
# add option for different random truncate
|
| 151 |
+
|
| 152 |
+
return encoded_inputs
|
| 153 |
+
|
| 154 |
+
def remove_ambiguous(self, encoded_inputs: Dict[str, List[int]]) -> Dict[str, List[List[int]]]:
|
| 155 |
+
"""
|
| 156 |
+
Remove ambiguous amino acids from the input sequences.
|
| 157 |
+
|
| 158 |
+
Args:
|
| 159 |
+
encoded_inputs (BatchEncoding): Tokenized inputs with keys like 'input_ids' as tensors.
|
| 160 |
+
|
| 161 |
+
Returns:
|
| 162 |
+
Dict[str, List[List[int]]]: Tokenized inputs without ambiguous amino acids.
|
| 163 |
+
"""
|
| 164 |
+
filtered_inputs = {key: [] for key in encoded_inputs}
|
| 165 |
+
|
| 166 |
+
for i, sequence in enumerate(encoded_inputs["input_ids"]):
|
| 167 |
+
mask = [token not in self.ambiguous_token_ids for token in sequence]
|
| 168 |
+
|
| 169 |
+
# Drop the sequence entirely if there is only ambiguous tokens
|
| 170 |
+
if not any(mask):
|
| 171 |
+
continue
|
| 172 |
+
|
| 173 |
+
# Otherwise remove only the ambiguous tokens
|
| 174 |
+
for key in encoded_inputs:
|
| 175 |
+
filtered_inputs[key].append(list(compress(encoded_inputs[key][i], mask)))
|
| 176 |
+
|
| 177 |
+
return filtered_inputs
|
| 178 |
+
|
| 179 |
+
def _pad(
|
| 180 |
+
self,
|
| 181 |
+
encoded_inputs: Dict[str, List[List[int]]],
|
| 182 |
+
padding: Union[bool, str] = True,
|
| 183 |
+
max_length: Optional[int] = None,
|
| 184 |
+
pad_to_multiple_of: int = 8,
|
| 185 |
+
**kwargs,
|
| 186 |
+
) -> Dict[str, List[List[int]]]:
|
| 187 |
+
"""
|
| 188 |
+
Remove ambiguous amino acids from the input sequences.
|
| 189 |
+
|
| 190 |
+
Args:
|
| 191 |
+
encoded_inputs (Dict[str, List[List[int]]): Tokenized inputs with keys like 'input_ids' as tensors.
|
| 192 |
+
|
| 193 |
+
Returns:
|
| 194 |
+
Dict[str, List[List[int]]]: Tokenized inputs without ambiguous amino acids.
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
if isinstance(encoded_inputs, list):
|
| 198 |
+
tmp = dict()
|
| 199 |
+
for key in encoded_inputs[0]:
|
| 200 |
+
tmp[key] = [encoded_inputs[i][key] for i in range(len(encoded_inputs))]
|
| 201 |
+
encoded_inputs = tmp
|
| 202 |
+
|
| 203 |
+
if max_length is None:
|
| 204 |
+
max_length = self.model_max_length
|
| 205 |
+
|
| 206 |
+
sequence_lengths = [len(sequence) for sequence in encoded_inputs["input_ids"]]
|
| 207 |
+
if padding == "longest" or padding == True:
|
| 208 |
+
max_length = min(max_length, max(sequence_lengths))
|
| 209 |
+
|
| 210 |
+
if pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
|
| 211 |
+
max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
|
| 212 |
+
|
| 213 |
+
for i, seq_len in enumerate(sequence_lengths):
|
| 214 |
+
if seq_len < max_length:
|
| 215 |
+
for key in encoded_inputs:
|
| 216 |
+
encoded_inputs[key][i] = encoded_inputs[key][i] + [self.key_to_padding[key]] * (max_length - seq_len)
|
| 217 |
+
|
| 218 |
+
return encoded_inputs
|
| 219 |
+
|
| 220 |
+
def pad(
|
| 221 |
+
self,
|
| 222 |
+
encoded_inputs: Dict[str, List[List[int]]],
|
| 223 |
+
padding: Union[bool, str] = True,
|
| 224 |
+
max_length: Optional[int] = None,
|
| 225 |
+
pad_to_multiple_of: int = 8,
|
| 226 |
+
return_tensors: str = "pt",
|
| 227 |
+
**kwargs,
|
| 228 |
+
) -> Dict[str, List[List[int]]]:
|
| 229 |
+
"""
|
| 230 |
+
Remove ambiguous amino acids from the input sequences.
|
| 231 |
+
|
| 232 |
+
Args:
|
| 233 |
+
encoded_inputs (Dict[str, List[List[int]]): Tokenized inputs with keys like 'input_ids' as tensors.
|
| 234 |
+
|
| 235 |
+
Returns:
|
| 236 |
+
Dict[str, List[List[int]]]: Tokenized inputs without ambiguous amino acids.
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
encoded_inputs = self._pad(
|
| 240 |
+
encoded_inputs,
|
| 241 |
+
padding,
|
| 242 |
+
max_length,
|
| 243 |
+
pad_to_multiple_of,
|
| 244 |
+
**kwargs,
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
if return_tensors is not None:
|
| 248 |
+
return BatchEncoding(encoded_inputs, tensor_type=return_tensors)
|
| 249 |
+
|
| 250 |
+
return encoded_inputs
|
| 251 |
+
|
| 252 |
+
def __call__(
|
| 253 |
+
self,
|
| 254 |
+
text: str | List[str],
|
| 255 |
+
max_length: Optional[int] = None,
|
| 256 |
+
padding: Union[bool, str] = False,
|
| 257 |
+
truncation: bool = False,
|
| 258 |
+
random_truncate: bool = True,
|
| 259 |
+
remove_ambiguous: bool = False,
|
| 260 |
+
return_special_tokens_mask: bool = True,
|
| 261 |
+
return_tensors: str = None,
|
| 262 |
+
**kwargs,
|
| 263 |
+
) -> Dict[str, Tensor]:
|
| 264 |
+
|
| 265 |
+
if isinstance(text, str):
|
| 266 |
+
encoded_inputs = self.__call__(
|
| 267 |
+
[text],
|
| 268 |
+
max_length,
|
| 269 |
+
padding,
|
| 270 |
+
truncation,
|
| 271 |
+
random_truncate,
|
| 272 |
+
remove_ambiguous,
|
| 273 |
+
return_special_tokens_mask,
|
| 274 |
+
return_tensors,
|
| 275 |
+
)
|
| 276 |
+
for key in encoded_inputs:
|
| 277 |
+
encoded_inputs[key] = encoded_inputs[key][0]
|
| 278 |
+
return encoded_inputs
|
| 279 |
+
|
| 280 |
+
# Tokenize without truncation or padding
|
| 281 |
+
encoded_inputs = super().__call__(
|
| 282 |
+
text,
|
| 283 |
+
padding=False,
|
| 284 |
+
truncation=False,
|
| 285 |
+
return_special_tokens_mask=return_special_tokens_mask,
|
| 286 |
+
**kwargs,
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
if max_length is None:
|
| 290 |
+
max_length = self.model_max_length
|
| 291 |
+
|
| 292 |
+
# Truncate
|
| 293 |
+
if truncation:
|
| 294 |
+
encoded_inputs = self.truncate(
|
| 295 |
+
encoded_inputs,
|
| 296 |
+
max_length=max_length,
|
| 297 |
+
random_truncate=random_truncate,
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
## NOTE: Moved this to after truncation to avoid the offset when random truncation is used
|
| 301 |
+
# Track original position indexes
|
| 302 |
+
encoded_inputs["position_ids"] = [list(range(len(seq))) for seq in encoded_inputs["input_ids"]]
|
| 303 |
+
|
| 304 |
+
# Remove ambiguous amino acids
|
| 305 |
+
if remove_ambiguous and self.ambiguous_token_ids is not None:
|
| 306 |
+
encoded_inputs = self.remove_ambiguous(encoded_inputs)
|
| 307 |
+
|
| 308 |
+
# Add padding
|
| 309 |
+
if padding:
|
| 310 |
+
encoded_inputs = self._pad(encoded_inputs, max_length=max_length, return_tensors=return_tensors)
|
| 311 |
+
|
| 312 |
+
if return_tensors is not None:
|
| 313 |
+
return BatchEncoding(encoded_inputs, tensor_type=return_tensors)
|
| 314 |
+
|
| 315 |
+
return encoded_inputs
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "<pad>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"1": {
|
| 12 |
+
"content": "<unk>",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"2": {
|
| 20 |
+
"content": "<mask>",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"3": {
|
| 28 |
+
"content": "<bos>",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"4": {
|
| 36 |
+
"content": "<eos>",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"ambiguous_token_ids": [
|
| 45 |
+
1,
|
| 46 |
+
6,
|
| 47 |
+
7,
|
| 48 |
+
8,
|
| 49 |
+
9,
|
| 50 |
+
10,
|
| 51 |
+
11
|
| 52 |
+
],
|
| 53 |
+
"auto_map": {
|
| 54 |
+
"AutoTokenizer": [
|
| 55 |
+
"tokenizer.ProteinTokenizer",
|
| 56 |
+
null
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
"bos_token": "<bos>",
|
| 60 |
+
"bos_token_id": 3,
|
| 61 |
+
"clean_up_tokenization_spaces": false,
|
| 62 |
+
"eos_token": "<eos>",
|
| 63 |
+
"eos_token_id": 4,
|
| 64 |
+
"extra_special_tokens": {},
|
| 65 |
+
"mask_token": "<mask>",
|
| 66 |
+
"mask_token_id": 2,
|
| 67 |
+
"max_length": 2048,
|
| 68 |
+
"model_input_names": [
|
| 69 |
+
"input_ids",
|
| 70 |
+
"attention_mask",
|
| 71 |
+
"special_tokens_mask"
|
| 72 |
+
],
|
| 73 |
+
"model_max_length": 2048,
|
| 74 |
+
"pad_token": "<pad>",
|
| 75 |
+
"pad_token_id": 0,
|
| 76 |
+
"padding_side": "right",
|
| 77 |
+
"tokenizer_class": "ProteinTokenizer",
|
| 78 |
+
"truncation_side": "right",
|
| 79 |
+
"unk_token": "<unk>",
|
| 80 |
+
"unk_token_id": 1
|
| 81 |
+
}
|