#NEWLINE# def calculate_reallocate_info( order , fgraph , storage_map , compute_map_re , dependencies ) : #NEWLINE# #INDENT# reallocated_info = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# viewed_by = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for var in fgraph . variables : #NEWLINE# #INDENT# viewed_by [ var ] = [ ] #NEWLINE# #UNINDENT# view_of = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# pre_allocated = set ( [ ] ) #NEWLINE# allocated = set ( [ ] ) #NEWLINE# for idx in range ( len ( order ) ) : #NEWLINE# #INDENT# node = order [ idx ] #NEWLINE# dmap = getattr ( node . op , 'destroy_map' , None ) #NEWLINE# vmap = getattr ( node . op , 'view_map' , None ) #NEWLINE# idx_o = 0 #NEWLINE# for out in node . outputs : #NEWLINE# #INDENT# for var in node . outputs : #NEWLINE# #INDENT# compute_map_re [ var ] [ 0 ] = 1 #NEWLINE# #UNINDENT# ins = None #NEWLINE# if ( dmap and ( idx_o in dmap ) ) : #NEWLINE# #INDENT# idx_v = dmap [ idx_o ] #NEWLINE# assert ( len ( idx_v ) == 1 ) , 'Here we only support the possibility to destroy one input' #NEWLINE# ins = node . inputs [ idx_v [ 0 ] ] #NEWLINE# #UNINDENT# if ( vmap and ( idx_o in vmap ) ) : #NEWLINE# #INDENT# assert ( ins is None ) #NEWLINE# idx_v = vmap [ idx_o ] #NEWLINE# assert ( len ( idx_v ) == 1 ) , 'Here we only support the possibility to view one input' #NEWLINE# ins = node . inputs [ idx_v [ 0 ] ] #NEWLINE# #UNINDENT# if ( ins is not None ) : #NEWLINE# #INDENT# assert isinstance ( ins , theano . Variable ) #NEWLINE# origin = view_of . get ( ins , ins ) #NEWLINE# view_of [ out ] = origin #NEWLINE# viewed_by [ origin ] . append ( out ) #NEWLINE# #UNINDENT# idx_o += 1 #NEWLINE# #UNINDENT# for ins in node . inputs : #NEWLINE# #INDENT# assert ( not ( ( ins in view_of ) and viewed_by [ ins ] ) ) #NEWLINE# if ( ( getattr ( ins , 'ndim' , None ) == 0 ) and ( not storage_map [ ins ] [ 0 ] ) and ( ins not in fgraph . outputs ) and ins . owner and all ( [ compute_map_re [ v ] [ 0 ] for v in dependencies . get ( ins , [ ] ) ] ) and ( ins not in allocated ) ) : #NEWLINE# #INDENT# reuse_out = None #NEWLINE# if ( ( ins not in view_of ) and ( not viewed_by . get ( ins , [ ] ) ) ) : #NEWLINE# #INDENT# for i in range ( ( idx + 1 ) , len ( order ) ) : #NEWLINE# #INDENT# if ( reuse_out is not None ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# for out in order [ i ] . outputs : #NEWLINE# #INDENT# if ( ( getattr ( out , 'ndim' , None ) == 0 ) and ( out not in pre_allocated ) and ( ins . type == out . type ) ) : #NEWLINE# #INDENT# reuse_out = out #NEWLINE# pre_allocated . add ( out ) #NEWLINE# allocated . add ( ins ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ins in view_of ) : #NEWLINE# #INDENT# origin = view_of [ ins ] #NEWLINE# if ( ins in viewed_by [ origin ] ) : #NEWLINE# #INDENT# viewed_by [ origin ] . remove ( ins ) #NEWLINE# #UNINDENT# if ( ( not viewed_by [ origin ] ) and ( origin not in fgraph . inputs ) and ( not isinstance ( origin , theano . Constant ) ) ) : #NEWLINE# #INDENT# for i in range ( ( idx + 1 ) , len ( order ) ) : #NEWLINE# #INDENT# if ( reuse_out is not None ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# for out in order [ i ] . outputs : #NEWLINE# #INDENT# if ( ( getattr ( out , 'ndim' , None ) == 0 ) and ( out not in pre_allocated ) and ( ins . type == out . type ) ) : #NEWLINE# #INDENT# reuse_out = out #NEWLINE# pre_allocated . add ( out ) #NEWLINE# allocated . add ( ins ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( reuse_out is not None ) : #NEWLINE# #INDENT# reallocated_info [ ins ] = [ ins , reuse_out ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return reallocated_info