context_project.h 12.0 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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 "paddle/framework/lod_tensor.h"
#include "paddle/operators/math/im2col.h"
19
#include "paddle/operators/math/math_function.h"
C
chengduoZH 已提交
20 21 22 23 24

namespace paddle {
namespace operators {
namespace math {

C
chengduoZH 已提交
25 26 27
using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;

C
chengduoZH 已提交
28
/*
C
chengduoZH 已提交
29
 * \brief Context projection concatenates features in adjacent time-steps in
C
chengduoZH 已提交
30 31 32
 * a sequence. The i-th row of the output is the concatenation of
 * context_length rows of the input. The context_length rows are the
 * consecutive rows from the i+shift_start row.
C
sss  
chengduoZH 已提交
33
 * ContextProjectGradFunctor is the inverse process of ContextProjectFunctor.
C
chengduoZH 已提交
34
 *
C
chengduoZH 已提交
35
 * \param in            Input data.
C
chengduoZH 已提交
36 37
 * \param Shape         The shape of Input data:
 *                        [mini-batch, input_hidden_size].
C
chengduoZH 已提交
38
 *
C
chengduoZH 已提交
39
 * \param padding_data  Padding data.
C
chengduoZH 已提交
40 41
 * \param Shape         The shape of Padding data:
 *                        [up_pad + down_pad, input_hidden_size].
C
chengduoZH 已提交
42
 *
C
chengduoZH 已提交
43
 * \param col           Col data.
C
chengduoZH 已提交
44 45
 * \param Shape         The shape of Col data:
 *                        [mini-batch, context_length * input_hidden_size].
C
chengduoZH 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 *
 * For a mini-batch of 2 variable lengths sentences, containing 3, and 1
 * time-steps:
 *
 * Assumed input (X) is a [4, M, N] float LoDTensor, and X->lod()[0] = [0, 3,
 * 4].
 * Besides, for the sake of simplicity, we assume M=1 and N=2.
 *
 * X = [[a1, a2;
 *       b1, b2;
 *       c1, c2]
 *      [d1, d2]]
 *
 * This is to say that input (X) has 4 words and the dimension of each word
 * representation is 2.
 *
 * - Case1:
C
chengduoZH 已提交
63 64 65
 *   If context_start is -1 and padding_trainable is false, we use zero to pad
 *   instead of learned weight to pad,
 *   and the context_length is 3, the output (Out) is:
C
chengduoZH 已提交
66
 *
C
chengduoZH 已提交
67 68 69 70
 *   Out =[[0,  0,  a1, a2, b1, b2;
 *          a1, a2, b1, b2, c1, c2;
 *          b1, b2, c1, c2, 0,  0 ]
 *          [0,  0, d1, d2, 0,  0 ]]
C
chengduoZH 已提交
71 72
 *
 * - Case2:
C
chengduoZH 已提交
73 74 75
 *   If context_start is -1 and padding_trainable is true, we use learned weight
 *   to pad,
 *   and the context_length is 3, the output (Out) is:
C
chengduoZH 已提交
76
 *
C
chengduoZH 已提交
77 78 79 80
 *   Out = [[w1, w2, a1, a2, b1, b2;
 *           a1, a2, b1, b2, c1, c2;
 *           b1, b2, c1, c2, w3, w4]
 *          [w1, w2, d1, d2, w3, w4]]
C
chengduoZH 已提交
81 82 83 84
 *
 */

template <typename Place, typename T>
C
chengduoZH 已提交
85
class ContextProjectFunctor {
C
chengduoZH 已提交
86
 public:
C
chengduoZH 已提交
87 88
  void operator()(const platform::DeviceContext& context, const LoDTensor& in,
                  const Tensor& padding_data, Tensor& col,
C
sss  
chengduoZH 已提交
89 90
                  bool padding_trainable, int context_start, int context_length,
                  int context_stride, int up_pad, int down_pad) {
C
chengduoZH 已提交
91
    auto lod_level_0 = in.lod()[0];
C
chengduoZH 已提交
92

C
chengduoZH 已提交
93
    math::Im2ColFunctor<math::ColFormat::kOCF, Place, float> im2col_ocf;
94 95 96
    if (platform::is_gpu_place(context.GetPlace())) {
      LOG(INFO) << "========= gpu ==========";
    }
C
sss  
chengduoZH 已提交
97 98 99 100 101 102 103 104 105 106 107

    int input_row_begin, input_row_end;
    int sequence_height, sequence_width;
    sequence_width = in.dims()[1];

    for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) {
      input_row_begin = (context_start > 0)
                            ? static_cast<int>(lod_level_0[i]) + context_start
                            : static_cast<int>(lod_level_0[i]);
      input_row_end = static_cast<int>(lod_level_0[i + 1]);

C
chengduoZH 已提交
108 109
      Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
                               static_cast<int>(lod_level_0[i + 1]));
C
sss  
chengduoZH 已提交
110 111 112 113

