def test_computation_performance(thr_and_double, fast_math, test_sampler_float): thr, double = thr_and_double size = 2 ** 15 batch = 2 ** 6 bijection = philox(64, 4) sampler = test_sampler_float.get_sampler(bijection, double) rng = CBRNG(Type(sampler.dtype, shape=(batch, size)), 1, sampler) dest_dev = thr.empty_like(rng.parameter.randoms) counters = rng.create_counters() counters_dev = thr.to_device(counters) rngc = rng.compile(thr, fast_math=fast_math) attempts = 10 times = [] for i in range(attempts): t1 = time.time() rngc(counters_dev, dest_dev) thr.synchronize() times.append(time.time() - t1) byte_size = size * batch * sampler.dtype.itemsize return min(times), byte_size
def test_computation_performance(thr_and_double, fast_math, test_sampler_float): thr, double = thr_and_double size = 2**15 batch = 2**6 sampler = test_sampler_float.get_sampler(double) rng = CBRNG(Type(sampler.dtype, shape=(batch, size)), 1, sampler) dest_dev = thr.empty_like(rng.parameter.randoms) counters = rng.create_counters() counters_dev = thr.to_device(counters) rngc = rng.compile(thr, fast_math=fast_math) attempts = 10 times = [] for i in range(attempts): t1 = time.time() rngc(counters_dev, dest_dev) thr.synchronize() times.append(time.time() - t1) byte_size = size * batch * sampler.dtype.itemsize return min(times), byte_size
def test_computation_convenience(thr): size = 10000 batch = 101 ref = UniformIntegerHelper(0, 511) rng = CBRNG.uniform_integer(Type(numpy.int32, shape=(batch, size)), 1, sampler_kwds=dict(low=ref.extent[0], high=ref.extent[1])) check_computation(thr, rng, ref)
def test_computation_convenience(thr): size = 10000 batch = 101 extent = (0, 511) mean, std = uniform_discrete_mean_and_std(*extent) rng = CBRNG.uniform_integer(Type(numpy.int32, shape=(batch, size)), 1, sampler_kwds=dict(low=extent[0], high=extent[1] + 1)) check_computation(thr, rng, extent=extent, mean=mean, std=std)
def test_computation_general(thr_and_double): size = 10000 batch = 101 thr, double = thr_and_double dtype = numpy.float64 if double else numpy.float32 mean, std = -2, 10 bijection = philox(64, 4) sampler = normal_bm(bijection, dtype, mean=mean, std=std) rng = CBRNG(Type(dtype, shape=(batch, size)), 1, sampler) check_computation(thr, rng, mean=mean, std=std)
def test_computation_general(thr_and_double): size = 10000 batch = 101 thr, double = thr_and_double bijection = philox(64, 4) ref = NormalBMHelper(mean=-2, std=10) sampler = ref.get_sampler(bijection, double) rng = CBRNG(Type(sampler.dtype, shape=(batch, size)), 1, sampler) check_computation(thr, rng, ref)
def randint(self, array, minval, maxval, seed=None): kernel_cache, thread = self.kernel_cache, self.thread key = (self.randint, array.shape, array.dtype, minval, maxval, thread) if key not in kernel_cache.keys(): log.info("compiling " + str(key)) rng = CBRNG.uniform_integer(Type(array.dtype, shape=array.shape), len(array.shape), # @UndefinedVariable sampler_kwds=dict(low=numpy.int32(minval), high=numpy.int32(maxval)), seed=seed) counters = thread.to_device(rng.create_counters()) kernel_cache[key] = (rng.compile(thread), counters) (rng, counters) = kernel_cache[key] rng(counters, array)
def test_computation_uniqueness(thr): """ A regression test for the bug with a non-updating counter. """ size = 10000 batch = 1 rng = CBRNG.normal_bm(Type(numpy.complex64, shape=(batch, size)), 1) dest1_dev = thr.empty_like(rng.parameter.randoms) dest2_dev = thr.empty_like(rng.parameter.randoms) counters = rng.create_counters() counters_dev = thr.to_device(counters) rngc = rng.compile(thr) rngc(counters_dev, dest1_dev) rngc(counters_dev, dest2_dev) assert not diff_is_negligible(dest1_dev.get(), dest2_dev.get())
def test_computation_uniqueness(thr): """ A regression test for the bug with a non-updating counter. """ size = 10000 batch = 1 rng = CBRNG.normal_bm(Type(numpy.complex64, shape=(batch, size)), 1) dest1_dev = thr.empty_like(rng.parameter.randoms) dest2_dev = thr.empty_like(rng.parameter.randoms) counters = rng.create_counters() counters_dev = thr.to_device(counters) rngc = rng.compile(thr) rngc(counters_dev, dest1_dev) rngc(counters_dev, dest2_dev) assert not diff_is_negligible(dest1_dev.get(), dest2_dev.get(), verbose=False)
def normal(self, array, mean, std, seed=None): kernel_cache, thread = self.kernel_cache, self.thread key = (self.normal, array.shape, array.dtype, mean, std, thread) if key not in kernel_cache.keys(): log.info("compiling " + str(key)) rng = CBRNG.normal_bm(Type(array.dtype, shape=array.shape), len(array.shape), # @UndefinedVariable sampler_kwds=dict(mean=numpy.float32(mean), std=numpy.float32(std)), seed=seed) counters = thread.to_device(rng.create_counters()) kernel_cache[key] = (rng.compile(thread), counters) (rng, counters) = kernel_cache[key] rng(counters, array) return array
def randint(self, array, minval, maxval, seed=None): kernel_cache, thread = self.kernel_cache, self.thread key = (self.randint, array.shape, array.dtype, minval, maxval, thread) if key not in kernel_cache.keys(): log.info("compiling " + str(key)) rng = CBRNG.uniform_integer( Type(array.dtype, shape=array.shape), len(array.shape), # @UndefinedVariable sampler_kwds=dict(low=numpy.int32(minval), high=numpy.int32(maxval)), seed=seed) counters = thread.to_device(rng.create_counters()) kernel_cache[key] = (rng.compile(thread), counters) (rng, counters) = kernel_cache[key] rng(counters, array)
def normal(self, array, mean, std, seed=None): kernel_cache, thread = self.kernel_cache, self.thread key = (self.normal, array.shape, array.dtype, mean, std, thread) if key not in kernel_cache.keys(): log.info("compiling " + str(key)) rng = CBRNG.normal_bm( Type(array.dtype, shape=array.shape), len(array.shape), # @UndefinedVariable sampler_kwds=dict(mean=numpy.float32(mean), std=numpy.float32(std)), seed=seed) counters = thread.to_device(rng.create_counters()) kernel_cache[key] = (rng.compile(thread), counters) (rng, counters) = kernel_cache[key] rng(counters, array) return array