split_selected_rows_op.h 3.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yancey 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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 <vector>
Y
Yi Wang 已提交
18 19
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
Y
Yancey 已提交
20 21 22 23

namespace paddle {
namespace operators {

S
seiriosPlus 已提交
24
static int FindOutIdx(int row, const std::vector<int64_t>& abs_sections) {
25 26 27
  for (size_t i = 1; i < abs_sections.size(); ++i) {
    if (row < abs_sections[i]) {
      return i - 1;
28 29
    }
  }
30 31 32
  return abs_sections.size() - 1;
}

S
seiriosPlus 已提交
33 34 35
static std::vector<int64_t> ToAbsoluteSection(
    const std::vector<int64_t>& height_sections) {
  std::vector<int64_t> abs_sections;
36 37 38 39 40 41
  abs_sections.resize(height_sections.size());
  abs_sections[0] = 0;
  for (size_t i = 1; i < height_sections.size(); ++i) {
    abs_sections[i] = height_sections[i - 1] + abs_sections[i - 1];
  }
  return abs_sections;
42 43
}

Y
Yancey 已提交
44 45 46 47 48 49
template <typename DeviceContext, typename T>
class SplitSelectedRowsOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<framework::SelectedRows>("X");
    auto outs = ctx.MultiOutput<framework::SelectedRows>("Out");
S
seiriosPlus 已提交
50
    auto height_sections = ctx.Attr<std::vector<int64_t>>("height_sections");
Y
Yancey 已提交
51

52 53
    auto abs_sections = ToAbsoluteSection(height_sections);

54 55
    auto x_rows = x->rows();
    std::vector<std::vector<int>> outs_rows_idx;
56 57
    std::vector<std::vector<int>> outs_dense_idx;

58
    outs_rows_idx.resize(outs.size());
59
    outs_dense_idx.resize(outs.size());
Y
Yancey 已提交
60

61 62 63
    auto row_numel = x->value().numel() / x->value().dims()[0];
    auto src = x->value().data<T>();

64
    // split rows index into output sparse vars
65
    for (size_t i = 0; i < x_rows.size(); ++i) {
66 67 68
      int out_idx = FindOutIdx(x_rows[i], abs_sections);
      outs_rows_idx[out_idx].push_back(x_rows[i]);
      outs_dense_idx[out_idx].push_back(i);
69 70
    }
    auto place = ctx.GetPlace();
Y
Yancey 已提交
71

72 73
    for (size_t i = 0; i < outs_rows_idx.size(); ++i) {
      auto rows_idx = outs_rows_idx[i];
74
      outs[i]->set_height(height_sections[i]);
75 76 77 78 79
      if (rows_idx.size() > 0) {
        auto dims = x->GetCompleteDims();
        dims[0] = rows_idx.size();
        outs[i]->mutable_value()->mutable_data<T>(dims, x->place());
        for (auto idx : rows_idx) {
80
          outs[i]->mutable_rows()->push_back(idx - abs_sections[i]);
81 82 83 84
        }
        auto dst = outs[i]->mutable_value()->mutable_data<T>(ctx.GetPlace());
        for (size_t j = 0; j < rows_idx.size(); j++) {
          if (platform::is_cpu_place(place)) {
85 86 87
            memory::Copy(
                platform::CPUPlace(), dst + j * row_numel, platform::CPUPlace(),
                src + outs_dense_idx[i][j] * row_numel, sizeof(T) * row_numel);
88 89 90 91
          } else {
#ifdef PADDLE_WITH_CUDA
            auto stream = ctx.cuda_device_context().stream();
            memory::Copy(platform::CUDAPlace(), dst + j * row_numel,
92 93
                         platform::CUDAPlace(),
                         src + outs_dense_idx[i][j] * row_numel,
94 95 96 97 98 99
                         sizeof(T) * row_numel, stream);
#else
            PADDLE_THROW("Paddle is not compiled with GPU");
#endif
          }
        }
Y
Yancey 已提交
100 101 102 103 104 105 106
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle