Caffe2 - C++ API
A deep learning, cross platform ML framework
onnx_while_op.cc
1 
17 #include "caffe2/operators/onnx_while_op.h"
18 
19 namespace caffe2 {
20 
21 REGISTER_CPU_OPERATOR(ONNXWhile, ONNXWhileOp<CPUContext>);
22 
23 OPERATOR_SCHEMA(ONNXWhile)
24  .NumInputs(2, INT_MAX)
25  .NumOutputs(0, INT_MAX)
26  .SetDoc(R"DOC(
27 *** EXPERIMENTAL. This operator is a work-in-progress. No assumption should be
28 made about the stability or correctness of this op. ***
29 
30 Generic Looping construct confirming to the ONNX Loop operator spec. This loop
31 has multiple termination conditions:
32 
33 1. Trip count. Iteration count specified at runtime. Set by specifying the
34  input M. Optional. Set to empty string to omit. Note that a static trip
35  count (specified at graph construction time) can be specified by passing
36  in a constant node for input M.
37 2. Loop termination condition. This is an input to the op that determines
38  whether to run the first interation and also a loop-carried dependency for
39  the body graph. The body graph must yield a value for the condition
40  variable, whether this input is provided or not.
41 
42 This table summarizes the operating modes of this operator with equivalent
43 C-style code:
44 
45 Operator inputs defined as (max_trip_count, condition_var). Omitted optional
46 inputs are represented as empty string. Concretely, in this caffe2 op an input
47 is marked as omitted by setting its 'has_{name}' argument to False.
48 
49  input ("", ""):
50  for (int i=0; ; ++i) {
51  cond = ... // Note this value is ignored, but is required in the body
52  }
53 
54  input ("", cond) // Note this is analogous to a while loop
55  bool cond = ...;
56  for (int i=0; cond; ++i) {
57  cond = ...;
58  }
59 
60  input ("", 1) // Note this is analogous to a do-while loop
61  bool cond = true
62  for (int i=0; cond; ++i) {
63  cond = ...;
64  }
65 
66  input (trip_count, "") // Note this is analogous to a for loop
67  int trip_count = ...
68  for (int i=0; i < trip_count; ++i) {
69  cond = ...; // ignored
70  }
71 
72  input (trip_count, cond)
73  int trip_count = ...;
74  bool cond = ...;
75  for (int i=0; i < trip_count && cond; ++i) {
76  cond = ...;
77  }
78  )DOC")
79  .Arg("loop_net", "Net executed on each iteration")
80  .Input(0, "condition", "Scalar boolean condition")
81  .AllowInplace([](int in, int out) -> bool { return true; });
82 
83 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.