parameter_recv.cc 8.8 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   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.

15
#include <algorithm>
Q
Qiao Longfei 已提交
16
#include <memory>
Q
Qiao Longfei 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <set>
#include <string>
#include <vector>

#include "paddle/fluid/operators/distributed/parameter_recv.h"

#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/selected_rows.h"
#include "paddle/fluid/framework/tensor.h"

#include "paddle/fluid/operators/distributed/distributed.h"
#include "paddle/fluid/operators/distributed/rpc_client.h"
#include "paddle/fluid/operators/distributed/variable_response.h"
#include "paddle/fluid/operators/distributed_ops/send_recv_util.h"
Q
Qiao Longfei 已提交
32
#include "paddle/fluid/operators/strided_memcpy.h"
Q
Qiao Longfei 已提交
33 34 35 36 37 38 39 40 41 42 43

namespace paddle {
namespace operators {
namespace distributed {

using LoDTensor = framework::LoDTensor;
using LoDTensor = framework::LoDTensor;
using SelectedRows = framework::SelectedRows;
using DDim = framework::DDim;

template <typename T>
T
tangwei12 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 97 98 99 100 101 102 103 104
void RecvSparseLodTensor(const CommContext &rpc_ctx,
                         const framework::Scope &scope) {
  platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
  auto cpu_place = platform::CPUPlace();
  auto &cpu_ctx = *pool.Get(cpu_place);

  distributed::RPCClient *rpc_client =
      distributed::RPCClient::GetInstance<RPCCLIENT_T>(rpc_ctx.trainer_id);

  std::unique_ptr<framework::Scope> local_scope = scope.NewTmpScope();
  std::vector<const float *> tensors;
  std::vector<distributed::VarHandlePtr> rets;
  for (size_t i = 0; i < rpc_ctx.splited_varnames.size(); i++) {
    auto &recv_var_name = rpc_ctx.splited_varnames[i];
    auto *local_var = local_scope->Var(recv_var_name);
    VLOG(4) << "recv " << recv_var_name << " from " << rpc_ctx.epmap[i];
    // sparse param in recv_scope is LoDTensor
    rets.push_back(rpc_client->AsyncGetVarNoBarrier(
        rpc_ctx.epmap[i], cpu_ctx, *local_scope.get(), recv_var_name,
        recv_var_name));

    const auto *value = local_var->Get<framework::LoDTensor>().data<float>();
    tensors.push_back(value);
  }

  for (size_t i = 0; i < rets.size(); i++) {
    PADDLE_ENFORCE_NE(rets[i]->Wait(), 0U, platform::errors::ExecutionTimeout(
                                               "internal error in RPCClient"));
  }

  auto *merged_var = scope.FindVar(rpc_ctx.var_name);

  if (merged_var == nullptr || !merged_var->IsInitialized()) {
    PADDLE_THROW(
        platform::errors::InvalidArgument("%s must initialized at first."));
  }
  auto dims1 = merged_var->Get<framework::LoDTensor>().dims()[1];
  int64_t height = 0;
  for (size_t i = 0; i < rpc_ctx.splited_varnames.size(); i++) {
    auto *splited_var = local_scope->FindVar(rpc_ctx.splited_varnames[i]);
    height += splited_var->Get<framework::LoDTensor>().dims()[0];
  }

  PADDLE_ENFORCE_EQ(merged_var->Get<framework::LoDTensor>().dims()[0], height,
                    "recved var must has same dims with local var");

  auto *merged_t = merged_var->GetMutable<framework::LoDTensor>();
  auto *merged_d = merged_t->mutable_data<float>(cpu_place);

  auto pserver_num = rpc_ctx.splited_varnames.size();
  for (int x = 0; x < height; ++x) {
    auto id = x % pserver_num;
    auto idx = x / pserver_num;
    std::memcpy(merged_d + x * dims1, tensors[id] + idx * dims1,
                sizeof(float) * dims1);
  }
}

template <typename T>
void RecvGeoSparseRecords(const CommContext &rpc_ctx,
                          const framework::Scope &scope) {
105 106 107 108 109 110 111
  platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
  auto cpu_place = platform::CPUPlace();
  auto &cpu_ctx = *pool.Get(cpu_place);

  distributed::RPCClient *rpc_client =
      distributed::RPCClient::GetInstance<RPCCLIENT_T>(rpc_ctx.trainer_id);

112
  std::unique_ptr<framework::Scope> local_scope = scope.NewTmpScope();
Q
Qiao Longfei 已提交
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  std::vector<distributed::VarHandlePtr> rets;
  for (size_t i = 0; i < rpc_ctx.splited_varnames.size(); i++) {
    auto &recv_var_name = rpc_ctx.splited_varnames[i];
    local_scope->Var(recv_var_name);
    VLOG(4) << "recv " << recv_var_name << " from " << rpc_ctx.epmap[i];
    // sparse param in recv_scope is LoDTensor
    rets.push_back(rpc_client->AsyncGetVar(rpc_ctx.epmap[i], cpu_ctx,
                                           *local_scope.get(), recv_var_name,
                                           recv_var_name, recv_var_name));
  }

  for (size_t i = 0; i < rets.size(); i++) {
    PADDLE_ENFORCE_NE(rets[i]->Wait(), 0U, platform::errors::ExecutionTimeout(
                                               "internal error in RPCClient"));
  }

  int64_t height = 0;
  int64_t ids_num = 0;
  int64_t width = 0;

  std::vector<int64_t> all_ids;
  auto pserver_num = rpc_ctx.splited_varnames.size();

  for (size_t i = 0; i < rpc_ctx.splited_varnames.size(); i++) {
    auto &recv_var_name = rpc_ctx.splited_varnames[i];
    auto *recv_var = local_scope->FindVar(recv_var_name);
    auto &recv_t = recv_var->Get<framework::SelectedRows>();

    height += recv_t.height();
    ids_num += recv_t.rows().size();
    width = recv_t.value().dims()[1];

T
tangwei12 已提交
146 147 148 149 150 151 152 153
    if (rpc_ctx.is_distributed) {
      std::copy(recv_t.rows().begin(), recv_t.rows().end(),
                std::back_inserter(all_ids));
    } else {
      std::transform(recv_t.rows().begin(), recv_t.rows().end(),
                     std::back_inserter(all_ids),
                     [&](int64_t id) { return id * pserver_num + i; });
    }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  }

  auto *var = scope.FindVar(rpc_ctx.var_name);
  auto *t_ = var->GetMutable<framework::SelectedRows>();
  T *out_data =
      t_->mutable_value()->mutable_data<T>({ids_num, width}, cpu_place);
  t_->set_height(height);
  t_->set_rows(all_ids);

  int64_t cnt = 0;
  for (size_t i = 0; i < rpc_ctx.splited_varnames.size(); i++) {
    auto &recv_var_name = rpc_ctx.splited_varnames[i];
    auto *recv_var = local_scope->FindVar(recv_var_name);
    auto &recv_t = recv_var->Get<framework::SelectedRows>();

    auto rows = recv_t.rows().size();
    const T *in_data = recv_t.value().data<T>();
    std::copy_n(in_data, rows * width, out_data + cnt);
    cnt += rows * width;
  }
  t_->SyncIndex();
}

template <typename T>
void RecvLodTensor(const CommContext &rpc_ctx, const framework::Scope &scope) {
Q
Qiao Longfei 已提交
179
  platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
180 181
  auto cpu_place = platform::CPUPlace();
  auto &cpu_ctx = *pool.Get(cpu_place);
Q
Qiao Longfei 已提交
182 183

  distributed::RPCClient *rpc_client =
Q
Qiao Longfei 已提交
184
      distributed::RPCClient::GetInstance<RPCCLIENT_T>(rpc_ctx.trainer_id);
Q
Qiao Longfei 已提交
185

186 187 188 189 190 191 192 193 194 195
  std::vector<distributed::VarHandlePtr> rets;

  // variable do not spilt
  if (rpc_ctx.origin_varnames.size() == 1 &&
      rpc_ctx.splited_varnames.size() == 1) {
    auto varname = rpc_ctx.origin_varnames[0];
    VLOG(4) << "recv " << varname << " from " << rpc_ctx.epmap[0];
    rets.push_back(rpc_client->AsyncGetVarNoBarrier(rpc_ctx.epmap[0], cpu_ctx,
                                                    scope, varname, varname));

Q
Qiao Longfei 已提交
196
    for (size_t i = 0; i < rets.size(); i++) {
197 198 199
      PADDLE_ENFORCE_NE(
          rets[i]->Wait(), 0U,
          platform::errors::ExecutionTimeout("internal error in RPCClient"));
Q
Qiao Longfei 已提交
200
    }
201 202 203

    VLOG(3) << "ParameterRecv out " << rpc_ctx.var_name;
    return;
Q
Qiao Longfei 已提交
204
  } else {
205 206 207
    PADDLE_ENFORCE(false, platform::errors::Unimplemented(
                              "ParameterRecv can not recv dense with multi "
                              "parts now, add it soon."));
Q
Qiao Longfei 已提交
208
  }
209
}
Q
Qiao Longfei 已提交
210

211 212
template <typename T>
void ParameterRecv<T>::operator()(const CommContext &rpc_ctx,
T
tangwei12 已提交
213 214
                                  const framework::Scope &scope,
                                  bool geo_records) {
215 216 217 218 219 220 221
  VLOG(3) << "ParameterRecv in " << rpc_ctx.var_name;

  PADDLE_ENFORCE_GE(rpc_ctx.origin_varnames.size(), 1,
                    platform::errors::InvalidArgument(
                        "origin_varnames.size() >= 1 is permitted"));

  if (rpc_ctx.is_sparse) {
T
tangwei12 已提交
222 223 224 225 226
    if (geo_records) {
      RecvGeoSparseRecords<T>(rpc_ctx, scope);
    } else {
      RecvSparseLodTensor<T>(rpc_ctx, scope);
    }
227 228
  } else {
    RecvLodTensor<T>(rpc_ctx, scope);
Q
Qiao Longfei 已提交
229 230
  }

231 232 233 234 235
  VLOG(3) << "ParameterRecv out " << rpc_ctx.var_name;
}
template <typename T>
void ParameterRecv<T>::operator()(const CommContext &rpc_ctx,
                                  const framework::Scope &scope) {
T
tangwei12 已提交
236
  this->operator()(rpc_ctx, scope, false);
Q
Qiao Longfei 已提交
237 238 239 240 241 242 243
}

template struct ParameterRecv<float>;

};  // namespace distributed
};  // namespace operators
};  // namespace paddle