split_selected_rows_op.h 4.4 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
#include "paddle/fluid/framework/op_registry.h"
Q
Qiao Longfei 已提交
19
#include "paddle/fluid/operators/distributed_ops/send_recv_util.h"
Y
Yi Wang 已提交
20
#include "paddle/fluid/operators/math/selected_rows_functor.h"
Y
Yancey 已提交
21 22 23 24 25 26 27 28 29 30

namespace paddle {
namespace operators {

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");
T
tangwei12 已提交
31
    auto height_sections = ctx.Attr<std::vector<int64_t>>("height_sections");
Y
Yancey 已提交
32

33 34
    auto abs_sections = ToAbsoluteSection(height_sections);

Q
Qiao Longfei 已提交
35 36
    auto& x_rows = x->rows();
    auto height = x->height();
37
    std::vector<std::vector<int>> outs_rows_idx;
38 39
    std::vector<std::vector<int>> outs_dense_idx;

40
    outs_rows_idx.resize(outs.size());
41
    outs_dense_idx.resize(outs.size());
Y
Yancey 已提交
42

43 44 45
    auto row_numel = x->value().numel() / x->value().dims()[0];
    auto src = x->value().data<T>();

46
    // split rows index into output sparse vars
47
    for (size_t i = 0; i < x_rows.size(); ++i) {
Q
Qiao Longfei 已提交
48
      auto& id = x_rows[i];
49 50 51 52 53
      PADDLE_ENFORCE_LT(id, height,
                        platform::errors::OutOfRange(
                            "Each row_id in x.rows must be less than x.height. "
                            "But received x.rows[%d] = %d, x.height = %d",
                            i, id, height));
Q
Qiao Longfei 已提交
54 55
      int out_idx = GetSectionIndex(id, abs_sections);
      outs_rows_idx[out_idx].push_back(id);
56
      outs_dense_idx[out_idx].push_back(i);
57 58
    }
    auto place = ctx.GetPlace();
Y
Yancey 已提交
59

60 61
    for (size_t i = 0; i < outs_rows_idx.size(); ++i) {
      auto rows_idx = outs_rows_idx[i];
62
      outs[i]->set_height(height_sections[i]);
63 64 65 66
      auto dims = x->GetCompleteDims();
      dims[0] = rows_idx.size();
      outs[i]->mutable_value()->mutable_data<T>(dims, x->place());
      outs[i]->mutable_rows()->clear();
67 68
      if (rows_idx.size() > 0) {
        for (auto idx : rows_idx) {
Q
Qiao Longfei 已提交
69
          auto id_offset = idx - abs_sections[i];
70 71 72 73 74 75
          PADDLE_ENFORCE_LT(
              id_offset, height_sections[i],
              platform::errors::OutOfRange("Each row_id in out.rows must be "
                                           "less than out.height. But recived "
                                           "out.rows = [%d], out.height = [%d]",
                                           id_offset, height_sections[i]));
Q
Qiao Longfei 已提交
76
          outs[i]->mutable_rows()->push_back(id_offset);
77 78 79 80
        }
        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)) {
81 82 83
            memory::Copy(
                platform::CPUPlace(), dst + j * row_numel, platform::CPUPlace(),
                src + outs_dense_idx[i][j] * row_numel, sizeof(T) * row_numel);
84
          } else {
85
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
86 87
            auto stream = ctx.cuda_device_context().stream();
            memory::Copy(platform::CUDAPlace(), dst + j * row_numel,
88 89
                         platform::CUDAPlace(),
                         src + outs_dense_idx[i][j] * row_numel,
90 91
                         sizeof(T) * row_numel, stream);
#else
92 93
            PADDLE_THROW(platform::errors::Unavailable(
                "Paddle is not compiled with CUDA. Cannot visit cuda device"));
94 95 96
#endif
          }
        }
Y
Yancey 已提交
97
      }
98
      PADDLE_ENFORCE_EQ(rows_idx.size(), outs[i]->rows().size(),
99 100 101 102
                        platform::errors::InvalidArgument(
                            "rows should has the same size with tensor dim 0. "
                            "But received rows = %d, tensor's dim[0] = %d.",
                            rows_idx.size(), outs[i]->rows().size()));
Y
Yancey 已提交
103 104 105 106 107 108
    }
  }
};

}  // namespace operators
}  // namespace paddle