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

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. */

#include "cross_map_normal_op.h"
H
hedaoyuan 已提交
16
#include "paddle/math/Vector.h"
H
hedaoyuan 已提交
17 18 19 20

namespace paddle {

// NCHW
H
hedaoyuan 已提交
21
template <>
H
hedaoyuan 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
void CrossMapNormal<DEVICE_TYPE_CPU>(real* outputs,
                                     real* denoms,
                                     real* inputs,
                                     size_t numSamples,
                                     size_t channels,
                                     size_t height,
                                     size_t width,
                                     size_t size,
                                     real scale,
                                     real pow) {
  size_t oneImage = height * width;
  size_t oneSample = channels * oneImage;

  CpuVector outputsV(numSamples * oneSample, outputs);
  CpuVector inputsV(numSamples * oneSample, inputs);
  CpuVector denomsV(numSamples * oneSample, denoms);

  denomsV = denomsV.constant(1.0);
  const int start = -((int)size - 1) / 2;
  const int end = (int)size + start;
  for (size_t i = 0; i < numSamples; i++) {
    real* oneDenom = denoms + i * oneSample;
    real* oneInput = inputs + i * oneSample;
H
hedaoyuan 已提交
45
    for (int c = 0; c < (int)channels; c++) {
H
hedaoyuan 已提交
46
      CpuVector denom(oneImage, oneDenom + c * oneImage);
H
hedaoyuan 已提交
47 48
      for (int s = start; s < end; s++) {
        if (c + s >= 0 && c + s < (int)channels) {
H
hedaoyuan 已提交
49
          CpuVector input(oneImage, oneInput + (c + s) * oneImage);
H
hedaoyuan 已提交
50 51 52 53 54
          denom += input.square() * scale;
        }
      }
    }
  }
H
hedaoyuan 已提交
55 56

  outputsV = inputsV * denomsV.pow(-pow);
H
hedaoyuan 已提交
57 58
}

H
hedaoyuan 已提交
59
template <>
H
hedaoyuan 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
void CrossMapNormalGrad<DEVICE_TYPE_CPU>(real* inputsGrad,
                                         real* inputsValue,
                                         real* outputsValue,
                                         real* outputsGrad,
                                         real* denoms,
                                         size_t numSamples,
                                         size_t channels,
                                         size_t height,
                                         size_t width,
                                         size_t size,
                                         real scale,
                                         real pow) {
  size_t oneSample = channels * height * width;
H
hedaoyuan 已提交
73 74
  std::function<CpuVector(real*, size_t)> oneImage = [=](real* data,
                                                         size_t offset) {
H
hedaoyuan 已提交
75
    return CpuVector(height * width, data + offset);
H
hedaoyuan 已提交
76 77
  };

H
hedaoyuan 已提交
78 79
  const int start = -((int)size) / 2;
  const int end = (int)size + start;
H
hedaoyuan 已提交
80
  const real ratio = -(real)2 * scale * pow;
H
hedaoyuan 已提交
81 82 83 84 85 86 87
  for (size_t i = 0; i < numSamples; i++) {
    size_t sOffset = i * oneSample;
    real* oneInputGrad = inputsGrad + sOffset;
    real* oneInputValue = inputsValue + sOffset;
    real* oneDenom = denoms + sOffset;
    real* oneOutputGrad = outputsGrad + sOffset;
    real* oneOutputValue = outputsValue + sOffset;
H
hedaoyuan 已提交
88 89

    for (int c = 0; c < (int)channels; c++) {
H
hedaoyuan 已提交
90 91 92 93 94
      size_t cOffset = c * height * width;
      CpuVector inputGrad = oneImage(oneInputGrad, cOffset);
      CpuVector inputValue = oneImage(oneInputValue, cOffset);
      CpuVector denom = oneImage(oneDenom, cOffset);
      CpuVector outputGrad = oneImage(oneOutputGrad, cOffset);
H
hedaoyuan 已提交
95 96 97 98

      inputGrad = inputGrad + denom.pow(-pow) * outputGrad;
      for (int s = start; s < end; s++) {
        if (c + s >= 0 && c + s < (int)channels) {
H
hedaoyuan 已提交
99 100 101 102
          size_t offset = (c + s) * height * width;
          CpuVector output = oneImage(oneOutputValue, offset);
          CpuVector outputGrad = oneImage(oneOutputGrad, offset);
          CpuVector denom = oneImage(oneDenom, offset);
H
hedaoyuan 已提交
103 104 105 106 107 108 109 110

          inputGrad += ((outputGrad * output * ratio) / denom) * inputValue;
        }
      }
    }
  }
}

H
hedaoyuan 已提交
111 112 113 114 115
/**
 * \param inputs[0] input value.
 * \param outputs[0] output value.
 * \param outputs[1] denoms.
 */
H
hedaoyuan 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
template <DeviceType Device>
class CrossMapNormalFunc : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
    size_ = config.get<size_t>("size");
    scale_ = config.get<real>("scale");
    pow_ = config.get<real>("pow");
  }

  void calc(const Arguments& inputs,
            const Arguments& outputs,
            const Arguments& inouts) override {
    CHECK_EQ(1, inputs.size());
    CHECK_EQ(2, outputs.size());
    CHECK_EQ(0, inouts.size());

132 133 134 135 136 137 138 139 140 141 142
    CHECK_EQ(inputs[0].dims_.size(), 4);
    for (size_t i = 0; i < inputs[0].dims_.size(); i++) {
      CHECK_EQ(inputs[0].dims_[i], outputs[0].dims_[i]);
      CHECK_EQ(inputs[0].dims_[i], outputs[1].dims_[i]);
    }

    size_t samples = inputs[0].dims_[0];
    size_t channels = inputs[0].dims_[1];
    size_t height = inputs[0].dims_[2];
    size_t width = inputs[0].dims_[3];

H
hedaoyuan 已提交
143 144 145 146 147 148 149 150 151 152
    CrossMapNormal<Device>(outputs[0].getData(),
                           outputs[1].getData(),
                           inputs[0].getData(),
                           samples,
                           channels,
                           height,
                           width,
                           size_,
                           scale_,
                           pow_);
H
hedaoyuan 已提交
153 154 155 156 157 158 159 160
  }

