benchmark_eager_cpu.cc 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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

17
#include <paddle/fluid/framework/op_registry.h>
18

19 20 21 22 23 24
#include <chrono>

#include "gtest/gtest.h"
#include "paddle/fluid/eager/api/all.h"
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/backward.h"
25
#include "paddle/fluid/imperative/tracer.h"
26
#include "paddle/phi/core/flags.h"
T
tianshuo78520a 已提交
27 28
#include "test/cpp/eager/performance_tests/benchmark_utils.h"
#include "test/cpp/eager/test_utils.h"
29 30 31 32 33

#ifdef WITH_GPERFTOOLS
#include "gperftools/profiler.h"
#endif

34 35 36 37 38 39 40
#include "paddle/phi/core/kernel_registry.h"

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);
41 42
PD_DECLARE_KERNEL(sum, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(sum_grad, CPU, ALL_LAYOUT);
43

44 45
using namespace egr;            // NOLINT
using namespace egr_utils_api;  // NOLINT
46

47 48
using eager_test::CreateTensorWithValue;

49 50
TEST(Benchmark, EagerScaleCPU) {
  // Prepare Device Contexts
51
  eager_test::InitEnv(paddle::platform::CPUPlace());
52

R
risemeup1 已提交
53
  for (const std::string mode : {"Accuracy", "Performance"}) {
54
    paddle::framework::DDim ddim = phi::make_ddim({2, 4, 4, 4});
55 56 57 58 59 60
    paddle::Tensor tensor = CreateTensorWithValue(ddim,
                                                  paddle::platform::CPUPlace(),
                                                  phi::DataType::FLOAT32,
                                                  phi::DataLayout::NCHW,
                                                  5.0,
                                                  true);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    RetainGradForTensor(tensor);

    if (mode == "Accuracy") {
      benchmark_eager_scale(tensor, true /* accuracy_check*/);

    } else if (mode == "Performance") {
      auto t_start = std::chrono::high_resolution_clock::now();
#ifdef WITH_GPERFTOOLS
      ProfilerStart("eager_scale_cpu.out");
#endif
      benchmark_eager_scale(tensor);

#ifdef WITH_GPERFTOOLS
      ProfilerStop();
#endif
      auto t_end = std::chrono::high_resolution_clock::now();
      double elapsed_time_ms =
          std::chrono::duration<double, std::milli>(t_end - t_start).count();

      std::cout << "Duration: " << elapsed_time_ms << " ms" << std::endl;

    } else {
      PADDLE_THROW(paddle::platform::errors::Fatal("Unknown benchmark mode"));
    }
  }
}

88 89 90 91
TEST(Benchmark, EagerMatmulCPU) {
  // Prepare Device Contexts
  eager_test::InitEnv(paddle::platform::CPUPlace());

R
risemeup1 已提交
92
  for (const std::string mode : {"Accuracy", "Performance"}) {
93
    paddle::framework::DDim ddimX = phi::make_ddim({2, 2});
94 95 96 97 98 99
    paddle::Tensor X = CreateTensorWithValue(ddimX,
                                             paddle::platform::CPUPlace(),
                                             phi::DataType::FLOAT32,
                                             phi::DataLayout::NCHW,
                                             1.0,
                                             true);
100 101 102
    RetainGradForTensor(X);

    paddle::framework::DDim ddimY = phi::make_ddim({2, 2});
103 104 105 106 107 108
    paddle::Tensor Y = CreateTensorWithValue(ddimY,
                                             paddle::platform::CPUPlace(),
                                             phi::DataType::FLOAT32,
                                             phi::DataLayout::NCHW,
                                             2.0,
                                             true);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    RetainGradForTensor(Y);

    if (mode == "Accuracy") {
      benchmark_eager_matmul(X, Y, true /* accuracy_check */);

    } else if (mode == "Performance") {
      auto t_start = std::chrono::high_resolution_clock::now();
#ifdef WITH_GPERFTOOLS
      ProfilerStart("eager_matmul_cpu.out");
#endif
      benchmark_eager_matmul(X, Y);

#ifdef WITH_GPERFTOOLS
      ProfilerStop();
#endif
      auto t_end = std::chrono::high_resolution_clock::now();
      double elapsed_time_ms =
          std::chrono::duration<double, std::milli>(t_end - t_start).count();
      std::cout << "Duration: " << elapsed_time_ms << " ms" << std::endl;

    } else {
      PADDLE_THROW(paddle::platform::errors::Fatal("Unknown benchmark mode"));
    }
  }
}

135 136
TEST(Benchmark, EagerIntermediateMatmulCPU) {
  // Prepare Device Contexts
137
  eager_test::InitEnv(paddle::platform::CPUPlace());
138 139 140 141

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

R
risemeup1 已提交
142
  for (const std::string mode : {"Accuracy", "Performance"}) {
143
    paddle::framework::DDim ddimX = phi::make_ddim({2, 2});
144 145 146 147 148 149
    paddle::Tensor X = CreateTensorWithValue(ddimX,
                                             paddle::platform::CPUPlace(),
                                             phi::DataType::FLOAT32,
                                             phi::DataLayout::NCHW,
                                             1.0,
                                             true);
150 151
    RetainGradForTensor(X);

152
    paddle::framework::DDim ddimY = phi::make_ddim({2, 2});
153 154 155 156 157 158
    paddle::Tensor Y = CreateTensorWithValue(ddimY,
                                             paddle::platform::CPUPlace(),
                                             phi::DataType::FLOAT32,
                                             phi::DataLayout::NCHW,
                                             2.0,
                                             true);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    RetainGradForTensor(Y);

    if (mode == "Accuracy") {
      benchmark_eager_intermediate_matmul(X, Y, true /* accuracy_check */);

    } else if (mode == "Performance") {
      auto t_start = std::chrono::high_resolution_clock::now();
#ifdef WITH_GPERFTOOLS
      ProfilerStart("eager_intermediate_matmul_cpu.out");
#endif
      benchmark_eager_intermediate_matmul(X, Y);

#ifdef WITH_GPERFTOOLS
      ProfilerStop();
#endif
      auto t_end = std::chrono::high_resolution_clock::now();
      double elapsed_time_ms =
          std::chrono::duration<double, std::milli>(t_end - t_start).count();
      std::cout << "Duration: " << elapsed_time_ms << " ms" << std::endl;

    } else {
      PADDLE_THROW(paddle::platform::errors::Fatal("Unknown benchmark mode"));
    }
  }
}

TEST(Benchmark, EagerIntermediateMLPCPU) {
  // Prepare Device Contexts
187
  eager_test::InitEnv(paddle::platform::CPUPlace());
188 189 190 191

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

R
risemeup1 已提交
192
  for (const std::string mode : {"Accuracy", "Performance"}) {
193
    paddle::framework::DDim ddimX = phi::make_ddim({MLP_M, MLP_N});
194 195 196 197 198 199
    paddle::Tensor X = CreateTensorWithValue(ddimX,
                                             paddle::platform::CPUPlace(),
                                             phi::DataType::FLOAT32,
                                             phi::DataLayout::NCHW,
                                             MLP_X_VAL,
                                             true);
200 201
    RetainGradForTensor(X);

202 203
    std::vector<paddle::Tensor> Ws;
    std::vector<paddle::Tensor> Bs;
204
    for (size_t i = 0; i < MLP_NUM_LINEAR; i++) {
205
      paddle::framework::DDim ddimW = phi::make_ddim({MLP_N, MLP_K});
206 207 208 209 210 211
      paddle::Tensor W = CreateTensorWithValue(ddimW,
                                               paddle::platform::CPUPlace(),
                                               phi::DataType::FLOAT32,
                                               phi::DataLayout::NCHW,
                                               MLP_W_VAL,
                                               true);
212 213
      RetainGradForTensor(W);

214
      paddle::framework::DDim ddimB = phi::make_ddim({MLP_K});
215 216 217 218 219 220
      paddle::Tensor B = CreateTensorWithValue(ddimB,
                                               paddle::platform::CPUPlace(),
                                               phi::DataType::FLOAT32,
                                               phi::DataLayout::NCHW,
                                               MLP_B_VAL,
                                               true);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      RetainGradForTensor(B);

      Ws.emplace_back(std::move(W));
      Bs.emplace_back(std::move(B));
    }

    if (mode == "Accuracy") {
      benchmark_eager_intermediate_mlp(X, Ws, Bs, true /* accuracy_check */);

    } else if (mode == "Performance") {
      auto t_start = std::chrono::high_resolution_clock::now();
#ifdef WITH_GPERFTOOLS
      ProfilerStart("eager_intermediate_mlp_cpu.out");
#endif
      benchmark_eager_intermediate_mlp(X, Ws, Bs);

#ifdef WITH_GPERFTOOLS
      ProfilerStop();
#endif
      auto t_end = std::chrono::high_resolution_clock::now();
      double elapsed_time_ms =
          std::chrono::duration<double, std::milli>(t_end - t_start).count();
      std::cout << "Duration: " << elapsed_time_ms << " ms" << std::endl;

    } else {
      PADDLE_THROW(paddle::platform::errors::Fatal("Unknown benchmark mode"));
    }
  }
}
250

251
USE_OP_ITSELF(scale);
252
USE_OP_ITSELF(elementwise_add);
253
USE_OP_ITSELF(matmul_v2);
254
USE_OP_ITSELF(reduce_sum);