1 #include "caffe2/operators/mod_op.h" 3 #include "caffe2/core/operator.h" 4 #include "caffe2/core/tensor.h" 10 bool ModOp<CPUContext>::DoRunWithType() {
11 auto& data = Input(DATA);
12 auto N = data.numel();
13 const auto* data_ptr = data.template data<T>();
15 auto* output = Output(0, Input(DATA).sizes(), at::dtype<T>());
16 auto* output_ptr = output->template mutable_data<T>();
18 for (
auto i = 0; i < N; i++) {
19 output_ptr[i] = data_ptr[i] % divisor_;
20 if (output_ptr[i] && sign_follow_divisor_ &&
21 ((output_ptr[i] > 0) != (divisor_ > 0))) {
22 output_ptr[i] += divisor_;
30 REGISTER_CPU_OPERATOR(Mod, ModOp<CPUContext>);
34 .Arg(
"divisor",
"*(type: int; default: 0)* Divisor of the modulo operation (must be >= 1).")
36 "sign_follow_divisor",
37 "*(type: bool; default: False)* If true, sign of output matches divisor, else if false, sign follows dividend.")
38 .IdenticalTypeAndShape()
39 .AllowInplace({{0, 0}})
41 Element-wise modulo operation. Each element in the output is the modulo result 42 of the corresponding element in the input data. The divisor of the modulo is 43 provided by the `divisor` argument. 46 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/mod_op.cc 50 <summary> <b>Example</b> </summary> 56 workspace.ResetWorkspace() 58 op = core.CreateOperator( 65 workspace.FeedBlob("X", (np.random.randint(100, size=(5,5)))) 66 print("X before running op:", workspace.FetchBlob("X")) 67 workspace.RunOperatorOnce(op) 68 print("X after running op:", workspace.FetchBlob("Y")) 94 .Input(0, "X",
"*(type: Tensor`<int>`)* Input tensor with int32 or int64 data.")
95 .Output(0,
"Y",
"*(type: Tensor`<int>`)* Output tensor of data with modulo operation applied.");
97 SHOULD_NOT_DO_GRADIENT(ModOp);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...