Caffe2 - C++ API
A deep learning, cross platform ML framework
assert_op.cc
1 #include "caffe2/operators/assert_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(Assert, AssertOp<CPUContext>);
6 
7 OPERATOR_SCHEMA(Assert)
8  .NumInputs(1)
9  .NumOutputs(0)
10  .SetDoc(R"DOC(
11 Takes in a tensor of type *bool*, *int*, *long*, or *long long* and checks if all values are True when coerced into a boolean. In other words, for non-bool types this asserts that all values in the tensor are non-zero. If a value is False after coerced into a boolean, the operator throws an error. Else, if all values are True, nothing is returned. For tracability, a custom error message can be set using the `error_msg` arguement.
12 
13 Github Links:
14 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/assert_op.cc
15 
16 <details>
17 
18 <summary> <b>Example</b> </summary>
19 
20 **Code**
21 
22 ```
23 
24 workspace.ResetWorkspace()
25 
26 op = core.CreateOperator(
27  "Assert",
28  ["A"],
29  [],
30  error_msg="Failed assertion from Assert operator"
31 )
32 
33 workspace.FeedBlob("A", np.random.randint(10, size=(3,3)).astype(np.int32))
34 print("A:", workspace.FetchBlob("A"))
35 try:
36  workspace.RunOperatorOnce(op)
37 except RuntimeError:
38  print("Assertion Failed!")
39 else:
40  print("Assertion Passed!")
41 
42 ```
43 
44 **Result**
45 
46 ```
47 
48 A:
49 [[7 5 6]
50  [1 2 4]
51  [5 3 7]]
52 Assertion Passed!
53 
54 ```
55 
56 </details>
57 
58  )DOC")
59  .Arg(
60  "error_msg",
61  "(*string*): custom error message to be thrown when the input does not pass assertion",
62  false)
63  .Input(0,"X","(*Tensor*): input tensor");
64 
65 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13