Caffe2 - C++ API
A deep learning, cross platform ML framework
elementwise_logical_ops.cc
1 #include "caffe2/operators/elementwise_logical_ops.h"
2 
3 namespace caffe2 {
4 namespace {
5 
6 REGISTER_CPU_OPERATOR(Where, WhereOp<CPUContext>);
7 
8 // Input: C, X, Y, output: Z
9 OPERATOR_SCHEMA(Where)
10  .NumInputs(3)
11  .NumOutputs(1)
12  .AllowInplace({{1, 2}})
13  .IdenticalTypeAndShapeOfInput(1)
14  .SetDoc(R"DOC(
15 Operator Where takes three input data (Tensor<bool>, Tensor<T>, Tensor<T>) and
16 produces one output data (Tensor<T>) where z = c ? x : y is applied elementwise.
17 )DOC")
18  .Input(0, "C", "input tensor containing booleans")
19  .Input(1, "X", "input tensor")
20  .Input(2, "Y", "input tensor")
21  .Output(0, "Z", "output tensor");
22 
23 SHOULD_NOT_DO_GRADIENT(Where);
24 
25 REGISTER_CPU_OPERATOR(IsMemberOf, IsMemberOfOp<CPUContext>);
26 
27 // Input: X, output: Y
28 OPERATOR_SCHEMA(IsMemberOf)
29  .NumInputs(1)
30  .NumOutputs(1)
31  .TensorInferenceFunction(
32  [](const OperatorDef&, const vector<TensorShape>& input_types) {
33  vector<TensorShape> out(1);
34  out[0] = input_types[0];
35  out[0].set_data_type(TensorProto_DataType::TensorProto_DataType_BOOL);
36  return out;
37  })
38  .Arg("value", "Declare one value for the membership test.")
39  .Arg(
40  "dtype",
41  "The data type for the elements of the output tensor."
42  "Strictly must be one of the types from DataType enum in TensorProto.")
43  .SetDoc(R"DOC(
44 IsMemberOf takes input data (Tensor<T>) and a list of values as argument, and
45 produces one output data (Tensor<bool>) where the function `f(x) = x in values`,
46 is applied to the data tensor elementwise.
47 )DOC")
48  .Input(0, "X", "Input tensor of any shape")
49  .Output(0, "Y", "Output tensor (same size as X containing booleans)");
50 
51 SHOULD_NOT_DO_GRADIENT(IsMemberOf);
52 
53 } // namespace
54 
55 template <>
56 std::unordered_set<int32_t>& IsMemberOfValueHolder::get<int32_t>() {
57  return int32_values_;
58 }
59 
60 template <>
61 std::unordered_set<int64_t>& IsMemberOfValueHolder::get<int64_t>() {
62  return int64_values_;
63 }
64 
65 template <>
66 std::unordered_set<bool>& IsMemberOfValueHolder::get<bool>() {
67  return bool_values_;
68 }
69 
70 template <>
71 std::unordered_set<string>& IsMemberOfValueHolder::get<string>() {
72  return string_values_;
73 }
74 
75 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...