request_handler_impl.cc 7.4 KB
Newer Older
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.

W
Wang Guibao 已提交
15
#include "paddle/fluid/operators/distributed/request_handler_impl.h"
16 17 18 19 20 21 22 23
#include <iostream>
#include <string>
#include <vector>

#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/selected_rows.h"
W
Wang Guibao 已提交
24
#include "paddle/fluid/framework/variable_helper.h"
25
#include "paddle/fluid/operators/distributed/rpc_server.h"
26
#include "paddle/fluid/string/piece.h"
T
tangwei12 已提交
27
#include "paddle/fluid/string/printf.h"
28 29 30

namespace paddle {
namespace operators {
31
namespace distributed {
32

T
tangwei12 已提交
33 34
// define LOOKUP_TABLE_PATH for checkpoint notify to save lookup table variables
// to directory specified.
T
tangwei12 已提交
35
constexpr char LOOKUP_TABLE_PATH[] = "kLookupTablePath";
T
tangwei12 已提交
36

37 38 39
bool RequestSendHandler::Handle(const std::string& varname,
                                framework::Scope* scope,
                                framework::Variable* invar,
Q
qiaolongfei 已提交
40
                                framework::Variable** outvar,
W
Wu Yi 已提交
41
                                const int trainer_id,
Q
Qiao Longfei 已提交
42 43
                                const std::string& out_var_name,
                                const std::string& table_name) {
M
minqiyang 已提交
44
  VLOG(4) << "RequestSendHandler:" << varname;
45 46 47

  // Sync
  if (varname == BATCH_BARRIER_MESSAGE) {
M
minqiyang 已提交
48
    VLOG(3) << "sync: recv BATCH_BARRIER_MESSAGE";
49
    rpc_server_->IncreaseBatchBarrier(kRequestSend);
Y
Yancey1989 已提交
50
  } else if (varname == COMPLETE_MESSAGE) {
M
minqiyang 已提交
51
    VLOG(3) << "sync: recv complete message";
Y
Yancey1989 已提交
52
    rpc_server_->Complete();
53
  } else {
54 55
    // Async
    if (!sync_mode_) {
M
minqiyang 已提交
56
      VLOG(3) << "async process var: " << varname;
Q
Qiao Longfei 已提交
57 58 59
      executor_->RunPreparedContext((*grad_to_prepared_ctx_)[varname].get(),
                                    scope);
      delete scope;
60 61 62
      return true;
    } else {  // sync
      rpc_server_->WaitCond(kRequestSend);
M
minqiyang 已提交
63
      VLOG(3) << "sync: processing received var: " << varname;
64

65 66 67 68
      if (invar == nullptr) {
        LOG(FATAL) << "sync: Can not find server side var: " << varname;
        return false;
      }
Y
Yancey1989 已提交
69
    }
70 71 72 73 74 75 76
  }
  return true;
}

bool RequestGetHandler::Handle(const std::string& varname,
                               framework::Scope* scope,
                               framework::Variable* invar,
Q
qiaolongfei 已提交
77
                               framework::Variable** outvar,
W
Wu Yi 已提交
78
                               const int trainer_id,
Q
Qiao Longfei 已提交
79 80
                               const std::string& out_var_name,
                               const std::string& table_name) {
81 82
  VLOG(4) << "RequestGetHandler:" << varname
          << " out_var_name: " << out_var_name;
83

Y
Yancey1989 已提交
84 85
  if (sync_mode_) {
    if (varname == FETCH_BARRIER_MESSAGE) {
M
minqiyang 已提交
86
      VLOG(3) << "sync: recv fetch barrier message";
Y
Yancey1989 已提交
87 88
      rpc_server_->IncreaseBatchBarrier(kRequestGet);
    } else {
89
      rpc_server_->WaitCond(kRequestGet);
Y
Yancey1989 已提交
90 91 92
      *outvar = scope_->FindVar(varname);
    }
  } else {
Y
Yancey1989 已提交
93
    if (varname != FETCH_BARRIER_MESSAGE && varname != COMPLETE_MESSAGE) {
W
Wu Yi 已提交
94 95 96 97
      if (enable_dc_asgd_) {
        // NOTE: the format is determined by distributed_transpiler.py
        std::string param_bak_name =
            string::Sprintf("%s.trainer_%d_bak", varname, trainer_id);
M
minqiyang 已提交
98
        VLOG(3) << "getting " << param_bak_name << " trainer_id " << trainer_id;
W
Wu Yi 已提交
99 100 101 102 103
        auto var = scope_->FindVar(varname);
        auto t_orig = var->Get<framework::LoDTensor>();
        auto param_bak = scope_->Var(param_bak_name);
        auto t = param_bak->GetMutable<framework::LoDTensor>();
        t->mutable_data(dev_ctx_->GetPlace(), t_orig.type());
M
minqiyang 已提交
104
        VLOG(3) << "copying " << varname << " to " << param_bak_name;
W
Wu Yi 已提交
105 106
        framework::TensorCopy(t_orig, dev_ctx_->GetPlace(), t);
      }
Y
Yancey1989 已提交
107
      *outvar = scope_->FindVar(varname);
108 109 110 111 112
    }
  }
  return true;
}

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
bool RequestGetNoBarrierHandler::Handle(const std::string& varname,
                                        framework::Scope* scope,
                                        framework::Variable* invar,
                                        framework::Variable** outvar,
                                        const int trainer_id,
                                        const std::string& out_var_name,
                                        const std::string& table_name) {
  VLOG(4) << "RequestGetNoBarrierHandler:" << varname
          << " out_var_name: " << out_var_name;

  // get var from pserver immediately without barriers
  string::Piece without_barrier_piece(WITHOUT_BARRIER_MESSAGE);
  string::Piece var_name_piece = string::Piece(varname);

  if (string::Contains(var_name_piece, without_barrier_piece)) {
    var_name_piece = string::TrimSuffix(var_name_piece, without_barrier_piece);
    VLOG(4) << "Get var " << var_name_piece << " with "
            << WITHOUT_BARRIER_MESSAGE;
    *outvar = scope_->FindVar(var_name_piece.ToString());
    return true;
  } else {
    PADDLE_THROW("GetNoBarrier must contain %s", WITHOUT_BARRIER_MESSAGE);
  }
  return true;
}

139 140 141
bool RequestPrefetchHandler::Handle(const std::string& varname,
                                    framework::Scope* scope,
                                    framework::Variable* invar,
Q
qiaolongfei 已提交
142
                                    framework::Variable** outvar,
W
Wu Yi 已提交
143
                                    const int trainer_id,
Q
Qiao Longfei 已提交
144 145
                                    const std::string& out_var_name,
                                    const std::string& table_name) {
M
minqiyang 已提交
146
  VLOG(4) << "RequestPrefetchHandler " << varname;
147

Q
Qiao Longfei 已提交
148
  if (table_name.empty()) {
Q
Qiao Longfei 已提交
149 150
    auto var_desc = program_->Block(0).FindVar(out_var_name);
    InitializeVariable(*outvar, var_desc->GetType());
Q
Qiao Longfei 已提交
151 152 153
    executor_->RunPreparedContext(
        (*prefetch_var_name_to_prepared_ctx_)[varname].get(), scope);
  } else {
Q
Qiao Longfei 已提交
154
    (*outvar)->GetMutable<framework::LoDTensor>();
Q
Qiao Longfei 已提交
155 156 157 158 159
    auto lookup_table_op =
        BuildLookupTableOp(table_name, varname, out_var_name);
    paddle::platform::CPUPlace cpu_place;
    lookup_table_op->Run(*scope, cpu_place);
  }
160 161 162
  return true;
}

T
tangwei12 已提交
163 164 165 166
bool RequestCheckpointHandler::Handle(const std::string& varname,
                                      framework::Scope* scope,
                                      framework::Variable* invar,
                                      framework::Variable** outvar,
W
Wu Yi 已提交
167
                                      const int trainer_id,
Q
Qiao Longfei 已提交
168 169
                                      const std::string& out_var_name,
                                      const std::string& table_name) {
170 171 172
  PADDLE_ENFORCE(
      checkpoint_notify_id != -1,
      "when checkpoint_notify_id = -1, there should be no RPC invoke.");
T
tangwei12 已提交
173

T
tangwei12 已提交
174
  // TODO(tangwei12): find out why scope will be error.
T
bug fix  
tangwei12 已提交
175
  auto* lt_var = scope_->FindVar(LOOKUP_TABLE_PATH)->GetMutable<std::string>();
T
tangwei12 已提交
176 177
  lt_var->clear();
  lt_var->append(out_var_name);
M
minqiyang 已提交
178 179
  VLOG(4) << "RequestCheckpointHandler update var kLookupTablePath to: "
          << out_var_name;
T
bug fix  
tangwei12 已提交
180
  executor_->RunPreparedContext(checkpoint_prepared_ctx_.get(), scope_);
T
bug fix  
tangwei12 已提交
181 182
  return true;
}
T
tangwei12 已提交
183

184
}  // namespace distributed
185 186
}  // namespace operators
}  // namespace paddle