request_handler_impl.cc 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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.

#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"
23 24
#include "paddle/fluid/operators/distributed/request_handler_impl.h"
#include "paddle/fluid/operators/distributed/rpc_server.h"
T
tangwei12 已提交
25
#include "paddle/fluid/string/printf.h"
26 27 28

namespace paddle {
namespace operators {
29
namespace distributed {
30

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

35 36 37
bool RequestSendHandler::Handle(const std::string& varname,
                                framework::Scope* scope,
                                framework::Variable* invar,
Q
qiaolongfei 已提交
38
                                framework::Variable** outvar,
W
Wu Yi 已提交
39
                                const int trainer_id,
Q
qiaolongfei 已提交
40
                                const std::string& out_var_name) {
41
  VLOG(40) << "RequestSendHandler:" << varname;
42 43 44

  // Sync
  if (varname == BATCH_BARRIER_MESSAGE) {
45
    VLOG(30) << "sync: recv BATCH_BARRIER_MESSAGE";
46
    rpc_server_->IncreaseBatchBarrier(kRequestSend);
Y
Yancey1989 已提交
47
  } else if (varname == COMPLETE_MESSAGE) {
48
    VLOG(30) << "sync: recv complete message";
Y
Yancey1989 已提交
49
    rpc_server_->Complete();
50
  } else {
51 52
    // Async
    if (!sync_mode_) {
53
      VLOG(30) << "async process var: " << varname;
54 55 56 57 58 59 60 61 62 63
      try {
        executor_->RunPreparedContext((*grad_to_prepared_ctx_)[varname].get(),
                                      scope);
      } catch (std::exception& e) {
        LOG(ERROR) << "async: run sub program error " << e.what();
        return false;
      }
      return true;
    } else {  // sync
      rpc_server_->WaitCond(kRequestSend);
64
      VLOG(30) << "sync: processing received var: " << varname;
65

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

bool RequestGetHandler::Handle(const std::string& varname,
                               framework::Scope* scope,
                               framework::Variable* invar,
Q
qiaolongfei 已提交
78
                               framework::Variable** outvar,
W
Wu Yi 已提交
79
                               const int trainer_id,
Q
qiaolongfei 已提交
80
                               const std::string& out_var_name) {
81
  VLOG(40) << "RequestGetHandler:" << varname;
Y
Yancey1989 已提交
82 83
  if (sync_mode_) {
    if (varname == FETCH_BARRIER_MESSAGE) {
84
      VLOG(30) << "sync: recv fetch barrier message";
Y
Yancey1989 已提交
85 86
      rpc_server_->IncreaseBatchBarrier(kRequestGet);
    } else {
87
      rpc_server_->WaitCond(kRequestGet);
Y
Yancey1989 已提交
88 89 90
      *outvar = scope_->FindVar(varname);
    }
  } else {
Y
Yancey1989 已提交
91
    if (varname != FETCH_BARRIER_MESSAGE && varname != COMPLETE_MESSAGE) {
W
Wu Yi 已提交
92 93 94 95
      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);
96 97
        VLOG(30) << "getting " << param_bak_name << " trainer_id "
                 << trainer_id;
W
Wu Yi 已提交
98 99 100 101 102
        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());
103
        VLOG(30) << "copying " << varname << " to " << param_bak_name;
W
Wu Yi 已提交
104 105
        framework::TensorCopy(t_orig, dev_ctx_->GetPlace(), t);
      }
Y
Yancey1989 已提交
106
      *outvar = scope_->FindVar(varname);
107 108 109 110 111 112 113 114
    }
  }
  return true;
}

bool RequestPrefetchHandler::Handle(const std::string& varname,
                                    framework::Scope* scope,
                                    framework::Variable* invar,
Q
qiaolongfei 已提交
115
                                    framework::Variable** outvar,
W
Wu Yi 已提交
116
                                    const int trainer_id,
Q
qiaolongfei 已提交
117
                                    const std::string& out_var_name) {
118
  VLOG(40) << "RequestPrefetchHandler " << varname;
119

Q
qiaolongfei 已提交
120
  auto var_desc = program_->Block(0).FindVar(out_var_name);
121
  InitializeVariable(*outvar, var_desc->GetType());
122 123
  executor_->RunPreparedContext(
      (*prefetch_var_name_to_prepared_ctx_)[varname].get(), scope);
124 125 126 127

  return true;
}

T
tangwei12 已提交
128 129 130 131
bool RequestCheckpointHandler::Handle(const std::string& varname,
                                      framework::Scope* scope,
                                      framework::Variable* invar,
                                      framework::Variable** outvar,
W
Wu Yi 已提交
132
                                      const int trainer_id,
T
bug fix  
tangwei12 已提交
133
                                      const std::string& out_var_name) {
134 135 136
  PADDLE_ENFORCE(
      checkpoint_notify_id != -1,
      "when checkpoint_notify_id = -1, there should be no RPC invoke.");
T
tangwei12 已提交
137

T
tangwei12 已提交
138
  // TODO(tangwei12): find out why scope will be error.
T
bug fix  
tangwei12 已提交
139
  auto* lt_var = scope_->FindVar(LOOKUP_TABLE_PATH)->GetMutable<std::string>();
T
tangwei12 已提交
140 141
  lt_var->clear();
  lt_var->append(out_var_name);
142 143
  VLOG(40) << "RequestCheckpointHandler update var kLookupTablePath to: "
           << out_var_name;
T
bug fix  
tangwei12 已提交
144
  executor_->RunPreparedContext(checkpoint_prepared_ctx_.get(), scope_);
T
bug fix  
tangwei12 已提交
145 146
  return true;
}
T
tangwei12 已提交
147

148
}  // namespace distributed
149 150
}  // namespace operators
}  // namespace paddle