backward_test.cc 11.5 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/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"

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

33
namespace egr {
34 35 36

TEST(Backward, SingleNodeEmptyGrad) {
  // Prepare Device Contexts
37
  eager_test::InitEnv(paddle::platform::CPUPlace());
38 39 40 41 42

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

  // Create Target Tensor
43
  egr::EagerTensor target_tensor = egr_utils_api::CreateTensorWithValue(
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);

  egr::EagerTensor leaf_tensor;
  {
    // 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);

    // Connect Tensor and AccumulationNode via AutoGradMeta
    auto acc_node_ptr = std::make_shared<egr::GradNodeAccumulation>();

    AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor);
    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);

68
    egr_utils_api::RetainGradForTensor(leaf_tensor);
69 70 71

    // Connect Node0 -> AccumulationNode via Edge
    auto meta = egr::AutogradMeta();
72
    meta.SetStopGradient(false);
73 74
    meta.SetSingleOutRankWithSlot(0, 0);
    meta.SetGradNode(acc_node_ptr);
75 76
    std::vector<egr::AutogradMeta*> res = {&meta};
    node0_ptr->AddEdges(&res, 0);
77 78 79 80 81 82
  }
  std::vector<egr::EagerTensor> outs = {target_tensor};
  // Run Backward
  RunBackward(outs, {});

  // Check Output Value
83
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 5.0);
84 85 86 87
}

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

  // Prepare Inputs
  std::vector<egr::EagerTensor> target_tensors;
  paddle::framework::DDim ddim = paddle::framework::make_ddim({4, 16, 16, 32});

  // Create Target Tensor
95
  egr::EagerTensor tensor = egr_utils_api::CreateTensorWithValue(
96 97 98 99 100 101
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
  target_tensors.emplace_back(std::move(tensor));

  std::vector<egr::EagerTensor> grad_tensors;
  // Create Grad Tensor
102
  egr::EagerTensor grad_tensor = egr_utils_api::CreateTensorWithValue(
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 10.0 /*value*/, false /*is_leaf*/);
  grad_tensors.emplace_back(std::move(grad_tensor));

  egr::EagerTensor leaf_tensor;
  {
    // 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);

    // Connect Tensor and AccumulationNode via AutoGradMeta
    auto acc_node_ptr = std::make_shared<egr::GradNodeAccumulation>();

    AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor);
    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);

131
    egr_utils_api::RetainGradForTensor(leaf_tensor);
132 133 134

    // Connect Node0 -> AccumulationNode via Edge
    auto meta = egr::AutogradMeta();
135
    meta.SetStopGradient(false);
136 137
    meta.SetSingleOutRankWithSlot(0, 0);
    meta.SetGradNode(acc_node_ptr);
138 139
    std::vector<egr::AutogradMeta*> res = {&meta};
    node0_ptr->AddEdges(&res, 0);
140 141 142 143 144 145
  }

  // Run Backward
  RunBackward(target_tensors, grad_tensors);

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

/*
Node1
  |
Node0
  |
 inp0
*/
TEST(Backward, LinearNodes) {
  // Prepare Device Contexts
158
  eager_test::InitEnv(paddle::platform::CPUPlace());
159 160 161 162 163 164

  // Prepare Inputs
  std::vector<egr::EagerTensor> target_tensors;
  paddle::framework::DDim ddim = paddle::framework::make_ddim({4, 16, 16, 32});

  // Create Target Tensor
165
  egr::EagerTensor tensor = egr_utils_api::CreateTensorWithValue(
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
  target_tensors.emplace_back(std::move(tensor));

  egr::EagerTensor leaf_tensor;
  {
    // 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);

    // Connect Node0 -> Node1 via Edge
    auto meta0 = egr::AutogradMeta();
195
    meta0.SetStopGradient(false);
196 197
    meta0.SetSingleOutRankWithSlot(0, 0);
    meta0.SetGradNode(node1_ptr);
198 199
    std::vector<egr::AutogradMeta*> res0 = {&meta0};
    node0_ptr->AddEdges(&res0, 0);
200 201 202 203 204 205 206 207 208

    // Connect Tensor and AccumulationNode via AutoGradMeta
    auto acc_node_ptr = std::make_shared<egr::GradNodeAccumulation>();

    AutogradMeta* auto_grad_meta1 = EagerUtils::autograd_meta(&leaf_tensor);
    auto_grad_meta1->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta1->SetSingleOutRankWithSlot(0, 0);

209
    egr_utils_api::RetainGradForTensor(leaf_tensor);
210 211 212

    // Connect Node1 -> AccumulationNode via Edge
    auto meta1 = egr::AutogradMeta();
213
    meta1.SetStopGradient(false);
214 215
    meta1.SetSingleOutRankWithSlot(0, 0);
    meta1.SetGradNode(acc_node_ptr);
216 217
    std::vector<egr::AutogradMeta*> res1 = {&meta1};
    node1_ptr->AddEdges(&res1, 0);
218 219 220 221 222 223
  }

  // Use Empty Grad Tensor
  RunBackward(target_tensors, {});

  // Check Output Value
224
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 50.0);
225 226 227 228 229 230 231 232 233 234 235
}

