提交 b1cc9fd8 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Move the _apply versions of the function under CLinkerOp

and clarify/update documentation.
上级 db7bffb4
...@@ -535,7 +535,6 @@ class CLinker(link.Linker): ...@@ -535,7 +535,6 @@ class CLinker(link.Linker):
for variable in self.variables: for variable in self.variables:
# it might be possible to inline constant variables as C literals # it might be possible to inline constant variables as C literals
## if getattr(variable, 'constant', False):
# policy = [[what to declare in the struct, # policy = [[what to declare in the struct,
# what to do at construction, # what to do at construction,
# what to do at destruction], # what to do at destruction],
...@@ -545,9 +544,6 @@ class CLinker(link.Linker): ...@@ -545,9 +544,6 @@ class CLinker(link.Linker):
if variable in self.inputs: if variable in self.inputs:
# we need to extract the new inputs at each run # we need to extract the new inputs at each run
# they do not need to be relayed to Python, so we don't sync # they do not need to be relayed to Python, so we don't sync
# if isinstance(variable, Constant):
# raise TypeError("Inputs to CLinker cannot be Constant.",
# variable)
policy = [[get_nothing, get_nothing, get_nothing], policy = [[get_nothing, get_nothing, get_nothing],
[get_c_declare, get_c_extract, get_c_cleanup]] [get_c_declare, get_c_extract, get_c_cleanup]]
elif variable in self.orphans: elif variable in self.orphans:
......
...@@ -1522,8 +1522,6 @@ def gcc_llvm(): ...@@ -1522,8 +1522,6 @@ def gcc_llvm():
It don't support all g++ parameters even if it support many of them. It don't support all g++ parameters even if it support many of them.
""" """
if gcc_llvm.is_llvm is None: if gcc_llvm.is_llvm is None:
pass
p = None
try: try:
p_out = output_subprocess_Popen(['g++', '--version']) p_out = output_subprocess_Popen(['g++', '--version'])
output = p_out[0] + p_out[1] output = p_out[0] + p_out[1]
...@@ -1535,9 +1533,9 @@ def gcc_llvm(): ...@@ -1535,9 +1533,9 @@ def gcc_llvm():
# compile when g++ is not available. If this happen, it # compile when g++ is not available. If this happen, it
# will crash later so supposing it is not llvm is "safe". # will crash later so supposing it is not llvm is "safe".
output = b('') output = b('')
del p
gcc_llvm.is_llvm = b("llvm") in output gcc_llvm.is_llvm = b("llvm") in output
return gcc_llvm.is_llvm return gcc_llvm.is_llvm
gcc_llvm.is_llvm = None gcc_llvm.is_llvm = None
......
...@@ -126,22 +126,6 @@ class CLinkerObject(object): ...@@ -126,22 +126,6 @@ class CLinkerObject(object):
""" """
return () return ()
def c_code_cache_version_apply(self, node):
"""Return a tuple of integers indicating the version of this Op.
An empty tuple indicates an 'unversioned' Op that will not be cached between processes.
The cache mechanism may erase cached modules that have been superceded by newer
versions. See `ModuleCache` for details.
:note: See also `c_code_cache_version()`
:note: This function overrides `c_code_cache_version` unless it explicitly calls
`c_code_cache_version`. The default implementation simply calls `c_code_cache_version`
and ignores the `node` argument.
"""
return self.c_code_cache_version()
def c_compile_args(self): def c_compile_args(self):
"""Optional: Return a list of compile args recommended to compile the """Optional: Return a list of compile args recommended to compile the
code returned by other methods in this class. code returned by other methods in this class.
...@@ -187,18 +171,6 @@ class CLinkerObject(object): ...@@ -187,18 +171,6 @@ class CLinkerObject(object):
self.__class__.__name__) self.__class__.__name__)
def c_init_code_apply(self, node, name):
"""
Optional: return a list of code snippets specific to the apply
to be inserted in module initialization.
:Exceptions:
- `MethodNotDefined`: the subclass does not override this method
"""
raise utils.MethodNotDefined("c_init_code_apply", type(self),
self.__class__.__name__)
class CLinkerOp(CLinkerObject): class CLinkerOp(CLinkerObject):
""" """
Interface definition for `Op` subclasses compiled by `CLinker`. Interface definition for `Op` subclasses compiled by `CLinker`.
...@@ -217,24 +189,25 @@ class CLinkerOp(CLinkerObject): ...@@ -217,24 +189,25 @@ class CLinkerOp(CLinkerObject):
:Parameters: :Parameters:
`node` : Apply instance `node` : Apply instance
WRITEME The node for which we are compiling the current c_code.
`name` : WRITEME The same Op may be used in more than one node.
WRITEME `name` : A string
A name that is automatically assigned and guaranteed to be unique.
`inputs` : list of strings `inputs` : list of strings
There is a string for each input of the function, and the string is the name of a C There is a string for each input of the function, and the
`PyObject` variable pointing to that input. string is the name of a C `PyObject` variable pointing to
that input.
`outputs` : list of strings `outputs` : list of strings
Each string is the name of a `PyObject` pointer where the Op should Each string is the name of a `PyObject` pointer where the
store its variables. As of version 0.4.0, this pointer could be Op should store its variables. This pointer may either be
NULL, or contain an object allocated during a previous call to the NULL, indicating that the Op must allocate appropriate
same function, unchanged from the end of the previous execution. objects or it may point to preallocated objects of the
In a future version, there will be no guarantee on where that right type and number of dimensions. In the case of
object will be created (it could be allocated during a previous preallocated objects, the Op must make sure that the shape
execution, or by another Op, by the Mode, etc.). It will still and strides meet requirements, and in the case they don't
be of an appropriate Type (in the Theano sense) to store the output either reallocate the object inplace or free it and
of the computation: for instance, for a TensorVariable, it will be a allocate an appropriate output. The type and number of
Numpy ndarray with the right number of dimensions, and the right dtype. dimensions are guaranteed to be appropriate.
However, its shape, or stride pattern, could not be adequate.
`sub` : dict of strings `sub` : dict of strings
extra symbols defined in `CLinker` sub symbols (such as 'fail'). extra symbols defined in `CLinker` sub symbols (such as 'fail').
WRITEME WRITEME
...@@ -246,10 +219,28 @@ class CLinkerOp(CLinkerObject): ...@@ -246,10 +219,28 @@ class CLinkerOp(CLinkerObject):
raise utils.MethodNotDefined('%s.c_code' \ raise utils.MethodNotDefined('%s.c_code' \
% self.__class__.__name__) % self.__class__.__name__)
def c_code_cleanup(self, node, name, inputs, outputs, sub): def c_code_cache_version_apply(self, node):
"""Optional: Return C code to run after c_code, whether it failed or not. """Return a tuple of integers indicating the version of this Op.
An empty tuple indicates an 'unversioned' Op that will not be
cached between processes.
The cache mechanism may erase cached modules that have been
superceded by newer versions. See `ModuleCache` for details.
:note: See also `c_code_cache_version()`
:note: This function overrides `c_code_cache_version` unless
it explicitly calls `c_code_cache_version`. The
default implementation simply calls
`c_code_cache_version` and ignores the `node` argument.
"""
return self.c_code_cache_version()
QUESTION: is this function optional? def c_code_cleanup(self, node, name, inputs, outputs, sub):
"""
Optional: Return C code to run after c_code, whether it failed
or not.
This is a convenient place to clean up things allocated by c_code(). This is a convenient place to clean up things allocated by c_code().
...@@ -259,39 +250,45 @@ class CLinkerOp(CLinkerObject): ...@@ -259,39 +250,45 @@ class CLinkerOp(CLinkerObject):
`name` : WRITEME `name` : WRITEME
WRITEME WRITEME
`inputs` : list of strings `inputs` : list of strings
There is a string for each input of the function, and the string is the name of a C There is a string for each input of the function, and the
`PyObject` variable pointing to that input. string is the name of a C `PyObject` variable pointing to
that input.
`outputs` : list of strings `outputs` : list of strings
Each string is the name of a `PyObject` pointer where the Op should store its Each string is the name of a `PyObject` pointer where the
variables. This pointer could be NULL, or contain an object of the right Op should store its variables. This pointer could be NULL,
Type (in the Theano sense) to store the output of the computation. or contain an object of the right Type (in the Theano
For instance, for a TensorVariable, it will be a Numpy ndarray with sense) to store the output of the computation. For
the right number of dimensions, and the right dtype. However, its instance, for a TensorVariable, it will be a Numpy ndarray
shape, or stride pattern, could not be adequate. with the right number of dimensions, and the right
It could be unchanged from the end of the previous execution, or allocated dtype. However, its shape, or stride pattern, could not be
by another Op, or by the Mode. adequate. It could be unchanged from the end of the
previous execution, or allocated by another Op, or by the
Mode.
`sub` : dict of strings `sub` : dict of strings
extra symbols defined in `CLinker` sub symbols (such as 'fail'). extra symbols defined in `CLinker` sub symbols (such as 'fail').
WRITEME WRITEME
WRITEME
:Exceptions: :Exceptions:
- `MethodNotDefined`: the subclass does not override this method - `MethodNotDefined`: the subclass does not override this method
""" """
raise utils.MethodNotDefined('%s.c_code_cleanup' \ raise utils.MethodNotDefined('%s.c_code_cleanup' \
% self.__class__.__name__) % self.__class__.__name__)
def c_support_code_apply(self, node, name): def c_support_code_apply(self, node, name):
"""Optional: Return utility code for use by an `Op` that will be inserted at global """Optional: Return utility code for use by an `Op` that will be
scope, that can be specialized for the support of a particular `Apply` node. inserted at struct scope, that can be specialized for the
support of a particular `Apply` node.
:param node: an Apply instance in the graph being compiled :param node: an Apply instance in the graph being compiled
:param node_id: a string or number that serves to uniquely identify this node. :param name: a string or number that serves to uniquely
Symbol names defined by this support code should include the node_id, so that they can identify this node. Symbol names defined by this
be called from the c_code, and so that they do not cause name collisions. support code should include the name, so that
they can be called from the c_code, and so that
they do not cause name collisions.
:note: This function is called in addition to c_support_code
and will supplement whatever is returned from there.
:Exceptions: :Exceptions:
- `MethodNotDefined`: Subclass does not implement this method - `MethodNotDefined`: Subclass does not implement this method
...@@ -300,6 +297,28 @@ class CLinkerOp(CLinkerObject): ...@@ -300,6 +297,28 @@ class CLinkerOp(CLinkerObject):
raise utils.MethodNotDefined("c_support_code_apply", raise utils.MethodNotDefined("c_support_code_apply",
type(self), self.__class__.__name__) type(self), self.__class__.__name__)
def c_init_code_apply(self, node, name):
"""
Optional: return a code string specific to the apply
to be inserted in the struct initialization code.
:param node: an Apply instance in the graph being compiled
:param name: a string or number that serves to uniquely
identify this node. Symbol names defined by this
support code should include the name, so that
they can be called from the c_code, and so that
they do not cause name collisions.
:note: This function is called in addition to c_init_code
and will supplement whatever is returned from there.
:Exceptions:
- `MethodNotDefined`: the subclass does not override this method
"""
raise utils.MethodNotDefined("c_init_code_apply", type(self),
self.__class__.__name__)
class PureOp(object): class PureOp(object):
""" """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论