(predict_net)
| 962 | _COPY_OPS = ["CopyCPUToGPU", "CopyGPUToCPU"] |
| 963 | |
| 964 | def _fuse_once(predict_net): |
| 965 | ssa, blob_versions = core.get_ssa(predict_net) |
| 966 | consumer_map = get_consumer_map(ssa) |
| 967 | versioned_external_output = [ |
| 968 | (name, blob_versions[name]) for name in predict_net.external_output |
| 969 | ] |
| 970 | |
| 971 | for op_id, op in enumerate(predict_net.op): |
| 972 | if op.type in _COPY_OPS: |
| 973 | fw_copy_versioned_output = ssa[op_id][1][0] |
| 974 | consumer_ids = [x[0] for x in consumer_map[fw_copy_versioned_output]] |
| 975 | reverse_op_type = _COPY_OPS[1 - _COPY_OPS.index(op.type)] |
| 976 | |
| 977 | is_fusable = ( |
| 978 | len(consumer_ids) > 0 |
| 979 | and fw_copy_versioned_output not in versioned_external_output |
| 980 | and all( |
| 981 | predict_net.op[_op_id].type == reverse_op_type |
| 982 | and ssa[_op_id][1][0] not in versioned_external_output |
| 983 | for _op_id in consumer_ids |
| 984 | ) |
| 985 | ) |
| 986 | |
| 987 | if is_fusable: |
| 988 | for rv_copy_op_id in consumer_ids: |
| 989 | # making each NextOp uses "a" directly and removing Copy ops |
| 990 | rs_copy_versioned_output = ssa[rv_copy_op_id][1][0] |
| 991 | next_op_id, inp_id = consumer_map[rs_copy_versioned_output][0] |
| 992 | predict_net.op[next_op_id].input[inp_id] = op.input[0] |
| 993 | # remove CopyOps |
| 994 | new_ops = [ |
| 995 | op |
| 996 | for i, op in enumerate(predict_net.op) |
| 997 | if i != op_id and i not in consumer_ids |
| 998 | ] |
| 999 | del predict_net.op[:] |
| 1000 | predict_net.op.extend(new_ops) |
| 1001 | return True |
| 1002 | |
| 1003 | return False |
| 1004 | |
| 1005 | # _fuse_once returns False is nothing can be fused |
| 1006 | while _fuse_once(predict_net): |
no test coverage detected