def testMutate0(): lib = mkDefaultLib() # region #+ Add recogFive to library inType = mkRealTensorSort([1, 1, 28, 28]) outType = mkBoolTensorSort([1, 1]) recogDigitType = mkFuncSort(inType, outType) lib.addItem(PPLibItem('recogFive', recogDigitType, None)) # endregion inType = mkListSort(mkRealTensorSort([1, 1, 28, 28])) outType = mkListSort(mkBoolTensorSort([1, 1])) sort = mkFuncSort(inType, outType) prog = PPFuncApp(fn=PPVar(name='lib.map_l'), args=[PPVar(name='lib.recogFive')]) for i in range(100): newProg = mutate(prog, lib) if newProg is None: continue print(repr_py(newProg)) isort = inferType(newProg, lib) assert isort is not None assert sort == isort
def testInfer7(): lib = mkDefaultLib() # region #+ Add recogFive to library inType = mkRealTensorSort([1, 1, 28, 28]) outType = mkBoolTensorSort([1, 1]) recogDigitType = mkFuncSort(inType, outType) lib.addItem(PPLibItem('recogFive', recogDigitType, None)) # endregion prog = PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPTermUnk( name='Unk', sort=PPFuncSort( args=[ PPFuncSort( args=[ PPListSort(param_sort=PPSortVar(name='A_1')) ], rtpe=PPListSort(param_sort=PPSortVar(name='B'))) ], rtpe=PPListSort(param_sort=PPTensorSort( param_sort=PPBool(), shape=[PPDimConst( value=1), PPDimConst(value=1)])))), PPVar(name='lib.map_l') ]) sortInferred = inferType(prog, lib) assert sortInferred is None
def testMutate2(): """ Unkprog """ lib = mkDefaultLib() addRecogFive(lib) inType = mkListSort(mkRealTensorSort([1, 1, 28, 28])) outType = mkRealTensorSort([1, 1]) sort = mkFuncSort(inType, outType) prog = \ PPTermUnk(name='nn_fun_x_906', sort=sort) for i in range(100): newProg = mutate(prog, lib) if newProg is None: continue # print(repr_py(newProg)) # print('newProg: ', newProg) # print('newProgReprPy: ', repr_py(newProg)) isort = inferType(newProg, lib) if isort is None: print(repr_py(newProg)) print('newProg: ', newProg) continue assert sort == isort
def testInfer5(): lib = mkDefaultLib() # region #+ Add recogFive to library inType = mkRealTensorSort([1, 1, 28, 28]) outType = mkBoolTensorSort([1, 1]) recogDigitType = mkFuncSort(inType, outType) lib.addItem(PPLibItem('recogFive', recogDigitType, None)) # endregion inType = mkRealTensorSort([1, 1, 28, 28]) outType = mkRealTensorSort([1, 1]) sort = mkFuncSort(inType, outType) prog = PPVar(name='lib.recogFive') sortInferred = inferType(prog, lib) print(repr_py_sort(sort)) print(sort) print(repr_py_sort(sortInferred)) print(sortInferred) assert sort == sortInferred
def testInfer2(): lib = mkDefaultLib() sort = mkFuncSort(mkRealTensorSort([1, 1]), mkBoolTensorSort([1, 1]), mkRealTensorSort([1, 1])) prog = PPTermUnk( name='nn_fun_x_906', sort=PPFuncSort(args=[ PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]), PPTensorSort(param_sort=PPBool(), shape=[PPDimConst(value=1), PPDimConst(value=1)]) ], rtpe=PPTensorSort( param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]))) sortInferred = inferType(prog, lib) print(repr_py_sort(sort)) print(sort) print(repr_py_sort(sortInferred)) print(sortInferred) assert sort == sortInferred
def testInfer8(): lib = mkDefaultLib() addRecogFive(lib) prog = PPVar(name='lib.map_l') sortInferred = inferType(prog, lib) print(repr_py_sort(sortInferred))
def func(term): nonlocal cid cid += 1 try: sort = inferType(term, lib) except ValueError as e: print('ValueError: %s' % e) print('prog: %s' % str(prog)) sort = None if sort is not None: idTermSorts.append((cid, term, sort)) return None
def testInfer3(): lib = mkDefaultLib() sort = mkRealTensorSort([1, 1]) prog = PPFuncApp(fn=PPVar(name='lib.zeros'), args=[PPIntConst(value=1)]) sortInferred = inferType(prog, lib) print(repr_py_sort(sort)) print(sort) print(repr_py_sort(sortInferred)) print(sortInferred) assert sort == sortInferred
def mutate_rec(term): nonlocal cid cid += 1 newTerm = None if cid < rid: if isinstance(term, PPFuncApp): childs = deconstructProg(term) childSorts = getChildSorts(term, lib) newChilds = [] for c, cs in zip(childs, childSorts): if isinstance(cs, PPDimVar): # Do not mutate DimVars that are direct function params. Example: zeros nc = c else: nc = mutate_rec(c) newChilds.append(nc) newTerm = constructProg(term, newChilds) else: childs = deconstructProg(term) newChilds = [mutate_rec(c) for c in childs] newTerm = constructProg(term, newChilds) elif cid == rid: try: tpe = inferType(term, lib) if tpe is not None: N = 10 for i in range(N): maxDepth = progDepth(term) + 2 newTerm = pg.genProg(tpe, maxDepth) if newTerm is not None: break # return same term if not able to generate a random term after N attempts. if newTerm is None: newTerm = term except ValueError as e: print('ValueError: %s' % e) print('term: %s' % str(term)) newTerm = term elif cid > rid: newTerm = term return newTerm
def testInfer9(): lib = mkDefaultLib() addRecogFive(lib) sort = mkFuncSort(mkListSort(mkRealTensorSort([1, 1, 28, 28])), mkListSort(mkBoolTensorSort([1, 1]))) prog = PPFuncApp( fn=PPVar(name='lib.map_l'), args=[ PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPTermUnk(name='Unk', sort=PPFuncSort(args=[PPSortVar(name='C')], rtpe=PPTensorSort( param_sort=PPBool(), shape=[ PPDimConst(value=1), PPDimConst(value=1) ]))), PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPTermUnk( name='Unk', sort=PPFuncSort(args=[ PPTensorSort(param_sort=PPBool(), shape=[ PPDimConst(value=1), PPDimConst(value=1) ]) ], rtpe=PPSortVar(name='C'))), PPVar(name='lib.recogFive') ]) ]) ]) sortInferred = inferType(prog, lib) assert sort == sortInferred
def testInfer13(): # lib.repeat(10, Unk) prog = PPFuncApp( fn=PPVar(name='lib.repeat'), args=[ PPIntConst(value=10), PPTermUnk( name='Unk', sort=PPFuncSort( args=[ PPListSort( param_sort=PPTensorSort(param_sort=PPReal(), shape=[ PPDimConst(value=1), PPDimConst(value=1), PPDimConst(value=28), PPDimConst(value=28) ])) ], rtpe=PPListSort( param_sort=PPTensorSort(param_sort=PPReal(), shape=[ PPDimConst(value=1), PPDimConst(value=1), PPDimConst(value=28), PPDimConst(value=28) ])))) ]) prog = PPFuncApp(fn=PPVar(name='lib.repeat'), args=[ PPIntConst(value=10), PPTermUnk(name='Unk', sort=PPFuncSort(args=[PPInt()], rtpe=PPInt())) ]) lib = mkDefaultLib() sortInferred = inferType(prog, lib) print(sortInferred)
def testInfer1(): lib = mkDefaultLib() # region #+ Add recogFive to library inType = mkRealTensorSort([1, 1, 28, 28]) outType = mkBoolTensorSort([1, 1]) recogDigitType = mkFuncSort(inType, outType) lib.addItem(PPLibItem('recogFive', recogDigitType, None)) # endregion inType = mkListSort(mkRealTensorSort([1, 1, 28, 28])) outType = mkRealTensorSort([1, 1]) sort = mkFuncSort(inType, outType) prog = \ PPFuncApp(fn=PPVar(name='lib.compose'), args=[PPFuncApp(fn=PPVar(name='lib.fold_l'), args=[PPTermUnk(name='nn_fun_x_906', sort=PPFuncSort( args=[PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]), PPTensorSort(param_sort=PPBool(), shape=[PPDimConst(value=1), PPDimConst(value=1)])], rtpe=PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]))), PPFuncApp(fn=PPVar(name='lib.zeros'), args=[PPIntConst(value=1)])]), PPFuncApp(fn=PPVar(name='lib.map_l'), args=[PPVar(name='lib.recogFive')])]) sortInferred = inferType(prog, lib) print(repr_py_sort(sort)) print(sort) print(repr_py_sort(sortInferred)) print(sortInferred) assert sort == sortInferred
def testInfer6(): lib = mkDefaultLib() inType = mkListSort(mkBoolTensorSort([1, 1])) outType = mkRealTensorSort([1, 1]) sort = mkFuncSort(inType, outType) prog = PPFuncApp(fn=PPVar(name='lib.fold_l'), args=[ PPTermUnk(name='nn_fun_x_906', sort=PPFuncSort(args=[ PPTensorSort(param_sort=PPReal(), shape=[ PPDimConst(value=1), PPDimConst(value=1) ]), PPTensorSort(param_sort=PPBool(), shape=[ PPDimConst(value=1), PPDimConst(value=1) ]) ], rtpe=PPTensorSort( param_sort=PPReal(), shape=[ PPDimConst(value=1), PPDimConst(value=1) ]))), PPFuncApp(fn=PPVar(name='lib.zeros'), args=[PPIntConst(value=1)]) ]) sortInferred = inferType(prog, lib) print(repr_py_sort(sort)) print(sort) print(repr_py_sort(sortInferred)) print(sortInferred) assert sort == sortInferred
def testMutate1(): lib = mkDefaultLib() addRecogFive(lib) inType = mkListSort(mkRealTensorSort([1, 1, 28, 28])) outType = mkRealTensorSort([1, 1]) sort = mkFuncSort(inType, outType) prog = \ PPFuncApp(fn=PPVar(name='lib.compose'), args=[PPFuncApp(fn=PPVar(name='lib.fold_l'), args=[PPTermUnk(name='nn_fun_x_906', sort=PPFuncSort( args=[PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]), PPTensorSort(param_sort=PPBool(), shape=[PPDimConst(value=1), PPDimConst(value=1)])], rtpe=PPTensorSort(param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)]))), PPFuncApp(fn=PPVar(name='lib.zeros'), args=[PPIntConst(value=1)])]), PPFuncApp(fn=PPVar(name='lib.map_l'), args=[PPVar(name='lib.recogFive')])]) for i in range(100): newProg = mutate(prog, lib) if newProg is None: continue # print(repr_py(newProg)) # print('newProg: ', newProg) # print('newProgReprPy: ', repr_py(newProg)) isort = inferType(newProg, lib) if isort is None: print(repr_py(newProg)) print('newProg: ', newProg) continue assert sort == isort
def testInfer12_1(): lib = None # lib.compose(lib.compose(lib.compose(Unk, lib.compose(Unk, lib.compose(lib.map_l, lib.map_l))), Unk), # lib.map_l(lib.recogFive)) prog1 = PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPTermUnk(name='Unk', sort=PPFuncSort( args=[PPSortVar(name='C')], rtpe=PPTensorSort( param_sort=PPReal(), shape=[ PPDimConst(value=1), PPDimConst(value=1) ]))), PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPTermUnk( name='Unk', sort=PPFuncSort(args=[ PPFuncSort( args=[ PPListSort( param_sort=PPListSort( param_sort= PPSortVar( name='A_1_2'))) ], rtpe=PPListSort( param_sort=PPListSort( param_sort=PPSortVar( name='B_1')))) ], rtpe=PPSortVar( name='C'))), PPFuncApp(fn=PPVar(name='lib.compose'), args=[ PPVar(name='lib.map_l'), PPVar(name='lib.map_l') ]) ]) ]), PPTermUnk( name='Unk', sort=PPFuncSort(args=[ PPListSort( param_sort=PPTensorSort(param_sort=PPBool(), shape=[ PPDimConst( value=1), PPDimConst(value=1) ])) ], rtpe=PPFuncSort( args=[PPSortVar(name='A_1_2')], rtpe=PPSortVar(name='B_1')))) ]), PPFuncApp(fn=PPVar(name='lib.map_l'), args=[PPVar(name='lib.recogFive')]) ]) prog1 = PPFuncApp(fn=PPVar(name='lib.map_l'), args=[PPVar(name='lib.recogFive')]) lib = mkDefaultLib() addRecogFive(lib) sortInferred = inferType(prog1, lib) print(sortInferred) sort = PPFuncSort(args=[ PPFuncSort(args=[ PPListSort(param_sort=PPListSort(param_sort=PPListSort( param_sort=PPSortVar(name='A_1')))) ], rtpe=PPListSort(param_sort=PPListSort(param_sort=PPListSort( param_sort=PPSortVar(name='B'))))) ], rtpe=PPTensorSort( param_sort=PPReal(), shape=[PPDimConst(value=1), PPDimConst(value=1)])) print(repr_py_sort(sort))
def testInfer11(): lib = mkDefaultLib() addRecogFive(lib) sort = mkFuncSort(mkListSort(mkRealTensorSort([1, 1, 28, 28])), mkListSort(mkBoolTensorSort([1, 1]))) prog = PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPTermUnk( name='Unk', sort=PPFuncSort( args=[PPSortVar(name='B_1')], rtpe=PPListSort( param_sort=PPTensorSort(param_sort=PPBool(), shape=[ PPDimConst( value=1), PPDimConst(value=1) ])))), PPTermUnk( name='Unk', sort=PPFuncSort(args=[ PPFuncSort(args=[ PPListSort(param_sort=PPSortVar(name='A')) ], rtpe=PPListSort(param_sort=PPSortVar( name='B_1_2'))) ], rtpe=PPSortVar(name='B_1'))) ]), PPFuncApp( fn=PPVar(name='lib.compose'), args=[ PPVar(name='lib.map_l'), PPTermUnk( name='Unk', sort=PPFuncSort(args=[ PPListSort( param_sort=PPTensorSort(param_sort=PPReal(), shape=[ PPDimConst( value=1), PPDimConst( value=1), PPDimConst( value=28), PPDimConst( value=28) ])) ], rtpe=PPFuncSort( args=[PPSortVar(name='A')], rtpe=PPSortVar(name='B_1_2')))) ]) ]) sortInferred = inferType(prog, lib) # prog is type checked but currently inferType only works for concrete types. assert sortInferred == sort