      sequence_height = static_cast<int>(out_t.dims()[0]);

      if (input_row_begin < input_row_end) {
C
chengduoZH 已提交
114
        Tensor in_t = in.Slice(input_row_begin, input_row_end);
C
sss  
chengduoZH 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

        std::vector<int64_t> output_shape(
            {sequence_height, 1, 1, context_length,
             sequence_width});  // output_height, output_width,
        // input_channels, filter_height, filter_width
        out_t.Resize(framework::make_ddim(output_shape));

        std::vector<int64_t> input_shape(
            {1, input_row_end - input_row_begin,
             sequence_width});  // input_channels, input_height, input_width
        in_t.Resize(framework::make_ddim(input_shape));

        im2col_ocf(context, in_t, out_t,
                   /*stride_height*/ context_stride, /*stride_width*/ 1, up_pad,
                   down_pad, 0, 0);
        out_t.Resize({sequence_height, context_length * sequence_width});
      }
    }
    if (padding_trainable) {
      for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) {
C
chengduoZH 已提交
135 136
        Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
                                 static_cast<int>(lod_level_0[i + 1]));
C
sss  
chengduoZH 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149

        sequence_height = static_cast<int>(out_t.dims()[0]);

        // add up trainable data
        out_t.Resize({sequence_height * context_length, sequence_width});

        if (up_pad > 0) {  // add up pad
          int padding_rows = std::min(
              up_pad, static_cast<int>(lod_level_0[i + 1] - lod_level_0[i]));

          for (int k = 0; k < padding_rows; ++k) {
            int padding_size =
                k + context_length < up_pad ? context_length : up_pad - k;
C
chengduoZH 已提交
150 151 152
            Tensor out_t_sub = out_t.Slice(k * context_length,
                                           k * context_length + padding_size);
            Tensor w_sub = padding_data.Slice(k, k + padding_size);
153
            out_t_sub.CopyFrom(w_sub, context.GetPlace(), context);
C
sss  
chengduoZH 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
          }
        }
        if (down_pad > 0) {  // add down pad
          int down_pad_begin_row =
              std::max(0,
                       (sequence_height - context_start - context_length) + 1) +
              1;
          int padding_begin = std::max(0, context_start - sequence_height);
          int padding_size =
              sequence_height - context_start >= context_length
                  ? 1
                  : context_length - (sequence_height - context_start);
          if (context_start >= sequence_height) padding_size = context_length;
          int padding_idx = padding_begin;
          for (int t = 0; t + down_pad_begin_row <= sequence_height;
               ++t, ++padding_size) {
            if (context_start >= sequence_height) padding_size = context_length;
            if (padding_size > context_length) {
              padding_size = context_length;
              padding_idx++;
            }
            if (padding_begin > 0 || sequence_height == context_start)
              padding_idx = padding_begin + t;
C
chengduoZH 已提交
177 178

            Tensor out_t_sub = out_t.Slice(
C
sss  
chengduoZH 已提交
179 180
                (down_pad_begin_row + t) * context_length - padding_size,
                (down_pad_begin_row + t) * context_length);
C
chengduoZH 已提交
181
            Tensor w_sub = padding_data.Slice(
C
sss  
chengduoZH 已提交
182
                up_pad + padding_idx, up_pad + padding_idx + padding_size);
183
            out_t_sub.CopyFrom(w_sub, context.GetPlace(), context);
C
sss  
chengduoZH 已提交
184 185 186 187 188 189 190 191 192 193 194
          }
        }
        out_t.Resize({sequence_height, context_length * sequence_width});
      }
    }
  }
};

