Repository: chainer/chainer

Issue:
flaky test: TestMaxPooling2D
https://jenkins.preferred.jp/job/chainer/job/chainer_pr/1808/TEST=chainer-py35,label=mn1-p100/console

>TestMaxPooling2D_use_chainerx_false__chainerx_device_None__use_cuda_true__cuda_device_0__use_cudnn_never__cudnn_deterministic_false__autotune_false__cudnn_fast_batch_normalization_false__use_ideep_never_param_1_{c_contiguous=True, dtype=float32, cover_all=True}.test_backward _

```
TestMaxPooling2D_use_chainerx_false__chainerx_device_None__use_cuda_true__cuda_device_0__use_cudnn_never__cudnn_deterministic_false__autotune_false__cudnn_fast_batch_normalization_false__use_ideep_never_param_1_{c_contiguous=True, dtype=float32, cover_all=True}.test_backward _
11:44:10 
11:44:10 self = <chainer.testing._bundle.TestMaxPooling2D_use_chainerx_false__chainerx_device_None__use_cuda_true__cuda_device_0__use_...rmalization_false__use_ideep_never_param_1_{c_contiguous=True, dtype=float32, cover_all=True} testMethod=test_backward>
11:44:10 backend_config = <BackendConfig use_chainerx=False chainerx_device=None use_cuda=True cuda_device=0 use_cudnn='never' cudnn_deterministic=False autotune=False cudnn_fast_batch_normalization=False use_ideep='never'>
11:44:10 
11:44:10     def test_backward(self, backend_config):
11:44:10 >       self.check_backward(self.inputs, self.grad_outputs, backend_config)
11:44:10 
11:44:10 backend_config = <BackendConfig use_chainerx=False chainerx_device=None use_cuda=True cuda_device=0 use_cudnn='never' cudnn_deterministic=False autotune=False cudnn_fast_batch_normalization=False use_ideep='never'>
11:44:10 self       = <chainer.testing._bundle.TestMaxPooling2D_use_chainerx_false__chainerx_device_None__use_cuda_true__cuda_device_0__use_...rmalization_false__use_ideep_never_param_1_{c_contiguous=True, dtype=float32, cover_all=True} testMethod=test_backward>
11:44:10 
11:44:10 tests/chainer_tests/functions_tests/pooling_tests/test_max_pooling_2d.py:178: 
11:44:10 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
11:44:10 tests/chainer_tests/functions_tests/pooling_tests/test_max_pooling_2d.py:175: in check_backward
11:44:10     **self.check_backward_options)
11:44:10 chainer/gradient_check.py:893: in check_backward
11:44:10     detect_nondifferentiable, is_immutable_params=False
11:44:10 chainer/gradient_check.py:466: in run
11:44:10     self._run()
11:44:10 chainer/gradient_check.py:509: in _run
11:44:10     self._compare_gradients(gx_numeric, gx_backward, directions)
11:44:10 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
11:44:10 
11:44:10 self = <chainer.gradient_check._CheckBackward object at 0x7f93863cae10>
11:44:10 gx_numeric = array(-0.004788816709622168)
11:44:10 gx_backward = array(-0.004657887073003297)
11:44:10 directions = [array([[[[  2.07434076e-01,   4.05558587e-02,  -1.15760826e-01],
11:44:10          [ -5.42721864e-02,   7.04536363e-02,  -5.41...22351665e-03,  -8.70999864e-03,   3.14574323e-03],
11:44:10          [  2.08380791e-01,   2.26841553e-01,   2.28057435e-01]]]])]
11:44:10 
11:44:10     def _compare_gradients(self, gx_numeric, gx_backward, directions):
11:44:10         atol = self.atol
11:44:10         rtol = self.rtol
11:44:10         # Compare the gradients
11:44:10         try:
11:44:10             testing.assert_allclose(
11:44:10                 gx_numeric, gx_backward, atol=atol, rtol=rtol)
11:44:10         except AssertionError as e:
11:44:10             eps = self.eps
11:44:10             x_data = self.x_data
11:44:10             y_grad = self.y_grad
11:44:10             f = six.StringIO()
11:44:10             f.write('check_backward failed (eps={} atol={} rtol={})\n'.format(
11:44:10                 eps, atol, rtol))
11:44:10             for i, x_ in enumerate(x_data):
11:44:10                 f.write('inputs[{}]:\n'.format(i))
11:44:10                 f.write('{}\n'.format(x_))
11:44:10             for i, gy_ in enumerate(y_grad):
11:44:10                 f.write('grad_outputs[{}]:\n'.format(i))
11:44:10                 f.write('{}\n'.format(gy_))
11:44:10             for i, d_ in enumerate(directions):
11:44:10                 f.write('directions[{}]:\n'.format(i))
11:44:10                 f.write('{}\n'.format(d_))
11:44:10             f.write('gradients (numeric):  {}\n'.format(gx_numeric))
11:44:10             f.write('gradients (backward): {}\n'.format(gx_backward))
11:44:10             f.write('\n')
11:44:10             f.write(str(e))
11:44:10 >           raise AssertionError(f.getvalue())
11:44:10 E           AssertionError: Parameterized test failed.
11:44:10 E           
11:44:10 E           Base test method: TestMaxPooling2D_use_chainerx_false__chainerx_device_None__use_cuda_true__cuda_device_0__use_cudnn_never__cudnn_deterministic_false__autotune_false__cudnn_fast_batch_normalization_false__use_ideep_never.test_backward
11:44:10 E           Test parameters:
11:44:10 E             c_contiguous: True
11:44:10 E             dtype: <class 'numpy.float32'>
11:44:10 E             cover_all: True
11:44:10 E           
11:44:10 E           
11:44:10 E           (caused by)
11:44:10 E           AssertionError: check_backward failed (eps=0.001 atol=0.0001 rtol=0.001)
11:44:10 E           inputs[0]:
11:44:10 E           [[[[-1.         -0.97222221 -0.94444442]
11:44:10 E              [-0.91666669 -0.8888889  -0.8611111 ]
11:44:10 E              [-0.83333331 -0.80555558 -0.77777779]
11:44:10 E              [-0.75       -0.72222221 -0.69444442]]
11:44:10 E           
11:44:10 E             [[-0.66666663 -0.6388889  -0.6111111 ]
11:44:10 E              [-0.58333337 -0.55555558 -0.52777779]
11:44:10 E              [-0.5        -0.47222221 -0.44444442]
11:44:10 E              [-0.41666669 -0.3888889  -0.3611111 ]]
11:44:10 E           
11:44:10 E             [[-0.33333331 -0.30555558 -0.27777779]
11:44:10 E              [-0.25       -0.22222221 -0.19444442]
11:44:10 E              [-0.16666669 -0.1388889  -0.1111111 ]
11:44:10 E              [-0.08333331 -0.05555558 -0.02777779]]]
11:44:10 E           
11:44:10 E           
11:44:10 E            [[[ 0.          0.02777779  0.05555558]
11:44:10 E              [ 0.08333337  0.11111116  0.13888884]
11:44:10 E              [ 0.16666663  0.19444442  0.22222221]
11:44:10 E              [ 0.25        0.27777779  0.30555558]]
11:44:10 E           
11:44:10 E             [[ 0.33333337  0.36111116  0.38888884]
11:44:10 E              [ 0.41666663  0.44444442  0.47222221]
11:44:10 E              [ 0.5         0.52777779  0.55555558]
11:44:10 E              [ 0.58333337  0.61111116  0.63888884]]
11:44:10 E           
11:44:10 E             [[ 0.66666663  0.69444442  0.72222221]
11:44:10 E              [ 0.75        0.77777779  0.80555558]
11:44:10 E              [ 0.83333337  0.86111116  0.88888884]
11:44:10 E              [ 0.91666663  0.94444442  0.97222221]]]]
11:44:10 E           grad_outputs[0]:
11:44:10 E           [[[[-0.68112725  0.97208649]
11:44:10 E              [-0.36703053  0.8593595 ]
11:44:10 E              [ 0.30736575  0.27167416]]
11:44:10 E           
11:44:10 E             [[-0.34524256 -0.02598712]
11:44:10 E              [-0.67752177 -0.64966011]
11:44:10 E              [ 0.72144878  0.33097723]]
11:44:10 E           
11:44:10 E             [[-0.0579711  -0.57706678]
11:44:10 E              [-0.87018639  0.51643556]
11:44:10 E              [ 0.41426352 -0.33979934]]]
11:44:10 E           
11:44:10 E           
11:44:10 E            [[[ 0.15900621 -0.34532672]
11:44:10 E              [-0.06399921 -0.82780933]
11:44:10 E              [ 0.73593926 -0.30457276]]
11:44:10 E           
11:44:10 E             [[-0.12708722 -0.13105574]
11:44:10 E              [ 0.69752222  0.94780743]
11:44:10 E              [ 0.98663449 -0.39181113]]
11:44:10 E           
11:44:10 E             [[-0.59754425  0.12429881]
11:44:10 E              [-0.53728098  0.9496783 ]
11:44:10 E              [-0.61039865  0.12098856]]]]
11:44:10 E           directions[0]:
11:44:10 E           [[[[  2.07434076e-01   4.05558587e-02  -1.15760826e-01]
11:44:10 E              [ -5.42721864e-02   7.04536363e-02  -5.41104494e-02]
11:44:10 E              [  6.92656949e-03  -1.32102861e-01  -5.12977046e-02]
11:44:10 E              [ -6.79941728e-02   7.19229501e-02   5.03192651e-02]]
11:44:10 E           
11:44:10 E             [[  2.55757531e-02  -1.03361754e-01   2.09279602e-01]
11:44:10 E              [  8.13926341e-02   1.15023799e-01   1.59489809e-01]
11:44:10 E              [ -4.23199672e-02   2.17958250e-02   1.17221094e-01]
11:44:10 E              [ -2.88871546e-02  -1.82443690e-02   1.39167940e-01]]
11:44:10 E           
11:44:10 E             [[  1.01128428e-04   1.56394587e-01  -6.24255018e-02]
11:44:10 E              [  1.96497673e-02   1.95019118e-01  -8.59613270e-02]
11:44:10 E              [  6.41217005e-02  -9.94319369e-02  -4.02137838e-03]
11:44:10 E              [  3.59261824e-02   4.79061538e-02  -1.49921065e-01]]]
11:44:10 E           
11:44:10 E           
11:44:10 E            [[[  1.14470081e-01  -1.74630851e-01  -1.19068117e-02]
11:44:10 E              [ -1.58765481e-01  -5.46029635e-03  -9.97137175e-02]
11:44:10 E              [  3.59797960e-02   9.12209406e-02  -7.87372271e-02]
11:44:10 E              [  5.72540934e-02  -5.47763512e-02   9.82026580e-03]]
11:44:10 E           
11:44:10 E             [[ -4.02011495e-02   8.95996716e-03  -9.48838542e-02]
11:44:10 E              [ -2.75203803e-01  -1.61994200e-01  -1.31850555e-01]
11:44:10 E              [  1.17121303e-01   1.20371200e-01   3.21676987e-01]
11:44:10 E              [ -2.40413858e-01   6.35632226e-02   1.01029339e-01]]
11:44:10 E           
11:44:10 E             [[  1.85149981e-02   1.43243154e-01   4.02411140e-02]
11:44:10 E              [ -5.08259111e-02   5.39851653e-03  -2.01091658e-01]
11:44:10 E              [ -3.22351665e-03  -8.70999864e-03   3.14574323e-03]
11:44:10 E              [  2.08380791e-01   2.26841553e-01   2.28057435e-01]]]]
11:44:10 E           gradients (numeric):  -0.004788816709622168
11:44:10 E           gradients (backward): -0.004657887073003297
11:44:10 E           
11:44:10 E           
11:44:10 E           Not equal to tolerance rtol=0.001, atol=0.0001
11:44:10 E           
11:44:10 E           (mismatch 100.0%)
11:44:10 E            x: array(-0.004788816709622168)
11:44:10 E            y: array(-0.004657887073003297)
11:44:10 E           
11:44:10 E           assert_allclose failed: 
11:44:10 E             shape: () ()
11:44:10 E             dtype: float64 float64
11:44:10 E             i: (0,)
11:44:10 E             x[i]: -0.004788816709622168
11:44:10 E             y[i]: -0.004657887073003297
11:44:10 E             relative error[i]: 0.0281092337720524
11:44:10 E             absolute error[i]: 0.00013092963661887058
11:44:10 E           x: -0.004788816709622168
11:44:10 E           y: -0.004657887073003297
11:44:10 
11:44:10 atol       = 0.0001
11:44:10 d_         = array([[[[  2.07434076e-01,   4.05558587e-02,  -1.15760826e-01],
11:44:10          [ -5.42721864e-02,   7.04536363e-02,  -5.411....22351665e-03,  -8.70999864e-03,   3.14574323e-03],
11:44:10          [  2.08380791e-01,   2.26841553e-01,   2.28057435e-01]]]])
11:44:10 directions = [array([[[[  2.07434076e-01,   4.05558587e-02,  -1.15760826e-01],
11:44:10          [ -5.42721864e-02,   7.04536363e-02,  -5.41...22351665e-03,  -8.70999864e-03,   3.14574323e-03],
11:44:10          [  2.08380791e-01,   2.26841553e-01,   2.28057435e-01]]]])]
11:44:10 eps        = 0.001
11:44:10 f          = <_io.StringIO object at 0x7f93864e1af8>
11:44:10 gx_backward = array(-0.004657887073003297)
11:44:10 gx_numeric = array(-0.004788816709622168)
11:44:10 gy_        = array([[[[-0.68112725,  0.97208649],
11:44:10          [-0.36703053,  0.8593595 ],
11:44:10          [ 0.30736575,  0.27167416]],
11:44:10 
11:44:10      ...[-0.59754425,  0.12429881],
11:44:10          [-0.53728098,  0.9496783 ],
11:44:10          [-0.61039865,  0.12098856]]]], dtype=float32)
11:44:10 i          = 0
11:44:10 rtol       = 0.001
11:44:10 self       = <chainer.gradient_check._CheckBackward object at 0x7f93863cae10>
11:44:10 x_         = array([[[[-1.        , -0.97222221, -0.94444442],
11:44:10          [-0.91666669, -0.8888889 , -0.8611111 ],
11:44:10          [-0.83333...,
11:44:10          [ 0.83333337,  0.86111116,  0.88888884],
11:44:10          [ 0.91666663,  0.94444442,  0.97222221]]]], dtype=float32)
11:44:10 x_data     = (array([[[[-1.        , -0.97222221, -0.94444442],
11:44:10          [-0.91666669, -0.8888889 , -0.8611111 ],
11:44:10          [-0.8333...         [ 0.83333337,  0.86111116,  0.88888884],
11:44:10          [ 0.91666663,  0.94444442,  0.97222221]]]], dtype=float32),)
11:44:10 y_grad     = (array([[[[-0.68112725,  0.97208649],
11:44:10          [-0.36703053,  0.8593595 ],
11:44:10          [ 0.30736575,  0.27167416]],
11:44:10 
11:44:10     ...0.59754425,  0.12429881],
11:44:10          [-0.53728098,  0.9496783 ],
11:44:10          [-0.61039865,  0.12098856]]]], dtype=float32),)
```


Discussion:
The error only occurs in the following conditions:
* `test_backward` (not `test_forward` nor `test_double_backward`), and
* `dtype = float32` (not `float16` nor `float64`), and
* `not chainerx`, and
* `gpu`, and
* `not cudnn`


Resolving patch:
diff --git a/chainer/functions/pooling/max_pooling_2d.py b/chainer/functions/pooling/max_pooling_2d.py
--- a/chainer/functions/pooling/max_pooling_2d.py
+++ b/chainer/functions/pooling/max_pooling_2d.py
@@ -106,7 +106,7 @@ def forward_gpu(self, x):
                for (int y = in_y_0; y < in_y_1; ++y) {
                  int offset_y = w * (y + h * c0);
                  for (int x = in_x_0; x < in_x_1; ++x) {
-                   float v = in[x + offset_y];
+                   T v = in[x + offset_y];
                    if (maxval < v) {
                      maxval   = v;
                      argmax_y = y;


Test patch: