section_worker.cc 4.5 KB
Newer Older
H
hutuxian 已提交
1 2 3 4 5 6 7 8 9 10 11
/* Copyright (c) 2019 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. */

12
#if defined(PADDLE_WITH_NCCL)
L
lilong12 已提交
13 14 15 16 17
#include <float.h>
#include "paddle/fluid/framework/executor_gc_helper.h"
#include "paddle/fluid/framework/garbage_collector.h"
#include "paddle/fluid/framework/program_desc.h"

H
hutuxian 已提交
18 19 20 21 22
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"

#include "paddle/fluid/framework/device_worker.h"
H
hutuxian 已提交
23
#include "paddle/fluid/framework/fleet/box_wrapper.h"
H
hutuxian 已提交
24 25 26 27 28 29 30 31 32
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/framework/trainer_desc.pb.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/lodtensor_printer.h"

namespace paddle {
namespace framework {

L
lilong12 已提交
33 34
uint64_t SectionWorker::batch_id_(0);

H
hutuxian 已提交
35
void SectionWorker::Initialize(const TrainerDesc& desc) {
H
hutuxian 已提交
36
  dev_ctx_ = platform::DeviceContextPool::Instance().Get(place_);
37 38
  program_.reset(
      new ProgramDesc(desc.section_param().section_config().program_desc()));
L
lilong12 已提交
39
  for (auto& op_desc : program_->Block(0).AllOps()) {
H
hutuxian 已提交
40 41 42 43 44
    ops_.push_back(OpRegistry::CreateOp(*op_desc));
  }
}

void SectionWorker::TrainFiles() {
45
  VLOG(5) << "begin section_worker TrainFiles";
H
hutuxian 已提交
46

47
  int64_t max_memory_size = GetEagerDeletionThreshold();
L
lilong12 已提交
48 49
  std::unique_ptr<GarbageCollector> gc;
  auto unused_vars_ = GetUnusedVars(program_->Block(0), ops_, skip_vars_);
50
  if (max_memory_size >= 0) {
L
lilong12 已提交
51
#ifdef PADDLE_WITH_CUDA
52 53 54 55 56
    if (platform::is_gpu_place(place_)) {
      if (IsFastEagerDeletionModeEnabled()) {
        gc.reset(new UnsafeFastGPUGarbageCollector(
            BOOST_GET_CONST(platform::CUDAPlace, place_), max_memory_size));
      }
H
hutuxian 已提交
57
    }
L
lilong12 已提交
58 59
#endif
  }
H
hutuxian 已提交
60

61 62 63 64 65 66
  for (int i = 0; i < num_microbatches_; ++i) {
    for (auto& op : ops_) {
      int op_role = op->Attr<int>(std::string("op_role"));
      // We run op with op_role = kLRSched only for the first microbatch
      // to avoid increasing the @LR_DECAY_STEP@ multiple times.
      bool run_first_mbatch = op_role == static_cast<int>(OpRole::kForward) ||
L
lilong12 已提交
67
                              op_role == (static_cast<int>(OpRole::kForward) |
68 69 70 71 72 73 74 75 76 77 78 79
                                          static_cast<int>(OpRole::kLoss)) ||
                              op_role == static_cast<int>(OpRole::kLRSched);
      bool run_others = op_role == static_cast<int>(OpRole::kForward) ||
                        op_role == (static_cast<int>(OpRole::kForward) |
                                    static_cast<int>(OpRole::kLoss));
      if ((i == 0 && run_first_mbatch) || (i != 0 && run_others)) {
        VLOG(3) << "Forward: running op " << op->Type() << " for micro-batch "
                << i;
        op->Run(*microbatch_scopes_[i], place_);
        if (gc) {
          DeleteUnusedTensors(*microbatch_scopes_[i], op.get(), unused_vars_,
                              gc.get());
L
lilong12 已提交
80
        }
H
hutuxian 已提交
81
      }
H
hutuxian 已提交
82
    }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    cudaDeviceSynchronize();
  }

  // backward pass
  for (int i = 0; i < num_microbatches_; ++i) {
    for (auto& op : ops_) {
      int op_role = op->Attr<int>(std::string("op_role"));
      if (op_role == static_cast<int>(OpRole::kBackward) ||
          op_role == (static_cast<int>(OpRole::kBackward) |
                      static_cast<int>(OpRole::kLoss))) {
        VLOG(3) << "Backward: running op " << op->Type() << " for micro-batch "
                << i;
        op->Run(*microbatch_scopes_[i], place_);
        if (gc) {
          DeleteUnusedTensors(*microbatch_scopes_[i], op.get(), unused_vars_,
                              gc.get());
L
lilong12 已提交
99 100
        }
      }
H
hutuxian 已提交
101
    }
102
    cudaDeviceSynchronize();
H
hutuxian 已提交
103 104
  }

105
  // update pass
H
hutuxian 已提交
106
  for (auto& op : ops_) {
107 108 109 110 111 112 113
    int op_role = op->Attr<int>(std::string("op_role"));
    if (op_role == static_cast<int>(OpRole::kOptimize)) {
      VLOG(3) << "Update: running op " << op->Type();
      op->Run(*microbatch_scopes_[0], place_);
      if (gc) {
        DeleteUnusedTensors(*microbatch_scopes_[0], op.get(), unused_vars_,
                            gc.get());
H
hutuxian 已提交
114 115 116
      }
    }
  }
117 118
  dev_ctx_->Wait();
  ++batch_id_;
H
hutuxian 已提交
119
}
120

H
hutuxian 已提交
121 122 123
}  // namespace framework
}  // namespace paddle
#endif