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