def initialize(self, device, **kwargs): super(StochasticPoolingBase, self).initialize(device=device, **kwargs) if self.uniform is None: self.uniform = Uniform(self) if self.uniform.output_bytes < (self.output_size << 1): if self.uniform.is_initialized: raise ValueError( "uniform is already initialized and does not have enough " "output size") self.uniform.output_bytes = self.output_size << 1 self.uniform.initialize(self.device)
class StochasticPoolingBase(OffsetPooling): """Stochastic pooling forward propagation. Attributes: uniform: instance of veles.prng.Uniform. """ MAPPING = set() hide_from_registry = True def __init__(self, workflow, **kwargs): super(StochasticPoolingBase, self).__init__(workflow, **kwargs) self.uniform = kwargs.get("uniform") def init_unpickled(self): super(StochasticPoolingBase, self).init_unpickled() self._rand_set = False self._rand_arg = 3 self._kernel_name = "stochastic_pooling" def initialize(self, device, **kwargs): super(StochasticPoolingBase, self).initialize(device=device, **kwargs) if self.uniform is None: self.uniform = Uniform(self) if self.uniform.output_bytes < (self.output_size << 1): if self.uniform.is_initialized: raise ValueError( "uniform is already initialized and does not have enough " "output size") self.uniform.output_bytes = self.output_size << 1 self.uniform.initialize(self.device) def ocl_init(self): super(StochasticPoolingBase, self).ocl_init() self.assign_kernel(self._kernel_name) self.set_args() def cuda_init(self): super(StochasticPoolingBase, self).cuda_init() self.assign_kernel(self._kernel_name) self.set_args() def add_ref(self, unit): pass def numpy_run(self): self.uniform.numpy_fill(self.output_size << 1) super(StochasticPoolingBase, self).numpy_run() def ocl_run(self): if not self._rand_set: self.set_arg(self._rand_arg, self.uniform.output) self._rand_set = True self.uniform.ocl_fill(self.output_size << 1) super(StochasticPoolingBase, self).ocl_run() def cuda_run(self): if not self._rand_set: self.set_arg(self._rand_arg, self.uniform.output) self._rand_set = True self.uniform.cuda_fill(self.output_size << 1) super(StochasticPoolingBase, self).cuda_run() def calculate_position_cpu(self, index, vsum): rnd = self.uniform.output.mem.view(dtype=numpy.uint16)[index] return rnd * vsum / 65536 def calculate_random_index_cpu(self, cut, index): rnd = self.uniform.output.mem.view(dtype=numpy.uint16)[index] return int(rnd * cut.size >> 16)