control_ops_util.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ## @package control_ops_util
  2. # Module caffe2.python.control_ops_util
  3. from caffe2.python import core
  4. def get_external_blob_names(net, lexical_scope):
  5. """
  6. Returns a set of blobs a given net depends on and a set of
  7. output blobs that are written by the net
  8. Inputs:
  9. net - net to return input/output blobs for;
  10. lexical_scope - all external blob names visible to the net
  11. """
  12. # Use the blobs that are actually read/written to as external inputs/outputs
  13. net_proto = net.Proto()
  14. net_ssa, _ = core.get_ssa(net_proto)
  15. input_names = core.get_undefined_blobs(net_ssa)
  16. for input_name in input_names:
  17. assert str(input_name) in lexical_scope, \
  18. "Input blob " + input_name + " is undefined"
  19. output_names = set()
  20. for op in net_proto.op:
  21. for output in op.output:
  22. if output in lexical_scope:
  23. output_names.add(output)
  24. return input_names, output_names
  25. def add_if_op(if_net, cond_blob, lexical_scope, then_net, else_net=None):
  26. """
  27. A helper function to add an If op to the net.
  28. Automatically determines whether blobs in the then/else subnets are external
  29. (from the outer workspace) or local (visible only inside subnet's workspace)
  30. based on lexical scope - set of all outer blob names visible to the 'If'
  31. operator. All the blobs in then/else subnets with names matching a name in lexical
  32. scope and all the blobs that are first used as the operators' inputs are
  33. considered outer blobs - these blobs must exist in the outer workspace,
  34. then/else subnets can read their values and new values written into these blobs
  35. will be visible outside of the 'If' operator. All other blobs are local - exist
  36. only within inner workspaces for then/else.
  37. Inputs:
  38. if_net - net to add an If op to;
  39. cond_blob - scalar bool blob reference, used as If condition;
  40. lexical_scope - a set of outer blob names visible to then/else branches;
  41. then_net/else_net - nets (core.Net) for then/else branches
  42. """
  43. then_input_blob_names, then_output_blob_names = get_external_blob_names(
  44. then_net, lexical_scope)
  45. else_input_blob_names = set()
  46. else_output_blob_names = set()
  47. if else_net:
  48. else_input_blob_names, else_output_blob_names = get_external_blob_names(
  49. else_net, lexical_scope)
  50. input_blob_names = then_input_blob_names | else_input_blob_names
  51. output_blob_names = then_output_blob_names | else_output_blob_names
  52. if_inputs = [cond_blob]
  53. if_inputs += [core.BlobReference(name=b, net=None) for b in input_blob_names]
  54. if_outputs = [core.BlobReference(name=b, net=None) for b in output_blob_names]
  55. do_then_net = core.Net('do_then_net')
  56. then_input_blobs = \
  57. [core.BlobReference(name=b, net=None) for b in then_input_blob_names]
  58. then_output_blobs = \
  59. [core.BlobReference(name=b, net=None) for b in then_output_blob_names]
  60. then_input_output_names_ordered = [
  61. str(b) for b in (then_input_blobs + then_output_blobs)]
  62. then_outer_blob_names = list(then_input_blob_names | then_output_blob_names)
  63. then_outer_blob_names_idx = [
  64. then_input_output_names_ordered.index(b) for b in then_outer_blob_names]
  65. # make sure to use net's name to have unique blob name across multiple subnets
  66. do_then_workspace_blob = if_net.NextScopedBlob(if_net.Name() + '/workspace_if_then')
  67. then_input_blobs.append(do_then_workspace_blob)
  68. then_output_blobs.append(do_then_workspace_blob)
  69. # make sure that added workspace pointer blobs are in if inputs/outputs
  70. if_inputs.append(do_then_workspace_blob)
  71. if_outputs.append(do_then_workspace_blob)
  72. do_then_net.Do(
  73. then_input_blobs,
  74. then_output_blobs,
  75. net=then_net.Proto(),
  76. inner_blobs=then_outer_blob_names,
  77. outer_blobs_idx=then_outer_blob_names_idx)
  78. do_then_net.AddExternalOutput(*then_output_blobs)
  79. if_args = {}
  80. if_args['then_net'] = do_then_net.Proto()
  81. do_else_workspace_blob = None
  82. if else_net:
  83. do_else_net = core.Net('do_else_net')
  84. else_input_blobs = \
  85. [core.BlobReference(name=b, net=None) for b in else_input_blob_names]
  86. else_output_blobs = \
  87. [core.BlobReference(name=b, net=None) for b in else_output_blob_names]
  88. else_input_output_names_ordered = [
  89. str(b) for b in (else_input_blobs + else_output_blobs)]
  90. else_outer_blob_names = list(else_input_blob_names | else_output_blob_names)
  91. else_outer_blob_names_idx = [
  92. else_input_output_names_ordered.index(b) for b in else_outer_blob_names]
  93. do_else_workspace_blob = \
  94. if_net.NextScopedBlob(if_net.Name() + '/workspace_if_else')
  95. else_input_blobs.append(do_else_workspace_blob)
  96. else_output_blobs.append(do_else_workspace_blob)
  97. # make sure that added workspace pointer blobs are in if inputs/outputs
  98. if_inputs.append(do_else_workspace_blob)
  99. if_outputs.append(do_else_workspace_blob)
  100. do_else_net.Do(
  101. else_input_blobs,
  102. else_output_blobs,
  103. net=else_net.Proto(),
  104. inner_blobs=else_outer_blob_names,
  105. outer_blobs_idx=else_outer_blob_names_idx)
  106. do_else_net.AddExternalOutput(*else_output_blobs)
  107. if_args['else_net'] = do_else_net.Proto()
  108. if_net.CreateScope([], [do_then_workspace_blob])
  109. if do_else_workspace_blob:
  110. if_net.CreateScope([], [do_else_workspace_blob])
  111. if_net.If(if_inputs, if_outputs, **if_args)
  112. if_net.AddExternalOutput(*if_outputs)
  113. def add_while_op(
  114. while_net, cond_blob, lexical_scope, loop_body_net, condition_body_net=None):
  115. """
  116. A helper function to add a While op to the net. Same rules for determining
  117. outer and inner blobs as for the 'If' operator apply for the 'While' operator
  118. loop and condition subnets. If specified, condition net is executed in a separate
  119. workspace before the first and after each iteration, the last operator must have
  120. a single scalar boolean output that is written into the condition blob.
  121. Inputs:
  122. while_net - net to add a While op to;
  123. cond_blob - scalar bool blob reference, used as a stop condition;
  124. lexical_scope - a set of outer blob names visible to the loop's body;
  125. loop_body_net - net to execute on each iteration;
  126. condition_body_net - net to compute condition value
  127. """
  128. input_blob_names, output_blob_names = get_external_blob_names(
  129. loop_body_net, lexical_scope)
  130. # Since it's possible that loop is not going to run even once
  131. # we have to add loop's external outputs into inputs
  132. input_blob_names |= output_blob_names
  133. loop_inputs = [core.BlobReference(name=b, net=None) for b in input_blob_names]
  134. loop_outputs = [core.BlobReference(name=b, net=None) for b in output_blob_names]
  135. while_inputs = [cond_blob] + loop_inputs
  136. while_outputs = [] + loop_outputs
  137. do_loop_body_net = core.Net('do_loop_body_net')
  138. loop_input_output_names_ordered = [
  139. str(b) for b in (loop_inputs + loop_outputs)]
  140. loop_body_outer_blob_names = list(input_blob_names | output_blob_names)
  141. loop_body_outer_blob_names_idx = [
  142. loop_input_output_names_ordered.index(b) for b in loop_body_outer_blob_names]
  143. do_loop_body_workspace_blob = \
  144. while_net.NextScopedBlob(while_net.Name() + '/workspace_loop_body')
  145. loop_inputs.append(do_loop_body_workspace_blob)
  146. loop_outputs.append(do_loop_body_workspace_blob)
  147. # make sure that added workspace pointer blobs are in While inputs/outputs
  148. while_inputs.append(do_loop_body_workspace_blob)
  149. while_outputs.append(do_loop_body_workspace_blob)
  150. do_loop_body_net.Do(
  151. loop_inputs,
  152. loop_outputs,
  153. net=loop_body_net.Proto(),
  154. inner_blobs=loop_body_outer_blob_names,
  155. outer_blobs_idx=loop_body_outer_blob_names_idx,
  156. copy_external_blobs=True)
  157. do_loop_body_net.AddExternalOutput(*loop_outputs)
  158. while_args = {}
  159. while_args['loop_net'] = do_loop_body_net.Proto()
  160. cond_workspace_blob = None
  161. if condition_body_net:
  162. cond_input_blob_names, cond_output_blob_names = get_external_blob_names(
  163. condition_body_net, lexical_scope)
  164. # make sure condition blob is written by condition net and is
  165. # visible outside of it
  166. found_condition_output = False
  167. for op in condition_body_net.Proto().op:
  168. if str(cond_blob) in op.output:
  169. found_condition_output = True
  170. break
  171. assert found_condition_output, \
  172. "Condition net does not write into condition blob"
  173. if str(cond_blob) not in cond_output_blob_names:
  174. cond_output_blob_names.add(str(cond_blob))
  175. cond_inputs = [core.BlobReference(name=b, net=None)
  176. for b in cond_input_blob_names]
  177. assert str(cond_blob) in cond_output_blob_names, \
  178. 'Condition blob expected in condition net output'
  179. cond_outputs = [core.BlobReference(name=b, net=None)
  180. for b in cond_output_blob_names]
  181. condition_net = core.Net('do_loop_condition_net')
  182. cond_input_output_names_ordered = [
  183. str(b) for b in (cond_inputs + cond_outputs)]
  184. cond_body_outer_blob_names = \
  185. list(cond_input_blob_names | cond_output_blob_names)
  186. cond_body_outer_blob_names_idx = [
  187. cond_input_output_names_ordered.index(b)
  188. for b in cond_body_outer_blob_names]
  189. cond_workspace_blob = \
  190. while_net.NextScopedBlob(while_net.Name() + '/workspace_loop_cond')
  191. cond_inputs.append(cond_workspace_blob)
  192. cond_outputs.append(cond_workspace_blob)
  193. condition_net.Do(
  194. cond_inputs,
  195. cond_outputs,
  196. net=condition_body_net.Proto(),
  197. inner_blobs=cond_body_outer_blob_names,
  198. outer_blobs_idx=cond_body_outer_blob_names_idx)
  199. condition_net.AddExternalOutput(*cond_outputs)
  200. while_args['cond_net'] = condition_net.Proto()
  201. while_inputs += [b for b in cond_inputs
  202. if str(b) not in input_blob_names]
  203. while_outputs += [b for b in cond_outputs
  204. if str(b) not in output_blob_names]
  205. if str(cond_blob) not in lexical_scope:
  206. while_net.ConstantFill(
  207. [],
  208. cond_blob,
  209. dtype=core.DataType.BOOL,
  210. value=False)
  211. while_net.CreateScope([], [do_loop_body_workspace_blob])
  212. if cond_workspace_blob:
  213. while_net.CreateScope([], [cond_workspace_blob])
  214. while_net.While(while_inputs, while_outputs, **while_args)
  215. while_net.AddExternalOutput(*while_outputs)