def test_random_erase_recomputation(ctx, func_name, seed, prob, area_ratios, aspect_ratios, replacements, n, share, inplace, base_axis, func_seed, channel_last): if channel_last and func_name == "RandomErase": pytest.skip("RandomErase with channel_last is only supported in CUDA.") from nbla_test_utils import recomputation_test rng = np.random.RandomState(seed) b, c, h, w = 4, 3, 32, 32 ishape = [b, h, w, c] if channel_last else [b, c, h, w] vinputs = [nn.Variable(ishape)] func_kwargs = { 'prob': prob, 'area_ratios': area_ratios, 'aspect_ratios': aspect_ratios, 'replacements': replacements, 'n': n, 'share': share, 'inplace': inplace, 'base_axis': base_axis, 'seed': seed, 'channel_last': channel_last } recomputation_test(rng=rng, func=F.random_erase, vinputs=vinputs, func_args=[], func_kwargs=func_kwargs, ctx=ctx)
def test_convolution_2d_recomputation(inshape, kernel, outmaps, pad, stride, dilation, group, with_bias, seed, num_bits, inq_iterations, selection_algorithm, func_seed, ctx, func_name): from nbla_test_utils import recomputation_test rng = np.random.RandomState(seed) # Input inputs = [rng.randn(*inshape).astype(np.float32)] # Weights inmaps = inshape[-3] kshape = (outmaps,) + (inmaps // group,) + kernel inputs += [rng.randn(*kshape).astype(np.float32)] # Indices inputs += [np.random.randint(2, size=kshape)] # Bias if with_bias: inputs += [rng.randn(outmaps).astype(np.float32)] else: inputs += [None] base_axis = len(inshape) - 3 func_args = [base_axis, pad, stride, dilation, group, num_bits, inq_iterations, selection_algorithm, func_seed] vinputs = [] for i in inputs: if i is None: vinputs.append(None) else: vinputs.append(nn.Variable.from_numpy_array(i)) recomputation_test(rng=rng, func=F.inq_convolution, vinputs=vinputs, func_args=func_args, func_kwargs={}, ctx=ctx)
def test_randn_forward_backward(seed, ctx, func_name, mu, sigma, shape): with nn.context_scope(ctx): o = F.randn(mu, sigma, shape, seed=seed) assert o.shape == tuple(shape) assert o.parent.name == func_name o.forward() if o.size >= 10000: est_mu = o.d.mean() est_sigma = o.d.std() np.isclose(est_mu, mu, atol=sigma) np.isclose(est_sigma, sigma, atol=sigma) else: data = [] for i in range(10000): o.forward() data += [o.d.copy()] est_mu = np.mean(np.array(data)) est_sigma = np.std(np.array(data)) np.isclose(est_mu, mu, atol=sigma) np.isclose(est_sigma, sigma, atol=sigma) # Checking recomputation func_args = [mu, sigma, shape, seed] recomputation_test(rng=None, func=F.randn, vinputs=[], func_args=func_args, func_kwargs={}, ctx=ctx)
def test_rand_gamma_forward(seed, ctx, func_name, k, theta, shape): with nn.context_scope(ctx): o = F.rand_gamma(k, theta, shape, seed=seed) assert o.shape == tuple(shape) assert o.parent.name == func_name o.forward() if o.size > 10000: est_mu = o.d.mean() est_sigma = o.d.std() else: data = [] for i in range(1000000 // o.size): o.forward() data += [o.d.copy()] est_mu = np.mean(np.array(data)) est_sigma = np.std(np.array(data)) mu = k * theta # theoretical mean var = k * theta * theta sigma = np.sqrt(var) # theoretical std assert np.isclose(est_mu, mu, atol=5e-2) assert np.isclose(est_sigma, sigma, atol=5e-2) # Checking recomputation func_args = [k, theta, shape, seed] recomputation_test(rng=None, func=F.rand_gamma, vinputs=[], func_args=func_args, func_kwargs={}, ctx=ctx)
def test_rand_binomial_forward(seed, ctx, func_name, n, p, shape): with nn.context_scope(ctx): o = F.rand_binomial(n, p, shape, seed=seed) assert o.shape == tuple(shape) assert o.parent.name == func_name o.forward() if o.size >= 10000: est_mu = o.d.mean() est_sigma = o.d.std() else: data = [] for i in range(10000): o.forward() data += [o.d.copy()] est_mu = np.mean(np.array(data)) est_sigma = np.std(np.array(data)) mu = n * p # theoretical mean sigma = np.sqrt(n * p * (1 - p)) # theoretical std assert np.isclose(est_mu, mu, atol=5e-2) assert np.isclose(est_sigma, sigma, atol=5e-2) # Checking recomputation func_args = [n, p, shape, seed] recomputation_test(rng=None, func=F.rand_binomial, vinputs=[], func_args=func_args, func_kwargs={}, ctx=ctx)
def test_inq_affine_recomputation(seed, base_axis, weight_shape, num_bits, bias, inq_iterations, selection_algorithm, func_seed, ctx, func_name): from nbla_test_utils import recomputation_test rng = np.random.RandomState(seed) # Input inputs = [rng.randn(2, 3, 4).astype(np.float32)] # Weights inputs += [rng.randn(*weight_shape).astype(np.float32)] # Indices inputs += [np.random.randint(2, size=weight_shape)] # Bias if bias: inputs += [rng.randn(*weight_shape[1:]).astype(np.float32)] else: inputs += [None] func_args = [ base_axis, num_bits, inq_iterations, selection_algorithm, func_seed ] vinputs = [] for i in inputs: if i is None: vinputs.append(None) else: vinputs.append(nn.Variable.from_numpy_array(i)) recomputation_test(rng=rng, func=F.inq_affine, vinputs=vinputs, func_args=func_args, func_kwargs={}, ctx=ctx)
def test_random_shift_recomputation(seed, inshape, shifts, border_mode, constant_value, func_seed, base_axis, ctx, func_name): from nbla_test_utils import recomputation_test rng = np.random.RandomState(seed) vinputs = [nn.Variable(inshape)] func_args = [shifts, border_mode, constant_value, base_axis, func_seed] recomputation_test(rng=rng, func=F.random_shift, vinputs=vinputs, func_args=func_args, func_kwargs={}, ctx=ctx)
def test_assign_recomputation(seed, ctx, func_name): rng = np.random.RandomState(seed) dst = nn.Variable((2, 3, 4)) src = nn.Variable((2, 3, 4)) recomputation_test(rng=rng, func=F.assign, vinputs=[dst, src], func_args=[], func_kwargs={}, ctx=ctx)
def test_dropout_recompute(p, seed, ctx, func_name): from nbla_test_utils import recomputation_test rng = np.random.RandomState(0) x = nn.Variable((2, 3, 4)) func_args = [p, seed] recomputation_test(rng=rng, func=F.dropout, vinputs=[x], func_args=func_args, func_kwargs={}, ctx=ctx)
def test_random_crop_recomputation(seed, inshape, shape, base_axis, ctx, func_name): from nbla_test_utils import recomputation_test rng = np.random.RandomState(0) vinputs = [nn.Variable(inshape)] func_args = [shape, base_axis, seed] recomputation_test(rng=rng, func=F.random_crop, vinputs=vinputs, func_args=func_args, func_kwargs={}, ctx=ctx)
def test_random_flip_recomputation(seed, axes, func_seed, base_axis, ctx, func_name): from nbla_test_utils import recomputation_test rng = np.random.RandomState(seed) vinputs = [nn.Variable((5, 4, 3))] func_args = [axes, base_axis, func_seed] recomputation_test(rng=rng, func=F.random_flip, vinputs=vinputs, func_args=func_args, func_kwargs={}, ctx=ctx)
def test_randint_forward(seed, ctx, func_name, low, high, shape): with nn.context_scope(ctx): o = F.randint(low, high, shape, seed=seed) assert o.shape == tuple(shape) assert o.parent.name == func_name o.forward() # NOTE: The following should be < high, # but use <= high because std::uniform_random contains a bug. assert np.all(o.d <= high) assert np.all(o.d >= low) # Checking recomputation func_args = [low, high, shape, seed] recomputation_test(rng=None, func=F.randint, vinputs=[], func_args=func_args, func_kwargs={}, ctx=ctx)
def test_image_augmentation_forward(seed, shape, ctx, func_name): rng = np.random.RandomState(seed) inputs = [rng.randn(16, 3, 8, 8).astype(np.float32)] i = nn.Variable(inputs[0].shape) # NNabla forward with nn.context_scope(ctx), nn.auto_forward(): o = F.image_augmentation(i) assert o.d.shape == inputs[0].shape func_kargs = { 'shape': shape, 'pad': (2, 2), 'min_scale': 0.8, 'max_scale': 1.2, 'angle': 0.2, 'aspect_ratio': 1.1, 'distortion': 0.1, 'flip_lr': True, 'flip_ud': False, 'brightness': 0.1, 'brightness_each': True, 'contrast': 1.1, 'contrast_center': 0.5, 'contrast_each': True, 'noise': 0.1, 'seed': 0} with nn.context_scope(ctx), nn.auto_forward(): o = F.image_augmentation(i, **func_kargs) assert o.d.shape == (inputs[0].shape[0],) + shape # Checking recomputation from nbla_test_utils import recomputation_test recomputation_test(rng=rng, func=F.image_augmentation, vinputs=[i], func_args=[], func_kwargs=func_kargs, ctx=ctx) func_kargs['seed'] = -1 recomputation_test(rng=rng, func=F.image_augmentation, vinputs=[i], func_args=[], func_kwargs=func_kargs, ctx=ctx)