<s> import inspect <EOL> import unittest <EOL> import numpy as np <EOL> import chainer <EOL> from chainer import cuda <EOL> from chainer import testing <EOL> from chainer . testing import attr <EOL> import re <EOL> import six <EOL> class Constant ( chainer . Function ) : <EOL> def __init__ ( self , outputs ) : <EOL> self . outputs = outputs <EOL> def forward_cpu ( self , inputs ) : <EOL> return self . outputs <EOL> def forward_gpu ( self , inputs ) : <EOL> return tuple ( map ( cuda . to_gpu , self . outputs ) ) <EOL> def backward_cpu ( self , inputs , grad_outputs ) : <EOL> return tuple ( map ( np . zeros_like , inputs ) ) <EOL> def backward_gpu ( self , inputs , grad_outputs ) : <EOL> return tuple ( map ( cuda . cupy . zeros_like , inputs ) ) <EOL> def constant ( xs , value ) : <EOL> return Constant ( value ) ( * xs ) <EOL> class TestVariable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = np . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:10> ) . astype ( np . float32 ) <EOL> self . a = np . random . uniform ( <NUM_LIT:0.1> , <NUM_LIT:10> , <NUM_LIT:10> ) . astype ( np . float32 ) <EOL> self . c = np . arange ( <NUM_LIT:10> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> ) . astype ( np . float32 ) <EOL> def test_repr ( self ) : <EOL> x = chainer . Variable ( self . x , name = '<STR_LIT:x>' ) <EOL> self . assertEqual ( repr ( x ) , '<STR_LIT>' ) <EOL> def test_str ( self ) : <EOL> x = chainer . Variable ( self . x , name = '<STR_LIT:x>' ) <EOL> self . assertEqual ( str ( x ) , '<STR_LIT:x>' ) <EOL> def check_len ( self , gpu ) : <EOL> x = self . x <EOL> if gpu : <EOL> x = cuda . to_gpu ( x ) <EOL> x = chainer . Variable ( x ) <EOL> self . assertEqual ( len ( x ) , <NUM_LIT:10> ) <EOL> def test_len_cpu ( self ) : <EOL> self . check_len ( False ) <EOL> @ attr . gpu <EOL> def test_len_gpu ( self ) : <EOL> self . check_len ( True ) <EOL> def check_label ( self , expected , gpu ) : <EOL> c = self . c <EOL> if gpu : <EOL> c = cuda . to_gpu ( c ) <EOL> c = chainer . Variable ( c ) <EOL> self . assertEqual ( c . label , expected ) <EOL> def test_label_cpu ( self ) : <EOL> self . check_label ( '<STR_LIT>' , False ) <EOL> @ attr . gpu <EOL> def test_label_gpu ( self ) : <EOL> self . check_label ( '<STR_LIT>' , True ) <EOL> def check_backward ( self , inputs , intermediates , outputs , retain_grad ) : <EOL> for o in outputs : <EOL> o . backward ( retain_grad ) <EOL> self . assertTrue ( all ( [ x . grad is not None for x in inputs ] ) ) <EOL> if retain_grad : <EOL> self . assertTrue ( all ( [ x . grad is not None for x in intermediates ] ) ) <EOL> else : <EOL> self . assertTrue ( all ( [ x . grad is None for x in intermediates ] ) ) <EOL> self . assertTrue ( any ( [ x . grad is not None for x in outputs ] ) ) <EOL> def create_linear_chain ( self , length , gpu ) : <EOL> if gpu : <EOL> x = chainer . Variable ( cuda . to_gpu ( self . x ) ) <EOL> else : <EOL> x = chainer . Variable ( self . x ) <EOL> ret = [ x ] <EOL> for i in six . moves . range ( length ) : <EOL> ret . append ( constant ( ( ret [ i ] , ) , ( self . a , ) ) ) <EOL> if gpu : <EOL> ret [ - <NUM_LIT:1> ] . grad = cuda . cupy . zeros_like ( ret [ - <NUM_LIT:1> ] . data ) <EOL> else : <EOL> ret [ - <NUM_LIT:1> ] . grad = np . zeros_like ( ret [ - <NUM_LIT:1> ] . data ) <EOL> return ret <EOL> def test_backward_cpu ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:2> , False ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:0> ] , ) , ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , False ) <EOL> @ attr . gpu <EOL> def test_backward_gpu ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:2> , True ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:0> ] , ) , ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , False ) <EOL> def test_backward_cpu_retain_grad ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:2> , False ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:0> ] , ) , ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , True ) <EOL> @ attr . gpu <EOL> def test_backward_gpu_retain_grad ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:2> , True ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:0> ] , ) , ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , True ) <EOL> def test_unchain_backward_cpu ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:3> , False ) <EOL> ret [ <NUM_LIT:1> ] . unchain_backward ( ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , ( ret [ <NUM_LIT:3> ] , ) , False ) <EOL> @ attr . gpu <EOL> def test_unchain_backward_gpu ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:3> , True ) <EOL> ret [ <NUM_LIT:1> ] . unchain_backward ( ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , ( ret [ <NUM_LIT:3> ] , ) , False ) <EOL> def test_unchain_backward_cpu_retain_grad ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:3> , False ) <EOL> ret [ <NUM_LIT:1> ] . unchain_backward ( ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , ( ret [ <NUM_LIT:3> ] , ) , False ) <EOL> @ attr . gpu <EOL> def test_unchain_backward_gpu_retain_grad ( self ) : <EOL> ret = self . create_linear_chain ( <NUM_LIT:3> , False ) <EOL> ret [ <NUM_LIT:1> ] . unchain_backward ( ) <EOL> self . check_backward ( ( ret [ <NUM_LIT:1> ] , ) , ( ret [ <NUM_LIT:2> ] , ) , ( ret [ <NUM_LIT:3> ] , ) , False ) <EOL> def test_invalid_value_type ( self ) : <EOL> with six . assertRaisesRegex ( self , TypeError , '<STR_LIT:int>' ) : <EOL> chainer . Variable ( <NUM_LIT:1> ) <EOL> def test_grad_type_check_pass ( self ) : <EOL> a = chainer . Variable ( np . empty ( ( <NUM_LIT:3> , ) , dtype = np . float32 ) ) <EOL> a . grad = np . ndarray ( ( <NUM_LIT:3> , ) , dtype = np . float32 ) <EOL> def test_grad_type_check_type ( self ) : <EOL> a = chainer . Variable ( np . empty ( ( ) , dtype = np . float32 ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> a . grad = np . float32 ( ) <EOL> @ attr . gpu <EOL> def test_grad_type_check_type_cpu_gpu_mixture ( self ) : <EOL> a = chainer . Variable ( np . empty ( ( <NUM_LIT:3> , ) , dtype = np . float32 ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> a . grad = cuda . cupy . empty ( ( <NUM_LIT:3> , ) , dtype = np . float32 ) <EOL> def test_grad_type_check_dtype ( self ) : <EOL> a = chainer . Variable ( np . empty ( ( <NUM_LIT:3> , ) , dtype = np . float32 ) ) <EOL> with self . assertRaises ( TypeError ) : <EOL> a . grad = np . empty ( ( <NUM_LIT:3> , ) , dtype = np . float64 ) <EOL> def test_grad_type_check_shape ( self ) : <EOL> a = chainer . Variable ( np . empty ( ( <NUM_LIT:3> , ) , dtype = np . float32 ) ) <EOL> with self . assertRaises ( ValueError ) : <EOL> a . grad = np . empty ( ( <NUM_LIT:2> , ) , dtype = np . float32 ) <EOL> def test_to_cpu_from_cpu ( self ) : <EOL> a = chainer . Variable ( np . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . grad = np . ones_like ( a . data ) <EOL> b = a . data <EOL> gb = a . grad <EOL> c = b . copy ( ) <EOL> gc = gb . copy ( ) <EOL> a . to_cpu ( ) <EOL> self . assertIs ( a . data , b ) <EOL> self . assertIs ( a . grad , gb ) <EOL> np . testing . assert_array_equal ( a . data , c ) <EOL> np . testing . assert_array_equal ( a . grad , gc ) <EOL> @ attr . gpu <EOL> def test_to_cpu ( self ) : <EOL> a = chainer . Variable ( cuda . cupy . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . grad = cuda . cupy . ones_like ( a . data ) <EOL> a . to_cpu ( ) <EOL> np . testing . assert_array_equal ( a . data , np . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> np . testing . assert_array_equal ( a . grad , np . ones ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_to_gpu_from_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> a = chainer . Variable ( cp . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . grad = cuda . cupy . ones_like ( a . data ) <EOL> b = a . data <EOL> gb = a . grad <EOL> c = b . copy ( ) <EOL> gc = gb . copy ( ) <EOL> a . to_gpu ( ) <EOL> self . assertIs ( a . data , b ) <EOL> self . assertIs ( a . grad , gb ) <EOL> cp . testing . assert_array_equal ( a . data , c ) <EOL> cp . testing . assert_array_equal ( a . grad , gc ) <EOL> @ attr . gpu <EOL> def test_to_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> a = chainer . Variable ( np . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . grad = np . ones ( <NUM_LIT:3> , dtype = np . float32 ) <EOL> a . to_gpu ( ) <EOL> cp . testing . assert_array_equal ( a . data , cp . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> cp . testing . assert_array_equal ( a . grad , cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . multi_gpu ( <NUM_LIT:2> ) <EOL> def test_to_gpu_from_another_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> a = chainer . Variable ( cp . zeros ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . grad = cuda . cupy . ones_like ( a . data ) <EOL> b = a . data . copy ( ) <EOL> gb = a . grad . copy ( ) <EOL> a . to_gpu ( <NUM_LIT:1> ) <EOL> self . assertEqual ( int ( cuda . get_device ( a . data ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( int ( cuda . get_device ( a . grad ) ) , <NUM_LIT:1> ) <EOL> cp . testing . assert_array_equal ( a . data , b ) <EOL> cp . testing . assert_array_equal ( a . grad , gb ) <EOL> def check_zerograd ( self , a_data , fill = False ) : <EOL> xp = cuda . get_array_module ( a_data ) <EOL> a = chainer . Variable ( a_data ) <EOL> if fill : <EOL> a . grad = xp . full_like ( a_data , np . nan ) <EOL> a . zerograd ( ) <EOL> self . assertIsNot ( a . grad , None ) <EOL> g_expect = xp . zeros_like ( a . data ) <EOL> xp . testing . assert_array_equal ( a . grad , g_expect ) <EOL> def test_zerograd_cpu ( self ) : <EOL> self . check_zerograd ( np . empty ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> def test_zerograd_fill_cpu ( self ) : <EOL> self . check_zerograd ( np . empty ( <NUM_LIT:3> , dtype = np . float32 ) , fill = True ) <EOL> @ attr . multi_gpu ( <NUM_LIT:2> ) <EOL> def test_zerograds_multi_gpu ( self ) : <EOL> cupy = cuda . cupy <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> a = chainer . Variable ( cupy . empty ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . zerograd ( ) <EOL> self . assertIsNot ( a . grad , None ) <EOL> self . assertEqual ( int ( a . grad . device ) , <NUM_LIT:1> ) <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> g_expect = cupy . zeros_like ( a . data ) <EOL> cupy . testing . assert_array_equal ( a . grad , g_expect ) <EOL> @ attr . multi_gpu ( <NUM_LIT:2> ) <EOL> def test_zerograds_fill_multi_gpu ( self ) : <EOL> cupy = cuda . cupy <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> a = chainer . Variable ( cupy . empty ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> a . grad = cupy . empty_like ( a . data ) <EOL> a . zerograd ( ) <EOL> self . assertEqual ( int ( a . grad . device ) , <NUM_LIT:1> ) <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> g_expect = cupy . zeros_like ( a . data ) <EOL> cupy . testing . assert_array_equal ( a . grad , g_expect ) <EOL> @ attr . gpu <EOL> def test_zerograd_gpu ( self ) : <EOL> self . check_zerograd ( cuda . cupy . empty ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_zerograd_fill_gpu ( self ) : <EOL> self . check_zerograd ( cuda . cupy . empty ( <NUM_LIT:3> , dtype = np . float32 ) , fill = True ) <EOL> def check_copydata ( self , data1 , data2 , expect ) : <EOL> xp = cuda . get_array_module ( data1 ) <EOL> v = chainer . Variable ( data1 ) <EOL> w = chainer . Variable ( data2 ) <EOL> v . copydata ( w ) <EOL> xp . testing . assert_array_equal ( v . data , expect ) <EOL> def test_copydata_cpu_to_cpu ( self ) : <EOL> self . check_copydata ( np . zeros ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> np . ones ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> np . ones ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_copydata_cpu_to_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> self . check_copydata ( cp . zeros ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> np . ones ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_copydata_gpu_to_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> self . check_copydata ( cp . zeros ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_copydata_gpu_to_cpu ( self ) : <EOL> cp = cuda . cupy <EOL> self . check_copydata ( np . zeros ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) , <EOL> np . ones ( <NUM_LIT:3> , dtype = np . float32 ) ) <EOL> @ attr . multi_gpu ( <NUM_LIT:2> ) <EOL> def test_copydata_gpu_to_another_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> with cuda . get_device ( <NUM_LIT:0> ) : <EOL> data1 = cp . zeros ( <NUM_LIT:3> , dtype = np . float32 ) <EOL> expect = cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> data2 = cp . ones ( <NUM_LIT:3> , dtype = np . float32 ) <EOL> self . check_copydata ( data1 , data2 , expect ) <EOL> def check_addgrad ( self , src , dst , expect ) : <EOL> xp = cuda . get_array_module ( dst ) <EOL> a = chainer . Variable ( src ) <EOL> a . grad = src <EOL> b = chainer . Variable ( dst ) <EOL> b . grad = dst <EOL> b . addgrad ( a ) <EOL> xp . testing . assert_array_equal ( b . grad , expect ) <EOL> def test_addgrad_cpu_to_cpu ( self ) : <EOL> self . check_addgrad ( np . full ( <NUM_LIT:3> , <NUM_LIT:10> , dtype = np . float32 ) , <EOL> np . full ( <NUM_LIT:3> , <NUM_LIT:20> , dtype = np . float32 ) , <EOL> np . full ( <NUM_LIT:3> , <NUM_LIT:30> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_addgrad_cpu_to_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> self . check_addgrad ( np . full ( <NUM_LIT:3> , <NUM_LIT:10> , dtype = np . float32 ) , <EOL> cp . full ( <NUM_LIT:3> , <NUM_LIT:20> , dtype = np . float32 ) , <EOL> cp . full ( <NUM_LIT:3> , <NUM_LIT:30> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_addgrad_gpu_to_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> self . check_addgrad ( cp . full ( <NUM_LIT:3> , <NUM_LIT:10> , dtype = np . float32 ) , <EOL> cp . full ( <NUM_LIT:3> , <NUM_LIT:20> , dtype = np . float32 ) , <EOL> cp . full ( <NUM_LIT:3> , <NUM_LIT:30> , dtype = np . float32 ) ) <EOL> @ attr . gpu <EOL> def test_addgrad_gpu_to_cpu ( self ) : <EOL> cp = cuda . cupy <EOL> self . check_addgrad ( cp . full ( <NUM_LIT:3> , <NUM_LIT:10> , dtype = np . float32 ) , <EOL> np . full ( <NUM_LIT:3> , <NUM_LIT:20> , dtype = np . float32 ) , <EOL> np . full ( <NUM_LIT:3> , <NUM_LIT:30> , dtype = np . float32 ) ) <EOL> @ attr . multi_gpu ( <NUM_LIT:2> ) <EOL> def test_addgrad_gpu_to_another_gpu ( self ) : <EOL> cp = cuda . cupy <EOL> with cuda . get_device ( <NUM_LIT:1> ) : <EOL> a = cp . full ( <NUM_LIT:3> , <NUM_LIT:10> , dtype = np . float32 ) <EOL> with cuda . get_device ( <NUM_LIT:0> ) : <EOL> b = cp . full ( <NUM_LIT:3> , <NUM_LIT:20> , dtype = np . float32 ) <EOL> c = cp . full ( <NUM_LIT:3> , <NUM_LIT:30> , dtype = np . float32 ) <EOL> self . check_addgrad ( a , b , c ) <EOL> class TestDebugPrint ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . arr = np . random . randn ( <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:5> ) . astype ( np . float32 ) <EOL> def check_debug_print ( self , v , mean , std ) : <EOL> result = v . debug_print ( ) <EOL> self . assertIn ( repr ( v ) , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . assertTrue ( re . match ( r'<STR_LIT>' , <EOL> result . splitlines ( ) [ <NUM_LIT:4> ] ) ) <EOL> msg = '<STR_LIT>' <EOL> msg = msg . format ( mean = mean , std = std ) <EOL> self . assertIn ( msg , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> v . zerograd ( ) <EOL> result = v . debug_print ( ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> v . grad = v . data <EOL> result = v . debug_print ( ) <EOL> msg = '<STR_LIT>' . format ( mean = mean , std = std ) <EOL> self . assertIn ( msg , result ) <EOL> def test_debug_print_cpu ( self ) : <EOL> v = chainer . Variable ( self . arr ) <EOL> result = v . debug_print ( ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . check_debug_print ( v , mean = float ( np . mean ( v . data ) ) , <EOL> std = float ( np . std ( v . data ) ) ) <EOL> @ attr . gpu <EOL> def test_debug_print_gpu ( self ) : <EOL> v = chainer . Variable ( self . arr ) <EOL> v . to_gpu ( <NUM_LIT:0> ) <EOL> result = v . debug_print ( ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . check_debug_print ( v , mean = float ( cuda . cupy . mean ( v . data ) ) , <EOL> std = float ( cuda . cupy . std ( v . data ) ) ) <EOL> class TestVariableSetCreator ( unittest . TestCase ) : <EOL> class MockFunction ( object ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> self . x = np . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , ( <NUM_LIT:2> , <NUM_LIT:5> ) ) . astype ( np . float32 ) <EOL> self . f = self . MockFunction ( ) <EOL> self . f . rank = <NUM_LIT:10> <EOL> def check_set_creator ( self , x ) : <EOL> x = chainer . Variable ( x ) <EOL> x . set_creator ( self . f ) <EOL> self . assertEqual ( x . creator , self . f ) <EOL> self . assertEqual ( x . rank , <NUM_LIT:11> ) <EOL> def test_set_creator_cpu ( self ) : <EOL> self . check_set_creator ( self . x ) <EOL> @ attr . gpu <EOL> def test_set_creator_gpu ( self ) : <EOL> self . check_set_creator ( cuda . to_gpu ( self . x ) ) <EOL> class TestVariableBackwardError ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = np . array ( [ <NUM_LIT:1> ] , np . float32 ) <EOL> def check_type_mismatch ( self , x_data ) : <EOL> xp = cuda . get_array_module ( x_data ) <EOL> class DummyFunction ( chainer . Function ) : <EOL> label = '<STR_LIT>' <EOL> def forward ( self , inputs ) : <EOL> return xp . array ( <NUM_LIT:1> , np . float32 ) , <EOL> def backward ( self , inputs , grads ) : <EOL> return [ <NUM_LIT:1> ] <EOL> x = chainer . Variable ( x_data ) <EOL> y = DummyFunction ( ) ( x ) <EOL> with six . assertRaisesRegex ( self , TypeError , '<STR_LIT>' ) : <EOL> y . backward ( ) <EOL> def test_type_mismatch_cpu ( self ) : <EOL> self . check_type_mismatch ( self . x ) <EOL> @ attr . gpu <EOL> def test_type_mismatch_gpu ( self ) : <EOL> self . check_type_mismatch ( cuda . to_gpu ( self . x ) ) <EOL> def check_dtype_mismatch ( self , x_data ) : <EOL> xp = cuda . get_array_module ( x_data ) <EOL> class DummyFunction ( chainer . Function ) : <EOL> label = '<STR_LIT>' <EOL> def forward ( self , inputs ) : <EOL> return xp . array ( <NUM_LIT:1> , np . float32 ) , <EOL> def backward ( self , inputs , grads ) : <EOL> return xp . array ( [ <NUM_LIT:1> ] , np . int32 ) , <EOL> x = chainer . Variable ( x_data ) <EOL> y = DummyFunction ( ) ( x ) <EOL> with six . assertRaisesRegex ( self , TypeError , '<STR_LIT>' ) : <EOL> y . backward ( ) <EOL> def test_dtype_mismatch_cpu ( self ) : <EOL> self . check_dtype_mismatch ( self . x ) <EOL> @ attr . gpu <EOL> def test_dtype_mismatch_gpu ( self ) : <EOL> self . check_dtype_mismatch ( cuda . to_gpu ( self . x ) ) <EOL> def check_shape_mismatch ( self , x_data ) : <EOL> xp = cuda . get_array_module ( x_data ) <EOL> class DummyFunction ( chainer . Function ) : <EOL> label = '<STR_LIT>' <EOL> def forward ( self , inputs ) : <EOL> return xp . array ( <NUM_LIT:1> , np . float32 ) , <EOL> def backward ( self , inputs , grads ) : <EOL> return xp . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , np . float32 ) , <EOL> x = chainer . Variable ( x_data ) <EOL> y = DummyFunction ( ) ( x ) <EOL> with six . assertRaisesRegex ( self , ValueError , '<STR_LIT>' ) : <EOL> y . backward ( ) <EOL> def test_shape_mismatch_cpu ( self ) : <EOL> self . check_shape_mismatch ( self . x ) <EOL> @ attr . gpu <EOL> def test_shape_mismatch_gpu ( self ) : <EOL> self . check_shape_mismatch ( cuda . to_gpu ( self . x ) ) <EOL> class TestVariableBackwardErrorTraceback ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = np . array ( [ <NUM_LIT:1> ] , np . float32 ) <EOL> chainer . set_debug ( True ) <EOL> def tearDown ( self ) : <EOL> chainer . set_debug ( False ) <EOL> def check_traceback ( self , x_data ) : <EOL> xp = cuda . get_array_module ( x_data ) <EOL> class DummyFunction ( chainer . Function ) : <EOL> label = '<STR_LIT>' <EOL> def forward ( self , inputs ) : <EOL> return xp . array ( <NUM_LIT:1> , np . float32 ) , <EOL> def backward ( self , inputs , grads ) : <EOL> return xp . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , np . float32 ) , <EOL> x = chainer . Variable ( x_data ) <EOL> line = inspect . currentframe ( ) . f_lineno + <NUM_LIT:1> <EOL> y = DummyFunction ( ) ( x ) <EOL> try : <EOL> y . backward ( ) <EOL> self . fail ( ) <EOL> except ValueError as e : <EOL> self . assertIn ( '<STR_LIT>' , str ( e ) ) <EOL> self . assertIn ( '<STR_LIT>' % line , str ( e ) ) <EOL> def test_traceback_cpu ( self ) : <EOL> self . check_traceback ( self . x ) <EOL> @ attr . gpu <EOL> def test_traceback_gpu ( self ) : <EOL> self . check_traceback ( cuda . to_gpu ( self . x ) ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
