File size: 14,282 Bytes
02e480f |
1 2 3 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
PATTERN = "(\[[^\]]+]|Br?|Cl?|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9])"
# Deep learning
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
# Transformers
from fast_transformers.attention import AttentionLayer
from fast_transformers.events import EventDispatcher, QKVEvent
from fast_transformers.transformers import TransformerEncoder, TransformerEncoderLayer
from fast_transformers.builders.base import BaseBuilder
from fast_transformers.builders.transformer_builders import BaseTransformerEncoderBuilder
from fast_transformers.builders.attention_builders import AttentionBuilder
from fast_transformers.feature_maps import GeneralizedRandomFeatures
from fast_transformers.masking import LengthMask
from transformers import BertTokenizer
# Data
import numpy as np
# Standard library
from functools import partial
import regex as re
import random
class MolTranBertTokenizer(BertTokenizer):
def __init__(self, vocab_file: str = '',
do_lower_case=False,
unk_token='<pad>',
sep_token='<eos>',
pad_token='<pad>',
cls_token='<bos>',
mask_token='<mask>',
**kwargs):
super().__init__(vocab_file,
unk_token=unk_token,
sep_token=sep_token,
pad_token=pad_token,
cls_token=cls_token,
mask_token=mask_token,
**kwargs)
self.regex_tokenizer = re.compile(PATTERN)
self.wordpiece_tokenizer = None
self.basic_tokenizer = None
def _tokenize(self, text):
split_tokens = self.regex_tokenizer.findall(text)
return split_tokens
def convert_idx_to_tokens(self, idx_tensor):
tokens = [self.convert_ids_to_tokens(idx) for idx in idx_tensor.tolist()]
return tokens
def convert_tokens_to_string(self, tokens):
stopwords = ['<bos>', '<eos>']
clean_tokens = [word for word in tokens if word not in stopwords]
out_string = ''.join(clean_tokens)
return out_string
## Transformer layers
class RotaryEmbedding(torch.nn.Module):
def __init__(self, dim, base=10000):
super().__init__()
inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim))
self.register_buffer('inv_freq', inv_freq)
self.seq_len_cached = 0
self.cos_cached = None
self.sin_cached = None
def forward(self, x, seq_dim=1):
seq_len = x.shape[seq_dim]
if seq_len != self.seq_len_cached:
self.seq_len_cached = seq_len
t = torch.arange(x.shape[seq_dim], device=x.device).type_as(self.inv_freq)
freqs = torch.einsum('i,j->ij', t, self.inv_freq)
emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
self.cos_cached = emb.cos()[None,:, None, :]
self.sin_cached = emb.sin()[None,:, None, :]
return self.cos_cached, self.sin_cached
def rotate_half(x):
x1, x2 = x[..., :x.shape[-1] // 2], x[..., x.shape[-1] // 2:]
return torch.cat((-x2, x1), dim=x1.ndim - 1) # dim=-1 triggers a bug in earlier torch versions
@torch.jit.script
def apply_rotary_pos_emb(q, k, cos, sin):
return (q * cos) + (rotate_half(q) * sin), (k * cos) + (rotate_half(k) * sin)
class RotateAttentionLayer(AttentionLayer):
"""Rotate attention layer inherits from fast_transformer attention layer.
The only thing added is an Embedding encoding, for more information
on the attention layer see the fast_transformers code
"""
def __init__(self, attention, d_model, n_heads, d_keys=None,
d_values=None, event_dispatcher=""):
super(RotateAttentionLayer, self).__init__(attention,d_model, n_heads, d_keys=d_keys,
d_values=d_values, event_dispatcher=event_dispatcher)
self.rotaryemb = RotaryEmbedding(d_keys)
print('Using Rotation Embedding')
def forward(self, queries, keys, values, attn_mask, query_lengths,
key_lengths):
"""
Using the same frame work as the fast_Transformers attention layer
but injecting rotary information to the queries and the keys
after the keys and queries are projected.
In the argument description we make use of the following sizes
- N: the batch size
- L: The maximum length of the queries
- S: The maximum length of the keys (the actual length per sequence
is given by the length mask)
- D: The input feature dimensionality passed in the constructor as
'd_model'
Arguments
---------
queries: (N, L, D) The tensor containing the queries
keys: (N, S, D) The tensor containing the keys
values: (N, S, D) The tensor containing the values
attn_mask: An implementation of BaseMask that encodes where each
query can attend to
query_lengths: An implementation of BaseMask that encodes how
many queries each sequence in the batch consists of
key_lengths: An implementation of BaseMask that encodes how
many queries each sequence in the batch consists of
Returns
-------
The new value for each query as a tensor of shape (N, L, D).
"""
# Extract the dimensions into local variables
N, L, _ = queries.shape
_, S, _ = keys.shape
H = self.n_heads
# Project the queries/keys/values
queries = self.query_projection(queries).view(N, L, H, -1)
keys = self.key_projection(keys).view(N, S, H, -1)
cos, sin = self.rotaryemb(queries)
queries, keys = apply_rotary_pos_emb(queries, keys, cos, sin)
values = self.value_projection(values).view(N, S, H, -1)
# Let the world know of the qkv
self.event_dispatcher.dispatch(QKVEvent(self, queries, keys, values))
# Compute the attention
new_values = self.inner_attention(
queries,
keys,
values,
attn_mask,
query_lengths,
key_lengths
).view(N, L, -1)
# Project the output and return
return self.out_projection(new_values)
class RotateEncoderBuilder(BaseTransformerEncoderBuilder):
"""Build a batch transformer encoder with Relative Rotary embeddings
for training or processing of sequences all elements at a time.
Example usage:
builder = RotateEncoderBuilder()
builder.n_layers = 12
builder.n_heads = 8
builder.feed_forward_dimensions = 1024
builder.query_dimensions = 64
builder.value_dimensions = 64
builder.dropout = 0.1
builder.attention_dropout = 0.1
builder.attention_type = "linear"
transformer = builder.get()
"""
def _get_attention_builder(self):
"""Return an instance of the appropriate attention builder."""
return AttentionBuilder()
def _get_attention_layer_class(self):
"""Return the class for the layer that projects queries keys and
values."""
return RotateAttentionLayer
def _get_encoder_class(self):
"""Return the class for the transformer encoder."""
return TransformerEncoder
def _get_encoder_layer_class(self):
"""Return the class for the transformer encoder layer."""
return TransformerEncoderLayer
class AutoEncoderLayer(nn.Module):
def __init__(self, feature_size, latent_size):
super().__init__()
self.encoder = self.Encoder(feature_size, latent_size)
self.decoder = self.Decoder(feature_size, latent_size)
class Encoder(nn.Module):
def __init__(self, feature_size, latent_size):
super().__init__()
self.is_cuda_available = torch.cuda.is_available()
self.fc1 = nn.Linear(feature_size, latent_size)
self.ln_f = nn.LayerNorm(latent_size)
self.lat = nn.Linear(latent_size, latent_size, bias=False)
def forward(self, x):
if self.is_cuda_available:
self.fc1.cuda()
self.ln_f.cuda()
self.lat.cuda()
x = x.cuda()
x = F.gelu(self.fc1(x))
x = self.ln_f(x)
x = self.lat(x)
return x # -> (N, D)
class Decoder(nn.Module):
def __init__(self, feature_size, latent_size):
super().__init__()
self.is_cuda_available = torch.cuda.is_available()
self.fc1 = nn.Linear(latent_size, latent_size)
self.ln_f = nn.LayerNorm(latent_size)
self.rec = nn.Linear(latent_size, feature_size, bias=False)
def forward(self, x):
if self.is_cuda_available:
self.fc1.cuda()
self.ln_f.cuda()
self.rec.cuda()
x = x.cuda()
x = F.gelu(self.fc1(x))
x = self.ln_f(x)
x = self.rec(x)
return x # -> (N, L*D)
class LangLayer(nn.Module):
def __init__(self, n_embd, n_vocab):
super().__init__()
self.is_cuda_available = torch.cuda.is_available()
self.embed = nn.Linear(n_embd, n_embd)
self.ln_f = nn.LayerNorm(n_embd)
self.head = nn.Linear(n_embd, n_vocab, bias=False)
def forward(self, tensor):
if self.is_cuda_available:
self.embed.cuda()
self.ln_f.cuda()
self.head.cuda()
tensor = tensor.cuda()
tensor = self.embed(tensor)
tensor = F.gelu(tensor)
tensor = self.ln_f(tensor)
tensor = self.head(tensor)
return tensor
class MoLEncoder(nn.Module):
def __init__(self, config, n_vocab):
super(MoLEncoder, self).__init__()
# embeddings
self.tok_emb = nn.Embedding(n_vocab, config.n_embd)
self.drop = nn.Dropout(config.d_dropout)
# transformer
builder = RotateEncoderBuilder.from_kwargs(
n_layers=config.n_layer,
n_heads=config.n_head,
query_dimensions=config.n_embd//config.n_head,
value_dimensions=config.n_embd//config.n_head,
feed_forward_dimensions=config.n_embd,
attention_type='linear',
# unless we do deterministic_eval here, we will have random outputs
feature_map=partial(GeneralizedRandomFeatures,
n_dims=config.num_feats,
deterministic_eval=False),
activation='gelu'
)
self.blocks = builder.get()
# classification
self.lang_model = LangLayer(config.n_embd, n_vocab)
def forward(self, idx, mask=None, inference=False):
if not inference:
x = self.tok_emb(idx) # each index maps to a (learnable) vector
x = self.drop(x)
#masking of the length of the inputs its handled in the Masked language part of the code
#do not attempt to handle it in the forward of the transformer
x = self.blocks(x)
logits = self.lang_model(x)
return logits
else:
x = self.tok_emb(idx) # each index maps to a (learnable) vector
x = self.drop(x)
#masking of the length of the inputs its handled in the Masked language part of the code
#do not attempt to handle it in the forward of the transformer
x = self.blocks(x, length_mask=LengthMask(mask.sum(-1), max_len=idx.shape[1]))
# mean pooling
token_embeddings = x
input_mask_expanded = mask.unsqueeze(-1).expand(token_embeddings.size()).float()
sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
true_set = sum_embeddings / sum_mask
return true_set, token_embeddings
class MoLDecoder(nn.Module):
def __init__(self, n_vocab, max_len, n_embd, n_gpu=None):
super(MoLDecoder, self).__init__()
self.max_len = max_len
self.n_embd = n_embd
self.n_gpu = n_gpu
self.autoencoder = AutoEncoderLayer(n_embd*max_len, n_embd)
self.lang_model = LangLayer(n_embd, n_vocab)
def forward(self, token_embeddings):
pred_set = self.autoencoder.encoder(token_embeddings) # (N, D)
pred_cte = self.autoencoder.decoder(pred_set) # (N, L*D)
pred_ids = self.lang_model(pred_cte.view(-1, self.max_len, self.n_embd))
return pred_set, pred_ids
class Smi_ted(nn.Module):
"""materials.smi-ted-Light 289M Parameters"""
def __init__(self, config, vocab):
super(Smi_ted, self).__init__()
self.config = config
self.padding_idx = 2
self.is_cuda_available = torch.cuda.is_available()
n_vocab = len(vocab.keys())
print(n_vocab, config.n_embd)
self.encoder = MoLEncoder(config, n_vocab)
self.decoder = MoLDecoder(n_vocab, config.max_len, config.n_embd)
self._set_seed(config.seed)
print('Vocab size:', n_vocab)
print(f'[PRE-TRAINING MODE - {str(self)}]')
def _init_weights(self, module):
if isinstance(module, (nn.Linear, nn.Embedding)):
module.weight.data.normal_(mean=0.0, std=0.02)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
def _set_seed(self, value):
print('Random Seed:', value)
random.seed(value)
torch.manual_seed(value)
torch.cuda.manual_seed(value)
torch.cuda.manual_seed_all(value)
np.random.seed(value)
cudnn.deterministic = True
cudnn.benchmark = False
def __str__(self):
return 'smi-ted-Light' |