def test_advinc_subtensor1_dtype(): # Test the mixed dtype case shp = (3, 4) for dtype1, dtype2 in [ ("float32", "int8"), ("float32", "float64"), ("uint64", "int8"), ("int64", "uint8"), ("float16", "int8"), ("float16", "float64"), ("float16", "float16"), ]: shared = gpuarray_shared_constructor xval = np.arange(np.prod(shp), dtype=dtype1).reshape(shp) + 1 yval = np.empty((2,) + shp[1:], dtype=dtype2) yval[:] = 10 x = shared(xval, name="x") y = tensor(dtype=yval.dtype, broadcastable=(False,) * len(yval.shape), name="y") expr = advanced_inc_subtensor1(x, y, [0, 2]) f = aesara.function([y], expr, mode=mode_with_gpu) assert ( sum( [ isinstance(node.op, GpuAdvancedIncSubtensor1_dev20) for node in f.maker.fgraph.toposort() ] ) == 1 ) rval = f(yval) rep = xval.copy() np.add.at(rep, [[0, 2]], yval) assert np.allclose(rval, rep)
def bincount(x, weights=None, minlength=None, assert_nonneg=False): """Count number of occurrences of each value in array of ints. The number of bins (of size 1) is one larger than the largest value in x. If minlength is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of x). Each bin gives the number of occurrences of its index value in x. If weights is specified the input array is weighted by it, i.e. if a value n is found at position i, out[n] += weight[i] instead of out[n] += 1. Parameters ---------- x : 1 dimension, nonnegative ints weights : array of the same shape as x with corresponding weights. Optional. minlength : A minimum number of bins for the output array. Optional. assert_nonneg : A flag that inserts an assert_op to check if every input x is nonnegative. Optional. .. versionadded:: 0.6 """ if x.ndim != 1: raise TypeError("Inputs must be of dimension 1.") if assert_nonneg: assert_op = Assert("Input to bincount has negative values!") x = assert_op(x, aet_all(x >= 0)) max_value = aet.cast(x.max() + 1, "int64") if minlength is not None: max_value = maximum(max_value, minlength) # Note: we do not use inc_subtensor(out[x], ...) in the following lines, # since out[x] raises an exception if the indices (x) are int8. if weights is None: out = aet.zeros([max_value], dtype=x.dtype) out = advanced_inc_subtensor1(out, 1, x) else: out = aet.zeros([max_value], dtype=weights.dtype) out = advanced_inc_subtensor1(out, weights, x) return out
def test_advinc_subtensor1(): # Test the second case in the opt local_gpu_advanced_incsubtensor1 for shp in [(3, 3), (3, 3, 3)]: shared = gpuarray_shared_constructor xval = np.arange(np.prod(shp), dtype="float32").reshape(shp) + 1 yval = np.empty((2,) + shp[1:], dtype="float32") yval[:] = 10 x = shared(xval, name="x") y = tensor(dtype="float32", broadcastable=(False,) * len(shp), name="y") expr = advanced_inc_subtensor1(x, y, [0, 2]) f = aesara.function([y], expr, mode=mode_with_gpu) assert ( sum( [ isinstance(node.op, GpuAdvancedIncSubtensor1) for node in f.maker.fgraph.toposort() ] ) == 1 ) rval = f(yval) rep = xval.copy() np.add.at(rep, [0, 2], yval) assert np.allclose(rval, rep)
def test_incsub_f16(): shp = (3, 3) shared = gpuarray_shared_constructor xval = np.arange(np.prod(shp), dtype="float16").reshape(shp) + 1 yval = np.empty((2,) + shp[1:], dtype="float16") yval[:] = 2 x = shared(xval, name="x") y = tensor(dtype="float16", broadcastable=(False,) * len(shp), name="y") expr = advanced_inc_subtensor1(x, y, [0, 2]) f = aesara.function([y], expr, mode=mode_with_gpu) assert ( sum( [ isinstance(node.op, GpuAdvancedIncSubtensor1) for node in f.maker.fgraph.toposort() ] ) == 1 ) rval = f(yval) rep = xval.copy() np.add.at(rep, [[0, 2]], yval) assert np.allclose(rval, rep) expr = inc_subtensor(x[1:], y) f = aesara.function([y], expr, mode=mode_with_gpu) assert ( sum( [isinstance(node.op, GpuIncSubtensor) for node in f.maker.fgraph.toposort()] ) == 1 ) rval = f(yval) rep = xval.copy() rep[1:] += yval assert np.allclose(rval, rep)
def test_advinc_subtensor1_vector_scalar(): # Test the case where x is a vector and y a scalar shp = (3,) for dtype1, dtype2 in [ ("float32", "int8"), ("float32", "float64"), ("float16", "int8"), ("float16", "float64"), ("float16", "float16"), ("int8", "int8"), ("int16", "int16"), ]: shared = gpuarray_shared_constructor xval = np.arange(np.prod(shp), dtype=dtype1).reshape(shp) + 1 yval = np.asarray(10, dtype=dtype2) x = shared(xval, name="x") y = tensor(dtype=yval.dtype, broadcastable=(False,) * len(yval.shape), name="y") expr = advanced_inc_subtensor1(x, y, [0, 2]) f = aesara.function([y], expr, mode=mode_with_gpu) assert ( sum( [ isinstance( node.op, (GpuAdvancedIncSubtensor1_dev20, GpuAdvancedIncSubtensor1), ) for node in f.maker.fgraph.toposort() ] ) == 1 ) rval = f(yval) rep = xval.copy() rep[[0, 2]] += yval assert np.allclose(rval, rep)