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

#include "gtest/gtest.h"
18

19 20 21 22
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_registry.h"
23
#include "paddle/phi/core/tensor_utils.h"
24 25
#include "paddle/phi/kernels/funcs/math_function.h"

26 27 28
#include "paddle/fluid/jit/layer.h"
#include "paddle/fluid/jit/serializer.h"

29 30 31 32 33 34 35 36 37 38 39 40 41 42
USE_OP_ITSELF(elementwise_add);
USE_OP_ITSELF(matmul_v2);
USE_OP_ITSELF(relu);
USE_OP_ITSELF(reduce_mean);
USE_OP_ITSELF(feed);
USE_OP_ITSELF(fetch);
USE_OP_ITSELF(scale);

PD_DECLARE_KERNEL(add, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(matmul, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(relu, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(mean, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(scale, CPU, ALL_LAYOUT);

43 44 45 46 47 48 49 50
#if defined(PADDLE_WITH_CUDA)
PD_DECLARE_KERNEL(add, KPS, ALL_LAYOUT);
PD_DECLARE_KERNEL(matmul, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(relu, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(mean, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(scale, GPU, ALL_LAYOUT);
#endif

51 52
namespace paddle {
namespace jit {
53
using DenseTensor = phi::DenseTensor;
54

55
std::vector<DenseTensor> PrepareInputs(const phi::Place& place) {
56
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
57
  auto& dev_ctx = *pool.Get(place);
58

59 60 61 62
  DenseTensor t;
  t.Resize(phi::make_ddim({2, 4}));
  t.mutable_data<float>(place);
  phi::funcs::set_constant(dev_ctx, &t, 2.);
63

64
  return {t};
65 66
}

67
TEST(CpuLayerTest, Construct) {
68 69 70 71
  auto place = phi::CPUPlace();
  std::string path = "./multi_program_load/export";
  auto layer = jit::Load(path, place);
  auto inputs = PrepareInputs(place);
72 73

  auto outs = layer.forward(inputs);
74
  auto out_data = outs[0].data<float>();
75 76
  EXPECT_NEAR(out_data[0], 0.02194316, 1e-6);

77
  auto func = layer.Function("infer");
78
  outs = (*func)(inputs);
79
  out_data = outs[0].data<float>();
80 81 82
  EXPECT_NEAR(out_data[0], 1.41562390, 1e-6);
}

83 84
#if defined(PADDLE_WITH_CUDA)
TEST(GpuLayerTest, Construct) {
85
  auto place = phi::GPUPlace();
86
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
87
  auto& dev_ctx = *pool.Get(place);
88 89 90
  const auto* dev_ctx_gpu = static_cast<const phi::GPUContext*>(&dev_ctx);
  DenseTensor cpu_dense_tensor;

91 92 93
  std::string path = "./multi_program_load/export";
  auto layer = jit::Load(path, place);
  auto inputs = PrepareInputs(place);
94 95

  auto outs = layer.forward(inputs);
96
  auto out_dense_tensor = outs[0];
97 98 99 100 101
  phi::Copy(
      *dev_ctx_gpu, out_dense_tensor, phi::CPUPlace(), true, &cpu_dense_tensor);
  auto out_data = cpu_dense_tensor.data<float>();
  EXPECT_NEAR(out_data[0], 0.02194316, 1e-6);

102
  auto func = layer.Function("infer");
103
  outs = (*func)(inputs);
104
  out_dense_tensor = outs[0];
105 106 107 108 109 110 111
  phi::Copy(
      *dev_ctx_gpu, out_dense_tensor, phi::CPUPlace(), true, &cpu_dense_tensor);
  out_data = cpu_dense_tensor.data<float>();
  EXPECT_NEAR(out_data[0], 1.41562390, 1e-6);
}
#endif

112 113
}  // namespace jit
}  // namespace paddle