Caffe2 - C++ API
A deep learning, cross platform ML framework
mod_op.cc
1 
17 #include "caffe2/operators/mod_op.h"
18 
19 #include "caffe2/core/operator.h"
20 #include "caffe2/core/tensor.h"
21 
22 namespace caffe2 {
23 
24 template <>
25 template <typename T>
26 bool ModOp<CPUContext>::DoRunWithType() {
27  auto& data = Input(DATA);
28  auto N = data.size();
29  const auto* data_ptr = data.template data<T>();
30 
31  auto* output = Output(0);
32  output->ResizeLike(Input(DATA));
33  auto* output_ptr = output->template mutable_data<T>();
34 
35  for (auto i = 0; i < N; i++) {
36  output_ptr[i] = data_ptr[i] % divisor_;
37  if (output_ptr[i] && sign_follow_divisor_ &&
38  ((output_ptr[i] > 0) != (divisor_ > 0))) {
39  output_ptr[i] += divisor_;
40  }
41  }
42  return true;
43 }
44 
45 namespace {
46 
47 REGISTER_CPU_OPERATOR(Mod, ModOp<CPUContext>);
48 OPERATOR_SCHEMA(Mod)
49  .NumInputs(1)
50  .NumOutputs(1)
51  .Arg("divisor", "The divisor of the modulo operation. Must >= 1")
52  .Arg(
53  "sign_follow_divisor",
54  "The sign of output follows Dividend if set to `false`. \
55  Otherwise follows Divisor")
56  .IdenticalTypeAndShape()
57  .AllowInplace({{0, 0}})
58  .SetDoc(R"DOC(
59 Elementwise modulo operation. Each element in the output is the modulo result
60 of the corresponding elment in the input data. The divisor of the modulo is
61 provided by the operator argument `divisor`.
62 )DOC")
63  .Input(0, "data", "input int32 or int64 data")
64  .Output(0, "output", "output of data with modulo operation applied");
65 
66 SHOULD_NOT_DO_GRADIENT(ModOp);
67 } // namespace
68 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.