def test_repl_realizeInval(): with setup_storage('test_repl_realizeInval'): try: repl_realize(instantiate(mkstage, {'a': 1}), force_interrupt=33) #type:ignore raise ShouldHaveFailed('wrong force_interrupt value') except AssertionError: pass
def test_repl_override(): with setup_storage('test_repl_override'): n1: DRef n2: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2 n1 = mkstage(m, {'a': '1'}, lambda i, tag: 33) n2 = mkstage(m, {'maman': n1}, lambda i, tag: 42) return n2 clo = instantiate(_setting) rh = repl_realize(clo, force_interrupt=[n1]) assert rh.dref == n1 b = repl_build(rh) with open(join(build_outpath(b), 'artifact'), 'w') as f: f.write('777') repl_continue(b.outgroups, rh=rh) rref = repl_rref(rh) assert rref is not None rrefn1 = store_deref(rref, n1)[Tag('out')] assert tryread(Path(join(store_rref2path(rrefn1), 'artifact'))) == '777'
def test_repl_basic(): with setup_storage('test_repl_default'): n1: DRef n2: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2 n1 = mkstage(m, {'a': '1'}) n2 = mkstage(m, {'maman': n1}) return n2 clo = instantiate(_setting) rh = repl_realize(clo, force_interrupt=False) assert repl_rref(rh) is not None rh = repl_realize(clo, force_interrupt=[n1, n2]) assert repl_rref(rh) is None assert rh.dref == n1 repl_continue(rh=rh) assert repl_rref(rh) is None assert rh.dref == n2 repl_continue(rh=rh) assert repl_rref(rh) is not None
def test_shellref(): with setup_storage('test_shellref') as s: with TemporaryDirectory() as tmp: mockshell=join(tmp,'mockshell') with open(mockshell,'w') as f: f.write(f"#!/bin/sh\n") f.write(f"pwd\n") chmod(mockshell, stat(mockshell).st_mode | S_IEXEC) environ['SHELL']=mockshell rref=realize(instantiate(mkstage, {'a':1})) shellref(rref) shellref(rref2dref(rref)) shellref() shell(store_rref2path(rref)) repl_realize(instantiate(mkstage, {'n':1}), force_interrupt=True) b=repl_build() o=build_outpath(b) shell(b) repl_cancelBuild(b) try: shellref('foo') # type:ignore raise ShouldHaveFailed('shellref should reject garbage') except AssertionError: pass
def test_repl_race(): with setup_storage('test_repl_recursion'): def _setting(m: Manager) -> DRef: n1 = mkstage(m, {'a': '1'}) n2 = mkstage(m, {'maman': n1}) return n2 clo = instantiate(_setting) rh = repl_realize(clo, force_interrupt=True) assert repl_rref(rh) is None assert rh.dref == clo.dref clo2 = instantiate(_setting) rref2 = realize(clo2) # Realize dref while repl_realizing same dref repl_cancel(rh) assert_valid_rref(rref2)
def test_repl_globalCancel(): with setup_storage('test_repl_globalCancel'): n1: DRef n2: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2 n1 = mkstage(m, {'a': '1'}) n2 = mkstage(m, {'maman': n1}) return n2 rh = repl_realize(instantiate(_setting), force_interrupt=True) assert repl_rref(rh) is None repl_cancel() assert rh.gen is None rref = realize(instantiate(_setting)) assert isrref(rref)
def test_repl_globalHelper(): with setup_storage('test_repl_globalHelper'): n1: DRef n2: DRef def _setting(m: Manager) -> DRef: nonlocal n1, n2 n1 = mkstage(m, {'a': '1'}) n2 = mkstage(m, {'maman': n1}) return n2 rh = repl_realize(instantiate(_setting), force_interrupt=True) assert repl_rref(rh) is None b = repl_build() with open(join(build_outpath(b), 'artifact.txt'), 'w') as f: f.write("Fooo") repl_continueBuild(b) rref = repl_rref(rh) assert rref is not None assert isfile(join(store_rref2path(rref), 'artifact.txt'))
def mnist_realize(b:Model): mnist_train(b) mnist_eval(b) def convnn_mnist(m:Manager)->DRef: mnist = fetchmnist(m) return mkdrv(m, mnist_config(mnist), match_best('accuracy.txt'), build_wrapper_(mnist_realize, Model)) realize(instantiate(convnn_mnist), force_rebuild=True) # Spoiler: will fail def mnist_eval_correct(b:Model): o = build_outpath(b) b.model.load_weights(join(o, "checkpoint.ckpt")) accuracy = b.model.evaluate(b.x_test, b.y_test, verbose = 0)[-1] print(accuracy) with open(join(o,'accuracy.txt'),'w') as f: f.write(str(accuracy)) from pylightnix import repl_realize, repl_buildargs, repl_continueBuild repl_realize(instantiate(convnn_mnist)) b=Model(repl_buildargs()) mnist_train(b) mnist_eval_correct(b) rref=repl_continueBuild(b) assert rref is not None print(rref)