gru_cpu_kernel.h 14.4 KB
Newer Older
G
guosheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/* 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. */

#pragma once
#include <type_traits>
#include "paddle/operators/math/detail/hl_activation_functions.h"
#include "paddle/operators/math/gru_compute.h"

namespace paddle {
namespace operators {
namespace math {
namespace detail {

#ifndef __NVCC__

template <class OpResetOutput, typename T>
void hl_naive_gru_forward_reset_output(OpResetOutput opResetOutput,
                                       T *gateValue, T *resetOutputValue,
                                       T *prevOutputValue, int frameSize,
                                       activation_mode_t active_gate) {
  T rValueUpdateGate;
  T rValueResetGate;
  T rValueResetOutput;
  T rPrevOut = 0;
  T *updateGate = gateValue;
  T *resetGate = gateValue + frameSize;

  for (int i = 0; i < frameSize; i++) {
    rValueUpdateGate = updateGate[i];
    rValueResetGate = resetGate[i];
    if (prevOutputValue) {
      rPrevOut = prevOutputValue[i];
    }

    hppl::cpu::ForwardAct<T> act;
    opResetOutput(rValueUpdateGate, rValueResetGate, rPrevOut,
                  rValueResetOutput, act(active_gate));

    updateGate[i] = rValueUpdateGate;
    resetGate[i] = rValueResetGate;
    resetOutputValue[i] = rValueResetOutput;
  }
}

template <class OpFinalOutput, typename T>
void hl_naive_gru_forward_final_output(OpFinalOutput opFinalOutput,
                                       T *gateValue, T *prevOutputValue,
                                       T *outputValue, int frameSize,
                                       activation_mode_t active_node) {
  T rValueUpdateGate;
  T rValueFrameState;
  T rPrevOut = 0;
  T rOutput;
  T *updateGate = gateValue;
  T *frameState = gateValue + frameSize * 2;

  for (int i = 0; i < frameSize; i++) {
    rValueUpdateGate = updateGate[i];
    rValueFrameState = frameState[i];
    if (prevOutputValue) {
      rPrevOut = prevOutputValue[i];
    }

    hppl::cpu::ForwardAct<T> act;
    opFinalOutput(rValueUpdateGate, rValueFrameState, rPrevOut, rOutput,
                  act(active_node));

    frameState[i] = rValueFrameState;
    outputValue[i] = rOutput;
  }
}

template <class OpResetOutput, typename T>
void hl_avx_gru_forward_reset_output(OpResetOutput opResetOutput, T *gateValue,
                                     T *resetOutputValue, T *prevOutputValue,
                                     int frameSize,
                                     activation_mode_t active_gate) {
#ifdef __AVX__
  __m256 rValueUpdateGate;
  __m256 rValueResetGate;
  __m256 rValueResetOutput;
  __m256 rPrevOut = _mm256_set1_ps(0.0f);
  __m256 *updateGate = (__m256 *)gateValue;
  __m256 *resetGate = (__m256 *)(gateValue + frameSize);

  for (int i = 0; i < frameSize / 8; i++) {
    rValueUpdateGate = updateGate[i];
    rValueResetGate = resetGate[i];
    if (prevOutputValue) {
      rPrevOut = ((__m256 *)prevOutputValue)[i];
    }

    opResetOutput(rValueUpdateGate, rValueResetGate, rPrevOut,
                  rValueResetOutput, hppl::avx::forward[active_gate]);

    updateGate[i] = rValueUpdateGate;
    resetGate[i] = rValueResetGate;
    ((__m256 *)resetOutputValue)[i] = rValueResetOutput;
  }
#endif
}

template <class OpFinalOutput, typename T>
void hl_avx_gru_forward_final_output(OpFinalOutput opFinalOutput, T *gateValue,
                                     T *prevOutputValue, T *outputValue,
                                     int frameSize,
                                     activation_mode_t active_node) {
#ifdef __AVX__
  __m256 rValueUpdateGate;
  __m256 rValueFrameState;
  __m256 rPrevOut = _mm256_set1_ps(0.0f);
  __m256 rOutput;
  __m256 *updateGate = (__m256 *)gateValue;
  __m256 *frameState = (__m256 *)(gateValue + frameSize * 2);

  for (int i = 0; i < frameSize / 8; i++) {
    rValueUpdateGate = updateGate[i];
    rValueFrameState = frameState[i];
    if (prevOutputValue) {
      rPrevOut = ((__m256 *)prevOutputValue)[i];
    }

    opFinalOutput(rValueUpdateGate, rValueFrameState, rPrevOut, rOutput,
                  hppl::avx::forward[active_node]);

    frameState[i] = rValueFrameState;
    ((__m256 *)outputValue)[i] = rOutput;
  }
#endif
}

template <class OpResetOutput, typename T>
inline void forward_reset_output(OpResetOutput opResetOutput,
                                 hl_gru_value<T> value, int frameSize,
                                 int batchSize, activation_mode_t active_gate) {
  for (int b = 0; b < batchSize; b++) {
    if (OpResetOutput::avx && !(frameSize & (8 - 1)) && (sizeof(T) == 4)) {
      hl_avx_gru_forward_reset_output(
          opResetOutput, value.gateValue, value.resetOutputValue,
          value.prevOutValue, frameSize, active_gate);
    } else {
      hl_naive_gru_forward_reset_output(
          opResetOutput, value.gateValue, value.resetOutputValue,
          value.prevOutValue, frameSize, active_gate);
    }

    value.gateValue += frameSize * 3;
    value.resetOutputValue += frameSize;
    if (value.prevOutValue) {
      value.prevOutValue += frameSize;
    }
  }
}

template <class OpFinalOutput, typename T>
inline void forward_final_output(OpFinalOutput opFinalOutput,
                                 hl_gru_value<T> value, int frameSize,
                                 int batchSize, activation_mode_t active_node) {
  for (int b = 0; b < batchSize; b++) {
    if (OpFinalOutput::avx && !(frameSize & (8 - 1)) && (sizeof(T) == 4)) {
      hl_avx_gru_forward_final_output(opFinalOutput, value.gateValue,
                                      value.prevOutValue, value.outputValue,
                                      frameSize, active_node);
    } else {
      hl_naive_gru_forward_final_output(opFinalOutput, value.gateValue,
                                        value.prevOutValue, value.outputValue,
                                        frameSize, active_node);
    }

    value.gateValue += frameSize * 3;
    value.outputValue += frameSize;
    if (value.prevOutValue) {
      value.prevOutValue += frameSize;
    }
  }
}

template <class OpStateGrad, typename T>
void hl_naive_gru_backward_state_grad(OpStateGrad opStateGrad, T *gateValue,
                                      T *gateGrad, T *prevOutValue,
                                      T *prevOutGrad, T *outputGrad,
                                      int frameSize,
                                      activation_mode_t active_node) {
  T rUpdateGateValue;
  T rUpdateGateGrad;
  T rFrameStateValue;
  T rFrameStateGrad;
  T rOutGrad;
  T rPrevOutValue = 0;
  T rPrevOutGrad = 0;
  T *updateGateValue = gateValue;
  T *updateGateGrad = gateGrad;
  T *frameStateValue = gateValue + frameSize * 2;
  T *frameStateGrad = gateGrad + frameSize * 2;

  for (int i = 0; i < frameSize; i++) {
    rUpdateGateValue = updateGateValue[i];
    rFrameStateValue = frameStateValue[i];
    rOutGrad = outputGrad[i];
    if (prevOutValue) {
      rPrevOutValue = prevOutValue[i];
    }
    if (prevOutGrad) {
      rPrevOutGrad = prevOutGrad[i];
    }

    hppl::cpu::BackwardAct<T> act;
    opStateGrad(rUpdateGateValue, rUpdateGateGrad, rFrameStateValue,
                rFrameStateGrad, rPrevOutValue, rPrevOutGrad, rOutGrad,
                act(active_node));

    updateGateGrad[i] = rUpdateGateGrad;
    frameStateGrad[i] = rFrameStateGrad;
    if (prevOutGrad) {
      prevOutGrad[i] = rPrevOutGrad;
    }
  }
}

template <class OpResetGrad, typename T>
void hl_naive_gru_backward_reset_grad(OpResetGrad opResetGrad, T *gateValue,
                                      T *gateGrad, T *prevOutValue,
                                      T *prevOutGrad, T *resetOutputGrad,
                                      int frameSize,
                                      activation_mode_t active_gate) {
  T rUpdateGateValue;
  T rUpdateGateGrad;
  T rResetGateValue;
  T rResetGateGrad;
  T rResetOutputGrad = 0;
  T rPrevOutValue = 0;
  T rPrevOutGrad = 0;
  T *updateGateValue = gateValue;
  T *updateGateGrad = gateGrad;
  T *resetGateValue = gateValue + frameSize;
  T *resetGateGrad = gateGrad + frameSize;

  for (int i = 0; i < frameSize; i++) {
    rUpdateGateValue = updateGateValue[i];
    rUpdateGateGrad = updateGateGrad[i];
    rResetGateValue = resetGateValue[i];

    if (prevOutValue && prevOutGrad) {
      rResetOutputGrad = resetOutputGrad[i];
    }
    if (prevOutValue) {
      rPrevOutValue = prevOutValue[i];
    }
    if (prevOutGrad) {
      rPrevOutGrad = prevOutGrad[i];
    }

    hppl::cpu::BackwardAct<T> act;
    opResetGrad(rUpdateGateValue, rUpdateGateGrad, rResetGateValue,
                rResetGateGrad, rPrevOutValue, rPrevOutGrad, rResetOutputGrad,
                act(active_gate));

    updateGateGrad[i] = rUpdateGateGrad;
    resetGateGrad[i] = rResetGateGrad;
    if (prevOutGrad) {
      prevOutGrad[i] = rPrevOutGrad;
    }
  }
}

template <class OpStateGrad, typename T>
void hl_avx_gru_backward_state_grad(OpStateGrad opStateGrad, T *gateValue,
                                    T *gateGrad, T *prevOutValue,
                                    T *prevOutGrad, T *outputGrad,
                                    int frameSize,
                                    activation_mode_t active_node) {
#ifdef __AVX__
  __m256 rUpdateGateValue;
  __m256 rUpdateGateGrad;
  __m256 rFrameStateValue;
  __m256 rFrameStateGrad;
  __m256 rOutGrad;
  __m256 rPrevOutValue = _mm256_set1_ps(0.0f);
  __m256 rPrevOutGrad = _mm256_set1_ps(0.0f);
  __m256 *updateGateValue = (__m256 *)gateValue;
  __m256 *updateGateGrad = (__m256 *)gateGrad;
  __m256 *frameStateValue = (__m256 *)(gateValue + frameSize * 2);
  __m256 *frameStateGrad = (__m256 *)(gateGrad + frameSize * 2);

  for (int i = 0; i < frameSize / 8; i++) {
    rUpdateGateValue = updateGateValue[i];
    rFrameStateValue = frameStateValue[i];
    rOutGrad = ((__m256 *)outputGrad)[i];
    if (prevOutValue) {
      rPrevOutValue = ((__m256 *)prevOutValue)[i];
    }
    if (prevOutGrad) {
      rPrevOutGrad = ((__m256 *)prevOutGrad)[i];
    }

    opStateGrad(rUpdateGateValue, rUpdateGateGrad, rFrameStateValue,
                rFrameStateGrad, rPrevOutValue, rPrevOutGrad, rOutGrad,
                hppl::avx::backward[active_node]);

    updateGateGrad[i] = rUpdateGateGrad;
    frameStateGrad[i] = rFrameStateGrad;
    if (prevOutGrad) {
      ((__m256 *)prevOutGrad)[i] = rPrevOutGrad;
    }
  }
#endif
}

template <class OpResetGrad, typename T>
void hl_avx_gru_backward_reset_grad(OpResetGrad opResetGrad, T *gateValue,
                                    T *gateGrad, T *prevOutValue,
                                    T *prevOutGrad, T *resetOutputGrad,
                                    int frameSize,
                                    activation_mode_t active_gate) {
#ifdef __AVX__
  __m256 rUpdateGateValue;
  __m256 rUpdateGateGrad;
  __m256 rResetGateValue;
  __m256 rResetGateGrad;
  __m256 rResetOutputGrad = _mm256_set1_ps(0.0f);
  __m256 rPrevOutValue = _mm256_set1_ps(0.0f);
  __m256 rPrevOutGrad = _mm256_set1_ps(0.0f);
  __m256 *updateGateValue = (__m256 *)gateValue;
  __m256 *updateGateGrad = (__m256 *)gateGrad;
  __m256 *resetGateValue = (__m256 *)(gateValue + frameSize);
  __m256 *resetGateGrad = (__m256 *)(gateGrad + frameSize);

  for (int i = 0; i < frameSize / 8; i++) {
    rUpdateGateValue = updateGateValue[i];
    rUpdateGateGrad = updateGateGrad[i];
    rResetGateValue = resetGateValue[i];

    if (prevOutValue && prevOutGrad) {
      rResetOutputGrad = ((__m256 *)resetOutputGrad)[i];
    }
    if (prevOutValue) {
      rPrevOutValue = ((__m256 *)prevOutValue)[i];
    }
    if (prevOutGrad) {
      rPrevOutGrad = ((__m256 *)prevOutGrad)[i];
    }

    opResetGrad(rUpdateGateValue, rUpdateGateGrad, rResetGateValue,
                rResetGateGrad, rPrevOutValue, rPrevOutGrad, rResetOutputGrad,
                hppl::avx::backward[active_gate]);

    updateGateGrad[i] = rUpdateGateGrad;
    resetGateGrad[i] = rResetGateGrad;
    if (prevOutGrad) {
      ((__m256 *)prevOutGrad)[i] = rPrevOutGrad;
    }
  }
#endif
}

template <class OpStateGrad, typename T>
inline void backward_state_grad(OpStateGrad opStateGrad, hl_gru_value<T> value,
                                hl_gru_grad<T> grad, int frameSize,
                                int batchSize, activation_mode_t active_node) {
  for (int b = 0; b < batchSize; b++) {
    if (OpStateGrad::avx && !(frameSize & (8 - 1)) && (sizeof(T) == 4)) {
      hl_avx_gru_backward_state_grad(
          opStateGrad, value.gateValue, grad.gateGrad, value.prevOutValue,
          grad.prevOutGrad, grad.outputGrad, frameSize, active_node);
    } else {
      hl_naive_gru_backward_state_grad(
          opStateGrad, value.gateValue, grad.gateGrad, value.prevOutValue,
          grad.prevOutGrad, grad.outputGrad, frameSize, active_node);
    }

    value.gateValue += frameSize * 3;
    if (value.prevOutValue) {
      value.prevOutValue += frameSize;
    }

    grad.gateGrad += frameSize * 3;
    grad.outputGrad += frameSize;
    if (grad.prevOutGrad) {
      grad.prevOutGrad += frameSize;
    }
  }
}

template <class OpResetGrad, typename T>
inline void backward_reset_grad(OpResetGrad opResetGrad, hl_gru_value<T> value,
                                hl_gru_grad<T> grad, int frameSize,
                                int batchSize, activation_mode_t active_gate) {
  for (int b = 0; b < batchSize; b++) {
    if (OpResetGrad::avx && !(frameSize & (8 - 1)) && (sizeof(T) == 4)) {
      hl_avx_gru_backward_reset_grad(
          opResetGrad, value.gateValue, grad.gateGrad, value.prevOutValue,
          grad.prevOutGrad, grad.resetOutputGrad, frameSize, active_gate);
    } else {
      hl_naive_gru_backward_reset_grad(
          opResetGrad, value.gateValue, grad.gateGrad, value.prevOutValue,
          grad.prevOutGrad, grad.resetOutputGrad, frameSize, active_gate);
    }

    value.gateValue += frameSize * 3;
    if (value.prevOutValue) {
      value.prevOutValue += frameSize;
    }

    grad.gateGrad += frameSize * 3;
    grad.resetOutputGrad += frameSize;
    if (grad.prevOutGrad) {
      grad.prevOutGrad += frameSize;
    }
  }
}

#endif

}  // namespace detail
}  // namespace math
}  // namespace operators
}  // namespace paddle