提交 cdba68ad authored 作者: Iban Harlouchet's avatar Iban Harlouchet

numpydoc for theano/gof/vm.py

上级 c99f5cb3
""" """
VMs that run Theano graph computations. VMs that run Theano graph computations.
A VM is not actually different from a Linker, we just decided A VM is not actually different from a Linker, we just decided
VM was a better name at some point. VM was a better name at some point.
""" """
from . import link from . import link
from collections import defaultdict from collections import defaultdict
...@@ -142,7 +144,6 @@ def calculate_reallocate_info(order, fgraph, storage_map, compute_map_re, ...@@ -142,7 +144,6 @@ def calculate_reallocate_info(order, fgraph, storage_map, compute_map_re,
class VM(object): class VM(object):
""" """
A VM object's __call__ method evaluates a Theano program. A VM object's __call__ method evaluates a Theano program.
...@@ -155,33 +156,35 @@ class VM(object): ...@@ -155,33 +156,35 @@ class VM(object):
advantage of lazy computation, though they still produce the correct advantage of lazy computation, though they still produce the correct
output for lazy nodes. output for lazy nodes.
Attributes: Parameters
----------
call_counts - list of integers, one for each thunk. call_count[i] is the nodes
number of times thunks[i] was called in the course of computations A list of nodes in toposort order.
performed by call_with_timers(). thunks
A list of thunks to execute those nodes, in toposort order.
call_times - list of floats, one for each thunk. call_times[i] is pre_call_clear
the amount of runtime spent on thunks[i] in the course of A list of containers to empty at the beginning of each call.
computations performed by call_with_timers().
Attributes
need_update_inputs - bool. True indicates that Function.__call__ ----------
must implement the feedback from output storage to input call_counts
storage. False means it *must not* repeat that feedback. List of integers, one for each thunk. call_count[i] is the number of
times thunks[i] was called in the course of computations performed by
call_with_timers().
call_times
List of floats, one for each thunk. call_times[i] is the amount of
runtime spent on thunks[i] in the course of computations performed by
call_with_timers().
need_update_inputs : bool
True indicates that Function.__call__ must implement the feedback from
output storage to input storage. False means it *must not* repeat that
feedback.
""" """
def __init__(self, nodes, thunks, pre_call_clear): def __init__(self, nodes, thunks, pre_call_clear):
"""
Allocate a virtual machine.
nodes - a list of nodes in toposort order
thunks - a list of thunks to execute those nodes, in toposort order
pre_call_clear - a list of containers to empty at the beginning of each
call.
"""
if len(nodes) != len(thunks): if len(nodes) != len(thunks):
raise ValueError() raise ValueError()
self.nodes = nodes self.nodes = nodes
...@@ -202,6 +205,7 @@ class VM(object): ...@@ -202,6 +205,7 @@ class VM(object):
Postcondition - all output variables have been computed. VMs vary in Postcondition - all output variables have been computed. VMs vary in
what exactly this means and how it is done. what exactly this means and how it is done.
""" """
raise NotImplementedError('override me') raise NotImplementedError('override me')
...@@ -212,6 +216,7 @@ class VM(object): ...@@ -212,6 +216,7 @@ class VM(object):
Free internal variables and outputs. Essentially, free as much memory Free internal variables and outputs. Essentially, free as much memory
as possible without intefering with the ability to evaluate subsequent as possible without intefering with the ability to evaluate subsequent
calls. calls.
""" """
raise NotImplementedError('override me') raise NotImplementedError('override me')
...@@ -247,10 +252,10 @@ class VM(object): ...@@ -247,10 +252,10 @@ class VM(object):
class Loop(VM): class Loop(VM):
""" """
Unconditional start-to-finish program execution in Python. Unconditional start-to-finish program execution in Python.
No garbage collection is allowed on intermediate results. No garbage collection is allowed on intermediate results.
""" """
# Some other part of Theano query that information # Some other part of Theano query that information
allow_gc = False allow_gc = False
...@@ -280,10 +285,10 @@ class Loop(VM): ...@@ -280,10 +285,10 @@ class Loop(VM):
class LoopGC(VM): class LoopGC(VM):
""" """
Unconditional start-to-finish program execution in Python. Unconditional start-to-finish program execution in Python.
Garbage collection is possible on intermediate results. Garbage collection is possible on intermediate results.
""" """
def __init__(self, nodes, thunks, pre_call_clear, post_thunk_clear): def __init__(self, nodes, thunks, pre_call_clear, post_thunk_clear):
...@@ -327,7 +332,6 @@ class LoopGC(VM): ...@@ -327,7 +332,6 @@ class LoopGC(VM):
class Stack(VM): class Stack(VM):
""" """
Finish-to-start evalution order of thunks. Finish-to-start evalution order of thunks.
...@@ -399,9 +403,11 @@ class Stack(VM): ...@@ -399,9 +403,11 @@ class Stack(VM):
raise ValueError("Must set dependencies when using GC") raise ValueError("Must set dependencies when using GC")
def run_thunk_of_node(self, node): def run_thunk_of_node(self, node):
"""Run the thunk corresponding to Apply instance `node` """
Run the thunk corresponding to Apply instance `node`.
Calls self.callback if it is defined. Calls self.callback if it is defined.
""" """
idx = self.node_idx[node] idx = self.node_idx[node]
t0 = time.time() t0 = time.time()
...@@ -683,34 +689,36 @@ except (OSError, theano.gof.cmodule.MissingGXX) as e: ...@@ -683,34 +689,36 @@ except (OSError, theano.gof.cmodule.MissingGXX) as e:
class VM_Linker(link.LocalLinker): class VM_Linker(link.LocalLinker):
""" """
Class that satisfies the Linker interface by acting as a VM factory. Class that satisfies the Linker interface by acting as a VM factory.
Parameters
----------
allow_gc
Force the virtual machine to clean up unnecessary
references, in order to allow garbage collection on
intermediate values during computation of a function.
If None use as default the value of the Theano flag allow_gc.
use_cloop
Use the C-based virtual machine if possible
callback
A callable object to call after each call to a thunk within
the virtual machine. It will be called with four arguments called
'node', 'thunk', 'storage_map', and 'compute_map'.
lazy
Useful only when use_cloop is False. When lazy is None, use the
theano flag vm.lazy value. Then if we have a None (default) we auto
detect if lazy evaluation is needed and use the apropriate
version. If lazy is True or False, we force the version used
between Loop/LoopGC and Stack.
c_thunks
If None or True, don't change the default. If False,
don't compile c code for the thunks.
""" """
def __init__(self, allow_gc=None, use_cloop=False, callback=None, def __init__(self, allow_gc=None, use_cloop=False, callback=None,
lazy=None, schedule=None, c_thunks=None): lazy=None, schedule=None, c_thunks=None):
"""
allow_gc - force the virtual machine to clean up unnecessary
references, in order to allow garbage collection on
intermediate values during computation of a function.
If None use as default the value of the Theano flag allow_gc.
use_cloop - use the C-based virtual machine if possible
callback - a callable object to call after each call to a thunk within
the virtual machine. It will be called with four arguments called
'node', 'thunk', 'storage_map', and 'compute_map'.
lazy - Useful only when use_cloop is False. When lazy is None, use the
theano flag vm.lazy value. Then if we have a None (default) we auto
detect if lazy evaluation is needed and use the apropriate
version. If lazy is True or False, we force the version used
between Loop/LoopGC and Stack.
c_thunks - If None or True, don't change the default. If False,
don't compile c code for the thunks.
"""
# Note: if more parameters are added to __init__, make sure to forward # Note: if more parameters are added to __init__, make sure to forward
# them in the "type(self)(...)" call in the "accept" method below. # them in the "type(self)(...)" call in the "accept" method below.
if allow_gc is None: if allow_gc is None:
...@@ -727,13 +735,20 @@ class VM_Linker(link.LocalLinker): ...@@ -727,13 +735,20 @@ class VM_Linker(link.LocalLinker):
def accept(self, fgraph, no_recycling=None): def accept(self, fgraph, no_recycling=None):
""" """
:param fgraph: a PerformLinker can have accepted one FunctionGraph
instance at a time.
:param no_recycling: WRITEME Parameters
----------
fgraph
A PerformLinker can have accepted one FunctionGraph instance
at a time.
no_recycling
WRITEME
Returns
-------
Self if fgraph is the first FunctionGraph that has ever been
associated to self, else, a new VM_Linker associated to fgraph.
:returns: self if fgraph is the first FunctionGraph that has ever been
associated to self, else, a new VM_Linker associated to fgraph.
""" """
if (config.profile and if (config.profile and
hasattr(theano, 'sandbox') and hasattr(theano, 'sandbox') and
...@@ -779,27 +794,24 @@ class VM_Linker(link.LocalLinker): ...@@ -779,27 +794,24 @@ class VM_Linker(link.LocalLinker):
Returns dict: variable K -> list of variables [v1, v2, v3, ...] Returns dict: variable K -> list of variables [v1, v2, v3, ...]
for each K in variables. for each K in variables.
The variables v1, v2, ... are the full set of variables that depend The variables v1, v2, ... are the full set of variables that depend
directly on K. When we know that none of them will need to be directly on K. When we know that none of them will need to be
computed, we know that: computed, we know that:
* K will not need to be computed * K will not need to be computed.
* if K is already computed, it can be released for garbage collection * If K is already computed, it can be released for garbage collection.
Parameters Parameters
---------- ----------
variables - iterable over the variables used in a graph computation. variables
Iterable over the variables used in a graph computation.
Notes
-----
It doesn't take care of the view_map/destroy_map. So it means it relies
on Python gc no to free the object real storage.
N.B. gc means garbage collection N.B. gc means garbage collection
Note
----
It don't take care of the view_map/destroy_map. So
it mean it rely on Python gc to don't free the object real
storage.
""" """
dependencies = {} dependencies = {}
for k in variables: for k in variables:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论