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

// Eager Dygraph

#include <chrono>

#include "gtest/gtest.h"
#include "paddle/fluid/eager/api/all.h"
21
#include "paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.h"
22 23
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/backward.h"
24
#include "paddle/fluid/eager/utils.h"
25
#include "paddle/fluid/imperative/tracer.h"
26
#include "paddle/phi/core/kernel_registry.h"
T
tianshuo78520a 已提交
27
#include "test/cpp/eager/test_utils.h"
28

29 30 31 32 33
PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(matmul, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(matmul_grad, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(add, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(add_grad, CPU, ALL_LAYOUT);
34 35
PD_DECLARE_KERNEL(sigmoid, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(sigmoid_grad, CPU, ALL_LAYOUT);
36

37
namespace egr {
38 39 40

TEST(Generated, Sigmoid) {
  // Prepare Device Contexts
41
  eager_test::InitEnv(paddle::platform::CPUPlace());
42 43
  VLOG(6) << "Init Env";
  // 1. Prepare Input
44
  paddle::framework::DDim ddim = phi::make_ddim({2, 4, 4, 4});
45
  VLOG(6) << "Make Dim";
46 47 48 49 50 51 52
  paddle::Tensor tensor =
      eager_test::CreateTensorWithValue(ddim,
                                        paddle::platform::CPUPlace(),
                                        phi::DataType::FLOAT32,
                                        phi::DataLayout::NCHW,
                                        0.0,
                                        true);
53
  VLOG(6) << "Make paddle::Tensor";
54
  egr_utils_api::RetainGradForTensor(tensor);
55 56 57
  VLOG(6) << "Retain Grad for Tensor";
  auto output_tensor = sigmoid_dygraph_function(tensor, {});
  VLOG(6) << "Run Backward";
58
  eager_test::CompareTensorWithValue<float>(output_tensor, 0.5);
59

60
  std::vector<paddle::Tensor> target_tensors = {output_tensor};
61
  VLOG(6) << "Runing Backward";
62
  Backward(target_tensors, {});
63 64

  VLOG(6) << "Finish Backward";
65
  eager_test::CompareGradTensorWithValue<float>(tensor, 0.25);
66 67 68 69
}

TEST(Generated, Matmul_v2) {
  // Prepare Device Contexts
70
  eager_test::InitEnv(paddle::platform::CPUPlace());
71 72 73 74 75

  auto tracer = std::make_shared<paddle::imperative::Tracer>();
  paddle::imperative::SetCurrentTracer(tracer);

  // 1. Prepare Input
76
  paddle::framework::DDim ddimX = phi::make_ddim({4, 16});
77 78 79 80 81 82 83
  paddle::Tensor X =
      eager_test::CreateTensorWithValue(ddimX,
                                        paddle::platform::CPUPlace(),
                                        phi::DataType::FLOAT32,
                                        phi::DataLayout::NCHW,
                                        3.0,
                                        true);
84
  egr_utils_api::RetainGradForTensor(X);
85

86
  paddle::framework::DDim ddimY = phi::make_ddim({16, 20});
87 88 89 90 91 92 93
  paddle::Tensor Y =
      eager_test::CreateTensorWithValue(ddimY,
                                        paddle::platform::CPUPlace(),
                                        phi::DataType::FLOAT32,
                                        phi::DataLayout::NCHW,
                                        2.0,
                                        true);
94
  egr_utils_api::RetainGradForTensor(Y);
95 96 97 98

  auto output_tensor = matmul_v2_dygraph_function(
      X, Y, {{"trans_x", false}, {"trans_y", false}});

99
  eager_test::CompareTensorWithValue<float>(output_tensor, 96);
100

101
  std::vector<paddle::Tensor> target_tensors = {output_tensor};
102
  Backward(target_tensors, {});
103

104 105
  eager_test::CompareGradTensorWithValue<float>(X, 2.0 * 20);
  eager_test::CompareGradTensorWithValue<float>(Y, 3.0 * 4);
106 107
}

108 109 110 111 112 113 114 115
TEST(Generated, ElementwiseAdd) {
  // Prepare Device Contexts
  eager_test::InitEnv(paddle::platform::CPUPlace());

  auto tracer = std::make_shared<paddle::imperative::Tracer>();
  paddle::imperative::SetCurrentTracer(tracer);

  // 1. Prepare Input
116
  paddle::framework::DDim ddimX = phi::make_ddim({4, 16});
117 118 119 120 121 122 123
  paddle::Tensor X =
      eager_test::CreateTensorWithValue(ddimX,
                                        paddle::platform::CPUPlace(),
                                        phi::DataType::FLOAT32,
                                        phi::DataLayout::NCHW,
                                        3.0,
                                        true);
124 125
  egr_utils_api::RetainGradForTensor(X);

126
  paddle::framework::DDim ddimY = phi::make_ddim({4, 16});
127 128 129 130 131 132 133
  paddle::Tensor Y =
      eager_test::CreateTensorWithValue(ddimY,
                                        paddle::platform::CPUPlace(),
                                        phi::DataType::FLOAT32,
                                        phi::DataLayout::NCHW,
                                        2.0,
                                        true);
134 135 136 137
  egr_utils_api::RetainGradForTensor(Y);

  auto output_tensor = elementwise_add_dygraph_function(X, Y, {});

138
  eager_test::CompareTensorWithValue<float>(output_tensor, 5);
139

140
  std::vector<paddle::Tensor> target_tensors = {output_tensor};
141
  Backward(target_tensors, {});
142

143 144
  eager_test::CompareGradTensorWithValue<float>(X, 1.0);
  eager_test::CompareGradTensorWithValue<float>(Y, 1.0);
145 146
}

147
}  // namespace egr
148

Y
YuanRisheng 已提交
149
USE_OP_ITSELF(sigmoid);
150
USE_OP_ITSELF(elementwise_add);
151
USE_OP_ITSELF(matmul_v2);