theano/compile/profiling.py
def count_running_memory(order, fgraph, nodes_mem, ignore_dmap=False):
            """
            Calculate memory with specific node order.

            Return a list including the following values
            1.  node_memory_size
                Sum of the size of all variables that actually allocate
                memory (excluding views, and inplace).
            2.  running_memory_size
                The memory allocated after the current apply node.
            3.  running_max_memory_size
                The maximum of running_memory_size during the function.
            4.  node_memory_saved_by_view
                The sum of memory saved by returning view instead of new
                allocation.
            5.  node_memory_saved_by_inplace
                The sum of memory saved by reusing the input instead of
                new allocation.

            """
            from theano.gpuarray import GpuArrayType
            # Initial Mem info values [CPU, GPU]
            node_memory_size = [0, 0]
            running_memory_size = [0, 0]
            running_max_memory_size = [0, 0]
            node_memory_saved_by_view = 0
            node_memory_saved_by_inplace = 0
            # This take only the inputs/outputs dependencies.
            dependencies = fgraph.profile.dependencies

            # Initial compute_map which is used to check if a node is valid
            compute_map = defaultdict(lambda: [0])
            for var in fgraph.inputs:
                compute_map[var][0] = 1

            # two data structure used to mimic Python gc
            viewed_by = {}  # {var1: [vars that view var1]}
            # The len of the list is the value of python ref
            # count. But we use a list, not just the ref count value.
            # This is more safe to help detect potential bug  in the algo
            for var in fgraph.variables:
                viewed_by[var] = []
            view_of = {}  # {var1: original var viewed by var1}
            # The orignal mean that we don't keep trac of all the intermediate
            # relationship in the view.

            for node in order:
                for var in node.outputs:
                    compute_map[var][0] = 1
                idx = 0
                if ignore_dmap:
                    dmap = None
                else:
                    dmap = getattr(node.op, 'destroy_map', None)
                vmap = getattr(node.op, 'view_map', None)
                val = nodes_mem[node]

                for v in val:
                    # TODO check the op returned a view
                    if dmap and idx in dmap:
                        node_memory_saved_by_inplace += v
                    # TODO check the op returned a view
                    elif vmap and idx in vmap:
                        node_memory_saved_by_view += v
                    idx += 1

                # Update the Python emulating dicts and add the memory
                # allocated by the node
                idx2 = 0
                for out in node.outputs:
                    if isinstance(out.type, GpuArrayType):
                        cg = 1
                    else:
                        cg = 0
                    ins = None
                    if dmap and idx2 in dmap:
                        vidx = dmap[idx2]
                        assert len(vidx) == 1, ("Here we only support the "
                                                "possibility to destroy one "
                                                "input")
                        ins = node.inputs[vidx[0]]
                    if vmap and idx2 in vmap:
                        assert ins is None
                        vidx = vmap[idx2]
                        assert len(vidx) == 1, ("Here we only support the "
                                                "possibility to view one "
                                                "input")
                        ins = node.inputs[vidx[0]]
                    if ins is not None:
                        # This is needed for destroy_map in case it
                        # return a partial view that is destroyed.  So
                        # the output could be different then the
                        # input.
                        assert isinstance(ins, theano.Variable)
                        # we keep trac of view only again the origin
                        origin = view_of.get(ins, ins)
                        view_of[out] = origin
                        viewed_by[origin].append(out)
                    else:
                        running_memory_size[cg] += var_mem[out]
                        node_memory_size[cg] += var_mem[out]
                    idx2 += 1

                running_max_memory_size[0] = max(running_max_memory_size[0],
                                                 running_memory_size[0])
                running_max_memory_size[1] = max(running_max_memory_size[1],
                                                 running_memory_size[1])

                # Mimic the combination of Theano and Python gc
                for ins in set(node.inputs):
                    assert not (ins in view_of and viewed_by[ins])
                    # we trac the original var, so this shouldn't happen
                    if isinstance(ins.type, GpuArrayType):
                        cg = 1
                    else:
                        cg = 0
                    if (dependencies[ins] and
                            ins not in fgraph.outputs and
                            ins.owner and
                            all([compute_map[v][0]
                                 for v in dependencies[ins]])):
                        if ins not in view_of and not viewed_by.get(ins, []):
                            running_memory_size[cg] -= var_mem[ins]
                        elif ins in view_of:
                            origin = view_of[ins]
                            viewed_by[origin].remove(ins)
                            if (not viewed_by[origin] and
                                    origin not in fgraph.inputs and
                                    not isinstance(origin, theano.Constant)):
                                running_memory_size[cg] -= var_mem[origin]
                    else:
                        # ins is viewed_by something else, so its
                        # memory isn't freed
                        pass

            return [node_memory_size, running_memory_size,
                    running_max_memory_size, node_memory_saved_by_inplace,
                    node_memory_saved_by_view]

            Calculate memory with specific node order.

            Return a list including the following values
            1.  node_memory_size
                Sum of the size of all variables that actually allocate
                memory (excluding views, and inplace).
            2.  running_memory_size
                The memory allocated after the current apply node.
            3.  running_max_memory_size
                The maximum of running_memory_size during the function.
            4.  node_memory_saved_by_view
                The sum of memory saved by returning view instead of new
                allocation.
            5.  node_memory_saved_by_inplace
                The sum of memory saved by reusing the input instead of
                new allocation.

            
