Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit 690d1cf

Browse files
committed
Do rebase and upstreaming
2 parents 80425ae + 58e08cf commit 690d1cf

File tree

23 files changed

+1092
-140
lines changed

23 files changed

+1092
-140
lines changed

chainer/function.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,48 @@ def backward_cpu_cosim(self, inputs, grad_outputs):
419419
chainer.enable_cosim()
420420
return output_cosim
421421

422-
def cpu_cosim_verify_result(self, mkl_result, numpy_result):
422+
def cpu_cosim_dump_forward_inputs(self, in_data):
423+
"""dump all forward inputs into file
424+
425+
Aims to dump the inputs into a file in order to reproduce offline,
426+
if encountering a mismatch in cosim results of forward prop.
427+
To support this feature, implement it for each function.
428+
429+
Args:
430+
inputs: Tuple of input arrays.
431+
432+
"""
433+
pass
434+
435+
def cpu_cosim_dump_backward_inputs(self, in_data, out_grad):
436+
"""dump all backward inputs into file
437+
438+
Aims to dump the inputs into a file in order to reproduce offline,
439+
if encountering a mismatch in cosim results of backward prop.
440+
To support this feature, implement it for each function.
441+
442+
Args:
443+
inputs: Tuple of input arrays.
444+
out_grad: Tuple of output gradient arrays.
445+
446+
"""
447+
pass
448+
449+
def cpu_cosim_dump_inputs(self, inputs, out_grad=None):
450+
"""dump all inputs into a file in order to reprocude errors offline.
451+
"""
452+
inputs = [x if isinstance(x, variable.Variable)
453+
else variable.Variable(x)
454+
for x in inputs]
455+
456+
in_data = tuple([x.data for x in inputs])
457+
458+
if out_grad is None:
459+
self.cpu_cosim_dump_forward_inputs(in_data)
460+
else:
461+
self.cpu_cosim_dump_backward_inputs(in_data, out_grad)
462+
463+
def cpu_cosim_verify_result(self, mkl_result, numpy_result, inputs, out_grad=None):
423464
"""cosim verify result between MKLDNN and numpy
424465
"""
425466
if not chainer.is_cosim():
@@ -449,7 +490,13 @@ def cpu_cosim_verify_result(self, mkl_result, numpy_result):
449490
numpy_y_nd = np.array(numpy_y)
450491
i = i + 1
451492
if isinstance(mkl_x_nd, np.ndarray):
452-
testing.assert_allclose(mkl_x_nd, numpy_y_nd, **check_options)
493+
try:
494+
testing.assert_allclose(mkl_x_nd, numpy_y_nd, **check_options)
495+
except AssertionError:
496+
self.cpu_cosim_dump_inputs(inputs, out_grad)
497+
raise
498+
except:
499+
raise
453500
else:
454501
raise KeyError('cosim, unexpected!')
455502

chainer/functions/activation/relu.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ def relu(x):
8989
(3, 2)
9090
9191
"""
92-
if mkld.check_with_mkld((x, ), (2, 4)):
92+
if not isinstance(x.data, cuda.ndarray) and \
93+
mkld.check_with_mkld((x, ), (2, 4)):
9394
func = ReLUMKLDNN()
9495
ret = func(x)
9596
if chainer.is_cosim():
9697
func.cosim_func = ReLU()
9798
numpy_result = func.cosim_func(x)
98-
func.cpu_cosim_verify_result(ret, numpy_result)
99+
func.cpu_cosim_verify_result(ret, numpy_result, (x, ))
99100
return ret
100101
else:
101102
if chainer.mkld.available and \

chainer/functions/array/concat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ def concat(xs, axis=1):
9393
9494
"""
9595
x = xs[0]
96-
if mkld.check_with_mkld((x, ), (4, )):
96+
if not isinstance(x.data, cuda.ndarray) and \
97+
mkld.check_with_mkld((x, ), (4, )):
9798
func = ConcatMKLDNN(axis=axis)
9899
ret = func(*xs)
99100
if chainer.is_cosim():
100101
func.cosim_func = Concat(axis=axis)
101102
numpy_result = func.cosim_func(*xs)
102-
func.cpu_cosim_verify_result(ret, numpy_result)
103+
func.cpu_cosim_verify_result(ret, numpy_result, xs)
103104
return ret
104105
else:
105106
return Concat(axis=axis)(*xs)

