hierarchical_sigmoid_op.h 7.1 KB
Newer Older
Y
Yancey1989 已提交
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. */

#pragma once
W
weixing02 已提交
16 17 18
#include <iostream>
#include <vector>
#include "paddle/fluid/framework/op_registry.h"
19
#include "paddle/fluid/framework/selected_rows.h"
W
weixing02 已提交
20 21 22 23
#include "paddle/fluid/operators/clip_op.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/matrix_bit_code.h"
#include "paddle/fluid/platform/transform.h"
Y
Yancey1989 已提交
24 25 26
namespace paddle {
namespace operators {

Y
Yancey1989 已提交
27 28 29
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
Y
Yancey1989 已提交
30
using platform::Transform;
Y
Yancey1989 已提交
31

Y
Yancey1989 已提交
32
template <typename DeviceContext, typename T>
Y
Yancey1989 已提交
33 34
class HierarchicalSigmoidOpKernel : public framework::OpKernel<T> {
 public:
Y
Yancey1989 已提交
35
  void Compute(const framework::ExecutionContext& ctx) const override {
Y
Yancey1989 已提交
36
    auto* in = ctx.Input<framework::Tensor>("X");
Y
Yancey1989 已提交
37
    auto* w = ctx.Input<framework::Tensor>("W");
38 39
    auto* path = ctx.Input<framework::Tensor>("PTable");
    auto* code = ctx.Input<framework::Tensor>("PCode");
W
weixing02 已提交
40
    auto* label = ctx.Input<framework::Tensor>("Label");
Y
Yancey1989 已提交
41
    auto* bias = ctx.Input<framework::Tensor>("Bias");
Y
Yancey1989 已提交
42
    auto* out = ctx.Output<framework::Tensor>("Out");
W
weixing02 已提交
43
    auto* pre_out = ctx.Output<framework::Tensor>("PreOut");
Y
Yancey1989 已提交
44
    size_t num_classes = static_cast<size_t>(ctx.Attr<int>("num_classes"));
45 46 47 48 49 50 51 52
    bool is_custom = false;
    if (path) {
      is_custom = true;
    } else {
      is_custom = false;
    }
    int64_t code_length =
        path ? path->dims()[1] : math::FindLastSet(num_classes - 1);
Y
Yancey1989 已提交
53 54
    int64_t batch_size = in->dims()[0];
    framework::Tensor sum;
W
weixing02 已提交
55
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
G
guosheng 已提交
56
    auto* pre_out_data = pre_out->mutable_data<T>(
Y
Yancey1989 已提交
57
        framework::make_ddim({batch_size, code_length}), ctx.GetPlace());
W
weixing02 已提交
58
    auto pre_out_mat = EigenMatrix<T>::From(*pre_out);
G
guosheng 已提交
59 60
    // Not all class(leaf) nodes' path lengths equal code_length, thus init as
    // 0s can avoid out of path's loss.
61
    math::SetConstant<DeviceContext, T> zero;
W
weixing02 已提交
62
    zero(dev_ctx, pre_out, static_cast<T>(0.0));
Y
Yancey1989 已提交
63 64
    auto& place = *ctx.template device_context<DeviceContext>().eigen_device();
    math::RowwiseSum<DeviceContext, T> row_sum;
65 66 67 68 69 70 71 72 73

    std::unique_ptr<math::MatrixBitCodeFunctor<T>> bit_code;
    if (!is_custom) {
      bit_code.reset(new math::MatrixBitCodeFunctor<T>(num_classes,
                                                       label->data<int64_t>()));
    } else {
      bit_code.reset(new math::MatrixBitCodeFunctor<T>(path, code,
                                                       label->data<int64_t>()));
    }
Y
Yancey1989 已提交
74

Y
Yancey1989 已提交
75 76
    std::vector<int64_t> sum_dims({batch_size, 1UL});
    sum.mutable_data<T>(framework::make_ddim(sum_dims), ctx.GetPlace());
Y
Yancey1989 已提交
77
    auto sum_mat = EigenMatrix<T>::From(sum);
Y
Yancey1989 已提交
78
    out->mutable_data<T>(ctx.GetPlace());
Y
Yancey1989 已提交
79
    auto out_mat = framework::EigenVector<T>::Flatten(*out);
Y
Yancey1989 已提交
80
    if (bias) {
81
      bit_code->Add(pre_out, *bias);
Y
Yancey1989 已提交
82
    }
83
    bit_code->Mul(pre_out, *w, *in);
G
guosheng 已提交
84
    // clip to [-40, 40]
Y
Yancey1989 已提交
85 86
    Transform<DeviceContext> trans;
    trans(ctx.template device_context<DeviceContext>(), pre_out_data,
W
weixing02 已提交
87
          pre_out_data + pre_out->numel(), pre_out_data,
Y
Yancey1989 已提交
88
          ClipFunctor<T>(static_cast<T>(-40.0), static_cast<T>(40.0)));
89
    bit_code->Sum(*pre_out, out, static_cast<T>(-1));
G
guosheng 已提交
90
    // use softrelu to calculate cross entropy
Y
Yancey1989 已提交
91
    pre_out_mat.device(place) = (static_cast<T>(1.0) + pre_out_mat.exp()).log();
W
weixing02 已提交
92
    row_sum(dev_ctx, *pre_out, &sum);
93 94 95 96
    // TODO(guosheng): Subtract the out of path's loss, since not all
    // class(leaf) nodes' path lengths equal code_length. But it won't break the
    // gradient check since both have the out of path's loss and will cancel out
    // each other.
Y
Yancey1989 已提交
97
    out_mat.device(place) = sum_mat + out_mat;
Y
Yancey1989 已提交
98
  }
Y
Yancey1989 已提交
99 100
};

Y
Yancey1989 已提交
101
template <typename DeviceContext, typename T>
Y
Yancey1989 已提交
102 103
class HierarchicalSigmoidGradOpKernel : public framework::OpKernel<T> {
 public:
Y
Yancey1989 已提交
104
  void Compute(const framework::ExecutionContext& ctx) const override {
Y
Yancey1989 已提交
105
    auto* in = ctx.Input<framework::Tensor>("X");
W
weixing02 已提交
106
    auto* w = ctx.Input<framework::Tensor>("W");
107 108
    auto* path = ctx.Input<framework::Tensor>("PTable");
    auto* code = ctx.Input<framework::Tensor>("PCode");
Y
Yancey1989 已提交
109
    auto* in_grad = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
W
weixing02 已提交
110 111 112
    auto* w_grad = ctx.Output<framework::Tensor>(framework::GradVarName("W"));
    auto* bias_grad =
        ctx.Output<framework::Tensor>(framework::GradVarName("Bias"));
W
weixing02 已提交
113
    auto* label = ctx.Input<framework::Tensor>("Label");
W
weixing02 已提交
114 115 116
    auto* pre_out = ctx.Input<framework::Tensor>("PreOut");
    auto* out_grad =
        ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
117 118 119 120 121 122 123 124 125
    framework::Tensor pre_out_grad;

    pre_out_grad.mutable_data<T>(pre_out->dims(), ctx.GetPlace());
    in_grad->mutable_data<T>(ctx.GetPlace());
    w_grad->mutable_data<T>(ctx.GetPlace());
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
    math::SetConstant<DeviceContext, T> zero;
    zero(dev_ctx, in_grad, static_cast<T>(0.0));
    zero(dev_ctx, w_grad, static_cast<T>(0.0));
W
weixing02 已提交
126

Y
Yancey1989 已提交
127
    size_t num_classes = static_cast<size_t>(ctx.Attr<int>("num_classes"));
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    bool is_custom = false;
    if (path) {
      is_custom = true;
    } else {
      is_custom = false;
    }

    std::unique_ptr<math::MatrixBitCodeFunctor<T>> bit_code;
    if (!is_custom) {
      bit_code.reset(new math::MatrixBitCodeFunctor<T>(num_classes,
                                                       label->data<int64_t>()));
    } else {
      bit_code.reset(new math::MatrixBitCodeFunctor<T>(path, code,
                                                       label->data<int64_t>()));
    }
144

Y
Yancey1989 已提交
145
    auto& place = *ctx.template device_context<DeviceContext>().eigen_device();
W
weixing02 已提交
146 147
    auto pre_out_mat = EigenMatrix<T>::From(*pre_out);
    auto pre_out_grad_mat = EigenMatrix<T>::From(pre_out_grad);
W
weixing02 已提交
148
    auto out_grad_mat = EigenMatrix<T>::From(*out_grad);
J
JiabinYang 已提交
149

150 151 152 153 154
    Eigen::array<int, 2> bcast({{1, static_cast<int>(pre_out_grad.dims()[1])}});

    // softrelu derivative
    pre_out_grad_mat.device(place) =
        static_cast<T>(1.0) - static_cast<T>(1.0) / pre_out_mat.exp();
155
    bit_code->Sub(&pre_out_grad);  // the gradient of clip(w * x + b)
W
weixing02 已提交
156
    pre_out_grad_mat.device(place) =
157
        pre_out_grad_mat * out_grad_mat.broadcast(bcast);
G
guosheng 已提交
158 159
    // TODO(guosheng): multiply pre_out_grad with subgradient of clipping to
    // be consistent with the clipping in forward.
W
weixing02 已提交
160 161
    if (bias_grad) {
      bias_grad->mutable_data<T>(ctx.GetPlace());
162
      zero(dev_ctx, bias_grad, static_cast<T>(0.0));
163
      bit_code->AddGrad(pre_out_grad, bias_grad);
Y
Yancey1989 已提交
164
    }
165 166
    bit_code->MulGradWeight(pre_out_grad, w_grad, *in);
    bit_code->MulGradError(pre_out_grad, *w, in_grad);
Y
Yancey1989 已提交
167
  }
Y
Yancey1989 已提交
168 169 170 171
};

}  // namespace operators
}  // namespace paddle