Calculate memory with specific node order.
Return a list including the following values
1.  node_memory_size
Sum of the size of all variables that actually allocate
memory (excluding views, and inplace).
2.  running_memory_size
The memory allocated after the current apply node.
3.  running_max_memory_size
The maximum of running_memory_size during the function.
4.  node_memory_saved_by_view
The sum of memory saved by returning view instead of new
allocation.
5.  node_memory_saved_by_inplace
The sum of memory saved by reusing the input instead of
new allocation.
Calculate memory with specific node order . Return a list including the following values 1 . node_memory_size Sum of the size of all variables that actually allocate memory ( excluding views and inplace ) . 2 . running_memory_size The memory allocated after the current apply node . 3 . running_max_memory_size The maximum of running_memory_size during the function . 4 . node_memory_saved_by_view The sum of memory saved by returning view instead of new allocation . 5 . node_memory_saved_by_inplace The sum of memory saved by reusing the input instead of new allocation .
def count_running_memory(order, fgraph, nodes_mem, ignore_dmap=False):
            from theano.gpuarray import GpuArrayType
            node_memory_size = [0, 0]
            running_memory_size = [0, 0]
            running_max_memory_size = [0, 0]
            node_memory_saved_by_view = 0
            node_memory_saved_by_inplace = 0
            dependencies = fgraph.profile.dependencies
            compute_map = defaultdict(lambda: [0])
            for var in fgraph.inputs:
                compute_map[var][0] = 1
            viewed_by = {}  
            for var in fgraph.variables:
                viewed_by[var] = []
            view_of = {}  
            for node in order:
                for var in node.outputs:
                    compute_map[var][0] = 1
                idx = 0
                if ignore_dmap:
                    dmap = None
                else:
                    dmap = getattr(node.op, 'destroy_map', None)
                vmap = getattr(node.op, 'view_map', None)
                val = nodes_mem[node]
                for v in val:
                    if dmap and idx in dmap:
                        node_memory_saved_by_inplace += v
                    elif vmap and idx in vmap:
                        node_memory_saved_by_view += v
                    idx += 1
                idx2 = 0
                for out in node.outputs:
                    if isinstance(out.type, GpuArrayType):
                        cg = 1
                    else:
                        cg = 0
                    ins = None
                    if dmap and idx2 in dmap:
                        vidx = dmap[idx2]
                        assert len(vidx) == 1, ("Here we only support the "
                                                "possibility to destroy one "
                                                "input")
                        ins = node.inputs[vidx[0]]
                    if vmap and idx2 in vmap:
                        assert ins is None
                        vidx = vmap[idx2]
                        assert len(vidx) == 1, ("Here we only support the "
                                                "possibility to view one "
                                                "input")
                        ins = node.inputs[vidx[0]]
                    if ins is not None:
                        assert isinstance(ins, theano.Variable)
                        origin = view_of.get(ins, ins)
                        view_of[out] = origin
                        viewed_by[origin].append(out)
                    else:
                        running_memory_size[cg] += var_mem[out]
                        node_memory_size[cg] += var_mem[out]
                    idx2 += 1
                running_max_memory_size[0] = max(running_max_memory_size[0],
                                                 running_memory_size[0])
                running_max_memory_size[1] = max(running_max_memory_size[1],
                                                 running_memory_size[1])
                for ins in set(node.inputs):
                    assert not (ins in view_of and viewed_by[ins])
                    if isinstance(ins.type, GpuArrayType):
                        cg = 1
                    else:
                        cg = 0
                    if (dependencies[ins] and
                            ins not in fgraph.outputs and
                            ins.owner and
                            all([compute_map[v][0]
                                 for v in dependencies[ins]])):
                        if ins not in view_of and not viewed_by.get(ins, []):
                            running_memory_size[cg] -= var_mem[ins]
                        elif ins in view_of:
                            origin = view_of[ins]
                            viewed_by[origin].remove(ins)
                            if (not viewed_by[origin] and
                                    origin not in fgraph.inputs and
                                    not isinstance(origin, theano.Constant)):
                                running_memory_size[cg] -= var_mem[origin]
                    else:
                        pass
            return [node_memory_size, running_memory_size,
                    running_max_memory_size, node_memory_saved_by_inplace,
                    node_memory_saved_by_view]