chainer/functions/connection/convolution_2d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,11 @@ def convolution_2d(x, W, b=None, stride=1, pad=0,
330330
if b is None:
331331
ret = func(x, W)
332332
numpy_result = func.cosim_func(x, W)
333+
func.cpu_cosim_verify_result(ret, numpy_result, (x, W))
333334
else:
334335
ret = func(x, W, b)
335336
numpy_result = func.cosim_func(x, W, b)
336-
func.cpu_cosim_verify_result(ret, numpy_result)
337+
func.cpu_cosim_verify_result(ret, numpy_result, (x, W, b))
337338
return ret
338339
else:
339340
func = Convolution2DFunction(

chainer/functions/connection/deconvolution_2d.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import chainer
44
from chainer import cuda
5+
from chainer import mkld
56
from chainer import function
67
from chainer.functions.connection import convolution_2d
78
from chainer.utils import conv
@@ -18,6 +19,8 @@
1819
_bwd_data_pref = \
1920
libcudnn.CUDNN_CONVOLUTION_BWD_DATA_SPECIFY_WORKSPACE_LIMIT
2021

22+
if mkld.available:
23+
Deconvolution2DFunctionMKLDNN = mkld.deconvolution_2d.Deconvolution2DFunctionMKLDNN
2124

2225
_check_cudnn_acceptable_type = convolution_2d._check_cudnn_acceptable_type
2326

@@ -321,7 +324,24 @@ def deconvolution_2d(x, W, b=None, stride=1, pad=0,
321324
w_O &= s_X (w - 1) + k_W - 2p_W.
322325
323326
"""
324-
func = Deconvolution2DFunction(stride, pad, outsize, deterministic)
327+
# XXX: Switch the route
328+
if not isinstance(x.data, cuda.ndarray) and \
329+
mkld.check_with_mkld((x, W), ()):
330+
func = Deconvolution2DFunctionMKLDNN(stride, pad, outsize, deterministic)
331+
if chainer.is_cosim():
332+
func.cosim_func = Deconvolution2DFunction(stride, pad, outsize, deterministic)
333+
if b is None:
334+
ret = func(x, W)
335+
numpy_result = func.cosim_func(x, W)
336+
func.cpu_cosim_verify_result(ret, numpy_result, (x, W))
337+
else:
338+
ret = func(x, W, b)
339+
numpy_result = func.cosim_func(x, W, b)
340+
func.cpu_cosim_verify_result(ret, numpy_result, (x, W, b))
341+
return ret
342+
else:
343+
func = Deconvolution2DFunction(stride, pad, outsize, deterministic)
344+
325345
if b is None:
326346
return func(x, W)
327347
else:

chainer/functions/connection/linear.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def linear(x, W, b=None):
150150
"""
151151
# XXX: switch the route, work on the critera
152152

153-
if not isinstance(x.data, cuda.ndarray) and \
153+
if not isinstance(x, cuda.ndarray) and \
154+
not isinstance(x.data, cuda.ndarray) and \
154155
mkld.check_with_mkld((x, W), (2, 4)):
155156
if b is None:
156157
return LinearFunctionMKLDNN()(x, W)

chainer/functions/normalization/batch_normalization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def fixed_batch_normalization(x, gamma, beta, mean, var, eps=2e-5):
432432
if chainer.is_cosim():
433433
func.cosim_func = BatchNormalizationFunction(eps, None, None, 0.0)
434434
numpy_result = func.cosim_func(x, gamma, beta, mean, var)
435-
func.cpu_cosim_verify_result(ret, numpy_result)
435+
func.cpu_cosim_verify_result(ret, numpy_result, (x, gamma, beta, mean, var))
436436
return ret
437437
else:
438438
return BatchNormalizationFunction(eps, None, None, 0.0)(

chainer/functions/normalization/local_response_normalization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def local_response_normalization(x, n=5, k=2, alpha=1e-4, beta=.75):
140140
if chainer.is_cosim():
141141
func.cosim_func = LocalResponseNormalization(n, k, alpha, beta)
142142
numpy_result = func.cosim_func(x)
143-
func.cpu_cosim_verify_result(ret, numpy_result)
143+
func.cpu_cosim_verify_result(ret, numpy_result, (x, ))
144144
return ret
145145
else:
146146
return LocalResponseNormalization(n, k, alpha, beta)(x)

chainer/functions/pooling/average_pooling_2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def average_pooling_2d(x, ksize, stride=None, pad=0):
145145
if chainer.is_cosim():
146146
func.cosim_func = AveragePooling2D(ksize, stride, pad, False)
147147
numpy_result = func.cosim_func(x)
148-
func.cpu_cosim_verify_result(ret, numpy_result)
148+
func.cpu_cosim_verify_result(ret, numpy_result, (x, ))
149149
return ret
150150
else:
151151
return AveragePooling2D(ksize, stride, pad, False)(x)

chainer/functions/pooling/max_pooling_2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def max_pooling_2d(x, ksize, stride=None, pad=0, cover_all=True):
179179
if chainer.is_cosim():
180180
func.cosim_func = MaxPooling2D(ksize, stride, pad, cover_all)
181181
numpy_result = func.cosim_func(x)
182-
func.cpu_cosim_verify_result(ret, numpy_result)
182+
func.cpu_cosim_verify_result(ret, numpy_result, (x, ))
183183
return ret
184184
else:
185185
return MaxPooling2D(ksize, stride, pad, cover_all)(x)

0 commit comments

Comments
 (0)