-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
134 lines (108 loc) · 4.33 KB
/
gen.py
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
import numpy as np
import keras_nlp
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_text as tf_text
from tensorflow import keras
from tensorflow.lite.python import interpreter
import time
import json
gpt2_tokenizer = keras_nlp.models.GPT2Tokenizer.from_preset("gpt2_base_en")
gpt2_preprocessor = keras_nlp.models.GPT2CausalLMPreprocessor.from_preset(
"gpt2_base_en",
sequence_length=256,
add_end_token=True,
)
gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset("gpt2_base_en", preprocessor=gpt2_preprocessor)
def merge_sentences(sentences, max_length):
res = []
cur_len = 0
cur_sentences = []
for s in sentences:
if cur_len + len(s) > max_length:
# If adding the next sentence exceeds `max_length`, we add the
# current sentences into collection
res.append(" ".join(cur_sentences))
cur_len = len(s)
cur_sentences = [s]
else:
cur_len += len(s)
cur_sentences.append(s)
res.append(" ".join(cur_sentences))
return res
max_length = 350
all_sentences = []
count = 0
def gettingAndMergin():
f=open('train_webmd_squad_v2_consec.json')
data=json.load(f)
f.close()
data=data['data']
list=[]
i=0
for arrayWrappedPara in data:
para=arrayWrappedPara['paragraphs'][0]
sents=[]
for sent in para['sent_list']:
if "()" not in sent:
sents.append(sent)
combined_res = merge_sentences(sents, max_length)
all_sentences.extend(combined_res)
gettingAndMergin()
tf_train_ds = tf.data.Dataset.from_tensor_slices(all_sentences)
part_of_ds = tf_train_ds.map(gpt2_preprocessor, tf.data.AUTOTUNE).batch(20).cache().prefetch(tf.data.AUTOTUNE)
gpt2_lm.include_preprocessing = False
num_epochs = 3
lr = tf.keras.optimizers.schedules.PolynomialDecay(
5e-5,
decay_steps=part_of_ds.cardinality() * num_epochs,
end_learning_rate=0.0,
)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
gpt2_lm.compile(
optimizer=keras.optimizers.experimental.Adam(lr),
loss=loss,
weighted_metrics=["accuracy"])
gpt2_lm.fit(part_of_ds, epochs=num_epochs)
gpt2_lm.backbone.save_weights("finetuned_model.h5")
@tf.function
def generate(prompt, max_length):
return gpt2_lm.generate(prompt, max_length)
concrete_func = generate.get_concrete_function(tf.TensorSpec([], tf.string), 100)
def run_inference(input, generate_tflite):
interp = interpreter.InterpreterWithCustomOps(
model_content=generate_tflite,
custom_op_registerers=tf_text.tflite_registrar.SELECT_TFTEXT_OPS)
interp.get_signature_list()
generator = interp.get_signature_runner('serving_default')
output = generator(prompt=np.array([input]))
print("\nGenerated with TFLite:\n", output["output_0"])
gpt2_lm.jit_compile = False
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],
gpt2_lm)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
converter.allow_custom_ops = True
converter.target_spec.experimental_select_user_tf_ops = ["UnsortedSegmentJoin", "UpperBound"]
converter._experimental_guarantee_all_funcs_one_use = True
generate_tflite = converter.convert()
print(run_inference("Glaucoma is ", generate_tflite))
with open('unquantized_gpt2.tflite', 'wb') as f:
f.write(generate_tflite)
gpt2_lm.jit_compile = False
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func],
gpt2_lm)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
converter.allow_custom_ops = True
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.experimental_select_user_tf_ops = ["UnsortedSegmentJoin", "UpperBound"]
converter._experimental_guarantee_all_funcs_one_use = True
quant_generate_tflite = converter.convert()
print(run_inference("I have kidney pain so ", quant_generate_tflite))
with open('quantized_gpt2.tflite', 'wb') as f:
f.write(quant_generate_tflite)