control.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. ## @package control
  2. # Module caffe2.python.control
  3. """
  4. Implement functions for controlling execution of nets and steps, including
  5. Do
  6. DoParallel
  7. For-loop
  8. While-loop
  9. Do-While-loop
  10. Switch
  11. If
  12. """
  13. from caffe2.python import core
  14. from future.utils import viewitems
  15. # Used to generate names of the steps created by the control functions.
  16. # It is actually the internal index of these steps.
  17. _current_idx = 1
  18. _used_step_names = set()
  19. def _get_next_step_name(control_name, base_name):
  20. global _current_idx, _used_step_names
  21. concat_name = '%s/%s' % (base_name, control_name)
  22. next_name = concat_name
  23. while next_name in _used_step_names:
  24. next_name = '%s_%d' % (concat_name, _current_idx)
  25. _current_idx += 1
  26. _used_step_names.add(next_name)
  27. return next_name
  28. def _MakeList(input):
  29. """ input is a tuple.
  30. Example:
  31. (a, b, c) --> [a, b, c]
  32. (a) --> [a]
  33. ([a, b, c]) --> [a, b, c]
  34. """
  35. if len(input) == 0:
  36. raise ValueError(
  37. 'input cannot be empty.')
  38. elif len(input) == 1:
  39. output = input[0]
  40. if not isinstance(output, list):
  41. output = [output]
  42. else:
  43. output = list(input)
  44. return output
  45. def _IsNets(nets_or_steps):
  46. if isinstance(nets_or_steps, list):
  47. return all(isinstance(n, core.Net) for n in nets_or_steps)
  48. else:
  49. return isinstance(nets_or_steps, core.Net)
  50. def _PrependNets(nets_or_steps, *nets):
  51. nets_or_steps = _MakeList((nets_or_steps,))
  52. nets = _MakeList(nets)
  53. if _IsNets(nets_or_steps):
  54. return nets + nets_or_steps
  55. else:
  56. return [Do('prepend', nets)] + nets_or_steps
  57. def _AppendNets(nets_or_steps, *nets):
  58. nets_or_steps = _MakeList((nets_or_steps,))
  59. nets = _MakeList(nets)
  60. if _IsNets(nets_or_steps):
  61. return nets_or_steps + nets
  62. else:
  63. return nets_or_steps + [Do('append', nets)]
  64. def GetConditionBlobFromNet(condition_net):
  65. """
  66. The condition blob is the last external_output that must
  67. be a single bool
  68. """
  69. assert len(condition_net.Proto().external_output) > 0, (
  70. "Condition net %s must has at least one external output" %
  71. condition_net.Proto.name)
  72. # we need to use a blob reference here instead of a string
  73. # otherwise, it will add another name_scope to the input later
  74. # when we create new ops (such as OR of two inputs)
  75. return core.BlobReference(condition_net.Proto().external_output[-1])
  76. def BoolNet(*blobs_with_bool_value):
  77. """A net assigning constant bool values to blobs. It is mainly used for
  78. initializing condition blobs, for example, in multi-task learning, we
  79. need to access reader_done blobs before reader_net run. In that case,
  80. the reader_done blobs must be initialized.
  81. Args:
  82. blobs_with_bool_value: one or more (blob, bool_value) pairs. The net will
  83. assign each bool_value to the corresponding blob.
  84. returns
  85. bool_net: A net assigning constant bool values to blobs.
  86. Examples:
  87. - BoolNet((blob_1, bool_value_1), ..., (blob_n, bool_value_n))
  88. - BoolNet([(blob_1, net1), ..., (blob_n, bool_value_n)])
  89. - BoolNet((cond_1, bool_value_1))
  90. """
  91. blobs_with_bool_value = _MakeList(blobs_with_bool_value)
  92. bool_net = core.Net('bool_net')
  93. for blob, bool_value in blobs_with_bool_value:
  94. out_blob = bool_net.ConstantFill(
  95. [],
  96. [blob],
  97. shape=[],
  98. value=bool_value,
  99. dtype=core.DataType.BOOL)
  100. bool_net.AddExternalOutput(out_blob)
  101. return bool_net
  102. def NotNet(condition_blob_or_net):
  103. """Not of a condition blob or net
  104. Args:
  105. condition_blob_or_net can be either blob or net. If condition_blob_or_net
  106. is Net, the condition is its last external_output
  107. that must be a single bool.
  108. returns
  109. not_net: the net NOT the input
  110. out_blob: the output blob of the not_net
  111. """
  112. if isinstance(condition_blob_or_net, core.Net):
  113. condition_blob = GetConditionBlobFromNet(condition_blob_or_net)
  114. else:
  115. condition_blob = condition_blob_or_net
  116. not_net = core.Net('not_net')
  117. out_blob = not_net.Not(condition_blob)
  118. not_net.AddExternalOutput(out_blob)
  119. return not_net, out_blob
  120. def _CopyConditionBlobNet(condition_blob):
  121. """Make a condition net that copies the condition_blob
  122. Args:
  123. condition_blob is a single bool.
  124. returns
  125. not_net: the net NOT the input
  126. out_blob: the output blob of the not_net
  127. """
  128. condition_net = core.Net('copy_condition_blob_net')
  129. out_blob = condition_net.Copy(condition_blob)
  130. condition_net.AddExternalOutput(out_blob)
  131. return condition_net, out_blob
  132. def MergeConditionNets(name, condition_nets, relation):
  133. """
  134. Merge multi condition nets into a single condition nets.
  135. Args:
  136. name: name of the new condition net.
  137. condition_nets: a list of condition nets. The last external_output
  138. of each condition net must be single bool value.
  139. relation: can be 'And' or 'Or'.
  140. Returns:
  141. - A new condition net. Its last external output is relation of all
  142. condition_nets.
  143. """
  144. if not isinstance(condition_nets, list):
  145. return condition_nets
  146. if len(condition_nets) <= 1:
  147. return condition_nets[0] if condition_nets else None
  148. merged_net = core.Net(name)
  149. for i in range(len(condition_nets)):
  150. net_proto = condition_nets[i].Proto()
  151. assert net_proto.device_option == merged_net.Proto().device_option
  152. assert net_proto.type == merged_net.Proto().type
  153. merged_net.Proto().op.extend(net_proto.op)
  154. merged_net.Proto().external_input.extend(net_proto.external_input)
  155. # discard external outputs as we're combining them together
  156. curr_cond = GetConditionBlobFromNet(condition_nets[i])
  157. if i == 0:
  158. last_cond = curr_cond
  159. else:
  160. last_cond = merged_net.__getattr__(relation)([last_cond, curr_cond])
  161. # merge attributes
  162. for k, v in viewitems(condition_nets[i]._attr_dict):
  163. merged_net._attr_dict[k] += v
  164. merged_net.AddExternalOutput(last_cond)
  165. return merged_net
  166. def CombineConditions(name, condition_nets, relation):
  167. """
  168. Combine conditions of multi nets into a single condition nets. Unlike
  169. MergeConditionNets, the actual body of condition_nets is not copied into
  170. the combine condition net.
  171. One example is about multi readers. Each reader net has a reader_done
  172. condition. When we want to check whether all readers are done, we can
  173. use this function to build a new net.
  174. Args:
  175. name: name of the new condition net.
  176. condition_nets: a list of condition nets. The last external_output
  177. of each condition net must be single bool value.
  178. relation: can be 'And' or 'Or'.
  179. Returns:
  180. - A new condition net. Its last external output is relation of all
  181. condition_nets.
  182. """
  183. if not condition_nets:
  184. return None
  185. if not isinstance(condition_nets, list):
  186. raise ValueError('condition_nets must be a list of nets.')
  187. if len(condition_nets) == 1:
  188. condition_blob = GetConditionBlobFromNet(condition_nets[0])
  189. condition_net, _ = _CopyConditionBlobNet(condition_blob)
  190. return condition_net
  191. combined_net = core.Net(name)
  192. for i in range(len(condition_nets)):
  193. curr_cond = GetConditionBlobFromNet(condition_nets[i])
  194. if i == 0:
  195. last_cond = curr_cond
  196. else:
  197. last_cond = combined_net.__getattr__(relation)(
  198. [last_cond, curr_cond])
  199. combined_net.AddExternalOutput(last_cond)
  200. return combined_net
  201. def Do(name, *nets_or_steps):
  202. """
  203. Execute the sequence of nets or steps once.
  204. Examples:
  205. - Do('myDo', net1, net2, ..., net_n)
  206. - Do('myDo', list_of_nets)
  207. - Do('myDo', step1, step2, ..., step_n)
  208. - Do('myDo', list_of_steps)
  209. """
  210. nets_or_steps = _MakeList(nets_or_steps)
  211. if (len(nets_or_steps) == 1 and isinstance(
  212. nets_or_steps[0], core.ExecutionStep)):
  213. return nets_or_steps[0]
  214. else:
  215. return core.scoped_execution_step(
  216. _get_next_step_name('Do', name), nets_or_steps)
  217. def DoParallel(name, *nets_or_steps):
  218. """
  219. Execute the nets or steps in parallel, waiting for all of them to finish
  220. Examples:
  221. - DoParallel('pDo', net1, net2, ..., net_n)
  222. - DoParallel('pDo', list_of_nets)
  223. - DoParallel('pDo', step1, step2, ..., step_n)
  224. - DoParallel('pDo', list_of_steps)
  225. """
  226. nets_or_steps = _MakeList(nets_or_steps)
  227. if (len(nets_or_steps) == 1 and isinstance(
  228. nets_or_steps[0], core.ExecutionStep)):
  229. return nets_or_steps[0]
  230. else:
  231. return core.scoped_execution_step(
  232. _get_next_step_name('DoParallel', name),
  233. nets_or_steps,
  234. concurrent_substeps=True)
  235. def _RunOnceIf(name, condition_blob_or_net, nets_or_steps):
  236. """
  237. Execute nets_or_steps once if condition_blob_or_net evaluates as true.
  238. If condition_blob_or_net is Net, the condition is its last external_output
  239. that must be a single bool. And this net will be executed before
  240. nets_or_steps so as to get the condition.
  241. """
  242. condition_not_net, stop_blob = NotNet(condition_blob_or_net)
  243. if isinstance(condition_blob_or_net, core.Net):
  244. nets_or_steps = _PrependNets(
  245. nets_or_steps, condition_blob_or_net, condition_not_net)
  246. else:
  247. nets_or_steps = _PrependNets(nets_or_steps, condition_not_net)
  248. def if_step(control_name):
  249. return core.scoped_execution_step(
  250. _get_next_step_name(control_name, name),
  251. nets_or_steps,
  252. should_stop_blob=stop_blob,
  253. only_once=True,
  254. )
  255. if _IsNets(nets_or_steps):
  256. bool_net = BoolNet((stop_blob, False))
  257. return Do(name + '/_RunOnceIf',
  258. bool_net, if_step('_RunOnceIf-inner'))
  259. else:
  260. return if_step('_RunOnceIf')
  261. def _RunOnceIfNot(name, condition_blob_or_net, nets_or_steps):
  262. """
  263. Similar to _RunOnceIf() but Execute nets_or_steps once if
  264. condition_blob_or_net evaluates as false.
  265. """
  266. if isinstance(condition_blob_or_net, core.Net):
  267. condition_blob = GetConditionBlobFromNet(condition_blob_or_net)
  268. nets_or_steps = _PrependNets(nets_or_steps, condition_blob_or_net)
  269. else:
  270. copy_net, condition_blob = _CopyConditionBlobNet(condition_blob_or_net)
  271. nets_or_steps = _PrependNets(nets_or_steps, copy_net)
  272. return core.scoped_execution_step(
  273. _get_next_step_name('_RunOnceIfNot', name),
  274. nets_or_steps,
  275. should_stop_blob=condition_blob,
  276. only_once=True,
  277. )
  278. def For(name, nets_or_steps, iter_num):
  279. """
  280. Execute nets_or_steps iter_num times.
  281. Args:
  282. nets_or_steps: a ExecutionStep or a Net or a list of ExecutionSteps or
  283. a list nets.
  284. iter_num: the number times to execute the nets_or_steps.
  285. Returns:
  286. A ExecutionStep instance.
  287. """
  288. init_net = core.Net('init-net')
  289. iter_cnt = init_net.CreateCounter([], init_count=iter_num)
  290. iter_net = core.Net('For-iter')
  291. iter_done = iter_net.CountDown([iter_cnt])
  292. for_step = core.scoped_execution_step(
  293. _get_next_step_name('For-inner', name),
  294. _PrependNets(nets_or_steps, iter_net),
  295. should_stop_blob=iter_done)
  296. return Do(name + '/For',
  297. Do(name + '/For-init-net', init_net),
  298. for_step)
  299. def While(name, condition_blob_or_net, nets_or_steps):
  300. """
  301. Execute nets_or_steps when condition_blob_or_net returns true.
  302. Args:
  303. condition_blob_or_net: If it is an instance of Net, its last
  304. external_output must be a single bool.
  305. nets_or_steps: a ExecutionStep or a Net or a list of ExecutionSteps or
  306. a list nets.
  307. Returns:
  308. A ExecutionStep instance.
  309. """
  310. condition_not_net, stop_blob = NotNet(condition_blob_or_net)
  311. if isinstance(condition_blob_or_net, core.Net):
  312. nets_or_steps = _PrependNets(
  313. nets_or_steps, condition_blob_or_net, condition_not_net)
  314. else:
  315. nets_or_steps = _PrependNets(nets_or_steps, condition_not_net)
  316. def while_step(control_name):
  317. return core.scoped_execution_step(
  318. _get_next_step_name(control_name, name),
  319. nets_or_steps,
  320. should_stop_blob=stop_blob,
  321. )
  322. if _IsNets(nets_or_steps):
  323. # In this case, while_step has sub-nets:
  324. # [condition_blob_or_net, condition_not_net, nets_or_steps]
  325. # If stop_blob is pre-set to True (this may happen when While() is
  326. # called twice), the loop will exit after executing
  327. # condition_blob_or_net. So we use BootNet to set stop_blob to
  328. # False.
  329. bool_net = BoolNet((stop_blob, False))
  330. return Do(name + '/While', bool_net, while_step('While-inner'))
  331. else:
  332. return while_step('While')
  333. def Until(name, condition_blob_or_net, nets_or_steps):
  334. """
  335. Similar to While() but execute nets_or_steps when
  336. condition_blob_or_net returns false
  337. """
  338. if isinstance(condition_blob_or_net, core.Net):
  339. stop_blob = GetConditionBlobFromNet(condition_blob_or_net)
  340. nets_or_steps = _PrependNets(nets_or_steps, condition_blob_or_net)
  341. else:
  342. stop_blob = core.BlobReference(str(condition_blob_or_net))
  343. return core.scoped_execution_step(
  344. _get_next_step_name('Until', name),
  345. nets_or_steps,
  346. should_stop_blob=stop_blob)
  347. def DoWhile(name, condition_blob_or_net, nets_or_steps):
  348. """
  349. Execute nets_or_steps when condition_blob_or_net returns true. It will
  350. execute nets_or_steps before evaluating condition_blob_or_net.
  351. Args:
  352. condition_blob_or_net: if it is an instance of Net, tts last external_output
  353. must be a single bool.
  354. nets_or_steps: a ExecutionStep or a Net or a list of ExecutionSteps or
  355. a list nets.
  356. Returns:
  357. A ExecutionStep instance.
  358. """
  359. condition_not_net, stop_blob = NotNet(condition_blob_or_net)
  360. if isinstance(condition_blob_or_net, core.Net):
  361. nets_or_steps = _AppendNets(
  362. nets_or_steps, condition_blob_or_net, condition_not_net)
  363. else:
  364. nets_or_steps = _AppendNets(nets_or_steps, condition_not_net)
  365. # If stop_blob is pre-set to True (this may happen when DoWhile() is
  366. # called twice), the loop will exit after executing the first net/step
  367. # in nets_or_steps. This is not what we want. So we use BootNet to
  368. # set stop_blob to False.
  369. bool_net = BoolNet((stop_blob, False))
  370. return Do(name + '/DoWhile', bool_net, core.scoped_execution_step(
  371. _get_next_step_name('DoWhile-inner', name),
  372. nets_or_steps,
  373. should_stop_blob=stop_blob,
  374. ))
  375. def DoUntil(name, condition_blob_or_net, nets_or_steps):
  376. """
  377. Similar to DoWhile() but execute nets_or_steps when
  378. condition_blob_or_net returns false. It will execute
  379. nets_or_steps before evaluating condition_blob_or_net.
  380. Special case: if condition_blob_or_net is a blob and is pre-set to
  381. true, then only the first net/step of nets_or_steps will be executed and
  382. loop is exited. So you need to be careful about the initial value the
  383. condition blob when using DoUntil(), esp when DoUntil() is called twice.
  384. """
  385. if not isinstance(condition_blob_or_net, core.Net):
  386. stop_blob = core.BlobReference(condition_blob_or_net)
  387. return core.scoped_execution_step(
  388. _get_next_step_name('DoUntil', name),
  389. nets_or_steps,
  390. should_stop_blob=stop_blob)
  391. nets_or_steps = _AppendNets(nets_or_steps, condition_blob_or_net)
  392. stop_blob = GetConditionBlobFromNet(condition_blob_or_net)
  393. # If stop_blob is pre-set to True (this may happen when DoWhile() is
  394. # called twice), the loop will exit after executing the first net/step
  395. # in nets_or_steps. This is not what we want. So we use BootNet to
  396. # set stop_blob to False.
  397. bool_net = BoolNet((stop_blob, False))
  398. return Do(name + '/DoUntil', bool_net, core.scoped_execution_step(
  399. _get_next_step_name('DoUntil-inner', name),
  400. nets_or_steps,
  401. should_stop_blob=stop_blob,
  402. ))
  403. def Switch(name, *conditions):
  404. """
  405. Execute the steps for which the condition is true.
  406. Each condition is a tuple (condition_blob_or_net, nets_or_steps).
  407. Note:
  408. 1. Multi steps can be executed if their conditions are true.
  409. 2. The conditions_blob_or_net (if it is Net) of all steps will be
  410. executed once.
  411. Examples:
  412. - Switch('name', (cond_1, net_1), (cond_2, net_2), ..., (cond_n, net_n))
  413. - Switch('name', [(cond_1, net1), (cond_2, net_2), ..., (cond_n, net_n)])
  414. - Switch('name', (cond_1, net_1))
  415. """
  416. conditions = _MakeList(conditions)
  417. return core.scoped_execution_step(
  418. _get_next_step_name('Switch', name),
  419. [_RunOnceIf(name + '/Switch', cond, step) for cond, step in conditions])
  420. def SwitchNot(name, *conditions):
  421. """
  422. Similar to Switch() but execute the steps for which the condition is False.
  423. """
  424. conditions = _MakeList(conditions)
  425. return core.scoped_execution_step(
  426. _get_next_step_name('SwitchNot', name),
  427. [_RunOnceIfNot(name + '/SwitchNot', cond, step)
  428. for cond, step in conditions])
  429. def If(name, condition_blob_or_net,
  430. true_nets_or_steps, false_nets_or_steps=None):
  431. """
  432. condition_blob_or_net is first evaluated or executed. If the condition is
  433. true, true_nets_or_steps is then executed, otherwise, false_nets_or_steps
  434. is executed.
  435. If condition_blob_or_net is Net, the condition is its last external_output
  436. that must be a single bool. And this Net will be executred before both
  437. true/false_nets_or_steps so as to get the condition.
  438. """
  439. if not false_nets_or_steps:
  440. return _RunOnceIf(name + '/If',
  441. condition_blob_or_net, true_nets_or_steps)
  442. if isinstance(condition_blob_or_net, core.Net):
  443. condition_blob = GetConditionBlobFromNet(condition_blob_or_net)
  444. else:
  445. condition_blob = condition_blob_or_net
  446. return Do(
  447. name + '/If',
  448. _RunOnceIf(name + '/If-true',
  449. condition_blob_or_net, true_nets_or_steps),
  450. _RunOnceIfNot(name + '/If-false', condition_blob, false_nets_or_steps)
  451. )
  452. def IfNot(name, condition_blob_or_net,
  453. true_nets_or_steps, false_nets_or_steps=None):
  454. """
  455. If condition_blob_or_net returns false, executes true_nets_or_steps,
  456. otherwise executes false_nets_or_steps
  457. """
  458. if not false_nets_or_steps:
  459. return _RunOnceIfNot(name + '/IfNot',
  460. condition_blob_or_net, true_nets_or_steps)
  461. if isinstance(condition_blob_or_net, core.Net):
  462. condition_blob = GetConditionBlobFromNet(condition_blob_or_net)
  463. else:
  464. condition_blob = condition_blob_or_net
  465. return Do(
  466. name + '/IfNot',
  467. _RunOnceIfNot(name + '/IfNot-true',
  468. condition_blob_or_net, true_nets_or_steps),
  469. _RunOnceIf(name + '/IfNot-false', condition_blob, false_nets_or_steps)
  470. )