prim_op_test.cc 22.3 KB
Newer Older
L
levi131 已提交
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
// 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_registry.h"
#include "paddle/fluid/framework/program_desc.h"

USE_OP_ITSELF(reshape_p);
USE_OP_ITSELF(broadcast_p);
USE_OP_ITSELF(reduce_p);
USE_OP_ITSELF(transpose_p);
USE_OP_ITSELF(split_p);
USE_OP_ITSELF(concat_p);
USE_OP_ITSELF(slice_select_p);
USE_OP_ITSELF(slice_assign_p);
USE_OP_ITSELF(gather_p);
USE_OP_ITSELF(scatter_add_p);
USE_OP_ITSELF(add_p);
USE_OP_ITSELF(sub_p);
USE_OP_ITSELF(mul_p);
USE_OP_ITSELF(div_p);
USE_OP_ITSELF(sqrt_p);
USE_OP_ITSELF(tanh_p);
USE_OP_ITSELF(matmul_p);
USE_OP_ITSELF(fill_constant_p);
37
USE_OP_ITSELF(log_p);
38 39 40
USE_OP_ITSELF(select_p);
USE_OP_ITSELF(eq_p);
USE_OP_ITSELF(pow_p);
41
USE_OP_ITSELF(max_p);
42
USE_OP_ITSELF(erf_p);
L
levi131 已提交
43 44 45 46

