hook_test.cc 8.1 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 23 24 25 26 27 28 29 30 31 32
// 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"
#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"

#include "paddle/fluid/eager/api/all.h"

#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/tensor_meta.h"

#include "paddle/fluid/eager/tests/test_utils.h"

33
namespace egr {
34

35 36
paddle::experimental::Tensor hook_function(
    const paddle::experimental::Tensor& t) {
37 38 39 40 41 42 43 44 45
  auto t_dense = std::dynamic_pointer_cast<pten::DenseTensor>(t.impl());

  auto ret_meta = pten::DenseTensorMeta(t_dense->dtype(), t_dense->dims(),
                                        t_dense->layout());
  auto place = t_dense->place();
  size_t bytes_size =
      paddle::framework::product(t_dense->dims()) * SizeOf(t_dense->dtype());
  auto ret_dense = std::make_shared<pten::DenseTensor>(
      pten::make_intrusive<paddle::experimental::SharedStorage>(
46
          paddle::memory::Alloc(place, bytes_size)),
47 48
      std::move(ret_meta));

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

  auto ret_impl = std::dynamic_pointer_cast<pten::TensorBase>(ret_dense);
56
  paddle::experimental::Tensor ret = paddle::experimental::Tensor();
57 58 59 60 61 62
  ret.set_impl(ret_impl);

  return ret;
}

TEST(RetainGrad, HookBeforeRetainGrad) {
63
  eager_test::InitEnv(paddle::platform::CPUPlace());
64 65

  // Prepare Inputs
66
  std::vector<paddle::experimental::Tensor> target_tensors;
67 68 69
  paddle::framework::DDim ddim = paddle::framework::make_ddim({4, 16, 16, 32});

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

  // 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();

  // Create AccumulationNode
  auto acc_node_ptr = std::make_shared<GradNodeAccumulation>();

  // Connect Input Tensor and ScaleNode via AutoGradMeta
  // Apply RetainGrad
  {
    // ScaleNode Hook: +3
90 91 92
    std::function<paddle::experimental::Tensor(
        const paddle::experimental::Tensor&)>
        hook = &hook_function;
93 94 95 96 97

    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);
98
    auto_grad_meta->SetStopGradient(false);
99 100 101 102
    target_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
            auto_grad_meta));

103 104 105
    egr_utils_api::RegisterGradientHookForTensor(target_tensor, hook);
    egr_utils_api::RetainGradForTensor(
        target_tensor);  // result: 1.0 + 3.0 = 4.0
106 107 108 109 110
  }

  // Connect ScaleNode -> AccumulationNode via Edge
  {
    auto meta = AutogradMeta();
111
    meta.SetStopGradient(false);
112 113
    meta.SetSingleOutRankWithSlot(0, 0);
    meta.SetGradNode(acc_node_ptr);
114 115
    std::vector<egr::AutogradMeta*> res = {&meta};
    scale_node_ptr->AddEdges(&res, 0);
116 117 118
  }

  // Retain Grad for leaf tensor1
119
  paddle::experimental::Tensor leaf_tensor = paddle::experimental::Tensor();
120 121
  {
    // AccumulationNode Hook: +3
122 123 124
    std::function<paddle::experimental::Tensor(
        const paddle::experimental::Tensor&)>
        hook = &hook_function;
125 126 127 128 129 130 131 132 133

    auto auto_grad_meta = std::make_shared<AutogradMeta>();
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
    leaf_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
            auto_grad_meta));

134 135 136
    egr_utils_api::RegisterGradientHookForTensor(leaf_tensor, hook);
    egr_utils_api::RetainGradForTensor(
        leaf_tensor);  // result: 4.0*5.0 + 3.0 = 23.0
137 138 139 140
  }

  RunBackward(target_tensors, {});

141 142
  eager_test::CompareGradTensorWithValue<float>(target_tensor, 4.0);
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 23.0);
143 144 145
}

TEST(RetainGrad, HookAfterRetainGrad) {
146
  eager_test::InitEnv(paddle::platform::CPUPlace());
147 148

  // Prepare Inputs
149
  std::vector<paddle::experimental::Tensor> target_tensors;
150 151 152
  paddle::framework::DDim ddim = paddle::framework::make_ddim({4, 16, 16, 32});

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

  // 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();
  // Create AccumulationNode
  auto acc_node_ptr = std::make_shared<GradNodeAccumulation>();

  // Connect Input Tensor and ScaleNode via AutoGradMeta
  // Apply RetainGrad
  {
    // ScaleNode Hook: +3
171 172 173
    std::function<paddle::experimental::Tensor(
        const paddle::experimental::Tensor&)>
        hook = &hook_function;
174 175 176 177 178

    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);
179
    auto_grad_meta->SetStopGradient(false);
180 181 182 183
    target_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
            auto_grad_meta));

184 185
    egr_utils_api::RetainGradForTensor(target_tensor);  // result: 1.0
    egr_utils_api::RegisterGradientHookForTensor(target_tensor, hook);
186 187 188 189 190
  }

  // Connect ScaleNode -> AccumulationNode via Edge
  {
    auto meta = AutogradMeta();
191
    meta.SetStopGradient(false);
192 193
    meta.SetSingleOutRankWithSlot(0, 0);
    meta.SetGradNode(acc_node_ptr);
194 195
    std::vector<egr::AutogradMeta*> res = {&meta};
    scale_node_ptr->AddEdges(&res, 0);
196 197 198
  }

  // Retain Grad for leaf tensor1
199
  paddle::experimental::Tensor leaf_tensor = paddle::experimental::Tensor();
200 201
  {
    // AccumulationNode Hook: +3
202 203 204
    std::function<paddle::experimental::Tensor(
        const paddle::experimental::Tensor&)>
        hook = &hook_function;
205 206 207 208 209 210 211 212 213

    auto auto_grad_meta = std::make_shared<AutogradMeta>();
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
    leaf_tensor.set_autograd_meta(
        std::dynamic_pointer_cast<paddle::experimental::AbstractAutogradMeta>(
            auto_grad_meta));

214 215 216 217 218
    egr_utils_api::RetainGradForTensor(
        leaf_tensor);  // RetainGrad for leaf tensor gets
                       // postponed, result: 4.0*5.0 + 3.0 =
                       // 23.0
    egr_utils_api::RegisterGradientHookForTensor(leaf_tensor, hook);
219 220 221
  }

  RunBackward(target_tensors, {});
222 223
  eager_test::CompareGradTensorWithValue<float>(target_tensor, 1.0);
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 23.0);
224
}
225
}  // namespace egr