initial release
Browse files- DLM_emb_model.py +181 -0
- __pycache__/DLM_emb_model.cpython-39.pyc +0 -0
- example.py +3 -0
- huggingface_config.py +160 -0
DLM_emb_model.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import argparse
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.optim as optim
|
| 10 |
+
from torch.utils.data import Dataset, DataLoader
|
| 11 |
+
from transformers import AutoModel, AutoTokenizer
|
| 12 |
+
from sklearn.model_selection import KFold
|
| 13 |
+
from sklearn.metrics import average_precision_score
|
| 14 |
+
from torch.nn.utils.rnn import pad_sequence
|
| 15 |
+
from sklearn.cluster import AgglomerativeClustering
|
| 16 |
+
from Bio import Phylo
|
| 17 |
+
from triton.language import bfloat16
|
| 18 |
+
from scipy.stats import pearsonr, spearmanr
|
| 19 |
+
import json
|
| 20 |
+
import itertools
|
| 21 |
+
import logging
|
| 22 |
+
|
| 23 |
+
import hydra
|
| 24 |
+
from hydra import compose, initialize, initialize_config_dir
|
| 25 |
+
import models
|
| 26 |
+
from collections import OrderedDict
|
| 27 |
+
import noise_schedule
|
| 28 |
+
|
| 29 |
+
import torch.nn.functional as F
|
| 30 |
+
import ast
|
| 31 |
+
from omegaconf import DictConfig, ListConfig
|
| 32 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 33 |
+
|
| 34 |
+
# current_directory = Path(__file__).parent
|
| 35 |
+
current_directory = Path('/data2/tianang/projects/Synergy')
|
| 36 |
+
|
| 37 |
+
with initialize_config_dir(config_dir="/data2/tianang/projects/mdlm/configs"):
|
| 38 |
+
config = compose(config_name="config")
|
| 39 |
+
|
| 40 |
+
class mol_emb_mdlm(nn.Module):
|
| 41 |
+
def __init__(self, config, vocab_size, ckpt_path, mask_index):
|
| 42 |
+
super(mol_emb_mdlm, self).__init__()
|
| 43 |
+
self.config = config
|
| 44 |
+
self.vocab_size = vocab_size
|
| 45 |
+
self.mask_index = mask_index
|
| 46 |
+
self.ckpt_path = ckpt_path
|
| 47 |
+
self.parameterization = self.config.parameterization
|
| 48 |
+
self.time_conditioning = self.config.time_conditioning
|
| 49 |
+
self.backbone = self.load_DIT() # hidden_size = 768
|
| 50 |
+
# print(self.bert.config.max_position_embeddings)
|
| 51 |
+
self.noise = noise_schedule.get_noise(self.config)
|
| 52 |
+
|
| 53 |
+
def _process_sigma(self, sigma):
|
| 54 |
+
if sigma is None:
|
| 55 |
+
assert self.parameterization == 'ar'
|
| 56 |
+
return sigma
|
| 57 |
+
if sigma.ndim > 1:
|
| 58 |
+
sigma = sigma.squeeze(-1)
|
| 59 |
+
if not self.time_conditioning:
|
| 60 |
+
sigma = torch.zeros_like(sigma)
|
| 61 |
+
assert sigma.ndim == 1, sigma.shape
|
| 62 |
+
return sigma
|
| 63 |
+
|
| 64 |
+
def _sample_t(self, n, device):
|
| 65 |
+
sampling_eps = 1e-3
|
| 66 |
+
_eps_t = torch.rand(n, device=device) * 0 # 因为是要做性质预测了这里
|
| 67 |
+
t = (1 - sampling_eps) * _eps_t + sampling_eps
|
| 68 |
+
return t * 0
|
| 69 |
+
|
| 70 |
+
def _forward(self, x, sigma, attnmask): # TODO: non pad 不一样的地方
|
| 71 |
+
sigma = self._process_sigma(sigma)
|
| 72 |
+
with torch.cuda.amp.autocast(dtype=torch.float32):
|
| 73 |
+
x = self.backbone.vocab_embed(x)
|
| 74 |
+
c = F.silu(self.backbone.sigma_map(sigma))
|
| 75 |
+
rotary_cos_sin = self.backbone.rotary_emb(x)
|
| 76 |
+
|
| 77 |
+
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
|
| 78 |
+
for i in range(len(self.backbone.blocks)):
|
| 79 |
+
x = self.backbone.blocks[i](x, rotary_cos_sin, c, seqlens=None, attnmask=attnmask) # TODO: non pad 不一样的地方
|
| 80 |
+
|
| 81 |
+
return x
|
| 82 |
+
|
| 83 |
+
def q_xt(self, x, move_chance):
|
| 84 |
+
"""Computes the noisy sample xt.
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
x: int torch.Tensor with shape (batch_size,
|
| 88 |
+
diffusion_model_input_length), input.
|
| 89 |
+
move_chance: float torch.Tensor with shape (batch_size, 1).
|
| 90 |
+
"""
|
| 91 |
+
move_indices = torch.rand(*x.shape, device=x.device) < move_chance
|
| 92 |
+
xt = torch.where(move_indices, self.mask_index, x)
|
| 93 |
+
return xt
|
| 94 |
+
|
| 95 |
+
def forward(self, input_ids, attention_mask=None):
|
| 96 |
+
t = self._sample_t(input_ids.shape[0], input_ids.device)
|
| 97 |
+
sigma, dsigma = self.noise(t)
|
| 98 |
+
unet_conditioning = sigma[:, None]
|
| 99 |
+
move_chance = 1 - torch.exp(-sigma[:, None])
|
| 100 |
+
xt = self.q_xt(input_ids, move_chance)
|
| 101 |
+
outputs = self._forward(xt, unet_conditioning, attnmask = attention_mask) # TODO: non pad 不一样的地方
|
| 102 |
+
return outputs
|
| 103 |
+
|
| 104 |
+
def load_DIT(self):
|
| 105 |
+
backbone = models.dit.DIT_non_pad(self.config, vocab_size=self.vocab_size) # TODO: non pad 不一样的地方
|
| 106 |
+
lightning_ckpt = torch.load(self.ckpt_path, map_location='cpu')
|
| 107 |
+
state_dict = lightning_ckpt['state_dict']
|
| 108 |
+
|
| 109 |
+
new_sd = OrderedDict()
|
| 110 |
+
for k, v in state_dict.items():
|
| 111 |
+
if k.startswith('backbone.'):
|
| 112 |
+
new_key = k[len('backbone.'):]
|
| 113 |
+
else:
|
| 114 |
+
new_key = k
|
| 115 |
+
new_sd[new_key] = v
|
| 116 |
+
|
| 117 |
+
backbone.load_state_dict(new_sd, strict=False)
|
| 118 |
+
|
| 119 |
+
return backbone
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class MolEmbDLM(nn.Module, PyTorchModelHubMixin):
|
| 123 |
+
def __init__(self, config, vocab_size, ckpt_path=None, mask_index=0, load_backbone=True):
|
| 124 |
+
super().__init__()
|
| 125 |
+
self.config = config
|
| 126 |
+
self.vocab_size = vocab_size
|
| 127 |
+
self.mask_index = mask_index
|
| 128 |
+
self.parameterization = config.parameterization
|
| 129 |
+
self.time_conditioning = config.time_conditioning
|
| 130 |
+
# 构建 backbone
|
| 131 |
+
self.backbone = self._build_backbone()
|
| 132 |
+
# 如果给了 ckpt_path 就加载,否则假设后面会用 state_dict 覆盖(Hub 场景)
|
| 133 |
+
if ckpt_path is not None and load_backbone:
|
| 134 |
+
print("Loading backbone from {}".format(ckpt_path))
|
| 135 |
+
self._load_lightning_ckpt(ckpt_path)
|
| 136 |
+
|
| 137 |
+
def _build_backbone(self):
|
| 138 |
+
return models.dit.DIT_non_pad(self.config, vocab_size=self.vocab_size)
|
| 139 |
+
|
| 140 |
+
def _load_lightning_ckpt(self, ckpt_path):
|
| 141 |
+
sd_full = torch.load(ckpt_path, map_location='cpu')
|
| 142 |
+
sd = sd_full['state_dict']
|
| 143 |
+
new_sd = {}
|
| 144 |
+
for k,v in sd.items():
|
| 145 |
+
if k.startswith('backbone.'):
|
| 146 |
+
new_sd[k[len('backbone.'):]] = v
|
| 147 |
+
self.backbone.load_state_dict(new_sd, strict=False)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def build_hf_config(hydra_cfg, tokenizer):
|
| 151 |
+
return {
|
| 152 |
+
"model_type": "mol_emb_raw", # 仅标识,不影响逻辑
|
| 153 |
+
"vocab_size": len(tokenizer.get_vocab()),
|
| 154 |
+
"hidden_size": hydra_cfg.model.hidden_size,
|
| 155 |
+
"n_blocks": hydra_cfg.model.n_blocks,
|
| 156 |
+
"n_heads": hydra_cfg.model.n_heads,
|
| 157 |
+
"max_position_embeddings": hydra_cfg.model.length,
|
| 158 |
+
"parameterization": hydra_cfg.parameterization,
|
| 159 |
+
"time_conditioning": hydra_cfg.time_conditioning,
|
| 160 |
+
"noise_schedule_type": hydra_cfg.noise.type,
|
| 161 |
+
"sigma_min": hydra_cfg.noise.sigma_min,
|
| 162 |
+
"sigma_max": hydra_cfg.noise.sigma_max,
|
| 163 |
+
"mask_index": tokenizer.mask_token_id,
|
| 164 |
+
"tokenizer_name_or_path": hydra_cfg.data.tokenizer_name_or_path
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
if __name__ == '__main__':
|
| 168 |
+
model_name = "ibm-research/materials.selfies-ted"
|
| 169 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 170 |
+
|
| 171 |
+
DIT_ckpt_path = '/data2/tianang/projects/mdlm/Checkpoints_fangping/1-255000-fine-tune.ckpt'
|
| 172 |
+
model = MolEmbDLM(config, len(tokenizer.get_vocab()), DIT_ckpt_path, tokenizer.mask_token_id)
|
| 173 |
+
|
| 174 |
+
hf_config = build_hf_config(config, tokenizer)
|
| 175 |
+
|
| 176 |
+
EXPORT_DIR = "/data2/tianang/projects/mdlm/huggingface/huggingface_model"
|
| 177 |
+
model.save_pretrained(EXPORT_DIR, config=hf_config) # 生成:pytorch_model.bin + config.json
|
| 178 |
+
tokenizer.save_pretrained(EXPORT_DIR)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
__pycache__/DLM_emb_model.cpython-39.pyc
ADDED
|
Binary file (5.68 kB). View file
|
|
|
example.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from DLM_emb_model import MolEmbDLM
|
| 2 |
+
model = MolEmbDLM.from_pretrained("Kiria-Nozan/ApexOracle")
|
| 3 |
+
model.eval()
|
huggingface_config.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import argparse
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.optim as optim
|
| 10 |
+
from torch.utils.data import Dataset, DataLoader
|
| 11 |
+
from transformers import AutoModel, AutoTokenizer
|
| 12 |
+
from sklearn.model_selection import KFold
|
| 13 |
+
from sklearn.metrics import average_precision_score
|
| 14 |
+
from torch.nn.utils.rnn import pad_sequence
|
| 15 |
+
from sklearn.cluster import AgglomerativeClustering
|
| 16 |
+
from Bio import Phylo
|
| 17 |
+
from triton.language import bfloat16
|
| 18 |
+
from scipy.stats import pearsonr, spearmanr
|
| 19 |
+
import json
|
| 20 |
+
import itertools
|
| 21 |
+
import logging
|
| 22 |
+
|
| 23 |
+
import hydra
|
| 24 |
+
from hydra import compose, initialize, initialize_config_dir
|
| 25 |
+
import models
|
| 26 |
+
from collections import OrderedDict
|
| 27 |
+
import noise_schedule
|
| 28 |
+
|
| 29 |
+
import torch.nn.functional as F
|
| 30 |
+
import ast
|
| 31 |
+
from omegaconf import DictConfig, ListConfig
|
| 32 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 33 |
+
|
| 34 |
+
# current_directory = Path(__file__).parent
|
| 35 |
+
current_directory = Path('/data2/tianang/projects/Synergy')
|
| 36 |
+
|
| 37 |
+
with initialize_config_dir(config_dir="/data2/tianang/projects/mdlm/configs"):
|
| 38 |
+
config = compose(config_name="config")
|
| 39 |
+
|
| 40 |
+
class mol_emb_mdlm(nn.Module):
|
| 41 |
+
def __init__(self, config, vocab_size, ckpt_path, mask_index):
|
| 42 |
+
super(mol_emb_mdlm, self).__init__()
|
| 43 |
+
self.config = config
|
| 44 |
+
self.vocab_size = vocab_size
|
| 45 |
+
self.mask_index = mask_index
|
| 46 |
+
self.ckpt_path = ckpt_path
|
| 47 |
+
self.parameterization = self.config.parameterization
|
| 48 |
+
self.time_conditioning = self.config.time_conditioning
|
| 49 |
+
self.backbone = self.load_DIT() # hidden_size = 768
|
| 50 |
+
# print(self.bert.config.max_position_embeddings)
|
| 51 |
+
self.noise = noise_schedule.get_noise(self.config)
|
| 52 |
+
|
| 53 |
+
def _process_sigma(self, sigma):
|
| 54 |
+
if sigma is None:
|
| 55 |
+
assert self.parameterization == 'ar'
|
| 56 |
+
return sigma
|
| 57 |
+
if sigma.ndim > 1:
|
| 58 |
+
sigma = sigma.squeeze(-1)
|
| 59 |
+
if not self.time_conditioning:
|
| 60 |
+
sigma = torch.zeros_like(sigma)
|
| 61 |
+
assert sigma.ndim == 1, sigma.shape
|
| 62 |
+
return sigma
|
| 63 |
+
|
| 64 |
+
def _sample_t(self, n, device):
|
| 65 |
+
sampling_eps = 1e-3
|
| 66 |
+
_eps_t = torch.rand(n, device=device) * 0 # 因为是要做性质预测了这里
|
| 67 |
+
t = (1 - sampling_eps) * _eps_t + sampling_eps
|
| 68 |
+
return t * 0
|
| 69 |
+
|
| 70 |
+
def _forward(self, x, sigma, attnmask): # TODO: non pad 不一样的地方
|
| 71 |
+
sigma = self._process_sigma(sigma)
|
| 72 |
+
with torch.cuda.amp.autocast(dtype=torch.float32):
|
| 73 |
+
x = self.backbone.vocab_embed(x)
|
| 74 |
+
c = F.silu(self.backbone.sigma_map(sigma))
|
| 75 |
+
rotary_cos_sin = self.backbone.rotary_emb(x)
|
| 76 |
+
|
| 77 |
+
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
|
| 78 |
+
for i in range(len(self.backbone.blocks)):
|
| 79 |
+
x = self.backbone.blocks[i](x, rotary_cos_sin, c, seqlens=None, attnmask=attnmask) # TODO: non pad 不一样的地方
|
| 80 |
+
|
| 81 |
+
return x
|
| 82 |
+
|
| 83 |
+
def q_xt(self, x, move_chance):
|
| 84 |
+
"""Computes the noisy sample xt.
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
x: int torch.Tensor with shape (batch_size,
|
| 88 |
+
diffusion_model_input_length), input.
|
| 89 |
+
move_chance: float torch.Tensor with shape (batch_size, 1).
|
| 90 |
+
"""
|
| 91 |
+
move_indices = torch.rand(*x.shape, device=x.device) < move_chance
|
| 92 |
+
xt = torch.where(move_indices, self.mask_index, x)
|
| 93 |
+
return xt
|
| 94 |
+
|
| 95 |
+
def forward(self, input_ids, attention_mask=None):
|
| 96 |
+
t = self._sample_t(input_ids.shape[0], input_ids.device)
|
| 97 |
+
sigma, dsigma = self.noise(t)
|
| 98 |
+
unet_conditioning = sigma[:, None]
|
| 99 |
+
move_chance = 1 - torch.exp(-sigma[:, None])
|
| 100 |
+
xt = self.q_xt(input_ids, move_chance)
|
| 101 |
+
outputs = self._forward(xt, unet_conditioning, attnmask = attention_mask) # TODO: non pad 不一样的地方
|
| 102 |
+
return outputs
|
| 103 |
+
|
| 104 |
+
def load_DIT(self):
|
| 105 |
+
backbone = models.dit.DIT_non_pad(self.config, vocab_size=self.vocab_size) # TODO: non pad 不一样的地方
|
| 106 |
+
lightning_ckpt = torch.load(self.ckpt_path, map_location='cpu')
|
| 107 |
+
state_dict = lightning_ckpt['state_dict']
|
| 108 |
+
|
| 109 |
+
new_sd = OrderedDict()
|
| 110 |
+
for k, v in state_dict.items():
|
| 111 |
+
if k.startswith('backbone.'):
|
| 112 |
+
new_key = k[len('backbone.'):]
|
| 113 |
+
else:
|
| 114 |
+
new_key = k
|
| 115 |
+
new_sd[new_key] = v
|
| 116 |
+
|
| 117 |
+
backbone.load_state_dict(new_sd, strict=False)
|
| 118 |
+
|
| 119 |
+
return backbone
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class MolEmbDLM(mol_emb_mdlm, PyTorchModelHubMixin):
|
| 123 |
+
"""
|
| 124 |
+
继承你的原模型 + Hub Mixin。
|
| 125 |
+
不重写任何方法,使用默认 _save_pretrained/_from_pretrained。
|
| 126 |
+
"""
|
| 127 |
+
pass
|
| 128 |
+
|
| 129 |
+
def build_hf_config(hydra_cfg, tokenizer):
|
| 130 |
+
return {
|
| 131 |
+
"model_type": "mol_emb_raw", # 仅标识,不影响逻辑
|
| 132 |
+
"vocab_size": len(tokenizer.get_vocab()),
|
| 133 |
+
"hidden_size": hydra_cfg.model.hidden_size,
|
| 134 |
+
"n_blocks": hydra_cfg.model.n_blocks,
|
| 135 |
+
"n_heads": hydra_cfg.model.n_heads,
|
| 136 |
+
"max_position_embeddings": hydra_cfg.model.length,
|
| 137 |
+
"parameterization": hydra_cfg.parameterization,
|
| 138 |
+
"time_conditioning": hydra_cfg.time_conditioning,
|
| 139 |
+
"noise_schedule_type": hydra_cfg.noise.type,
|
| 140 |
+
"sigma_min": hydra_cfg.noise.sigma_min,
|
| 141 |
+
"sigma_max": hydra_cfg.noise.sigma_max,
|
| 142 |
+
"mask_index": tokenizer.mask_token_id,
|
| 143 |
+
"tokenizer_name_or_path": hydra_cfg.data.tokenizer_name_or_path
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
if __name__ == '__main__':
|
| 147 |
+
model_name = "ibm-research/materials.selfies-ted"
|
| 148 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 149 |
+
|
| 150 |
+
DIT_ckpt_path = '/data2/tianang/projects/mdlm/Checkpoints_fangping/1-255000-fine-tune.ckpt'
|
| 151 |
+
model = MolEmbDLM(config, len(tokenizer.get_vocab()), DIT_ckpt_path, tokenizer.mask_token_id)
|
| 152 |
+
|
| 153 |
+
hf_config = build_hf_config(config, tokenizer)
|
| 154 |
+
|
| 155 |
+
EXPORT_DIR = "/data2/tianang/projects/mdlm/huggingface/huggingface_model"
|
| 156 |
+
model.save_pretrained(EXPORT_DIR, config=hf_config) # 生成:pytorch_model.bin + config.json
|
| 157 |
+
tokenizer.save_pretrained(EXPORT_DIR)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|