提交 b739baeb authored 作者: James Bergstra's avatar James Bergstra

fixing bugs and adding docs to module

上级 4e6fd916
...@@ -195,8 +195,10 @@ def _optcheck_env(input_specs, output_specs, accept_inplace = False): ...@@ -195,8 +195,10 @@ def _optcheck_env(input_specs, output_specs, accept_inplace = False):
inputs, outputs = gof.graph.clone(orig_inputs, orig_outputs) inputs, outputs = gof.graph.clone(orig_inputs, orig_outputs)
equivalence_tracker = _ResultEquivalenceTracker() equivalence_tracker = _ResultEquivalenceTracker()
env = gof.env.Env(inputs, outputs, env = gof.env.Env(inputs, outputs,
features=[equivalence_tracker, #DestroyHandler is not needed because it is actually installed by an optimization
gof.DestroyHandler(do_imports_on_attach=False)]) # after canonicalization. This results in a big speed gain.
#features=[equivalence_tracker, gof.DestroyHandler(do_imports_on_attach=False)])
features=[equivalence_tracker])
if not accept_inplace: if not accept_inplace:
for node in env.nodes: for node in env.nodes:
......
"""Classes implementing Theano's Module system. """Classes implementing Theano's Module system.
Rationale
=========
Functions in theano can share containers, when the `value` argument to `In` is a Container Functions in theano can share containers, when the `value` argument to `In` is a Container
instance. This feature makes it possible for multiple functions to use (and update) the same instance. This feature makes it possible for multiple functions to use (and update) the same
inputs. inputs.
...@@ -17,6 +20,53 @@ have become `ModuleInstances`, Members have become `Container`s, and Methods hav ...@@ -17,6 +20,53 @@ have become `ModuleInstances`, Members have become `Container`s, and Methods hav
`Function`s. `Function`s.
This structure contains numbers and functions, and is ready for computation. This structure contains numbers and functions, and is ready for computation.
Design Documentation
====================
Module Graph
------------
Components form a tree structure. Each component may have a _parent_ to which it is _bound_.
When we call `make`, this tree structure is replicated with ComponentInstances instead of
Components. Wheras Components are primarily symbolic, ComponentInstances are sparse matrices,
ndarrays, callable functions, etc.
Compilation via make
--------------------
Conversion from a Component graph to a ComponentInstance graph is performed by `Component.make`.
This method traverses the Component graph in two passes.
In the first pass (the allocate pass), it creates storage for all Results that are contained in the graph (see
`Component.allocate`). These are the module variables.
In the second pass (the build pass), it creates functions that (in general) operate on these module variables.
This pass also serves to construct all ComponentInstance-derived instances as well, such as
`ModuleInstance`s. The objects that are returned from this second pass are the return value of
`Component.make`.
In the third pass (the initialize pass), is optional and not necessarily recursive through the
graph.
The purpose of the third pass is to call the initialize method of the ComponentInstances built
during the second pass.
During this pass the ComponentInstance graph is complete. It is a good time to fill storage
allocated in phase 1 with sensible values.
Class Structure
---------------
The most important classes for the user API here are `Module`, `ModuleInstance`, and `Method`.
Several other classes are defined to factorize functionality.
- `Component`: WRITEME: what properties make something a Component?
- `_RComponent`: WRITEME: what properties make something a Component?
- `External`: WRITEME: what properties hold? What
- `Member`: WRITEME: what properties hold? What do they do?
""" """
__docformat__ = "restructuredtext en" __docformat__ = "restructuredtext en"
...@@ -291,8 +341,12 @@ class Member(_RComponent): ...@@ -291,8 +341,12 @@ class Member(_RComponent):
r = self.r r = self.r
if memo and r in memo: if memo and r in memo:
return memo[r] return memo[r]
rval = gof.Container(r, storage = [getattr(r, 'data', None)]) assert isinstance(r, gof.Result)
memo[r] = io.In(result = r, value = rval, mutable = False) rval = gof.Container(r, storage = [getattr(r, 'data', None)],
readonly=isinstance(r, gof.Constant))
memo[r] = io.In(result=r,
value=rval,
mutable=False)
return memo[r] return memo[r]
def build(self, mode, memo): def build(self, mode, memo):
...@@ -474,7 +528,9 @@ class Method(Component): ...@@ -474,7 +528,9 @@ class Method(Component):
' enclosing module or of one of its submodules.' % (r, self.name, self)) ' enclosing module or of one of its submodules.' % (r, self.name, self))
else: else:
return io.In(result=r, return io.In(result=r,
value=gof.Container(r, storage=[None]), value=gof.Container(r,
storage=[getattr(r, 'data', None)],
readonly=(isinstance(r, gof.Constant))),
mutable=False) mutable=False)
inputs = self.inputs inputs = self.inputs
...@@ -494,6 +550,8 @@ class Method(Component): ...@@ -494,6 +550,8 @@ class Method(Component):
# Deal with updates to shared storage # Deal with updates to shared storage
for k, v in self.updates.iteritems(): for k, v in self.updates.iteritems():
assert isinstance(k, gof.Result) assert isinstance(k, gof.Result)
if isinstance(k, gof.Constant):
raise TypeError('Module Constants cannot be updated', k)
assert isinstance(v, gof.Result) assert isinstance(v, gof.Result)
#identify an input for result k #identify an input for result k
...@@ -517,6 +575,8 @@ class Method(Component): ...@@ -517,6 +575,8 @@ class Method(Component):
' Use inputs to use your own storage, use updates to ' ' Use inputs to use your own storage, use updates to '
'work on module-shared storage'), k) 'work on module-shared storage'), k)
# Deal with module inputs that are not updated
outputs = self.outputs outputs = self.outputs
_inputs = [x.result for x in inputs] _inputs = [x.result for x in inputs]
# Grab the results that are not accessible from either the inputs or the updates. # Grab the results that are not accessible from either the inputs or the updates.
...@@ -529,10 +589,15 @@ class Method(Component): ...@@ -529,10 +589,15 @@ class Method(Component):
# Add this input to the inputs; we require that storage already exists for them, # Add this input to the inputs; we require that storage already exists for them,
# but otherwise they are immutable. # but otherwise they are immutable.
if isinstance(input, gof.Value): # and not isinstance(input, gof.Constant): if isinstance(input, gof.Value): # and not isinstance(input, gof.Constant):
#input might be Value or Constant
storage = get_storage(input) storage = get_storage(input)
assert type(storage) is io.In assert type(storage) is io.In
container = storage.value container = storage.value
container.value = input.data #the user is allowed to change this value between function calls if it isn't a constant
assert container.readonly == (isinstance(input, gof.Constant))
#the function is not allowed to change this value
assert storage.mutable == False
else: else:
storage = get_storage(input, not allocate_all) storage = get_storage(input, not allocate_all)
assert type(storage) is io.In assert type(storage) is io.In
...@@ -1095,7 +1160,12 @@ class Module(ComponentDict): ...@@ -1095,7 +1160,12 @@ class Module(ComponentDict):
# the object built under the name obj.XXX # the object built under the name obj.XXX
if methodname.startswith('_instance_'): if methodname.startswith('_instance_'):
new_methodname = methodname[len('_instance_'):] new_methodname = methodname[len('_instance_'):]
if not hasattr(inst, new_methodname): if hasattr(inst, new_methodname):
print >> sys.stderr, "WARNING: not overriding already-defined method",
print >> sys.stderr, getattr(inst, new_methodname),
print >> sys.stderr, "with",
print >> sys.stderr, getattr(self, methodname)
else:
curried = Curry(self, methodname, inst) curried = Curry(self, methodname, inst)
# setattr doesn't work here because we overrode __setattr__ # setattr doesn't work here because we overrode __setattr__
# setattr(inst, new_methodname, curried) # setattr(inst, new_methodname, curried)
......
...@@ -98,6 +98,7 @@ class T_module(unittest.TestCase): ...@@ -98,6 +98,7 @@ class T_module(unittest.TestCase):
assert i assert i
#test that we can set a value to the data the get this value #test that we can set a value to the data the get this value
if not isinstance(m1.x, gof.Constant):
inst.x=-1 inst.x=-1
inst.y=-2 inst.y=-2
inst.ldx[0]['x']=-3 inst.ldx[0]['x']=-3
...@@ -474,9 +475,9 @@ class T_module(unittest.TestCase): ...@@ -474,9 +475,9 @@ class T_module(unittest.TestCase):
m = M.make() m = M.make()
try: try:
m.y = 77 #fail? m.y = 77 #fail?
assert 0 #assign to constant should not have worked
except: except:
pass pass
assert m.y == 40
assert m.f(20) == 100 assert m.f(20) == 100
def test_raise_NotImplemented(self): def test_raise_NotImplemented(self):
......
...@@ -84,6 +84,9 @@ class Apply(utils.object2): ...@@ -84,6 +84,9 @@ class Apply(utils.object2):
else: else:
raise TypeError("The 'outputs' argument to Apply must contain Result instances with no owner, not %s" % output) raise TypeError("The 'outputs' argument to Apply must contain Result instances with no owner, not %s" % output)
self._creation_idx = _creation_idx[0]
_creation_idx[0] += 1
def default_output(self): def default_output(self):
"""Returns the default output for this node. """Returns the default output for this node.
...@@ -123,9 +126,6 @@ class Apply(utils.object2): ...@@ -123,9 +126,6 @@ class Apply(utils.object2):
return self return self
def __hash__(self): def __hash__(self):
if not hasattr(self, '_creation_idx'):
self._creation_idx = _creation_idx[0]
_creation_idx[0] += 1
return self._creation_idx return self._creation_idx
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论