<s> from __future__ import absolute_import , print_function , division <EOL> from nose . plugins . skip import SkipTest <EOL> import unittest <EOL> import numpy <EOL> from theano import config <EOL> from theano import gof <EOL> import theano <EOL> import theano . tensor <EOL> from theano . compat import exc_message <EOL> from theano . compile import debugmode <EOL> import theano . compile <EOL> from theano . tests import unittest_tools as utt <EOL> def test0 ( ) : <EOL> x = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x ] , ( ( <NUM_LIT> * x ) + <NUM_LIT:7> ) / <NUM_LIT> , mode = debugmode . DebugMode ( ) ) <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> class BROKEN_ON_PURPOSE_Add ( gof . Op ) : <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __init__ ( self , py_offset ) : <EOL> gof . Op . __init__ ( self ) <EOL> self . py_offset = py_offset <EOL> def make_node ( self , a , b ) : <EOL> a = theano . tensor . as_tensor_variable ( a ) <EOL> b = theano . tensor . as_tensor_variable ( b ) <EOL> assert a . type . dtype == '<STR_LIT>' <EOL> assert a . type . dtype == b . type . dtype <EOL> assert a . type . ndim == <NUM_LIT:1> <EOL> r = gof . Apply ( self , [ a , b ] , [ a . type ( ) ] ) <EOL> return r <EOL> def perform ( self , node , inp , out_ ) : <EOL> a , b = inp <EOL> out , = out_ <EOL> z = a + b <EOL> if self . py_offset : <EOL> out [ <NUM_LIT:0> ] = z + <NUM_LIT:0.5> <EOL> else : <EOL> out [ <NUM_LIT:0> ] = z <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:1> , ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> a , b = inp <EOL> z , = out <EOL> return """<STR_LIT>""" % dict ( locals ( ) , ** sub ) <EOL> inconsistent = BROKEN_ON_PURPOSE_Add ( False ) <EOL> off_by_half = BROKEN_ON_PURPOSE_Add ( True ) <EOL> class WeirdBrokenOp ( gof . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __init__ ( self , behaviour ) : <EOL> gof . Op . __init__ ( self ) <EOL> self . behaviour = behaviour <EOL> def make_node ( self , a ) : <EOL> a_ = theano . tensor . as_tensor_variable ( a ) <EOL> r = gof . Apply ( self , [ a_ ] , [ a_ . type ( ) ] ) <EOL> return r <EOL> def dontuse_perform ( self , node , inp , out_ ) : <EOL> a , = inp <EOL> out , = out_ <EOL> if self . behaviour == '<STR_LIT>' : <EOL> out [ <NUM_LIT:0> ] = a * <NUM_LIT:2> <EOL> elif self . behaviour == '<STR_LIT>' : <EOL> out [ <NUM_LIT:0> ] = a <EOL> out [ <NUM_LIT:0> ] *= <NUM_LIT:2> <EOL> elif self . behaviour == '<STR_LIT>' : <EOL> out [ <NUM_LIT:0> ] = a * <NUM_LIT:1> <EOL> elif self . behaviour == '<STR_LIT>' : <EOL> out [ <NUM_LIT:0> ] = a <EOL> else : <EOL> raise ValueError ( self . behaviour ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:1> , ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> a , = inp <EOL> z , = out <EOL> if "<STR_LIT>" in self . behaviour : <EOL> z_code = """<STR_LIT>""" <EOL> else : <EOL> z_code = """<STR_LIT>""" <EOL> prep_vars = """<STR_LIT>""" <EOL> if self . behaviour == '<STR_LIT>' : <EOL> behaviour = "<STR_LIT>" <EOL> elif self . behaviour == '<STR_LIT>' : <EOL> behaviour = "<STR_LIT>" <EOL> elif self . behaviour == '<STR_LIT>' : <EOL> behaviour = "<STR_LIT>" <EOL> elif self . behaviour == '<STR_LIT>' : <EOL> behaviour = "<STR_LIT>" <EOL> else : <EOL> raise ValueError ( self . behaviour ) <EOL> prep_vars2 = """<STR_LIT>""" <EOL> total = ( ( z_code + prep_vars + behaviour + prep_vars2 ) <EOL> % dict ( locals ( ) , ** sub ) ) <EOL> return total <EOL> wb2i = WeirdBrokenOp ( '<STR_LIT>' ) <EOL> wb2 = WeirdBrokenOp ( '<STR_LIT>' ) <EOL> wb1i = WeirdBrokenOp ( '<STR_LIT>' ) <EOL> wb1 = WeirdBrokenOp ( '<STR_LIT>' ) <EOL> def test_badthunkoutput ( ) : <EOL> a = theano . tensor . dvector ( ) <EOL> b = theano . tensor . dvector ( ) <EOL> f_good = theano . function ( [ a , b ] , <EOL> off_by_half ( a , b ) , <EOL> mode = debugmode . DebugMode ( check_c_code = theano . config . cxx ) ) <EOL> f_inconsistent = theano . function ( [ a , b ] , <EOL> inconsistent ( a , b ) , <EOL> mode = debugmode . DebugMode ( check_c_code = theano . config . cxx ) ) <EOL> f_good ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> if not theano . config . cxx : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> try : <EOL> f_inconsistent ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> except debugmode . BadThunkOutput as e : <EOL> assert e . r . owner . op is inconsistent <EOL> return <EOL> assert False <EOL> def test_badoptimization ( ) : <EOL> @ gof . local_optimizer ( [ theano . tensor . add ] ) <EOL> def insert_broken_add ( node ) : <EOL> if node . op == theano . tensor . add : <EOL> return [ off_by_half ( * node . inputs ) ] <EOL> return False <EOL> edb = gof . EquilibriumDB ( ) <EOL> edb . register ( '<STR_LIT>' , insert_broken_add , '<STR_LIT:all>' ) <EOL> opt = edb . query ( '<STR_LIT>' ) <EOL> a = theano . tensor . dvector ( ) <EOL> b = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ a , b ] , a + b , <EOL> mode = debugmode . DebugMode ( optimizer = opt ) ) <EOL> try : <EOL> f ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , ) <EOL> except debugmode . BadOptimization as e : <EOL> assert str ( e . reason ) == '<STR_LIT>' <EOL> return <EOL> assert False <EOL> def test_badoptimization_opt_err ( ) : <EOL> """<STR_LIT>""" <EOL> @ gof . local_optimizer ( [ theano . tensor . add ] ) <EOL> def insert_bigger_b_add ( node ) : <EOL> if node . op == theano . tensor . add : <EOL> inputs = list ( node . inputs ) <EOL> if inputs [ - <NUM_LIT:1> ] . owner is None : <EOL> inputs [ - <NUM_LIT:1> ] = theano . tensor . concatenate ( ( inputs [ - <NUM_LIT:1> ] , <EOL> inputs [ - <NUM_LIT:1> ] ) ) <EOL> return [ node . op ( * inputs ) ] <EOL> return False <EOL> edb = gof . EquilibriumDB ( ) <EOL> edb . register ( '<STR_LIT>' , insert_bigger_b_add , '<STR_LIT:all>' ) <EOL> opt = edb . query ( '<STR_LIT>' ) <EOL> a = theano . tensor . dvector ( ) <EOL> b = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ a , b ] , a + b , <EOL> mode = debugmode . DebugMode ( optimizer = opt ) ) <EOL> try : <EOL> f ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , ) <EOL> except Exception as e : <EOL> assert '<STR_LIT>' in exc_message ( e ) <EOL> return <EOL> assert False <EOL> def test_stochasticoptimization ( ) : <EOL> last_time_replaced = [ False ] <EOL> @ gof . local_optimizer ( [ theano . tensor . add ] ) <EOL> def insert_broken_add_sometimes ( node ) : <EOL> if node . op == theano . tensor . add : <EOL> last_time_replaced [ <NUM_LIT:0> ] = not last_time_replaced [ <NUM_LIT:0> ] <EOL> if last_time_replaced [ <NUM_LIT:0> ] : <EOL> return [ off_by_half ( * node . inputs ) ] <EOL> return False <EOL> edb = gof . EquilibriumDB ( ) <EOL> edb . register ( <EOL> '<STR_LIT>' , <EOL> insert_broken_add_sometimes , <EOL> '<STR_LIT:all>' ) <EOL> opt = edb . query ( '<STR_LIT>' ) <EOL> a = theano . tensor . dvector ( ) <EOL> b = theano . tensor . dvector ( ) <EOL> try : <EOL> theano . function ( [ a , b ] , <EOL> theano . tensor . add ( a , b ) , <EOL> mode = debugmode . DebugMode ( <EOL> optimizer = opt , <EOL> check_c_code = True , <EOL> stability_patience = max ( <NUM_LIT:2> , config . DebugMode . patience ) ) ) <EOL> except debugmode . StochasticOrder : <EOL> return <EOL> assert False <EOL> def test_just_c_code ( ) : <EOL> if not theano . config . cxx : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> x = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x ] , wb2 ( x ) , <EOL> mode = debugmode . DebugMode ( check_py_code = False ) ) <EOL> assert numpy . all ( f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) == [ <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> def test_baddestroymap ( ) : <EOL> class BadAdd ( gof . Op ) : <EOL> def make_node ( self , a , b ) : <EOL> c = a . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , = out <EOL> c [ <NUM_LIT:0> ] = a <EOL> c [ <NUM_LIT:0> ] += b <EOL> x = theano . tensor . dvector ( ) <EOL> y = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x , y ] , BadAdd ( ) ( x , y ) , mode = '<STR_LIT>' ) <EOL> try : <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert False <EOL> except debugmode . BadDestroyMap : <EOL> pass <EOL> def test_baddestroymap_c ( ) : <EOL> if not theano . config . cxx : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> x = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x ] , wb2i ( x ) , <EOL> mode = debugmode . DebugMode ( check_py_code = False ) ) <EOL> try : <EOL> assert numpy . all ( f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) == [ <NUM_LIT:2> , <NUM_LIT:4> ] ) <EOL> assert False <EOL> except debugmode . BadDestroyMap : <EOL> pass <EOL> class Test_ViewMap ( unittest . TestCase ) : <EOL> class BadAddRef ( gof . Op ) : <EOL> def make_node ( self , a , b ) : <EOL> c = b . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , = out <EOL> c [ <NUM_LIT:0> ] = b <EOL> class BadAddSlice ( gof . Op ) : <EOL> def make_node ( self , a , b ) : <EOL> c = b . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , = out <EOL> c [ <NUM_LIT:0> ] = b [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> def test_badviewmap_ref ( self ) : <EOL> x = theano . tensor . dvector ( ) <EOL> y = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x , y ] , self . BadAddRef ( ) ( x , y ) , mode = '<STR_LIT>' ) <EOL> try : <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert False <EOL> except debugmode . BadViewMap : <EOL> return <EOL> def test_badviewmap_slice ( self ) : <EOL> x = theano . tensor . dvector ( ) <EOL> y = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x , y ] , self . BadAddSlice ( ) ( x , y ) , <EOL> mode = '<STR_LIT>' ) <EOL> try : <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert False <EOL> except debugmode . BadViewMap : <EOL> return <EOL> def test_goodviewmap ( self ) : <EOL> goodop = self . BadAddRef ( ) <EOL> goodop . view_map = { <NUM_LIT:0> : [ <NUM_LIT:1> ] } <EOL> x = theano . tensor . dvector ( ) <EOL> y = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x , y ] , goodop ( x , y ) , mode = '<STR_LIT>' ) <EOL> try : <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:1> ] , [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:4> ] ) <EOL> return <EOL> except debugmode . BadViewMap : <EOL> assert False <EOL> def test_badviewmap_c ( self ) : <EOL> if not theano . config . cxx : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> x = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x ] , wb1i ( x ) , <EOL> mode = debugmode . DebugMode ( check_py_code = False ) ) <EOL> try : <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert False <EOL> except debugmode . BadViewMap : <EOL> pass <EOL> def test_aliased_outputs_ok ( self ) : <EOL> class CustomOp ( gof . Op ) : <EOL> view_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] , <NUM_LIT:1> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , a , b ) : <EOL> c = a . type ( ) <EOL> d = a . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c , d ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , d = out <EOL> c [ <NUM_LIT:0> ] = a <EOL> d [ <NUM_LIT:0> ] = a [ <NUM_LIT:1> : ] <EOL> x = theano . tensor . dvector ( '<STR_LIT:x>' ) <EOL> y = theano . tensor . dvector ( '<STR_LIT:y>' ) <EOL> f = theano . function ( [ x , y ] , CustomOp ( ) ( x , y ) , mode = '<STR_LIT>' ) <EOL> r0 , r1 = f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> assert numpy . all ( r0 == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert numpy . all ( r1 == [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> def test_aliased_outputs_ok_output ( self ) : <EOL> class CustomOp ( gof . Op ) : <EOL> def make_node ( self , a , b ) : <EOL> c = a . type ( ) <EOL> d = a . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c , d ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , d = out <EOL> r = a * <NUM_LIT:2> <EOL> c [ <NUM_LIT:0> ] = r <EOL> d [ <NUM_LIT:0> ] = r [ <NUM_LIT:1> : ] <EOL> x = theano . tensor . dvector ( ) <EOL> y = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x , y ] , CustomOp ( ) ( x , y ) , mode = '<STR_LIT>' ) <EOL> r0 , r1 = f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> assert numpy . all ( r0 == [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] ) <EOL> assert numpy . all ( r1 == [ <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] ) <EOL> def test_aliased_outputs_ok_shadow ( self ) : <EOL> class CustomOp ( gof . Op ) : <EOL> def make_node ( self , a , b ) : <EOL> c = a . type ( ) <EOL> d = a . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c , d ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , d = out <EOL> r = a * <NUM_LIT:1> <EOL> c [ <NUM_LIT:0> ] = r <EOL> d [ <NUM_LIT:0> ] = r [ <NUM_LIT:1> : ] <EOL> x = theano . tensor . dvector ( '<STR_LIT:x>' ) <EOL> y = theano . tensor . dvector ( '<STR_LIT:y>' ) <EOL> f = theano . function ( [ x , y ] , CustomOp ( ) ( x , y ) [ <NUM_LIT:0> ] * <NUM_LIT:2> , mode = '<STR_LIT>' ) <EOL> r0 = f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> assert numpy . all ( r0 == [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] ) <EOL> def test_aliased_outputs_bad ( self ) : <EOL> class CustomOp ( gof . Op ) : <EOL> def make_node ( self , a , b ) : <EOL> c = a . type ( ) <EOL> d = a . type ( ) <EOL> return gof . Apply ( self , [ a , b ] , [ c , d ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> a , b = inp <EOL> c , d = out <EOL> r = a * <NUM_LIT:1> <EOL> c [ <NUM_LIT:0> ] = r [ : - <NUM_LIT:1> ] <EOL> d [ <NUM_LIT:0> ] = r [ <NUM_LIT:1> : ] <EOL> custom_op = CustomOp ( ) <EOL> x = theano . tensor . dvector ( ) <EOL> y = theano . tensor . dvector ( ) <EOL> bad_xy0 , bad_xy1 = custom_op ( x , y ) <EOL> out = bad_xy0 * <NUM_LIT:2> + bad_xy1 * <NUM_LIT:2> <EOL> f = theano . function ( [ x , y ] , out , mode = '<STR_LIT>' ) <EOL> try : <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ) <EOL> assert False <EOL> except debugmode . BadViewMap : <EOL> pass <EOL> class Test_check_isfinite ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . old_ts = theano . tensor . TensorType . filter_checks_isfinite <EOL> self . old_dm = theano . compile . mode . predefined_modes [ <EOL> '<STR_LIT>' ] . check_isfinite <EOL> def tearDown ( self ) : <EOL> theano . tensor . TensorType . filter_checks_isfinite = self . old_ts <EOL> theano . compile . mode . predefined_modes [ <EOL> '<STR_LIT>' ] . check_isfinite = self . old_dm <EOL> def test_check_isfinite ( self ) : <EOL> x = theano . tensor . vector ( ) <EOL> f = theano . function ( [ x ] , ( x + <NUM_LIT:2> ) * <NUM_LIT:5> , mode = '<STR_LIT>' ) <EOL> g = theano . function ( [ x ] , theano . tensor . log ( x ) , mode = '<STR_LIT>' ) <EOL> f ( numpy . log ( [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) . astype ( config . floatX ) ) <EOL> self . assertRaises ( debugmode . InvalidValueError , f , <EOL> numpy . log ( [ <NUM_LIT:3> , - <NUM_LIT:4> , <NUM_LIT:5> ] ) . astype ( config . floatX ) ) <EOL> self . assertRaises ( debugmode . InvalidValueError , f , <EOL> ( numpy . asarray ( [ <NUM_LIT:0> , <NUM_LIT:1.0> , <NUM_LIT:0> ] ) / <NUM_LIT:0> ) . astype ( config . floatX ) ) <EOL> self . assertRaises ( debugmode . InvalidValueError , f , <EOL> ( numpy . asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) / <NUM_LIT:0> ) . astype ( config . floatX ) ) <EOL> self . assertRaises ( debugmode . InvalidValueError , g , <EOL> numpy . asarray ( [ <NUM_LIT:3> , - <NUM_LIT:4> , <NUM_LIT:5> ] , dtype = config . floatX ) ) <EOL> theano . tensor . TensorType . filter_checks_isfinite = False <EOL> theano . compile . mode . predefined_modes [ <EOL> '<STR_LIT>' ] . check_isfinite = False <EOL> f ( numpy . asarray ( numpy . asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) / <NUM_LIT:0> , <EOL> dtype = config . floatX ) ) <EOL> def test_check_isfinite_disabled ( self ) : <EOL> x = theano . tensor . dvector ( ) <EOL> f = theano . function ( [ x ] , ( x + <NUM_LIT:2> ) * <NUM_LIT:5> , <EOL> mode = debugmode . DebugMode ( check_isfinite = False ) ) <EOL> f ( numpy . log ( [ <NUM_LIT:3> , - <NUM_LIT:4> , <NUM_LIT:5> ] ) ) <EOL> infs = numpy . asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) / <NUM_LIT:0> <EOL> f ( infs ) <EOL> return <EOL> class BrokenCImplementationAdd ( gof . Op ) : <EOL> __props__ = ( ) <EOL> def make_node ( self , a , b ) : <EOL> a = theano . tensor . as_tensor_variable ( a ) <EOL> b = theano . tensor . as_tensor_variable ( b ) <EOL> assert a . type . dtype == '<STR_LIT>' <EOL> assert a . type . dtype == b . type . dtype <EOL> assert a . type . ndim == <NUM_LIT:2> <EOL> r = gof . Apply ( self , [ a , b ] , [ a . type ( ) ] ) <EOL> return r <EOL> def perform ( self , node , inp , out_ ) : <EOL> a , b = inp <EOL> out , = out_ <EOL> z = a + b <EOL> out [ <NUM_LIT:0> ] = z <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:1> , ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> a , b = inp <EOL> z , = out <EOL> debug = <NUM_LIT:0> <EOL> return """<STR_LIT>""" % dict ( locals ( ) , ** sub ) <EOL> class VecAsRowAndCol ( gof . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> def make_node ( self , v ) : <EOL> if not isinstance ( v , gof . Variable ) : <EOL> v = theano . tensor . as_tensor_variable ( v ) <EOL> assert v . type . ndim == <NUM_LIT:1> <EOL> type_class = type ( v . type ) <EOL> out_r_type = type_class ( dtype = v . dtype , broadcastable = ( True , False ) ) <EOL> out_c_type = type_class ( dtype = v . dtype , broadcastable = ( False , True ) ) <EOL> return gof . Apply ( self , [ v ] , [ out_r_type ( ) , out_c_type ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> v , = inp <EOL> r , c = out <EOL> lv = v . shape [ <NUM_LIT:0> ] <EOL> if ( r [ <NUM_LIT:0> ] is None ) or ( r [ <NUM_LIT:0> ] . shape != ( <NUM_LIT:1> , lv ) ) : <EOL> r [ <NUM_LIT:0> ] = node . outputs [ <NUM_LIT:0> ] . type . value_zeros ( ( <NUM_LIT:1> , lv ) ) <EOL> if ( c [ <NUM_LIT:0> ] is None ) or ( c [ <NUM_LIT:0> ] . shape != ( lv , <NUM_LIT:1> ) ) : <EOL> c [ <NUM_LIT:0> ] = node . outputs [ <NUM_LIT:1> ] . type . value_zeros ( ( lv , <NUM_LIT:1> ) ) <EOL> for i in range ( lv ) : <EOL> r [ <NUM_LIT:0> ] [ <NUM_LIT:0> , i ] = v [ i ] <EOL> c [ <NUM_LIT:0> ] [ i , <NUM_LIT:0> ] = v [ i ] <EOL> class Test_preallocated_output ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rng = numpy . random . RandomState ( seed = utt . fetch_seed ( ) ) <EOL> def test_f_contiguous ( self ) : <EOL> a = theano . tensor . fmatrix ( '<STR_LIT:a>' ) <EOL> b = theano . tensor . fmatrix ( '<STR_LIT:b>' ) <EOL> z = BrokenCImplementationAdd ( ) ( a , b ) <EOL> out = theano . tensor . dot ( z , numpy . eye ( <NUM_LIT:7> ) ) <EOL> a_val = self . rng . randn ( <NUM_LIT:7> , <NUM_LIT:7> ) . astype ( '<STR_LIT>' ) <EOL> b_val = self . rng . randn ( <NUM_LIT:7> , <NUM_LIT:7> ) . astype ( '<STR_LIT>' ) <EOL> mode = debugmode . DebugMode ( <EOL> check_preallocated_output = [ '<STR_LIT>' ] ) <EOL> f = theano . function ( [ a , b ] , out , mode = mode ) <EOL> f ( a_val , b_val ) <EOL> mode = debugmode . DebugMode ( <EOL> check_preallocated_output = [ '<STR_LIT>' ] ) <EOL> f = theano . function ( [ a , b ] , out , mode = mode ) <EOL> if theano . config . cxx : <EOL> self . assertRaises ( debugmode . BadThunkOutput , f , a_val , b_val ) <EOL> else : <EOL> f ( a_val , b_val ) <EOL> def test_f_contiguous_out ( self ) : <EOL> a = theano . tensor . fmatrix ( '<STR_LIT:a>' ) <EOL> b = theano . tensor . fmatrix ( '<STR_LIT:b>' ) <EOL> out = BrokenCImplementationAdd ( ) ( a , b ) <EOL> a_val = self . rng . randn ( <NUM_LIT:7> , <NUM_LIT:7> ) . astype ( '<STR_LIT>' ) <EOL> b_val = self . rng . randn ( <NUM_LIT:7> , <NUM_LIT:7> ) . astype ( '<STR_LIT>' ) <EOL> mode = debugmode . DebugMode ( <EOL> check_preallocated_output = [ '<STR_LIT>' ] ) <EOL> f = theano . function ( [ a , b ] , out , mode = mode ) <EOL> f ( a_val , b_val ) <EOL> mode = debugmode . DebugMode ( <EOL> check_preallocated_output = [ '<STR_LIT>' ] ) <EOL> f = theano . function ( [ a , b ] , out , mode = mode ) <EOL> if theano . config . cxx : <EOL> self . assertRaises ( debugmode . BadThunkOutput , f , a_val , b_val ) <EOL> else : <EOL> f ( a_val , b_val ) <EOL> def test_output_broadcast_tensor ( self ) : <EOL> v = theano . tensor . fvector ( '<STR_LIT:v>' ) <EOL> c , r = VecAsRowAndCol ( ) ( v ) <EOL> f = theano . function ( [ v ] , [ c , r ] ) <EOL> v_val = self . rng . randn ( <NUM_LIT:5> ) . astype ( '<STR_LIT>' ) <EOL> f ( v_val ) <EOL> def test_output_broadcast_cuda ( self ) : <EOL> from theano . sandbox import cuda <EOL> if not cuda . cuda_available : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> if cuda . use . device_number is None : <EOL> cuda . use ( "<STR_LIT>" , <EOL> force = True , <EOL> default_to_move_computation_to_gpu = False , <EOL> move_shared_float32_to_gpu = False , <EOL> enable_cuda = False ) <EOL> v = cuda . fvector ( '<STR_LIT:v>' ) <EOL> c , r = VecAsRowAndCol ( ) ( v ) <EOL> f = theano . function ( [ v ] , [ c , r ] ) <EOL> v_val = cuda . CudaNdarray ( self . rng . randn ( <NUM_LIT:5> ) . astype ( '<STR_LIT>' ) ) <EOL> f ( v_val ) </s>
