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

15
#include <cmath>
16
#include <string>
17
#include <vector>
18 19

#include "gtest/gtest.h"
20

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

29
#include "paddle/fluid/jit/function.h"
30
#include "paddle/fluid/jit/function_utils.h"
31 32 33
#include "paddle/fluid/jit/layer.h"
#include "paddle/fluid/jit/serializer.h"

34 35 36 37 38 39 40
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);
41
USE_OP_ITSELF(transfer_layout);
42 43 44 45 46 47 48

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);

49 50 51 52 53 54 55 56
#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

57 58
namespace paddle {
namespace jit {
59
using DenseTensor = phi::DenseTensor;
60

61
std::vector<Tensor> PrepareInputs(const phi::Place& place) {
62
  platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
63
  auto& dev_ctx = *pool.Get(place);
64

65 66 67 68
  DenseTensor t;
  t.Resize(phi::make_ddim({2, 4}));
  t.mutable_data<float>(place);
  phi::funcs::set_constant(dev_ctx, &t, 2.);
69

70
  return utils::ToTensors({t});
71 72
}

73 74 75 76 77
TEST(CpuLayerTest, Function) {
  auto func_null = Function();
  EXPECT_TRUE(!func_null.IsValid());
}

78
TEST(CpuLayerTest, Construct) {
79 80 81
  auto place = phi::CPUPlace();
  std::string path = "./multi_program_load/export";
  auto layer = jit::Load(path, place);
82

83 84 85 86 87 88
  float fbias = layer.Attribute<float>("fbias");
  EXPECT_FLOAT_EQ(fbias, 1.4);

  int ds = layer.Attribute<int>("down_sampling");
  EXPECT_EQ(ds, 4);

89
  std::string fstr = layer.Attribute<framework::String>("fstr");
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  EXPECT_STREQ(fstr.c_str(), "save str property");

  std::vector<int> ints = layer.Attribute<std::vector<int>>("ints");
  EXPECT_EQ(ints[0], 10);
  EXPECT_EQ(ints[1], 20);

  std::vector<float> floats = layer.Attribute<std::vector<float>>("floats");
  EXPECT_FLOAT_EQ(floats[0], 1.1);
  EXPECT_FLOAT_EQ(floats[1], 2.2);

  std::vector<std::string> strs =
      layer.Attribute<std::vector<std::string>>("strs");
  EXPECT_STREQ(strs[0].c_str(), "hello");
  EXPECT_STREQ(strs[1].c_str(), "world");

  // functions
  auto inputs = PrepareInputs(place);
107
  auto outs = layer.forward(inputs);
108
  auto out_data = outs[0].data<float>();
109 110
  EXPECT_NEAR(out_data[0], 0.02194316, 1e-6);

111
  auto func = layer.Function("infer");
112
  EXPECT_TRUE(func.IsValid());
113
  outs = func(inputs);
114
  out_data = outs[0].data<float>();
115
  EXPECT_NEAR(out_data[0], 1.41562390, 1e-6);
116 117 118 119
  auto pow_out =
      paddle::experimental::pow(outs[0], paddle::experimental::Scalar(2));
  out_data = pow_out.data<float>();
  EXPECT_NEAR(out_data[0], pow(1.41562390, 2.0), 1e-6);
120 121
}

122 123
#if defined(PADDLE_WITH_CUDA)
TEST(GpuLayerTest, Construct) {
124
  auto place = phi::GPUPlace();
125

126 127 128
  std::string path = "./multi_program_load/export";
  auto layer = jit::Load(path, place);
  auto inputs = PrepareInputs(place);
129 130

  auto outs = layer.forward(inputs);
131 132 133 134
  auto gpu_tensor = outs[0];
  auto cpu_tensor =
      paddle::experimental::copy_to(gpu_tensor, phi::CPUPlace(), true);
  auto out_data = cpu_tensor.data<float>();
135 136
  EXPECT_NEAR(out_data[0], 0.02194316, 1e-6);

137
  auto func = layer.Function("infer");
138
  EXPECT_TRUE(func.IsValid());
139
  outs = func(inputs);
140 141 142
  gpu_tensor = outs[0];
  cpu_tensor = paddle::experimental::copy_to(gpu_tensor, phi::CPUPlace(), true);
  out_data = cpu_tensor.data<float>();
143
  EXPECT_NEAR(out_data[0], 1.41562390, 1e-6);
144 145 146 147 148

  auto sqrt_out = paddle::experimental::sqrt(outs[0]);
  cpu_tensor = paddle::experimental::copy_to(sqrt_out, phi::CPUPlace(), true);
  out_data = cpu_tensor.data<float>();
  EXPECT_NEAR(out_data[0], sqrt(1.41562390), 1e-6);
149 150 151
}
#endif

152 153
}  // namespace jit
}  // namespace paddle