A helper function to add a While op to the net. Same rules for determining outer and inner blobs as for the 'If' operator apply for the 'While' operator loop and condition subnets. If specified, condition net is executed in a separate workspace before the first and after each iterat
(
while_net, cond_blob, lexical_scope, loop_body_net, condition_body_net=None)
| 139 | |
| 140 | |
| 141 | def add_while_op( |
| 142 | while_net, cond_blob, lexical_scope, loop_body_net, condition_body_net=None): |
| 143 | """ |
| 144 | A helper function to add a While op to the net. Same rules for determining |
| 145 | outer and inner blobs as for the 'If' operator apply for the 'While' operator |
| 146 | loop and condition subnets. If specified, condition net is executed in a separate |
| 147 | workspace before the first and after each iteration, the last operator must have |
| 148 | a single scalar boolean output that is written into the condition blob. |
| 149 | Inputs: |
| 150 | while_net - net to add a While op to; |
| 151 | cond_blob - scalar bool blob reference, used as a stop condition; |
| 152 | lexical_scope - a set of outer blob names visible to the loop's body; |
| 153 | loop_body_net - net to execute on each iteration; |
| 154 | condition_body_net - net to compute condition value |
| 155 | """ |
| 156 | input_blob_names, output_blob_names = get_external_blob_names( |
| 157 | loop_body_net, lexical_scope) |
| 158 | |
| 159 | # Since it's possible that loop is not going to run even once |
| 160 | # we have to add loop's external outputs into inputs |
| 161 | input_blob_names |= output_blob_names |
| 162 | |
| 163 | loop_inputs = [core.BlobReference(name=b, net=None) for b in input_blob_names] |
| 164 | loop_outputs = [core.BlobReference(name=b, net=None) for b in output_blob_names] |
| 165 | |
| 166 | while_inputs = [cond_blob] + loop_inputs |
| 167 | while_outputs = [] + loop_outputs |
| 168 | |
| 169 | do_loop_body_net = core.Net('do_loop_body_net') |
| 170 | |
| 171 | loop_input_output_names_ordered = [ |
| 172 | str(b) for b in (loop_inputs + loop_outputs)] |
| 173 | loop_body_outer_blob_names = list(input_blob_names | output_blob_names) |
| 174 | loop_body_outer_blob_names_idx = [ |
| 175 | loop_input_output_names_ordered.index(b) for b in loop_body_outer_blob_names] |
| 176 | |
| 177 | do_loop_body_workspace_blob = \ |
| 178 | while_net.NextScopedBlob(while_net.Name() + '/workspace_loop_body') |
| 179 | |
| 180 | loop_inputs.append(do_loop_body_workspace_blob) |
| 181 | loop_outputs.append(do_loop_body_workspace_blob) |
| 182 | # make sure that added workspace pointer blobs are in While inputs/outputs |
| 183 | while_inputs.append(do_loop_body_workspace_blob) |
| 184 | while_outputs.append(do_loop_body_workspace_blob) |
| 185 | |
| 186 | do_loop_body_net.Do( |
| 187 | loop_inputs, |
| 188 | loop_outputs, |
| 189 | net=loop_body_net.Proto(), |
| 190 | inner_blobs=loop_body_outer_blob_names, |
| 191 | outer_blobs_idx=loop_body_outer_blob_names_idx, |
| 192 | copy_external_blobs=True) |
| 193 | do_loop_body_net.AddExternalOutput(*loop_outputs) |
| 194 | |
| 195 | while_args = {} |
| 196 | while_args['loop_net'] = do_loop_body_net.Proto() |
| 197 | |
| 198 | cond_workspace_blob = None |
no test coverage detected
searching dependent graphs…