File size: 96,433 Bytes
d4d4f16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6bYaCABobL5q"
   },
   "source": [
    "##### Copyright 2021 The TensorFlow Authors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cellView": "form",
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:18.997731Z",
     "iopub.status.busy": "2022-12-14T03:38:18.997516Z",
     "iopub.status.idle": "2022-12-14T03:38:19.001439Z",
     "shell.execute_reply": "2022-12-14T03:38:19.000889Z"
    },
    "id": "FlUw7tSKbtg4"
   },
   "outputs": [],
   "source": [
    "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "MfBg1C5NB3X0"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/migrate/validate_correctness\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/migrate/validate_correctness.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/validate_correctness.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/guide/migrate/validate_correctness.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "yAMJsAn7NDbc"
   },
   "source": [
    "# Validating correctness & numerical equivalence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vyddl2kckpdN"
   },
   "source": [
    "When migrating your TensorFlow code from TF1.x to TF2, it is a good practice to ensure that your migrated code behaves the same way in TF2 as it did in TF1.x. \n",
    "\n",
    "This guide covers migration code examples with the `tf.compat.v1.keras.utils.track_tf1_style_variables` modeling shim applied to `tf.keras.layers.Layer` methods. Read the [model mapping guide](./model_mapping.ipynb) to find out more about the TF2 modeling shims.\n",
    "\n",
    "This guide details approaches you can use to: \n",
    "* Validate the correctness of the results obtained from training models using the migrated code \n",
    "* Validate the numerical equivalence of your code across TensorFlow versions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TaYgaekzOAHf"
   },
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:19.005343Z",
     "iopub.status.busy": "2022-12-14T03:38:19.004839Z",
     "iopub.status.idle": "2022-12-14T03:38:21.539848Z",
     "shell.execute_reply": "2022-12-14T03:38:21.538733Z"
    },
    "id": "FkHX044DzVsd"
   },
   "outputs": [],
   "source": [
    "!pip uninstall -y -q tensorflow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:21.544536Z",
     "iopub.status.busy": "2022-12-14T03:38:21.543790Z",
     "iopub.status.idle": "2022-12-14T03:38:44.840458Z",
     "shell.execute_reply": "2022-12-14T03:38:44.839299Z"
    },
    "id": "M1ZgieHtyzKI"
   },
   "outputs": [],
   "source": [
    "# Install tf-nightly as the DeterministicRandomTestTool is available only in\n",
    "# Tensorflow 2.8\n",
    "!pip install -q tf-nightly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:44.845262Z",
     "iopub.status.busy": "2022-12-14T03:38:44.844580Z",
     "iopub.status.idle": "2022-12-14T03:38:46.873821Z",
     "shell.execute_reply": "2022-12-14T03:38:46.872724Z"
    },
    "id": "ohYETq4NCX4J"
   },
   "outputs": [],
   "source": [
    "!pip install -q tf_slim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:46.878200Z",
     "iopub.status.busy": "2022-12-14T03:38:46.877941Z",
     "iopub.status.idle": "2022-12-14T03:38:49.308586Z",
     "shell.execute_reply": "2022-12-14T03:38:49.307853Z"
    },
    "id": "MFey2HxcktP6"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-12-14 03:38:47.140140: E tensorflow/tsl/lib/monitoring/collection_registry.cc:81] Cannot register 2 metrics with the same name: /tensorflow/core/bfc_allocator_delay\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import tensorflow.compat.v1 as v1\n",
    "\n",
    "import numpy as np\n",
    "import tf_slim as slim\n",
    "import sys\n",
    "\n",
    "\n",
    "from contextlib import contextmanager"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:49.312559Z",
     "iopub.status.busy": "2022-12-14T03:38:49.312144Z",
     "iopub.status.idle": "2022-12-14T03:38:53.362680Z",
     "shell.execute_reply": "2022-12-14T03:38:53.361644Z"
    },
    "id": "OriidSSAmRtW"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cloning into 'models'...\r\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Enumerating objects: 3590, done.\u001b[K\r\n",
      "remote: Counting objects:   0% (1/3590)\u001b[K\r",
      "remote: Counting objects:   1% (36/3590)\u001b[K\r",
      "remote: Counting objects:   2% (72/3590)\u001b[K\r",
      "remote: Counting objects:   3% (108/3590)\u001b[K\r",
      "remote: Counting objects:   4% (144/3590)\u001b[K\r",
      "remote: Counting objects:   5% (180/3590)\u001b[K\r",
      "remote: Counting objects:   6% (216/3590)\u001b[K\r",
      "remote: Counting objects:   7% (252/3590)\u001b[K\r",
      "remote: Counting objects:   8% (288/3590)\u001b[K\r",
      "remote: Counting objects:   9% (324/3590)\u001b[K\r",
      "remote: Counting objects:  10% (359/3590)\u001b[K\r",
      "remote: Counting objects:  11% (395/3590)\u001b[K\r",
      "remote: Counting objects:  12% (431/3590)\u001b[K\r",
      "remote: Counting objects:  13% (467/3590)\u001b[K\r",
      "remote: Counting objects:  14% (503/3590)\u001b[K\r",
      "remote: Counting objects:  15% (539/3590)\u001b[K\r",
      "remote: Counting objects:  16% (575/3590)\u001b[K\r",
      "remote: Counting objects:  17% (611/3590)\u001b[K\r",
      "remote: Counting objects:  18% (647/3590)\u001b[K\r",
      "remote: Counting objects:  19% (683/3590)\u001b[K\r",
      "remote: Counting objects:  20% (718/3590)\u001b[K\r",
      "remote: Counting objects:  21% (754/3590)\u001b[K\r",
      "remote: Counting objects:  22% (790/3590)\u001b[K\r",
      "remote: Counting objects:  23% (826/3590)\u001b[K\r",
      "remote: Counting objects:  24% (862/3590)\u001b[K\r",
      "remote: Counting objects:  25% (898/3590)\u001b[K\r",
      "remote: Counting objects:  26% (934/3590)\u001b[K\r",
      "remote: Counting objects:  27% (970/3590)\u001b[K\r",
      "remote: Counting objects:  28% (1006/3590)\u001b[K\r",
      "remote: Counting objects:  29% (1042/3590)\u001b[K\r",
      "remote: Counting objects:  30% (1077/3590)\u001b[K\r",
      "remote: Counting objects:  31% (1113/3590)\u001b[K\r",
      "remote: Counting objects:  32% (1149/3590)\u001b[K\r",
      "remote: Counting objects:  33% (1185/3590)\u001b[K\r",
      "remote: Counting objects:  34% (1221/3590)\u001b[K\r",
      "remote: Counting objects:  35% (1257/3590)\u001b[K\r",
      "remote: Counting objects:  36% (1293/3590)\u001b[K\r",
      "remote: Counting objects:  37% (1329/3590)\u001b[K\r",
      "remote: Counting objects:  38% (1365/3590)\u001b[K\r",
      "remote: Counting objects:  39% (1401/3590)\u001b[K\r",
      "remote: Counting objects:  40% (1436/3590)\u001b[K\r",
      "remote: Counting objects:  41% (1472/3590)\u001b[K\r",
      "remote: Counting objects:  42% (1508/3590)\u001b[K\r",
      "remote: Counting objects:  43% (1544/3590)\u001b[K\r",
      "remote: Counting objects:  44% (1580/3590)\u001b[K\r",
      "remote: Counting objects:  45% (1616/3590)\u001b[K\r",
      "remote: Counting objects:  46% (1652/3590)\u001b[K\r",
      "remote: Counting objects:  47% (1688/3590)\u001b[K\r",
      "remote: Counting objects:  48% (1724/3590)\u001b[K\r",
      "remote: Counting objects:  49% (1760/3590)\u001b[K\r",
      "remote: Counting objects:  50% (1795/3590)\u001b[K\r",
      "remote: Counting objects:  51% (1831/3590)\u001b[K\r",
      "remote: Counting objects:  52% (1867/3590)\u001b[K\r",
      "remote: Counting objects:  53% (1903/3590)\u001b[K\r",
      "remote: Counting objects:  54% (1939/3590)\u001b[K\r",
      "remote: Counting objects:  55% (1975/3590)\u001b[K\r",
      "remote: Counting objects:  56% (2011/3590)\u001b[K\r",
      "remote: Counting objects:  57% (2047/3590)\u001b[K\r",
      "remote: Counting objects:  58% (2083/3590)\u001b[K\r",
      "remote: Counting objects:  59% (2119/3590)\u001b[K\r",
      "remote: Counting objects:  60% (2154/3590)\u001b[K\r",
      "remote: Counting objects:  61% (2190/3590)\u001b[K\r",
      "remote: Counting objects:  62% (2226/3590)\u001b[K\r",
      "remote: Counting objects:  63% (2262/3590)\u001b[K\r",
      "remote: Counting objects:  64% (2298/3590)\u001b[K\r",
      "remote: Counting objects:  65% (2334/3590)\u001b[K\r",
      "remote: Counting objects:  66% (2370/3590)\u001b[K\r",
      "remote: Counting objects:  67% (2406/3590)\u001b[K\r",
      "remote: Counting objects:  68% (2442/3590)\u001b[K\r",
      "remote: Counting objects:  69% (2478/3590)\u001b[K\r",
      "remote: Counting objects:  70% (2513/3590)\u001b[K\r",
      "remote: Counting objects:  71% (2549/3590)\u001b[K\r",
      "remote: Counting objects:  72% (2585/3590)\u001b[K\r",
      "remote: Counting objects:  73% (2621/3590)\u001b[K\r",
      "remote: Counting objects:  74% (2657/3590)\u001b[K\r",
      "remote: Counting objects:  75% (2693/3590)\u001b[K\r",
      "remote: Counting objects:  76% (2729/3590)\u001b[K\r",
      "remote: Counting objects:  77% (2765/3590)\u001b[K\r",
      "remote: Counting objects:  78% (2801/3590)\u001b[K\r",
      "remote: Counting objects:  79% (2837/3590)\u001b[K\r",
      "remote: Counting objects:  80% (2872/3590)\u001b[K\r",
      "remote: Counting objects:  81% (2908/3590)\u001b[K\r",
      "remote: Counting objects:  82% (2944/3590)\u001b[K\r",
      "remote: Counting objects:  83% (2980/3590)\u001b[K\r",
      "remote: Counting objects:  84% (3016/3590)\u001b[K\r",
      "remote: Counting objects:  85% (3052/3590)\u001b[K\r",
      "remote: Counting objects:  86% (3088/3590)\u001b[K\r",
      "remote: Counting objects:  87% (3124/3590)\u001b[K\r",
      "remote: Counting objects:  88% (3160/3590)\u001b[K\r",
      "remote: Counting objects:  89% (3196/3590)\u001b[K\r",
      "remote: Counting objects:  90% (3231/3590)\u001b[K\r",
      "remote: Counting objects:  91% (3267/3590)\u001b[K\r",
      "remote: Counting objects:  92% (3303/3590)\u001b[K\r",
      "remote: Counting objects:  93% (3339/3590)\u001b[K\r",
      "remote: Counting objects:  94% (3375/3590)\u001b[K\r",
      "remote: Counting objects:  95% (3411/3590)\u001b[K\r",
      "remote: Counting objects:  96% (3447/3590)\u001b[K\r",
      "remote: Counting objects:  97% (3483/3590)\u001b[K\r",
      "remote: Counting objects:  98% (3519/3590)\u001b[K\r",
      "remote: Counting objects:  99% (3555/3590)\u001b[K\r",
      "remote: Counting objects: 100% (3590/3590)\u001b[K\r",
      "remote: Counting objects: 100% (3590/3590), done.\u001b[K\r\n",
      "remote: Compressing objects:   0% (1/3005)\u001b[K\r",
      "remote: Compressing objects:   1% (31/3005)\u001b[K\r",
      "remote: Compressing objects:   2% (61/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:   3% (91/3005)\u001b[K\r",
      "remote: Compressing objects:   4% (121/3005)\u001b[K\r",
      "remote: Compressing objects:   5% (151/3005)\u001b[K\r",
      "remote: Compressing objects:   6% (181/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:   7% (211/3005)\u001b[K\r",
      "remote: Compressing objects:   8% (241/3005)\u001b[K\r",
      "remote: Compressing objects:   9% (271/3005)\u001b[K\r",
      "remote: Compressing objects:  10% (301/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  11% (331/3005)\u001b[K\r",
      "remote: Compressing objects:  12% (361/3005)\u001b[K\r",
      "remote: Compressing objects:  13% (391/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  14% (421/3005)\u001b[K\r",
      "remote: Compressing objects:  15% (451/3005)\u001b[K\r",
      "remote: Compressing objects:  16% (481/3005)\u001b[K\r",
      "remote: Compressing objects:  17% (511/3005)\u001b[K\r",
      "remote: Compressing objects:  18% (541/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  19% (571/3005)\u001b[K\r",
      "remote: Compressing objects:  20% (601/3005)\u001b[K\r",
      "remote: Compressing objects:  21% (632/3005)\u001b[K\r",
      "remote: Compressing objects:  22% (662/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  23% (692/3005)\u001b[K\r",
      "remote: Compressing objects:  24% (722/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  25% (752/3005)\u001b[K\r",
      "remote: Compressing objects:  26% (782/3005)\u001b[K\r",
      "remote: Compressing objects:  27% (812/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  28% (842/3005)\u001b[K\r",
      "remote: Compressing objects:  29% (872/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  30% (902/3005)\u001b[K\r",
      "remote: Compressing objects:  31% (932/3005)\u001b[K\r",
      "remote: Compressing objects:  32% (962/3005)\u001b[K\r",
      "remote: Compressing objects:  33% (992/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  34% (1022/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  35% (1052/3005)\u001b[K\r",
      "remote: Compressing objects:  36% (1082/3005)\u001b[K\r",
      "remote: Compressing objects:  37% (1112/3005)\u001b[K\r",
      "remote: Compressing objects:  38% (1142/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  39% (1172/3005)\u001b[K\r",
      "remote: Compressing objects:  40% (1202/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  41% (1233/3005)\u001b[K\r",
      "remote: Compressing objects:  42% (1263/3005)\u001b[K\r",
      "remote: Compressing objects:  43% (1293/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  44% (1323/3005)\u001b[K\r",
      "remote: Compressing objects:  45% (1353/3005)\u001b[K\r",
      "remote: Compressing objects:  46% (1383/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  47% (1413/3005)\u001b[K\r",
      "remote: Compressing objects:  48% (1443/3005)\u001b[K\r",
      "remote: Compressing objects:  49% (1473/3005)\u001b[K\r",
      "remote: Compressing objects:  50% (1503/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  51% (1533/3005)\u001b[K\r",
      "remote: Compressing objects:  52% (1563/3005)\u001b[K\r",
      "remote: Compressing objects:  53% (1593/3005)\u001b[K\r",
      "remote: Compressing objects:  54% (1623/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  55% (1653/3005)\u001b[K\r",
      "remote: Compressing objects:  56% (1683/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  57% (1713/3005)\u001b[K\r",
      "remote: Compressing objects:  58% (1743/3005)\u001b[K\r",
      "remote: Compressing objects:  59% (1773/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  60% (1803/3005)\u001b[K\r",
      "remote: Compressing objects:  61% (1834/3005)\u001b[K\r",
      "remote: Compressing objects:  62% (1864/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  63% (1894/3005)\u001b[K\r",
      "remote: Compressing objects:  64% (1924/3005)\u001b[K\r",
      "remote: Compressing objects:  65% (1954/3005)\u001b[K\r",
      "remote: Compressing objects:  66% (1984/3005)\u001b[K\r",
      "remote: Compressing objects:  67% (2014/3005)\u001b[K\r",
      "remote: Compressing objects:  68% (2044/3005)\u001b[K\r",
      "remote: Compressing objects:  69% (2074/3005)\u001b[K\r",
      "remote: Compressing objects:  70% (2104/3005)\u001b[K\r",
      "remote: Compressing objects:  71% (2134/3005)\u001b[K\r",
      "remote: Compressing objects:  72% (2164/3005)\u001b[K\r",
      "remote: Compressing objects:  73% (2194/3005)\u001b[K\r",
      "remote: Compressing objects:  73% (2207/3005)\u001b[K\r",
      "remote: Compressing objects:  74% (2224/3005)\u001b[K\r",
      "remote: Compressing objects:  75% (2254/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  76% (2284/3005)\u001b[K\r",
      "remote: Compressing objects:  77% (2314/3005)\u001b[K\r",
      "remote: Compressing objects:  78% (2344/3005)\u001b[K\r",
      "remote: Compressing objects:  79% (2374/3005)\u001b[K\r",
      "remote: Compressing objects:  80% (2404/3005)\u001b[K\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remote: Compressing objects:  81% (2435/3005)\u001b[K\r",
      "remote: Compressing objects:  82% (2465/3005)\u001b[K\r",
      "remote: Compressing objects:  83% (2495/3005)\u001b[K\r",
      "remote: Compressing objects:  84% (2525/3005)\u001b[K\r",
      "remote: Compressing objects:  85% (2555/3005)\u001b[K\r",
      "remote: Compressing objects:  86% (2585/3005)\u001b[K\r",
      "remote: Compressing objects:  87% (2615/3005)\u001b[K\r",
      "remote: Compressing objects:  88% (2645/3005)\u001b[K\r",
      "remote: Compressing objects:  89% (2675/3005)\u001b[K\r",
      "remote: Compressing objects:  90% (2705/3005)\u001b[K\r",
      "remote: Compressing objects:  91% (2735/3005)\u001b[K\r",
      "remote: Compressing objects:  92% (2765/3005)\u001b[K\r",
      "remote: Compressing objects:  93% (2795/3005)\u001b[K\r",
      "remote: Compressing objects:  94% (2825/3005)\u001b[K\r",
      "remote: Compressing objects:  95% (2855/3005)\u001b[K\r",
      "remote: Compressing objects:  96% (2885/3005)\u001b[K\r",
      "remote: Compressing objects:  97% (2915/3005)\u001b[K\r",
      "remote: Compressing objects:  98% (2945/3005)\u001b[K\r",
      "remote: Compressing objects:  99% (2975/3005)\u001b[K\r",
      "remote: Compressing objects: 100% (3005/3005)\u001b[K\r",
      "remote: Compressing objects: 100% (3005/3005), done.\u001b[K\r\n",
      "Receiving objects:   0% (1/3590)\r",
      "Receiving objects:   1% (36/3590)\r",
      "Receiving objects:   2% (72/3590)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:   3% (108/3590)\r",
      "Receiving objects:   4% (144/3590)\r",
      "Receiving objects:   5% (180/3590)\r",
      "Receiving objects:   6% (216/3590)\r",
      "Receiving objects:   7% (252/3590)\r",
      "Receiving objects:   8% (288/3590)\r",
      "Receiving objects:   9% (324/3590)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  10% (359/3590)\r",
      "Receiving objects:  11% (395/3590)\r",
      "Receiving objects:  12% (431/3590)\r",
      "Receiving objects:  13% (467/3590)\r",
      "Receiving objects:  14% (503/3590)\r",
      "Receiving objects:  15% (539/3590)\r",
      "Receiving objects:  16% (575/3590)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  17% (611/3590)\r",
      "Receiving objects:  18% (647/3590)\r",
      "Receiving objects:  19% (683/3590)\r",
      "Receiving objects:  20% (718/3590)\r",
      "Receiving objects:  21% (754/3590)\r",
      "Receiving objects:  22% (790/3590)\r",
      "Receiving objects:  23% (826/3590)\r",
      "Receiving objects:  24% (862/3590)\r",
      "Receiving objects:  25% (898/3590)\r",
      "Receiving objects:  26% (934/3590)\r",
      "Receiving objects:  27% (970/3590)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  28% (1006/3590)\r",
      "Receiving objects:  29% (1042/3590)\r",
      "Receiving objects:  30% (1077/3590)\r",
      "Receiving objects:  31% (1113/3590)\r",
      "Receiving objects:  32% (1149/3590)\r",
      "Receiving objects:  33% (1185/3590)\r",
      "Receiving objects:  34% (1221/3590)\r",
      "Receiving objects:  35% (1257/3590)\r",
      "Receiving objects:  36% (1293/3590)\r",
      "Receiving objects:  37% (1329/3590)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  38% (1365/3590)\r",
      "Receiving objects:  39% (1401/3590)\r",
      "Receiving objects:  40% (1436/3590)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  41% (1472/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  42% (1508/3590), 4.73 MiB | 9.45 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  43% (1544/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  44% (1580/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  45% (1616/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  46% (1652/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  47% (1688/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  48% (1724/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  49% (1760/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  50% (1795/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  51% (1831/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  52% (1867/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  53% (1903/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  54% (1939/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  55% (1975/3590), 4.73 MiB | 9.45 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  56% (2011/3590), 4.73 MiB | 9.45 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  57% (2047/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  58% (2083/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  59% (2119/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  60% (2154/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  61% (2190/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  62% (2226/3590), 4.73 MiB | 9.45 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  63% (2262/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  64% (2298/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  65% (2334/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  66% (2370/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  67% (2406/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  68% (2442/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  69% (2478/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  70% (2513/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  71% (2549/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  72% (2585/3590), 4.73 MiB | 9.45 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  73% (2621/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  74% (2657/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  75% (2693/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  76% (2729/3590), 4.73 MiB | 9.45 MiB/s\r",
      "Receiving objects:  76% (2763/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  77% (2765/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  78% (2801/3590), 27.43 MiB | 27.42 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  79% (2837/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  80% (2872/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  81% (2908/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  82% (2944/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  83% (2980/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  84% (3016/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  85% (3052/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  86% (3088/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  87% (3124/3590), 27.43 MiB | 27.42 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  88% (3160/3590), 27.43 MiB | 27.42 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  89% (3196/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  90% (3231/3590), 27.43 MiB | 27.42 MiB/s\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Receiving objects:  91% (3267/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  92% (3303/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  93% (3339/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  94% (3375/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  95% (3411/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  96% (3447/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  97% (3483/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects:  98% (3519/3590), 27.43 MiB | 27.42 MiB/s\r",
      "remote: Total 3590 (delta 943), reused 1501 (delta 531), pack-reused 0\u001b[K\r\n",
      "Receiving objects:  99% (3555/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects: 100% (3590/3590), 27.43 MiB | 27.42 MiB/s\r",
      "Receiving objects: 100% (3590/3590), 47.08 MiB | 35.58 MiB/s, done.\r\n",
      "Resolving deltas:   0% (0/943)\r",
      "Resolving deltas:   1% (16/943)\r",
      "Resolving deltas:   3% (31/943)\r",
      "Resolving deltas:   5% (49/943)\r",
      "Resolving deltas:   6% (57/943)\r",
      "Resolving deltas:   7% (68/943)\r",
      "Resolving deltas:   8% (76/943)\r",
      "Resolving deltas:   9% (86/943)\r",
      "Resolving deltas:  10% (95/943)\r",
      "Resolving deltas:  11% (104/943)\r",
      "Resolving deltas:  12% (116/943)\r",
      "Resolving deltas:  13% (127/943)\r",
      "Resolving deltas:  14% (134/943)\r",
      "Resolving deltas:  15% (145/943)\r",
      "Resolving deltas:  16% (152/943)\r",
      "Resolving deltas:  17% (166/943)\r",
      "Resolving deltas:  18% (176/943)\r",
      "Resolving deltas:  19% (183/943)\r",
      "Resolving deltas:  20% (193/943)\r",
      "Resolving deltas:  21% (200/943)\r",
      "Resolving deltas:  22% (208/943)\r",
      "Resolving deltas:  23% (218/943)\r",
      "Resolving deltas:  24% (227/943)\r",
      "Resolving deltas:  25% (237/943)\r",
      "Resolving deltas:  26% (250/943)\r",
      "Resolving deltas:  27% (257/943)\r",
      "Resolving deltas:  29% (281/943)\r",
      "Resolving deltas:  30% (284/943)\r",
      "Resolving deltas:  31% (295/943)\r",
      "Resolving deltas:  32% (303/943)\r",
      "Resolving deltas:  33% (313/943)\r",
      "Resolving deltas:  34% (321/943)\r",
      "Resolving deltas:  35% (335/943)\r",
      "Resolving deltas:  36% (342/943)\r",
      "Resolving deltas:  37% (353/943)\r",
      "Resolving deltas:  38% (359/943)\r",
      "Resolving deltas:  39% (368/943)\r",
      "Resolving deltas:  40% (378/943)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resolving deltas:  41% (387/943)\r",
      "Resolving deltas:  42% (399/943)\r",
      "Resolving deltas:  43% (406/943)\r",
      "Resolving deltas:  45% (426/943)\r",
      "Resolving deltas:  46% (440/943)\r",
      "Resolving deltas:  47% (447/943)\r",
      "Resolving deltas:  48% (454/943)\r",
      "Resolving deltas:  49% (463/943)\r",
      "Resolving deltas:  50% (472/943)\r",
      "Resolving deltas:  51% (481/943)\r",
      "Resolving deltas:  52% (491/943)\r",
      "Resolving deltas:  53% (500/943)\r",
      "Resolving deltas:  54% (510/943)\r",
      "Resolving deltas:  55% (519/943)\r",
      "Resolving deltas:  56% (533/943)\r",
      "Resolving deltas:  57% (538/943)\r",
      "Resolving deltas:  58% (550/943)\r",
      "Resolving deltas:  59% (557/943)\r",
      "Resolving deltas:  60% (568/943)\r",
      "Resolving deltas:  61% (579/943)\r",
      "Resolving deltas:  62% (585/943)\r",
      "Resolving deltas:  63% (595/943)\r",
      "Resolving deltas:  64% (604/943)\r",
      "Resolving deltas:  65% (613/943)\r",
      "Resolving deltas:  66% (624/943)\r",
      "Resolving deltas:  67% (632/943)\r",
      "Resolving deltas:  68% (644/943)\r",
      "Resolving deltas:  69% (651/943)\r",
      "Resolving deltas:  70% (668/943)\r",
      "Resolving deltas:  72% (680/943)\r",
      "Resolving deltas:  73% (694/943)\r",
      "Resolving deltas:  74% (699/943)\r",
      "Resolving deltas:  75% (708/943)\r",
      "Resolving deltas:  76% (724/943)\r",
      "Resolving deltas:  77% (733/943)\r",
      "Resolving deltas:  78% (741/943)\r",
      "Resolving deltas:  79% (747/943)\r",
      "Resolving deltas:  80% (756/943)\r",
      "Resolving deltas:  81% (764/943)\r",
      "Resolving deltas:  82% (774/943)\r",
      "Resolving deltas:  83% (783/943)\r",
      "Resolving deltas:  84% (793/943)\r",
      "Resolving deltas:  85% (802/943)\r",
      "Resolving deltas:  86% (811/943)\r",
      "Resolving deltas:  87% (826/943)\r",
      "Resolving deltas:  88% (831/943)\r",
      "Resolving deltas:  89% (842/943)\r",
      "Resolving deltas:  90% (852/943)\r",
      "Resolving deltas:  91% (862/943)\r",
      "Resolving deltas:  92% (868/943)\r",
      "Resolving deltas:  93% (877/943)\r",
      "Resolving deltas:  94% (888/943)\r",
      "Resolving deltas:  95% (897/943)\r",
      "Resolving deltas:  96% (908/943)\r",
      "Resolving deltas:  97% (915/943)\r",
      "Resolving deltas:  98% (925/943)\r",
      "Resolving deltas:  99% (936/943)\r"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resolving deltas: 100% (943/943)\r",
      "Resolving deltas: 100% (943/943), done.\r\n"
     ]
    }
   ],
   "source": [
    "!git clone --depth=1 https://github.com/tensorflow/models.git\n",
    "import models.research.slim.nets.inception_resnet_v2 as inception"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TRacYNxnN-nk"
   },
   "source": [
    "If you're putting a nontrivial chunk of forward pass code into the shim, you want to know that it is behaving the same way as it did in TF1.x. For example, consider trying to put an entire TF-Slim Inception-Resnet-v2 model into the shim as such:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:53.366962Z",
     "iopub.status.busy": "2022-12-14T03:38:53.366681Z",
     "iopub.status.idle": "2022-12-14T03:38:53.370866Z",
     "shell.execute_reply": "2022-12-14T03:38:53.370295Z"
    },
    "id": "IijQZtxeaErg"
   },
   "outputs": [],
   "source": [
    "# TF1 Inception resnet v2 forward pass based on slim layers\n",
    "def inception_resnet_v2(inputs, num_classes, is_training):\n",
    "  with slim.arg_scope(\n",
    "    inception.inception_resnet_v2_arg_scope(batch_norm_scale=True)):\n",
    "    return inception.inception_resnet_v2(inputs, num_classes, is_training=is_training)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:53.374046Z",
     "iopub.status.busy": "2022-12-14T03:38:53.373505Z",
     "iopub.status.idle": "2022-12-14T03:38:53.697683Z",
     "shell.execute_reply": "2022-12-14T03:38:53.697035Z"
    },
    "id": "Z_-Oxg9OlSd4"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /tmpfs/tmp/ipykernel_118303/2131234657.py:8: The name tf.keras.utils.track_tf1_style_variables is deprecated. Please use tf.compat.v1.keras.utils.track_tf1_style_variables instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "class InceptionResnetV2(tf.keras.layers.Layer):\n",
    "  \"\"\"Slim InceptionResnetV2 forward pass as a Keras layer\"\"\"\n",
    "\n",
    "  def __init__(self, num_classes, **kwargs):\n",
    "    super().__init__(**kwargs)\n",
    "    self.num_classes = num_classes\n",
    "\n",
    "  @tf.compat.v1.keras.utils.track_tf1_style_variables\n",
    "  def call(self, inputs, training=None):\n",
    "    is_training = training or False \n",
    "    \n",
    "    # Slim does not accept `None` as a value for is_training,\n",
    "    # Keras will still pass `None` to layers to construct functional models\n",
    "    # without forcing the layer to always be in training or in inference.\n",
    "    # However, `None` is generally considered to run layers in inference.\n",
    "    \n",
    "    with slim.arg_scope(\n",
    "        inception.inception_resnet_v2_arg_scope(batch_norm_scale=True)):\n",
    "      return inception.inception_resnet_v2(\n",
    "          inputs, self.num_classes, is_training=is_training)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EqFmpktjlvh9"
   },
   "source": [
    "As it so happens, this layer actually works perfectly fine out of the box (complete with accurate regularization loss tracking). \n",
    "\n",
    "However, this is not something you want to take for granted. Follow the below steps to verify that it is actually behaving as it did in TF1.x, down to observing perfect numerical equivalence. These steps can also help you triangulate what part of the forward pass is causing a divergence from TF1.x (identify if the divergence arises in the model forward pass as opposed to a different part of the model)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mmgubd9vkevp"
   },
   "source": [
    "## Step 1: Verify variables are only created once\n",
    "\n",
    "The very first thing you should verify is that you have correctly built the model in a way that reuses variables in each call rather than accidentally creating and using new variables each time. For example, if your model creates a new Keras layer or calls `tf.Variable` in each forward pass call then it is most likely failing to capture variables and creating new ones each time.\n",
    "\n",
    "Below are two context manager scopes you can use to detect when your model is creating new variables and debug which part of the model is doing it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:53.701383Z",
     "iopub.status.busy": "2022-12-14T03:38:53.700896Z",
     "iopub.status.idle": "2022-12-14T03:38:53.707635Z",
     "shell.execute_reply": "2022-12-14T03:38:53.707033Z"
    },
    "id": "VMTfTXC0zW97"
   },
   "outputs": [],
   "source": [
    "@contextmanager\n",
    "def assert_no_variable_creations():\n",
    "  \"\"\"Assert no variables are created in this context manager scope.\"\"\"\n",
    "  def invalid_variable_creator(next_creator, **kwargs):\n",
    "    raise ValueError(\"Attempted to create a new variable instead of reusing an existing one. Args: {}\".format(kwargs))\n",
    "\n",
    "  with tf.variable_creator_scope(invalid_variable_creator):\n",
    "    yield\n",
    "\n",
    "@contextmanager\n",
    "def catch_and_raise_created_variables():\n",
    "  \"\"\"Raise all variables created within this context manager scope (if any).\"\"\"\n",
    "  created_vars = []\n",
    "  def variable_catcher(next_creator, **kwargs):\n",
    "    var = next_creator(**kwargs)\n",
    "    created_vars.append(var)\n",
    "    return var\n",
    "\n",
    "  with tf.variable_creator_scope(variable_catcher):\n",
    "    yield\n",
    "  if created_vars:\n",
    "    raise ValueError(\"Created vars:\", created_vars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WOKUtciktQqv"
   },
   "source": [
    "The first scope (`assert_no_variable_creations()`) will raise an error immediately once you try creating a variable within the scope. This allows you to inspect the stacktrace (and use interactive debugging) to figure out exactly what lines of code created a variable instead of reusing an existing one.\n",
    "\n",
    "The second scope (`catch_and_raise_created_variables()`) will raise an exception at the end of the scope if any variables ended up being created. This exception will include the list of all variables created in the scope. This is useful for figuring out what the set of all weights your model is creating is in case you can spot general patterns. However, it is less useful for identifying the exact lines of code where those variables got created.\n",
    "\n",
    "Use both scopes below to verify that the shim-based InceptionResnetV2 layer does not create any new variables after the first call (presumably reusing them)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:38:53.711113Z",
     "iopub.status.busy": "2022-12-14T03:38:53.710699Z",
     "iopub.status.idle": "2022-12-14T03:39:03.248779Z",
     "shell.execute_reply": "2022-12-14T03:39:03.248055Z"
    },
    "id": "O9FAGotiuLbK"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py:2212: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.\n",
      "  warnings.warn('`layer.apply` is deprecated and '\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/legacy_tf_layers/core.py:332: UserWarning: `tf.layers.flatten` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Flatten` instead.\n",
      "  warnings.warn('`tf.layers.flatten` is deprecated and '\n"
     ]
    }
   ],
   "source": [
    "model = InceptionResnetV2(1000)\n",
    "height, width = 299, 299\n",
    "num_classes = 1000\n",
    "\n",
    "inputs = tf.ones( (1, height, width, 3))\n",
    "# Create all weights on the first call\n",
    "model(inputs)\n",
    "\n",
    "# Verify that no new weights are created in followup calls\n",
    "with assert_no_variable_creations():\n",
    "  model(inputs)\n",
    "with catch_and_raise_created_variables():\n",
    "  model(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9ylT-EIhu1lK"
   },
   "source": [
    "In the example below, observe how these decorators work on a layer that incorrectly creates new weights each time instead of reusing existing ones."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:03.253141Z",
     "iopub.status.busy": "2022-12-14T03:39:03.252469Z",
     "iopub.status.idle": "2022-12-14T03:39:03.256619Z",
     "shell.execute_reply": "2022-12-14T03:39:03.256047Z"
    },
    "id": "gXqhPQWWtMAw"
   },
   "outputs": [],
   "source": [
    "class BrokenScalingLayer(tf.keras.layers.Layer):\n",
    "  \"\"\"Scaling layer that incorrectly creates new weights each time:\"\"\"\n",
    "\n",
    "  @tf.compat.v1.keras.utils.track_tf1_style_variables\n",
    "  def call(self, inputs):\n",
    "    var = tf.Variable(initial_value=2.0)\n",
    "    bias = tf.Variable(initial_value=2.0, name='bias')\n",
    "    return inputs * var + bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:03.260192Z",
     "iopub.status.busy": "2022-12-14T03:39:03.259627Z",
     "iopub.status.idle": "2022-12-14T03:39:03.277902Z",
     "shell.execute_reply": "2022-12-14T03:39:03.277304Z"
    },
    "id": "ztUKlMdGvHSq"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/tmpfs/tmp/ipykernel_118303/1128777590.py\", line 7, in <module>\n",
      "    model(inputs)\n",
      "  File \"/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/utils/traceback_utils.py\", line 70, in error_handler\n",
      "    raise e.with_traceback(filtered_tb) from None\n",
      "  File \"/tmpfs/tmp/ipykernel_118303/3224979076.py\", line 6, in call\n",
      "    var = tf.Variable(initial_value=2.0)\n",
      "  File \"/tmpfs/tmp/ipykernel_118303/1829430118.py\", line 5, in invalid_variable_creator\n",
      "    raise ValueError(\"Attempted to create a new variable instead of reusing an existing one. Args: {}\".format(kwargs))\n",
      "ValueError: Exception encountered when calling layer 'broken_scaling_layer' (type BrokenScalingLayer).\n",
      "\n",
      "Attempted to create a new variable instead of reusing an existing one. Args: {'initial_value': 2.0, 'trainable': None, 'validate_shape': True, 'caching_device': None, 'name': None, 'variable_def': None, 'dtype': None, 'import_scope': None, 'constraint': None, 'synchronization': <VariableSynchronization.AUTO: 0>, 'aggregation': <VariableAggregation.NONE: 0>, 'shape': None, 'experimental_enable_variable_lifting': None}\n",
      "\n",
      "Call arguments received by layer 'broken_scaling_layer' (type BrokenScalingLayer):\n",
      "  • inputs=tf.Tensor(shape=(1, 299, 299, 3), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "model = BrokenScalingLayer()\n",
    "inputs = tf.ones( (1, height, width, 3))\n",
    "model(inputs)\n",
    "\n",
    "try:\n",
    "  with assert_no_variable_creations():\n",
    "    model(inputs)\n",
    "except ValueError as err:\n",
    "  import traceback\n",
    "  traceback.print_exc()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:03.281111Z",
     "iopub.status.busy": "2022-12-14T03:39:03.280539Z",
     "iopub.status.idle": "2022-12-14T03:39:03.289140Z",
     "shell.execute_reply": "2022-12-14T03:39:03.288582Z"
    },
    "id": "6VyfMJ50vZqZ"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Created vars:', [<tf.Variable 'broken_scaling_layer_1/Variable:0' shape=() dtype=float32, numpy=2.0>, <tf.Variable 'broken_scaling_layer_1/bias:0' shape=() dtype=float32, numpy=2.0>])\n"
     ]
    }
   ],
   "source": [
    "model = BrokenScalingLayer()\n",
    "inputs = tf.ones( (1, height, width, 3))\n",
    "model(inputs)\n",
    "\n",
    "try:\n",
    "  with catch_and_raise_created_variables():\n",
    "    model(inputs)\n",
    "except ValueError as err:\n",
    "  print(err)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JDaiTArcv49M"
   },
   "source": [
    "You can fix the layer by making sure it only creates the weights once and then reuses them each time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:03.292412Z",
     "iopub.status.busy": "2022-12-14T03:39:03.291983Z",
     "iopub.status.idle": "2022-12-14T03:39:03.301456Z",
     "shell.execute_reply": "2022-12-14T03:39:03.300908Z"
    },
    "id": "FN1Oa10iviv8"
   },
   "outputs": [],
   "source": [
    "class FixedScalingLayer(tf.keras.layers.Layer):\n",
    "  \"\"\"Scaling layer that incorrectly creates new weights each time:\"\"\"\n",
    "  def __init__(self):\n",
    "    super().__init__()\n",
    "    self.var = None\n",
    "    self.bias = None\n",
    "\n",
    "  @tf.compat.v1.keras.utils.track_tf1_style_variables\n",
    "  def call(self, inputs):\n",
    "    if self.var is None:\n",
    "      self.var = tf.Variable(initial_value=2.0)\n",
    "      self.bias = tf.Variable(initial_value=2.0, name='bias')\n",
    "    return inputs * self.var + self.bias\n",
    "\n",
    "model = FixedScalingLayer()\n",
    "inputs = tf.ones( (1, height, width, 3))\n",
    "model(inputs)\n",
    "\n",
    "with assert_no_variable_creations():\n",
    "  model(inputs)\n",
    "with catch_and_raise_created_variables():\n",
    "  model(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "MuiZZ7ktwCcn"
   },
   "source": [
    "### Troubleshooting\n",
    "\n",
    "Here are some common reasons why your model might accidentally be creating new weights instead of reusing existing ones:\n",
    "\n",
    "1. It uses an explicit `tf.Variable` call without reusing already-created `tf.Variables`. Fix this by first checking if it has not been created then reusing the existing ones.\n",
    "2. It creates a Keras layer or model directly in the forward pass each time (as opposed to `tf.compat.v1.layers`). Fix this by first checking if it has not been created then reusing the existing ones.\n",
    "3. It is built on top of `tf.compat.v1.layers` but fails to assign all `compat.v1.layers` an explicit name or to wrap your `compat.v1.layer` usage inside of a named `variable_scope`, causing the autogenerated layer names to increment in each model call. Fix this by putting a named `tf.compat.v1.variable_scope` inside your shim-decorated method that wraps all of your `tf.compat.v1.layers` usage."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "V4iZLV9BnwKM"
   },
   "source": [
    "## Step 2: Check that variable counts, names, and shapes match\n",
    "\n",
    "The second step is to make sure your layer running in TF2 creates the same number of weights, with the same shapes, as the corresponding code does in TF1.x.\n",
    "\n",
    "You can do a mix of manually checking them to see that they match, and doing the checks programmatically in a unit test as shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:03.305013Z",
     "iopub.status.busy": "2022-12-14T03:39:03.304461Z",
     "iopub.status.idle": "2022-12-14T03:39:21.004861Z",
     "shell.execute_reply": "2022-12-14T03:39:21.004090Z"
    },
    "id": "m_aqag5fpun5"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer_v1.py:1694: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.\n",
      "  warnings.warn('`layer.apply` is deprecated and '\n"
     ]
    }
   ],
   "source": [
    "# Build the forward pass inside a TF1.x graph, and \n",
    "# get the counts, shapes, and names of the variables\n",
    "graph = tf.Graph()\n",
    "with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "  height, width = 299, 299\n",
    "  num_classes = 1000\n",
    "  inputs = tf.ones( (1, height, width, 3))\n",
    "\n",
    "  out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)\n",
    "\n",
    "  tf1_variable_names_and_shapes = {\n",
    "      var.name: (var.trainable, var.shape) for var in tf.compat.v1.global_variables()}\n",
    "  num_tf1_variables = len(tf.compat.v1.global_variables())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WT1-cm99vfNU"
   },
   "source": [
    "Next, do the same for the shim-wrapped layer in TF2.\n",
    "Notice that the model is also called multiple times before grabbing the weights. This is done to effectively test for variable reuse."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:21.008893Z",
     "iopub.status.busy": "2022-12-14T03:39:21.008637Z",
     "iopub.status.idle": "2022-12-14T03:39:24.226380Z",
     "shell.execute_reply": "2022-12-14T03:39:24.225669Z"
    },
    "id": "S7ND-lBSqmnE"
   },
   "outputs": [],
   "source": [
    "height, width = 299, 299\n",
    "num_classes = 1000\n",
    "\n",
    "model = InceptionResnetV2(num_classes)\n",
    "# The weights will not be created until you call the model\n",
    "\n",
    "inputs = tf.ones( (1, height, width, 3))\n",
    "# Call the model multiple times before checking the weights, to verify variables\n",
    "# get reused rather than accidentally creating additional variables\n",
    "out, endpoints = model(inputs, training=False)\n",
    "out, endpoints = model(inputs, training=False)\n",
    "\n",
    "# Grab the name: shape mapping and the total number of variables separately,\n",
    "# because in TF2 variables can be created with the same name\n",
    "num_tf2_variables = len(model.variables)\n",
    "tf2_variable_names_and_shapes = {\n",
    "    var.name: (var.trainable, var.shape) for var in model.variables}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:24.230091Z",
     "iopub.status.busy": "2022-12-14T03:39:24.229816Z",
     "iopub.status.idle": "2022-12-14T03:39:24.233961Z",
     "shell.execute_reply": "2022-12-14T03:39:24.233379Z"
    },
    "id": "pY2P_4wqsOYw"
   },
   "outputs": [],
   "source": [
    "# Verify that the variable counts, names, and shapes all match:\n",
    "assert num_tf1_variables == num_tf2_variables\n",
    "assert tf1_variable_names_and_shapes == tf2_variable_names_and_shapes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "N4YKJzSVwWkc"
   },
   "source": [
    "The shim-based InceptionResnetV2 layer passes this test. However, in the case where they don't match, you can run it through a diff (text or other) to see where the differences are.\n",
    "\n",
    "This can provide a clue as to what part of the model isn't behaving as expected. With eager execution you can use pdb, interactive debugging, and breakpoints to dig into the parts of the model that seem suspicious, and debug what is going wrong in more depth."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2gYrt-_0xpRM"
   },
   "source": [
    "### Troubleshooting\n",
    "\n",
    "* Pay close attention to the names of any variables created directly by explicit `tf.Variable` calls and Keras layers/models as their variable name generation semantics may differ slightly between TF1.x graphs and TF2 functionality such as eager execution and `tf.function` even if everything else is working properly. If this is the case for you, adjust your test to account for any slightly different naming semantics.\n",
    "\n",
    "* You may sometimes find that the `tf.Variable`s, `tf.keras.layers.Layer`s, or `tf.keras.Model`s created in your training loop's forward pass are missing from your TF2 variables list even if they were captured by the variables collection in TF1.x. Fix this by assigning the variables/layers/models that your forward pass creates to instance attributes in your model. See [here](https://www.tensorflow.org/guide/keras/custom_layers_and_models) for more info."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "fOQJ_hUGnzkq"
   },
   "source": [
    "## Step 3: Reset all variables, check numerical equivalence with all randomness disabled\n",
    "\n",
    "The next step is to verify numerical equivalence for both the actual outputs and the regularization loss tracking when you fix the model such that there is no random number generation involved (such as during inference).\n",
    "\n",
    "The exact way to do this may depend on your specific model, but in most models (such as this one), you can do this by:\n",
    "1. Initializing the weights to the same value with no randomness. This can be done by resetting them to a fixed value after they have been created.\n",
    "2. Running the model in inference mode to avoid triggering any dropout layers which can be sources of randomness.\n",
    "\n",
    "The following code demonstrates how you can compare the TF1.x and TF2 results this way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:24.237784Z",
     "iopub.status.busy": "2022-12-14T03:39:24.237280Z",
     "iopub.status.idle": "2022-12-14T03:39:49.563479Z",
     "shell.execute_reply": "2022-12-14T03:39:49.562787Z"
    },
    "id": "kL4PzD2Cxzmp"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: 0.001182976\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0.00299837, 0.00299837, 0.00299837, 0.00299837, 0.00299837],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph = tf.Graph()\n",
    "with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "  height, width = 299, 299\n",
    "  num_classes = 1000\n",
    "  inputs = tf.ones( (1, height, width, 3))\n",
    "\n",
    "  out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)\n",
    "\n",
    "  # Rather than running the global variable initializers,\n",
    "  # reset all variables to a constant value\n",
    "  var_reset = tf.group([var.assign(tf.ones_like(var) * 0.001) for var in tf.compat.v1.global_variables()])\n",
    "  sess.run(var_reset)\n",
    "\n",
    "  # Grab the outputs & regularization loss\n",
    "  reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
    "  tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
    "  tf1_output = sess.run(out)\n",
    "\n",
    "print(\"Regularization loss:\", tf1_regularization_loss)\n",
    "tf1_output[0][:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IKkoM_x72rUa"
   },
   "source": [
    "Get the TF2 results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:49.567413Z",
     "iopub.status.busy": "2022-12-14T03:39:49.567128Z",
     "iopub.status.idle": "2022-12-14T03:39:53.933510Z",
     "shell.execute_reply": "2022-12-14T03:39:53.932839Z"
    },
    "id": "kb086gJwzsNo"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: tf.Tensor(0.0011829757, shape=(), dtype=float32)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(5,), dtype=float32, numpy=\n",
       "array([0.00299837, 0.00299837, 0.00299837, 0.00299837, 0.00299837],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "height, width = 299, 299\n",
    "num_classes = 1000\n",
    "\n",
    "model = InceptionResnetV2(num_classes)\n",
    "\n",
    "inputs = tf.ones((1, height, width, 3))\n",
    "# Call the model once to create the weights\n",
    "out, endpoints = model(inputs, training=False)\n",
    "\n",
    "# Reset all variables to the same fixed value as above, with no randomness\n",
    "for var in model.variables:\n",
    "  var.assign(tf.ones_like(var) * 0.001)\n",
    "tf2_output, endpoints = model(inputs, training=False)\n",
    "\n",
    "# Get the regularization loss\n",
    "tf2_regularization_loss = tf.math.add_n(model.losses)\n",
    "\n",
    "print(\"Regularization loss:\", tf2_regularization_loss)\n",
    "tf2_output[0][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:53.936827Z",
     "iopub.status.busy": "2022-12-14T03:39:53.936548Z",
     "iopub.status.idle": "2022-12-14T03:39:53.939905Z",
     "shell.execute_reply": "2022-12-14T03:39:53.939251Z"
    },
    "id": "CUfWqlgIK6ej"
   },
   "outputs": [],
   "source": [
    "# Create a dict of tolerance values\n",
    "tol_dict={'rtol':1e-06, 'atol':1e-05}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:53.943322Z",
     "iopub.status.busy": "2022-12-14T03:39:53.942814Z",
     "iopub.status.idle": "2022-12-14T03:39:53.947107Z",
     "shell.execute_reply": "2022-12-14T03:39:53.946575Z"
    },
    "id": "R-C07eTo0WTr"
   },
   "outputs": [],
   "source": [
    "# Verify that the regularization loss and output both match\n",
    "# when we fix the weights and avoid randomness by running inference:\n",
    "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5UUq_Fuc2zDO"
   },
   "source": [
    "The numbers match between TF1.x and TF2 when you remove sources of randomness, and the TF2-compatible `InceptionResnetV2` layer passes the test.\n",
    "\n",
    "If you are observing the results diverging for your own models, you can use printing or pdb and interactive debugging to identify where and why the results start to diverge. Eager execution can make this significantly easier. You can also use an ablation approach to run only small portions of the model on fixed intermediate inputs and isolate where the divergence happens.\n",
    "\n",
    "Conveniently, many slim nets (and other models) also expose intermediate endpoints that you can probe."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "btRbak-0ou15"
   },
   "source": [
    "## Step 4: Align random number generation, check numerical equivalence in both training and inference\n",
    "\n",
    "The final step is to verify that the TF2 model numerically matches the TF1.x model, even when accounting for random number generation in variable initialization and in the forward pass itself (such as dropout layers during the forward pass).\n",
    "\n",
    "You can do this by using the testing tool below to make random number generation semantics match between TF1.x graphs/sessions and eager execution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jYq-JHiC39QC"
   },
   "source": [
    "TF1 legacy graphs/sessions and TF2 eager execution use different stateful random number generation semantics.\n",
    "\n",
    "In `tf.compat.v1.Session`s, if no seeds are specified, the random number generation depends on how many operations are in the graph at the time when the random operation is added, and how many times the graph is run. In eager execution, stateful random number generation depends on the global seed, the operation random seed, and how many times the operation with the operation with the given random seed is run. See \n",
    "`tf.random.set_seed` for more info."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BQbb8Hyk5YVi"
   },
   "source": [
    "The following [`v1.keras.utils.DeterministicRandomTestTool`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/keras/utils/DeterministicRandomTestTool) class provides a context manager `scope()` that can make stateful random operations use the same seed across both TF1 graphs/sessions and eager execution.\n",
    "\n",
    "The tool provides two testing modes: \n",
    "1. `constant` which uses the same seed for every single operation no matter how many times it has been called and,\n",
    "2. `num_random_ops` which uses the number of previously-observed stateful random operations as the operation seed.\n",
    "\n",
    "This applies both to the stateful random operations used for creating and initializing variables, and to the stateful random operations used in computation (such as for dropout layers)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "MoyZenhGHDA-"
   },
   "source": [
    "Generate three random tensors to show how to use this tool to make stateful random number generation match between sessions and eager execution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:53.950786Z",
     "iopub.status.busy": "2022-12-14T03:39:53.950284Z",
     "iopub.status.idle": "2022-12-14T03:39:53.989064Z",
     "shell.execute_reply": "2022-12-14T03:39:53.988502Z"
    },
    "id": "DDFfjrbXEWED"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[2.5063772],\n",
       "        [2.7488918],\n",
       "        [1.4839486]], dtype=float32),\n",
       " array([[2.5063772, 2.7488918, 1.4839486],\n",
       "        [1.5633398, 2.1358476, 1.3693532],\n",
       "        [0.3598416, 1.8287641, 2.5314465]], dtype=float32),\n",
       " array([[2.5063772, 2.7488918, 1.4839486],\n",
       "        [1.5633398, 2.1358476, 1.3693532],\n",
       "        [0.3598416, 1.8287641, 2.5314465]], dtype=float32))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
    "with random_tool.scope():\n",
    "  graph = tf.Graph()\n",
    "  with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "    a = tf.random.uniform(shape=(3,1))\n",
    "    a = a * 3\n",
    "    b = tf.random.uniform(shape=(3,3))\n",
    "    b = b * 3\n",
    "    c = tf.random.uniform(shape=(3,3))\n",
    "    c = c * 3\n",
    "    graph_a, graph_b, graph_c = sess.run([a, b, c])\n",
    "\n",
    "graph_a, graph_b, graph_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:53.992308Z",
     "iopub.status.busy": "2022-12-14T03:39:53.991806Z",
     "iopub.status.idle": "2022-12-14T03:39:54.011113Z",
     "shell.execute_reply": "2022-12-14T03:39:54.010470Z"
    },
    "id": "o9bkdPuTFpYr"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: shape=(3, 1), dtype=float32, numpy=\n",
       " array([[2.5063772],\n",
       "        [2.7488918],\n",
       "        [1.4839486]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
       " array([[2.5063772, 2.7488918, 1.4839486],\n",
       "        [1.5633398, 2.1358476, 1.3693532],\n",
       "        [0.3598416, 1.8287641, 2.5314465]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
       " array([[2.5063772, 2.7488918, 1.4839486],\n",
       "        [1.5633398, 2.1358476, 1.3693532],\n",
       "        [0.3598416, 1.8287641, 2.5314465]], dtype=float32)>)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
    "with random_tool.scope():\n",
    "  a = tf.random.uniform(shape=(3,1))\n",
    "  a = a * 3\n",
    "  b = tf.random.uniform(shape=(3,3))\n",
    "  b = b * 3\n",
    "  c = tf.random.uniform(shape=(3,3))\n",
    "  c = c * 3\n",
    "\n",
    "a, b, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.014331Z",
     "iopub.status.busy": "2022-12-14T03:39:54.013797Z",
     "iopub.status.idle": "2022-12-14T03:39:54.017855Z",
     "shell.execute_reply": "2022-12-14T03:39:54.017276Z"
    },
    "id": "qRJYFydsGIbF"
   },
   "outputs": [],
   "source": [
    "# Demonstrate that the generated random numbers match\n",
    "np.testing.assert_allclose(graph_a, a.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(graph_b, b.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(graph_c, c.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "J8IWCnS-WFrB"
   },
   "source": [
    "However, notice that in `constant` mode, because `b` and `c` were generated with the same seed and have the same shape, they will have exactly the same values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.021063Z",
     "iopub.status.busy": "2022-12-14T03:39:54.020543Z",
     "iopub.status.idle": "2022-12-14T03:39:54.024066Z",
     "shell.execute_reply": "2022-12-14T03:39:54.023479Z"
    },
    "id": "IdxV89q2WPid"
   },
   "outputs": [],
   "source": [
    "np.testing.assert_allclose(b.numpy(), c.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vQTm7joHHh57"
   },
   "source": [
    "### Trace order\n",
    "If you are worried about some random numbers matching in `constant` mode reducing your confidence in your numerical equivalence test (for example if several weights take on the same initializations), you can use the `num_random_ops` mode to avoid this. In the `num_random_ops` mode, the generated random numbers will depend on the ordering of random ops in the program."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.027409Z",
     "iopub.status.busy": "2022-12-14T03:39:54.026970Z",
     "iopub.status.idle": "2022-12-14T03:39:54.054591Z",
     "shell.execute_reply": "2022-12-14T03:39:54.054025Z"
    },
    "id": "L-AeD148VygJ"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[2.5063772],\n",
       "        [2.7488918],\n",
       "        [1.4839486]], dtype=float32),\n",
       " array([[0.45038545, 1.9197761 , 2.4536333 ],\n",
       "        [1.0371652 , 2.9898582 , 1.924583  ],\n",
       "        [0.25679827, 1.6579313 , 2.8418403 ]], dtype=float32),\n",
       " array([[2.9634383 , 1.0862181 , 2.6042497 ],\n",
       "        [0.70099247, 2.3920312 , 1.0470468 ],\n",
       "        [0.18173039, 0.8359269 , 1.0508587 ]], dtype=float32))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  graph = tf.Graph()\n",
    "  with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "    a = tf.random.uniform(shape=(3,1))\n",
    "    a = a * 3\n",
    "    b = tf.random.uniform(shape=(3,3))\n",
    "    b = b * 3\n",
    "    c = tf.random.uniform(shape=(3,3))\n",
    "    c = c * 3\n",
    "    graph_a, graph_b, graph_c = sess.run([a, b, c])\n",
    "\n",
    "graph_a, graph_b, graph_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.057715Z",
     "iopub.status.busy": "2022-12-14T03:39:54.057212Z",
     "iopub.status.idle": "2022-12-14T03:39:54.070419Z",
     "shell.execute_reply": "2022-12-14T03:39:54.069817Z"
    },
    "id": "CedD41NuVygK"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<tf.Tensor: shape=(3, 1), dtype=float32, numpy=\n",
       " array([[2.5063772],\n",
       "        [2.7488918],\n",
       "        [1.4839486]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
       " array([[0.45038545, 1.9197761 , 2.4536333 ],\n",
       "        [1.0371652 , 2.9898582 , 1.924583  ],\n",
       "        [0.25679827, 1.6579313 , 2.8418403 ]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(3, 3), dtype=float32, numpy=\n",
       " array([[2.9634383 , 1.0862181 , 2.6042497 ],\n",
       "        [0.70099247, 2.3920312 , 1.0470468 ],\n",
       "        [0.18173039, 0.8359269 , 1.0508587 ]], dtype=float32)>)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  a = tf.random.uniform(shape=(3,1))\n",
    "  a = a * 3\n",
    "  b = tf.random.uniform(shape=(3,3))\n",
    "  b = b * 3\n",
    "  c = tf.random.uniform(shape=(3,3))\n",
    "  c = c * 3\n",
    "\n",
    "a, b, c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.073744Z",
     "iopub.status.busy": "2022-12-14T03:39:54.073122Z",
     "iopub.status.idle": "2022-12-14T03:39:54.077214Z",
     "shell.execute_reply": "2022-12-14T03:39:54.076668Z"
    },
    "id": "5We2xSnLVygL"
   },
   "outputs": [],
   "source": [
    "# Demonstrate that the generated random numbers match\n",
    "np.testing.assert_allclose(graph_a, a.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(graph_b, b.numpy(), **tol_dict )\n",
    "np.testing.assert_allclose(graph_c, c.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.080256Z",
     "iopub.status.busy": "2022-12-14T03:39:54.079793Z",
     "iopub.status.idle": "2022-12-14T03:39:54.082990Z",
     "shell.execute_reply": "2022-12-14T03:39:54.082356Z"
    },
    "id": "BBFG1xehWneM"
   },
   "outputs": [],
   "source": [
    "# Demonstrate that with the 'num_random_ops' mode,\n",
    "# b & c took on different values even though\n",
    "# their generated shape was the same\n",
    "assert not np.allclose(b.numpy(), c.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "OfX_VexcVqSA"
   },
   "source": [
    "However, notice that in this mode random generation is sensitive to program order, and so the following generated random numbers do not match."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.086042Z",
     "iopub.status.busy": "2022-12-14T03:39:54.085613Z",
     "iopub.status.idle": "2022-12-14T03:39:54.102152Z",
     "shell.execute_reply": "2022-12-14T03:39:54.101559Z"
    },
    "id": "cZt__ElEIDl_"
   },
   "outputs": [],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  a = tf.random.uniform(shape=(3,1))\n",
    "  a = a * 3\n",
    "  b = tf.random.uniform(shape=(3,3))\n",
    "  b = b * 3\n",
    "\n",
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  b_prime = tf.random.uniform(shape=(3,3))\n",
    "  b_prime = b_prime * 3\n",
    "  a_prime = tf.random.uniform(shape=(3,1))\n",
    "  a_prime = a_prime * 3\n",
    "\n",
    "assert not np.allclose(a.numpy(), a_prime.numpy())\n",
    "assert not np.allclose(b.numpy(), b_prime.numpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "nHhOLHyQIkAe"
   },
   "source": [
    "To allow for debugging variations due to tracing order, `DeterministicRandomTestTool` in `num_random_ops` mode allows you to see how many random operations have been traced with the `operation_seed` property."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.105583Z",
     "iopub.status.busy": "2022-12-14T03:39:54.105101Z",
     "iopub.status.idle": "2022-12-14T03:39:54.114616Z",
     "shell.execute_reply": "2022-12-14T03:39:54.114047Z"
    },
    "id": "33RCSICuJEyV"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  print(random_tool.operation_seed)\n",
    "  a = tf.random.uniform(shape=(3,1))\n",
    "  a = a * 3\n",
    "  print(random_tool.operation_seed)\n",
    "  b = tf.random.uniform(shape=(3,3))\n",
    "  b = b * 3\n",
    "  print(random_tool.operation_seed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bkQD3NpOMxIv"
   },
   "source": [
    "If you need to account for varying trace order in your tests, you can even set the auto-incrementing `operation_seed` explicitly. For example, you can use this to make random number generation match across two different program orders."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.117828Z",
     "iopub.status.busy": "2022-12-14T03:39:54.117272Z",
     "iopub.status.idle": "2022-12-14T03:39:54.134599Z",
     "shell.execute_reply": "2022-12-14T03:39:54.134050Z"
    },
    "id": "6W4sS_wOM8CH"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  print(random_tool.operation_seed)\n",
    "  a = tf.random.uniform(shape=(3,1))\n",
    "  a = a * 3\n",
    "  print(random_tool.operation_seed)\n",
    "  b = tf.random.uniform(shape=(3,3))\n",
    "  b = b * 3\n",
    "\n",
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  random_tool.operation_seed = 1\n",
    "  b_prime = tf.random.uniform(shape=(3,3))\n",
    "  b_prime = b_prime * 3\n",
    "  random_tool.operation_seed = 0\n",
    "  a_prime = tf.random.uniform(shape=(3,1))\n",
    "  a_prime = a_prime * 3\n",
    "\n",
    "np.testing.assert_allclose(a.numpy(), a_prime.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(b.numpy(), b_prime.numpy(), **tol_dict)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bP5Kx1OcNbvM"
   },
   "source": [
    "However, `DeterministicRandomTestTool` disallows reusing already-used operation seeds, so make sure the auto-incremented sequences cannot overlap. This is because eager execution generates different numbers for follow-on usages of the same operation seed while TF1 graphs and sessions do not, so raising an error helps keep session and eager stateful random number generation in line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.138009Z",
     "iopub.status.busy": "2022-12-14T03:39:54.137470Z",
     "iopub.status.idle": "2022-12-14T03:39:54.147707Z",
     "shell.execute_reply": "2022-12-14T03:39:54.147126Z"
    },
    "id": "GmBgg5hzNa5H"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This `DeterministicRandomTestTool` object is trying to re-use the already-used operation seed 1. It cannot guarantee random numbers will match between eager and sessions when an operation seed is reused. You most likely set `operation_seed` explicitly but used a value that caused the naturally-incrementing operation seed sequences to overlap with an already-used seed.\n"
     ]
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  random_tool.operation_seed = 1\n",
    "  b_prime = tf.random.uniform(shape=(3,3))\n",
    "  b_prime = b_prime * 3\n",
    "  random_tool.operation_seed = 0\n",
    "  a_prime = tf.random.uniform(shape=(3,1))\n",
    "  a_prime = a_prime * 3\n",
    "  try:\n",
    "    c = tf.random.uniform(shape=(3,1))\n",
    "    raise RuntimeError(\"An exception should have been raised before this, \" +\n",
    "                     \"because the auto-incremented operation seed will \" +\n",
    "                     \"overlap an already-used value\")\n",
    "  except ValueError as err:\n",
    "    print(err)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "U-bLOeCmOn-4"
   },
   "source": [
    "### Verifying Inference\n",
    "\n",
    "You can now use the `DeterministicRandomTestTool` to make sure the `InceptionResnetV2` model matches in inference, even when using the random weight initialization. For a stronger test condition due to matching program order, use the `num_random_ops` mode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:39:54.151016Z",
     "iopub.status.busy": "2022-12-14T03:39:54.150495Z",
     "iopub.status.idle": "2022-12-14T03:40:15.741319Z",
     "shell.execute_reply": "2022-12-14T03:40:15.740627Z"
    },
    "id": "8TWOrflkPa7T"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: 1.2254326\n"
     ]
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  graph = tf.Graph()\n",
    "  with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "    height, width = 299, 299\n",
    "    num_classes = 1000\n",
    "    inputs = tf.ones( (1, height, width, 3))\n",
    "\n",
    "    out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=False)\n",
    "\n",
    "    # Initialize the variables\n",
    "    sess.run(tf.compat.v1.global_variables_initializer())\n",
    "\n",
    "    # Grab the outputs & regularization loss\n",
    "    reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
    "    tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
    "    tf1_output = sess.run(out)\n",
    "\n",
    "  print(\"Regularization loss:\", tf1_regularization_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:40:15.744904Z",
     "iopub.status.busy": "2022-12-14T03:40:15.744601Z",
     "iopub.status.idle": "2022-12-14T03:40:18.420342Z",
     "shell.execute_reply": "2022-12-14T03:40:18.419659Z"
    },
    "id": "Qcx6ur4KPMI1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: tf.Tensor(1.2254325, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "height, width = 299, 299\n",
    "num_classes = 1000\n",
    "\n",
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  model = InceptionResnetV2(num_classes)\n",
    "\n",
    "  inputs = tf.ones((1, height, width, 3))\n",
    "  tf2_output, endpoints = model(inputs, training=False)\n",
    "\n",
    "  # Grab the regularization loss as well\n",
    "  tf2_regularization_loss = tf.math.add_n(model.losses)\n",
    "\n",
    "print(\"Regularization loss:\", tf2_regularization_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:40:18.423973Z",
     "iopub.status.busy": "2022-12-14T03:40:18.423477Z",
     "iopub.status.idle": "2022-12-14T03:40:18.427976Z",
     "shell.execute_reply": "2022-12-14T03:40:18.427410Z"
    },
    "id": "m_SS2b6qPFl1"
   },
   "outputs": [],
   "source": [
    "# Verify that the regularization loss and output both match\n",
    "# when using the DeterministicRandomTestTool:\n",
    "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TKSktIRaP-5b"
   },
   "source": [
    "### Verifying Training\n",
    "\n",
    "Because `DeterministicRandomTestTool` works for *all* stateful random operations (including both weight initialization and computation such as dropout layers), you can use it to verify the models match in training mode as well. You can again use the `num_random_ops` mode because the program order of the stateful random ops matches."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:40:18.431282Z",
     "iopub.status.busy": "2022-12-14T03:40:18.430734Z",
     "iopub.status.idle": "2022-12-14T03:40:42.196309Z",
     "shell.execute_reply": "2022-12-14T03:40:42.195578Z"
    },
    "id": "nMBFVa1kQTJH"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/layers/normalization/batch_normalization.py:581: _colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: 1.22548\n"
     ]
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  graph = tf.Graph()\n",
    "  with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "    height, width = 299, 299\n",
    "    num_classes = 1000\n",
    "    inputs = tf.ones( (1, height, width, 3))\n",
    "\n",
    "    out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=True)\n",
    "\n",
    "    # Initialize the variables\n",
    "    sess.run(tf.compat.v1.global_variables_initializer())\n",
    "\n",
    "    # Grab the outputs & regularization loss\n",
    "    reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
    "    tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
    "    tf1_output = sess.run(out)\n",
    "\n",
    "  print(\"Regularization loss:\", tf1_regularization_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:40:42.199907Z",
     "iopub.status.busy": "2022-12-14T03:40:42.199657Z",
     "iopub.status.idle": "2022-12-14T03:40:45.015643Z",
     "shell.execute_reply": "2022-12-14T03:40:45.014921Z"
    },
    "id": "-jlBkwI5QTJI"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: tf.Tensor(1.2254798, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "height, width = 299, 299\n",
    "num_classes = 1000\n",
    "\n",
    "random_tool = v1.keras.utils.DeterministicRandomTestTool(mode='num_random_ops')\n",
    "with random_tool.scope():\n",
    "  model = InceptionResnetV2(num_classes)\n",
    "\n",
    "  inputs = tf.ones((1, height, width, 3))\n",
    "  tf2_output, endpoints = model(inputs, training=True)\n",
    "\n",
    "  # Grab the regularization loss as well\n",
    "  tf2_regularization_loss = tf.math.add_n(model.losses)\n",
    "\n",
    "print(\"Regularization loss:\", tf2_regularization_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:40:45.019242Z",
     "iopub.status.busy": "2022-12-14T03:40:45.018962Z",
     "iopub.status.idle": "2022-12-14T03:40:45.023395Z",
     "shell.execute_reply": "2022-12-14T03:40:45.022782Z"
    },
    "id": "IL9mjTLnQTJJ"
   },
   "outputs": [],
   "source": [
    "# Verify that the regularization loss and output both match\n",
    "# when using the DeterministicRandomTestTool\n",
    "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "uJTZvmfnQqZH"
   },
   "source": [
    "You have now verified that the `InceptionResnetV2` model running eagerly with decorators around `tf.keras.layers.Layer` numerically matches the slim network running in TF1 graphs and sessions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "xpOAei5vRAPa"
   },
   "source": [
    "Note: When using the `DeterministicRandomTestTool` in `num_random_ops` mode, it is suggested you directly use and call the `tf.keras.layers.Layer` method decorator when testing for numerical equivalence. Embedding it within a Keras functional model or other Keras models can produce differences in stateful random operation tracing order that can be tricky to reason about or match exactly when comparing TF1.x graphs/sessions and eager execution. \n",
    "\n",
    "For example, calling the `InceptionResnetV2` layer directly with `training=True` interleaves variable initialization with the dropout order according to the network creation order.\n",
    "\n",
    "On the other hand, first putting the `tf.keras.layers.Layer` decorator in a Keras functional model and only then calling the model with `training=True` is equivalent to initializing all variables then using the dropout layer. This produces a different tracing order and a different set of random numbers.\n",
    "\n",
    "However, the default `mode='constant'` is not sensitive to these differences in tracing order and will pass without extra work even when embedding the layer in a Keras functional model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:40:45.026795Z",
     "iopub.status.busy": "2022-12-14T03:40:45.026567Z",
     "iopub.status.idle": "2022-12-14T03:41:08.547454Z",
     "shell.execute_reply": "2022-12-14T03:41:08.546677Z"
    },
    "id": "0dSR4ZNvYNYm"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: 1.2239965\n"
     ]
    }
   ],
   "source": [
    "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
    "with random_tool.scope():\n",
    "  graph = tf.Graph()\n",
    "  with graph.as_default(), tf.compat.v1.Session(graph=graph) as sess:\n",
    "    height, width = 299, 299\n",
    "    num_classes = 1000\n",
    "    inputs = tf.ones( (1, height, width, 3))\n",
    "\n",
    "    out, endpoints = inception_resnet_v2(inputs, num_classes, is_training=True)\n",
    "\n",
    "    # Initialize the variables\n",
    "    sess.run(tf.compat.v1.global_variables_initializer())\n",
    "\n",
    "    # Get the outputs & regularization losses\n",
    "    reg_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES)\n",
    "    tf1_regularization_loss = sess.run(tf.math.add_n(reg_losses))\n",
    "    tf1_output = sess.run(out)\n",
    "\n",
    "  print(\"Regularization loss:\", tf1_regularization_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:41:08.551835Z",
     "iopub.status.busy": "2022-12-14T03:41:08.551188Z",
     "iopub.status.idle": "2022-12-14T03:41:16.167387Z",
     "shell.execute_reply": "2022-12-14T03:41:16.166695Z"
    },
    "id": "iMPMMnPtYUY7"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py:1345: UserWarning: `layer.updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.\n",
      "  warnings.warn('`layer.updates` will be removed in a future version. '\n",
      "/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/legacy_tf_layers/base.py:627: UserWarning: `layer.updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.\n",
      "  self.updates, tf.compat.v1.GraphKeys.UPDATE_OPS\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Regularization loss: tf.Tensor(1.2239964, shape=(), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "height, width = 299, 299\n",
    "num_classes = 1000\n",
    "\n",
    "random_tool = v1.keras.utils.DeterministicRandomTestTool()\n",
    "with random_tool.scope():\n",
    "  keras_input = tf.keras.Input(shape=(height, width, 3))\n",
    "  layer = InceptionResnetV2(num_classes)\n",
    "  model = tf.keras.Model(inputs=keras_input, outputs=layer(keras_input))\n",
    "\n",
    "  inputs = tf.ones((1, height, width, 3))\n",
    "  tf2_output, endpoints = model(inputs, training=True)\n",
    "\n",
    "  # Get the regularization loss\n",
    "  tf2_regularization_loss = tf.math.add_n(model.losses)\n",
    "\n",
    "print(\"Regularization loss:\", tf2_regularization_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-12-14T03:41:16.171428Z",
     "iopub.status.busy": "2022-12-14T03:41:16.170699Z",
     "iopub.status.idle": "2022-12-14T03:41:16.175329Z",
     "shell.execute_reply": "2022-12-14T03:41:16.174712Z"
    },
    "id": "jf46KUVyYUY8"
   },
   "outputs": [],
   "source": [
    "# Verify that the regularization loss and output both match\n",
    "# when using the DeterministicRandomTestTool\n",
    "np.testing.assert_allclose(tf1_regularization_loss, tf2_regularization_loss.numpy(), **tol_dict)\n",
    "np.testing.assert_allclose(tf1_output, tf2_output.numpy(), **tol_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "hWXHjtkiZ09V"
   },
   "source": [
    "## Step 3b or 4b (optional): Testing with pre-existing checkpoints\n",
    "\n",
    "After step 3 or step 4 above, it can be useful to run your numerical equivalence tests when starting from pre-existing name-based checkpoints if you have some. This can test both that your legacy checkpoint loading is working correctly and that the model itself is working right. The [Reusing TF1.x checkpoints guide](./reuse_checkpoints.ipynb) covers how to reuse your pre-existing TF1.x checkpoints and transfer them over to TF2 checkpoints.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "v6i3MFmGcxYx"
   },
   "source": [
    "## Additional Testing & Troubleshooting\n",
    "\n",
    "As you add more numerical equivalence tests, you may also choose to add a test that verifies your gradient computation (or even your optimizer updates) match.\n",
    "\n",
    "Backpropagation and gradient computation are more prone to floating point numerical instabilities than model forward passes. This means that as your equivalence tests cover more non-isolated parts of your training, you may begin to see non-trivial numerics differences between running fully eagerly and your TF1 graphs. This may be caused by TensorFlow's graph optimizations that do things such as replace subexpressions in a graph with fewer mathematical operations.\n",
    "\n",
    "To isolate whether this is likely to be the case, you can compare your TF1 code to TF2 computation happening inside of a `tf.function` (which applies graph optimization passes like your TF1 graph) rather than to a purely eager computation. Alternatively, you can try using `tf.config.optimizer.set_experimental_options` to disable optimization passes such as `\"arithmetic_optimization\"` before your TF1 computation to see if the result ends up numerically closer to your TF2 computation results. In your actual training runs it is recommended you use `tf.function` with optimization passes enabled for performance reasons, but you may find it useful to disable them in your numerical equivalence unit tests.\n",
    "\n",
    "Similarly, you may also find that `tf.compat.v1.train` optimizers and TF2 optimizers have slightly different floating point numerics properties than TF2 optimizers, even if the mathematical formulas they are representing are the same. This is less likely to be an issue in your training runs, but it may require a higher numerical tolerance in equivalence unit tests."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "validate_correctness.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}