提交 51c981b0 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Rename context to params.

上级 8d3a67b7
......@@ -43,11 +43,11 @@ There are less methods to define for an Op than for a Type:
that a python exception is set) if your C code needs to
raise an exception.
``sub['context']``
``sub['params']``
(optional) The name of the variable which holds the context
for the node. This will only appear if the op has requested
a context by having a :meth:`get_context()` method that return
a context by having a :meth:`get_params()` method that return
something other than None.
.. method:: c_code_cleanup(node, name, input_names, output_names, sub)
......@@ -142,11 +142,11 @@ There are less methods to define for an Op than for a Type:
that a python exception is set) if your C code needs to
raise an exception.
``sub['context']``
``sub['params']``
(optional) The name of the variable which holds the context
for the node. This will only appear if the op has requested
a context by having a :meth:`get_context()` method that return
a context by having a :meth:`get_params()` method that return
something other than None.
.. method:: c_support_code()
......@@ -223,18 +223,18 @@ There are less methods to define for an Op than for a Type:
is high or when theano compilation directory is shared by many
process (like on a network file server on a cluster).
.. method:: get_context(node)
.. method:: get_params(node)
(optional) If defined, should return the runtime context the op
needs. This context will be passed to the C code through the
variable named in `sub['context']`. The variable is also
(optional) If defined, should return the runtime params the op
needs. These parameters will be passed to the C code through the
variable named in `sub['params']`. The variable is also
available for use in the code returned by
:meth:`c_init_code_struct`. If it returns `None` this is
considered the same as if the method was not defined.
If this method is defined and does not return `None`, then the
Op *must* have a `context_type` property with the Type to use
for the context variable.
Op *must* have a `params_type` property with the Type to use
for the params variable.
.. attribute:: _f16_ok
......
......@@ -924,8 +924,8 @@ In addition to these macros, the ``init_code_struct``, ``code``, and
You can add a semicolon after the macro if it makes your editor
happy.
* ``CONTEXT`` : Name of the context variable for this node. (only
for Ops which have a context, which is discussed elsewhere)
* ``PARAMS`` : Name of the params variable for this node. (only
for Ops which have params, which is discussed elsewhere)
Finally the tag ``code`` and ``code_cleanup`` have macros to
pass the inputs and output names. These are name ``INPUT_{i}`` and
......
......@@ -574,22 +574,22 @@ class CLinker(link.Linker):
self.variables = [var for var in self.inputs if not len(var.clients)]
self.variables += graph.variables(self.inputs, self.outputs)
# This adds a hidden input which is the context for each node
# This adds a hidden input which is the params for each node
# that needs it
self.contexts = dict()
self.node_params = dict()
for node in self.node_order:
ctx = node.run_context()
if ctx is not graph.NoContext:
params = node.run_params()
if params is not graph.NoParams:
# try to avoid creating more than one variable for the
# same context.
if ctx in self.contexts:
var = self.contexts[ctx]
assert var.type == node.context_type
var.clients.append((node, 'context'))
# same params.
if params in self.node_params:
var = self.node_params[params]
assert var.type == node.params_type
var.clients.append((node, 'params'))
else:
var = graph.Constant(node.context_type, ctx)
var.clients = [(node, 'context')]
self.contexts[ctx] = var
var = graph.Constant(node.params_type, params)
var.clients = [(node, 'params')]
self.node_params[params] = var
self.variables.append(var)
# The orphans field is listified to ensure a consistent order.
......@@ -732,9 +732,9 @@ class CLinker(link.Linker):
sub = dict(failure_var=failure_var)
ctx = node.run_context()
if ctx is not graph.NoContext:
context_var = symbol[self.contexts[ctx]]
params = node.run_params()
if params is not graph.NoParams:
params_var = symbol[self.node_params[params]]
# The placeholder will be replaced by a hash of the entire
# code (module + support code) in DynamicModule.code.
......@@ -750,16 +750,16 @@ class CLinker(link.Linker):
# Make the CodeBlock for c_code
sub['id'] = id
sub['fail'] = failure_code(sub)
if ctx is not graph.NoContext:
sub['context'] = context_var
if params is not graph.NoParams:
sub['params'] = params_var
sub_struct = dict()
sub_struct['id'] = id + 1
sub_struct['fail'] = failure_code_init(sub)
if ctx is not graph.NoContext:
# Since context inputs are always constants they are
if params is not graph.NoParams:
# Since params inputs are always constants they are
# guaranteed to be available in the struct init code.
sub_struct['context'] = context_var
sub_struct['params'] = params_var
struct_support = ""
struct_init = ""
......
......@@ -22,7 +22,7 @@ __docformat__ = "restructuredtext en"
is_same_graph_with_merge = None
equal_computations = None
NoContext = object()
NoParams = object()
class Node(utils.object2):
......@@ -123,14 +123,14 @@ class Apply(Node):
else:
raise TypeError("The 'outputs' argument to Apply must contain Variable instances with no owner, not %s" % output)
def run_context(self):
def run_params(self):
"""
Returns the context for the node, or NoContext if no context is set.
Returns the params for the node, or NoParams if no params is set.
"""
if hasattr(self.op, 'get_context'):
return self.op.get_context(self)
return NoContext
if hasattr(self.op, 'get_params'):
return self.op.get_params(self)
return NoParams
def default_output(self):
"""
......@@ -253,7 +253,7 @@ class Apply(Node):
Property: Number of outputs.
"""
context_type = property(lambda self: self.op.context_type, doc='type to use for the context')
params_type = property(lambda self: self.op.params_type, doc='type to use for the params')
class Variable(Node):
......
......@@ -857,9 +857,9 @@ class Op(utils.object2, PureOp, CLinkerOp):
p = node.op.perform
ctx = node.run_context()
params = node.run_params()
if ctx is graph.NoContext:
if params is graph.NoParams:
# default arguments are stored in the closure of `rval`
def rval(p=p, i=node_input_storage, o=node_output_storage, n=node):
r = p(n, [x[0] for x in i], o)
......@@ -867,11 +867,11 @@ class Op(utils.object2, PureOp, CLinkerOp):
compute_map[o][0] = True
return r
else:
ctx_val = node.context_type.filter(ctx)
params_val = node.params_type.filter(params)
def rval(p=p, i=node_input_storage, o=node_output_storage, n=node,
ctx=ctx_val):
r = p(n, [x[0] for x in i], o, ctx)
params=params_val):
r = p(n, [x[0] for x in i], o, params)
for o in node.outputs:
compute_map[o][0] = True
return r
......@@ -1403,9 +1403,9 @@ class COp(Op):
define_macros.append("#define FAIL %s" % (
self._lquote_macro(sub['fail']),))
undef_macros.append("#undef FAIL")
if 'context' in sub:
define_macros.append("#define CONTEXT %s" % (sub['context'],))
undef_macros.append("#undef CONTEXT")
if 'params' in sub:
define_macros.append("#define PARAMS %s" % (sub['params'],))
undef_macros.append("#undef PARAMS")
return os.linesep.join(define_macros), os.linesep.join(undef_macros)
......@@ -1442,21 +1442,21 @@ class COp(Op):
define_macros, undef_macros = self.get_c_macros(node, name,
check_input=False)
ctx = ""
if 'context' in sub:
ctx = ", %s" % (sub['context'],)
params = ""
if 'params' in sub:
params = ", %s" % (sub['params'],)
# Generate the C code
return """
%(define_macros)s
{
if (%(func_name)s(%(func_args)s%(ctx)s) != 0) {
if (%(func_name)s(%(func_args)s%(params)s) != 0) {
%(fail)s
}
}
%(undef_macros)s
""" % dict(func_name=self.func_name,
fail=sub['fail'], ctx=ctx,
fail=sub['fail'], params=params,
func_args=self.format_c_function_args(inp, out),
define_macros=define_macros,
undef_macros=undef_macros)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论