File size: 17,085 Bytes
d891407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import os
import json
import tensorflow as tf
from tqdm import tqdm
from GPT import *
import pickle
import argparse
import sys



def save_module(save_weights, model, vectorizer, save_tokenizer):
    
        # Save the GPT Model
        with open(save_weights, 'wb') as file:
            pickle.dump(model.weights, file)
        
        #Save the Vectorizer Model
        vocabulary = vectorizer.get_vocabulary()

        # Encode the vocabulary as JSON-compatible strings
        encoded_vocabulary = [word.encode('unicode_escape').decode('utf-8') for word in vocabulary]
        encoded_vocabulary = encoded_vocabulary[2:]

        # Save the encoded vocabulary to a JSON file
        with open(save_tokenizer, 'w') as f:
            json.dump(encoded_vocabulary, f)
            print("Vocabulary size saved: " + str(len(encoded_vocabulary)))
    
    
    
    

def read_file(f, vectorizer, chunk_size = 1024, starting_chunk = 0, ending_chunk = 5, gpt_input = 10):
    i = 0
    chunk = []
    
    while True:
        data = f.read(chunk_size)
        
        if not data or i > ending_chunk:
            break
        
        if i >= starting_chunk and i <= ending_chunk:
            file_contents = data.split()
            input_tokens, output_tokens = [], []
            for j in range(len(file_contents) - gpt_input - 1):
                input_tokens += [file_contents[j : j + gpt_input]]
                output_tokens += [file_contents[j + gpt_input]]
               
            
            X = [' '.join(input_tokens[j]) for j in range(len(input_tokens))]
            Y = output_tokens
            
            X = vectorizer(X)
            Y = vectorizer(Y)
            
            output = tf.concat([X, Y], 1)
        
            yield output
        
        i += 1


def get_model(gpt_input, d_model, h, vocab_size, decoder_stacks, GPT_attention):
    input_words = tf.keras.layers.Input((gpt_input))
    embedding = tf.keras.layers.Embedding(vocab_size + 2, d_model)(input_words)
    positional_enc = PositionalEmbedding(words = gpt_input, embedding_size = d_model)(embedding)
    decoder = Decoder(num_heads = h, key_dim = gpt_input, key_embedding = d_model, GPT_attention = GPT_attention)(positional_enc)
    
    for _ in range(decoder_stacks - 1):
        decoder = Decoder(num_heads = h, key_dim = gpt_input, key_embedding = d_model, GPT_attention = GPT_attention)(decoder)
    
    decoder = tf.keras.layers.Flatten()(decoder)
    linear_layer = tf.keras.layers.Dense(vocab_size + 3)(decoder)
    softmax = tf.nn.softmax(linear_layer)
    GPT = tf.keras.Model(inputs = input_words, outputs = softmax)
    
    return GPT


