示例#1
0
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
示例#2
0
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
示例#3
0
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)
示例#4
0
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)
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
0
 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)
示例#9
0
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())
示例#10
0
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)
示例#11
0
 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
示例#12
0
    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)
示例#13
0
    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