hook_test_intermidiate.cc 7.8 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 33 34 35 36 37
// Copyright (c) 2022 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 "gtest/gtest.h"

#include "paddle/fluid/eager/api/all.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/imperative/tracer.h"
#include "paddle/pten/core/dense_tensor.h"

#include "paddle/fluid/eager/api/generated/fluid_generated/dygraph_forward_api.h"
#include "paddle/pten/core/kernel_registry.h"

namespace egr {

paddle::experimental::Tensor hook_function(
    const paddle::experimental::Tensor& t) {
  auto t_dense = std::dynamic_pointer_cast<pten::DenseTensor>(t.impl());

  auto ret_meta = pten::DenseTensorMeta(t_dense->dtype(), t_dense->dims(),
                                        t_dense->layout());
  auto place = t_dense->place();
38
  size_t bytes_size = pten::product(t_dense->dims()) * SizeOf(t_dense->dtype());
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  auto ret_dense = std::make_shared<pten::DenseTensor>(
      pten::make_intrusive<paddle::experimental::SharedStorage>(
          paddle::memory::Alloc(place, bytes_size)),
      std::move(ret_meta));

  float* t_ptr = t_dense->mutable_data<float>(place);
  float* ret_ptr = ret_dense->mutable_data<float>(place);
  for (int i = 0; i < ret_dense->numel(); i++) {
    ret_ptr[i] = t_ptr[i] + 3.0;
  }

  auto ret_impl = std::dynamic_pointer_cast<pten::TensorBase>(ret_dense);
  paddle::experimental::Tensor ret = paddle::experimental::Tensor();
  ret.set_impl(ret_impl);

  return ret;
}

TEST(Hook_intermidiate, Sigmoid) {
  // Prepare Device Contexts
  VLOG(6) << "Init Env";
  eager_test::InitEnv(paddle::platform::CPUPlace());

  VLOG(6) << "Make Dim";
63
  paddle::framework::DDim ddim = pten::make_ddim({2, 4, 4, 4});
64 65 66 67 68 69 70 71 72 73 74

  VLOG(6) << "Make paddle::experimental::Tensor";
  paddle::experimental::Tensor tensor = egr_utils_api::CreateTensorWithValue(
      ddim, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 0.0, true);

  VLOG(6) << "Make Hook function";
  std::function<paddle::experimental::Tensor(
      const paddle::experimental::Tensor&)>
      hook = &hook_function;

75 76 77 78 79 80 81 82 83
  VLOG(6) << "Make ReduceHook function";
  auto reduce_hook = [&](void) -> void {
    auto* t_ptr = std::dynamic_pointer_cast<pten::DenseTensor>(tensor.impl())
                      ->data<float>();
    for (int i = 0; i < tensor.numel(); i++) {
      t_ptr[i] = 100.0;  // set to 100.0
    }
  };

84 85 86 87 88 89
  VLOG(6) << "Retain Grad for Tensor";
  egr_utils_api::RetainGradForTensor(tensor);

  VLOG(6) << "Register GradientHook for Tensor";
  egr_utils_api::RegisterGradientHookForTensor(tensor, hook);

90 91 92
  VLOG(6) << "Register ReduceHook for Tensor";
  egr_utils_api::RegisterReduceHookForTensor(tensor, reduce_hook);

93 94 95 96 97 98 99 100 101 102 103 104 105
  VLOG(6) << "Runing Forward";
  auto output_tensor = sigmoid_dygraph_function(tensor, {});
  VLOG(6) << "Finish Forward";

  eager_test::CompareTensorWithValue<float>(output_tensor, 0.5);

  std::vector<paddle::experimental::Tensor> target_tensors = {output_tensor};

  VLOG(6) << "Runing Backward";
  RunBackward(target_tensors, {});
  VLOG(6) << "Finish Backward";

  eager_test::CompareGradTensorWithValue<float>(tensor, 0.25 + 3);
106 107 108 109 110 111 112

  VLOG(6) << "Checking ReduceHook results";
  for (int i = 0; i < tensor.numel(); i++) {
    CHECK_EQ(std::dynamic_pointer_cast<pten::DenseTensor>(tensor.impl())
                 ->data<float>()[i],
             static_cast<float>(100.0f));
  }
113 114 115 116 117 118 119 120 121 122 123
  VLOG(6) << "After Tests";
}

TEST(Hook_intermidiate, 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
124
  paddle::framework::DDim ddimX = pten::make_ddim({4, 16});
125 126 127 128 129
  paddle::experimental::Tensor X = egr_utils_api::CreateTensorWithValue(
      ddimX, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 3.0, true);
  egr_utils_api::RetainGradForTensor(X);

130
  paddle::framework::DDim ddimY = pten::make_ddim({4, 16});
131 132 133 134 135 136 137 138
  paddle::experimental::Tensor Y = egr_utils_api::CreateTensorWithValue(
      ddimY, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 2.0, true);

  std::function<paddle::experimental::Tensor(
      const paddle::experimental::Tensor&)>
      hook = &hook_function;

139 140 141 142 143 144 145 146
  auto reduce_hook = [&](void) -> void {
    auto* t_ptr =
        std::dynamic_pointer_cast<pten::DenseTensor>(Y.impl())->data<float>();
    for (int i = 0; i < Y.numel(); i++) {
      t_ptr[i] = 100.0;  // set to 100.0
    }
  };

147 148
  egr_utils_api::RetainGradForTensor(Y);
  egr_utils_api::RegisterGradientHookForTensor(Y, hook);
149
  egr_utils_api::RegisterReduceHookForTensor(Y, reduce_hook);
150 151 152 153 154 155 156 157 158 159

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

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

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

  eager_test::CompareGradTensorWithValue<float>(X, 1.0);
  eager_test::CompareGradTensorWithValue<float>(Y, 4.0);
160 161 162 163 164 165 166

  // Checking ReduceHook results
  for (int i = 0; i < Y.numel(); i++) {
    CHECK_EQ(std::dynamic_pointer_cast<pten::DenseTensor>(Y.impl())
                 ->data<float>()[i],
             static_cast<float>(100.0f));
  }
167 168 169 170 171 172 173 174 175 176
}

TEST(Hook_intermidiate, Matmul_v2) {
  // Prepare Device Contexts
  eager_test::InitEnv(paddle::platform::CPUPlace());

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

  // 1. Prepare Input
177
  paddle::framework::DDim ddimX = pten::make_ddim({4, 16});
178 179 180 181 182
  paddle::experimental::Tensor X = egr_utils_api::CreateTensorWithValue(
      ddimX, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 3.0, true);
  egr_utils_api::RetainGradForTensor(X);

183
  paddle::framework::DDim ddimY = pten::make_ddim({16, 20});
184 185 186 187 188 189 190 191
  paddle::experimental::Tensor Y = egr_utils_api::CreateTensorWithValue(
      ddimY, paddle::platform::CPUPlace(), pten::DataType::FLOAT32,
      pten::DataLayout::NCHW, 2.0, true);

  std::function<paddle::experimental::Tensor(
      const paddle::experimental::Tensor&)>
      hook = &hook_function;

192 193 194 195 196 197 198 199
  auto reduce_hook = [&](void) -> void {
    auto* t_ptr =
        std::dynamic_pointer_cast<pten::DenseTensor>(Y.impl())->data<float>();
    for (int i = 0; i < Y.numel(); i++) {
      t_ptr[i] = 100.0;  // set to 100.0
    }
  };

200 201
  egr_utils_api::RetainGradForTensor(Y);
  egr_utils_api::RegisterGradientHookForTensor(Y, hook);
202
  egr_utils_api::RegisterReduceHookForTensor(Y, reduce_hook);
203 204 205 206 207 208 209 210 211 212 213 214

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

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

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

  eager_test::CompareGradTensorWithValue<float>(X, 2.0 * 20);
  eager_test::CompareGradTensorWithValue<float>(Y, 3.0 * 4 + 3);

215 216 217 218 219 220 221
  // Checking ReduceHook results
  for (int i = 0; i < Y.numel(); i++) {
    CHECK_EQ(std::dynamic_pointer_cast<pten::DenseTensor>(Y.impl())
                 ->data<float>()[i],
             static_cast<float>(100.0f));
  }
}
222 223 224 225 226
}  // namespace egr

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