namespace paddle {
namespace framework {

47 48
static void NewVar(BlockDesc *block,
                   const std::string &name,
L
levi131 已提交
49 50 51 52 53 54 55 56 57
                   const std::vector<int64_t> &shape) {
  auto *var_desc = block->Var(name);
  if (shape.size() > 0) {
    var_desc->SetShape(shape);
    var_desc->SetType(proto::VarType::LOD_TENSOR);
    var_desc->SetDataType(proto::VarType_Type_FP32);
  }
}

58 59 60 61
static void AppendOp(BlockDesc *block,
                     const std::string &type,
                     VariableNameMap inputs,
                     VariableNameMap outputs,
L
levi131 已提交
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
                     AttributeMap attrs) {
  auto &op_info = OpInfoMap::Instance().Get(type);
  if (op_info.Checker()) {
    op_info.Checker()->Check(&attrs);
  }

  auto *op = block->AppendOp();
  op->SetType(type);
  for (auto &pair : inputs) {
    op->SetInput(pair.first, pair.second);
  }

  for (auto &pair : outputs) {
    op->SetOutput(pair.first, pair.second);
    for (auto &var_name : pair.second) {
      if (!block->FindVarRecursive(var_name)) {
        NewVar(block, var_name, {});
      }
    }
  }

  op->SetAttrMap(attrs);
  op->InferVarType(block);
  op->InferShape(*block);
}

TEST(PrimOp, reshape_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
97 98 99 100
  AppendOp(block,
           "reshape_p",
           {{"X", {x0}}},
           {{"Y", {x1}}},
L
levi131 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
           {{"shape", std::vector<int64_t>{12, 5}}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 2UL);
  ASSERT_EQ(shapes[0], 12L);
  ASSERT_EQ(shapes[1], 5L);
}

TEST(PrimOp, broadcast_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 1};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
119 120 121 122
  AppendOp(block,
           "broadcast_p",
           {{"X", {x0}}},
           {{"Y", {x1}}},
L
levi131 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
           {{"shape", std::vector<int64_t>{3, 4, 5}}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, reduce_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape);
143 144 145 146
  AppendOp(block,
           "reduce_p",
           {{"X", {x0}}},
           {{"Y", {x1}}},
L
levi131 已提交
147 148 149 150 151 152
           {{"axis", std::vector<int64_t>{0, 2}}, {"keepdim", false}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 1UL);
  ASSERT_EQ(shapes[0], 4L);
153 154 155 156
  AppendOp(block,
           "reduce_p",
           {{"X", {x0}}},
           {{"Y", {x2}}},
L
levi131 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
           {{"axis", std::vector<int64_t>{0, 2}}, {"keepdim", true}});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 1L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 1L);
}

TEST(PrimOp, transpose_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
176 177 178 179
  AppendOp(block,
           "transpose_p",
           {{"X", {x0}}},
           {{"Y", {x1}}},
L
levi131 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
           {{"axis", std::vector<int64_t>{2, 1, 0}}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 5L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 3L);
}

TEST(PrimOp, split_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{6, 8, 10};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";
  std::string x3 = "x3";

  NewVar(block, x0, shape);
201 202 203 204
  AppendOp(block,
           "split_p",
           {{"X", {x0}}},
           {{"YS", {x1, x2, x3}}},
L
levi131 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
           {{"axis", int64_t{1}},
            {"num_or_sections", std::vector<int64_t>{2, 4, 2}}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 2L);
  ASSERT_EQ(shapes[2], 10L);
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 10L);
  ASSERT_EQ(block->Var("x3")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x3")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x3")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 2L);
  ASSERT_EQ(shapes[2], 10L);
  std::string x4 = "x4";
  std::string x5 = "x5";
  AppendOp(
231 232 233 234
      block,
      "split_p",
      {{"X", {x0}}},
      {{"YS", {x4, x5}}},
L
levi131 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
      {{"axis", int64_t{2}}, {"num_or_sections", std::vector<int64_t>{2}}});
  ASSERT_EQ(block->Var("x4")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x4")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x4")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 8L);
  ASSERT_EQ(shapes[2], 5L);
  ASSERT_EQ(block->Var("x5")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x5")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x5")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 8L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, concat_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape_0{3, 1, 5};
  std::vector<int64_t> shape_1{3, 4, 5};
  std::vector<int64_t> shape_2{3, 6, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";
  std::string x3 = "x3";

  NewVar(block, x0, shape_0);
  NewVar(block, x1, shape_1);
  NewVar(block, x2, shape_2);
267 268 269 270
  AppendOp(block,
           "concat_p",
           {{"XS", {x0, x1, x2}}},
           {{"Y", {x3}}},
L
levi131 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
           {{"axis", int64_t{1}}});
  ASSERT_EQ(block->Var("x3")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x3")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x3")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 11L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, slice_select_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{6, 8, 10};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
290 291 292 293
  AppendOp(block,
           "slice_select_p",
           {{"X", {x0}}},
           {{"Y", {x1}}},
L
levi131 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
           {{"axis", std::vector<int64_t>{0, 1, 2}},
            {"starts", std::vector<int64_t>{0, 0, 0}},
            {"ends", std::vector<int64_t>{5, 7, 9}},
            {"strides", std::vector<int64_t>{2, 2, 2}}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, slice_assign_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape_0{6, 8, 10};
  std::vector<int64_t> shape_1{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape_0);
  NewVar(block, x1, shape_1);
319 320 321 322
  AppendOp(block,
           "slice_assign_p",
           {{"X", {x0}}, {"Y", {x1}}},
           {{"Z", {x2}}},
L
levi131 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
           {{"axis", std::vector<int64_t>{0, 1, 2}},
            {"starts", std::vector<int64_t>{0, 0, 0}},
            {"ends", std::vector<int64_t>{5, 7, 9}},
            {"strides", std::vector<int64_t>{2, 2, 2}}});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 8L);
  ASSERT_EQ(shapes[2], 10L);
}

TEST(PrimOp, gather_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{6, 8, 10};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
345 346 347 348
  AppendOp(block,
           "gather_p",
           {{"X", {x0}}},
           {{"Y", {x1}}},
L
levi131 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
           {{"axis", int64_t{1}}, {"index", std::vector<int64_t>{0, 2, 5}}});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 3L);
  ASSERT_EQ(shapes[2], 10L);
  std::string index_t = "index_t";
  std::string x2 = "x2";

  auto *var_desc = block->Var(index_t);
  var_desc->SetShape(std::vector<int64_t>{3});
  var_desc->SetType(proto::VarType::LOD_TENSOR);
  var_desc->SetDataType(proto::VarType_Type_INT32);
364 365 366 367 368
  AppendOp(block,
           "gather_p",
           {{"X", {x0}}, {"IndexTensor", {index_t}}},
           {{"Y", {x2}}},
           {{"axis", int64_t{1}}});
L
levi131 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 3L);
  ASSERT_EQ(shapes[2], 10L);
}

TEST(PrimOp, scatter_add_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape_0{6, 8, 10};
  std::vector<int64_t> shape_1{6, 3, 10};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape_0);
  NewVar(block, x1, shape_1);
390 391 392 393
  AppendOp(block,
           "scatter_add_p",
           {{"X", {x0}}, {"Y", {x1}}},
           {{"Z", {x2}}},
L
levi131 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
           {{"axis", int64_t{1}}, {"index", std::vector<int64_t>{0, 2, 5}}});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 8L);
  ASSERT_EQ(shapes[2], 10L);
  std::string index_t = "index_t";
  std::string x3 = "x3";

