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

#include "glog/logging.h"
#include "gtest/gtest.h"
#include "paddle/fluid/eager/accumulation/accumulation_node.h"
20
#include "paddle/fluid/eager/api/all.h"
21 22 23 24
#include "paddle/fluid/eager/api/generated/eager_generated/backwards/scale_node.h"
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/backward.h"
#include "paddle/fluid/eager/grad_node_info.h"
25
#include "paddle/fluid/eager/hooks.h"
26
#include "paddle/fluid/eager/tests/test_utils.h"
27
#include "paddle/phi/core/dense_tensor.h"
28
#include "paddle/phi/core/kernel_registry.h"
29
#include "paddle/phi/core/tensor_meta.h"
30 31 32

PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);

33
namespace egr {
34

35 36
paddle::experimental::Tensor hook_function(
    const paddle::experimental::Tensor& t) {
37
  auto t_dense = std::dynamic_pointer_cast<phi::DenseTensor>(t.impl());
38

39 40
  auto ret_meta = phi::DenseTensorMeta(
      t_dense->dtype(), t_dense->dims(), t_dense->layout());
41
  auto place = t_dense->place();
42 43
  size_t bytes_size = phi::product(t_dense->dims()) * SizeOf(t_dense->dtype());
  auto ret_dense = std::make_shared<phi::DenseTensor>(
Z
zyfncg 已提交
44
      paddle::memory::Alloc(place, bytes_size), std::move(ret_meta));
45

46 47
  float* t_ptr = t_dense->mutable_data<float>(place);
  float* ret_ptr = ret_dense->mutable_data<float>(place);
48 49 50 51
  for (int i = 0; i < ret_dense->numel(); i++) {
    ret_ptr[i] = t_ptr[i] + 3.0;
  }

52
  auto ret_impl = std::dynamic_pointer_cast<phi::TensorBase>(ret_dense);
53
  paddle::experimental::Tensor ret = paddle::experimental::Tensor();
54 55 56 57 58 59
  ret.set_impl(ret_impl);

  return ret;
}

TEST(RetainGrad, HookBeforeRetainGrad) {
60
  eager_test::InitEnv(paddle::platform::CPUPlace());
61 62

  // Prepare Inputs
63
  std::vector<paddle::experimental::Tensor> target_tensors;
64
  paddle::framework::DDim ddim = phi::make_ddim({4, 16, 16, 32});
65 66

  // Create Target Tensor
67 68 69 70 71 72 73
  paddle::experimental::Tensor tensor =
      egr_utils_api::CreateTensorWithValue(ddim,
                                           paddle::platform::CPUPlace(),
                                           phi::DataType::FLOAT32,
                                           phi::DataLayout::NCHW,
                                           1.0 /*value*/,
                                           false /*is_leaf*/);
74
  target_tensors.emplace_back(std::move(tensor));
75
  paddle::experimental::Tensor& target_tensor = target_tensors[0];
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  // Create ScaleNode
  auto scale_node_ptr = std::make_shared<GradNodeScale>(1, 1);
  scale_node_ptr->SetAttributes_scale(5.0 /*scale*/);

  // Set grad in/out meta for node0
  scale_node_ptr->SetDefaultGradInOutMeta();

  // Connect Input Tensor and ScaleNode via AutoGradMeta
  // Apply RetainGrad
  {
    // ScaleNode Hook: +3

    auto auto_grad_meta = std::make_shared<AutogradMeta>();
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(scale_node_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
93
    auto_grad_meta->SetStopGradient(false);
94 95 96 97
    target_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
            auto_grad_meta));

98 99
    egr_utils_api::RegisterGradientHookForTensor(
        target_tensor, std::make_shared<egr::CppTensorHook>(hook_function));
100 101
    egr_utils_api::RetainGradForTensor(
        target_tensor);  // result: 1.0 + 3.0 = 4.0
102 103
    egr_utils_api::RetainGradForTensor(
        target_tensor);  // result: 1.0 + 3.0 = 4.0
104 105 106
  }

