MCPcopy Create free account
hub / github.com/apache/singa / infer_dependency

Function infer_dependency

python/singa/autograd.py:71–102  ·  view source on GitHub ↗

Infer the dependency of all operations with the given op as the last operation. Operator A is depending on B if A uses the output(s) of B. Args: op: an Operator instance, e.g. the loss operation. Return: a Counter instance with the operation as the key,

(op)

Source from the content-addressed store, hash-verified

69
70
71def infer_dependency(op):
72 """
73 Infer the dependency of all operations with the
74 given op as the last operation.
75 Operator A is depending on B if A uses the output(s) of B.
76
77 Args:
78 op: an Operator instance, e.g. the loss operation.
79
80 Return:
81 a Counter instance with the operation as the key,
82 and the number of operations that are depending on it as the value;
83 and a Counter instance with the id of the output tensor as the key, and
84 the number of operations that are depending on it as the value.
85 """
86
87 # current op is not inserted into the dependency_count
88 # if the current op is not a terminal op, then this function may just
89 # count dependency of a branch.
90 op_count = Counter()
91 tensor_count = Counter()
92 queue = deque([op])
93 while len(queue) > 0:
94 cur_op = queue.pop()
95 for src_op, xid, _, _ in cur_op.src:
96 if src_op not in op_count:
97 op_count[src_op] = 1
98 queue.append(src_op)
99 else:
100 op_count[src_op] += 1
101 tensor_count[xid] += 1
102 return op_count, tensor_count
103
104
105def gradients(y, dy=None):

Callers 1

backwardFunction · 0.85

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected