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