test_arg_mapping_context.cc 4.1 KB
Newer Older
W
weishengying 已提交
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 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/* 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 <gtest/gtest.h>

#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/inference/tensorrt/plugin_arg_mapping_context.h"

namespace paddle {
namespace inference {
namespace tensorrt {

TEST(ArgMappingContexTest, BasicFunction) {
  paddle::framework::proto::OpDesc op;
  op.set_type("imaged_op");
  auto *input_var = op.add_inputs();
  input_var->set_parameter("X");
  *input_var->add_arguments() = "input";

  auto *output_var = op.add_outputs();
  output_var->set_parameter("Out");
  *output_var->add_arguments() = "output";

  auto *attr = op.add_attrs();
  attr->set_name("int_attr");
  attr->set_type(paddle::framework::proto::AttrType::INT);
  attr->set_i(1);

  attr = op.add_attrs();
  attr->set_name("float_attr");
  attr->set_type(paddle::framework::proto::AttrType::FLOAT);
  attr->set_f(1.0);

  attr = op.add_attrs();
  attr->set_name("string_attr");
  attr->set_type(paddle::framework::proto::AttrType::STRING);
  attr->set_s("1");

  attr = op.add_attrs();
  attr->set_name("bool_attr");
  attr->set_type(paddle::framework::proto::AttrType::BOOLEAN);
  attr->set_b(true);

  attr = op.add_attrs();
  attr->set_name("ints_attr");
  attr->set_type(paddle::framework::proto::AttrType::INTS);
  attr->add_ints(1);
  attr->add_ints(2);

  attr = op.add_attrs();
  attr->set_name("floats_attr");
  attr->set_type(paddle::framework::proto::AttrType::FLOATS);
  attr->add_floats(1.0);
  attr->add_floats(2.0);

  attr = op.add_attrs();
  attr->set_name("strings_attr");
  attr->set_type(paddle::framework::proto::AttrType::STRINGS);
  attr->add_strings("1");
  attr->add_strings("2");

  attr = op.add_attrs();
  attr->set_name("bools_attr");
  attr->set_type(paddle::framework::proto::AttrType::BOOLEANS);
  attr->add_bools(true);
  attr->add_bools(true);

  framework::OpDesc op_desc(op, nullptr);
  PluginArgumentMappingContext context(&op_desc);

  EXPECT_EQ(context.HasInput("X"), true);
  EXPECT_EQ(context.HasOutput("Out"), true);
  EXPECT_EQ(context.HasAttr("int_attr"), true);

  int int_attr = any_cast<int>(context.Attr("int_attr"));
  EXPECT_EQ(int_attr, 1);

  float flaot_attr = any_cast<float>(context.Attr("float_attr"));
  EXPECT_EQ(flaot_attr, 1);

  std::string string_attr = any_cast<std::string>(context.Attr("string_attr"));
  EXPECT_EQ(string_attr, "1");

  bool bool_attr = any_cast<bool>(context.Attr("bool_attr"));
  EXPECT_EQ(bool_attr, true);

  std::vector<int> ints_attr =
      any_cast<std::vector<int>>(context.Attr("ints_attr"));
  EXPECT_EQ(ints_attr[0], 1);
  EXPECT_EQ(ints_attr[1], 2);

  std::vector<float> floats_attr =
      any_cast<std::vector<float>>(context.Attr("floats_attr"));
  EXPECT_EQ(floats_attr[0], 1.0);
  EXPECT_EQ(floats_attr[1], 2.0);

  std::vector<std::string> strings_attr =
      any_cast<std::vector<std::string>>(context.Attr("strings_attr"));
  EXPECT_EQ(strings_attr[0], "1");
  EXPECT_EQ(strings_attr[1], "2");

  std::vector<bool> bools_attr =
      any_cast<std::vector<bool>>(context.Attr("bools_attr"));
  EXPECT_EQ(bools_attr[0], true);
  EXPECT_EQ(bools_attr[1], true);

  EXPECT_EQ(context.InputSize("X"), true);
  EXPECT_EQ(context.OutputSize("Out"), true);
  EXPECT_EQ(context.IsDenseTensorInput("X"), false);
  EXPECT_EQ(context.IsDenseTensorInputs("X"), false);
  EXPECT_EQ(context.IsSelectedRowsInput("X"), false);
  EXPECT_EQ(context.IsDenseTensorVectorInput("X"), false);

  EXPECT_EQ(context.IsDenseTensorOutput("Out"), false);
  EXPECT_EQ(context.IsSelectedRowsOutput("Out"), false);
  EXPECT_EQ(context.IsForInferShape(), false);
}

}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle