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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1"
   },
   "source": [
    "# A Masked Language Model for Project CodeNet\n",
    "\n",
    "> Copyright (c) 2021 International Business Machines Corporation  \n",
    "Prepared by [Geert Janssen](geert@us.ibm.com>)\n",
    "\n",
    "## Introduction\n",
    "\n",
    "This experiment investigates whether a popular attention model to\n",
    "construct a masked language model (MLM) can be used for source code\n",
    "instead of natural language sentences. We here closely follow the\n",
    "approach by Ankur Singh documented in his\n",
    "[blog](https://keras.io/examples/nlp/masked_language_modeling).\n",
    "\n",
    "The goal of the model is to be able to infer the correct token for a\n",
    "masked-out token at an arbitrary position in the source text.\n",
    "We will use the special token literal `[mask]` to represent the masked\n",
    "out token. We assume that in the training and test sets precisely one token\n",
    "is randomly masked per sample. The original token at that position is\n",
    "then the golden label, or ground truth."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cellView": "form",
    "id": "cjzhCC12YNfP"
   },
   "outputs": [],
   "source": [
    "#@title Imports\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras.layers.experimental.preprocessing import TextVectorization\n",
    "from tensorflow.keras import Sequential, Model, losses, metrics, optimizers\n",
    "from tensorflow.keras import callbacks\n",
    "from tensorflow.keras.layers import MultiHeadAttention, LayerNormalization\n",
    "from tensorflow.keras.layers import Dense, Dropout, Input, Embedding\n",
    "from dataclasses import dataclass\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import glob\n",
    "import random\n",
    "import tarfile\n",
    "import requests\n",
    "import os\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2"
   },
   "source": [
    "## Dataset\n",
    "\n",
    "The Project CodeNet dataset consist of a large collection (close to 14 million) of\n",
    "submissions in various programming languages to problems posed on\n",
    "online judging sites. The submissions are typically small, complete\n",
    "programs in a single source file. There are 1000s of problems in this dataset.\n",
    "\n",
    "We extract a selection of C programming language files from the\n",
    "Project CodeNet dataset for training and evaluation:\n",
    "\n",
    "| Aspect              | Value |\n",
    "| ------------------- | ----- |\n",
    "| purpose             | training |\n",
    "| submission status   | Accepted |\n",
    "| smallest size       | 200 bytes |\n",
    "| largest size        | 500 bytes |\n",
    "| samples per problem | at most 100 |\n",
    "| problems            | 0-3417 |\n",
    "| total samples       | 50,000 |\n",
    "\n",
    "Notice that the training and evaluation are derived from\n",
    "non-overlapping sets of problems. It might also be interesting to see\n",
    "what happens when a different split is made, e.g. by selecting 110\n",
    "submissions from each problem and using 100 for training and the rest for evaluation.\n",
    "\n",
    "| Aspect              | Value |\n",
    "| ------------------- |------ |\n",
    "| purpose             | evaluation |\n",
    "| submission status   | Accepted |\n",
    "| smallest size       | 200 bytes |\n",
    "| largest size        | 500 bytes |\n",
    "| samples per problem | at most 100 |\n",
    "| problems            | 3418-3636 |\n",
    "| total samples       | 5,000 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download the subset of the Project CodeNet data described above.\n",
    "file_name = \"Project_CodeNet_MLM.tar.gz\"\n",
    "data_url = f\"https://dax-cdn.cdn.appdomain.cloud/dax-project-codenet/1.0.0/{file_name}\"\n",
    "\n",
    "# Download tar archive to local disk\n",
    "if os.path.exists(file_name):\n",
    "    os.remove(file_name) \n",
    "with open(file_name, \"wb\") as f:\n",
    "    f.write(requests.get(data_url).content)\n",
    "    \n",
    "# Extract contents of archive to local disk\n",
    "if os.path.exists(\"tokens\"):\n",
    "    shutil.rmtree(\"tokens\")    \n",
    "with tarfile.open(file_name) as f:\n",
    "    f.extractall()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3"
   },
   "source": [
    "## Data preparation\n",
    "\n",
    "Each C file is tokenized into a vocabulary of 414 distinct tokens:\n",
    "\n",
    "| Type           | Count | Description |\n",
    "| -------------- | ----: | -- |\n",
    "|the keyword     |    95 | all C++20 reserved words |\n",
    "|the function    |   279 | function names in common header files |\n",
    "|the identifier  |    18 | standard identifiers, like stderr, etc. |\n",
    "|the punctuator  |    15 | small set of punctuation symbols |\n",
    "|# or ##         |     2 | the 2 C preprocessor symbols |\n",
    "|the token class |     5 | one of: id, number, operator, character, string |\n",
    "\n",
    "By _the keyword_, _the function_ and so on, we mean the actual keyword\n",
    "or function literal, like `while` for a keyword and `strlen` for a function.\n",
    "The tokens are output on a single line separated by spaces.\n",
    "It turns out that our training set overall uses some 200 out of the 414\n",
    "possible tokens; not all keywords and standard functions are used presumably.\n",
    "\n",
    "This code snippet:\n",
    "```C\n",
    "for (i = 0; i < strlen(s); i++) {}\n",
    "```\n",
    "\n",
    "will be converted to:\n",
    "```C\n",
    "for ( id = number ; id < strlen ( id ) ; id operator ) { }\n",
    "```\n",
    "\n",
    "The tokenized source files are read into a pandas dataframe and\n",
    "processed by the Keras `TextVectorization` layer to extract a vocabulary\n",
    "and encode all token lines into vocabulary indices. Index 0 is\n",
    "reserved for padding; index 1 is the `<UNK>` value for Out-Of-Vocabulary\n",
    "tokens (not used in our case since the input vocabulary is of fixed\n",
    "size); the last index (least frequent position) is dedicated to encode\n",
    "the special `[mask]` token. Each sample will have a fixed token length\n",
    "of 256. The average number of tokens per sample across the training\n",
    "set is 131. Short samples are padded with 0 and too large ones are\n",
    "simply truncated. The same operations will be applied to the test set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xaKaJk5wr4LD",
    "outputId": "40c85f5c-eb40-4579-e25e-03487cde4e9d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                              tokens\n",
      "0  # include < id . id > # include < id . id > in...\n",
      "1  # include < id . id > int main ( void ) { int ...\n",
      "2  # include < id . id > # include < id . id > in...\n",
      "3  # include < id . id > int id ( int id , int id...\n",
      "4  long long id [ number ] [ number ] , id , id ;...\n"
     ]
    }
   ],
   "source": [
    "# Read all files and return content as list of lines.\n",
    "def get_text_list_from_files(files):\n",
    "    text_list = []\n",
    "    for name in files:\n",
    "        with open(name) as f:\n",
    "            for line in f:\n",
    "                text_list.append(line)\n",
    "    return text_list\n",
    "\n",
    "# Compose the full path names to the token files.\n",
    "# Creates and returns a dataframe with single key \"tokens\".\n",
    "def get_data_from_text_files(folder_name):\n",
    "    files = glob.glob(folder_name + '/*.toks')\n",
    "    texts = get_text_list_from_files(files)\n",
    "    df = pd.DataFrame({'tokens': texts})\n",
    "    df = df.sample(len(df)).reset_index(drop=True)\n",
    "    return df\n",
    "\n",
    "train_data = get_data_from_text_files('tokens/train')\n",
    "print(train_data.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "rFNaRXWmsOwp"
   },
   "source": [
    "Let's collect all configuration parameters in a one place:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "jl8CauE9sWLG"
   },
   "outputs": [],
   "source": [
    "@dataclass\n",
    "class Config:\n",
    "    MAX_LEN = 256               # length of each input sample in tokens\n",
    "    BATCH_SIZE = 32             # batch size\n",
    "    LR = 0.001                  # learning rate\n",
    "    VOCAB_SIZE = 256            # max. number of words in vocabulary\n",
    "    EMBED_DIM = 128             # word embedding vector size\n",
    "    NUM_HEAD = 8                # number of attention heads (BERT)\n",
    "    FF_DIM = 128                # feedforward dimension (BERT)\n",
    "    NUM_LAYERS = 1              # number of BERT module layers\n",
    "\n",
    "config = Config()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "SdOiGnv7shXC"
   },
   "source": [
    "We use the Keras TextVectorization layer to process all data and extract a vocabulary of tokens to which we add the special `[mask]` token:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "wGjbPQq8swbW",
    "outputId": "98adfc09-673b-4de0-bc08-70420c115100"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vocabulary size: 203\n",
      "padding token vocab[0]: \"\"\n",
      "OOV token vocab[1]: \"[UNK]\"\n",
      "mask token vocab[202]: \"[mask]\"\n"
     ]
    }
   ],
   "source": [
    "# No special text filtering.\n",
    "def custom_standardization(input_data):\n",
    "    return input_data\n",
    "\n",
    "# Create TextVectorization layer.\n",
    "def get_vectorize_layer(texts, vocab_size, max_seq):\n",
    "    vectorize_layer = TextVectorization(\n",
    "        max_tokens=vocab_size,\n",
    "        output_mode='int',\n",
    "        standardize=custom_standardization,\n",
    "        output_sequence_length=max_seq,\n",
    "    )\n",
    "    # Create vocabulary over all texts:\n",
    "    vectorize_layer.adapt(texts)\n",
    "    # Insert special mask token in vocabulary:\n",
    "    vocab = vectorize_layer.get_vocabulary()\n",
    "    vocab = vocab[2:len(vocab)-1] + ['[mask]']\n",
    "    vectorize_layer.set_vocabulary(vocab)\n",
    "    return vectorize_layer\n",
    "\n",
    "vectorize_layer = get_vectorize_layer(\n",
    "    train_data.tokens.values.tolist(),\n",
    "    config.VOCAB_SIZE,\n",
    "    config.MAX_LEN,\n",
    ")\n",
    "\n",
    "vocab = vectorize_layer.get_vocabulary()\n",
    "print('vocabulary size:', len(vocab))\n",
    "print('padding token vocab[0]: \"%s\"' % vocab[0])\n",
    "print('OOV token vocab[1]: \"%s\"' % vocab[1])\n",
    "print('mask token vocab[%d]: \"%s\"' % (len(vocab)-1, vocab[len(vocab)-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ehiNdjVktMV9",
    "outputId": "dfadd95e-e056-4fd7-cd41-5b2e856335e3"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mask_token_id: 202\n",
      "x_all_tokens.shape: (50000, 256)\n"
     ]
    }
   ],
   "source": [
    "# Encode the token strings to int vocab indices.\n",
    "def encode(texts):\n",
    "    encoded_texts = vectorize_layer(texts)\n",
    "    return encoded_texts.numpy()\n",
    "\n",
    "# Get mask token id for masked language model\n",
    "mask_token_id = encode(['[mask]'])[0][0]\n",
    "print('mask_token_id:', mask_token_id) # (always last index in vocab)\n",
    "\n",
    "# Randomly replace tokens by the [mask] and keep replaced token as label.\n",
    "def get_masked_input_and_labels(encoded_texts):\n",
    "    # These numbers come from something called the \"BERT recipe\":\n",
    "    # 15% used for prediction. 80% of that is masked. 10% is random token,\n",
    "    # 10% is just left as is.\n",
    "\n",
    "    # 15% masking:\n",
    "    inp_mask = np.random.rand(*encoded_texts.shape) < 0.15\n",
    "    # Do not mask special tokens:\n",
    "    inp_mask[encoded_texts < 2] = False\n",
    "    # Set targets to -1 by default, it means ignore:\n",
    "    labels = -1 * np.ones(encoded_texts.shape, dtype=int)\n",
    "    # Set golden labels for the masked tokens:\n",
    "    labels[inp_mask] = encoded_texts[inp_mask]\n",
    "    # False positions -> -1, True -> encoded word (vocab index)\n",
    "\n",
    "    # Prepare input\n",
    "    encoded_texts_masked = np.copy(encoded_texts)\n",
    "    # Set input to [mask] for 90% of tokens (leaving 10% unchanged):\n",
    "    inp_mask_2mask = inp_mask & (np.random.rand(*encoded_texts.shape) < 0.90)\n",
    "    encoded_texts_masked[inp_mask_2mask] = mask_token_id\n",
    "\n",
    "    # Set 10% to a random token\n",
    "    inp_mask_2random = inp_mask_2mask & (np.random.rand(*encoded_texts.shape) < 1 / 9)\n",
    "    encoded_texts_masked[inp_mask_2random] = np.random.randint(\n",
    "        2, mask_token_id, inp_mask_2random.sum())\n",
    "\n",
    "    # Prepare sample_weights to pass to .fit() method:\n",
    "    sample_weights = np.ones(encoded_texts.shape)\n",
    "    sample_weights[labels == -1] = 0\n",
    "\n",
    "    # y_labels would be same as encoded_texts, i.e., input tokens\n",
    "    y_labels = np.copy(encoded_texts)\n",
    "\n",
    "    return encoded_texts_masked, y_labels, sample_weights\n",
    "\n",
    "# Prepare data for masked language model\n",
    "\n",
    "# Encoding step:\n",
    "x_all_tokens = encode(train_data.tokens.values)\n",
    "print('x_all_tokens.shape:', x_all_tokens.shape)\n",
    "\n",
    "# Masking step:\n",
    "x_masked_train, y_masked_labels, sample_weights = get_masked_input_and_labels(\n",
    "    x_all_tokens\n",
    ")\n",
    "\n",
    "mlm_ds = (\n",
    "    tf.data.Dataset.from_tensor_slices(\n",
    "        (x_masked_train, y_masked_labels, sample_weights))\n",
    "    .shuffle(1000)\n",
    "    .batch(config.BATCH_SIZE)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4"
   },
   "source": [
    "## Model\n",
    "\n",
    "As mentioned above, the BERT-like model is copied from the Keras\n",
    "example \"End-to-end Masked Language Modeling with BERT\" by Ankur Singh, implemented in this [Jupyter Notebook](https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/nlp/ipynb/masked_language_modeling.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xDJIPbEptaYW",
    "outputId": "f7a544b4-25bb-4b84-cd95-b92ee5f2a15e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"masked_bert_model\"\n",
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "input_1 (InputLayer)            [(None, 256)]        0                                            \n",
      "__________________________________________________________________________________________________\n",
      "word_embedding (Embedding)      (None, 256, 128)     32768       input_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "tf.__operators__.add (TFOpLambd (None, 256, 128)     0           word_embedding[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "encoder_0/multiheadattention (M (None, 256, 128)     66048       tf.__operators__.add[0][0]       \n",
      "                                                                 tf.__operators__.add[0][0]       \n",
      "                                                                 tf.__operators__.add[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "encoder_0/att_dropout (Dropout) (None, 256, 128)     0           encoder_0/multiheadattention[0][0\n",
      "__________________________________________________________________________________________________\n",
      "tf.__operators__.add_1 (TFOpLam (None, 256, 128)     0           tf.__operators__.add[0][0]       \n",
      "                                                                 encoder_0/att_dropout[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "encoder_0/att_layernormalizatio (None, 256, 128)     256         tf.__operators__.add_1[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "encoder_0/ffn (Sequential)      (None, 256, 128)     33024       encoder_0/att_layernormalization[\n",
      "__________________________________________________________________________________________________\n",
      "encoder_0/ffn_dropout (Dropout) (None, 256, 128)     0           encoder_0/ffn[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "tf.__operators__.add_2 (TFOpLam (None, 256, 128)     0           encoder_0/att_layernormalization[\n",
      "                                                                 encoder_0/ffn_dropout[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "encoder_0/ffn_layernormalizatio (None, 256, 128)     256         tf.__operators__.add_2[0][0]     \n",
      "__________________________________________________________________________________________________\n",
      "mlm_cls (Dense)                 (None, 256, 256)     33024       encoder_0/ffn_layernormalization[\n",
      "==================================================================================================\n",
      "Total params: 165,376\n",
      "Trainable params: 165,376\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "def bert_module(query, key, value, i):\n",
    "    # Multi headed self-attention\n",
    "    att_out = MultiHeadAttention(\n",
    "        num_heads=config.NUM_HEAD,\n",
    "        key_dim=config.EMBED_DIM // config.NUM_HEAD,\n",
    "        name='encoder_{}/multiheadattention'.format(i))(query, key, value)\n",
    "    att_out = Dropout(0.1, name='encoder_{}/att_dropout'.format(i))(att_out)\n",
    "    att_out = LayerNormalization(\n",
    "        epsilon=1e-6,\n",
    "        name='encoder_{}/att_layernormalization'.format(i))(query + att_out)\n",
    "\n",
    "    # Feed-forward layer\n",
    "    ffn = Sequential([\n",
    "            Dense(config.FF_DIM, activation='relu'),\n",
    "            Dense(config.EMBED_DIM)\n",
    "            ], name='encoder_{}/ffn'.format(i))\n",
    "    ffn_out = ffn(att_out)\n",
    "    ffn_out = Dropout(0.1, name='encoder_{}/ffn_dropout'.format(i))(ffn_out)\n",
    "    sequence_output = LayerNormalization(\n",
    "        epsilon=1e-6,\n",
    "        name='encoder_{}/ffn_layernormalization'.format(i))(att_out + ffn_out)\n",
    "    return sequence_output\n",
    "\n",
    "def get_pos_encoding_matrix(max_len, d_emb):\n",
    "    pos_enc = np.array(\n",
    "        [\n",
    "            [pos / np.power(10000, 2 * (j // 2) / d_emb) for j in range(d_emb)]\n",
    "            if pos != 0\n",
    "            else np.zeros(d_emb)\n",
    "            for pos in range(max_len)\n",
    "        ]\n",
    "    )\n",
    "    # 0::2 means start at 0 and step 2 (all even)\n",
    "    pos_enc[1:, 0::2] = np.sin(pos_enc[1:, 0::2])  # dim 2i\n",
    "    pos_enc[1:, 1::2] = np.cos(pos_enc[1:, 1::2])  # dim 2i+1\n",
    "    return pos_enc\n",
    "\n",
    "loss_fn = losses.SparseCategoricalCrossentropy(\n",
    "    reduction=losses.Reduction.NONE\n",
    "    )\n",
    "loss_tracker = metrics.Mean(name='loss')\n",
    "\n",
    "class MaskedLanguageModel(Model):\n",
    "    def train_step(self, inputs):\n",
    "        if len(inputs) == 3:\n",
    "            features, labels, sample_weight = inputs\n",
    "        else:\n",
    "            features, labels = inputs\n",
    "            sample_weight = None\n",
    "\n",
    "        with tf.GradientTape() as tape:\n",
    "            predictions = self(features, training=True)\n",
    "            loss = loss_fn(labels, predictions, sample_weight=sample_weight)\n",
    "\n",
    "        # Compute gradients:\n",
    "        trainable_vars = self.trainable_variables\n",
    "        gradients = tape.gradient(loss, trainable_vars)\n",
    "\n",
    "        # Update weights:\n",
    "        self.optimizer.apply_gradients(zip(gradients, trainable_vars))\n",
    "\n",
    "        # Compute our own metrics:\n",
    "        loss_tracker.update_state(loss, sample_weight=sample_weight)\n",
    "\n",
    "        # Return a dict mapping metric names to current value\n",
    "        return {'loss': loss_tracker.result()}\n",
    "\n",
    "    @property\n",
    "    def metrics(self):\n",
    "        # We list our `Metric` objects here so that `reset_states()` can be\n",
    "        # called automatically at the start of each epoch\n",
    "        # or at the start of `evaluate()`.\n",
    "        # If you don't implement this property, you have to call\n",
    "        # `reset_states()` yourself at the time of your choosing.\n",
    "        return [loss_tracker]\n",
    "\n",
    "def create_masked_language_bert_model():\n",
    "    inputs = Input((config.MAX_LEN,), dtype=tf.int64)\n",
    "\n",
    "    word_embeddings = Embedding(\n",
    "        input_dim=config.VOCAB_SIZE,\n",
    "        output_dim=config.EMBED_DIM,\n",
    "        name='word_embedding')(inputs)\n",
    "\n",
    "    position_embeddings = Embedding(\n",
    "        input_dim=config.MAX_LEN,\n",
    "        output_dim=config.EMBED_DIM,\n",
    "        weights=[get_pos_encoding_matrix(config.MAX_LEN, config.EMBED_DIM)],\n",
    "        name='position_embedding',\n",
    "        )(tf.range(start=0, limit=config.MAX_LEN, delta=1))\n",
    "\n",
    "    encoder_out = word_embeddings + position_embeddings\n",
    "\n",
    "    for i in range(config.NUM_LAYERS):\n",
    "        encoder_out = bert_module(encoder_out, encoder_out, encoder_out, i)\n",
    "\n",
    "    mlm_output = Dense(config.VOCAB_SIZE, name='mlm_cls',\n",
    "                       activation='softmax')(encoder_out)\n",
    "    mlm_model = MaskedLanguageModel(inputs, mlm_output,\n",
    "                                    name='masked_bert_model')\n",
    "    optimizer = optimizers.Adam(learning_rate=config.LR)\n",
    "    mlm_model.compile(optimizer=optimizer)\n",
    "    return mlm_model\n",
    "\n",
    "mlm_model = create_masked_language_bert_model()\n",
    "mlm_model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5"
   },
   "source": [
    "## Training\n",
    "\n",
    "The model is trained with 50,000 samples in batches of 32 (1563\n",
    "batches per epoch) over 5 epochs with a learning rate of 0.001 using the\n",
    "Adam optimizer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 528
    },
    "id": "Giycf1_xtnX8",
    "outputId": "829e91cd-2275-4e9c-9ecf-a478b3117378"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train BERT MLM model on Project CodeNet:\n",
      "Epoch 1/5\n",
      "1563/1563 [==============================] - 465s 297ms/step - loss: 1.9673\n",
      "Epoch 2/5\n",
      "1563/1563 [==============================] - 497s 318ms/step - loss: 0.7906\n",
      "Epoch 3/5\n",
      "1563/1563 [==============================] - 493s 316ms/step - loss: 0.5638\n",
      "Epoch 4/5\n",
      "1563/1563 [==============================] - 495s 317ms/step - loss: 0.4734\n",
      "Epoch 5/5\n",
      "1563/1563 [==============================] - 463s 296ms/step - loss: 0.4187\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVUAAAFACAYAAAAMF+8GAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAAxV0lEQVR4nO3de1hUBf4/8PcwAwx3GUZAQFyvCd5QUdRVF3LEtrzQTzQ3s4u5RVhGWmuSmmYmq2KsKV8tTbfLlpmadldSUxetDCHFTaUsNVDioiD3mTm/P4iREXS4nJk5M/N+PU/P03DOzLwd492Zc/vIBEEQQEREonCydgAiInvCUiUiEhFLlYhIRCxVIiIRsVSJiETEUiUiEhFLlazq4MGDkMlkuHTpUqueJ5PJ8M4775gplfXei2yfwtoByDbIZLLbLu/SpQt++eWXVr/uiBEjUFBQAH9//1Y9r6CgAB06dGj1+xGZG0uVWqSgoMDw75mZmZg8eTKysrLQqVMnAIBcLjdav7a2Fi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WKhGRiFiqREQiYqkSEYmIpUpEJCKWKhGRiFiqREQi+v99/JRwtQwdugAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 864x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print('Train BERT MLM model on Project CodeNet:')\n",
    "history = mlm_model.fit(mlm_ds, epochs=5)\n",
    "#mlm_model.save('bert_mlm_codenet.h5')\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('ggplot')\n",
    "\n",
    "def plot_history(history):\n",
    "    loss = history.history['loss']\n",
    "    x = range(1, len(loss) + 1)\n",
    "\n",
    "    plt.figure(figsize=(12, 5))\n",
    "    plt.subplot(1, 2, 1)\n",
    "    plt.plot(x, loss, 'b', label='Training loss')\n",
    "    plt.title('Training loss')\n",
    "    plt.legend()\n",
    "    plt.show()\n",
    "\n",
    "plot_history(history)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6"
   },
   "source": [
    "## Evaluation\n",
    "\n",
    "We evaluate the trained model on a test set of 5,000 samples taken\n",
    "from problems not considered for the training set. Each sample is\n",
    "preprocessed in the same way as the training samples and one token\n",
    "(never a padding!) is arbitrarily replaced by the `[mask]`. Then a\n",
    "prediction is generated and the top 1 and top 5 results are compared\n",
    "with the expected value. The achieved accuracies are printed in the end.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5Rbd460Tt4fI",
    "outputId": "af5f397a-ab5b-470b-db97-0d33ef541c23"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                              tokens\n",
      "0  # include < id . id > int main ( void ) { int ...\n",
      "1  # include < id . id > int main ( ) { int id , ...\n",
      "2  # include < id . id > int main ( ) { char id [...\n",
      "3  # include < id . id > int main ( void ) { int ...\n",
      "4  # include < id . id > int main ( void ) { char...\n",
      "number of test samples: 5000\n",
      "top-1 accuracy: 0.9248\n",
      "top-5 accuracy: 0.9958\n"
     ]
    }
   ],
   "source": [
    "# Load pretrained bert model\n",
    "#from tensorflow import keras\n",
    "#mlm_model = keras.models.load_model('bert_mlm_codenet.h5',\n",
    "#    custom_objects={'MaskedLanguageModel': MaskedLanguageModel})\n",
    "mlm_model.trainable = False\n",
    "\n",
    "# token<->id mappings as dicts:\n",
    "id2token = dict(enumerate(vocab))\n",
    "token2id = {y: x for x, y in id2token.items()}\n",
    "\n",
    "# Turns text into list of vocabulary indices.\n",
    "def prep(text):\n",
    "    R = [0] * config.MAX_LEN # all padding\n",
    "    text = text.split()\n",
    "    ntoks = len(text)\n",
    "    if ntoks > config.MAX_LEN:\n",
    "        ntoks = config.MAX_LEN\n",
    "        text = text[:ntoks]\n",
    "    # pick random position (never a padding):\n",
    "    k = random.randint(0, ntoks-1)\n",
    "    golden = 0\n",
    "    for i in range(len(text)):\n",
    "        w = text[i]\n",
    "        if w in token2id:\n",
    "            R[i] = token2id[w]\n",
    "        else:\n",
    "            R[i] = 1 # OOV: [UNK]\n",
    "        if i == k:\n",
    "            golden = R[i]\n",
    "            R[i] = mask_token_id\n",
    "    return k, golden, np.array(R)\n",
    "\n",
    "def predict(text):\n",
    "    mask_index, golden, R = prep(text)\n",
    "    sample = np.reshape(R, (1, config.MAX_LEN))\n",
    "    prediction = mlm_model.predict(sample)\n",
    "    # all substitute word probabilities:\n",
    "    mask_prediction = prediction[0][mask_index]\n",
    "    # word indices with top-k highest probabilities:\n",
    "    top_k = 5\n",
    "    top_indices = mask_prediction.argsort()[-top_k:][::-1]\n",
    "    # probabilities of the top_k\n",
    "    values = mask_prediction[top_indices]\n",
    "    correct_top1 = top_indices[0] == golden\n",
    "    correct_top5 = False\n",
    "    for i in range(len(top_indices)):\n",
    "        if top_indices[i] == golden:\n",
    "            correct_top5 = True\n",
    "            break\n",
    "    return correct_top1, correct_top5\n",
    "\n",
    "# Enumerate all test samples:\n",
    "test_data = get_data_from_text_files('tokens/test')\n",
    "print(test_data.head())\n",
    "\n",
    "correct_top1 = 0\n",
    "correct_top5 = 0\n",
    "num_tests = 0\n",
    "for test in test_data['tokens']:\n",
    "    # predict and check\n",
    "    top1, top5 = predict(test)\n",
    "    if top1:\n",
    "        correct_top1+=1\n",
    "    if top5:\n",
    "        correct_top5+=1\n",
    "    num_tests+=1\n",
    "\n",
    "print('number of test samples:', num_tests)\n",
    "print('top-1 accuracy:', correct_top1/num_tests);\n",
    "print('top-5 accuracy:', correct_top5/num_tests);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7"
   },
   "source": [
    "## References\n",
    "\n",
    "> <a id=\"1\">[1]</a>\n",
    "Ankur Singh,\n",
    "[\"End-to-end Masked Language Modeling with BERT\"](https://keras.io/examples/nlp/masked_language_modeling)\n",
    "\n",
    "> <a id=\"2\">[2]</a>\n",
    "[CodeXGLUE -- Code Completion (token level)](https://github.com/microsoft/CodeXGLUE/tree/main/Code-Code/CodeCompletion-token)\n",
    "\n",
    "> <a id=\"3\">[3]</a>\n",
    "[AllenNLP Demo on Masked Language Modeling](https://demo.allennlp.org/masked-lm)\n",
    "\n",
    "> <a id=\"4\">[4]</a>\n",
    "Zhangyin Feng, Daya Guo, Duyu Tang, Nan Duan, Xiaocheng Feng, Ming Gong,\n",
    "Linjun Shou, Bing Qin, Ting Liu, Daxin Jiang, Ming Zhou,\n",
    "[CodeBERT: A Pre-Trained Model for Programming and Natural Languages](https://arxiv.org/abs/2002.08155)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "Project_CodeNet_MLM.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}