Add trans before and after this FC_Prune->(Relu)->FC_Prune chain.
(cur, id2node, name2id, ops, model)
| 71 | |
| 72 | |
| 73 | def transFCRelu(cur, id2node, name2id, ops, model): |
| 74 | """ |
| 75 | Add trans before and after this FC_Prune->(Relu)->FC_Prune chain. |
| 76 | """ |
| 77 | # 1. add trans before the start of this chain |
| 78 | # assuming that cur is a FC_Prune, and it has only one input |
| 79 | pre = cur.prev.itervalues().next() |
| 80 | # Create a node /op and insert it. |
| 81 | # TODO(wyiming): check whether it is correct here |
| 82 | current_blob = model.Transpose(cur.op.input[0], cur.op.input[0] + "_trans") |
| 83 | # print model.net.Proto() |
| 84 | trans_op = model.net.Proto().op[-1] |
| 85 | trans_node = NetDefNode(trans_op.output[0], "Transpose", pre, trans_op) |
| 86 | trans_node.visited = True |
| 87 | pre_new = trans_node |
| 88 | |
| 89 | # 2. use while loop to visit the chain |
| 90 | while True: |
| 91 | # breakup with the parent |
| 92 | cur.deleteInput(pre) |
| 93 | if not (cur.optype == "FC_Prune" or cur.optype == "Relu"): |
| 94 | print("Reaching the end of the chain") |
| 95 | break |
| 96 | if len(cur.ops) > 1: |
| 97 | print("A FC/Relu giving more than 1 useful outputs") |
| 98 | if cur.optype == "FC_Prune": |
| 99 | op = cur.op |
| 100 | wcsr, iw, jw = maskNallocate(op.input[1]) |
| 101 | bias_name = op.input[3] |
| 102 | # TODO(wyiming): create a new Op here |
| 103 | current_blob = model.FC_Sparse(current_blob, |
| 104 | cur.op.output[0] + "_Sparse", |
| 105 | wcsr, iw, jw, bias_name) |
| 106 | sps_op = model.net.Proto().op[-1] |
| 107 | sps_node = NetDefNode(cur.op.output[0] + "_Sparse", |
| 108 | "FC_Sparse", |
| 109 | pre_new, sps_op) |
| 110 | sps_node.visited = True |
| 111 | pre_new = sps_node |
| 112 | if cur.optype == "Relu": |
| 113 | op = cur.op |
| 114 | current_blob = model.Relu(current_blob, current_blob) |
| 115 | rel_op = model.net.Proto().op[-1] |
| 116 | rel_node = NetDefNode(str(current_blob), "Relu", |
| 117 | pre_new, rel_op) |
| 118 | rel_node.visited = True |
| 119 | pre_new = rel_node |
| 120 | |
| 121 | cur.visited = True |
| 122 | pre = cur |
| 123 | flag = False |
| 124 | for _, temp in cur.ops.iteritems(): |
| 125 | if temp.optype == "Relu" or temp.optype == "FC_Prune": |
| 126 | flag = True |
| 127 | cur = temp |
| 128 | if not flag: |
| 129 | # assume that there is only 1 output that is not PrintOP |
| 130 | cur = cur.ops.itervalues().next() |
no test coverage detected
searching dependent graphs…