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

pep8

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