Caffe2 - C++ API
A deep learning, cross platform ML framework
mod_op.cc
1 #include "caffe2/operators/mod_op.h"
2 
3 #include "caffe2/core/operator.h"
4 #include "caffe2/core/tensor.h"
5 
6 namespace caffe2 {
7 
8 template <>
9 template <typename T>
10 bool ModOp<CPUContext>::DoRunWithType() {
11  auto& data = Input(DATA);
12  auto N = data.size();
13  const auto* data_ptr = data.template data<T>();
14 
15  auto* output = Output(0);
16  output->ResizeLike(Input(DATA));
17  auto* output_ptr = output->template mutable_data<T>();
18 
19  for (auto i = 0; i < N; i++) {
20  output_ptr[i] = data_ptr[i] % divisor_;
21  if (output_ptr[i] && sign_follow_divisor_ &&
22  ((output_ptr[i] > 0) != (divisor_ > 0))) {
23  output_ptr[i] += divisor_;
24  }
25  }
26  return true;
27 }
28 
29 namespace {
30 
31 REGISTER_CPU_OPERATOR(Mod, ModOp<CPUContext>);
32 OPERATOR_SCHEMA(Mod)
33  .NumInputs(1)
34  .NumOutputs(1)
35  .Arg("divisor", "The divisor of the modulo operation. Must >= 1")
36  .Arg(
37  "sign_follow_divisor",
38  "The sign of output follows Dividend if set to `false`. \
39  Otherwise follows Divisor")
40  .IdenticalTypeAndShape()
41  .AllowInplace({{0, 0}})
42  .SetDoc(R"DOC(
43 Elementwise modulo operation. Each element in the output is the modulo result
44 of the corresponding elment in the input data. The divisor of the modulo is
45 provided by the operator argument `divisor`.
46 )DOC")
47  .Input(0, "data", "input int32 or int64 data")
48  .Output(0, "output", "output of data with modulo operation applied");
49 
50 SHOULD_NOT_DO_GRADIENT(ModOp);
51 } // namespace
52 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...