/*
    Node2
    |   |
Node0   Node1
  |      |
 inp0   inp1
*/
TEST(Backward, WithAccumulation) {
  // Prepare Device Contexts
236
  eager_test::InitEnv(paddle::platform::CPUPlace());
237 238 239 240 241 242

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

  // Create Target Tensor
  std::vector<egr::EagerTensor> target_tensors;
243
  egr::EagerTensor tensor0 = egr_utils_api::CreateTensorWithValue(
244 245
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
246
  egr::EagerTensor tensor1 = egr_utils_api::CreateTensorWithValue(
247 248 249 250 251 252 253
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 1.0 /*value*/, false /*is_leaf*/);
  target_tensors.emplace_back(std::move(tensor0));
  target_tensors.emplace_back(std::move(tensor1));

  // Create Grad Tensor
  std::vector<egr::EagerTensor> grad_tensors;
254
  egr::EagerTensor grad_tensor0 = egr_utils_api::CreateTensorWithValue(
255 256
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 5.0 /*value*/, false /*is_leaf*/);
257
  egr::EagerTensor grad_tensor1 = egr_utils_api::CreateTensorWithValue(
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 10.0 /*value*/, false /*is_leaf*/);
  grad_tensors.emplace_back(std::move(grad_tensor0));
  grad_tensors.emplace_back(std::move(grad_tensor1));

  egr::EagerTensor leaf_tensor;
  {
    // 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);

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

    // Connect Node0 -> Node2 via Edge
    auto meta0 = egr::AutogradMeta();
294
    meta0.SetStopGradient(false);
295 296
    meta0.SetSingleOutRankWithSlot(0, 0);
    meta0.SetGradNode(node2_ptr);
297 298
    std::vector<egr::AutogradMeta*> res0 = {&meta0};
    node0_ptr->AddEdges(&res0, 0);
299 300 301

    // Connect Node1 -> Node2 via Edge
    auto meta1 = egr::AutogradMeta();
302
    meta1.SetStopGradient(false);
303 304
    meta1.SetSingleOutRankWithSlot(0, 0);
    meta1.SetGradNode(node2_ptr);
305 306
    std::vector<egr::AutogradMeta*> res1 = {&meta1};
    node1_ptr->AddEdges(&res1, 0);
307 308 309 310 311 312 313 314 315

    // Connect Tensor and AccumulationNode via AutoGradMeta
    auto acc_node_ptr = std::make_shared<egr::GradNodeAccumulation>();

    AutogradMeta* auto_grad_meta2 = EagerUtils::autograd_meta(&leaf_tensor);
    auto_grad_meta2->SetGradNode(
        std::dynamic_pointer_cast<GradNodeBase>(acc_node_ptr));
    auto_grad_meta2->SetSingleOutRankWithSlot(0, 0);

316
    egr_utils_api::RetainGradForTensor(leaf_tensor);
317 318 319

    // Connect Node2 -> AccumulationNode via Edge
    auto meta2 = egr::AutogradMeta();
320
    meta2.SetStopGradient(false);
321 322
    meta2.SetSingleOutRankWithSlot(0, 0);
    meta2.SetGradNode(acc_node_ptr);
323 324
    std::vector<egr::AutogradMeta*> res2 = {&meta2};
    node2_ptr->AddEdges(&res2, 0);
325 326 327 328
  }

  RunBackward(target_tensors, grad_tensors);

329
  eager_test::CompareGradTensorWithValue<float>(leaf_tensor, 2500.0);
330 331
}

332
}  // namespace egr