accumulation_node.cc 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 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 "paddle/fluid/eager/accumulation/accumulation_node.h"
#include "paddle/fluid/eager/eager_tensor.h"
17
#include "paddle/fluid/imperative/gradient_accumulator.h"
18

19 20
#include "paddle/phi/api/all.h"
#include "paddle/phi/core/dense_tensor.h"
21 22 23 24 25 26

#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/errors.h"

#include "glog/logging.h"
27

28 29
namespace egr {

30 31
static void CopyOrAddTensor(paddle::experimental::Tensor* tensor,
                            const paddle::experimental::Tensor& t) {
32 33 34 35 36
  if (!tensor->defined() || !tensor->initialized()) {
    // Simply copy tensor->impl
    *tensor = t;
  } else {
    // Accumulation
37
    paddle::imperative::TensorAdd<paddle::experimental::Tensor>(t, tensor);
38 39 40
  }
}

41
std::vector<std::vector<paddle::experimental::Tensor>> GradNodeAccumulation::
42 43
operator()(
    const std::vector<std::vector<paddle::experimental::Tensor>>& grads) {
44
  VLOG(3) << "Running Eager Backward Node: GradNodeAccumulation";
45 46 47 48 49 50 51 52 53 54 55
  PADDLE_ENFORCE(grads.size() == 1,
                 paddle::platform::errors::Fatal(
                     "GradNodeAccumulation should take exactly 1 grad tensor"
                     "However received: %d slot.",
                     grads.size()));
  PADDLE_ENFORCE(grads[0].size() == 1,
                 paddle::platform::errors::Fatal(
                     "GradNodeAccumulation should take exactly 1 grad tensor"
                     "However received: %d in slot %d .",
                     grads[0].size(), 0));
  // Apply Gradient Hooks
56
  paddle::experimental::Tensor grad_out;
57
  if (GradientHooksRegistered()) {
58
    std::vector<std::vector<paddle::experimental::Tensor>> hooked_grads =
59
        ApplyGradientHooks(grads);
60
    grad_out = hooked_grads[0][0];
61
  } else {
62
    grad_out = grads[0][0];
63 64
  }

65
  if (!weak_grad_.expired()) {
66 67
    auto grad = weak_grad_.lock();
    CopyOrAddTensor(grad.get(), grad_out);
68 69 70 71 72 73 74
  }

  // Apply Reduce Hooks
  if (ReduceHooksRegistered()) {
    ApplyReduceHooks();
  }

75
  return {{grad_out}};
76 77
}

78
void GradNodeAccumulation::RegisterReduceHook(
79 80
    std::shared_ptr<TensorVoidHook>&& hook) {
  reduce_hooks_.emplace_back(std::move(hook));
81 82 83 84
}

void GradNodeAccumulation::ApplyReduceHooks() {
  for (auto& hook : reduce_hooks_) {
85
    (*hook)();
86 87
  }
}
88
}  // namespace egr