def test_constant(self): x = Constant(MyType(), 2, name="x") y = MyVariable("y") z = Constant(MyType(), 2, name="z") e = op1(op1(x, y), y) g = FunctionGraph([y], [e]) PatternOptimizer((op1, z, "1"), (op2, "1", z)).optimize(g) assert str(g) == "FunctionGraph(Op1(Op2(y, z), y))"
def __init__(self, type, data, name=None): Constant.__init__(self, type, data, name) self.tag.unique_value = None if isinstance(data, np.ndarray) and data.ndim > 0: flat_data = data.ravel() if flat_data.shape[0]: if (flat_data == flat_data[0]).all(): self.tag.unique_value = flat_data[0]
def test_constant_merging(self): x = MyVariable("x") y = Constant(MyType(), 2, name="y") z = Constant(MyType(), 2, name="z") e = op1(op2(x, y), op2(x, y), op2(x, z)) g = FunctionGraph([x, y, z], [e]) MergeOptimizer().optimize(g) strg = str(g) assert (strg == "FunctionGraph(Op1(*1 -> Op2(x, y), *1, *1))" or strg == "FunctionGraph(Op1(*1 -> Op2(x, z), *1, *1))")
def test_identical_constant_args(self): x = MyVariable("x") y = Constant(MyType(), 2, name="y") z = Constant(MyType(), 2, name="z") with config.change_flags(compute_test_value="off"): e1 = op1(y, z) g = FunctionGraph([x, y, z], [e1]) MergeOptimizer().optimize(g) strg = str(g) assert strg == "FunctionGraph(Op1(y, y))" or strg == "FunctionGraph(Op1(z, z))"
def test_constant_merging(self): x = MyVariable("x") y = Constant(MyType(), 2, name="y") z = Constant(MyType(), 2, name="z") e = op1(op2(x, y), op2(x, y), op2(x, z)) g = FunctionGraph([x, y, z], [e], clone=False) MergeOptimizer().optimize(g) out_var = g.outputs[0] var_1, var_2, var_3 = out_var.owner.inputs assert var_1 is var_2 assert var_2 is var_3
def test_identical_constant_args(self): x = MyVariable("x") y = Constant(MyType(), 2, name="y") z = Constant(MyType(), 2, name="z") e1 = op1(y, z) g = FunctionGraph([x, y, z], [e1], clone=False) MergeOptimizer().optimize(g) assert g.outputs[0].owner.op == op1 input_1 = g.outputs[0].owner.inputs[0] assert input_1 is g.outputs[0].owner.inputs[1]
def __init__(self, type, data, name=None): assert isinstance(data, slice) # Numpy ndarray aren't hashable, so get rid of them. if isinstance(data.start, np.ndarray): assert data.start.ndim == 0 assert str(data.start.dtype) in integer_dtypes data = slice(int(data.start), data.stop, data.step) elif isinstance(data.stop, np.ndarray): assert data.stop.ndim == 0 assert str(data.stop.dtype) in integer_dtypes data = slice(data.start, int(data.stop), data.step) elif isinstance(data.step, np.ndarray): assert data.step.ndim == 0 assert str(data.step.dtype) in integer_dtypes data = slice(data.start, int(data.stop), data.step) Constant.__init__(self, type, data, name)
def __init__(self, type, data, name=None): data_shape = np.shape(data) if len(data_shape) != type.ndim or any( ds != ts for ds, ts in zip(np.shape(data), type.shape) if ts is not None): raise ValueError( f"Shape of data ({data_shape}) does not match shape of type ({type.shape})" ) # We want all the shape information from `data` new_type = type.clone(shape=data_shape) assert not any(s is None for s in new_type.shape) Constant.__init__(self, new_type, data, name)
def test_opwiseclinker_constant(): x, y, z = inputs() x = Constant(tdouble, 7.2, name="x") e = add(mul(x, y), mul(y, z)) lnk = OpWiseCLinker().accept(FunctionGraph([y, z], [e])) fn = make_function(lnk) res = fn(1.5, 3.0) assert res == 15.3
def test_c_fail_error(): x, y, z = inputs() x = Constant(tdouble, 7.2, name="x") e = add_fail(mul(x, y), mul(y, z)) lnk = OpWiseCLinker().accept(FunctionGraph([y, z], [e])) fn = make_function(lnk) with pytest.raises(RuntimeError): fn(1.5, 3.0)
def test_none_Constant(): # FIXME: This is a poor test. # Tests equals # We had an error in the past with unpickling o1 = Constant(NoneTypeT(), None, name="NoneConst") o2 = Constant(NoneTypeT(), None, name="NoneConst") assert o1.equals(o2) assert NoneConst.equals(o1) assert o1.equals(NoneConst) assert NoneConst.equals(o2) assert o2.equals(NoneConst) # This trigger equals that returned the wrong answer in the past. import pickle import aesara x = vector("x") y = argmax(x) kwargs = {} # We can't pickle DebugMode if aesara.config.mode in ["DebugMode", "DEBUG_MODE"]: kwargs = {"mode": "FAST_RUN"} f = aesara.function([x], [y], **kwargs) pickle.loads(pickle.dumps(f))
def test_pre_greedy_local_optimizer(): empty_fgraph = FunctionGraph([], []) x = MyVariable("x") y = MyVariable("y") c1 = Constant(MyType(), 1, "c1") c2 = Constant(MyType(), 2, "c2") o1 = op2(c1, c2) o3 = op1(c1, y) o2 = op1(o1, c2, x, o3, o1) assert o2.owner.inputs[0].owner is not None assert o2.owner.inputs[4].owner is not None # This should fold `o1`, because it has only `Constant` arguments, and # replace it with the `Constant` result cst = pre_greedy_local_optimizer(empty_fgraph, [constant_folding], o2) assert cst.owner.inputs[0].owner is None assert cst.owner.inputs[1] is c2 assert cst.owner.inputs[2] is x assert cst.owner.inputs[3] is o3 assert cst.owner.inputs[4] is cst.owner.inputs[0] # We're going to do it again, except this time `o1` is # in the `fgraph`, so it shouldn't be folded fg = FunctionGraph([], [o1], clone=False) o2 = op1(o1, c2, x, o3, o1) cst = pre_greedy_local_optimizer(fg, [constant_folding], o2) assert cst.owner.inputs[0] is o1 assert cst.owner.inputs[4] is cst.owner.inputs[0] # What exactly is this supposed to test? ms = MakeSlice()(1) cst = pre_greedy_local_optimizer(empty_fgraph, [constant_folding], ms) assert isinstance(cst, SliceConstant) # Make sure constant of slice signature is hashable. assert isinstance(hash(cst.signature()), int)
def test_pre_constant_merge(): empty_fgraph = FunctionGraph([], []) x = MyVariable("x") y = MyVariable("y") c1 = Constant(MyType(), 1, "c1") c2 = Constant(MyType(), 1, "c1") o1 = op2(c1, x) o2 = op1(o1, y, c2) assert c1 is not c2 res = pre_constant_merge(empty_fgraph, [o2]) assert [o2] == res assert o2.owner.inputs[2] is c1 o2 = op1(o1, y, c2) fg = FunctionGraph([x, y], [o2], clone=False) assert o2.owner in fg.apply_nodes res = pre_constant_merge(fg, [o2]) assert res == [o2] assert o2.owner.inputs[2] is c2 # What is this supposed to test? ms = MakeSlice()(1) res = pre_constant_merge(empty_fgraph, [ms]) assert res == [ms] const_slice = SliceConstant(type=slicetype, data=slice(1, None, 2)) assert isinstance(const_slice, Constant) adv = AdvancedSubtensor()(matrix(), [2, 3], const_slice) res = pre_constant_merge(empty_fgraph, adv) assert res == [adv]
def test_clinker_literal_inlining(): x, y, z = inputs() z = Constant(tdouble, 4.12345678) e = add(mul(add(x, y), div(x, y)), bad_sub(bad_sub(x, y), z)) lnk = CLinker().accept(FunctionGraph([x, y], [e])) fn = make_function(lnk) assert abs(fn(2.0, 2.0) + 0.12345678) < 1e-9 code = lnk.code_gen() # print "=== Code generated ===" # print code assert "4.12345678" in code # we expect the number to be inlined
def make_node(self, x, index): assert isinstance(x.type, TypedListType) if not isinstance(index, Variable): if isinstance(index, slice): index = Constant(SliceType(), index) return Apply(self, [x, index], [x.type()]) else: index = at.constant(index, ndim=0, dtype="int64") return Apply(self, [x, index], [x.ttype()]) if isinstance(index.type, SliceType): return Apply(self, [x, index], [x.type()]) elif isinstance(index, TensorVariable) and index.ndim == 0: assert index.dtype == "int64" return Apply(self, [x, index], [x.ttype()]) else: raise TypeError("Expected scalar or slice as index.")
def test_MatrixSlice(): from aesara.graph.basic import Constant cache = {} n = sy.Symbol('n', integer=True) X = sy.MatrixSymbol('X', n, n) Y = X[1:2:3, 4:5:6] Yt = aesara_code_(Y, cache=cache) s = Scalar('int64') assert tuple(Yt.owner.op.idx_list) == (slice(s, s, s), slice(s, s, s)) assert Yt.owner.inputs[0] == aesara_code_(X, cache=cache) # == doesn't work in Aesara like it does in SymPy. You have to use # equals. assert all(Yt.owner.inputs[i].equals(Constant(s, i)) for i in range(1, 7)) k = sy.Symbol('k') aesara_code_(k, dtypes={k: 'int32'}) start, stop, step = 4, k, 2 Y = X[start:stop:step] Yt = aesara_code_(Y, dtypes={n: 'int32', k: 'int32'})
def __init__(self, type, data, name=None): Constant.__init__(self, type, data, name)
def MyConstant(data): return Constant(MyType(), data=data)
SliceType.Constant = SliceConstant class NoneTypeT(Generic): """ Inherit from Generic to have c code working. """ def filter(self, x, strict=False, allow_downcast=None): if x is None: return x else: raise TypeError("Expected None!") @staticmethod def may_share_memory(a, b): # None never share memory between object, in the sence of DebugMode. # Python None are singleton return False none_type_t = NoneTypeT() # This is a variable instance. It can be used only once per fgraph. # So use NoneConst.clone() before using it in an Aesara graph. # Use NoneConst.equals(x) to check if two variable are NoneConst. NoneConst = Constant(none_type_t, None, name="NoneConst") __all__ = ["make_slice", "slicetype", "none_type_t", "NoneConst"]
def test_constant(self): x, y, z = inputs() y = Constant(tdouble, 2.0) e = mul(add(x, y), div(x, y)) fn = perform_linker(FunctionGraph([x], [e])).make_function() assert fn(1.0) == 1.5
def make_node(self, path): if isinstance(path, str): path = Constant(Generic(), path) return Apply(self, [path], [tensor(self.dtype, shape=self.broadcastable)])
def MyConstant(name, data=None): return Constant(MyType(), data, name=name)