<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> from . import link <EOL> from collections import defaultdict <EOL> import logging <EOL> import os <EOL> import sys <EOL> import time <EOL> import warnings <EOL> from theano . configparser import ( config , _config_var_list ) <EOL> import theano . gof . cmodule <EOL> from six import iteritems , itervalues <EOL> from six . moves import xrange <EOL> logger = logging . getLogger ( __name__ ) <EOL> def calculate_reallocate_info ( order , fgraph , storage_map , compute_map_re , <EOL> dependencies ) : <EOL> reallocated_info = { } <EOL> viewed_by = { } <EOL> for var in fgraph . variables : <EOL> viewed_by [ var ] = [ ] <EOL> view_of = { } <EOL> pre_allocated = set ( [ ] ) <EOL> allocated = set ( [ ] ) <EOL> for idx in range ( len ( order ) ) : <EOL> node = order [ idx ] <EOL> dmap = getattr ( node . op , '<STR_LIT>' , None ) <EOL> vmap = getattr ( node . op , '<STR_LIT>' , None ) <EOL> idx_o = <NUM_LIT:0> <EOL> for out in node . outputs : <EOL> for var in node . outputs : <EOL> compute_map_re [ var ] [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> ins = None <EOL> if dmap and idx_o in dmap : <EOL> idx_v = dmap [ idx_o ] <EOL> assert len ( idx_v ) == <NUM_LIT:1> , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ins = node . inputs [ idx_v [ <NUM_LIT:0> ] ] <EOL> if vmap and idx_o in vmap : <EOL> assert ins is None <EOL> idx_v = vmap [ idx_o ] <EOL> assert len ( idx_v ) == <NUM_LIT:1> , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ins = node . inputs [ idx_v [ <NUM_LIT:0> ] ] <EOL> if ins is not None : <EOL> assert isinstance ( ins , theano . Variable ) <EOL> origin = view_of . get ( ins , ins ) <EOL> view_of [ out ] = origin <EOL> viewed_by [ origin ] . append ( out ) <EOL> idx_o += <NUM_LIT:1> <EOL> for ins in node . inputs : <EOL> assert not ( ins in view_of and viewed_by [ ins ] ) <EOL> if ( getattr ( ins , '<STR_LIT>' , None ) == <NUM_LIT:0> and not storage_map [ ins ] [ <NUM_LIT:0> ] and <EOL> ins not in fgraph . outputs and ins . owner and <EOL> all ( [ compute_map_re [ v ] [ <NUM_LIT:0> ] <EOL> for v in dependencies . get ( ins , [ ] ) ] ) and <EOL> ins not in allocated ) : <EOL> reuse_out = None <EOL> if ins not in view_of and not viewed_by . get ( ins , [ ] ) : <EOL> for i in range ( idx + <NUM_LIT:1> , len ( order ) ) : <EOL> if reuse_out is not None : <EOL> break <EOL> for out in order [ i ] . outputs : <EOL> if ( getattr ( out , '<STR_LIT>' , None ) == <NUM_LIT:0> and <EOL> out not in pre_allocated and <EOL> ins . type == out . type ) : <EOL> reuse_out = out <EOL> pre_allocated . add ( out ) <EOL> allocated . add ( ins ) <EOL> break <EOL> elif ins in view_of : <EOL> origin = view_of [ ins ] <EOL> if ins in viewed_by [ origin ] : <EOL> viewed_by [ origin ] . remove ( ins ) <EOL> if ( not viewed_by [ origin ] and <EOL> origin not in fgraph . inputs and <EOL> not isinstance ( origin , theano . Constant ) ) : <EOL> for i in range ( idx + <NUM_LIT:1> , len ( order ) ) : <EOL> if reuse_out is not None : <EOL> break <EOL> for out in order [ i ] . outputs : <EOL> if ( getattr ( out , '<STR_LIT>' , None ) == <NUM_LIT:0> and <EOL> out not in pre_allocated and <EOL> ins . type == out . type ) : <EOL> reuse_out = out <EOL> pre_allocated . add ( out ) <EOL> allocated . add ( ins ) <EOL> break <EOL> if reuse_out is not None : <EOL> reallocated_info [ ins ] = [ ins , reuse_out ] <EOL> return reallocated_info <EOL> class VM ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nodes , thunks , pre_call_clear ) : <EOL> if len ( nodes ) != len ( thunks ) : <EOL> raise ValueError ( ) <EOL> self . nodes = nodes <EOL> self . thunks = thunks <EOL> self . pre_call_clear = pre_call_clear <EOL> self . call_counts = [ <NUM_LIT:0> ] * len ( nodes ) <EOL> self . call_times = [ <NUM_LIT:0> ] * len ( nodes ) <EOL> self . time_thunks = False <EOL> self . need_update_inputs = True <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def clear_storage ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def update_profile ( self , profile ) : <EOL> for node , thunk , t , c in zip ( self . nodes , self . thunks , <EOL> self . call_times , self . call_counts ) : <EOL> profile . apply_time . setdefault ( node , <NUM_LIT:0.0> ) <EOL> profile . apply_time [ node ] += t <EOL> profile . apply_callcount . setdefault ( node , <NUM_LIT:0> ) <EOL> profile . apply_callcount [ node ] += c <EOL> profile . apply_cimpl [ node ] = hasattr ( thunk , '<STR_LIT>' ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> profile . variable_shape = self . variable_shape . copy ( ) <EOL> profile . variable_strides = self . variable_strides . copy ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> profile . node_executed_order = self . node_executed_order [ : ] <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> profile . node_cleared_order = self . node_cleared_order [ : ] <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> profile . dependencies = self . dependencies <EOL> for i in xrange ( len ( self . call_times ) ) : <EOL> self . call_times [ i ] = <NUM_LIT:0.0> <EOL> self . call_counts [ i ] = <NUM_LIT:0> <EOL> class Loop ( VM ) : <EOL> """<STR_LIT>""" <EOL> allow_gc = False <EOL> def __call__ ( self ) : <EOL> if self . time_thunks : <EOL> for cont in self . pre_call_clear : <EOL> cont [ <NUM_LIT:0> ] = None <EOL> try : <EOL> for i , ( thunk , node ) in enumerate ( zip ( self . thunks , <EOL> self . nodes ) ) : <EOL> t0 = time . time ( ) <EOL> thunk ( ) <EOL> t1 = time . time ( ) <EOL> self . call_counts [ i ] += <NUM_LIT:1> <EOL> self . call_times [ i ] += t1 - t0 <EOL> except : <EOL> link . raise_with_op ( node , thunk ) <EOL> else : <EOL> for cont in self . pre_call_clear : <EOL> cont [ <NUM_LIT:0> ] = None <EOL> try : <EOL> for thunk , node in zip ( self . thunks , self . nodes ) : <EOL> thunk ( ) <EOL> except : <EOL> link . raise_with_op ( node , thunk ) <EOL> class LoopGC ( VM ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nodes , thunks , pre_call_clear , post_thunk_clear ) : <EOL> super ( LoopGC , self ) . __init__ ( nodes , thunks , pre_call_clear ) <EOL> self . post_thunk_clear = post_thunk_clear <EOL> self . allow_gc = True <EOL> if not ( len ( nodes ) == len ( thunks ) == len ( post_thunk_clear ) ) : <EOL> raise ValueError ( ) <EOL> def __call__ ( self ) : <EOL> if self . time_thunks : <EOL> for cont in self . pre_call_clear : <EOL> cont [ <NUM_LIT:0> ] = None <EOL> try : <EOL> i = <NUM_LIT:0> <EOL> for thunk , node , old_storage in zip ( self . thunks , <EOL> self . nodes , <EOL> self . post_thunk_clear ) : <EOL> t0 = time . time ( ) <EOL> thunk ( ) <EOL> t1 = time . time ( ) <EOL> self . call_counts [ i ] += <NUM_LIT:1> <EOL> self . call_times [ i ] += t1 - t0 <EOL> for old_s in old_storage : <EOL> old_s [ <NUM_LIT:0> ] = None <EOL> i += <NUM_LIT:1> <EOL> except : <EOL> link . raise_with_op ( node , thunk ) <EOL> else : <EOL> for cont in self . pre_call_clear : <EOL> cont [ <NUM_LIT:0> ] = None <EOL> try : <EOL> for thunk , node , old_storage in zip ( self . thunks , self . nodes , <EOL> self . post_thunk_clear ) : <EOL> thunk ( ) <EOL> for old_s in old_storage : <EOL> old_s [ <NUM_LIT:0> ] = None <EOL> except : <EOL> link . raise_with_op ( node , thunk ) <EOL> class Stack ( VM ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nodes , thunks , pre_call_clear , <EOL> storage_map , compute_map , fgraph , allow_gc , <EOL> dependencies = None , callback = None ) : <EOL> super ( Stack , self ) . __init__ ( nodes , thunks , pre_call_clear ) <EOL> self . allow_gc = allow_gc <EOL> self . message = "<STR_LIT>" <EOL> self . base_apply_stack = [ o . owner for o in fgraph . outputs if o . owner ] <EOL> self . outputs = fgraph . outputs <EOL> self . storage_map = storage_map <EOL> self . variable_shape = { } <EOL> self . variable_strides = { } <EOL> self . compute_map = compute_map <EOL> self . node_idx = node_idx = { } <EOL> self . callback = callback <EOL> ords = fgraph . orderings ( ) <EOL> for i , node in enumerate ( self . nodes ) : <EOL> node_idx [ node ] = i <EOL> node . destroy_dependencies = [ ] <EOL> if node in ords : <EOL> for prereq in ords [ node ] : <EOL> node . destroy_dependencies += prereq . outputs <EOL> self . dependencies = dependencies <EOL> if self . allow_gc and self . dependencies is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def run_thunk_of_node ( self , node ) : <EOL> """<STR_LIT>""" <EOL> idx = self . node_idx [ node ] <EOL> t0 = time . time ( ) <EOL> rval = self . thunks [ idx ] ( ) <EOL> self . node_executed_order . append ( node ) <EOL> dt = max ( time . time ( ) - t0 , <NUM_LIT> ) <EOL> if self . callback is not None : <EOL> self . callback ( <EOL> node = node , <EOL> thunk = self . thunks [ idx ] , <EOL> storage_map = self . storage_map , <EOL> compute_map = self . compute_map , <EOL> ) <EOL> return rval , dt <EOL> def __call__ ( self , output_subset = None ) : <EOL> storage_map = self . storage_map <EOL> compute_map = self . compute_map <EOL> thunks = self . thunks <EOL> dependencies = self . dependencies <EOL> self . node_executed_order = [ ] <EOL> self . node_cleared_order = [ ] <EOL> for k in self . storage_map : <EOL> compute_map [ k ] [ <NUM_LIT:0> ] = ( k . owner is None ) <EOL> if output_subset is not None : <EOL> apply_stack = [ self . outputs [ i ] . owner for i in output_subset <EOL> if self . outputs [ i ] . owner ] <EOL> else : <EOL> apply_stack = list ( self . base_apply_stack ) <EOL> last_apply_stack_len = - <NUM_LIT:1> <EOL> for var , data in iteritems ( self . storage_map ) : <EOL> if data [ <NUM_LIT:0> ] is None : <EOL> continue <EOL> if hasattr ( var . type , '<STR_LIT>' ) : <EOL> sh = var . type . get_shape_info ( data [ <NUM_LIT:0> ] ) <EOL> else : <EOL> sh = '<STR_LIT>' <EOL> self . variable_shape [ var ] = sh <EOL> st = getattr ( data [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if getattr ( data [ <NUM_LIT:0> ] , '<STR_LIT>' , False ) and data [ <NUM_LIT:0> ] . flags . c_contiguous : <EOL> st = '<STR_LIT:c>' <EOL> elif ( hasattr ( data [ <NUM_LIT:0> ] , '<STR_LIT>' ) and <EOL> data [ <NUM_LIT:0> ] . is_c_contiguous ( ) ) : <EOL> st = "<STR_LIT:c>" <EOL> self . variable_strides [ var ] = st <EOL> while apply_stack : <EOL> apply_stack_len = len ( apply_stack ) <EOL> assert apply_stack_len != last_apply_stack_len <EOL> last_apply_stack_len = apply_stack_len <EOL> current_apply = apply_stack . pop ( ) <EOL> current_inputs = current_apply . inputs <EOL> current_outputs = current_apply . outputs <EOL> current_deps = current_inputs + current_apply . destroy_dependencies <EOL> computed_ins = all ( compute_map [ v ] [ <NUM_LIT:0> ] for v in current_deps ) <EOL> computed_outs = all ( compute_map [ v ] [ <NUM_LIT:0> ] for v in current_outputs ) <EOL> if not thunks [ self . node_idx [ current_apply ] ] . lazy : <EOL> if computed_ins and not computed_outs : <EOL> try : <EOL> _ , dt = self . run_thunk_of_node ( current_apply ) <EOL> del _ <EOL> if config . profile : <EOL> current_idx = self . node_idx [ current_apply ] <EOL> self . call_counts [ current_idx ] += <NUM_LIT:1> <EOL> self . call_times [ current_idx ] += dt <EOL> for ( idx , o ) in enumerate ( <EOL> thunks [ self . node_idx [ <EOL> current_apply ] ] . outputs ) : <EOL> var = self . nodes [ current_idx ] . outputs [ idx ] <EOL> if hasattr ( var . type , '<STR_LIT>' ) : <EOL> sh = var . type . get_shape_info ( o [ <NUM_LIT:0> ] ) <EOL> else : <EOL> sh = '<STR_LIT>' <EOL> self . variable_shape [ var ] = sh <EOL> st = getattr ( o [ <NUM_LIT:0> ] , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if ( getattr ( o [ <NUM_LIT:0> ] , '<STR_LIT>' , False ) and <EOL> o [ <NUM_LIT:0> ] . flags . c_contiguous ) : <EOL> st = '<STR_LIT:c>' <EOL> elif ( hasattr ( data [ <NUM_LIT:0> ] , '<STR_LIT>' ) and <EOL> data [ <NUM_LIT:0> ] . is_c_contiguous ( ) ) : <EOL> st = "<STR_LIT:c>" <EOL> self . variable_strides [ var ] = st <EOL> except Exception : <EOL> link . raise_with_op ( <EOL> current_apply , <EOL> self . thunks [ self . node_idx [ current_apply ] ] , <EOL> storage_map = storage_map ) <EOL> for o in current_apply . outputs : <EOL> compute_map [ o ] [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> input_index = [ ] <EOL> if self . allow_gc : <EOL> for i in current_apply . inputs : <EOL> if ( dependencies [ i ] and <EOL> i . owner and <EOL> i not in self . outputs ) : <EOL> if all ( compute_map [ v ] [ <NUM_LIT:0> ] <EOL> for v in dependencies [ i ] ) : <EOL> storage_map [ i ] [ <NUM_LIT:0> ] = None <EOL> input_index . append ( <EOL> current_apply . inputs . index ( i ) ) <EOL> compute_map [ i ] [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> if ( config . warn . vm_gc_bug and <EOL> current_apply in apply_stack and <EOL> getattr ( current_apply . op , <EOL> '<STR_LIT>' , <EOL> False ) ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> stacklevel = <NUM_LIT:3> <EOL> ) <EOL> self . node_cleared_order . append ( input_index ) <EOL> elif not computed_ins : <EOL> apply_stack . append ( current_apply ) <EOL> apply_stack . extend ( inp . owner <EOL> for inp in current_deps <EOL> if inp . owner ) <EOL> elif not computed_outs : <EOL> try : <EOL> requires , dt = self . run_thunk_of_node ( current_apply ) <EOL> current_idx = self . node_idx [ current_apply ] <EOL> self . call_counts [ current_idx ] += <NUM_LIT:1> <EOL> self . call_times [ current_idx ] += dt <EOL> except Exception : <EOL> link . raise_with_op ( <EOL> current_apply , <EOL> self . thunks [ self . node_idx [ current_apply ] ] , <EOL> storage_map = storage_map ) <EOL> if requires : <EOL> for r in requires : <EOL> apply_stack . append ( current_apply ) <EOL> if current_apply . inputs [ r ] . owner : <EOL> apply_stack . append ( current_apply . inputs [ r ] . owner ) <EOL> else : <EOL> if config . profile : <EOL> for ( idx , o ) in enumerate ( thunks [ <EOL> self . node_idx [ current_apply ] ] . outputs ) : <EOL> var = self . nodes [ <EOL> self . node_idx [ current_apply ] ] . outputs [ idx ] <EOL> if hasattr ( var . type , '<STR_LIT>' ) : <EOL> sh = var . type . get_shape_info ( o [ <NUM_LIT:0> ] ) <EOL> else : <EOL> sh = '<STR_LIT>' <EOL> self . variable_shape [ var ] = sh <EOL> st = getattr ( o [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( getattr ( o [ <NUM_LIT:0> ] , '<STR_LIT>' , False ) and <EOL> o [ <NUM_LIT:0> ] . flags . c_contiguous ) : <EOL> st = '<STR_LIT:c>' <EOL> elif ( hasattr ( data [ <NUM_LIT:0> ] , '<STR_LIT>' ) and <EOL> data [ <NUM_LIT:0> ] . is_c_contiguous ( ) ) : <EOL> st = "<STR_LIT:c>" <EOL> self . variable_strides [ var ] = st <EOL> input_index = [ ] <EOL> if self . allow_gc : <EOL> for i in current_apply . inputs : <EOL> if ( dependencies [ i ] and i . owner and <EOL> i not in self . outputs ) : <EOL> empty_storage_map = True <EOL> for x in dependencies [ i ] : <EOL> if not compute_map [ x ] [ <NUM_LIT:0> ] : <EOL> empty_storage_map = False <EOL> break <EOL> if empty_storage_map : <EOL> storage_map [ i ] [ <NUM_LIT:0> ] = None <EOL> input_index . append ( <EOL> current_apply . inputs . index ( i ) ) <EOL> compute_map [ i ] [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> self . node_cleared_order . append ( input_index ) <EOL> final_index = [ ] <EOL> if self . allow_gc : <EOL> for v in storage_map : <EOL> if v . owner and v not in self . outputs : <EOL> if compute_map [ v ] [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> continue <EOL> else : <EOL> storage_map [ v ] [ <NUM_LIT:0> ] = None <EOL> final_index . append ( v ) <EOL> compute_map [ v ] [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> self . node_cleared_order . append ( final_index ) <EOL> try : <EOL> from . import lazylinker_c <EOL> class CVM ( lazylinker_c . CLazyLinker , VM ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> lazylinker_c . CLazyLinker . __init__ ( self , * args , ** kwargs ) <EOL> except ImportError : <EOL> pass <EOL> except ( OSError , theano . gof . cmodule . MissingGXX ) as e : <EOL> assert not [ x for x in _config_var_list <EOL> if x . fullname == '<STR_LIT>' ] [ <NUM_LIT:0> ] . default . startswith ( '<STR_LIT>' ) , e <EOL> pass <EOL> class VM_Linker ( link . LocalLinker ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , allow_gc = None , use_cloop = False , callback = None , <EOL> lazy = None , schedule = None , c_thunks = None , <EOL> allow_partial_eval = None ) : <EOL> if allow_gc is None : <EOL> allow_gc = config . allow_gc <EOL> self . fgraph = None <EOL> self . allow_gc = allow_gc <EOL> self . use_cloop = use_cloop <EOL> self . callback = callback <EOL> self . lazy = lazy <EOL> self . c_thunks = c_thunks <EOL> self . allow_partial_eval = allow_partial_eval <EOL> self . updated_vars = { } <EOL> if schedule : <EOL> self . schedule = schedule <EOL> def accept ( self , fgraph , no_recycling = None ) : <EOL> """<STR_LIT>""" <EOL> if ( config . profile and <EOL> hasattr ( theano , '<STR_LIT>' ) and <EOL> hasattr ( theano . sandbox , '<STR_LIT>' ) and <EOL> theano . sandbox . cuda . cuda_enabled ) : <EOL> if os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) != '<STR_LIT:1>' : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if no_recycling is None : <EOL> no_recycling = [ ] <EOL> if self . fgraph is not None and self . fgraph is not fgraph : <EOL> return type ( self ) ( <EOL> allow_gc = self . allow_gc , <EOL> use_cloop = self . use_cloop , <EOL> callback = self . callback , <EOL> lazy = self . lazy , <EOL> schedule = self . schedule , <EOL> c_thunks = self . c_thunks , <EOL> ) . accept ( fgraph , no_recycling ) <EOL> self . fgraph = fgraph <EOL> self . no_recycling = no_recycling <EOL> return self <EOL> def accept_var_updates ( self , updated_vars ) : <EOL> self . updated_vars = updated_vars <EOL> def compute_gc_dependencies ( self , variables ) : <EOL> """<STR_LIT>""" <EOL> dependencies = { } <EOL> for k in variables : <EOL> dependencies [ k ] = [ ] <EOL> if k . owner and k . clients : <EOL> ls = [ ] <EOL> for cl in k . clients : <EOL> if cl [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> ls += cl [ <NUM_LIT:0> ] . outputs <EOL> dependencies [ k ] += ls <EOL> return dependencies <EOL> def make_vm ( self , nodes , thunks , <EOL> input_storage , output_storage , storage_map , <EOL> post_thunk_clear , <EOL> computed , <EOL> compute_map , <EOL> updated_vars , <EOL> ) : <EOL> pre_call_clear = [ storage_map [ v ] for v in self . no_recycling ] <EOL> if ( self . callback is not None or <EOL> ( config . profile and config . profile_memory ) or <EOL> getattr ( self , '<STR_LIT>' , False ) ) : <EOL> if self . use_cloop and self . callback is not None : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> if self . use_cloop and config . profile_memory : <EOL> warnings . warn ( <EOL> '<STR_LIT>' ) <EOL> if self . use_cloop and getattr ( self , '<STR_LIT>' , False ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> deps = self . compute_gc_dependencies ( storage_map ) <EOL> vm = Stack ( <EOL> nodes , thunks , pre_call_clear , <EOL> storage_map , compute_map , <EOL> self . fgraph , self . allow_gc , <EOL> dependencies = deps , <EOL> callback = self . callback ) <EOL> elif self . use_cloop : <EOL> nodes_idx = { } <EOL> vars_idx = { } <EOL> for i , node in enumerate ( nodes ) : <EOL> nodes_idx [ node ] = i <EOL> for v in node . inputs + node . outputs : <EOL> vars_idx . setdefault ( v , len ( vars_idx ) ) <EOL> for v in self . fgraph . inputs + self . fgraph . outputs : <EOL> vars_idx . setdefault ( v , len ( vars_idx ) ) <EOL> nodes_idx_inv = { } <EOL> vars_idx_inv = { } <EOL> for ( node , i ) in iteritems ( nodes_idx ) : <EOL> nodes_idx_inv [ i ] = node <EOL> for ( var , i ) in iteritems ( vars_idx ) : <EOL> vars_idx_inv [ i ] = var <EOL> storage_map_list = [ storage_map [ vars_idx_inv [ i ] ] <EOL> for i in xrange ( len ( vars_idx_inv ) ) ] <EOL> compute_map_list = [ compute_map [ vars_idx_inv [ i ] ] <EOL> for i in xrange ( len ( vars_idx_inv ) ) ] <EOL> if nodes : <EOL> assert type ( storage_map_list [ <NUM_LIT:0> ] ) is list <EOL> assert type ( compute_map_list [ <NUM_LIT:0> ] ) is list <EOL> dependency_map = self . compute_gc_dependencies ( storage_map ) <EOL> dependency_map_list = [ <EOL> [ vars_idx [ d ] for d in dependency_map [ vars_idx_inv [ i ] ] ] <EOL> for i in xrange ( len ( vars_idx_inv ) ) ] <EOL> base_input_output_list = [ ] <EOL> node_n_inputs = [ ] <EOL> node_n_outputs = [ ] <EOL> node_input_offset = [ ] <EOL> node_output_offset = [ ] <EOL> for node in nodes : <EOL> inputs_idx = [ vars_idx [ v ] for v in node . inputs ] <EOL> outputs_idx = [ vars_idx [ v ] for v in node . outputs ] <EOL> node_n_inputs . append ( len ( inputs_idx ) ) <EOL> node_n_outputs . append ( len ( outputs_idx ) ) <EOL> node_input_offset . append ( len ( base_input_output_list ) ) <EOL> base_input_output_list . extend ( inputs_idx ) <EOL> node_output_offset . append ( len ( base_input_output_list ) ) <EOL> base_input_output_list . extend ( outputs_idx ) <EOL> var_owner = [ None ] * len ( vars_idx ) <EOL> for ( var , i ) in iteritems ( vars_idx ) : <EOL> if var . owner : <EOL> var_owner [ i ] = nodes_idx [ var . owner ] <EOL> is_lazy_list = [ int ( th . lazy ) for th in thunks ] <EOL> output_vars = [ vars_idx [ v ] for v in self . fgraph . outputs ] <EOL> ords = self . fgraph . orderings ( ) <EOL> node_prereqs = [ ] <EOL> node_output_size = [ ] <EOL> for i , node in enumerate ( nodes ) : <EOL> node_output_size . append ( <NUM_LIT:0> ) <EOL> prereq_var_idxs = [ ] <EOL> for prereq_node in ords . get ( node , [ ] ) : <EOL> prereq_var_idxs . extend ( <EOL> [ vars_idx [ v ] for v in prereq_node . outputs ] ) <EOL> prereq_var_idxs = list ( set ( prereq_var_idxs ) ) <EOL> prereq_var_idxs . sort ( ) <EOL> node_prereqs . append ( prereq_var_idxs ) <EOL> update_storage = [ ] <EOL> update_in_from_out = { } <EOL> for ( ivar , ovar ) in iteritems ( updated_vars ) : <EOL> update_in_from_out [ vars_idx [ ovar ] ] = vars_idx [ ivar ] <EOL> for oidx in output_vars : <EOL> if oidx in update_in_from_out : <EOL> update_storage . append ( update_in_from_out [ oidx ] ) <EOL> c0 = sys . getrefcount ( node_n_inputs ) <EOL> vm = CVM ( <EOL> nodes , <EOL> thunks , <EOL> pre_call_clear , <EOL> allow_gc = self . allow_gc , <EOL> call_counts = [ <NUM_LIT:0> ] * len ( nodes ) , <EOL> call_times = [ <NUM_LIT:0.0> ] * len ( nodes ) , <EOL> compute_map_list = compute_map_list , <EOL> storage_map_list = storage_map_list , <EOL> base_input_output_list = base_input_output_list , <EOL> node_n_inputs = node_n_inputs , <EOL> node_n_outputs = node_n_outputs , <EOL> node_input_offset = node_input_offset , <EOL> node_output_offset = node_output_offset , <EOL> var_owner = var_owner , <EOL> is_lazy_list = is_lazy_list , <EOL> output_vars = output_vars , <EOL> node_prereqs = node_prereqs , <EOL> node_output_size = node_output_size , <EOL> update_storage = update_storage , <EOL> dependencies = dependency_map_list , <EOL> ) <EOL> assert c0 == sys . getrefcount ( node_n_inputs ) <EOL> else : <EOL> lazy = self . lazy <EOL> if lazy is None : <EOL> lazy = config . vm . lazy <EOL> if lazy is None : <EOL> lazy = not all ( [ ( not th . lazy ) for th in thunks ] ) <EOL> if not lazy : <EOL> if self . allow_gc : <EOL> vm = LoopGC ( <EOL> nodes , <EOL> thunks , <EOL> pre_call_clear , <EOL> post_thunk_clear , <EOL> ) <EOL> else : <EOL> vm = Loop ( <EOL> nodes , <EOL> thunks , <EOL> pre_call_clear , <EOL> ) <EOL> else : <EOL> deps = self . compute_gc_dependencies ( storage_map ) <EOL> vm = Stack ( <EOL> nodes , thunks , pre_call_clear , <EOL> storage_map , compute_map , <EOL> self . fgraph , self . allow_gc , <EOL> dependencies = deps <EOL> ) <EOL> return vm <EOL> def make_all ( self , profiler = None , input_storage = None , <EOL> output_storage = None , storage_map = None , <EOL> ) : <EOL> fgraph = self . fgraph <EOL> order = self . schedule ( fgraph ) <EOL> no_recycling = self . no_recycling <EOL> input_storage , output_storage , storage_map = link . map_storage ( <EOL> fgraph , order , input_storage , output_storage , storage_map ) <EOL> compute_map = { } <EOL> for k in storage_map : <EOL> compute_map [ k ] = [ k . owner is None ] <EOL> thunks = [ ] <EOL> compute_map_re = defaultdict ( lambda : [ <NUM_LIT:0> ] ) <EOL> for var in fgraph . inputs : <EOL> compute_map_re [ var ] [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> if getattr ( fgraph . profile , '<STR_LIT>' , None ) : <EOL> dependencies = getattr ( fgraph . profile , '<STR_LIT>' ) <EOL> else : <EOL> dependencies = self . compute_gc_dependencies ( storage_map ) <EOL> reallocated_info = calculate_reallocate_info ( <EOL> order , fgraph , storage_map , compute_map_re , dependencies ) <EOL> for node in order : <EOL> try : <EOL> if self . c_thunks is False : <EOL> node . op . _op_use_c_code = False <EOL> thunks . append ( node . op . make_thunk ( node , <EOL> storage_map , <EOL> compute_map , <EOL> no_recycling ) ) <EOL> if not hasattr ( thunks [ - <NUM_LIT:1> ] , '<STR_LIT>' ) : <EOL> thunks [ - <NUM_LIT:1> ] . lazy = False <EOL> except Exception as e : <EOL> e . args = ( "<STR_LIT>" <EOL> "<STR_LIT>" , node , "<STR_LIT:\n>" ) + e . args <EOL> raise <EOL> for node , thunk in zip ( order , thunks ) : <EOL> thunk . inputs = [ storage_map [ v ] for v in node . inputs ] <EOL> thunk . outputs = [ storage_map [ v ] for v in node . outputs ] <EOL> lazy = self . lazy <EOL> if lazy is None : <EOL> lazy = config . vm . lazy <EOL> if lazy is None : <EOL> lazy = not all ( [ ( not th . lazy ) for th in thunks ] ) <EOL> if not ( lazy or ( config . profile and config . profile_memory ) or <EOL> self . use_cloop or self . callback ) : <EOL> for pair in itervalues ( reallocated_info ) : <EOL> storage_map [ pair [ <NUM_LIT:1> ] ] = storage_map [ pair [ <NUM_LIT:0> ] ] <EOL> computed , last_user = link . gc_helper ( order ) <EOL> if self . allow_gc : <EOL> post_thunk_clear = [ ] <EOL> for node in order : <EOL> clear_after_this_thunk = [ ] <EOL> for input in node . inputs : <EOL> if ( input in computed and <EOL> input not in fgraph . outputs and <EOL> node == last_user [ input ] and <EOL> input not in reallocated_info ) : <EOL> clear_after_this_thunk . append ( storage_map [ input ] ) <EOL> post_thunk_clear . append ( clear_after_this_thunk ) <EOL> else : <EOL> post_thunk_clear = None <EOL> vm = self . make_vm ( order , thunks , <EOL> input_storage , output_storage , storage_map , <EOL> post_thunk_clear , <EOL> computed , <EOL> compute_map , <EOL> self . updated_vars , <EOL> ) <EOL> vm . storage_map = storage_map <EOL> return ( vm , <EOL> [ link . Container ( input , storage ) <EOL> for input , storage in zip ( fgraph . inputs , input_storage ) ] , <EOL> [ link . Container ( output , storage , True ) <EOL> for output , storage in zip ( fgraph . outputs , output_storage ) ] , <EOL> thunks , <EOL> order ) <EOL> def __setstate__ ( self , d ) : <EOL> self . __dict__ . update ( d ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . c_thunks = True </s>
