def test_gammaincc_nan(): x1 = aet.dscalar() x2 = aet.dscalar() y = gammaincc(x1, x2) test_func = CLinker().accept(FunctionGraph([x1, x2], [y])).make_function() assert np.isnan(test_func(-1, 1)) assert np.isnan(test_func(1, -1)) assert np.isnan(test_func(-1, -1))
def test_shared_input_output(): # Test bug reported on the mailing list by Alberto Orlandi # https://groups.google.com/d/topic/theano-users/6dLaEqc2R6g/discussion # The shared variable is both an input and an output of the function. inc = iscalar("inc") state = shared(0) state.name = "state" linker = CLinker() mode = Mode(linker=linker) f = function([inc], state, updates=[(state, state + inc)], mode=mode) g = function([inc], state, updates=[(state, state + inc)]) # Initial value f0 = f(0) g0 = g(0) assert f0 == g0 == 0, (f0, g0) # Increment state via f, returns the previous value. f2 = f(2) assert f2 == f0, (f2, f0) f0 = f(0) g0 = g(0) assert f0 == g0 == 2, (f0, g0) # Increment state via g, returns the previous value g3 = g(3) assert g3 == g0, (g3, g0) f0 = f(0) g0 = g(0) assert f0 == g0 == 5, (f0, g0) vstate = shared(np.zeros(3, dtype="int32")) vstate.name = "vstate" fv = function([inc], vstate, updates=[(vstate, vstate + inc)], mode=mode) gv = function([inc], vstate, updates=[(vstate, vstate + inc)]) # Initial value fv0 = fv(0) gv0 = gv(0) assert np.all(fv0 == 0), fv0 assert np.all(gv0 == 0), gv0 # Increment state via f, returns the previous value. fv2 = fv(2) assert np.all(fv2 == fv0), (fv2, fv0) fv0 = fv(0) gv0 = gv(0) assert np.all(fv0 == 2), fv0 assert np.all(gv0 == 2), gv0 # Increment state via g, returns the previous value gv3 = gv(3) assert np.all(gv3 == gv0), (gv3, gv0) fv0 = fv(0) gv0 = gv(0) assert np.all(fv0 == 5), fv0 assert np.all(gv0 == 5), gv0
def test_c_views(self): x_at = vector() thunk, inputs, outputs = (CLinker().accept( FunctionGraph([x_at], [x_at[None]])).make_thunk()) # This is a little hackish, but we're hoping that--by running this more than # a few times--we're more likely to run into random memory that isn't the same # as the broadcasted value; that way, we'll be able to tell that we're getting # junk data from a poorly constructed array view. x_val = np.broadcast_to(2039, (5000, )) for i in range(1000): inputs[0].storage[0] = x_val thunk() # Make sure it's a view of the original data assert np.shares_memory(x_val, outputs[0].storage[0]) # Confirm the broadcasted value in the output assert np.array_equiv(outputs[0].storage[0], 2039)
def test_duallinker_mismatch(): x, y, z = inputs() # bad_sub is correct in C but erroneous in Python e = bad_sub(mul(x, y), mul(y, z)) g = FunctionGraph([x, y, z], [e]) lnk = DualLinker(checker=_my_checker).accept(g) fn = make_function(lnk) # good assert make_function(CLinker().accept(g))(1.0, 2.0, 3.0) == -4.0 # good assert make_function(OpWiseCLinker().accept(g))(1.0, 2.0, 3.0) == -4.0 # (purposely) wrong assert make_function(PerformLinker().accept(g))(1.0, 2.0, 3.0) == -10.0 with pytest.raises(MyExc): # this runs OpWiseCLinker and PerformLinker in parallel and feeds # variables of matching operations to _my_checker to verify that they # are the same. fn(1.0, 2.0, 3.0)
NavigatorOptimizer, ) from aesara.graph.optdb import EquilibriumDB, LocalGroupDB, Query, SequenceDB, TopoDB from aesara.link.basic import PerformLinker from aesara.link.c.basic import CLinker, OpWiseCLinker from aesara.link.jax import JAXLinker from aesara.link.vm import VMLinker _logger = logging.getLogger("aesara.compile.mode") # If a string is passed as the linker argument in the constructor for # Mode, it will be used as the key to retrieve the real linker in this # dictionary predefined_linkers = { "py": PerformLinker(), # Use allow_gc Aesara flag "c": CLinker(), # Don't support gc. so don't check allow_gc "c|py": OpWiseCLinker(), # Use allow_gc Aesara flag "c|py_nogc": OpWiseCLinker(allow_gc=False), "vm": VMLinker(use_cloop=False), # Use allow_gc Aesara flag "cvm": VMLinker(use_cloop=True), # Use allow_gc Aesara flag "vm_nogc": VMLinker(allow_gc=False, use_cloop=False), "cvm_nogc": VMLinker(allow_gc=False, use_cloop=True), "jax": JAXLinker(), } def register_linker(name, linker): """Add a `Linker` which can be referred to by `name` in `Mode`.""" if name in predefined_linkers: raise ValueError(f"Linker name already taken: {name}") predefined_linkers[name] = linker
def test_clinker_single_node(): x, y, z = inputs() node = add.make_node(x, y) lnk = CLinker().accept(FunctionGraph(node.inputs, node.outputs)) fn = make_function(lnk) assert fn(2.0, 7.0) == 9
def test_clinker_straightforward(): x, y, z = inputs() e = add(mul(add(x, y), div(x, y)), bad_sub(bad_sub(x, y), z)) lnk = CLinker().accept(FunctionGraph([x, y, z], [e])) fn = make_function(lnk) assert fn(2.0, 2.0, 2.0) == 2.0
def test_SymPyCCode(): op = SymPyCCode([xs, ys], xs + ys) e = op(xt, yt) g = FunctionGraph([xt, yt], [e]) fn = make_function(CLinker().accept(g)) assert fn(1.0, 2.0) == 3.0
def test_c_inplace(self): self.with_linker_inplace(CLinker(), self.cop, self.ctype, self.rand_cval)
def test_c(self): self.with_linker(CLinker(), self.cop, self.ctype, self.rand_cval)