generated_test.cc 4.6 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.

// 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"
#include "paddle/pten/core/kernel_registry.h"

33
namespace egr {
34 35 36

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

52
  std::vector<paddle::experimental::Tensor> target_tensors = {output_tensor};
53 54 55 56
  VLOG(6) << "Runing Backward";
  RunBackward(target_tensors, {});

  VLOG(6) << "Finish Backward";
57
  eager_test::CompareGradTensorWithValue<float>(tensor, 0.25);
58 59 60 61
}

TEST(Generated, Matmul_v2) {
  // Prepare Device Contexts
62
  eager_test::InitEnv(paddle::platform::CPUPlace());
63 64 65 66 67 68

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

  // 1. Prepare Input
  paddle::framework::DDim ddimX = paddle::framework::make_ddim({4, 16});
69
  paddle::experimental::Tensor X = egr_utils_api::CreateTensorWithValue(
70 71
      ddimX, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 3.0, true);
72
  egr_utils_api::RetainGradForTensor(X);
73 74

  paddle::framework::DDim ddimY = paddle::framework::make_ddim({16, 20});
75
  paddle::experimental::Tensor Y = egr_utils_api::CreateTensorWithValue(
76 77
      ddimY, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 2.0, true);
78
  egr_utils_api::RetainGradForTensor(Y);
79 80 81 82

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

83
  eager_test::CompareTensorWithValue<float>(output_tensor, 96);
84

85
  std::vector<paddle::experimental::Tensor> target_tensors = {output_tensor};
86 87
  RunBackward(target_tensors, {});

88 89
  eager_test::CompareGradTensorWithValue<float>(X, 2.0 * 20);
  eager_test::CompareGradTensorWithValue<float>(Y, 3.0 * 4);
90 91
}

92 93 94 95 96 97 98 99 100
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
  paddle::framework::DDim ddimX = paddle::framework::make_ddim({4, 16});
101
  paddle::experimental::Tensor X = egr_utils_api::CreateTensorWithValue(
102 103 104 105 106
      ddimX, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 3.0, true);
  egr_utils_api::RetainGradForTensor(X);

  paddle::framework::DDim ddimY = paddle::framework::make_ddim({4, 16});
107
  paddle::experimental::Tensor Y = egr_utils_api::CreateTensorWithValue(
108 109 110 111 112 113
      ddimY, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 2.0, true);
  egr_utils_api::RetainGradForTensor(Y);

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

114
  eager_test::CompareTensorWithValue<float>(output_tensor, 5);
115

116
  std::vector<paddle::experimental::Tensor> target_tensors = {output_tensor};
117 118
  RunBackward(target_tensors, {});

119 120
  eager_test::CompareGradTensorWithValue<float>(X, 1.0);
  eager_test::CompareGradTensorWithValue<float>(Y, 1.0);
121 122
}

123
}  // namespace egr
124 125

USE_OP(sigmoid);
126
USE_OP_ITSELF(elementwise_add);
127
USE_OP(matmul_v2);