reduce_and_gather.h 5.4 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) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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 <algorithm>
#include <map>
#include <vector>
19

C
chengduoZH 已提交
20 21 22 23 24 25 26 27
#include "paddle/fluid/framework/details/reduce_and_gather.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/selected_rows.h"
namespace paddle {
namespace framework {
namespace details {

struct ReduceLoDTensor {
C
chengduoZH 已提交
28
  const std::vector<const LoDTensor *> &src_tensors_;
C
chengduoZH 已提交
29 30
  LoDTensor &dst_tensor_;

C
chengduoZH 已提交
31
  ReduceLoDTensor(const std::vector<const LoDTensor *> &src, LoDTensor *dst)
C
chengduoZH 已提交
32 33 34
      : src_tensors_(src), dst_tensor_(*dst) {}

  template <typename T>
D
dzhwinter 已提交
35
  void apply() const {
36 37 38
    PADDLE_ENFORCE_NE(src_tensors_.empty(), true,
                      platform::errors::InvalidArgument(
                          "The number of tensors to be reduced is 0."));
C
chengduoZH 已提交
39
    auto &t0 = *src_tensors_[0];
40 41 42
    PADDLE_ENFORCE_NE(t0.numel(), 0,
                      platform::errors::InvalidArgument(
                          "The size of first tensor to be reduced is 0."));
C
chengduo 已提交
43

C
chengduoZH 已提交
44 45 46
    dst_tensor_.Resize(t0.dims());
    T *dst = dst_tensor_.mutable_data<T>(platform::CPUPlace());

C
chengduo 已提交
47
    for (size_t i = 0; i < src_tensors_.size(); ++i) {
C
chengduoZH 已提交
48
      auto &t = *src_tensors_[i];
C
chengduo 已提交
49 50 51 52
      if (dst == t.data<T>()) {
        continue;
      }

53 54 55 56 57 58 59 60 61 62 63 64 65
      PADDLE_ENFORCE_EQ(t.dims(), t0.dims(),
                        platform::errors::InvalidArgument(
                            "The shape of tensors to be reduced must be "
                            "consistent. The shape of current tensor is %s, "
                            "but the shape of the first tensor is %s.",
                            t.dims(), t0.dims()));

      PADDLE_ENFORCE_EQ(t.type(), t0.type(),
                        platform::errors::InvalidArgument(
                            "The type of tensors to be reduced must be "
                            "consistent. The type of current tensor is %s, "
                            "but the type of the first tensor is %s.",
                            t.type(), t0.type()));
C
chengduoZH 已提交
66 67 68 69 70 71
      std::transform(t.data<T>(), t.data<T>() + t.numel(), dst, dst,
                     [](T a, T b) -> T { return a + b; });
    }
  }
};

C
chengduo 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
struct ReduceBufferData {
  const std::vector<const void *> &src_data_;
  void *dst_data_;
  int64_t numel_;

  ReduceBufferData(const std::vector<const void *> &src, void *dst,
                   int64_t numel)
      : src_data_(src), dst_data_(dst), numel_(numel) {}

  template <typename T>
  void apply() const {
    T *dst_data = reinterpret_cast<T *>(dst_data_);
    for (size_t i = 0; i < src_data_.size(); ++i) {
      auto srd_data = reinterpret_cast<const T *>(src_data_[i]);
      VLOG(10) << "dst: " << dst_data_ << ", " << srd_data;
      if (srd_data == dst_data_) {
        continue;
      }

      std::transform(srd_data, srd_data + numel_, dst_data, dst_data,
                     [](T a, T b) -> T { return a + b; });
    }
  }
};

97 98 99 100 101 102 103 104 105 106
struct GatherLocalSelectedRowsFunctor {
  GatherLocalSelectedRowsFunctor(
      const std::vector<const SelectedRows *> &src_selected_rows,
      const std::vector<platform::Place> &in_places,
      const std::map<platform::Place, platform::DeviceContext *> &dev_ctxes,
      const platform::Place &out_place, SelectedRows *dst_selected_rows)
      : dev_ctxes_(dev_ctxes),
        in_places_(in_places),
        out_place_(out_place),
        dst_selected_rows_(dst_selected_rows) {
107 108 109
    PADDLE_ENFORCE_NE(src_selected_rows.empty(), true,
                      platform::errors::InvalidArgument(
                          "The number of selected_rows to be gathered is 0."));
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    std::vector<int64_t> out_rows;

    for (auto in_sr_ptr : src_selected_rows) {
      auto &in_sr = *in_sr_ptr;
      in_tensors_.emplace_back(in_sr.value());
      out_rows.insert(out_rows.end(), in_sr.rows().begin(), in_sr.rows().end());
    }

    auto &pre_in = src_selected_rows[0];

    auto &dst_tensor = *dst_selected_rows_;
    dst_tensor.set_height(pre_in->height());
    dst_tensor.set_rows(out_rows);
    size_t rows = out_rows.size();
    DDim out_dim = pre_in->GetCompleteDims();
    out_dim[0] = static_cast<int64_t>(rows);
    dst_tensor.mutable_value()->Resize(out_dim);
    dst_tensor.mutable_value()->mutable_data(out_place, pre_in->value().type());
C
chengduoZH 已提交
129 130
  }

131 132 133 134 135 136 137 138 139 140 141
  void operator()() {
    auto *out_tensor = dst_selected_rows_->mutable_value();
    // copy
    int s = 0, e = 0;
    for (size_t j = 0; j < in_tensors_.size(); ++j) {
      e += in_tensors_[j].dims()[0];
      auto sub_out = out_tensor->Slice(s, e);
      paddle::framework::TensorCopy(in_tensors_[j], out_place_,
                                    *(dev_ctxes_.at(in_places_[j])), &sub_out);
      s = e;
    }
C
chengduoZH 已提交
142
  }
143 144 145 146 147 148 149 150 151

 private:
  const std::map<platform::Place, platform::DeviceContext *> &dev_ctxes_;
  std::vector<platform::Place> in_places_;
  std::vector<Tensor> in_tensors_;

  platform::Place out_place_;
  SelectedRows *dst_selected_rows_;
};
C
chengduoZH 已提交
152 153 154 155

}  // namespace details
}  // namespace framework
}  // namespace paddle