def MinimalGPT(data_path='.', 
               learning_rate=0, 
               output_length=0, 
               epochs = 1, 
               batch_size = 1, 
               gpt_input=10, 
               d_model=128, 
               h=8, 
               decoder_stacks=1, 
               starting_chunk = 0,
               ending_chunk = 5,
               chunk_size = 10,
               token_end=40000,
               vocabulary_start = 0,
               vocabulary_end = 40000,
               save=False, 
               load_tokenizer=None, 
               load_weights=None, 
               save_tokenizer=None,
               save_weights=None,
               optimizer=None,
               inference_only = False,
               return_model_and_vectorizer = False,
               return_model_and_vectorizer_and_output = False,
               GPT_attention = False,
               TPU = False):
    
    if chunk_size:
        chunk_size *= 1024
    
    
    if inference_only == False:
        with open(data_path, 'r', encoding = 'utf-8') as file:
            corpus = file.read()
            #file_contents = corpus.split()[token_start : token_end]
            #print("Total tokens: " + str(len(file_contents)))
            
    
    if load_tokenizer:
            with open(load_tokenizer, 'r') as f:
                encoded_vocabulary = json.load(f)

            # Decode the encoded vocabulary to original strings
            vocabulary = [word.encode('utf-8').decode('unicode_escape') for word in encoded_vocabulary]
            vectorizer = tf.keras.layers.TextVectorization(standardize = None, split = 'whitespace')
            vectorizer.set_vocabulary(vocabulary)
            vocab_size = vectorizer.vocabulary_size()
            
    else:
        vocab = []
        for word in tqdm(corpus.split()[vocabulary_start : vocabulary_end]):
            vocab += [word]
            vocab = list(set(vocab))
        vocab_size = len(vocab)
        vectorizer = tf.keras.layers.TextVectorization(standardize = None, split = 'whitespace', vocabulary = vocab)
        print('New Vectorizer created successfully...')
        print("Vocabulary Size: " + str(vocab_size))   
        del corpus
        
    
    #if inference_only == False:
    #    input_tokens, output_tokens = [], []
    #    for i in tqdm(range(len(file_contents) - gpt_input - 1)):
    #        input_tokens += [file_contents[i : i + gpt_input]]
    #        output_tokens += [file_contents[i + gpt_input]]
               
            
    #    X = [' '.join(input_tokens[i]) for i in tqdm(range(len(input_tokens)))]
    #    Y = output_tokens
    
    #    del corpus
    
    #   X = vectorizer(X)
    #    Y = vectorizer(Y)
    
    if load_weights:
        model = get_model(gpt_input = gpt_input, d_model = d_model, h = h, decoder_stacks = decoder_stacks, vocab_size = vocab_size - 2, GPT_attention = GPT_attention)
        
        with open(load_weights, 'rb') as file:
            W = pickle.load(file)
            model.set_weights(W)
    else:
        model = get_model(gpt_input = gpt_input, d_model = d_model, h = h, decoder_stacks = decoder_stacks, vocab_size = vocab_size, GPT_attention = GPT_attention)
    
    
    print(model.summary())
    
    
    if inference_only == False:
        # Compile the model
        if not optimizer:
            model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), loss='sparse_categorical_crossentropy')
        else:
            model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy')
        
        # Train the model
        if learning_rate > 0:
            
            for epoch in tqdm(range(epochs)):
                
                with open(data_path, 'r', encoding='utf-8') as f:
                    chunk_number = 1
                    for chunk in read_file(f,
                                           vectorizer,
                                           chunk_size,
                                           starting_chunk,
                                           ending_chunk,
                                           gpt_input):
                        print('Chunk_size: ' + str(chunk.shape[0]))
                        model.fit(chunk[:, :gpt_input], tf.reshape(chunk[:, -1], (-1, 1)), batch_size = batch_size, epochs=1)
                        print("Chunk Number " + str(chunk_number) + "/" +str(ending_chunk - starting_chunk + 1) + " processed!")
                        chunk_number += 1
        
    
    # Print the output of the Model
    output_seq = generate_output(gpt_input = gpt_input, model = model, vectorizer = vectorizer, text_size = output_length, input_sequence = [])
        
    if save == True and TPU == False:
        print('Saveeeeee')
        
        save_module(save_weights, model, vectorizer, save_tokenizer)
        
    if save == True and TPU == True:
        
        return save_weights, model, vectorizer, save_tokenizer, output_seq
        # Save the GPT Model
        #with open(save_weights, 'wb') as file:
        #    pickle.dump(model.weights, file)
        
        #Save the Vectorizer Model
        #vocabulary = vectorizer.get_vocabulary()

        # Encode the vocabulary as JSON-compatible strings
        #encoded_vocabulary = [word.encode('unicode_escape').decode('utf-8') for word in vocabulary]
        #encoded_vocabulary = encoded_vocabulary[2:]

        # Save the encoded vocabulary to a JSON file
        #with open(save_tokenizer, 'w') as f:
        #    json.dump(encoded_vocabulary, f)
        #    print("Vocabulary size saved: " + str(len(encoded_vocabulary)))
            
       
    if return_model_and_vectorizer:
        return model, vectorizer
    elif return_model_and_vectorizer_and_output:
        return model, vectorizer, output_seq.replace('@@ ', '')
    else:
        return output_seq.replace('@@ ', '')