  auto *var_desc = block->Var(index_t);
  var_desc->SetShape(std::vector<int64_t>{3});
  var_desc->SetType(proto::VarType::LOD_TENSOR);
  var_desc->SetDataType(proto::VarType_Type_INT32);
409 410
  AppendOp(block,
           "scatter_add_p",
L
levi131 已提交
411
           {{"X", {x0}}, {"Y", {x1}}, {"IndexTensor", {index_t}}},
412 413
           {{"Z", {x3}}},
           {{"axis", int64_t{1}}});
L
levi131 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  ASSERT_EQ(block->Var("x3")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x3")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x3")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 6L);
  ASSERT_EQ(shapes[1], 8L);
  ASSERT_EQ(shapes[2], 10L);
}

TEST(PrimOp, add_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape);
  NewVar(block, x1, shape);
  AppendOp(block, "add_p", {{"X", {x0}}, {"Y", {x1}}}, {{"Z", {x2}}}, {});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, sub_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape);
  NewVar(block, x1, shape);
  AppendOp(block, "sub_p", {{"X", {x0}}, {"Y", {x1}}}, {{"Z", {x2}}}, {});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, mul_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape);
  NewVar(block, x1, shape);
  AppendOp(block, "mul_p", {{"X", {x0}}, {"Y", {x1}}}, {{"Z", {x2}}}, {});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, div_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape);
  NewVar(block, x1, shape);
  AppendOp(block, "div_p", {{"X", {x0}}, {"Y", {x1}}}, {{"Z", {x2}}}, {});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, sqrt_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
  AppendOp(block, "sqrt_p", {{"X", {x0}}}, {{"Y", {x1}}}, {});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, tanh_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
  AppendOp(block, "tanh_p", {{"X", {x0}}}, {{"Y", {x1}}}, {});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, matmul_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape_0{3, 4, 5};
  std::vector<int64_t> shape_1{3, 5, 8};

  std::string x0 = "x0";
  std::string x1 = "x1";
  std::string x2 = "x2";

  NewVar(block, x0, shape_0);
  NewVar(block, x1, shape_1);
  AppendOp(block, "matmul_p", {{"X", {x0}}, {"Y", {x1}}}, {{"Z", {x2}}}, {});
  ASSERT_EQ(block->Var("x2")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x2")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x2")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 8L);
  std::vector<int64_t> shape_2{4, 5};
  std::vector<int64_t> shape_3{5, 8};

  std::string x3 = "x3";
  std::string x4 = "x4";
  std::string x5 = "x5";

  NewVar(block, x3, shape_2);
  NewVar(block, x4, shape_3);
  AppendOp(block, "matmul_p", {{"X", {x3}}, {"Y", {x4}}}, {{"Z", {x5}}}, {});
  ASSERT_EQ(block->Var("x5")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x5")->GetDataType(), proto::VarType_Type_FP32);
  shapes = block->Var("x5")->GetShape();
  ASSERT_EQ(shapes.size(), 2UL);
  ASSERT_EQ(shapes[0], 4L);
  ASSERT_EQ(shapes[1], 8L);
}

