backward_test.cc 11.3 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
// 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/api/utils/tensor_utils.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/tests/test_utils.h"

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

30 31
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/tensor_meta.h"
32

33 34 35
#include "paddle/phi/core/kernel_registry.h"

PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);
36
PD_DECLARE_KERNEL(copy, CPU, ALL_LAYOUT);
37
PD_DECLARE_KERNEL(add, CPU, ALL_LAYOUT);
38

39
namespace egr {
40 41 42

TEST(Backward, SingleNodeEmptyGrad) {
  // Prepare Device Contexts
43
  eager_test::InitEnv(paddle::platform::CPUPlace());
44 45

  // Prepare Inputs
46
  paddle::framework::DDim ddim = phi::make_ddim({4, 16, 16, 32});
47 48

  // Create Target Tensor
49 50
  paddle::experimental::Tensor target_tensor =
      egr_utils_api::CreateTensorWithValue(
51 52
          ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
          phi::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
53

54
  paddle::experimental::Tensor leaf_tensor;
55 56 57 58 59 60 61 62 63 64 65
  {
    // Create Scale Node
    auto node0_ptr = std::make_shared<GradNodeScale>(1, 1);
    node0_ptr->SetAttributes_scale(5.0 /*scale*/);

    // Set grad in/out meta
    node0_ptr->SetDefaultGradInOutMeta();
    AutogradMeta* auto_grad_meta = EagerUtils::autograd_meta(&target_tensor);
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(node0_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
66
    auto_grad_meta->SetStopGradient(false);
67

68 69
    AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor);

70
    // Connect Tensor and AccumulationNode via AutoGradMeta
71 72
    auto acc_node_ptr =
        std::make_shared<egr::GradNodeAccumulation>(auto_grad_meta1);
73 74 75 76

    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);
77
    auto_grad_meta1->SetStopGradient(false);
78

79
    node0_ptr->SetGradOutMeta({leaf_tensor}, 0);
80
  }
81
  std::vector<paddle::experimental::Tensor> outs = {target_tensor};
82
  // Run Backward
83
  Backward(outs, {});
84 85

  // Check Output Value
86
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 5.0);
87 88 89 90
}

TEST(Backward, SingleNodeCustomGrad) {
  // Prepare Device Contexts
91
  eager_test::InitEnv(paddle::platform::CPUPlace());
92 93

  // Prepare Inputs
94
  std::vector<paddle::experimental::Tensor> target_tensors;
95
  paddle::framework::DDim ddim = phi::make_ddim({4, 16, 16, 32});
96 97

  // Create Target Tensor
98
  paddle::experimental::Tensor tensor = egr_utils_api::CreateTensorWithValue(
99 100
      ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
101 102
  target_tensors.emplace_back(std::move(tensor));

103
  std::vector<paddle::experimental::Tensor> grad_tensors;
104
  // Create Grad Tensor
105 106
  paddle::experimental::Tensor grad_tensor =
      egr_utils_api::CreateTensorWithValue(
107 108
          ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
          phi::DataLayout::NCHW, 10.0 /*value*/, false /*is_leaf*/);
109 110
  grad_tensors.emplace_back(std::move(grad_tensor));

111
  paddle::experimental::Tensor leaf_tensor;
112 113 114 115 116 117 118 119 120 121 122 123 124 125
  {
    // Create Scale Node
    auto node0_ptr = std::make_shared<GradNodeScale>(1, 1);
    node0_ptr->SetAttributes_scale(5.0 /*scale*/);

    // Set grad in/out meta
    node0_ptr->SetDefaultGradInOutMeta();

    // Connect Tensor and Node via AutoGradMeta
    AutogradMeta* auto_grad_meta =
        EagerUtils::autograd_meta(&(target_tensors[0]));
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(node0_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
126
    auto_grad_meta->SetStopGradient(false);
127 128

    AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor);
129 130 131 132
    // Connect Tensor and AccumulationNode via AutoGradMeta
    auto acc_node_ptr =
        std::make_shared<egr::GradNodeAccumulation>(auto_grad_meta1);

133 134 135
    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);
136
    auto_grad_meta1->SetStopGradient(false);
137
    node0_ptr->SetGradOutMeta({leaf_tensor}, 0);
138 139 140
  }

  // Run Backward
141
  Backward(target_tensors, grad_tensors);
142 143

  // Check Output Value
144
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 50.0);
145 146 147 148 149 150 151 152 153 154 155
}