# Example code to execute when the script file is called

def main():
    print("This code is executed when the script file is called directly.")

# Check if the script is being run as the main module
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--data-path', help='File: Corresponding to corpus or training text [String]')
    parser.add_argument('-l', '--learning-rate', help='Float: Learning Rate. The model will train ONLY IF the rate is > 0, skip otherwise [Float]', type=float)
    parser.add_argument('-ol', '--output-length', help='Length of the output sequence to be generated', type=int)
    parser.add_argument('-e', '--epochs', help='Number of training Epochs [Int]', type=int)
    parser.add_argument('-b', '--batch-size', help='Size of each batch [Int]', type=int)
    parser.add_argument('-s', '--gpt-input', help='Number of Tokens of text the model inputs at a time [Int]', type=int)
    parser.add_argument('-dm', '--d-model', help='Embedding layer output dimensions [Int]', type=int)
    parser.add_argument('-p', '--multi-head', help='Number of Multi-head Attention layer in parallel [Int]', type=int)
    parser.add_argument('-ds', '--decoder-stacks', help='Number of stacked Decoder layer [Int]', type=int)
    parser.add_argument('-sc', '--chunk-start', help='The chunk number in the corpus to mark it as the starting point of the training [Int]', type=int)
    parser.add_argument('-ec', '--chunk-end', help='The chunk number in the corpus to mark it as the end point of the training [Int]', type=int)
    parser.add_argument('-csz', '--chunk-size', help='The size of each chunk in KB.', type=int)
    parser.add_argument('-vs', '--vocabulary-start', help='Token number from the corpus to mark the starting point of vocabulary data [Int]', type=int)
    parser.add_argument('-ve', '--vocabulary-end', help='Token number from the corpus to mark the end point of vocabulary data [Int]', type=int)
    parser.add_argument('-sd', '--save', help='Save the Model and Vectorizer data to disk [True/False]', action='store_true')
    parser.add_argument('-lt', '--load-tokenizer', help='File: Vectorization layer [File]')
    parser.add_argument('-lw', '--load-weights', help='File: Model Weights [File]')
    parser.add_argument('-st', '--save-tokenizer', help='File: Saving Vectorizer File [File]')
    parser.add_argument('-sw', '--save-weights', help='File: Saving Model Weights[File]')
    parser.add_argument('-ot', '--optimizer', help='Optimizer consistent to TensorFlow optimizer class [tf.keras.optimizers]')
    parser.add_argument('-i', '--inference-only', help='Only Print the output of the model in Inference Mode [True/False]', action='store_true')
    parser.add_argument('-mv', '--model-vectorizer', help='Return Model, Vectorizer Tuple [True/False]', action='store_true')
    parser.add_argument('-mvo', '--model-vectorizer-output', help='Return Model, Vectorizer, Output Tuple [True/False]', action='store_true')
    parser.add_argument('-ga', '--gpt-style-attention', help='Uses GPT-styled attention. Note: (d-model) parameter should be divisible by (multi-head), otherwise the program will throw an error! [True/False]', action='store_true')
    parser.add_argument('-tpu', '--TPU', help='Use Tensor Processor Units (Distributed Learning)', action='store_true')
    
    
    args = parser.parse_args()
    
    
    data_path = args.data_path
    learning_rate = args.learning_rate
    output_length = args.output_length
    epochs = args.epochs
    batch_size = args.batch_size
    gpt_input = args.gpt_input
    d_model = args.d_model
    h = args.multi_head
    stacks = args.decoder_stacks
    chunk_start = args.chunk_start
    chunk_end = args.chunk_end
    chunk_size = args.chunk_size
    vocabulary_start = args.vocabulary_start
    vocabulary_end = args.vocabulary_end
    save = args.save
    load_tokenizer = args.load_tokenizer
    load_weights = args.load_weights
    save_tokenizer = args.save_tokenizer
    save_weights = args.save_weights
    optimizer = args.optimizer
    inference_only = args.inference_only
    model_and_vectorizer = args.model_vectorizer
    GPT_attention = args.gpt_style_attention
    model_vectorizer_output = args.model_vectorizer_output
    
    
    
    configuration = {
    'data_path': args.data_path,
    'learning_rate': args.learning_rate,
    'output_length': args.output_length,
    'epochs': args.epochs,
    'batch_size': args.batch_size,
    'gpt_input': args.gpt_input,
    'd_model': args.d_model,
    'h': args.multi_head,
    'stacks': args.decoder_stacks,
    'chunk_start': args.chunk_start,
    'chunk_end': args.chunk_end,
    'chunk_size': args.chunk_size,
    'vocabulary_start': args.vocabulary_start,
    'vocabulary_end': args.vocabulary_end,
    'save': args.save,
    'load_tokenizer': args.load_tokenizer,
    'load_weights': args.load_weights,
    'save_tokenizer': args.save_tokenizer,
    'save_weights': args.save_weights,
    'optimizer': args.optimizer,
    'inference_only': args.inference_only,
    'model_and_vectorizer': args.model_vectorizer,
    'model_vectorizer_output': args.model_vectorizer_output,
    'GPT_Attention' : args.gpt_style_attention
    }

    # Save the configuration to a JSON file
    with open('last-configuration.json', 'w') as file:
        json.dump(configuration, file)
        
    
    
    if args.TPU == True:
        
        resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')
        tf.config.experimental_connect_to_cluster(resolver)
        # This is the TPU initialization code that has to be at the beginning.
        tf.tpu.experimental.initialize_tpu_system(resolver)
        print("All devices: ", tf.config.list_logical_devices('TPU'))
        
        
        strategy = tf.distribute.TPUStrategy(resolver)
        
        with strategy.scope():
            
            output = MinimalGPT(data_path = data_path, 
                   learning_rate = learning_rate, 
                   output_length = output_length,
                   epochs = epochs, 
                   batch_size = batch_size, 
                   gpt_input = gpt_input, 
                   d_model = d_model, 
                   h = h, 
                   decoder_stacks = stacks, 
                   starting_chunk = chunk_start,
                   ending_chunk = chunk_end,
                   chunk_size = chunk_size,
                   vocabulary_start = vocabulary_start,
                   vocabulary_end = vocabulary_end,
                   save = save, 
                   load_tokenizer = load_tokenizer, 
                   load_weights = load_weights, 
                   save_tokenizer = save_tokenizer,
                   save_weights = save_weights,
                   optimizer = optimizer,
                   inference_only = inference_only,
                   return_model_and_vectorizer = model_and_vectorizer,
                   return_model_and_vectorizer_and_output = model_vectorizer_output,
                   GPT_attention = GPT_attention, 
                   TPU = True)
    
        save_module(output[0], output[1], output[2], output[3])
        
        print(output[4])
        sys.exit(0)
    
    
    output = MinimalGPT(data_path = data_path, 
                       learning_rate = learning_rate, 
                       output_length = output_length,
                       epochs = epochs, 
                       batch_size = batch_size, 
                       gpt_input = gpt_input, 
                       d_model = d_model, 
                       h = h, 
                       decoder_stacks = stacks, 
                       starting_chunk = chunk_start,
                       ending_chunk = chunk_end,
                       chunk_size = chunk_size,
                       vocabulary_start = vocabulary_start,
                       vocabulary_end = vocabulary_end,
                       save = save, 
                       load_tokenizer = load_tokenizer, 
                       load_weights = load_weights, 
                       save_tokenizer = save_tokenizer,
                       save_weights = save_weights,
                       optimizer = optimizer,
                       inference_only = inference_only,
                       return_model_and_vectorizer = model_and_vectorizer,
                       return_model_and_vectorizer_and_output = model_vectorizer_output,
                       GPT_attention = GPT_attention,
                       TPU = False)
    print(output)