TEST(PrimOp, fill_constant_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::string x0 = "x0";

588 589 590 591
  AppendOp(block,
           "fill_constant_p",
           {{}},
           {{"Y", {x0}}},
L
levi131 已提交
592 593 594 595 596 597 598 599 600 601 602 603
           {{"value", 0.0f},
            {"dtype", proto::VarType_Type_FP32},
            {"shape", std::vector<int64_t>{3, 4, 5}}});
  ASSERT_EQ(block->Var("x0")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x0")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x0")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
TEST(PrimOp, log_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
  AppendOp(block, "log_p", {{"X", {x0}}}, {{"Y", {x1}}}, {});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
TEST(PrimOp, select_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{2, 3};

  std::string cond = "cond";
  std::string x = "x";
  std::string y = "y";
  std::string z = "z";

  NewVar(block, cond, shape);
  NewVar(block, x, shape);
  NewVar(block, y, shape);

  AppendOp(block,
           "select_p",
           {{"Condition", {cond}}, {"X", {x}}, {"Y", {y}}},
           {{"Z", {z}}},
           {});
  ASSERT_EQ(block->Var("z")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("z")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("z")->GetShape();
  ASSERT_EQ(shapes.size(), 2UL);
  ASSERT_EQ(shapes[0], 2L);
  ASSERT_EQ(shapes[1], 3L);
}

TEST(PrimOp, eq_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x = "x";
  std::string y = "y";
  std::string z = "z";

  NewVar(block, x, shape);
  NewVar(block, y, shape);
  AppendOp(block, "eq_p", {{"X", {x}}, {"Y", {y}}}, {{"Z", {z}}}, {});
  ASSERT_EQ(block->Var("z")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("z")->GetDataType(), proto::VarType::BOOL);
  auto shapes = block->Var("z")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

TEST(PrimOp, pow_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x = "x";
  std::string y = "y";
  std::string z = "z";

  NewVar(block, x, shape);
  NewVar(block, y, shape);
  AppendOp(block, "pow_p", {{"X", {x}}, {"Y", {y}}}, {{"Z", {z}}}, {});
  ASSERT_EQ(block->Var("z")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("z")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("z")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
TEST(PrimOp, max_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{2, 3, 4};

  std::string x = "x";
  std::string y = "y";
  std::string z = "z";

  NewVar(block, x, shape);
  NewVar(block, y, shape);

  AppendOp(block, "max_p", {{"X", {x}}, {"Y", {y}}}, {{"Z", {z}}}, {});
  ASSERT_EQ(block->Var("z")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("z")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("z")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 2L);
  ASSERT_EQ(shapes[1], 3L);
  ASSERT_EQ(shapes[2], 4L);
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
TEST(PrimOp, erf_p) {
  ProgramDesc program;
  auto *block = program.MutableBlock(0);
  std::vector<int64_t> shape{3, 4, 5};

  std::string x0 = "x0";
  std::string x1 = "x1";

  NewVar(block, x0, shape);
  AppendOp(block, "erf_p", {{"X", {x0}}}, {{"Y", {x1}}}, {});
  ASSERT_EQ(block->Var("x1")->GetType(), proto::VarType::LOD_TENSOR);
  ASSERT_EQ(block->Var("x1")->GetDataType(), proto::VarType_Type_FP32);
  auto shapes = block->Var("x1")->GetShape();
  ASSERT_EQ(shapes.size(), 3UL);
  ASSERT_EQ(shapes[0], 3L);
  ASSERT_EQ(shapes[1], 4L);
  ASSERT_EQ(shapes[2], 5L);
}

L
levi131 已提交
733 734
}  // namespace framework
}  // namespace paddle