<s> from __future__ import print_function <EOL> import gc <EOL> import sys <EOL> import time <EOL> import unittest <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy <EOL> from six import itervalues <EOL> from theano import function <EOL> from theano . gof import vm <EOL> from theano . gof import OpWiseCLinker <EOL> from six . moves import xrange <EOL> from theano . compile import Mode <EOL> from theano import tensor <EOL> from theano . ifelse import ifelse <EOL> import theano <EOL> class TestCallbacks ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . n_callbacks = { } <EOL> def callback ( self , node , thunk , storage_map , compute_map ) : <EOL> key = node . op . __class__ . __name__ <EOL> self . n_callbacks . setdefault ( key , <NUM_LIT:0> ) <EOL> self . n_callbacks [ key ] += <NUM_LIT:1> <EOL> def test_callback ( self ) : <EOL> a , b , c = tensor . scalars ( '<STR_LIT:abc>' ) <EOL> f = function ( [ a , b , c ] , ( a + b ) + c , <EOL> mode = Mode ( <EOL> optimizer = None , <EOL> linker = vm . VM_Linker ( callback = self . callback ) ) ) <EOL> f ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert sum ( self . n_callbacks . values ( ) ) == len ( f . maker . fgraph . toposort ( ) ) <EOL> f ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert ( sum ( self . n_callbacks . values ( ) ) == <EOL> len ( f . maker . fgraph . toposort ( ) ) * <NUM_LIT:2> ) <EOL> def test_callback_with_ifelse ( self ) : <EOL> a , b , c = tensor . scalars ( '<STR_LIT:abc>' ) <EOL> f = function ( [ a , b , c ] , ifelse ( a , <NUM_LIT:2> * b , <NUM_LIT:2> * c ) , <EOL> mode = Mode ( <EOL> optimizer = None , <EOL> linker = vm . VM_Linker ( callback = self . callback ) ) ) <EOL> f ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert self . n_callbacks [ '<STR_LIT>' ] == <NUM_LIT:2> <EOL> def test_c_thunks ( ) : <EOL> a = tensor . scalars ( '<STR_LIT:a>' ) <EOL> b , c = tensor . vectors ( '<STR_LIT>' ) <EOL> cases = [ False ] <EOL> if theano . config . cxx : <EOL> cases . append ( True ) <EOL> for c_thunks in cases : <EOL> f = function ( [ a , b , c ] , ifelse ( a , a * b , b * c ) , <EOL> mode = Mode ( <EOL> optimizer = None , <EOL> linker = vm . VM_Linker ( c_thunks = c_thunks , <EOL> use_cloop = False ) ) ) <EOL> f ( <NUM_LIT:1> , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] ) <EOL> from nose . tools import assert_raises <EOL> assert_raises ( ValueError , f , <NUM_LIT:0> , [ <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> assert any ( [ hasattr ( t , '<STR_LIT>' ) for t in f . fn . thunks ] ) == c_thunks <EOL> def test_speed ( ) : <EOL> if not theano . config . cxx : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def build_graph ( x , depth = <NUM_LIT:5> ) : <EOL> z = x <EOL> for d in range ( depth ) : <EOL> z = ( z + z ) <EOL> return z <EOL> def numpy_version ( x , depth ) : <EOL> z = x <EOL> for d in xrange ( depth ) : <EOL> z = ( z + z ) <EOL> return z <EOL> def time_numpy ( ) : <EOL> steps_a = <NUM_LIT:5> <EOL> steps_b = <NUM_LIT:100> <EOL> x = numpy . asarray ( [ <NUM_LIT> , <NUM_LIT> ] , dtype = theano . config . floatX ) <EOL> numpy_version ( x , steps_a ) <EOL> t0 = time . time ( ) <EOL> t1 = time . time ( ) <EOL> t2 = time . time ( ) <EOL> t3 = time . time ( ) <EOL> t_a = t1 - t0 <EOL> t_b = t3 - t2 <EOL> print ( "<STR_LIT>" % ( <EOL> '<STR_LIT>' , <EOL> ( <NUM_LIT:1000> * ( t_b - t_a ) / ( steps_b - steps_a ) ) ) ) <EOL> def time_linker ( name , linker ) : <EOL> steps_a = <NUM_LIT:5> <EOL> steps_b = <NUM_LIT:100> <EOL> x = tensor . vector ( ) <EOL> a = build_graph ( x , steps_a ) <EOL> b = build_graph ( x , steps_b ) <EOL> f_a = function ( [ x ] , a , <EOL> mode = Mode ( optimizer = None , linker = linker ( ) ) ) <EOL> f_b = function ( [ x ] , b , <EOL> mode = Mode ( optimizer = None , linker = linker ( ) ) ) <EOL> f_a ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> t0 = time . time ( ) <EOL> f_a ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> t1 = time . time ( ) <EOL> f_b ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> t2 = time . time ( ) <EOL> f_b ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> t3 = time . time ( ) <EOL> t_a = t1 - t0 <EOL> t_b = t3 - t2 <EOL> print ( "<STR_LIT>" % ( <EOL> name , <EOL> ( <NUM_LIT:1000> * ( t_b - t_a ) / ( steps_b - steps_a ) ) ) ) <EOL> time_linker ( '<STR_LIT>' , OpWiseCLinker ) <EOL> time_linker ( '<STR_LIT>' , vm . VM_Linker ) <EOL> time_linker ( '<STR_LIT>' , lambda : vm . VM_Linker ( allow_gc = False ) ) <EOL> if theano . config . cxx : <EOL> time_linker ( '<STR_LIT>' , lambda : vm . VM_Linker ( allow_gc = False , <EOL> use_cloop = True ) ) <EOL> time_numpy ( ) <EOL> def test_speed_lazy ( ) : <EOL> def build_graph ( x , depth = <NUM_LIT:5> ) : <EOL> z = x <EOL> for d in range ( depth ) : <EOL> z = ifelse ( z [ <NUM_LIT:0> ] > <NUM_LIT:0> , - z , z ) <EOL> return z <EOL> def time_linker ( name , linker ) : <EOL> steps_a = <NUM_LIT:10> <EOL> steps_b = <NUM_LIT:100> <EOL> x = tensor . vector ( ) <EOL> a = build_graph ( x , steps_a ) <EOL> b = build_graph ( x , steps_b ) <EOL> f_a = function ( [ x ] , a , <EOL> mode = Mode ( optimizer = None , <EOL> linker = linker ( ) ) ) <EOL> f_b = function ( [ x ] , b , <EOL> mode = Mode ( optimizer = None , <EOL> linker = linker ( ) ) ) <EOL> f_a ( [ <NUM_LIT> ] ) <EOL> t0 = time . time ( ) <EOL> f_a ( [ <NUM_LIT> ] ) <EOL> t1 = time . time ( ) <EOL> f_b ( [ <NUM_LIT> ] ) <EOL> t2 = time . time ( ) <EOL> f_b ( [ <NUM_LIT> ] ) <EOL> t3 = time . time ( ) <EOL> t_a = t1 - t0 <EOL> t_b = t3 - t2 <EOL> print ( "<STR_LIT>" % ( <EOL> name , <EOL> ( <NUM_LIT:1000> * ( t_b - t_a ) / ( steps_b - steps_a ) ) ) ) <EOL> time_linker ( '<STR_LIT>' , vm . VM_Linker ) <EOL> time_linker ( '<STR_LIT>' , lambda : vm . VM_Linker ( allow_gc = False ) ) <EOL> if theano . config . cxx : <EOL> time_linker ( '<STR_LIT>' , lambda : vm . VM_Linker ( allow_gc = False , <EOL> use_cloop = True ) ) <EOL> def test_allow_gc_cvm ( ) : <EOL> mode = theano . config . mode <EOL> if mode in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> mode = "<STR_LIT>" <EOL> v = theano . tensor . vector ( ) <EOL> f = theano . function ( [ v ] , v + <NUM_LIT:1> , mode = mode ) <EOL> f ( [ <NUM_LIT:1> ] ) <EOL> n = list ( f . maker . fgraph . apply_nodes ) [ <NUM_LIT:0> ] . outputs [ <NUM_LIT:0> ] <EOL> assert f . fn . storage_map [ n ] [ <NUM_LIT:0> ] is None <EOL> assert f . fn . allow_gc is True <EOL> f . fn . allow_gc = False <EOL> assert f . fn . allow_gc is False <EOL> f ( [ <NUM_LIT:1> ] ) <EOL> assert f . fn . storage_map [ n ] [ <NUM_LIT:0> ] is not None <EOL> f . fn . allow_gc = True <EOL> assert f . fn . allow_gc is True <EOL> f ( [ <NUM_LIT:1> ] ) <EOL> assert f . fn . storage_map [ n ] [ <NUM_LIT:0> ] is None <EOL> run_memory_usage_tests = False <EOL> if run_memory_usage_tests : <EOL> def test_leak2 ( ) : <EOL> import theano . sandbox . cuda as cuda <EOL> for i in xrange ( <NUM_LIT> ) : <EOL> n = numpy . asarray ( [ <NUM_LIT> , <NUM_LIT> ] , dtype = '<STR_LIT:f>' ) <EOL> c = sys . getrefcount ( n ) <EOL> a = cuda . CudaNdarray ( n ) <EOL> a . sum ( ) <EOL> assert c == sys . getrefcount ( n ) <EOL> del a <EOL> if not i % <NUM_LIT:1000> : <EOL> print ( '<STR_LIT:.>' , end = '<STR_LIT:U+0020>' ) <EOL> print ( gc . collect ( ) , end = '<STR_LIT:U+0020>' ) <EOL> print ( gc . collect ( ) ) <EOL> sys . stdout . flush ( ) <EOL> def test_no_leak_many_graphs ( ) : <EOL> for i in xrange ( <NUM_LIT> ) : <EOL> x = tensor . vector ( ) <EOL> z = x <EOL> for d in range ( <NUM_LIT:10> ) : <EOL> z = tensor . sin ( - z + <NUM_LIT:1> ) <EOL> f = function ( [ x ] , z , mode = Mode ( optimizer = None , linker = '<STR_LIT>' ) ) <EOL> if not i % <NUM_LIT:100> : <EOL> print ( gc . collect ( ) ) <EOL> sys . stdout . flush ( ) <EOL> gc . collect ( ) <EOL> if <NUM_LIT:1> : <EOL> f ( [ <NUM_LIT> ] ) <EOL> f ( [ <NUM_LIT> ] ) <EOL> f ( [ <NUM_LIT> ] ) <EOL> f ( [ <NUM_LIT> ] ) <EOL> def test_no_leak_many_call_lazy ( ) : <EOL> def build_graph ( x , depth = <NUM_LIT:5> ) : <EOL> z = x <EOL> for d in range ( depth ) : <EOL> z = ifelse ( z . mean ( ) > <NUM_LIT:0.5> , - z , z ) <EOL> return z <EOL> def time_linker ( name , linker ) : <EOL> steps_a = <NUM_LIT:10> <EOL> x = tensor . dvector ( ) <EOL> a = build_graph ( x , steps_a ) <EOL> f_a = function ( [ x ] , a , <EOL> mode = Mode ( optimizer = None , <EOL> linker = linker ( ) ) ) <EOL> inp = numpy . random . rand ( <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> f_a ( inp ) <EOL> if <NUM_LIT:0> : <EOL> import resource <EOL> pre = resource . getrusage ( resource . RUSAGE_SELF ) <EOL> post = resource . getrusage ( resource . RUSAGE_SELF ) <EOL> print ( pre . ru_ixrss , post . ru_ixrss ) <EOL> print ( pre . ru_idrss , post . ru_idrss ) <EOL> print ( pre . ru_maxrss , post . ru_maxrss ) <EOL> print ( <NUM_LIT:1> ) <EOL> time_linker ( '<STR_LIT>' , <EOL> lambda : vm . VM_Linker ( allow_gc = False , use_cloop = True ) ) <EOL> print ( <NUM_LIT:2> ) <EOL> time_linker ( '<STR_LIT>' , <EOL> lambda : vm . VM_Linker ( allow_gc = False , use_cloop = False ) ) <EOL> def test_no_leak_many_call_nonlazy ( ) : <EOL> def build_graph ( x , depth = <NUM_LIT:5> ) : <EOL> z = x <EOL> for d in range ( depth ) : <EOL> z = tensor . sin ( - z + <NUM_LIT:1> ) <EOL> return z <EOL> def time_linker ( name , linker ) : <EOL> steps_a = <NUM_LIT:10> <EOL> x = tensor . dvector ( ) <EOL> a = build_graph ( x , steps_a ) <EOL> f_a = function ( [ x ] , a , <EOL> mode = Mode ( optimizer = None , <EOL> linker = linker ( ) ) ) <EOL> inp = numpy . random . rand ( <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT> ) : <EOL> f_a ( inp ) <EOL> print ( <NUM_LIT:1> ) <EOL> time_linker ( '<STR_LIT>' , <EOL> lambda : vm . VM_Linker ( allow_gc = False , use_cloop = True ) ) <EOL> print ( <NUM_LIT:2> ) <EOL> time_linker ( '<STR_LIT>' , <EOL> lambda : vm . VM_Linker ( allow_gc = False , use_cloop = False ) ) <EOL> class RunOnce ( theano . Op ) : <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __init__ ( self ) : <EOL> self . nb_run = <NUM_LIT:0> <EOL> def make_node ( self , x ) : <EOL> return theano . Apply ( self , [ x ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> assert self . nb_run == <NUM_LIT:0> <EOL> self . nb_run += <NUM_LIT:1> <EOL> outputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = inputs [ <NUM_LIT:0> ] . copy ( ) <EOL> def test_vm_gc ( ) : <EOL> """<STR_LIT>""" <EOL> x = theano . tensor . vector ( ) <EOL> p = RunOnce ( ) ( x ) <EOL> mode = theano . Mode ( linker = theano . gof . vm . VM_Linker ( lazy = True ) ) <EOL> f = theano . function ( [ theano . In ( x , mutable = True ) ] , [ p + <NUM_LIT:1> , p + <NUM_LIT:2> ] , <EOL> mode = mode ) <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> p = RunOnce ( ) ( x ) <EOL> pp = p + p <EOL> f = theano . function ( [ x ] , [ pp + pp ] , <EOL> mode = mode ) <EOL> f ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_reallocation ( ) : <EOL> x = tensor . scalar ( '<STR_LIT:x>' ) <EOL> y = tensor . scalar ( '<STR_LIT:y>' ) <EOL> z = tensor . tanh ( <NUM_LIT:3> * x + y ) + tensor . cosh ( x + <NUM_LIT:5> * y ) <EOL> for l in [ vm . VM_Linker ( allow_gc = False , lazy = False , use_cloop = False ) , <EOL> vm . VM_Linker ( allow_gc = True , lazy = False , use_cloop = False ) ] : <EOL> m = theano . compile . get_mode ( theano . Mode ( linker = l ) ) <EOL> m = m . excluding ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> f = theano . function ( [ x , y ] , z , name = "<STR_LIT>" , <EOL> mode = m ) <EOL> output = f ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert output <EOL> storage_map = f . fn . storage_map <EOL> def check_storage ( storage_map ) : <EOL> from theano . tensor . var import TensorConstant <EOL> for i in storage_map : <EOL> if not isinstance ( i , TensorConstant ) : <EOL> keys_copy = list ( storage_map . keys ( ) ) [ : ] <EOL> keys_copy . remove ( i ) <EOL> for o in keys_copy : <EOL> if ( storage_map [ i ] [ <NUM_LIT:0> ] and <EOL> storage_map [ i ] [ <NUM_LIT:0> ] is storage_map [ o ] [ <NUM_LIT:0> ] ) : <EOL> return [ True , storage_map [ o ] [ <NUM_LIT:0> ] ] <EOL> return [ False , None ] <EOL> assert check_storage ( storage_map ) [ <NUM_LIT:0> ] <EOL> assert len ( set ( id ( v ) for v in <EOL> itervalues ( storage_map ) ) ) < len ( storage_map ) </s>
