lite_engine_op_test.cc 4.7 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
/* Copyright (c) 2019 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 <gtest/gtest.h>

#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/inference/utils/singleton.h"
#include "paddle/fluid/operators/lite/lite_engine_op.h"
#include "paddle/fluid/operators/lite/ut_helper.h"

USE_NO_KERNEL_OP(lite_engine)

using paddle::inference::lite::AddTensorToBlockDesc;
W
Wilber 已提交
28
using paddle::inference::lite::AddFetchListToBlockDesc;
石晓伟 已提交
29 30 31 32
using paddle::inference::lite::CreateTensor;
using paddle::inference::lite::serialize_params;
namespace paddle {
namespace operators {
33 34

#if defined(PADDLE_WITH_CUDA)
石晓伟 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
TEST(LiteEngineOp, engine_op) {
  framework::ProgramDesc program;
  auto* block_ = program.Proto()->mutable_blocks(0);
  framework::BlockDesc block_desc(&program, block_);
  auto* feed0 = block_desc.AppendOp();
  feed0->SetType("feed");
  feed0->SetInput("X", {"feed"});
  feed0->SetOutput("Out", {"x"});
  feed0->SetAttr("col", 0);
  auto* feed1 = block_desc.AppendOp();
  feed1->SetType("feed");
  feed1->SetInput("X", {"feed"});
  feed1->SetOutput("Out", {"y"});
  feed1->SetAttr("col", 1);
  LOG(INFO) << "create elementwise_add op";
  auto* elt_add = block_desc.AppendOp();
  elt_add->SetType("elementwise_add");
  elt_add->SetInput("X", std::vector<std::string>({"x"}));
  elt_add->SetInput("Y", std::vector<std::string>({"y"}));
  elt_add->SetOutput("Out", std::vector<std::string>({"z"}));
  elt_add->SetAttr("axis", -1);
  LOG(INFO) << "create fetch op";
  auto* fetch = block_desc.AppendOp();
  fetch->SetType("fetch");
  fetch->SetInput("X", std::vector<std::string>({"z"}));
  fetch->SetOutput("Out", std::vector<std::string>({"out"}));
  fetch->SetAttr("col", 0);
  // Set inputs' variable shape in BlockDesc
  AddTensorToBlockDesc(block_, "x", std::vector<int64_t>({2, 4}), true);
  AddTensorToBlockDesc(block_, "y", std::vector<int64_t>({2, 4}), true);
  AddTensorToBlockDesc(block_, "z", std::vector<int64_t>({2, 4}), false);
W
Wilber 已提交
66
  AddFetchListToBlockDesc(block_, "out");
石晓伟 已提交
67 68 69 70 71
  *block_->add_ops() = *feed1->Proto();
  *block_->add_ops() = *feed0->Proto();
  *block_->add_ops() = *elt_add->Proto();
  *block_->add_ops() = *fetch->Proto();
  framework::Scope scope;
72
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
石晓伟 已提交
73 74 75 76 77 78 79
  platform::CUDAPlace place;
  platform::CUDADeviceContext ctx(place);
#else
  platform::CPUPlace place;
  platform::CPUDeviceContext ctx(place);
#endif
  // Prepare variables.
80 81
  CreateTensor(&scope, "x", std::vector<int64_t>({2, 4}), true);
  CreateTensor(&scope, "y", std::vector<int64_t>({2, 4}), true);
石晓伟 已提交
82 83 84 85 86 87 88
  CreateTensor(&scope, "out", std::vector<int64_t>({2, 4}), false);

  ASSERT_EQ(block_->ops_size(), 4);

  std::vector<std::string> repetitive_params{"x", "y"};
  inference::lite::EngineConfig config;
  config.valid_places = {
89 90
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    paddle::lite_api::Place({TARGET(kCUDA), PRECISION(kFloat)}),
石晓伟 已提交
91
#endif
92 93
    paddle::lite_api::Place({TARGET(kX86), PRECISION(kFloat)}),
    paddle::lite_api::Place({TARGET(kHost), PRECISION(kAny)}),
石晓伟 已提交
94 95 96 97 98 99 100 101 102 103 104 105
  };
  serialize_params(&(config.param), &scope, repetitive_params);
  config.model = program.Proto()->SerializeAsString();
  LOG(INFO) << "create lite_engine desc";
  framework::OpDesc engine_op_desc(nullptr);
  engine_op_desc.SetType("lite_engine");
  engine_op_desc.SetInput("Xs", std::vector<std::string>({"x", "y"}));
  engine_op_desc.SetOutput("Ys", std::vector<std::string>({"out"}));
  std::string engine_key = "engine_0";
  engine_op_desc.SetAttr("engine_key", engine_key);
  engine_op_desc.SetAttr("enable_int8", false);
  engine_op_desc.SetAttr("use_gpu", true);
106
  engine_op_desc.SetAttr("zero_copy", true);
石晓伟 已提交
107 108 109 110 111 112 113 114 115 116 117
  engine_op_desc.SetBlockAttr("sub_block", &block_desc);
  inference::Singleton<inference::lite::EngineManager>::Global().Create(
      engine_key, config);
  LOG(INFO) << "create engine op";
  auto engine_op = framework::OpRegistry::CreateOp(engine_op_desc);
  LOG(INFO) << "engine_op " << engine_op.get();
  // Execute them.
  LOG(INFO) << "engine_op run";
  engine_op->Run(scope, place);
  LOG(INFO) << "done";
}
118 119
#endif

石晓伟 已提交
120 121
}  // namespace operators
}  // namespace paddle