Best Python code snippet using sure_python
data_8.py
Source:data_8.py  
1#!python32'''Data for day 8.'''3from collections import defaultdict4import operator5INPUT = '''j inc -19 if jhb >= 106es inc -432 if gbu <= -57es inc 278 if ib > -98es inc -835 if ib >= -69q dec 420 if buw == -210ytr dec -258 if xzn < 911okl inc -49 if j > -1012s inc 156 if fzu >= -113eb inc -304 if gbu < 214ae inc 220 if zd >= -515gbu inc -659 if xzn < 516wf dec -590 if cpq <= -317xzn inc 690 if fij <= 518q dec 414 if eb > -31119ae dec 841 if fzu != -320jhb dec 632 if r <= 221fzu inc 383 if s > 15922eb inc -915 if wf != 023ytr inc 374 if cpq <= 124on inc 974 if q < -40425es dec -729 if iuz <= -426xzn inc -502 if zd > -927on dec -345 if xzn > 18628xzn dec -775 if zd == 029jhb inc 980 if es < -55130iuz dec 226 if buw > -131xzn dec -606 if es != -55532fij inc 162 if s > 15033ytr dec -884 if ytr > 62334iy dec -132 if xzn < 157835s dec 435 if yvh > 736buw inc -379 if okl >= -5237zd dec 18 if ae != -62238xzn inc 801 if q < -40739xzn inc -688 if fij >= 17140iy inc 291 if ytr > 152241r dec -438 if iuz != -22642gbu inc 402 if rtu < -443iy inc 541 if laz > -844gbu dec -843 if q >= -41645ytr inc -953 if ib > 346iy dec -487 if zd >= -2147j inc -444 if fzu != 048fij inc 254 if ytr >= 151149ae inc -849 if fzu <= 650laz dec 831 if r != 051s inc -700 if laz == 052buw inc -211 if q == -41453q inc 333 if yvh == 054on inc 223 if q >= -8655iuz inc -632 if q < -7256yvh inc 103 if jhb >= 34657ae inc -493 if iuz >= -86358r inc -245 if yvh > 9459gbu dec -417 if ib <= 860su dec 911 if s == -53661es dec -812 if jhb != 35762jhb dec 265 if q <= -8063s inc 964 if j < 864buw dec 184 if buw == -59065buw inc 940 if buw >= -77466buw inc -739 if eb >= -30467s dec 219 if fij >= 41668su inc 32 if h != -869fzu inc -341 if su < 3170r inc -946 if r <= -23671on inc 469 if yvh == 10372xzn inc -213 if es == 25573r dec -35 if q > -7874xzn dec -879 if zd <= -975jhb dec 616 if fij <= 42276laz inc -925 if s >= 19677jhb dec 121 if buw != -57878iy inc 50 if ae != -195479rtu inc 834 if h < 180ib inc -653 if fzu <= -881fzu dec 736 if iy != 121082es inc -850 if buw != -56583ae dec 457 if cpq == 084ytr dec -283 if su == 3085h inc -398 if fij == 41686j inc 163 if xzn == 303687ae dec 263 if rtu != 82788su dec 259 if iy != 121089r inc 300 if s != 20890rtu inc 181 if ae <= -268491ib dec -545 if laz < -91592zd dec -27 if okl > -5693r inc 243 if r >= -89594fzu inc -238 if iuz > -86695cpq dec 98 if su != 3296yvh dec -422 if wf <= 297cpq inc -873 if laz == -92598cpq dec -315 if rtu > 82599es inc -899 if eb != -304100ytr dec -966 if on < 2016101jhb dec 678 if ae != -2690102h dec 316 if rtu == 834103s inc -950 if jhb >= -1338104fij inc 434 if es > -597105cpq inc -673 if zd <= 10106ytr dec -33 if ae > -2688107h inc 593 if jhb <= -1331108su inc -743 if okl <= -45109gbu inc 315 if es >= -601110h dec 919 if gbu != 916111xzn inc -972 if iuz != -860112q dec -267 if s < -740113q dec 441 if laz <= -922114r inc 511 if on <= 2012115on dec 626 if q != -260116yvh inc -338 if yvh >= 535117rtu dec -950 if rtu == 844118gbu dec -970 if ytr > 2512119fij inc 867 if q > -265120su dec 95 if su >= -719121xzn inc -487 if okl <= -46122okl inc -173 if h <= -114123laz dec -723 if su == -806124on dec 172 if q > -246125zd dec -229 if s < -744126jhb inc 90 if okl >= -213127fzu inc 572 if okl < -225128eb dec 90 if jhb == -1332129ib inc 955 if ae >= -2686130fzu dec -151 if eb <= -390131fzu dec -844 if r <= -131132buw dec -741 if s >= -751133h dec -324 if rtu != 828134s dec 86 if rtu < 842135okl dec -423 if su == -806136ytr dec -89 if okl < 202137rtu dec 487 if s > -829138ib dec 176 if zd > 234139r dec -827 if ae >= -2679140okl inc 985 if rtu == 834141buw inc -118 if gbu >= 1881142laz inc -443 if iy != 1202143iy dec -722 if iuz > -868144xzn inc -408 if ib >= 1323145on inc 509 if on == 1385146okl dec 705 if buw <= 54147h dec 918 if eb != -398148ae dec -701 if okl == 481149es inc 715 if rtu <= 827150laz dec 142 if zd < 239151ytr inc 689 if zd <= 247152yvh inc -644 if zd < 244153iy inc -469 if eb < -385154rtu inc -91 if xzn == 1169155eb dec 9 if su <= -816156s inc -418 if es == -595157r inc 320 if jhb >= -1340158q inc 87 if iy != 1465159yvh dec 184 if ae == -1976160es inc -231 if okl == 478161cpq inc -658 if s > -1257162fij dec 344 if fzu < 758163cpq dec -911 if yvh <= -118164j dec 699 if h > -723165laz dec -356 if okl > 474166on dec 989 if yvh >= -122167ae dec 634 if zd <= 235168s dec -361 if ib <= 1322169ytr dec 735 if zd >= 235170h dec 897 if fzu < 750171fzu dec 838 if j > -541172q inc -588 if h > -724173h dec 37 if laz < -423174ytr inc 247 if es != -601175fij inc 51 if jhb >= -1335176h dec 20 if wf != 10177r inc -563 if rtu <= 744178ae dec 406 if zd > 234179fzu inc -341 if ytr == 2805180iy dec -595 if ytr <= 2802181iy inc -948 if jhb < -1331182su inc -377 if h <= -769183fzu inc 977 if fij == 1424184es dec 555 if s != -1258185ae inc -925 if zd != 238186laz inc -348 if ib == 1324187eb dec 748 if ae == -2388188r dec -344 if wf < 2189fzu dec -498 if ae >= -2383190jhb inc 4 if fij != 1424191ae inc -828 if iy <= 522192ae dec -633 if ib > 1322193h dec -81 if fzu <= 557194ytr inc 542 if es < -1141195j dec -539 if fzu != 557196jhb dec -356 if fzu < 560197laz inc -895 if laz >= -781198q dec 732 if eb == -1142199yvh inc 221 if ib != 1326200h inc -216 if okl == 481201jhb dec 355 if iy > 520202ib inc 651 if r >= -44203r inc -947 if j > -1204ytr dec 292 if es < -1141205gbu dec 161 if h < -904206xzn dec 33 if fzu > 549207zd dec 781 if buw != 56208yvh dec 836 if j < 9209zd dec 385 if q != -1488210ib inc 402 if xzn == 1136211cpq dec 876 if iy <= 509212cpq dec -94 if cpq <= -972213s inc -609 if r == -983214ytr inc 59 if h > -909215okl inc -854 if wf <= 4216ib inc -284 if ae > -2584217ytr dec -359 if ytr > 3112218su dec 236 if rtu <= 748219xzn inc 103 if q == -1491220wf inc -701 if q == -1488221jhb inc 954 if on == 905222iy dec 135 if es >= -1141223q dec -904 if buw >= 49224xzn dec 459 if su > -1429225jhb inc -282 if q == -584226gbu dec 239 if cpq < -881227zd inc 987 if rtu <= 750228ae inc -31 if yvh < -725229fij inc 841 if q < -574230j inc -720 if okl <= -373231rtu dec 906 if iuz >= -865232h inc 137 if su <= -1425233okl dec 690 if fzu <= 545234q inc -508 if rtu >= -163235fij dec -751 if rtu > -166236fij inc 763 if laz >= -1677237fzu dec 42 if q < -1088238okl dec -156 if fij != 3786239iy inc -977 if ib >= 2087240laz dec -31 if su <= -1412241buw inc -358 if r == -983242eb dec 743 if fzu > 508243fzu dec 3 if eb >= -1876244yvh dec -190 if wf <= -696245okl inc -168 if h <= -901246su dec -704 if wf < -706247eb dec 587 if fzu >= 512248su inc -778 if zd == 444249ae inc -134 if laz < -1637250on inc -92 if fzu < 519251ib inc 217 if s > -1864252cpq inc 46 if q > -1098253cpq dec 321 if xzn <= 673254fij dec -633 if zd != 443255zd dec 869 if on == 807256buw inc -506 if laz > -1647257q inc 640 if s <= -1862258zd dec 943 if okl < -378259rtu inc -498 if buw >= -814260cpq dec 651 if fzu > 507261fij inc -636 if j < -714262zd inc -167 if ae != -2754263eb dec 369 if fzu >= 505264su dec -549 if q != -454265cpq inc -755 if okl > -391266ib inc 810 if su <= -1652267wf dec 159 if laz > -1651268buw dec -978 if okl != -377269cpq dec -288 if j != -715270buw inc -470 if okl == -380271cpq inc -997 if iuz > -867272cpq inc -855 if on < 815273es dec -372 if buw != 173274r inc -503 if ae > -2753275buw dec 963 if ae == -2755276r inc 70 if cpq > -3816277eb inc -39 if fij != 3786278q inc 696 if jhb < -295279laz dec -643 if j <= -718280gbu dec 767 if cpq > -3815281buw dec 571 if es > -785282on dec -220 if rtu <= -657283fij dec -286 if zd > -675284iuz dec -790 if ae >= -2751285buw inc -457 if rtu <= -652286h inc -345 if laz != -1653287es dec 996 if su <= -1644288es dec 594 if iy >= -467289s dec 981 if wf == -860290zd inc -828 if cpq >= -3808291laz inc -245 if r <= -1416292su inc -61 if j >= -726293gbu dec -409 if on >= 1029294fzu dec 182 if eb != -2875295fij dec 440 if iy == -462296jhb dec -21 if su <= -1704297iuz dec -627 if r != -1425298ib dec 787 if jhb < -292299r dec 624 if yvh > -546300buw inc 625 if jhb <= -289301jhb inc -874 if on >= 1025302ib dec 224 if iy != -455303r inc 482 if gbu <= 1126304fij inc -848 if okl != -393305es dec -513 if gbu != 1127306fzu dec -189 if iuz < 569307ae inc 317 if cpq <= -3804308h inc -214 if eb < -2873309zd inc 124 if wf > -865310r dec 892 if gbu <= 1137311ytr inc -92 if fij > 2770312iy inc 302 if rtu == -661313iy dec 503 if yvh < -549314yvh inc 276 if fij < 2775315q inc -612 if jhb <= -1151316q inc 949 if iy != -159317jhb dec 771 if h == -1458318yvh inc 389 if on == 1033319s inc 167 if jhb >= -1160320gbu inc 194 if jhb >= -1165321fzu inc 678 if laz >= -1893322iuz inc -226 if zd != -1360323okl dec -490 if ib >= 2077324su dec -478 if fij < 2784325s inc 540 if s >= -2677326fij dec 555 if buw <= -858327cpq dec -261 if fij != 2220328rtu inc -378 if okl >= 103329zd dec 991 if fzu == 1198330fzu dec 464 if xzn < 678331xzn inc -28 if ae > -2432332wf dec 84 if rtu >= -1041333okl dec 670 if s <= -2128334h inc -548 if yvh < 123335wf dec 579 if cpq < -3544336su inc -160 if ae >= -2439337wf dec -845 if zd < -2361338es dec -19 if q != 573339j dec 763 if fij >= 2214340wf dec -486 if su < -1384341ae dec -887 if cpq >= -3541342xzn dec 879 if es != -1836343iuz inc 840 if iuz == 333344on dec -251 if j != -1471345ib inc -65 if fij <= 2224346fzu inc -673 if r >= -2932347r inc -319 if su < -1390348es dec 730 if s >= -2145349yvh inc 593 if q != 571350ytr inc 367 if ib >= 2031351wf inc -546 if zd >= -2369352yvh inc -814 if fzu != 61353xzn inc -798 if on == 1284354j dec 832 if okl != -565355q dec 210 if zd >= -2366356j inc -392 if r >= -3257357jhb dec -490 if ytr < 3381358buw inc 173 if rtu < -1035359okl inc 81 if j > -1877360ib inc -207 if wf == -1583361ib dec -494 if cpq == -3547362laz dec -331 if rtu >= -1042363cpq inc -697 if jhb <= -1157364ib dec 601 if eb != -2889365ib dec 613 if yvh == 714366xzn dec -948 if jhb > -1160367su inc 651 if q > 376368okl dec 613 if rtu > -1045369eb dec -348 if gbu == 1322370iy inc -508 if cpq == -4235371q inc -768 if su <= -1383372s dec -612 if on >= 1283373iuz inc 942 if gbu != 1322374okl dec -707 if ytr != 3379375on inc -658 if buw <= -689376on dec -246 if es == -2566377j inc -335 if eb == -2532378wf inc 341 if cpq != -4248379fzu inc 297 if rtu > -1042380cpq inc 624 if ib >= 1091381q dec -961 if wf < -1233382h inc -688 if okl == -390383yvh inc 378 if iuz <= 1182384q inc -177 if ib <= 1095385ib dec -52 if s > -1520386su dec -822 if on <= 878387wf inc -962 if cpq <= -3629388fzu inc -481 if okl > -386389iy inc -146 if rtu > -1048390ib dec -523 if s < -1522391fzu dec 121 if fzu == 358392j dec -112 if ae != -2434393laz dec 967 if laz >= -1566394gbu dec -825 if on > 876395wf dec 658 if su <= -570396q dec -247 if jhb <= -1155397r dec -781 if eb <= -2524398zd inc 363 if fzu != 244399q dec 139 if jhb < -1147400es dec 185 if iuz >= 1171401iuz inc 267 if su > -573402jhb dec 745 if es > -2745403ib inc 646 if s == -1524404okl inc 139 if h < -2700405j dec -199 if wf < -1233406gbu inc -848 if r != -2466407es dec -570 if xzn == 799408rtu dec -990 if ytr != 3379409ae dec -48 if yvh != 1089410iy inc 143 if buw > -695411eb dec -645 if q > 485412xzn dec -295 if yvh != 1090413es dec -302 if on < 879414ib dec 222 if wf < -1233415eb inc 587 if wf > -1244416zd dec 896 if su < -567417ib dec -405 if q < 502418buw inc -282 if r < -2460419fij inc 810 if gbu > 468420h inc 322 if ae > -2388421su inc -843 if j == -1896422jhb dec 896 if yvh >= 1089423h dec 961 if cpq >= -3624424laz dec -799 if ae > -2375425zd inc -490 if h <= -3332426fij inc 639 if q > 489427ib inc 160 if rtu < -47428yvh dec 82 if eb != -1302429cpq dec -219 if eb <= -1291430es dec 713 if okl > -260431s inc -314 if wf != -1242432s inc -366 if ytr != 3386433zd inc -332 if zd != -3393434iy dec -669 if ae > -2390435j dec 151 if su == -1412436wf inc 354 if jhb >= -2056437xzn dec 580 if iy == 510438laz inc 692 if iuz >= 1441439fzu inc -913 if on <= 875440s inc -855 if buw == -975441zd inc 68 if gbu > 466442iuz inc -729 if iuz == 1440443gbu inc 261 if j != -2056444q inc -977 if su != -1421445laz dec 451 if r != -2473446okl inc 93 if rtu == -49447eb inc 98 if j != -2052448ae inc -176 if buw == -973449j dec 492 if rtu <= -50450j dec -806 if buw < -964451laz dec 407 if eb != -1195452iy inc -77 if s == -1890453q dec 900 if yvh >= 1003454r dec -530 if es <= -2591455iy dec -612 if buw <= -972456zd dec 615 if buw < -967457ytr inc 541 if xzn >= 1086458gbu inc 62 if gbu <= 739459gbu inc 283 if r != -1934460ytr dec -282 if cpq < -3391461yvh inc 48 if gbu < 1088462jhb dec 412 if yvh >= 1052463es dec 417 if yvh >= 1051464fzu inc -660 if cpq < -3397465h inc -545 if h <= -3332466fij inc -179 if iuz > 707467zd inc -217 if okl == -158468laz inc -342 if yvh != 1057469on inc -856 if buw <= -969470xzn dec 188 if su < -1404471cpq dec 916 if xzn > 904472eb inc 642 if wf != -884473okl dec 64 if iy >= 1044474xzn inc -694 if gbu != 1075475ae dec -478 if yvh <= 1061476iuz dec 374 if fzu <= -1335477zd dec 910 if r == -1940478gbu dec 543 if su < -1409479okl inc 523 if laz != -3732480ytr inc -898 if jhb != -2465481su dec -593 if ytr != 4204482buw dec -56 if gbu <= 542483zd inc -348 if wf <= -895484iuz inc 264 if buw != -913485okl inc 702 if wf <= -879486q dec 283 if wf > -892487laz dec -901 if wf != -892488on inc 256 if rtu <= -48489iy inc 57 if su == -1408490fzu inc -732 if su != -1408491ytr dec 559 if eb >= -563492iuz dec 967 if es > -3016493j inc 849 if wf > -883494xzn inc -764 if q != -1671495ib dec -555 if r == -1946496r dec -205 if yvh == 1058497laz inc 826 if ae >= -2085498gbu inc -43 if buw >= -926499j inc -341 if wf > -889500s dec -343 if gbu >= 504501iuz dec 306 if fij >= 3486502fij dec -905 if iuz >= -667503ae dec 174 if fzu >= -2073504on dec -861 if buw > -920505jhb inc -513 if iuz >= -672506okl dec -389 if j == -1582507ib inc 419 if iy == 1041508xzn inc -271 if eb < -568509buw inc 230 if on <= 1139510rtu inc 745 if su <= -1406511gbu dec -430 if iy != 1046512s inc 357 if su >= -1417513r dec -370 if rtu > 694514buw dec 989 if r < -1373515okl dec -817 if iuz < -665516h dec 435 if laz != -2005517jhb inc -142 if iy < 1036518rtu inc -375 if fzu <= -2059519zd inc -974 if j < -1572520es dec -330 if yvh == 1058521on dec 661 if fzu < -2068522j inc 59 if on == 1133523cpq inc -503 if q >= -1673524iuz dec -123 if j < -1521525yvh inc -749 if su > -1416526cpq dec 847 if zd != -6359527laz inc 940 if iuz > -553528su dec -999 if on > 1128529cpq dec 43 if h > -4325530j dec -869 if ib != 3032531zd dec 871 if xzn != -552532j dec 986 if laz != -1067533buw inc -723 if cpq < -5700534eb dec 153 if iy >= 1035535fij inc -182 if j < -1636536on dec 148 if su != -419537buw dec 923 if xzn < -549538cpq inc 35 if h >= -4330539fij dec 755 if es < -2671540q dec 745 if buw >= -2340541wf inc -49 if ae == -2255542zd dec 631 if j == -1640543jhb dec 902 if eb != -723544iy inc 408 if ib <= 3033545iuz inc 948 if on <= 989546zd inc -991 if laz < -1055547xzn dec -294 if ib >= 3032548wf dec 797 if iy == 1449549iy dec 467 if fzu >= -2073550fij dec -491 if fzu >= -2075551es dec 946 if eb != -714552ib dec -726 if gbu < 934553fij inc -705 if eb < -704554ytr dec -143 if jhb < -3870555buw dec -381 if iuz <= 408556on inc 275 if iy == 991557ib dec 37 if laz != -1057558eb inc -456 if xzn < -543559okl dec 91 if cpq != -5666560fzu dec 520 if fij < 2339561j dec -910 if ib <= 3747562zd inc 661 if fij <= 2341563r inc -620 if es > -3629564rtu dec 907 if buw > -1951565es inc -151 if jhb == -3880566s dec 777 if q != -2410567r inc -582 if jhb < -3871568rtu dec 487 if r >= -2576569es dec 532 if xzn > -546570su dec -289 if rtu == -166571rtu inc -33 if iuz <= 400572rtu inc 566 if rtu < -189573yvh inc 752 if on != 988574buw dec 856 if s >= -1536575q dec 64 if rtu >= 358576cpq inc 324 if j <= -1640577jhb inc -359 if r > -2570578q dec -325 if es < -3770579ib inc 893 if eb != -1165580laz inc 811 if jhb != -4248581yvh inc -744 if fij <= 2345582ae inc -830 if s >= -1536583cpq inc 773 if ytr <= 3790584cpq inc 936 if gbu >= 931585jhb dec -944 if eb >= -1172586j dec 987 if gbu > 920587s dec 560 if r <= -2562588jhb inc -435 if eb > -1171589s dec 922 if es < -3772590ae inc -5 if yvh < 327591buw inc 83 if r > -2563592ytr dec 167 if rtu > 365593fzu dec 144 if okl > 2173594fij dec -248 if ytr > 3613595h dec 406 if zd < -7324596j dec -807 if fzu != -2741597xzn inc -58 if gbu != 924598jhb dec -878 if es > -3773599cpq dec 525 if yvh != 327600okl dec -555 if s != -3011601wf dec 395 if su <= -121602r dec 231 if ae < -3089603iy inc 57 if ae > -3088604buw inc -862 if cpq < -5098605cpq dec -191 if su != -120606r dec 18 if zd == -7325607cpq dec -156 if fij >= 2586608su dec 743 if on != 987609xzn inc 493 if cpq >= -4753610r dec 847 if ib != 4642611h inc 675 if ytr == 3625612iy inc 934 if xzn < -545613fzu dec -982 if ae == -3090614iuz dec 500 if iy != 1911615eb dec 676 if xzn > -560616s inc -440 if ib >= 4650617s dec 662 if ae != -3092618buw inc -625 if rtu != 357619es dec -936 if iuz != -100620ib inc -849 if cpq < -4750621s dec 544 if cpq != -4758622zd inc -432 if es < -2845623gbu inc -787 if okl < 2741624gbu dec -336 if buw < -4287625on dec -790 if gbu <= 474626buw dec -627 if q <= -2145627h inc -142 if q != -2156628cpq dec 264 if iuz > -98629ib inc 19 if q >= -2157630zd dec -579 if q != -2140631es dec 567 if j <= -1820632rtu dec -213 if iy != 1926633zd dec 546 if iy < 1922634buw inc -847 if wf >= -2134635laz dec -136 if xzn >= -560636xzn inc 516 if zd != -7292637wf inc -560 if ib >= 3807638gbu dec 828 if wf == -2689639on dec -126 if gbu >= -361640yvh inc -153 if r != -3660641iuz inc 277 if r != -3663642yvh inc -941 if iuz > -108643j inc -176 if iuz >= -104644jhb dec -124 if rtu < 581645r inc -456 if rtu != 583646buw inc 346 if cpq > -4765647yvh inc 841 if yvh == -777648buw inc 806 if eb > -1853649eb inc -821 if zd < -7285650s dec -257 if j <= -2000651buw inc 734 if r != -4111652q dec 299 if fzu >= -1756653ytr dec 363 if su < -870654gbu inc 812 if jhb != -3616655q inc -373 if fij <= 2587656xzn dec 954 if r == -4119657jhb dec 275 if okl < 2738658fij inc -894 if fzu != -1752659wf inc -122 if okl > 2738660laz dec -942 if q > -2820661gbu dec -297 if jhb >= -3885662r dec -994 if zd <= -7285663ytr inc 544 if es >= -3416664jhb dec 326 if wf <= -2688665h dec 949 if yvh == 64666wf dec -884 if zd == -7292667rtu inc -456 if ib >= 3805668es inc -375 if h == -5817669cpq dec 675 if q > -2828670fzu dec 706 if buw != -2639671h dec -230 if h >= -5821672ae inc 576 if su != -866673j inc 903 if fij >= 1686674buw dec -693 if h <= -5583675fij inc -416 if jhb > -4210676q inc -648 if okl < 2739677fij inc 736 if su != -876678ytr dec 869 if cpq < -5435679ytr inc 126 if eb > -2664680buw dec 702 if eb > -2664681es dec 392 if xzn == -1506682rtu dec 837 if r != -3132683ytr dec 729 if s > -4228684buw dec -760 if fij >= 2008685cpq dec -358 if q != -3477686s dec -292 if jhb > -4214687laz inc -77 if rtu > -720688es inc 427 if zd == -7292689r inc 179 if jhb >= -4209690gbu inc -163 if q != -3460691buw inc 381 if laz >= -187692okl inc -167 if su > -865693fzu dec 289 if laz >= -183694ytr inc 544 if gbu < 601695fzu dec 706 if q < -3464696rtu inc -605 if eb < -2658697buw dec 199 if iuz < -96698su dec 599 if h < -5585699on inc 895 if rtu == -1310700on dec -661 if yvh != 58701su dec 995 if q <= -3469702buw dec -471 if q <= -3466703wf dec -487 if ae > -2524704buw inc 542 if es > -3381705fij dec 599 if okl >= 2734706ae inc 295 if xzn <= -1511707s inc -293 if ytr < 3985708s inc 571 if ae < -2506709ytr inc -199 if wf < -1313710zd inc -155 if on <= 2568711su dec -221 if buw <= 23712s inc 961 if ytr <= 3790713zd inc -200 if es > -3375714ae dec -266 if fzu <= -3160715fzu inc -197 if h < -5583716q dec 765 if es <= -3366717j inc -967 if yvh > 57718iuz inc -111 if laz <= -181719es dec 403 if j >= -2065720wf dec -542 if eb < -2665721zd inc -518 if eb <= -2664722r dec 593 if s != -2700723jhb dec -422 if gbu < 594724ae inc -399 if xzn >= -1503725ib dec -244 if h != -5583726fzu dec 412 if es != -3773727rtu dec -620 if r < -3542728ytr dec -342 if yvh < 65729iy inc 303 if rtu <= -1314730rtu inc 602 if on != 2562731buw dec 546 if fzu <= -3766732fij dec -376 if zd == -8165733eb inc 332 if xzn <= -1510734ae inc 231 if iuz != -212735j dec 703 if rtu <= -1316736su dec -636 if on <= 2561737iy dec 260 if r != -3544738ae dec 744 if ae <= -2248739jhb inc -128 if ae <= -2985740s dec -692 if yvh <= 72741buw dec -419 if wf < -777742j dec 627 if rtu != -1312743wf inc -736 if gbu == 591744buw inc 532 if jhb <= -3914745j inc 290 if yvh != 55746zd inc -920 if wf > -1518747es inc 48 if es >= -3781748xzn dec -88 if s >= -2004749okl inc 263 if iy == 1959750es dec -584 if iuz <= -205751rtu dec 211 if okl <= 2996752buw dec -580 if ae >= -2999753fij inc 992 if iuz >= -220754q dec 685 if zd == -9085755wf dec 406 if r <= -3532756cpq dec 336 if fzu <= -3769757ytr dec 813 if cpq > -5414758iuz dec 456 if zd >= -9086759fij dec -372 if on == 2562760zd inc -182 if wf < -1916761jhb inc 992 if xzn < -1408762es inc 899 if ib != 4049763iy inc 88 if iy > 1951764su dec 363 if rtu > -1323765es inc -311 if wf < -1911766cpq inc 328 if iuz <= -659767es dec 349 if r < -3534768eb dec 488 if ib < 4065769r inc 639 if r >= -3542770wf dec 105 if wf < -1913771laz inc 95 if buw >= 52772wf inc -260 if buw > 53773wf dec 975 if gbu <= 598774iuz inc 788 if on != 2555775jhb dec -717 if wf <= -2992776rtu dec -551 if rtu <= -1315777fij inc -392 if wf == -2990778eb inc -267 if okl == 3000779su dec 884 if iuz > 118780fzu inc 704 if iuz < 123781xzn dec 810 if laz <= -90782cpq dec 961 if ytr > 3310783on inc -936 if on != 2562784on dec -968 if s >= -1994785eb inc -240 if q <= -4912786ib dec -760 if okl < 2991787cpq dec 692 if iy < 2051788es inc 477 if j != -3100789fzu dec 832 if fzu <= -3063790okl inc 30 if gbu <= 596791su dec 607 if eb == -3661792xzn inc 829 if ib < 4060793iy dec -895 if wf != -2990794wf dec 481 if cpq > -5768795okl dec -432 if jhb < -2199796yvh inc -765 if su < -4086797iy inc -903 if r < -2892798h dec -145 if iuz <= 121799zd dec 392 if on <= 2569800okl inc -36 if rtu != -773801xzn dec -551 if r >= -2909802j inc 461 if s <= -1993803iuz inc 889 if zd < -9651804yvh dec 269 if iuz <= 1012805fzu dec -330 if ib <= 4067806fij dec -931 if su != -4096807yvh inc 277 if h > -5446808iy inc -316 if es == -2904809q dec 57 if cpq != -5773810jhb dec 784 if yvh == -686811yvh dec -785 if ib != 4058812s inc 13 if ib != 4066813rtu dec 372 if iy < 1731814h inc -901 if buw > 48815cpq inc -295 if ytr == 3310816gbu dec 460 if j >= -2638817ib dec 942 if rtu >= -1143818r inc -231 if ib > 3121819h inc 275 if ib == 3108820iuz dec -599 if xzn == -848821fzu dec -293 if rtu != -1139822ae dec -911 if yvh < -687823on dec -969 if ae >= -2084824jhb dec 432 if iy != 1723825cpq dec 369 if iuz >= 1602826cpq inc 161 if q == -4919827su dec -818 if buw > 49828r inc 176 if rtu <= -1138829su inc 19 if xzn < -841830gbu inc -987 if laz >= -100831ytr inc 349 if j >= -2643832wf dec 371 if q < -4909833rtu dec 203 if xzn >= -852834wf dec 410 if rtu < -1339835ib dec 448 if gbu < -392836yvh dec 130 if iy == 1723837ytr inc -363 if zd >= -9663838cpq dec -69 if okl >= 3422839iy dec 250 if ae <= -2080840cpq dec -273 if iy > 1465841gbu dec 37 if ib > 2673842q dec -552 if laz == -92843q inc 600 if ib <= 2669844ib dec 570 if gbu != -392845wf inc -299 if fij > 4080846buw inc -756 if s <= -1984847gbu inc -707 if gbu == -401848gbu inc -937 if gbu != -398849fzu dec 265 if r != -2724850r dec -786 if s >= -1993851wf dec 607 if laz <= -92852eb dec 424 if wf < -4675853xzn dec 17 if fij >= 4083854on dec -829 if gbu >= -1333855h inc -669 if xzn <= -860856j dec 393 if ib < 2106857es dec -958 if fzu > -3568858es inc 943 if rtu == -1342859xzn dec -274 if rtu > -1349860buw inc 88 if laz == -92861yvh dec 865 if laz > -87862gbu inc -415 if laz > -96863jhb inc -555 if yvh > -830864on inc -442 if ae == -2081865iy dec 504 if iuz > 1599866q inc 132 if yvh >= -818867yvh inc 642 if yvh == -823868yvh inc 54 if yvh <= -176869yvh inc 799 if wf == -4685870gbu inc -861 if on == 3918871jhb dec 435 if r < -1929872fzu dec -180 if iy <= 976873j dec -462 if xzn > -596874q inc 268 if cpq > -5944875ae inc 154 if gbu < -2604876laz dec 484 if q <= -3499877iy dec -127 if iuz <= 1609878ae inc 119 if h > -7018879s dec 723 if s < -1981880buw inc 124 if fzu > -3390881r dec -947 if su < -3265882ytr dec -851 if ib <= 2089883r dec 267 if j != -2562884r dec 289 if ae == -1808885es dec 489 if gbu != -2601886okl dec -2 if q == -3499887jhb inc -486 if buw != -485888fij inc -38 if h <= -7022889zd inc -910 if r >= -2500890yvh dec -457 if wf >= -4685891es inc -458 if yvh >= 1129892eb dec -851 if buw >= -500893xzn inc -983 if on >= 3914894iuz dec 137 if rtu > -1343895su inc 723 if okl <= 3434896s dec -254 if zd > -10577897iy dec -304 if buw < -482898su dec 405 if r != -2492899buw inc 419 if es != -2906900rtu dec 360 if r <= -2493901gbu inc 178 if xzn == -1574902iuz inc 349 if q < -3489903fij inc -484 if zd == -10574904buw inc -211 if ytr != 3299905ae dec -387 if es != -2899906h dec 384 if wf > -4683907ib dec 100 if xzn != -1573908s dec 540 if r > -2502909ib dec 217 if ytr != 3301910okl inc -560 if on > 3912911buw inc 220 if wf > -4687912buw inc -904 if iuz >= 1819913xzn inc -474 if es == -2905914fij dec 671 if laz < -569915r inc 842 if okl >= 2878916fzu inc 889 if rtu == -1702917buw dec -334 if buw > -975918buw dec -758 if su < -2942919rtu inc 719 if ae == -1421920iuz inc 295 if fzu <= -2496921es inc 713 if xzn <= -1572922es dec 987 if eb > -3237923j inc 704 if cpq == -5934924ytr inc -74 if fzu > -2503925s dec -958 if xzn != -1569926fzu inc -53 if ytr == 3222927fzu dec 937 if okl == 2877928ae inc 980 if yvh > 1119929r inc 293 if q > -3504930ae dec 199 if fzu == -2555931xzn inc 87 if r >= -2206932eb dec 557 if r >= -2200933on inc -612 if wf > -4690934j dec 234 if ib < 1783935eb inc 261 if okl < 2869936gbu inc 796 if cpq > -5936937xzn dec -149 if gbu < -1631938es inc 693 if rtu > -989939j inc -61 if q == -3503940r dec -227 if okl <= 2874941cpq inc -245 if fzu == -2553942s dec -299 if ae > -434943j dec 593 if s > -2041944xzn dec -398 if on < 3304945on inc 663 if h <= -7012946jhb dec 592 if iuz > 2119947iy inc -666 if ytr > 3219948gbu inc -563 if iy <= 740949xzn dec -77 if es < -2484950su inc 590 if q <= -3499951jhb dec -17 if on <= 3965952okl dec -106 if su < -2353953buw dec 554 if h > -7017954eb dec -279 if ytr != 3212955buw inc 950 if eb != -2699956j dec 297 if j != -2698957r inc -507 if q != -3494958q inc -838 if xzn == -1261959iuz dec -632 if cpq <= -6171960laz inc -863 if okl >= 2865961zd dec 764 if jhb != -3682962iuz inc -193 if wf < -4684963xzn dec -672 if iuz > 2556964fij inc 800 if okl == 2868965q dec 699 if eb == -2694966q inc -126 if fij >= 4208967j dec 413 if j <= -2982968r dec 528 if on <= 3969969iy dec -849 if fzu < -2553970wf dec 21 if xzn < -1267971on inc 273 if ae <= -447972eb inc 59 if iy == 732973q dec 392 if s != -2042974wf inc -589 if laz < -1431975su inc 39 if q <= -5546976q inc 755 if rtu >= -988977on dec -501 if es > -2498978gbu inc 439 if yvh != 1131979es dec 730 if h >= -7013980xzn dec 641 if gbu > -1762981ib inc -610 if j != -3403982xzn inc 557 if ae != -441983fij dec 453 if su != -2307984su inc 748 if gbu <= -1753985rtu dec 140 if okl != 2858986h inc 619 if j != -3403987s dec 466 if yvh == 1129988iy dec -639 if okl <= 2873989on dec 725 if q < -4790990gbu inc -256 if cpq == -6184991laz dec 58 if jhb != -3680992gbu dec 352 if eb < -2687993s dec -151 if gbu >= -2112994fzu dec -791 if zd != -11327995fij dec 368 if yvh > 1126996okl inc -951 if es >= -3225997jhb dec 419 if q < -4808998rtu dec 100 if s < -2350999es inc 894 if rtu >= -12271000q dec 613 if s >= -23551001r inc -691 if q < -54081002xzn dec -546 if iy <= 13701003ae inc 996 if su >= -15641004buw inc 559 if okl <= 1919'''1005OPERATORS = {1006    '>': operator.gt,1007    '<': operator.lt,1008    '>=': operator.ge,1009    '<=': operator.le,1010    '!=': operator.ne,1011    '==': operator.eq1012}1013OPERATORS_INV = {v: k for k, v in OPERATORS.items()}1014CHANGE = {1015    'dec': operator.sub,1016    'inc': operator.add1017}1018CHANGE_INV = {1019    operator.sub: '-',1020    operator.add: '+'1021}1022class Instruction(object):1023    '''An instruction.'''1024    def __init__(self, register, change, amount, condition):1025        self.register = register1026        self.change = change1027        self.amount = amount1028        self.condition = condition1029    def __str__(self):1030        change = CHANGE_INV[self.change]1031        return f'Instruction({self.register} {change} {self.amount}, {self.condition})'1032    def __repr__(self):1033        return str(self)1034    def update(self, amount):1035        '''Update value of amount with change.'''1036        return self.change(amount, self.amount)1037    @staticmethod1038    def create(row):1039        '''Create an instruction from a row.'''1040        change = CHANGE[row[1]]1041        amount = int(row[2])1042        condition = Condition.create(row)1043        return Instruction(row[0], change, amount, condition)1044class Condition(object):1045    '''A condition.'''1046    def __init__(self, register, op, amount):1047        self.register = register1048        self.op = op1049        self.amount = amount1050    def __str__(self):1051        op = OPERATORS_INV[self.op]1052        return f'Condition({self.register} {op} {self.amount})'1053    def check(self, amount):1054        '''Check amount against condition.'''1055        return self.op(amount, self.amount)1056    @staticmethod1057    def create(row):1058        '''Create a condition from a row.'''1059        op = OPERATORS[row[5]]1060        amount = int(row[6])1061        return Condition(row[4], op, amount)1062class Registers(object):1063    '''Container for registers.'''1064    def __init__(self):1065        self.registers = defaultdict(int)1066    def __str__(self):1067        return str(self.registers)1068    def __iter__(self):1069        return iter(self.registers.items())1070    def check(self, condition):1071        '''Check a condition.'''1072        register_amount = self.registers[condition.register]1073        return condition.check(register_amount)1074    def update(self, instruction):1075        '''Update registers with instruction.'''1076        if self.check(instruction.condition):1077            current_amount = self.registers[instruction.register]1078            new_amount = instruction.update(current_amount)1079            self.registers[instruction.register] = new_amount1080def parse_input(input):1081    '''Parse input data.'''1082    rows = (row.strip().split(' ') for row in input.split('\n'))1083    instructions = [Instruction.create(row) for row in rows]1084    return instructions...transformer代码.py
Source:transformer代码.py  
1"""2code by Tae Hwan Jung(Jeff Jung) @graykode, Derek Miller @dmmiller612, modify by shwei3Reference: https://github.com/jadore801120/attention-is-all-you-need-pytorch4           https://github.com/JayParks/transformer5"""6"""7代ç ä½ç½®ï¼8ææææä½ ç¨Pytorch代ç å®ç°Transformer模åï¼è¶
详ç»ç代ç è§£è¯»ï¼9https://ugirc.blog.csdn.net/article/details/12039404210"""11# ====================================================================================================12# æ°æ®æå»º13import math14import torch15import numpy as np16import torch.nn as nn17import torch.optim as optim18import torch.utils.data as Data19device = 'cpu'20# device = 'cuda'21# transformer epochs22epochs = 10023# epochs = 100024# è¿éææ²¡æç¨ä»ä¹å¤§åçæ°æ®éï¼èæ¯æå¨è¾å
¥äºä¸¤å¯¹å¾·è¯âè±è¯çå¥å25# è¿ææ¯ä¸ªåçç´¢å¼ä¹æ¯ææå¨ç¡¬ç¼ç ä¸å»çï¼ä¸»è¦æ¯ä¸ºäºéä½ä»£ç é
读é¾åº¦26# S: Symbol that shows starting of decoding input27# E: Symbol that shows starting of decoding output28# P: Symbol that will fill in blank sequence if current batch data size is short than time steps29sentences = [30    # å¾·è¯åè±è¯çåè¯ä¸ªæ°ä¸è¦æ±ç¸å31    # enc_input                dec_input           dec_output32    ['ich mochte ein bier P', 'S i want a beer .', 'i want a beer . E'],33    ['ich mochte ein cola P', 'S i want a coke .', 'i want a coke . E']34]35# å¾·è¯åè±è¯çåè¯è¦åå¼å»ºç«è¯åº36# Padding Should be Zero37src_vocab = {'P': 0, 'ich': 1, 'mochte': 2, 'ein': 3, 'bier': 4, 'cola': 5}38src_idx2word = {i: w for i, w in enumerate(src_vocab)}39src_vocab_size = len(src_vocab)40tgt_vocab = {'P': 0, 'i': 1, 'want': 2, 'a': 3, 'beer': 4, 'coke': 5, 'S': 6, 'E': 7, '.': 8}41idx2word = {i: w for i, w in enumerate(tgt_vocab)}42tgt_vocab_size = len(tgt_vocab)43src_len = 5  # ï¼æºå¥åçé¿åº¦ï¼enc_input max sequence length44tgt_len = 6  # dec_input(=dec_output) max sequence length45# Transformer Parameters46d_model = 512  # Embedding Sizeï¼token embeddingåpositionç¼ç ç维度ï¼47d_ff = 2048  # FeedForward dimension (两次线æ§å±ä¸çéèå± 512->2048->512ï¼çº¿æ§å±æ¯ç¨æ¥åç¹å¾æåçï¼ï¼å½ç¶æåä¼åæ¥ä¸ä¸ªprojectionå±48d_k = d_v = 64  # dimension of K(=Q), Vï¼QåKç维度éè¦ç¸åï¼è¿éä¸ºäºæ¹ä¾¿è®©K=Vï¼49n_layers = 6  # number of Encoder of Decoder Layerï¼Blockç个æ°ï¼50n_heads = 8  # number of heads in Multi-Head Attentionï¼æå å¥å¤´ï¼51# ==============================================================================================52# æ°æ®æå»º53def make_data(sentences):54    """æåè¯åºå转æ¢ä¸ºæ°ååºå"""55    enc_inputs, dec_inputs, dec_outputs = [], [], []56    for i in range(len(sentences)):57        enc_input = [[src_vocab[n] for n in sentences[i][0].split()]]  # [[1, 2, 3, 4, 0], [1, 2, 3, 5, 0]]58        dec_input = [[tgt_vocab[n] for n in sentences[i][1].split()]]  # [[6, 1, 2, 3, 4, 8], [6, 1, 2, 3, 5, 8]]59        dec_output = [[tgt_vocab[n] for n in sentences[i][2].split()]]  # [[1, 2, 3, 4, 8, 7], [1, 2, 3, 5, 8, 7]]60        enc_inputs.extend(enc_input)61        dec_inputs.extend(dec_input)62        dec_outputs.extend(dec_output)63    return torch.LongTensor(enc_inputs), torch.LongTensor(dec_inputs), torch.LongTensor(dec_outputs)64enc_inputs, dec_inputs, dec_outputs = make_data(sentences)65class MyDataSet(Data.Dataset):66    """èªå®ä¹DataLoader"""67    def __init__(self, enc_inputs, dec_inputs, dec_outputs):68        super(MyDataSet, self).__init__()69        self.enc_inputs = enc_inputs70        self.dec_inputs = dec_inputs71        self.dec_outputs = dec_outputs72    def __len__(self):73        return self.enc_inputs.shape[0]74    def __getitem__(self, idx):75        return self.enc_inputs[idx], self.dec_inputs[idx], self.dec_outputs[idx]76loader = Data.DataLoader(MyDataSet(enc_inputs, dec_inputs, dec_outputs), 2, True)77# ====================================================================================================78# Transformer模å79class PositionalEncoding(nn.Module):80    def __init__(self, d_model, dropout=0.1, max_len=5000):81        super(PositionalEncoding, self).__init__()82        self.dropout = nn.Dropout(p=dropout)83        pe = torch.zeros(max_len, d_model)84        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)85        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))86        pe[:, 0::2] = torch.sin(position * div_term)87        pe[:, 1::2] = torch.cos(position * div_term)88        pe = pe.unsqueeze(0).transpose(0, 1)89        self.register_buffer('pe', pe)90    def forward(self, x):91        """92        x: [seq_len, batch_size, d_model]93        """94        x = x + self.pe[:x.size(0), :]95        return self.dropout(x)96def get_attn_pad_mask(seq_q, seq_k):97    # pad maskçä½ç¨ï¼å¨å¯¹valueåéå æå¹³åçæ¶åï¼å¯ä»¥è®©pad对åºçalpha_ij=0ï¼è¿æ ·æ³¨æåå°±ä¸ä¼èèå°padåé98    """è¿éçq,kè¡¨ç¤ºçæ¯ä¸¤ä¸ªåºåï¼è·æ³¨æåæºå¶çq,k没æå
³ç³»ï¼ï¼ä¾å¦encoder_inputs (x1,x2,..xm)åencoder_inputs (x1,x2..xm)99    encoderådecoderé½å¯è½è°ç¨è¿ä¸ªå½æ°ï¼æä»¥seq_lenè§æ
åµèå®100    seq_q: [batch_size, seq_len]101    seq_k: [batch_size, seq_len]102    seq_len could be src_len or it could be tgt_len103    seq_len in seq_q and seq_len in seq_k maybe not equal104    """105    batch_size, len_q = seq_q.size()  # è¿ä¸ªseq_qåªæ¯ç¨æ¥expand维度ç106    batch_size, len_k = seq_k.size()107    # eq(zero) is PAD token108    # ä¾å¦:seq_k = [[1,2,3,4,0], [1,2,3,5,0]]109    pad_attn_mask = seq_k.data.eq(0).unsqueeze(1)  # [batch_size, 1, len_k], True is masked110    return pad_attn_mask.expand(batch_size, len_q, len_k)  # [batch_size, len_q, len_k] ææä¸ä¸ªç«æ¹ä½(batch_sizeä¸ªè¿æ ·çç©éµ)111def get_attn_subsequence_mask(seq):112    """建议æå°åºæ¥ççæ¯ä»ä¹çè¾åºï¼ä¸ç®äºç¶ï¼113    seq: [batch_size, tgt_len]114    """115    attn_shape = [seq.size(0), seq.size(1), seq.size(1)]116    # attn_shape: [batch_size, tgt_len, tgt_len]117    subsequence_mask = np.triu(np.ones(attn_shape), k=1)  # çæä¸ä¸ªä¸ä¸è§ç©éµ118    subsequence_mask = torch.from_numpy(subsequence_mask).byte()119    return subsequence_mask  # [batch_size, tgt_len, tgt_len]120# ==========================================================================================121class ScaledDotProductAttention(nn.Module):122    def __init__(self):123        super(ScaledDotProductAttention, self).__init__()124    def forward(self, Q, K, V, attn_mask):125        """126        Q: [batch_size, n_heads, len_q, d_k]127        K: [batch_size, n_heads, len_k, d_k]128        V: [batch_size, n_heads, len_v(=len_k), d_v]129        attn_mask: [batch_size, n_heads, seq_len, seq_len]130        说æï¼å¨encoder-decoderçAttentionå±ä¸len_q(q1,..qt)ålen_k(k1,...km)å¯è½ä¸å131        """132        scores = torch.matmul(Q, K.transpose(-1, -2)) / np.sqrt(d_k)  # scores : [batch_size, n_heads, len_q, len_k]133        # maskç©éµå¡«å
scoresï¼ç¨-1e9å¡«å
scoresä¸ä¸attn_maskä¸å¼ä¸º1ä½ç½®ç¸å¯¹åºçå
ç´ ï¼134        scores.masked_fill_(attn_mask, -1e9)  # Fills elements of self tensor with value where mask is True.135        attn = nn.Softmax(dim=-1)(scores)  # 对æåä¸ä¸ªç»´åº¦(v)åsoftmax136        # scores : [batch_size, n_heads, len_q, len_k] * V: [batch_size, n_heads, len_v(=len_k), d_v]137        context = torch.matmul(attn, V)  # context: [batch_size, n_heads, len_q, d_v]138        # contextï¼[[z1,z2,...],[...]]åé, attn注æåç¨çç©éµï¼ç¨äºå¯è§åçï¼139        return context, attn140class MultiHeadAttention(nn.Module):141    """è¿ä¸ªAttentionç±»å¯ä»¥å®ç°:142    EncoderçSelf-Attention143    DecoderçMasked Self-Attention144    Encoder-DecoderçAttention145    """146    def __init__(self):147        super(MultiHeadAttention, self).__init__()148        self.W_Q = nn.Linear(d_model, d_k * n_heads, bias=False)  # q,kå¿
须维度ç¸åï¼ä¸ç¶æ æ³åç¹ç§¯149        self.W_K = nn.Linear(d_model, d_k * n_heads, bias=False)150        self.W_V = nn.Linear(d_model, d_v * n_heads, bias=False)151        self.fc = nn.Linear(n_heads * d_v, d_model, bias=False)152    def forward(self, input_Q, input_K, input_V, attn_mask):153        """154        input_Q: [batch_size, len_q, d_model]155        input_K: [batch_size, len_k, d_model]156        input_V: [batch_size, len_v(=len_k), d_model]157        attn_mask: [batch_size, seq_len, seq_len]158        """159        residual, batch_size = input_Q, input_Q.size(0)160        # ä¸é¢çå¤å¤´çåæ°ç©éµæ¯æ¾å¨ä¸èµ·å线æ§åæ¢çï¼ç¶ååææå¤ä¸ªå¤´ï¼è¿æ¯å·¥ç¨å®ç°çæå·§161        # B: batch_size, S:seq_len, D: dim162        # (B, S, D) -proj-> (B, S, D_new) -split-> (B, S, Head, W) -trans-> (B, Head, S, W)163        #           线æ§åæ¢               ææå¤å¤´164        # Q: [batch_size, n_heads, len_q, d_k]165        Q = self.W_Q(input_Q).view(batch_size, -1, n_heads, d_k).transpose(1, 2)166        # K: [batch_size, n_heads, len_k, d_k] # KåVçé¿åº¦ä¸å®ç¸åï¼ç»´åº¦å¯ä»¥ä¸å167        K = self.W_K(input_K).view(batch_size, -1, n_heads, d_k).transpose(1, 2)168        # V: [batch_size, n_heads, len_v(=len_k), d_v]169        V = self.W_V(input_V).view(batch_size, -1, n_heads, d_v).transpose(1, 2)170        # å ä¸ºæ¯å¤å¤´ï¼æä»¥maskç©éµè¦æ©å
æ4ç»´ç171        # attn_mask: [batch_size, seq_len, seq_len] -> [batch_size, n_heads, seq_len, seq_len]172        attn_mask = attn_mask.unsqueeze(1).repeat(1, n_heads, 1, 1)173        # context: [batch_size, n_heads, len_q, d_v], attn: [batch_size, n_heads, len_q, len_k]174        context, attn = ScaledDotProductAttention()(Q, K, V, attn_mask)175        # ä¸é¢å°ä¸å头çè¾åºåéæ¼æ¥å¨ä¸èµ·176        # context: [batch_size, n_heads, len_q, d_v] -> [batch_size, len_q, n_heads * d_v]177        context = context.transpose(1, 2).reshape(batch_size, -1, n_heads * d_v)178        # ååä¸ä¸ªprojection179        output = self.fc(context)  # [batch_size, len_q, d_model]180        return nn.LayerNorm(d_model).to(device)(output + residual), attn181# Pytorchä¸çLinearåªä¼å¯¹æåä¸ç»´æä½ï¼æä»¥æ£å¥½æ¯æä»¬å¸æçæ¯ä¸ªä½ç½®ç¨åä¸ä¸ªå
¨è¿æ¥ç½ç»182class PoswiseFeedForwardNet(nn.Module):183    def __init__(self):184        super(PoswiseFeedForwardNet, self).__init__()185        self.fc = nn.Sequential(186            nn.Linear(d_model, d_ff, bias=False),187            nn.ReLU(),188            nn.Linear(d_ff, d_model, bias=False)189        )190    def forward(self, inputs):191        """192        inputs: [batch_size, seq_len, d_model]193        """194        residual = inputs195        output = self.fc(inputs)196        return nn.LayerNorm(d_model).to(device)(output + residual)  # [batch_size, seq_len, d_model]197class EncoderLayer(nn.Module):198    def __init__(self):199        super(EncoderLayer, self).__init__()200        self.enc_self_attn = MultiHeadAttention()201        self.pos_ffn = PoswiseFeedForwardNet()202    def forward(self, enc_inputs, enc_self_attn_mask):203        """E204        enc_inputs: [batch_size, src_len, d_model]205        enc_self_attn_mask: [batch_size, src_len, src_len]  maskç©éµ(pad mask or sequence mask)206        """207        # enc_outputs: [batch_size, src_len, d_model], attn: [batch_size, n_heads, src_len, src_len]208        # 第ä¸ä¸ªenc_inputs * W_Q = Q209        # 第äºä¸ªenc_inputs * W_K = K210        # 第ä¸ä¸ªenc_inputs * W_V = V211        enc_outputs, attn = self.enc_self_attn(enc_inputs, enc_inputs, enc_inputs,212                                               enc_self_attn_mask)  # enc_inputs to same Q,K,Vï¼æªçº¿æ§åæ¢åï¼213        enc_outputs = self.pos_ffn(enc_outputs)214        # enc_outputs: [batch_size, src_len, d_model]215        return enc_outputs, attn216class DecoderLayer(nn.Module):217    def __init__(self):218        super(DecoderLayer, self).__init__()219        self.dec_self_attn = MultiHeadAttention()220        self.dec_enc_attn = MultiHeadAttention()221        self.pos_ffn = PoswiseFeedForwardNet()222    def forward(self, dec_inputs, enc_outputs, dec_self_attn_mask, dec_enc_attn_mask):223        """224        dec_inputs: [batch_size, tgt_len, d_model]225        enc_outputs: [batch_size, src_len, d_model]226        dec_self_attn_mask: [batch_size, tgt_len, tgt_len]227        dec_enc_attn_mask: [batch_size, tgt_len, src_len]228        """229        # dec_outputs: [batch_size, tgt_len, d_model], dec_self_attn: [batch_size, n_heads, tgt_len, tgt_len]230        dec_outputs, dec_self_attn = self.dec_self_attn(dec_inputs, dec_inputs, dec_inputs,231                                                        dec_self_attn_mask)  # è¿éçQ,K,Vå
¨æ¯Decoderèªå·±çè¾å
¥232        # dec_outputs: [batch_size, tgt_len, d_model], dec_enc_attn: [batch_size, h_heads, tgt_len, src_len]233        dec_outputs, dec_enc_attn = self.dec_enc_attn(dec_outputs, enc_outputs, enc_outputs,234                                                      dec_enc_attn_mask)  # Attentionå±çQ(æ¥èªdecoder) å K,V(æ¥èªencoder)235        dec_outputs = self.pos_ffn(dec_outputs)  # [batch_size, tgt_len, d_model]236        return dec_outputs, dec_self_attn, dec_enc_attn  # dec_self_attn, dec_enc_attnè¿ä¸¤ä¸ªæ¯ä¸ºäºå¯è§åç237class Encoder(nn.Module):238    def __init__(self):239        super(Encoder, self).__init__()240        self.src_emb = nn.Embedding(src_vocab_size, d_model)  # token Embedding241        self.pos_emb = PositionalEncoding(d_model)  # Transformerä¸ä½ç½®ç¼ç æ¶åºå®çï¼ä¸éè¦å¦ä¹ 242        self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])243    def forward(self, enc_inputs):244        """245        enc_inputs: [batch_size, src_len]246        """247        enc_outputs = self.src_emb(enc_inputs)  # [batch_size, src_len, d_model]248        enc_outputs = self.pos_emb(enc_outputs.transpose(0, 1)).transpose(0, 1)  # [batch_size, src_len, d_model]249        # Encoderè¾å
¥åºåçpad maskç©éµ250        enc_self_attn_mask = get_attn_pad_mask(enc_inputs, enc_inputs)  # [batch_size, src_len, src_len]251        enc_self_attns = []  # å¨è®¡ç®ä¸ä¸éè¦ç¨å°ï¼å®ä¸»è¦ç¨æ¥ä¿åä½ æ¥ä¸æ¥è¿åçattentionçå¼ï¼è¿ä¸ªä¸»è¦æ¯ä¸ºäºä½ ç»çåå¾çï¼ç¨æ¥çå个è¯ä¹é´çå
³ç³»252        for layer in self.layers:  # for循ç¯è®¿é®nn.ModuleList对象253            # ä¸ä¸ä¸ªblockçè¾åºenc_outputsä½ä¸ºå½åblockçè¾å
¥254            # enc_outputs: [batch_size, src_len, d_model], enc_self_attn: [batch_size, n_heads, src_len, src_len]255            enc_outputs, enc_self_attn = layer(enc_outputs,256                                               enc_self_attn_mask)  # ä¼ å
¥çenc_outputså
¶å®æ¯inputï¼ä¼ å
¥maskç©éµæ¯å ä¸ºä½ è¦åself attention257            enc_self_attns.append(enc_self_attn)  # è¿ä¸ªåªæ¯ä¸ºäºå¯è§å258        return enc_outputs, enc_self_attns259class Decoder(nn.Module):260    def __init__(self):261        super(Decoder, self).__init__()262        self.tgt_emb = nn.Embedding(tgt_vocab_size, d_model)  # Decoderè¾å
¥çembedè¯è¡¨263        self.pos_emb = PositionalEncoding(d_model)264        self.layers = nn.ModuleList([DecoderLayer() for _ in range(n_layers)])  # Decoderçblocks265    def forward(self, dec_inputs, enc_inputs, enc_outputs):266        """267        dec_inputs: [batch_size, tgt_len]268        enc_inputs: [batch_size, src_len]269        enc_outputs: [batch_size, src_len, d_model]   # ç¨å¨Encoder-Decoder Attentionå±270        """271        dec_outputs = self.tgt_emb(dec_inputs)  # [batch_size, tgt_len, d_model]272        dec_outputs = self.pos_emb(dec_outputs.transpose(0, 1)).transpose(0, 1).to(273            device)  # [batch_size, tgt_len, d_model]274        # Decoderè¾å
¥åºåçpad maskç©éµï¼è¿ä¸ªä¾åä¸decoderæ¯æ²¡æå padçï¼å®é
åºç¨ä¸é½æ¯æpadå¡«å
çï¼275        dec_self_attn_pad_mask = get_attn_pad_mask(dec_inputs, dec_inputs).to(device)  # [batch_size, tgt_len, tgt_len]276        # Masked Self_Attentionï¼å½åæ¶å»æ¯çä¸å°æªæ¥çä¿¡æ¯ç277        dec_self_attn_subsequence_mask = get_attn_subsequence_mask(dec_inputs).to(278            device)  # [batch_size, tgt_len, tgt_len]279        # Decoder䏿䏤ç§maskç©éµç¸å ï¼æ¢å±è½äºpadçä¿¡æ¯ï¼ä¹å±è½äºæªæ¥æ¶å»çä¿¡æ¯ï¼280        dec_self_attn_mask = torch.gt((dec_self_attn_pad_mask + dec_self_attn_subsequence_mask),281                                      0).to(device)  # [batch_size, tgt_len, tgt_len]; torch.gtæ¯è¾ä¸¤ä¸ªç©éµçå
ç´ ï¼å¤§äºåè¿å1ï¼å¦åè¿å0282        # è¿ä¸ªmask主è¦ç¨äºencoder-decoder attentionå±283        # get_attn_pad_maskä¸»è¦æ¯enc_inputsçpad maskç©éµ(å ä¸ºencæ¯å¤çK,Vçï¼æ±Attentionæ¶æ¯ç¨v1,v2,..vmå»å æçï¼è¦æpad对åºçv_içç¸å
³ç³»æ°è®¾ä¸º0ï¼è¿æ ·æ³¨æåå°±ä¸ä¼å
³æ³¨padåé)284        #                       dec_inputsåªæ¯æä¾expandçsizeç285        dec_enc_attn_mask = get_attn_pad_mask(dec_inputs, enc_inputs)  # [batc_size, tgt_len, src_len]286        dec_self_attns, dec_enc_attns = [], []287        for layer in self.layers:288            # dec_outputs: [batch_size, tgt_len, d_model], dec_self_attn: [batch_size, n_heads, tgt_len, tgt_len], dec_enc_attn: [batch_size, h_heads, tgt_len, src_len]289            # DecoderçBlockæ¯ä¸ä¸ä¸ªBlockçè¾åºdec_outputsï¼ååï¼åEncoderç½ç»çè¾åºenc_outputsï¼åºå®ï¼290            dec_outputs, dec_self_attn, dec_enc_attn = layer(dec_outputs, enc_outputs, dec_self_attn_mask,291                                                             dec_enc_attn_mask)292            dec_self_attns.append(dec_self_attn)293            dec_enc_attns.append(dec_enc_attn)294        # dec_outputs: [batch_size, tgt_len, d_model]295        return dec_outputs, dec_self_attns, dec_enc_attns296class Transformer(nn.Module):297    def __init__(self):298        super(Transformer, self).__init__()299        self.encoder = Encoder().to(device)300        self.decoder = Decoder().to(device)301        self.projection = nn.Linear(d_model, tgt_vocab_size, bias=False).to(device)302    def forward(self, enc_inputs, dec_inputs):303        """Transformersçè¾å
¥ï¼ä¸¤ä¸ªåºå304        enc_inputs: [batch_size, src_len]305        dec_inputs: [batch_size, tgt_len]306        """307        # tensor to store decoder outputs308        # outputs = torch.zeros(batch_size, tgt_len, tgt_vocab_size).to(self.device)309        # enc_outputs: [batch_size, src_len, d_model], enc_self_attns: [n_layers, batch_size, n_heads, src_len, src_len]310        # ç»è¿Encoderç½ç»åï¼å¾å°çè¾åºè¿æ¯[batch_size, src_len, d_model]311        enc_outputs, enc_self_attns = self.encoder(enc_inputs)312        # dec_outputs: [batch_size, tgt_len, d_model], dec_self_attns: [n_layers, batch_size, n_heads, tgt_len, tgt_len], dec_enc_attn: [n_layers, batch_size, tgt_len, src_len]313        dec_outputs, dec_self_attns, dec_enc_attns = self.decoder(dec_inputs, enc_inputs, enc_outputs)314        # dec_outputs: [batch_size, tgt_len, d_model] -> dec_logits: [batch_size, tgt_len, tgt_vocab_size]315        dec_logits = self.projection(dec_outputs)316        return dec_logits.view(-1, dec_logits.size(-1)), enc_self_attns, dec_self_attns, dec_enc_attns317model = Transformer().to(device)318# è¿éçæå¤±å½æ°éé¢è®¾ç½®äºä¸ä¸ªåæ° ignore_index=0ï¼å ä¸º "pad" è¿ä¸ªåè¯çç´¢å¼ä¸º 0ï¼è¿æ ·è®¾ç½®ä»¥åï¼å°±ä¸ä¼è®¡ç® "pad" çæå¤±ï¼å ä¸ºæ¬æ¥ "pad" 乿²¡ææä¹ï¼ä¸éè¦è®¡ç®ï¼319criterion = nn.CrossEntropyLoss(ignore_index=0)320optimizer = optim.SGD(model.parameters(), lr=1e-3, momentum=0.99)  # ç¨adamçè¯ææä¸å¥½321# ====================================================================================================322for epoch in range(epochs):323    for enc_inputs, dec_inputs, dec_outputs in loader:324        """325        enc_inputs: [batch_size, src_len]326        dec_inputs: [batch_size, tgt_len]327        dec_outputs: [batch_size, tgt_len]328        """329        enc_inputs, dec_inputs, dec_outputs = enc_inputs.to(device), dec_inputs.to(device), dec_outputs.to(device)330        # outputs: [batch_size * tgt_len, tgt_vocab_size]331        outputs, enc_self_attns, dec_self_attns, dec_enc_attns = model(enc_inputs, dec_inputs)332        loss = criterion(outputs, dec_outputs.view(-1))  # dec_outputs.view(-1):[batch_size * tgt_len * tgt_vocab_size]333        print('Epoch:', '%04d' % (epoch + 1), 'loss =', '{:.6f}'.format(loss))334        optimizer.zero_grad()335        loss.backward()336        optimizer.step()337def greedy_decoder(model, enc_input, start_symbol):338    """è´ªå¿ç¼ç 339    For simplicity, a Greedy Decoder is Beam search when K=1. This is necessary for inference as we don't know the340    target sequence input. Therefore we try to generate the target input word by word, then feed it into the transformer.341    Starting Reference: http://nlp.seas.harvard.edu/2018/04/03/attention.html#greedy-decoding342    :param model: Transformer Model343    :param enc_input: The encoder input344    :param start_symbol: The start symbol. In this example it is 'S' which corresponds to index 4345    :return: The target input346    """347    enc_outputs, enc_self_attns = model.encoder(enc_input)348    dec_input = torch.zeros(1, 0).type_as(enc_input.data)349    terminal = False350    next_symbol = start_symbol351    while not terminal:352        # 颿µé¶æ®µï¼dec_inputåºåä¼ä¸ç¹ç¹åé¿ï¼æ¯æ¬¡æ·»å ä¸ä¸ªæ°é¢æµåºæ¥çåè¯ï¼353        dec_input = torch.cat([dec_input.to(device), torch.tensor([[next_symbol]], dtype=enc_input.dtype).to(device)],354                              -1)355        dec_outputs, _, _ = model.decoder(dec_input, enc_input, enc_outputs)356        projected = model.projection(dec_outputs)357        prob = projected.squeeze(0).max(dim=-1, keepdim=False)[1]358        # å¢éæ´æ°ï¼æä»¬å¸æéå¤åè¯é¢æµç»ææ¯ä¸æ ·çï¼359        # æä»¬å¨é¢æµæ¯ä¼éæ©æ§å¿½ç¥éå¤ç颿µçè¯ï¼åªæåææ°é¢æµçåè¯æ¼æ¥å°è¾å
¥åºåä¸360        next_word = prob.data[-1]  # æ¿åºå½å颿µçåè¯(æ°å)ãæä»¬ç¨x'_t对åºçè¾åºz_tå»é¢æµä¸ä¸ä¸ªåè¯çæ¦çï¼ä¸ç¨z_1,z_2..z_{t-1}361        next_symbol = next_word362        if next_symbol == tgt_vocab["E"]:363            terminal = True364        # print(next_word)365    # greedy_dec_predict = torch.cat(366    #     [dec_input.to(device), torch.tensor([[next_symbol]], dtype=enc_input.dtype).to(device)],367    #     -1)368    greedy_dec_predict = dec_input[:, 1:]369    return greedy_dec_predict370# ==========================================================================================371# 颿µé¶æ®µ372enc_inputs, _, _ = next(iter(loader))373for i in range(len(enc_inputs)):374    greedy_dec_predict = greedy_decoder(model, enc_inputs[i].view(1, -1).to(device), start_symbol=tgt_vocab["S"])375    print(enc_inputs[i], '->', greedy_dec_predict.squeeze())376    print([src_idx2word[t.item()] for t in enc_inputs[i]], '->',...DecParserUnitTest.py
Source:DecParserUnitTest.py  
1## @file2# This file contain unit test for DecParser3#4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>5#6# SPDX-License-Identifier: BSD-2-Clause-Patent78import os9import unittest10from Logger.Log import FatalError1112from Parser.DecParser import \13    Dec, \14    _DecDefine, \15    _DecLibraryclass, \16    _DecPcd, \17    _DecGuid, \18    FileContent, \19    _DecBase, \20    CleanString2122from Object.Parser.DecObject import _DecComments2324#25# Test CleanString26#27class CleanStringTestCase(unittest.TestCase):28    def testCleanString(self):29        Line, Comment = CleanString('')30        self.assertEqual(Line, '')31        self.assertEqual(Comment, '')3233        Line, Comment = CleanString('line without comment')34        self.assertEqual(Line, 'line without comment')35        self.assertEqual(Comment, '')3637        Line, Comment = CleanString('# pure comment')38        self.assertEqual(Line, '')39        self.assertEqual(Comment, '# pure comment')4041        Line, Comment = CleanString('line # and comment')42        self.assertEqual(Line, 'line')43        self.assertEqual(Comment, '# and comment')4445    def testCleanStringCpp(self):46        Line, Comment = CleanString('line // and comment', AllowCppStyleComment = True)47        self.assertEqual(Line, 'line')48        self.assertEqual(Comment, '# and comment')4950#51# Test _DecBase._MacroParser function52#53class MacroParserTestCase(unittest.TestCase):54    def setUp(self):55        self.dec = _DecBase(FileContent('dummy', []))5657    def testCorrectMacro(self):58        self.dec._MacroParser('DEFINE MACRO1 = test1')59        self.failIf('MACRO1' not in self.dec._LocalMacro)60        self.assertEqual(self.dec._LocalMacro['MACRO1'], 'test1')6162    def testErrorMacro1(self):63        # Raise fatal error, macro name must be upper case letter64        self.assertRaises(FatalError, self.dec._MacroParser, 'DEFINE not_upper_case = test2')6566    def testErrorMacro2(self):67        # No macro name given68        self.assertRaises(FatalError, self.dec._MacroParser, 'DEFINE ')6970#71# Test _DecBase._TryBackSlash function72#73class TryBackSlashTestCase(unittest.TestCase):74    def setUp(self):75        Content = [76            # Right case77            'test no backslash',7879            'test with backslash \\',80            'continue second line',8182            # Do not precede with whitespace83            '\\',8485            # Empty line after backlash is not allowed86            'line with backslash \\',87            ''88        ]89        self.dec = _DecBase(FileContent('dummy', Content))9091    def testBackSlash(self):92        #93        # Right case, assert return values94        #95        ConcatLine, CommentList = self.dec._TryBackSlash(self.dec._RawData.GetNextLine(), [])96        self.assertEqual(ConcatLine, 'test no backslash')97        self.assertEqual(CommentList, [])9899        ConcatLine, CommentList = self.dec._TryBackSlash(self.dec._RawData.GetNextLine(), [])100        self.assertEqual(CommentList, [])101        self.assertEqual(ConcatLine, 'test with backslash continue second line')102103        #104        # Error cases, assert raise exception105        #106        self.assertRaises(FatalError, self.dec._TryBackSlash, self.dec._RawData.GetNextLine(), [])107        self.assertRaises(FatalError, self.dec._TryBackSlash, self.dec._RawData.GetNextLine(), [])108109#110# Test _DecBase.Parse function111#112class DataItem(_DecComments):113    def __init__(self):114        _DecComments.__init__(self)115        self.String = ''116117class Data(_DecComments):118    def __init__(self):119        _DecComments.__init__(self)120        # List of DataItem121        self.ItemList = []122123class TestInner(_DecBase):124    def __init__(self, RawData):125        _DecBase.__init__(self, RawData)126        self.ItemObject = Data()127128    def _StopCurrentParsing(self, Line):129        return Line == '[TOP]'130131    def _ParseItem(self):132        Item = DataItem()133        Item.String = self._RawData.CurrentLine134        self.ItemObject.ItemList.append(Item)135        return Item136137    def _TailCommentStrategy(self, Comment):138        return Comment.find('@comment') != -1139140class TestTop(_DecBase):141    def __init__(self, RawData):142        _DecBase.__init__(self, RawData)143        # List of Data144        self.ItemObject = []145146    # Top parser147    def _StopCurrentParsing(self, Line):148        return False149150    def _ParseItem(self):151        TestParser = TestInner(self._RawData)152        TestParser.Parse()153        self.ItemObject.append(TestParser.ItemObject)154        return TestParser.ItemObject155156class ParseTestCase(unittest.TestCase):157    def setUp(self):158        pass159160    def testParse(self):161        Content = \162        '''# Top comment163        [TOP]164          # sub1 head comment165          (test item has both head and tail comment) # sub1 tail comment166          # sub2 head comment167          (test item has head and special tail comment)168          # @comment test TailCommentStrategy branch169170          (test item has no comment)171172        # test NextLine branch173        [TOP]174          sub-item175        '''176        dec = TestTop(FileContent('dummy', Content.splitlines()))177        dec.Parse()178179        # Two sections180        self.assertEqual(len(dec.ItemObject), 2)181182        data = dec.ItemObject[0]183        self.assertEqual(data._HeadComment[0][0], '# Top comment')184        self.assertEqual(data._HeadComment[0][1], 1)185186        # 3 subitems187        self.assertEqual(len(data.ItemList), 3)188189        dataitem = data.ItemList[0]190        self.assertEqual(dataitem.String, '(test item has both head and tail comment)')191        # Comment content192        self.assertEqual(dataitem._HeadComment[0][0], '# sub1 head comment')193        self.assertEqual(dataitem._TailComment[0][0], '# sub1 tail comment')194        # Comment line number195        self.assertEqual(dataitem._HeadComment[0][1], 3)196        self.assertEqual(dataitem._TailComment[0][1], 4)197198        dataitem = data.ItemList[1]199        self.assertEqual(dataitem.String, '(test item has head and special tail comment)')200        # Comment content201        self.assertEqual(dataitem._HeadComment[0][0], '# sub2 head comment')202        self.assertEqual(dataitem._TailComment[0][0], '# @comment test TailCommentStrategy branch')203        # Comment line number204        self.assertEqual(dataitem._HeadComment[0][1], 5)205        self.assertEqual(dataitem._TailComment[0][1], 7)206207        dataitem = data.ItemList[2]208        self.assertEqual(dataitem.String, '(test item has no comment)')209        # Comment content210        self.assertEqual(dataitem._HeadComment, [])211        self.assertEqual(dataitem._TailComment, [])212213        data = dec.ItemObject[1]214        self.assertEqual(data._HeadComment[0][0], '# test NextLine branch')215        self.assertEqual(data._HeadComment[0][1], 11)216217        # 1 subitems218        self.assertEqual(len(data.ItemList), 1)219220        dataitem = data.ItemList[0]221        self.assertEqual(dataitem.String, 'sub-item')222        self.assertEqual(dataitem._HeadComment, [])223        self.assertEqual(dataitem._TailComment, [])224225#226# Test _DecDefine._ParseItem227#228class DecDefineTestCase(unittest.TestCase):229    def GetObj(self, Content):230        Obj = _DecDefine(FileContent('dummy', Content.splitlines()))231        Obj._RawData.CurrentLine = Obj._RawData.GetNextLine()232        return Obj233234    def testDecDefine(self):235        item = self.GetObj('PACKAGE_NAME = MdePkg')._ParseItem()236        self.assertEqual(item.Key, 'PACKAGE_NAME')237        self.assertEqual(item.Value, 'MdePkg')238239    def testDecDefine1(self):240        obj = self.GetObj('PACKAGE_NAME')241        self.assertRaises(FatalError, obj._ParseItem)242243    def testDecDefine2(self):244        obj = self.GetObj('unknown_key = ')245        self.assertRaises(FatalError, obj._ParseItem)246247    def testDecDefine3(self):248        obj = self.GetObj('PACKAGE_NAME = ')249        self.assertRaises(FatalError, obj._ParseItem)250251#252# Test _DecLibraryclass._ParseItem253#254class DecLibraryTestCase(unittest.TestCase):255    def GetObj(self, Content):256        Obj = _DecLibraryclass(FileContent('dummy', Content.splitlines()))257        Obj._RawData.CurrentLine = Obj._RawData.GetNextLine()258        return Obj259260    def testNoInc(self):261        obj = self.GetObj('UefiRuntimeLib')262        self.assertRaises(FatalError, obj._ParseItem)263264    def testEmpty(self):265        obj = self.GetObj(' | ')266        self.assertRaises(FatalError, obj._ParseItem)267268    def testLibclassNaming(self):269        obj = self.GetObj('lowercase_efiRuntimeLib|Include/Library/UefiRuntimeLib.h')270        self.assertRaises(FatalError, obj._ParseItem)271272    def testLibclassExt(self):273        obj = self.GetObj('RuntimeLib|Include/Library/UefiRuntimeLib.no_h')274        self.assertRaises(FatalError, obj._ParseItem)275276    def testLibclassRelative(self):277        obj = self.GetObj('RuntimeLib|Include/../UefiRuntimeLib.h')278        self.assertRaises(FatalError, obj._ParseItem)279280#281# Test _DecPcd._ParseItem282#283class DecPcdTestCase(unittest.TestCase):284    def GetObj(self, Content):285        Obj = _DecPcd(FileContent('dummy', Content.splitlines()))286        Obj._RawData.CurrentLine = Obj._RawData.GetNextLine()287        Obj._RawData.CurrentScope = [('PcdsFeatureFlag'.upper(), 'COMMON')]288        return Obj289290    def testOK(self):291        item = self.GetObj('gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d')._ParseItem()292        self.assertEqual(item.TokenSpaceGuidCName, 'gEfiMdePkgTokenSpaceGuid')293        self.assertEqual(item.TokenCName, 'PcdComponentNameDisable')294        self.assertEqual(item.DefaultValue, 'FALSE')295        self.assertEqual(item.DatumType, 'BOOLEAN')296        self.assertEqual(item.TokenValue, '0x0000000d')297298    def testNoCvar(self):299        obj = self.GetObj('123ai.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d')300        self.assertRaises(FatalError, obj._ParseItem)301302    def testSplit(self):303        obj = self.GetObj('gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable FALSE|BOOLEAN|0x0000000d')304        self.assertRaises(FatalError, obj._ParseItem)305306        obj = self.GetObj('gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d | abc')307        self.assertRaises(FatalError, obj._ParseItem)308309    def testUnknownType(self):310        obj = self.GetObj('gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|unknown|0x0000000d')311        self.assertRaises(FatalError, obj._ParseItem)312313    def testVoid(self):314        obj = self.GetObj('gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|abc|VOID*|0x0000000d')315        self.assertRaises(FatalError, obj._ParseItem)316317    def testUINT(self):318        obj = self.GetObj('gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|0xabc|UINT8|0x0000000d')319        self.assertRaises(FatalError, obj._ParseItem)320321#322# Test _DecInclude._ParseItem323#324class DecIncludeTestCase(unittest.TestCase):325    #326    # Test code to be added327    #328    pass329330#331# Test _DecGuid._ParseItem332#333class DecGuidTestCase(unittest.TestCase):334    def GetObj(self, Content):335        Obj = _DecGuid(FileContent('dummy', Content.splitlines()))336        Obj._RawData.CurrentLine = Obj._RawData.GetNextLine()337        Obj._RawData.CurrentScope = [('guids'.upper(), 'COMMON')]338        return Obj339340    def testCValue(self):341        item = self.GetObj('gEfiIpSecProtocolGuid={ 0xdfb386f7, 0xe100, 0x43ad,'342                           ' {0x9c, 0x9a, 0xed, 0x90, 0xd0, 0x8a, 0x5e, 0x12 }}')._ParseItem()343        self.assertEqual(item.GuidCName, 'gEfiIpSecProtocolGuid')344        self.assertEqual(item.GuidCValue, '{ 0xdfb386f7, 0xe100, 0x43ad, {0x9c, 0x9a, 0xed, 0x90, 0xd0, 0x8a, 0x5e, 0x12 }}')345346    def testGuidString(self):347        item = self.GetObj('gEfiIpSecProtocolGuid=1E73767F-8F52-4603-AEB4-F29B510B6766')._ParseItem()348        self.assertEqual(item.GuidCName, 'gEfiIpSecProtocolGuid')349        self.assertEqual(item.GuidCValue, '1E73767F-8F52-4603-AEB4-F29B510B6766')350351    def testNoValue1(self):352        obj = self.GetObj('gEfiIpSecProtocolGuid')353        self.assertRaises(FatalError, obj._ParseItem)354355    def testNoValue2(self):356        obj = self.GetObj('gEfiIpSecProtocolGuid=')357        self.assertRaises(FatalError, obj._ParseItem)358359    def testNoName(self):360        obj = self.GetObj('=')361        self.assertRaises(FatalError, obj._ParseItem)362363#364# Test Dec.__init__365#366class DecDecInitTestCase(unittest.TestCase):367    def testNoDecFile(self):368        self.assertRaises(FatalError, Dec, 'No_Such_File')369370class TmpFile:371    def __init__(self, File):372        self.File = File373374    def Write(self, Content):375        try:376            FileObj = open(self.File, 'w')377            FileObj.write(Content)378            FileObj.close()379        except:380            pass381382    def Remove(self):383        try:384            os.remove(self.File)385        except:386            pass387388#389# Test Dec._UserExtentionSectionParser390#391class DecUESectionTestCase(unittest.TestCase):392    def setUp(self):393        self.File = TmpFile('test.dec')394        self.File.Write(395'''[userextensions.intel."myid"]396[userextensions.intel."myid".IA32]397[userextensions.intel."myid".IA32,]398[userextensions.intel."myid]399'''400        )401402    def tearDown(self):403        self.File.Remove()404405    def testUserExtentionHeader(self):406        dec = Dec('test.dec', False)407408        # OK: [userextensions.intel."myid"]409        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]410        dec._UserExtentionSectionParser()411        self.assertEqual(len(dec._RawData.CurrentScope), 1)412        self.assertEqual(dec._RawData.CurrentScope[0][0], 'userextensions'.upper())413        self.assertEqual(dec._RawData.CurrentScope[0][1], 'intel')414        self.assertEqual(dec._RawData.CurrentScope[0][2], '"myid"')415        self.assertEqual(dec._RawData.CurrentScope[0][3], 'COMMON')416417        # OK: [userextensions.intel."myid".IA32]418        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]419        dec._UserExtentionSectionParser()420        self.assertEqual(len(dec._RawData.CurrentScope), 1)421        self.assertEqual(dec._RawData.CurrentScope[0][0], 'userextensions'.upper())422        self.assertEqual(dec._RawData.CurrentScope[0][1], 'intel')423        self.assertEqual(dec._RawData.CurrentScope[0][2], '"myid"')424        self.assertEqual(dec._RawData.CurrentScope[0][3], 'IA32')425426        # Fail: [userextensions.intel."myid".IA32,]427        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]428        self.assertRaises(FatalError, dec._UserExtentionSectionParser)429430        # Fail: [userextensions.intel."myid]431        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]432        self.assertRaises(FatalError, dec._UserExtentionSectionParser)433434#435# Test Dec._SectionHeaderParser436#437class DecSectionTestCase(unittest.TestCase):438    def setUp(self):439        self.File = TmpFile('test.dec')440        self.File.Write(441'''[no section start or end442[,] # empty sub-section443[unknow_section_name]444[Includes.IA32.other] # no third one445[PcdsFeatureFlag, PcdsFixedAtBuild] # feature flag PCD must not be in the same section of other types of PCD446[Includes.IA32, Includes.IA32]447[Includes, Includes.IA32] # common cannot be with other arch448[Includes.IA32, PcdsFeatureFlag] # different section name449'''     )450451    def tearDown(self):452        self.File.Remove()453454    def testSectionHeader(self):455        dec = Dec('test.dec', False)456        # [no section start or end457        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]458        self.assertRaises(FatalError, dec._SectionHeaderParser)459460        #[,] # empty sub-section461        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]462        self.assertRaises(FatalError, dec._SectionHeaderParser)463464        # [unknow_section_name]465        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]466        self.assertRaises(FatalError, dec._SectionHeaderParser)467468        # [Includes.IA32.other] # no third one469        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]470        self.assertRaises(FatalError, dec._SectionHeaderParser)471472        # [PcdsFeatureFlag, PcdsFixedAtBuild]473        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]474        self.assertRaises(FatalError, dec._SectionHeaderParser)475476        # [Includes.IA32, Includes.IA32]477        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]478        dec._SectionHeaderParser()479        self.assertEqual(len(dec._RawData.CurrentScope), 1)480        self.assertEqual(dec._RawData.CurrentScope[0][0], 'Includes'.upper())481        self.assertEqual(dec._RawData.CurrentScope[0][1], 'IA32')482483        # [Includes, Includes.IA32] # common cannot be with other arch484        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]485        self.assertRaises(FatalError, dec._SectionHeaderParser)486487        # [Includes.IA32, PcdsFeatureFlag] # different section name not allowed488        dec._RawData.CurrentLine = CleanString(dec._RawData.GetNextLine())[0]489        self.assertRaises(FatalError, dec._SectionHeaderParser)490491#492# Test Dec._ParseDecComment493#494class DecDecCommentTestCase(unittest.TestCase):495    def testDecHeadComment(self):496        File = TmpFile('test.dec')497        File.Write(498       '''# abc499          ##''')500        dec = Dec('test.dec', False)501        dec.ParseDecComment()502        self.assertEqual(len(dec._HeadComment), 2)503        self.assertEqual(dec._HeadComment[0][0], '# abc')504        self.assertEqual(dec._HeadComment[0][1], 1)505        self.assertEqual(dec._HeadComment[1][0], '##')506        self.assertEqual(dec._HeadComment[1][1], 2)507        File.Remove()508509    def testNoDoubleComment(self):510        File = TmpFile('test.dec')511        File.Write(512       '''# abc513          #514          [section_start]''')515        dec = Dec('test.dec', False)516        dec.ParseDecComment()517        self.assertEqual(len(dec._HeadComment), 2)518        self.assertEqual(dec._HeadComment[0][0], '# abc')519        self.assertEqual(dec._HeadComment[0][1], 1)520        self.assertEqual(dec._HeadComment[1][0], '#')521        self.assertEqual(dec._HeadComment[1][1], 2)522        File.Remove()523524if __name__ == '__main__':525    import Logger.Logger526    Logger.Logger.Initialize()527    unittest.main()
...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
