MCPcopy Create free account
hub / github.com/apache/singa / slice_test

Method slice_test

test/python/test_operation.py:2804–2846  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

2802 self.reduceMean_test(gpu_dev)
2803
2804 def slice_test(self, dev):
2805 X = np.random.randn(20, 10, 5).astype(np.float32)
2806 indexes = np.array(range(20 * 10 * 5)).reshape(20, 10, 5)
2807 configs = [
2808 # starts, ends, axes, steps, y
2809 [[0, 0], [3, 10], [0, 1], [1, 1], X[0:3, 0:10],
2810 indexes[0:3, 0:10]], # slice
2811 [[0, 0, 3], [20, 10, 4], None, None, X[:, :, 3:4],
2812 indexes[:, :, 3:4]], # slice_default_axes
2813 [[1], [1000], [1], [1], X[:, 1:1000],
2814 indexes[:, 1:1000]], # slice_end_out_of_bounds
2815 [[0], [-1], [1], [1], X[:, 0:-1],
2816 indexes[:, 0:-1]], # slice_end_out_of_bounds
2817 [[20, 10, 4], [0, 0, 1], [0, 1, 2], [-1, -3, -2],
2818 X[20:0:-1, 10:0:-3, 4:1:-2], indexes[20:0:-1, 10:0:-3,
2819 4:1:-2]], # slice_neg_steps
2820 [[0, 0, 3], [20, 10, 4], [0, -2, -1], None, X[:, :, 3:4],
2821 indexes[:, :, 3:4]], # slice_negative_axes
2822 # [[1000], [1000], [1], [1], X[:, 1000:1000], indexes[:, 1000:1000]], # slice_start_out_of_bounds # cannot support empty tensor
2823 ]
2824 for starts, ends, axes, steps, y, dx_idx in configs:
2825 dy = np.ones(y.shape).astype(np.float32)
2826
2827 x = tensor.from_numpy(X)
2828 dy = tensor.from_numpy(dy)
2829 x.to_device(dev)
2830 dy.to_device(dev)
2831
2832 result = autograd.slice(x, starts, ends, axes, steps)
2833 dx = result.creator.backward(dy.data)
2834
2835 dx_idx = tuple(dx_idx.flatten().tolist())
2836 dX = np.array([
2837 1. if i in dx_idx else 0. for i in range(20 * 10 * 5)
2838 ]).reshape(X.shape)
2839
2840 np.testing.assert_array_almost_equal(tensor.to_numpy(result),
2841 y,
2842 decimal=5)
2843 np.testing.assert_array_almost_equal(tensor.to_numpy(
2844 tensor.from_raw_tensor(dx)),
2845 dX,
2846 decimal=5)
2847
2848 def test_slice_cpu(self):
2849 self.slice_test(cpu_dev)

Callers 2

test_slice_cpuMethod · 0.95
test_slice_gpuMethod · 0.95

Calls 4

tupleFunction · 0.50
reshapeMethod · 0.45
to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected