johnpaulbin commited on
Commit
3b6f52b
1 Parent(s): 0bdf9c6

first commit

Browse files
MAR-INF/MANIFEST.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "createdOn": "26/06/2021 03:42:34",
3
+ "runtime": "python",
4
+ "model": {
5
+ "modelName": "gpt-2-en-large-finetune",
6
+ "serializedFile": "pytorch_model.bin",
7
+ "handler": "handler.py",
8
+ "modelVersion": "1.0"
9
+ },
10
+ "archiverVersion": "0.3.0"
11
+ }
config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "bos_token_id": 50256,
10
+ "embd_pdrop": 0.1,
11
+ "eos_token_id": 50256,
12
+ "gradient_checkpointing": false,
13
+ "id2label": {
14
+ "0": "LABEL_0"
15
+ },
16
+ "initializer_range": 0.02,
17
+ "label2id": {
18
+ "LABEL_0": 0
19
+ },
20
+ "layer_norm_epsilon": 1e-05,
21
+ "model_type": "gpt2",
22
+ "n_ctx": 1024,
23
+ "n_embd": 1280,
24
+ "n_head": 20,
25
+ "n_inner": null,
26
+ "n_layer": 36,
27
+ "n_positions": 1024,
28
+ "resid_pdrop": 0.1,
29
+ "scale_attn_weights": true,
30
+ "summary_activation": null,
31
+ "summary_first_dropout": 0.1,
32
+ "summary_proj_to_labels": true,
33
+ "summary_type": "cls_index",
34
+ "summary_use_proj": true,
35
+ "task_specific_params": {
36
+ "text-generation": {
37
+ "do_sample": true,
38
+ "max_length": 50
39
+ }
40
+ },
41
+ "transformers_version": "4.6.1",
42
+ "use_cache": true,
43
+ "vocab_size": 50257
44
+ }
handler.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gc
3
+ from ts.torch_handler.base_handler import BaseHandler
4
+ from transformers import GPT2LMHeadModel
5
+
6
+ import logging
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ class SampleTransformerModel(BaseHandler):
12
+ def __init__(self):
13
+ super(SampleTransformerModel, self).__init__()
14
+ self.model = None
15
+ self.device = None
16
+ self.initialized = False
17
+
18
+ def load_model(self, model_dir):
19
+ self.model = GPT2LMHeadModel.from_pretrained(model_dir, return_dict=True)
20
+ self.model.to(self.device)
21
+
22
+ def initialize(self, ctx):
23
+ # self.manifest = ctx.manifest
24
+ properties = ctx.system_properties
25
+ model_dir = properties.get("model_dir")
26
+ self.device = torch.device("cuda:" + str(properties.get("gpu_id")) if torch.cuda.is_available() else "cpu")
27
+
28
+ self.load_model(model_dir)
29
+
30
+ self.model.eval()
31
+ self.initialized = True
32
+
33
+ def preprocess(self, requests):
34
+ input_batch = {}
35
+ for idx, data in enumerate(requests):
36
+ input_ids = torch.tensor([data.get("body").get("text")]).to(self.device)
37
+ input_batch["input_ids"] = input_ids
38
+ input_batch["num_samples"] = data.get("body").get("num_samples")
39
+ input_batch["length"] = data.get("body").get("length") + len(data.get("body").get("text"))
40
+ del requests
41
+ gc.collect()
42
+ return input_batch
43
+
44
+ def inference(self, input_batch):
45
+ input_ids = input_batch["input_ids"]
46
+ length = input_batch["length"]
47
+
48
+ inference_output = self.model.generate(input_ids,
49
+ bos_token_id=self.model.config.bos_token_id,
50
+ eos_token_id=self.model.config.eos_token_id,
51
+ pad_token_id=self.model.config.eos_token_id,
52
+ do_sample=True,
53
+ max_length=length,
54
+ top_k=50,
55
+ top_p=0.95,
56
+ no_repeat_ngram_size=2,
57
+ num_return_sequences=input_batch["num_samples"])
58
+
59
+ if torch.cuda.is_available():
60
+ torch.cuda.empty_cache()
61
+ del input_batch
62
+ gc.collect()
63
+ return inference_output
64
+
65
+ def postprocess(self, inference_output):
66
+ output = inference_output.cpu().numpy().tolist()
67
+ del inference_output
68
+ gc.collect()
69
+ return [output]
70
+
71
+ def handle(self, data, context):
72
+ # self.context = context
73
+ data = self.preprocess(data)
74
+ data = self.inference(data)
75
+ data = self.postprocess(data)
76
+ return data
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bcd94c85a44a9fcd31cd1d2c8b4b71069bfcbe98143bc3c95612e55276e16cc9
3
+ size 3134064907
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"bos_token": "<|endoftext|>", "eos_token": "<|endoftext|>", "unk_token": "<|endoftext|>"}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff