cvm_op.h 3.8 KB
Newer Older
H
fix doc  
heqiaozhi 已提交
1
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
H
heqiaozhi 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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 "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;

T
tangwei12 已提交
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
template <typename T>
void CvmComputeKernel(const bool use_cvm, const int64_t item_width, const T** X,
                      T** Y) {
  const auto cvm_offset = use_cvm ? 0 : 2;

  std::memcpy(*Y, *X + cvm_offset, (item_width - cvm_offset) * sizeof(T));

  if (use_cvm) {
    (*Y)[0] = log((*Y)[0] + 1);
    (*Y)[1] = log((*Y)[1] + 1) - (*Y)[0];
  }

  (*X) += item_width;
  (*Y) += item_width - cvm_offset;
}

template <typename T>
void CvmGradComputeKernel(const bool use_cvm, const int64_t item_width,
                          const T& CVM, const T** DY, T** DX) {
  const auto cvm_offset = use_cvm ? 0 : 2;

  std::memcpy(*DX + cvm_offset, *DY, (item_width - cvm_offset) * sizeof(T));

  (*DX)[0] = (&CVM)[0];
  (*DX)[1] = (&CVM)[1];

  (*DX) += item_width;
  (*DY) += item_width - cvm_offset;
}

H
heqiaozhi 已提交
55 56 57 58
template <typename T>
class CVMOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
T
tangwei12 已提交
59
    const auto* x = context.Input<LoDTensor>("X");
H
heqiaozhi 已提交
60
    const T* x_data = x->data<T>();
T
tangwei12 已提交
61 62 63 64 65 66

    auto batch_size = x->dims()[0];
    auto item_size = x->numel() / batch_size;
    auto use_cvm = context.Attr<bool>("use_cvm");

    auto* y = context.Output<LoDTensor>("Y");
H
heqiaozhi 已提交
67 68
    T* y_data = y->mutable_data<T>(context.GetPlace());

T
tangwei12 已提交
69 70 71 72 73 74 75 76 77 78
    // for Input X do not have Lod Information.
    if (x->NumLevels() == 0) {
      for (int i = 0; i < batch_size; i++) {
        CvmComputeKernel(use_cvm, item_size, &x_data, &y_data);
      }
    } else {
      auto lod = x->lod()[0];
      for (int i = 0; i < lod.size() - 1; ++i) {
        for (int j = 0; j < lod[i + 1] - lod[i]; ++j) {
          CvmComputeKernel(use_cvm, item_size, &x_data, &y_data);
H
heqiaozhi 已提交
79 80 81 82 83 84 85 86 87 88
        }
      }
    }
  }
};

template <typename T>
class CVMGradOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
T
tangwei12 已提交
89
    auto* dx = context.Output<LoDTensor>(framework::GradVarName("X"));
H
heqiaozhi 已提交
90 91 92 93
    T* dx_data = dx->mutable_data<T>(context.GetPlace());

    const Tensor* cvm = context.Input<Tensor>("CVM");
    const T* cvm_data = cvm->data<T>();
T
tangwei12 已提交
94 95

    const auto* dOut =
H
heqiaozhi 已提交
96 97 98
        context.Input<framework::LoDTensor>(framework::GradVarName("Y"));
    const T* dout_data = dOut->data<T>();

T
tangwei12 已提交
99
    auto use_cvm = context.Attr<bool>("use_cvm");
H
heqiaozhi 已提交
100

T
tangwei12 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    auto offset = 2;
    auto batch_size = dx->dims()[0];
    auto item_size = dx->numel() / batch_size;

    // for Input X do not have Lod Information.
    if (dx->NumLevels() == 0) {
      for (int x = 0; x < batch_size; ++x) {
        CvmGradComputeKernel(use_cvm, item_size, *cvm_data, &dout_data,
                             &dx_data);
        cvm_data += offset;
      }
    } else {
      auto lod = dx->lod()[0];
      int seq_num = static_cast<int>(lod.size()) - 1;
      for (int i = 0; i < seq_num; ++i) {
        for (int j = 0; j < lod[i + 1] - lod[i]; ++j) {
          CvmGradComputeKernel(use_cvm, item_size, *cvm_data, &dout_data,
                               &dx_data);
H
heqiaozhi 已提交
119
        }
T
tangwei12 已提交
120
        cvm_data += offset;
H
heqiaozhi 已提交
121 122 123 124 125 126
      }
    }
  }
};
}  // namespace operators
}  // namespace paddle