def assert_cuda_active(should_be_active): if should_be_active is True: # assert has_cuda() # Make sure we have cuda for the test if not has_cuda(): warnings.warn("You are trying to use cuda but it's not installed.") return set_cuda_active(should_be_active) if should_be_active is True: assert is_cuda_active() # Make sure it is properly activated
def add(self, node, dy, caller=None): selfid = id(node) if selfid in self.variables: v = self.variables[selfid] with self.unlock_node(v): if has_cuda() and isinstance(dy, GPUValue): diff = v.get_gpu() + dy v.set_gpu(diff) else: v[...] += dy else: if has_cuda() and isinstance(dy, GPUValue): dy = Variable(dy) self.variables[selfid] = dy if node._auto_update: self._auto_updates.append(node) self._backwards[selfid] += 1 return self._refcounts[selfid] <= self._backwards[selfid]
import numpy as np from renom.core import Node from renom.layers.function.utils import imnpool, poolnim import renom.cuda as cu if cu.has_cuda(): from renom.cuda.gpuvalue import GPUValue, get_gpu from renom.cuda import is_cuda_active class SimpleContainer(object): def __init__(self, item): self._item = item class max_unpoolnd(Node): def __new__(cls, x, prev_pool): assert len(x.shape) > 3 and len(x.shape) < 6, \ "Unpoolnd accepts tensors whose dimension is dim > 3 and dim < 6. Actual is {}".format( len(x.shape)) return cls.calc_value(x, prev_pool._item) @classmethod def _oper_cpu(cls, x, prev_pool): result = poolnim(prev_pool.attrs._x, x, prev_pool.attrs._kernel, prev_pool.attrs._stride, prev_pool.attrs._padding, mode="max") ret = cls._create_node(result) ret.attrs._x = x ret.attrs._original_x = prev_pool.attrs._x ret.attrs._kernel = prev_pool.attrs._kernel ret.attrs._stride = prev_pool.attrs._stride
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import pytest from itertools import product import renom.cuda as cuda from renom.utility.reinforcement.replaybuffer import ReplayBuffer from renom.utility.searcher import GridSearcher, RandomSearcher, BayesSearcher skipgpu = pytest.mark.skipif(not cuda.has_cuda(), reason="cuda is not installed") skipmultigpu = pytest.mark.skipif(not cuda.has_cuda() or (cuda.cuGetDeviceCount() < 2), reason="Number of gpu card is less than 2.") np.random.seed(101) eps = np.sqrt(np.finfo(np.float32).eps) def auto_diff(function, node, *args): loss = function(*args) return loss.grad().get(node) def numeric_diff(function, node, *args): shape = node.shape diff = np.zeros_like(node) if True: # 5 point numerical diff coefficients1 = [1, -8, 8, -1] coefficients2 = [-2, -1, 1, 2]
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import pytest from itertools import product import renom.cuda as cuda from renom.utility.reinforcement.replaybuffer import ReplayBuffer from renom.utility.searcher import GridSearcher, RandomSearcher, BayesSearcher skipgpu = pytest.mark.skipif(not cuda.has_cuda(), reason="cuda is not installed") skipmultigpu = pytest.mark.skipif( not cuda.has_cuda() or (cuda.cuGetDeviceCount() < 2), reason="Number of gpu card is less than 2.") np.random.seed(10) eps = np.sqrt(np.finfo(np.float32).eps) def auto_diff(function, node, *args): loss = function(*args) return loss.grad().get(node) def numeric_diff(function, node, *args): shape = node.shape diff = np.zeros_like(node) if True: # 5 point numerical diff coefficients1 = [1, -8, 8, -1] coefficients2 = [-2, -1, 1, 2]
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import pytest import renom.cuda as cuda from renom.utility.reinforcement.replaybuffer import ReplayBuffer skipgpu = pytest.mark.skipif(not cuda.has_cuda(), reason="cuda is not installed") np.random.seed(9) eps = np.sqrt(np.finfo(np.float32).eps) def auto_diff(function, node, *args): loss = function(*args) return loss.grad().get(node) def numeric_diff(function, node, *args): shape = node.shape diff = np.zeros_like(node) if True: # 5 point numerical diff coefficients1 = [1, -8, 8, -1] coefficients2 = [-2, -1, 1, 2] c = 12 else: # 3 point numerical diff coefficients1 = [1, -1] coefficients2 = [1, -1] c = 2