Caffe2 - C++ API
A deep learning, cross platform ML framework
minmax_ops.cc
1 
17 #include "caffe2/operators/minmax_ops.h"
18 
19 namespace caffe2 {
20 
21 REGISTER_CPU_OPERATOR(Max, MaxOp<float, CPUContext>);
22 REGISTER_CPU_OPERATOR(Min, MinOp<float, CPUContext>);
23 
24 OPERATOR_SCHEMA(Max)
25  .NumInputs(1, INT_MAX)
26  .NumOutputs(1)
27  .IdenticalTypeAndShapeOfInput(0)
28  .AllowInplace({{0, 0}})
29  .SetDoc(R"DOC(
30 Element-wise max of each of the input tensors. The first input tensor can be
31 used in-place as the output tensor, in which case the max will be done in
32 place and results will be accumulated in input0. All inputs and outputs must
33 have the same shape and data type.
34 )DOC")
35  .Input(0, "data_0", "First of the input tensors. Can be inplace.")
36  .Output(0, "max", "Output tensor. Same dimension as inputs.");
37 
38 OPERATOR_SCHEMA(Min)
39  .NumInputs(1, INT_MAX)
40  .NumOutputs(1)
41  .IdenticalTypeAndShapeOfInput(0)
42  .AllowInplace({{0, 0}})
43  .SetDoc(R"DOC(
44 Element-wise min of each of the input tensors. The first input tensor can be
45 used in-place as the output tensor, in which case the min will be done in
46 place and results will be accumulated in input0. All inputs and outputs must
47 have the same shape and data type.
48 )DOC")
49  .Input(0, "data_0", "First of the input tensors. Can be inplace.")
50  .Output(0, "min", "Output tensor. Same dimension as inputs.");
51 
52 template <typename T, class Context>
53 bool MaxOp<T, Context>::Compute() {
54  auto& input0 = Input(0);
55  const int N = input0.size();
56  T* output_data = Output(0)->template mutable_data<T>();
57 
58  for (int i = 1; i < InputSize(); i++) {
59  auto input_data = Input(i).template data<T>();
60  EigenVectorMap<T> output_vec(output_data, N);
61  output_vec = output_vec.cwiseMax(ConstEigenVectorMap<T>(input_data, N));
62  }
63 
64  return true;
65 }
66 
67 template <typename T, class Context>
68 bool MinOp<T, Context>::Compute() {
69  auto& input0 = Input(0);
70  const int N = input0.size();
71  T* output_data = Output(0)->template mutable_data<T>();
72 
73  for (int i = 1; i < InputSize(); i++) {
74  auto input_data = Input(i).template data<T>();
75  EigenVectorMap<T> output_vec(output_data, N);
76  output_vec = output_vec.cwiseMin(ConstEigenVectorMap<T>(input_data, N));
77  }
78 
79  return true;
80 }
81 
82 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.