generated_test.cc 4.7 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
// 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"
#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/utils.h"

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

#include "paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.h"
31
#include "paddle/phi/core/kernel_registry.h"
32

33 34 35 36 37 38
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);

39
namespace egr {
40 41 42

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

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

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

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

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

  // 1. Prepare Input
74
  paddle::framework::DDim ddimX = phi::make_ddim({4, 16});
75
  paddle::experimental::Tensor X = egr_utils_api::CreateTensorWithValue(
76 77
      ddimX, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 3.0, true);
78
  egr_utils_api::RetainGradForTensor(X);
79

80
  paddle::framework::DDim ddimY = phi::make_ddim({16, 20});
81
  paddle::experimental::Tensor Y = egr_utils_api::CreateTensorWithValue(
82 83
      ddimY, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 2.0, true);
84
  egr_utils_api::RetainGradForTensor(Y);
85 86 87 88

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

89
  eager_test::CompareTensorWithValue<float>(output_tensor, 96);
90

91
  std::vector<paddle::experimental::Tensor> target_tensors = {output_tensor};
92
  RunBackward(target_tensors, {});
93

94 95
  eager_test::CompareGradTensorWithValue<float>(X, 2.0 * 20);
  eager_test::CompareGradTensorWithValue<float>(Y, 3.0 * 4);
96 97
}

98 99 100 101 102 103 104 105
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
106
  paddle::framework::DDim ddimX = phi::make_ddim({4, 16});
107
  paddle::experimental::Tensor X = egr_utils_api::CreateTensorWithValue(
108 109
      ddimX, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 3.0, true);
110 111
  egr_utils_api::RetainGradForTensor(X);

112
  paddle::framework::DDim ddimY = phi::make_ddim({4, 16});
113
  paddle::experimental::Tensor Y = egr_utils_api::CreateTensorWithValue(
114 115
      ddimY, paddle::platform::CPUPlace(), phi::DataType::FLOAT32,
      phi::DataLayout::NCHW, 2.0, true);
116 117 118 119
  egr_utils_api::RetainGradForTensor(Y);

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

120
  eager_test::CompareTensorWithValue<float>(output_tensor, 5);
121

122
  std::vector<paddle::experimental::Tensor> target_tensors = {output_tensor};
123
  RunBackward(target_tensors, {});
124

125 126
  eager_test::CompareGradTensorWithValue<float>(X, 1.0);
  eager_test::CompareGradTensorWithValue<float>(Y, 1.0);
127 128
}

129
}  // namespace egr
130 131

USE_OP(sigmoid);
132
USE_OP_ITSELF(elementwise_add);
133
USE_OP_ITSELF(matmul_v2);