/*
Node1
  |
Node0
  |
 inp0
*/
TEST(Backward, LinearNodes) {
  // Prepare Device Contexts
156
  eager_test::InitEnv(paddle::platform::CPUPlace());
157 158

  // Prepare Inputs
159
  std::vector<paddle::experimental::Tensor> target_tensors;
160
  paddle::framework::DDim ddim = phi::make_ddim({4, 16, 16, 32});
161 162

  // Create Target Tensor
163
  paddle::experimental::Tensor tensor = egr_utils_api::CreateTensorWithValue(
164 165
      ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
166 167
  target_tensors.emplace_back(std::move(tensor));

168
  paddle::experimental::Tensor leaf_tensor;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  {
    // Create Node0
    auto node0_ptr = std::make_shared<GradNodeScale>(1, 1);
    node0_ptr->SetAttributes_scale(5.0 /*scale*/);

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

    // Create Node1
    auto node1_ptr = std::make_shared<GradNodeScale>(1, 1);
    node1_ptr->SetAttributes_scale(10.0 /*scale*/);

    // Set grad in/out meta for node1
    node1_ptr->SetDefaultGradInOutMeta();

    // Connect Input Tensor and Node0 via AutoGradMeta
    AutogradMeta* auto_grad_meta =
        EagerUtils::autograd_meta(&(target_tensors[0]));
    auto_grad_meta->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(node0_ptr));
    auto_grad_meta->SetSingleOutRankWithSlot(0, 0);
190
    auto_grad_meta->SetStopGradient(false);
191
    // Connect Node0 -> Node1 via Edge
192 193 194 195 196 197
    auto tmp_tensor = paddle::experimental::Tensor();
    auto* meta0 = EagerUtils::autograd_meta(&tmp_tensor);
    meta0->SetStopGradient(false);
    meta0->SetSingleOutRankWithSlot(0, 0);
    meta0->SetGradNode(node1_ptr);
    node0_ptr->SetGradOutMeta(tmp_tensor, 0);
198

199
    AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor);
200
    // Connect Tensor and AccumulationNode via AutoGradMeta
201 202
    auto acc_node_ptr =
        std::make_shared<egr::GradNodeAccumulation>(auto_grad_meta1);
203 204 205 206 207

    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);

208
    auto_grad_meta1->SetStopGradient(false);
209
    node1_ptr->SetGradOutMeta(leaf_tensor, 0);
210 211 212
  }

  // Use Empty Grad Tensor
213
  Backward(target_tensors, {});
214 215

  // Check Output Value
216
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 50.0);
217 218 219 220 221 222 223 224 225 226 227
}

