messege
Browse files- MAR-INF/MANIFEST.json +11 -0
- TextGenerationHandlerForString.py +89 -0
- config.json +51 -0
- merges.txt +0 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +1 -0
- tokenizer.json +0 -0
- tokenizer_config.json +1 -0
- vocab.json +0 -0
MAR-INF/MANIFEST.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"createdOn": "09/06/2022 17:16:36",
|
3 |
+
"runtime": "python",
|
4 |
+
"model": {
|
5 |
+
"modelName": "gpt-2-ko-small-finetune",
|
6 |
+
"serializedFile": "pytorch_model.bin",
|
7 |
+
"handler": "TextGenerationHandlerForString.py",
|
8 |
+
"modelVersion": "1.0"
|
9 |
+
},
|
10 |
+
"archiverVersion": "0.5.1"
|
11 |
+
}
|
TextGenerationHandlerForString.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gc
|
2 |
+
import json
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from ts.torch_handler.base_handler import BaseHandler
|
6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
+
|
8 |
+
import logging
|
9 |
+
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
|
13 |
+
class TextGenerationHandlerForString(BaseHandler):
|
14 |
+
def __init__(self):
|
15 |
+
super(TextGenerationHandlerForString, self).__init__()
|
16 |
+
self.model = None
|
17 |
+
self.tokenizer = None
|
18 |
+
self.device = None
|
19 |
+
self.task_config = None
|
20 |
+
self.initialized = False
|
21 |
+
|
22 |
+
def load_model(self, model_dir):
|
23 |
+
if self.device.type == "cuda":
|
24 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_dir, torch_dtype="auto", low_cpu_mem_usage=True)
|
25 |
+
if self.model.dtype == torch.float32:
|
26 |
+
self.model = self.model.half()
|
27 |
+
else:
|
28 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_dir, torch_dtype="auto")
|
29 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
30 |
+
try:
|
31 |
+
self.task_config = self.model.config.task_specific_params["text-generation"]
|
32 |
+
except Exception:
|
33 |
+
self.task_config = {}
|
34 |
+
# TODO: Need to compare performance
|
35 |
+
self.model.to(self.device, non_blocking=True)
|
36 |
+
|
37 |
+
def initialize(self, ctx):
|
38 |
+
self.manifest = ctx.manifest
|
39 |
+
properties = ctx.system_properties
|
40 |
+
model_dir = properties.get("model_dir")
|
41 |
+
self.device = torch.device(
|
42 |
+
"cuda:" + str(properties.get("gpu_id"))
|
43 |
+
if torch.cuda.is_available()
|
44 |
+
else "cpu"
|
45 |
+
)
|
46 |
+
self.load_model(model_dir)
|
47 |
+
self.model.eval()
|
48 |
+
self.initialized = True
|
49 |
+
|
50 |
+
def preprocess(self, requests):
|
51 |
+
input_batch = {}
|
52 |
+
for idx, data in enumerate(requests):
|
53 |
+
input_batch["input_text"] = data.get("body").get("text")
|
54 |
+
input_batch["num_samples"] = data.get("body").get("num_samples")
|
55 |
+
input_batch["length"] = data.get("body").get("length")
|
56 |
+
del requests
|
57 |
+
gc.collect()
|
58 |
+
return input_batch
|
59 |
+
|
60 |
+
def inference(self, input_batch):
|
61 |
+
input_text = input_batch["input_text"]
|
62 |
+
length = input_batch["length"]
|
63 |
+
num_samples = input_batch["num_samples"]
|
64 |
+
input_ids = self.tokenizer.encode(input_text, return_tensors="pt").to(
|
65 |
+
self.device
|
66 |
+
)
|
67 |
+
self.task_config["max_length"] = length
|
68 |
+
self.task_config["num_return_sequences"] = num_samples
|
69 |
+
inference_output = self.model.generate(input_ids, **self.task_config)
|
70 |
+
if torch.cuda.is_available():
|
71 |
+
torch.cuda.empty_cache()
|
72 |
+
del input_batch
|
73 |
+
gc.collect()
|
74 |
+
return inference_output
|
75 |
+
|
76 |
+
def postprocess(self, inference_output):
|
77 |
+
output = self.tokenizer.batch_decode(
|
78 |
+
inference_output.tolist(), skip_special_tokens=True
|
79 |
+
)
|
80 |
+
del inference_output
|
81 |
+
gc.collect()
|
82 |
+
return [json.dumps(output, ensure_ascii=False)]
|
83 |
+
|
84 |
+
def handle(self, data, context):
|
85 |
+
self.context = context
|
86 |
+
data = self.preprocess(data)
|
87 |
+
data = self.inference(data)
|
88 |
+
data = self.postprocess(data)
|
89 |
+
return data
|
config.json
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "/model",
|
3 |
+
"_num_labels": 1,
|
4 |
+
"activation_function": "gelu_new",
|
5 |
+
"architectures": [
|
6 |
+
"GPT2LMHeadModel"
|
7 |
+
],
|
8 |
+
"attn_pdrop": 0.1,
|
9 |
+
"author": "Heewon Jeon([email protected])",
|
10 |
+
"bos_token_id": 0,
|
11 |
+
"created_date": "2021-04-28",
|
12 |
+
"embd_pdrop": 0.1,
|
13 |
+
"eos_token_id": 1,
|
14 |
+
"gradient_checkpointing": false,
|
15 |
+
"id2label": {
|
16 |
+
"0": "LABEL_0"
|
17 |
+
},
|
18 |
+
"initializer_range": 0.02,
|
19 |
+
"label2id": {
|
20 |
+
"LABEL_0": 0
|
21 |
+
},
|
22 |
+
"layer_norm_epsilon": 1e-05,
|
23 |
+
"license": "CC-BY-NC-SA 4.0",
|
24 |
+
"model_type": "gpt2",
|
25 |
+
"n_ctx": 1024,
|
26 |
+
"n_embd": 768,
|
27 |
+
"n_head": 12,
|
28 |
+
"n_inner": null,
|
29 |
+
"n_layer": 12,
|
30 |
+
"n_positions": 1024,
|
31 |
+
"pad_token_id": 3,
|
32 |
+
"reorder_and_upcast_attn": false,
|
33 |
+
"resid_pdrop": 0.1,
|
34 |
+
"scale_attn_by_inverse_layer_idx": false,
|
35 |
+
"scale_attn_weights": true,
|
36 |
+
"summary_activation": null,
|
37 |
+
"summary_first_dropout": 0.1,
|
38 |
+
"summary_proj_to_labels": true,
|
39 |
+
"summary_type": "cls_index",
|
40 |
+
"summary_use_proj": true,
|
41 |
+
"task_specific_params": {
|
42 |
+
"text-generation": {
|
43 |
+
"do_sample": true,
|
44 |
+
"max_length": 50
|
45 |
+
}
|
46 |
+
},
|
47 |
+
"torch_dtype": "float32",
|
48 |
+
"transformers_version": "4.13.0",
|
49 |
+
"use_cache": true,
|
50 |
+
"vocab_size": 51200
|
51 |
+
}
|
merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f67056887c88024a8cc3de7c99e259097b8baa5249490c50830700b4891d4aa4
|
3 |
+
size 513300713
|
special_tokens_map.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>", "mask_token": "<mask>"}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>", "add_prefix_space": false, "pad_token": "<pad>", "mask_token": "<mask>", "special_tokens_map_file": null, "name_or_path": "/model", "tokenizer_class": "GPT2Tokenizer"}
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|