forked from TencentAI4S/CD-GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_example.py
More file actions
59 lines (48 loc) · 2.14 KB
/
predict_example.py
File metadata and controls
59 lines (48 loc) · 2.14 KB
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
# -*- coding: utf-8 -*-
# Copyright (c) 2024, Tencent Inc. All rights reserved.
# Data: 2024/8/28 14:15
import argparse
import os.path
import torch
from config import get_config
from model import CDGPTResiduePairPrediction, CDGPTTokenPrediction, CDGPTSequencePrediction
from tokenizer import SentencePieceTokenizer
def parse_args():
parser = argparse.ArgumentParser(description='CD-GPT model prediction')
parser.add_argument('--model', default="checkpoints/CD-GPT-1b.pth", help='model checkpoint path')
parser.add_argument('--tokenizer', default="checkpoints/tokenizer.model", help='tokenizer path')
parser.add_argument('--head', default="sequence", choices=['sequence', 'token', 'residuepair'], help='output head type, must be sequence, token or residuepair')
parser.add_argument('--num_classes', default=2, type=int, help="number of prediction categories")
args = parser.parse_args()
return args
def setup():
torch.set_grad_enabled(False)
def main(args):
setup()
cfg = get_config()
cfg.tokenizer.path = args.tokenizer
cfg.model.num_classes = args.num_classes
tokenizer = SentencePieceTokenizer(args.tokenizer)
cfg.tokenizer.pad_id = tokenizer.pad
model_path = args.model
assert os.path.exists(model_path)
state = torch.load(model_path, map_location="cpu")
output_head = args.head
assert output_head in ('sequence', 'token', 'residuepair')
if output_head == "sequence":
model = CDGPTSequencePrediction(cfg)
elif output_head == "token":
model = CDGPTTokenPrediction(cfg)
else:
model = CDGPTResiduePairPrediction(cfg)
model.load_state_dict(state["model"], strict=False)
print(f"load checkpoint form: {model_path}")
model.half().cuda().eval()
input_sequence = "KALTARQQEVFDLIRDHISQTGMPPTRAEIAQRLGFRSPNAAEEHLKALARKGVIEIVSGASRGIRLLQEE"
x = tokenizer.encode(input_sequence, eos=False, device="cuda") if output_head == 'sequence' else tokenizer.encode_token(input_sequence, eos=False, device=model.device)
x = x.unsqueeze(0)
output = model(x)["output"]
print(output)
if __name__ == '__main__':
args = parse_args()
main(args)