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)
| 69 | |
| 70 | |
| 71 | def 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 | |
| 105 | def gradients(y, dy=None): |