def test_allow_input_downcast_int(self): a = wvector("a") # int16 b = bvector("b") # int8 c = bscalar("c") # int8 f = pfunc([a, b, c], (a + b + c), allow_input_downcast=True) # Value too big for a, b, or c, silently ignored assert f([2 ** 20], [1], 0) == 1 assert f([3], [312], 0) == 59 assert f([3], [1], 806) == 42 g = pfunc([a, b, c], (a + b + c), allow_input_downcast=False) # All values are in range. Since they're not ndarrays (but lists # or scalars), they will be converted, and their value checked. assert np.all(g([3], [6], 0) == 9) # Values are in range, but a dtype too large has explicitly been given # For performance reasons, no check of the data is explicitly performed # (It might be OK to change this in the future.) with pytest.raises(TypeError): g([3], np.array([6], dtype="int16"), 0) # Value too big for b, raises TypeError with pytest.raises(TypeError): g([3], [312], 0) h = pfunc([a, b, c], (a + b + c)) # Default: allow_input_downcast=None # Everything here should behave like with False assert np.all(h([3], [6], 0) == 9) with pytest.raises(TypeError): h([3], np.array([6], dtype="int16"), 0) with pytest.raises(TypeError): h([3], [312], 0)
def test_cast_float16(self): f16 = vector(dtype="float16") f32 = fvector() i8 = bvector() f = aesara.function( [f16, f32, i8], [ f16.astype("float32"), f32.astype("float16"), f32.astype("float64"), f16.astype("int8"), f32.astype("int8"), i8.astype("float16"), i8.astype("float32"), ], mode=mode_with_gpu, ) d1 = (np.random.rand(4) * 10).astype("float16") d2 = (np.random.rand(5) * 10).astype("float32") d3 = (np.random.rand(6) * 10).astype("int8") res = f(d1, d2, d3) for i, out in enumerate(f.outputs): dtype = out.variable.dtype assert res[i].dtype == dtype inp = out.variable.owner.inputs[0] if inp.dtype == "float16": d = d1 elif inp.dtype == "float32": d = d2 else: d = d3 assert_allclose(d.astype(dtype), res[i])
def test_param_allow_downcast_int(self): a = wvector("a") # int16 b = bvector("b") # int8 c = bscalar("c") # int8 f = pfunc( [ In(a, allow_downcast=True), In(b, allow_downcast=False), In(c, allow_downcast=None), ], (a + b + c), ) # Both values are in range. Since they're not ndarrays (but lists), # they will be converted, and their value checked. assert np.all(f([3], [6], 1) == 10) # Values are in range, but a dtype too large has explicitly been given # For performance reasons, no check of the data is explicitly performed # (It might be OK to change this in the future.) with pytest.raises(TypeError): f([3], np.array([6], dtype="int16"), 1) # Value too big for a, silently ignored assert np.all(f([2 ** 20], np.ones(1, dtype="int8"), 1) == 2) # Value too big for b, raises TypeError with pytest.raises(TypeError): f([3], [312], 1) # Value too big for c, raises TypeError with pytest.raises(TypeError): f([3], [6], 806)
def test_composite_elemwise_float16(self): w = bvector() x = vector(dtype="float16") y = fvector() cz = tanh(x + aet.cast(y, "float16")) o = ( cz - cz ** 2 + aet.cast(x, "int16") + aet.cast(x, "float32") + aet.cast(w, "float16") - aet.constant(np.float16(1.0)) ) aesara.function([w, x, y], o, mode=mode_with_gpu) v = vector(dtype="uint8") w = vector(dtype="float16") x = vector(dtype="float16") y = vector(dtype="float16") z = vector(dtype="float16") o = aet.switch(v, mul(w, x, y), z) aesara.function([v, w, x, y, z], o, mode=mode_with_gpu)