提交 66cb3bf6 authored 作者: Frederic's avatar Frederic

pep8

上级 23230db9
...@@ -22,6 +22,8 @@ from theano.compat import cmp ...@@ -22,6 +22,8 @@ from theano.compat import cmp
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
def memodict(f): def memodict(f):
""" Memoization decorator for a function taking a single argument """ """ Memoization decorator for a function taking a single argument """
class memodict(defaultdict): class memodict(defaultdict):
...@@ -41,6 +43,7 @@ def make_depends(): ...@@ -41,6 +43,7 @@ def make_depends():
if ainp.owner)) if ainp.owner))
return depends return depends
def make_dependence_cmp(): def make_dependence_cmp():
""" Create a comparator to represent the dependence of nodes in a graph """ """ Create a comparator to represent the dependence of nodes in a graph """
...@@ -53,12 +56,15 @@ def make_dependence_cmp(): ...@@ -53,12 +56,15 @@ def make_dependence_cmp():
Returns negative number if b depends on a Returns negative number if b depends on a
Returns 0 otherwise Returns 0 otherwise
""" """
if depends((a, b)): return 1 if depends((a, b)):
if depends((b, a)): return -1 return 1
if depends((b, a)):
return -1
return 0 return 0
return dependence return dependence
def reverse_dict(d): def reverse_dict(d):
"""Reverses direction of dependence dict """Reverses direction of dependence dict
...@@ -78,6 +84,7 @@ def reverse_dict(d): ...@@ -78,6 +84,7 @@ def reverse_dict(d):
result[val] = result.get(val, tuple()) + (key, ) result[val] = result.get(val, tuple()) + (key, )
return result return result
def _toposort(edges): def _toposort(edges):
""" Topological sort algorithm by Kahn [1] - O(nodes + vertices) """ Topological sort algorithm by Kahn [1] - O(nodes + vertices)
...@@ -112,6 +119,7 @@ def _toposort(edges): ...@@ -112,6 +119,7 @@ def _toposort(edges):
raise ValueError("Input has cycles") raise ValueError("Input has cycles")
return L return L
def posort(l, *cmps): def posort(l, *cmps):
""" Partially ordered sort with multiple comparators """ Partially ordered sort with multiple comparators
...@@ -162,6 +170,7 @@ def posort(l, *cmps): ...@@ -162,6 +170,7 @@ def posort(l, *cmps):
return _toposort(comes_after) return _toposort(comes_after)
def sort_apply_nodes(inputs, outputs, cmps): def sort_apply_nodes(inputs, outputs, cmps):
""" Order a graph of apply nodes according to a list of comparators """ Order a graph of apply nodes according to a list of comparators
...@@ -184,6 +193,7 @@ def sort_apply_nodes(inputs, outputs, cmps): ...@@ -184,6 +193,7 @@ def sort_apply_nodes(inputs, outputs, cmps):
return posort(list_of_nodes(inputs, outputs), *cmps) return posort(list_of_nodes(inputs, outputs), *cmps)
def sort_schedule_fn(*cmps): def sort_schedule_fn(*cmps):
""" Make a schedule function from comparators """ Make a schedule function from comparators
...@@ -192,11 +202,13 @@ def sort_schedule_fn(*cmps): ...@@ -192,11 +202,13 @@ def sort_schedule_fn(*cmps):
""" """
dependence = make_dependence_cmp() dependence = make_dependence_cmp()
cmps = (dependence,) + cmps cmps = (dependence,) + cmps
def schedule(fgraph): def schedule(fgraph):
""" Order nodes in a FunctionGraph """ """ Order nodes in a FunctionGraph """
return sort_apply_nodes(fgraph.inputs, fgraph.outputs, cmps) return sort_apply_nodes(fgraph.inputs, fgraph.outputs, cmps)
return schedule return schedule
def key_to_cmp(key): def key_to_cmp(key):
def key_cmp(a, b): def key_cmp(a, b):
return cmp(key(a), key(b)) return cmp(key(a), key(b))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论