def count_running_memory ( order , fgraph , nodes_mem , ignore_dmap = False ) : from theano . gpuarray import GpuArrayType node_memory_size = [ 0 , 0 ] running_memory_size = [ 0 , 0 ] running_max_memory_size = [ 0 , 0 ] node_memory_saved_by_view = 0 node_memory_saved_by_inplace = 0 dependencies = fgraph . profile . dependencies compute_map = defaultdict ( lambda : [ 0 ] ) for var in fgraph . inputs : compute_map [ var ] [ 0 ] = 1 viewed_by = { } for var in fgraph . variables : viewed_by [ var ] = [ ] view_of = { } for node in order : for var in node . outputs : compute_map [ var ] [ 0 ] = 1 idx = 0 if ignore_dmap : dmap = None else : dmap = getattr ( node . op , 'destroy_map' , None ) vmap = getattr ( node . op , 'view_map' , None ) val = nodes_mem [ node ] for v in val : if dmap and idx in dmap : node_memory_saved_by_inplace += v elif vmap and idx in vmap : node_memory_saved_by_view += v idx += 1 idx2 = 0 for out in node . outputs : if isinstance ( out . type , GpuArrayType ) : cg = 1 else : cg = 0 ins = None if dmap and idx2 in dmap : vidx = dmap [ idx2 ] assert len ( vidx ) == 1 , ( "Here we only support the " "possibility to destroy one " "input" ) ins = node . inputs [ vidx [ 0 ] ] if vmap and idx2 in vmap : assert ins is None vidx = vmap [ idx2 ] assert len ( vidx ) == 1 , ( "Here we only support the " "possibility to view one " "input" ) ins = node . inputs [ vidx [ 0 ] ] if ins is not None : assert isinstance ( ins , theano . Variable ) origin = view_of . get ( ins , ins ) view_of [ out ] = origin viewed_by [ origin ] . append ( out ) else : running_memory_size [ cg ] += var_mem [ out ] node_memory_size [ cg ] += var_mem [ out ] idx2 += 1 running_max_memory_size [ 0 ] = max ( running_max_memory_size [ 0 ] , running_memory_size [ 0 ] ) running_max_memory_size [ 1 ] = max ( running_max_memory_size [ 1 ] , running_memory_size [ 1 ] ) for ins in set ( node . inputs ) : assert not ( ins in view_of and viewed_by [ ins ] ) if isinstance ( ins . type , GpuArrayType ) : cg = 1 else : cg = 0 if ( dependencies [ ins ] and ins not in fgraph . outputs and ins . owner and all ( [ compute_map [ v ] [ 0 ] for v in dependencies [ ins ] ] ) ) : if ins not in view_of and not viewed_by . get ( ins , [ ] ) : running_memory_size [ cg ] -= var_mem [ ins ] elif ins in view_of : origin = view_of [ ins ] viewed_by [ origin ] . remove ( ins ) if ( not viewed_by [ origin ] and origin not in fgraph . inputs and not isinstance ( origin , theano . Constant ) ) : running_memory_size [ cg ] -= var_mem [ origin ] else : pass return [ node_memory_size , running_memory_size , running_max_memory_size , node_memory_saved_by_inplace , node_memory_saved_by_view ]
Calculate memory with specific node order.
Calculate memory with specific node order .
"""
            Calculate memory with specific node order.

            Return a list including the following values
            1.  node_memory_size
                Sum of the size of all variables that actually allocate
                memory (excluding views, and inplace).
            2.  running_memory_size
                The memory allocated after the current apply node.
            3.  running_max_memory_size
                The maximum of running_memory_size during the function.
            4.  node_memory_saved_by_view
                The sum of memory saved by returning view instead of new
                allocation.
            5.  node_memory_saved_by_inplace
                The sum of memory saved by reusing the input instead of
                new allocation.

            """ # Initial Mem info values [CPU, GPU] # This take only the inputs/outputs dependencies. # Initial compute_map which is used to check if a node is valid # two data structure used to mimic Python gc # {var1: [vars that view var1]} # The len of the list is the value of python ref # count. But we use a list, not just the ref count value. # This is more safe to help detect potential bug  in the algo # {var1: original var viewed by var1} # The orignal mean that we don't keep trac of all the intermediate # relationship in the view. # TODO check the op returned a view # TODO check the op returned a view # Update the Python emulating dicts and add the memory # allocated by the node # This is needed for destroy_map in case it # return a partial view that is destroyed.  So # the output could be different then the # input. # we keep trac of view only again the origin # Mimic the combination of Theano and Python gc # we trac the original var, so this shouldn't happen # ins is viewed_by something else, so its # memory isn't freed
order
fgraph
nodes_mem
ignore_dmap