Upload AMPLIFY
Browse files- amplify.py +1 -1
- tokenizer.py +133 -0
amplify.py
CHANGED
|
@@ -11,7 +11,7 @@ from xformers.ops import SwiGLU, memory_efficient_attention
|
|
| 11 |
|
| 12 |
from .rmsnorm import RMSNorm
|
| 13 |
from .rotary import precompute_freqs_cis, apply_rotary_emb
|
| 14 |
-
from
|
| 15 |
|
| 16 |
from transformers import PreTrainedModel, PretrainedConfig
|
| 17 |
from transformers.modeling_outputs import MaskedLMOutput
|
|
|
|
| 11 |
|
| 12 |
from .rmsnorm import RMSNorm
|
| 13 |
from .rotary import precompute_freqs_cis, apply_rotary_emb
|
| 14 |
+
from .tokenizer import ProteinTokenizer
|
| 15 |
|
| 16 |
from transformers import PreTrainedModel, PretrainedConfig
|
| 17 |
from transformers.modeling_outputs import MaskedLMOutput
|
tokenizer.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from typing import List, Optional, Union
|
| 3 |
+
from torch import Tensor
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ProteinTokenizer(object):
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
vocab_path: str,
|
| 10 |
+
pad_token_id: int,
|
| 11 |
+
mask_token_id: int,
|
| 12 |
+
bos_token_id: int,
|
| 13 |
+
eos_token_id: int,
|
| 14 |
+
unk_token_id: int,
|
| 15 |
+
other_special_token_ids: Optional[List[int]],
|
| 16 |
+
**kwargs,
|
| 17 |
+
):
|
| 18 |
+
"""Vocabulary comprising the amino acids, and the special tokens <unk>, <bos>, <eos>, <pad> and <mask>.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
vocab_path (str): Path to the vocabulary file to load.
|
| 22 |
+
pad_token_id (int): <PAD> token index.
|
| 23 |
+
mask_token_id (int): <MASK> token index.
|
| 24 |
+
bos_token_id (int): <BOS> token index.
|
| 25 |
+
eos_token_id (int): <EOS> token index.
|
| 26 |
+
unk_token_id (int): <UNK> token index.
|
| 27 |
+
other_special_token_ids (Optional[List[int]]): List of additional special tokens.
|
| 28 |
+
"""
|
| 29 |
+
self._token_to_id = dict()
|
| 30 |
+
self._id_to_token = dict()
|
| 31 |
+
|
| 32 |
+
with open(vocab_path, "r") as vocab_file:
|
| 33 |
+
for i, token in enumerate(vocab_file):
|
| 34 |
+
token = token.strip()
|
| 35 |
+
self._token_to_id[token] = i
|
| 36 |
+
self._id_to_token[i] = token
|
| 37 |
+
|
| 38 |
+
# Padding token
|
| 39 |
+
self.pad_token_id = pad_token_id
|
| 40 |
+
self.pad_token = self._token_to_id.get(pad_token_id)
|
| 41 |
+
|
| 42 |
+
# Beginning and end of sequence
|
| 43 |
+
self.bos_token_id = bos_token_id
|
| 44 |
+
self.eos_token_id = eos_token_id
|
| 45 |
+
self.bos_token = self._token_to_id.get(bos_token_id)
|
| 46 |
+
self.eos_token = self._token_to_id.get(eos_token_id)
|
| 47 |
+
|
| 48 |
+
# Mask token
|
| 49 |
+
self.mask_token_id = mask_token_id
|
| 50 |
+
self.mask_token = self._token_to_id.get(mask_token_id)
|
| 51 |
+
|
| 52 |
+
# Unknown token
|
| 53 |
+
self.unk_token_id = unk_token_id
|
| 54 |
+
self.unk_token = self._id_to_token.get(unk_token_id)
|
| 55 |
+
|
| 56 |
+
# Set of all special token indices
|
| 57 |
+
self.special_token_ids = set()
|
| 58 |
+
self.special_token_ids.add(pad_token_id)
|
| 59 |
+
self.special_token_ids.add(mask_token_id)
|
| 60 |
+
self.special_token_ids.add(bos_token_id)
|
| 61 |
+
self.special_token_ids.add(eos_token_id)
|
| 62 |
+
self.special_token_ids.add(unk_token_id)
|
| 63 |
+
if other_special_token_ids is not None:
|
| 64 |
+
self.special_token_ids.update(other_special_token_ids)
|
| 65 |
+
|
| 66 |
+
def __len__(self) -> int:
|
| 67 |
+
return len(self._token_to_id)
|
| 68 |
+
|
| 69 |
+
def token_to_id(self, token: str) -> int:
|
| 70 |
+
return self._token_to_id.get(token, self.unk_token_id)
|
| 71 |
+
|
| 72 |
+
def id_to_token(self, index: int) -> str:
|
| 73 |
+
return self._id_to_token.get(index, self.unk_token)
|
| 74 |
+
|
| 75 |
+
def encode(
|
| 76 |
+
self,
|
| 77 |
+
tokens: List[str],
|
| 78 |
+
max_length: Optional[int] = None,
|
| 79 |
+
add_special_tokens: bool = True,
|
| 80 |
+
random_truncate: bool = True,
|
| 81 |
+
**kwargs,
|
| 82 |
+
) -> Union[List[int], Tensor]:
|
| 83 |
+
"""Encodes a list of tokens into a list or tensor of token indices.
|
| 84 |
+
|
| 85 |
+
Args:
|
| 86 |
+
tokens (List[str]): Sequence of tokens to encode.
|
| 87 |
+
max_length (Optional[int], optional): Truncate the sequence to the specified length. Defaults to None.
|
| 88 |
+
add_special_tokens (bool, optional): Add special tokens <bos> and <eos> at the start and end.. Defaults to True.
|
| 89 |
+
random_truncate (bool, optional): Truncate the sequence to a random subsequence of if longer than truncate.
|
| 90 |
+
Defaults to True.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
Union[List[int], Tensor]: Token indices.
|
| 94 |
+
"""
|
| 95 |
+
token_ids = list(map(self.token_to_id, tokens))
|
| 96 |
+
if add_special_tokens:
|
| 97 |
+
token_ids = [self.bos_token_id] + token_ids + [self.eos_token_id]
|
| 98 |
+
if max_length is not None and max_length < len(token_ids):
|
| 99 |
+
if random_truncate:
|
| 100 |
+
offset = int(torch.randint(0, len(token_ids) - max_length, (1,)).item())
|
| 101 |
+
else:
|
| 102 |
+
offset = 0
|
| 103 |
+
token_ids = token_ids[offset : offset + max_length]
|
| 104 |
+
return torch.as_tensor(token_ids, dtype=torch.long)
|
| 105 |
+
|
| 106 |
+
def decode(
|
| 107 |
+
self,
|
| 108 |
+
token_ids: List[int],
|
| 109 |
+
skip_special_tokens: bool = True,
|
| 110 |
+
**kwargs,
|
| 111 |
+
) -> Union[List[str], str]:
|
| 112 |
+
"""Decodes a list or tensor of token ids into a list or string of tokens.
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
token_ids (List[int]): Token indices to decode.
|
| 116 |
+
skip_special_tokens (bool, optional): Skip the special tokens <bos> and <eos> at the start and end.
|
| 117 |
+
Defaults to True.
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
Union[List[str], str]: Protein.
|
| 121 |
+
"""
|
| 122 |
+
if torch.is_tensor(token_ids):
|
| 123 |
+
token_ids = token_ids.tolist()
|
| 124 |
+
|
| 125 |
+
if skip_special_tokens:
|
| 126 |
+
if len(token_ids) > 0 and token_ids[0] in self.special_token_ids:
|
| 127 |
+
token_ids = token_ids[1:]
|
| 128 |
+
if len(token_ids) > 0 and token_ids[-1] in self.special_token_ids:
|
| 129 |
+
token_ids = token_ids[:-1]
|
| 130 |
+
|
| 131 |
+
tokens = " ".join(map(self.id_to_token, token_ids))
|
| 132 |
+
|
| 133 |
+
return tokens
|