private:
  size_t size_;
  real scale_;
  real pow_;
};

H
hedaoyuan 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/**
 * \param inputs[0] input value.
 * \param inputs[1] output value.
 * \param inputs[2] output grad.
 * \param inputs[3] denoms.
 * \param outputs[0] input grad.
 */
template <DeviceType Device>
class CrossMapNormalGradFunc : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
    size_ = config.get<size_t>("size");
    scale_ = config.get<real>("scale");
    pow_ = config.get<real>("pow");
  }

  void calc(const Arguments& inputs,
            const Arguments& outputs,
            const Arguments& inouts) override {
    CHECK_EQ(4, inputs.size());
    CHECK_EQ(1, outputs.size());
    CHECK_EQ(0, inouts.size());

    CHECK_EQ(inputs[0].dims_.size(), 4);
    for (size_t i = 0; i < inputs[0].dims_.size(); i++) {
      CHECK_EQ(inputs[0].dims_[i], inputs[1].dims_[i]);
      CHECK_EQ(inputs[0].dims_[i], inputs[2].dims_[i]);
      CHECK_EQ(inputs[0].dims_[i], inputs[3].dims_[i]);
      CHECK_EQ(inputs[0].dims_[i], outputs[0].dims_[i]);
    }

    size_t samples = inputs[0].dims_[0];
    size_t channels = inputs[0].dims_[1];
    size_t height = inputs[0].dims_[2];
    size_t width = inputs[0].dims_[3];

    CrossMapNormalGrad<Device>(outputs[0].getData(),
                               inputs[0].getData(),
                               inputs[1].getData(),
                               inputs[2].getData(),
                               inputs[3].getData(),
                               samples,
                               channels,
                               height,
                               width,
                               size_,
                               scale_,
                               pow_);
  }

private:
  size_t size_;
  real scale_;
  real pow_;
};

H
hedaoyuan 已提交
217
REGISTER_TYPED_FUNC(CrossMapNormal, CPU, CrossMapNormalFunc);
H
hedaoyuan 已提交
218
REGISTER_TYPED_FUNC(CrossMapNormalGrad, CPU, CrossMapNormalGradFunc);
H
hedaoyuan 已提交
219 220
#ifndef PADDLE_ONLY_CPU
REGISTER_TYPED_FUNC(CrossMapNormal, GPU, CrossMapNormalFunc);
H
hedaoyuan 已提交
221
REGISTER_TYPED_FUNC(CrossMapNormalGrad, GPU, CrossMapNormalGradFunc);
H
hedaoyuan 已提交
222
#endif
H
hedaoyuan 已提交
223

H
hedaoyuan 已提交
224
}  // namespace paddle