accumulation_node.cc 3.0 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 27

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

#include "glog/logging.h"

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

namespace egr {

void GradNodeAccumulation::RetainGrad(
42 43
    const std::function<paddle::experimental::Tensor(
        const paddle::experimental::Tensor&)>& hook) {
44 45 46
  retain_grad_hook_ = hook;
}

47 48 49
std::vector<std::vector<paddle::experimental::Tensor>> GradNodeAccumulation::
operator()(
    const std::vector<std::vector<paddle::experimental::Tensor>>& grads) {
50 51 52 53 54 55 56 57 58 59 60 61
  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
  if (GradientHooksRegistered()) {
62
    std::vector<std::vector<paddle::experimental::Tensor>> hooked_grads =
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        ApplyGradientHooks(grads);
    // TODO(jiabin): It's little weird
    CopyOrAddTensor(&accumulated_grad, hooked_grads[0][0]);
  } else {
    CopyOrAddTensor(&accumulated_grad, grads[0][0]);
  }

  if (retain_grad_hook_ != nullptr) {
    retain_grad_hook_(accumulated_grad);
  }

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

  return {{accumulated_grad}};
}

82 83 84 85 86 87 88 89 90 91
void GradNodeAccumulation::RegisterReduceHook(
    const std::function<void(void)>& hook) {
  reduce_hooks_.emplace_back(hook);
}

void GradNodeAccumulation::ApplyReduceHooks() {
  for (auto& hook : reduce_hooks_) {
    hook();
  }
}
92
}  // namespace egr