template <typename Place, typename T>
class ContextProjectGradFunctor {
 public:
C
chengduoZH 已提交
195 196
  void operator()(const platform::DeviceContext& context, LoDTensor& in,
                  Tensor& padding_data, Tensor& col, bool padding_trainable,
C
sss  
chengduoZH 已提交
197 198 199 200
                  int context_start, int context_length, int context_stride,
                  int up_pad, int down_pad, bool input_grad, bool pad_grad) {
    auto lod_level_0 = in.lod()[0];

C
chengduoZH 已提交
201
    math::Col2ImFunctor<math::ColFormat::kOCF, Place, float> col2im_ocf;
C
chengduoZH 已提交
202 203 204

    int input_row_begin, input_row_end;
    int sequence_height, sequence_width;
C
chengduoZH 已提交
205 206
    sequence_width = in.dims()[1];

C
sss  
chengduoZH 已提交
207
    if (input_grad) {
C
chengduoZH 已提交
208 209 210 211 212 213
      for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) {
        input_row_begin = (context_start > 0)
                              ? static_cast<int>(lod_level_0[i]) + context_start
                              : static_cast<int>(lod_level_0[i]);
        input_row_end = static_cast<int>(lod_level_0[i + 1]);

C
chengduoZH 已提交
214 215
        Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
                                 static_cast<int>(lod_level_0[i + 1]));
C
chengduoZH 已提交
216 217 218 219

        sequence_height = static_cast<int>(out_t.dims()[0]);

        if (input_row_begin < input_row_end) {
C
chengduoZH 已提交
220
          Tensor in_t = in.Slice(input_row_begin, input_row_end);
C
chengduoZH 已提交
221 222 223 224 225 226 227 228 229 230 231 232

          std::vector<int64_t> output_shape(
              {sequence_height, 1, 1, context_length,
               sequence_width});  // output_height, output_width,
          // input_channels, filter_height, filter_width
          out_t.Resize(framework::make_ddim(output_shape));

          std::vector<int64_t> input_shape(
              {1, input_row_end - input_row_begin,
               sequence_width});  // input_channels, input_height, input_width
          in_t.Resize(framework::make_ddim(input_shape));

C
sss  
chengduoZH 已提交
233 234 235
          col2im_ocf(context, in_t, out_t,
                     /*stride_height*/ context_stride, /*stride_width*/ 1,
                     up_pad, down_pad, 0, 0);
C
chengduoZH 已提交
236
          out_t.Resize({sequence_height, context_length * sequence_width});
C
chengduoZH 已提交
237
        }
C
chengduoZH 已提交
238
      }
C
chengduoZH 已提交
239
    }
C
sss  
chengduoZH 已提交
240
    if (pad_grad) {
C
chengduoZH 已提交
241
      if (padding_trainable) {
C
chengduoZH 已提交
242
        for (int i = 0; i < static_cast<int>(lod_level_0.size()) - 1; ++i) {
C
chengduoZH 已提交
243 244
          Tensor out_t = col.Slice(static_cast<int>(lod_level_0[i]),
                                   static_cast<int>(lod_level_0[i + 1]));
C
chengduoZH 已提交
245 246

          sequence_height = static_cast<int>(out_t.dims()[0]);
C
chengduoZH 已提交
247
          out_t.Resize({sequence_height * context_length, sequence_width});
C
chengduoZH 已提交
248

C
sss  
chengduoZH 已提交
249
          if (up_pad > 0) {
C
chengduoZH 已提交
250 251 252 253 254 255
            int padding_rows = std::min(
                up_pad, static_cast<int>(lod_level_0[i + 1] - lod_level_0[i]));

            for (int k = 0; k < padding_rows; ++k) {
              int padding_size =
                  k + context_length < up_pad ? context_length : up_pad - k;
C
chengduoZH 已提交
256 257 258
              Tensor out_t_sub = out_t.Slice(k * context_length,
                                             k * context_length + padding_size);
              Tensor w_sub = padding_data.Slice(k, k + padding_size);
259 260
              axpy<Place, T>(context, w_sub.numel(), static_cast<T>(1),
                             out_t_sub.data<T>(), w_sub.data<T>());
C
chengduoZH 已提交
261
            }
C
chengduoZH 已提交
262
          }
C
sss  
chengduoZH 已提交
263
          if (down_pad > 0) {
C
chengduoZH 已提交
264 265 266 267 268 269 270 271 272
            int down_pad_begin_row =
                std::max(
                    0, (sequence_height - context_start - context_length) + 1) +
                1;
            int padding_begin = std::max(0, context_start - sequence_height);
            int padding_size =
                sequence_height - context_start >= context_length
                    ? 1
                    : context_length - (sequence_height - context_start);
C
chengduoZH 已提交
273
            if (context_start >= sequence_height) padding_size = context_length;
C
chengduoZH 已提交
274 275 276 277 278 279 280 281 282 283 284
            int padding_idx = padding_begin;
            for (int t = 0; t + down_pad_begin_row <= sequence_height;
                 ++t, ++padding_size) {
              if (context_start >= sequence_height)
                padding_size = context_length;
              if (padding_size > context_length) {
                padding_size = context_length;
                padding_idx++;
              }
              if (padding_begin > 0 || sequence_height == context_start)
                padding_idx = padding_begin + t;
C
chengduoZH 已提交
285 286

              Tensor out_t_sub = out_t.Slice(
C
chengduoZH 已提交
287 288
                  (down_pad_begin_row + t) * context_length - padding_size,
                  (down_pad_begin_row + t) * context_length);
C
chengduoZH 已提交
289
              Tensor w_sub = padding_data.Slice(
C
chengduoZH 已提交
290
                  up_pad + padding_idx, up_pad + padding_idx + padding_size);
291 292
              axpy<Place, T>(context, w_sub.numel(), static_cast<T>(1),
                             out_t_sub.data<T>(), w_sub.data<T>());
C
chengduoZH 已提交
293 294
            }
          }
C
chengduoZH 已提交
295
          out_t.Resize({sequence_height, context_length * sequence_width});
C
chengduoZH 已提交
296 297 298 299 300 301 302 303 304
        }
      }
    }
  }
};

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