我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用chainer.cuda.get_device_from_array()。
def update(self, s, i): """Update decoder state Args: s (any): Current (hidden, cell) states. If ``None`` is specified zero-vector is used. i (int): input label. Return: (~chainer.Variable) updated decoder state """ if cuda.get_device_from_array(s[0].data).id >= 0: xp = cuda.cupy else: xp = np v = chainer.Variable(xp.array([i],dtype=np.int32)) x = self.embed(v) if s is not None: hy, cy, dy = self.lstm(s[0], s[1], [x]) else: hy, cy, dy = self.lstm(None, None, [x]) return hy, cy, dy
def init_state(self, param): xp = cuda.get_array_module(param.data) with cuda.get_device_from_array(param.data): self.state['ms'] = xp.zeros_like(param.data)
def init_state(self, param): with cuda.get_device_from_array(param.data): self.state['s'] = []
def _sum_sqnorm(arr): sq_sum = collections.defaultdict(float) for x in arr: with cuda.get_device_from_array(x) as dev: x = x.ravel() s = x.dot(x) sq_sum[int(dev)] += s return sum([float(i) for i in six.itervalues(sq_sum)])
def __call__(self, opt): norm = np.sqrt(_sum_sqnorm([p.grad for p in opt.target.params(False)])) if norm == 0: return rate = self.threshold / norm if rate < 1: for param in opt.target.params(False): grad = param.grad with cuda.get_device_from_array(grad): grad *= rate
def __call__(self, rule, param): g = param.grad with cuda.get_device_from_array(g): g *= self.rate
def backward(self, inputs, grad_outputs): assert self.comm.size == len(grad_outputs) xp = cuda.get_array_module(*inputs) with cuda.get_device_from_array(*inputs): gys = tuple([gy for gy in grad_outputs]) gx = self.comm.alltoall(gys) gx = [xp.array(_gx) for _gx in gx] return tuple(gx)
def backward(self, inputs, grad_outputs): xp = cuda.get_array_module(*inputs) with cuda.get_device_from_array(*inputs): grad = self.comm.recv(self.peer_rank, self.peer_tag) if isinstance(grad, tuple): return tuple([xp.array(gy) for gy in grad]) else: return xp.array(grad),