NaiveConvOp.cpp 5.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

H
Bug fix  
hedaoyuan 已提交
15
#include "ConvOp.h"
H
hedaoyuan 已提交
16 17 18 19 20 21 22 23 24 25 26

namespace paddle {

/*
 * The three arguments are stored in memory in row major order.
 * inputData  = [batchSize, inputChannels, inputHeight, inputWidth]
 * filterData = [outputChannels, inputChannels, filterHeight, filterWidth]
 * outputData = [batchSize, outputChannels, outputHeight, outputWidth]
 */
template <class T>
class NaiveConvFunctor {
W
Wu Yi 已提交
27
 public:
H
hedaoyuan 已提交
28 29 30 31 32 33 34 35 36 37 38 39
  void operator()(const T* inputData,
                  size_t batchSize,
                  size_t inputChannels,
                  size_t inputHeight,
                  size_t inputWidth,
                  const T* filterData,
                  size_t filterHeight,
                  size_t filterWidth,
                  T* outputData,
                  size_t outputChannels,
                  size_t outputHeight,
                  size_t outputWidth,
40 41 42 43
                  size_t paddingH,
                  size_t paddingW,
                  size_t strideH,
                  size_t strideW) {
H
hedaoyuan 已提交
44 45 46 47
    for (size_t batch = 0; batch < batchSize; batch++) {
      for (size_t outC = 0; outC < outputChannels; outC++) {
        for (size_t outH = 0; outH < outputHeight; outH++) {
          for (size_t outW = 0; outW < outputWidth; outW++) {
48 49
            const int inStartH = (outH * strideH) - paddingH;
            const int inStartW = (outW * strideW) - paddingW;
H
hedaoyuan 已提交
50 51 52 53 54 55 56
            T outValue = (T)0;
            for (size_t inC = 0; inC < inputChannels; inC++) {
              for (size_t fH = 0; fH < filterHeight; fH++) {
                for (size_t fW = 0; fW < filterWidth; fW++) {
                  T inValue;
                  const int inH = inStartH + fH;
                  const int inW = inStartW + fW;
L
liaogang 已提交
57 58
                  if ((inH >= 0 && inH < (int)inputHeight) &&
                      (inW >= 0 && inW < (int)inputWidth)) {
H
hedaoyuan 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                    size_t offsetInput =
                        batch * inputChannels * inputHeight * inputWidth +
                        inC * inputHeight * inputWidth + inH * inputWidth + inW;
                    inValue = inputData[offsetInput];
                  } else {
                    inValue = (T)0;
                  }
                  size_t offsetFilter =
                      outC * inputChannels * filterHeight * filterWidth +
                      inC * filterHeight * filterWidth + fH * filterWidth + fW;
                  T filterValue = filterData[offsetFilter];
                  outValue += (inValue * filterValue);
                }
              }
            }

            size_t offset =
                batch * outputChannels * outputHeight * outputWidth +
                outC * outputHeight * outputWidth + outH * outputWidth + outW;
            outputData[offset] = outValue;
          }
        }
      }
    }
  }
};

template <DeviceType Device>
class NaiveConvFunction : public ConvFunctionBase {
W
Wu Yi 已提交
88
 public:
H
hedaoyuan 已提交
89 90 91 92
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

L
liaogang 已提交
93
  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
94 95 96
    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();
H
hedaoyuan 已提交
97 98 99 100 101 102
    checkShape(input, filter, output);
  }

  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
H
hedaoyuan 已提交
103
    CHECK_EQ(outputs[0].getArgType(), ASSIGN_TO);
H
hedaoyuan 已提交
104
    check(inputs, outputs);
H
hedaoyuan 已提交
105 106 107 108 109 110

    size_t batchSize = inputs[0].shape()[0];
    size_t inputChannels = inputs[0].shape()[1];
    size_t inputHeight = inputs[0].shape()[2];
    size_t inputWidth = inputs[0].shape()[3];
    size_t filterHeight = inputs[1].shape()[2];
111
    size_t filterWidth = inputs[1].shape()[3];
H
hedaoyuan 已提交
112 113 114 115
    size_t outputChannels = outputs[0].shape()[1];
    size_t outputHeight = outputs[0].shape()[2];
    size_t outputWidth = outputs[0].shape()[3];

H
Bug fix  
hedaoyuan 已提交
116 117 118 119
    real* inputData = inputs[0].data<real>();
    real* filterData = inputs[1].data<real>();
    real* outputData = outputs[0].data<real>();
    NaiveConvFunctor<real> conv;
H
hedaoyuan 已提交
120 121 122 123 124 125 126 127 128 129 130 131
    conv(inputData,
         batchSize,
         inputChannels,
         inputHeight,
         inputWidth,
         filterData,
         filterHeight,
         filterWidth,
         outputData,
         outputChannels,
         outputHeight,
         outputWidth,
132 133 134 135
         paddingH(),
         paddingW(),
         strideH(),
         strideW());
H
hedaoyuan 已提交
136 137 138 139 140 141
  }
};

REGISTER_TYPED_FUNC(NaiveConv, CPU, NaiveConvFunction);

}  // namespace paddle