  // Retain Grad for leaf tensor1
107
  paddle::experimental::Tensor leaf_tensor = paddle::experimental::Tensor();
108 109
  {
    // AccumulationNode Hook: +3
110 111
    auto tmp_tensor0 = paddle::experimental::Tensor();
    auto auto_grad_meta = EagerUtils::autograd_meta(&tmp_tensor0);
112

113
    auto acc_node_ptr = std::make_shared<GradNodeAccumulation>(auto_grad_meta);
114 115 116

    auto_grad_meta->SetStopGradient(false);
    auto_grad_meta->SetGradNode(acc_node_ptr);
117
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
118 119
    std::vector<egr::AutogradMeta*> res = {auto_grad_meta};
    scale_node_ptr->SetGradOutMeta(tmp_tensor0, 0);
120

121 122
    leaf_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
123
            tmp_tensor0.mutable_autograd_meta()));
124

125 126
    egr_utils_api::RegisterGradientHookForTensor(
        leaf_tensor, std::make_shared<egr::CppTensorHook>(hook_function));
127 128
    egr_utils_api::RetainGradForTensor(
        leaf_tensor);  // result: 4.0*5.0 + 3.0 = 23.0
129 130
  }

131
  Backward(target_tensors, {});
132

133 134
  eager_test::CompareGradTensorWithValue<float>(target_tensor, 4.0);
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 23.0);
135 136 137
}

TEST(RetainGrad, HookAfterRetainGrad) {
138
  eager_test::InitEnv(paddle::platform::CPUPlace());
139 140

  // Prepare Inputs
141
  std::vector<paddle::experimental::Tensor> target_tensors;
142
  paddle::framework::DDim ddim = phi::make_ddim({4, 16, 16, 32});
143 144

  // Create Target Tensor
145 146 147 148 149 150 151
  paddle::experimental::Tensor tensor =
      egr_utils_api::CreateTensorWithValue(ddim,
                                           paddle::platform::CPUPlace(),
                                           phi::DataType::FLOAT32,
                                           phi::DataLayout::NCHW,
                                           1.0 /*value*/,
                                           false /*is_leaf*/);
152
  target_tensors.emplace_back(std::move(tensor));
153
  paddle::experimental::Tensor& target_tensor = target_tensors[0];
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  // Create ScaleNode
  auto scale_node_ptr = std::make_shared<GradNodeScale>(1, 1);
  scale_node_ptr->SetAttributes_scale(5.0 /*scale*/);
  // Set grad in/out meta for node0
  scale_node_ptr->SetDefaultGradInOutMeta();

  // Connect Input Tensor and ScaleNode via AutoGradMeta
  // Apply RetainGrad
  {
    // ScaleNode Hook: +3

    auto auto_grad_meta = std::make_shared<AutogradMeta>();
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(scale_node_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
170
    auto_grad_meta->SetStopGradient(false);
171 172 173 174
    target_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
            auto_grad_meta));

175
    egr_utils_api::RetainGradForTensor(target_tensor);  // result: 1.0
176 177
    egr_utils_api::RegisterGradientHookForTensor(
        target_tensor, std::make_shared<egr::CppTensorHook>(hook_function));
178 179 180
  }

  // Retain Grad for leaf tensor1
181
  paddle::experimental::Tensor leaf_tensor = paddle::experimental::Tensor();
182 183
  {
    // AccumulationNode Hook: +3
184 185 186
    auto tmp_tensor0 = paddle::experimental::Tensor();
    auto auto_grad_meta = EagerUtils::autograd_meta(&tmp_tensor0);
    auto acc_node_ptr = std::make_shared<GradNodeAccumulation>(auto_grad_meta);
187 188
    auto_grad_meta->SetGradNode(acc_node_ptr);
    auto_grad_meta->SetStopGradient(false);
189
    scale_node_ptr->SetGradOutMeta(tmp_tensor0, 0);
190

191 192 193
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
    leaf_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
194
            tmp_tensor0.mutable_autograd_meta()));
195

196 197
    egr_utils_api::RegisterGradientHookForTensor(
        leaf_tensor, std::make_shared<egr::CppTensorHook>(hook_function));
198 199
  }

200
  Backward(target_tensors, {});
201 202
  eager_test::CompareGradTensorWithValue<float>(target_tensor, 1.0);
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 23.0);
203
}
204
}  // namespace egr