/*
    Node2
    |   |
Node0   Node1
  |      |
 inp0   inp1
*/
TEST(Backward, WithAccumulation) {
  // Prepare Device Contexts
228
  eager_test::InitEnv(paddle::platform::CPUPlace());
229 230

  // Prepare Inputs
231
  paddle::framework::DDim ddim = phi::make_ddim({4, 16, 16, 32});
232 233

  // Create Target Tensor
234 235
  std::vector<paddle::experimental::Tensor> target_tensors;
  paddle::experimental::Tensor tensor0 = egr_utils_api::CreateTensorWithValue(
236 237
      ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
238
  paddle::experimental::Tensor tensor1 = egr_utils_api::CreateTensorWithValue(
239 240
      ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
241 242 243 244
  target_tensors.emplace_back(std::move(tensor0));
  target_tensors.emplace_back(std::move(tensor1));

  // Create Grad Tensor
245 246 247
  std::vector<paddle::experimental::Tensor> grad_tensors;
  paddle::experimental::Tensor grad_tensor0 =
      egr_utils_api::CreateTensorWithValue(
248 249
          ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
          phi::DataLayout::NCHW, 5.0 /*value*/, false /*is_leaf*/);
250 251
  paddle::experimental::Tensor grad_tensor1 =
      egr_utils_api::CreateTensorWithValue(
252 253
          ddim, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
          phi::DataLayout::NCHW, 10.0 /*value*/, false /*is_leaf*/);
254 255 256
  grad_tensors.emplace_back(std::move(grad_tensor0));
  grad_tensors.emplace_back(std::move(grad_tensor1));

257
  paddle::experimental::Tensor leaf_tensor;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  {
    // Create Node0
    auto node0_ptr = std::make_shared<GradNodeScale>(1, 1);
    node0_ptr->SetAttributes_scale(5.0 /*scale*/);
    node0_ptr->SetDefaultGradInOutMeta();

    // Create Node1
    auto node1_ptr = std::make_shared<GradNodeScale>(1, 1);
    node1_ptr->SetAttributes_scale(10.0 /*scale*/);
    node1_ptr->SetDefaultGradInOutMeta();
    // Create Node2
    auto node2_ptr = std::make_shared<GradNodeScale>(1, 1);
    node2_ptr->SetAttributes_scale(20.0 /*scale*/);
    node2_ptr->SetDefaultGradInOutMeta();
    // Connect Inp0 and Node0 via AutoGradMeta
    AutogradMeta* auto_grad_meta0 =
        EagerUtils::autograd_meta(&(target_tensors[0]));
    auto_grad_meta0->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(node0_ptr));
    auto_grad_meta0->SetSingleOutRankWithSlot(0, 0);
278
    auto_grad_meta0->SetStopGradient(false);
279 280 281 282 283 284
    // Connect Inp1 and Node1 via AutoGradMeta
    AutogradMeta* auto_grad_meta1 =
        EagerUtils::autograd_meta(&(target_tensors[1]));
    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(node1_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);
285
    auto_grad_meta1->SetStopGradient(false);
286 287

    // Connect Node0 -> Node2 via Edge
288 289 290 291 292 293
    auto tmp_tensor0 = paddle::experimental::Tensor();
    auto* meta0 = EagerUtils::autograd_meta(&tmp_tensor0);
    meta0->SetStopGradient(false);
    meta0->SetSingleOutRankWithSlot(0, 0);
    meta0->SetGradNode(node2_ptr);
    node0_ptr->SetGradOutMeta(tmp_tensor0, 0);
294 295

    // Connect Node1 -> Node2 via Edge
296 297 298 299 300 301
    auto tmp_tensor1 = paddle::experimental::Tensor();
    auto* meta1 = EagerUtils::autograd_meta(&tmp_tensor1);
    meta1->SetStopGradient(false);
    meta1->SetSingleOutRankWithSlot(0, 0);
    meta1->SetGradNode(node2_ptr);
    node1_ptr->SetGradOutMeta(tmp_tensor1, 0);
302

303
    AutogradMeta* auto_grad_meta2 = EagerUtils::autograd_meta(&leaf_tensor);
304
    // Connect Tensor and AccumulationNode via AutoGradMeta
305 306
    auto acc_node_ptr =
        std::make_shared<egr::GradNodeAccumulation>(auto_grad_meta2);
307 308 309 310 311

    auto_grad_meta2->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta2->SetSingleOutRankWithSlot(0, 0);

312 313
    auto_grad_meta2->SetStopGradient(false);
    std::vector<egr::AutogradMeta*> res2 = {auto_grad_meta2};
314
    node2_ptr->SetGradOutMeta(leaf_tensor, 0);
315 316
  }

317
  Backward(target_tensors, grad_tensors);
318

319
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 2500.0);
320 321
}

322
}  // namespace egr