net_builder_test.cc 48.8 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// Copyright (c) 2021 CINN 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 "paddle/cinn/frontend/net_builder.h"

#include <gtest/gtest.h>

#include <algorithm>
#include <memory>
#include <random>
#include <vector>

#include "paddle/cinn/common/target.h"
#include "paddle/cinn/frontend/optimize.h"
#include "paddle/cinn/frontend/syntax.h"
#include "paddle/cinn/hlir/framework/graph.h"
#include "paddle/cinn/hlir/framework/graph_compiler.h"
#include "paddle/cinn/hlir/framework/tensor.h"
#include "paddle/cinn/hlir/op/use_ops.h"
#include "paddle/cinn/utils/data_util.h"
#ifdef CINN_WITH_CUDA
#include <cuda_runtime.h>
#endif

namespace cinn {
namespace frontend {

using hlir::framework::OpRegistry;

namespace {
Program CreateAddProgram() {
  constexpr int M = 32;
  constexpr int N = 24;

  NetBuilder builder("net_builder");
47 48 49 50
  auto a = builder.CreateInput(Float(32), {M, N}, "A");
  auto b = builder.CreateInput(Float(32), {M, N}, "B");
  auto c = builder.Add(a, b);
  auto d = builder.Add(a, c);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  auto program = builder.Build();

  return program;
}

template <typename T, typename Alloc = std::allocator<T>>
std::ostream& operator<<(std::ostream& os, const std::vector<T, Alloc>& vec) {
  os << "{ ";
  for (auto e : vec) {
    os << e << " ";
  }
  os << "}\n";
  return os;
}
}  // namespace

TEST(net_build, basic) {
68 69 70 71
  LOG(INFO) << "The size of registered operators: "
            << OpRegistry::Global()->ListAllNames().size();
  LOG(INFO) << "Registered operators:\n"
            << OpRegistry::Global()->ListAllNames();
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 133 134 135 136 137 138 139
  auto program = CreateAddProgram();
  // output program
  for (int i = 0; i < program.size(); i++) {
    LOG(INFO) << "instruction: " << program[i];
  }
}

TEST(net_build, program_execute_multi_elementwise_add) {
  auto program = CreateAddProgram();
#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);
  LOG(INFO) << "graph:\n" << graph->Visualize();

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>("A");
  scope->Var<hlir::framework::Tensor>("B");

  auto A = scope->GetTensor("A");
  auto B = scope->GetTensor("B");
  SetRandData<float>(A, target);
  SetRandData<float>(B, target);

  runtime_program->Execute();
}
#ifdef CINN_WITH_CUDA
TEST(net_build, program_execute_fc) {
  constexpr int B = 10;  // batch size
  constexpr int M = 32;
  constexpr int K = 18;
  constexpr int N = 24;

  NetBuilder builder("net_builder");
  auto a = builder.CreateInput(Float(32), {B * M, K}, "A");
  auto w = builder.CreateInput(Float(32), {K, N}, "W");  // weight
  auto b = builder.CreateInput(Float(32), {N}, "B");     // bias

  auto mul_out = builder.Matmul(a, w);
  auto add_out = builder.Add(mul_out, b);
  auto program = builder.Build();

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);
  LOG(INFO) << "graph:\n" << graph->Visualize();

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(a.id()));
  scope->Var<hlir::framework::Tensor>(std::string(w.id()));
  scope->Var<hlir::framework::Tensor>(std::string(b.id()));
  scope->Var<hlir::framework::Tensor>(std::string(mul_out->id));

140 141 142
  auto a_ten = scope->GetTensor(std::string(a.id()));
  auto w_ten = scope->GetTensor(std::string(w.id()));
  auto b_ten = scope->GetTensor(std::string(b.id()));
143
  auto fake_out_ten = scope->GetTensor(std::string(mul_out->id));
144
  auto add_out_ten = scope->GetTensor(std::string(add_out->id));
145 146 147 148 149 150 151 152 153 154 155 156 157 158
  SetRandData<float>(a_ten, target);
  SetRandData<float>(w_ten, target);
  SetRandData<float>(b_ten, target);

  runtime_program->Execute();
}
#endif

#ifdef CINN_WITH_CUDA
TEST(net_build, program_execute_multi_elementwise_add_bf16) {
  constexpr int M = 32;
  constexpr int N = 24;

  NetBuilder builder("net_builder");
159 160 161 162
  auto a = builder.CreateInput(cinn::common::BFloat16(), {M, N}, "A");
  auto b = builder.CreateInput(cinn::common::BFloat16(), {M, N}, "B");
  auto c = builder.Add(a, b);
  auto d = builder.Add(a, c);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  auto program = builder.Build();

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);
  LOG(INFO) << "graph:\n" << graph->Visualize();

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>("A");
  scope->Var<hlir::framework::Tensor>("B");

  auto A = scope->GetTensor("A");
  auto B = scope->GetTensor("B");
  SetRandData<float>(A, target);
  SetRandData<float>(B, target);

  runtime_program->Execute();
}

TEST(net_build, program_execute_fc_bf16) {
  constexpr int B = 10;  // batch size
  constexpr int M = 32;
  constexpr int K = 18;
  constexpr int N = 24;

  NetBuilder builder("net_builder");
  auto a = builder.CreateInput(cinn::common::BFloat16(), {B * M, K}, "A");
198 199 200
  auto w =
      builder.CreateInput(cinn::common::BFloat16(), {K, N}, "W");    // weight
  auto b = builder.CreateInput(cinn::common::BFloat16(), {N}, "B");  // bias
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

  auto mul_out = builder.Matmul(a, w);
  auto add_out = builder.Add(mul_out, b);
  auto program = builder.Build();

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);
  LOG(INFO) << "graph:\n" << graph->Visualize();

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(a.id()));
  scope->Var<hlir::framework::Tensor>(std::string(w.id()));
  scope->Var<hlir::framework::Tensor>(std::string(b.id()));
  scope->Var<hlir::framework::Tensor>(std::string(mul_out->id));

225 226 227
  auto a_ten = scope->GetTensor(std::string(a.id()));
  auto w_ten = scope->GetTensor(std::string(w.id()));
  auto b_ten = scope->GetTensor(std::string(b.id()));
228
  auto fake_out_ten = scope->GetTensor(std::string(mul_out->id));
229
  auto add_out_ten = scope->GetTensor(std::string(add_out->id));
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  SetRandData<float>(a_ten, target);
  SetRandData<float>(w_ten, target);
  SetRandData<float>(b_ten, target);

  runtime_program->Execute();
}
#endif

TEST(net_build, program_execute_pool2d) {
  const int B = 16;
  const int C = 64;
  const int H = 112;
  const int W = 112;

  NetBuilder builder("net_builder");
245
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "Img");
246 247 248 249
  std::string pooling_type = "max";
  std::vector<int> ksize{3, 3};
  std::vector<int> strides{2, 2};
  std::vector<int> paddings{1, 1, 1, 1};
250 251 252 253 254
  bool ceil_mode = false;
  bool exclusive = true;
  bool global_pooling = false;
  std::string data_format = "NCHW";
  bool adaptive = false;
255
  std::string padding_algorithm = "EXPLICIT";
256
  Variable pool_out = builder.Pool2d(input,
257 258 259 260 261 262 263 264 265 266
                                     pooling_type,
                                     ksize,
                                     strides,
                                     paddings,
                                     ceil_mode,
                                     exclusive,
                                     global_pooling,
                                     data_format,
                                     adaptive,
                                     padding_algorithm);
267
  auto program = builder.Build();
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);
  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(pool_out->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  runtime_program->Execute();
}

TEST(net_build, program_execute_reverse) {
  const int B = 16;
  const int C = 3;
  const int H = 224;
  const int W = 224;

  NetBuilder builder("net_builder");
296
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "Img");
297
  Variable reverse_out = builder.Reverse(input, {2, 3});
298
  auto program = builder.Build();
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);
  LOG(INFO) << "graph:\n" << graph->Visualize();

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(reverse_out->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  runtime_program->Execute();
}

TEST(net_build, program_execute_gather) {
323
  const int B = 4;
324 325 326 327 328 329
  const int H_IN1 = 18;
  const int H_IN2 = 14;

  NetBuilder builder("net_builder");
  Placeholder input1 = builder.CreateInput(Float(32), {B, H_IN1}, "In1");
  Placeholder input2 = builder.CreateInput(Int(32), {H_IN2}, "In2");
330 331
  Variable output = builder.Gather(input1, input2, 1);
  auto program = builder.Build();
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input1.id()));
  scope->Var<hlir::framework::Tensor>(std::string(input2.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input1_tensor = scope->GetTensor(std::string(input1.id()));
  SetRandData<float>(input1_tensor, target);
  std::vector<float> input1_data = GetTensorData<float>(input1_tensor, target);

  auto input2_tensor = scope->GetTensor(std::string(input2.id()));
  SetRandInt(input2_tensor, target, -1, 0, H_IN1);
  std::vector<int> input2_data = GetTensorData<int>(input2_tensor, target);

  runtime_program->Execute();

359
  auto output_tensor = scope->GetTensor(std::string(output->id));
360 361 362 363 364 365 366 367 368 369 370
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_tensor->type(), Float(32));
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H_IN2);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int h = 0; h < H_IN2; ++h) {
      std::string line;
371 372
      int index = h + H_IN2 * b;
      float in_data = input1_data[input2_data[h] + H_IN1 * b];
373 374 375 376 377 378 379 380 381
      float out_data = output_data[index];
      line += (std::to_string(out_data) + ", ");
      EXPECT_EQ(in_data, out_data);
      VLOG(6) << line;
    }
  }
}

TEST(net_build, program_execute_gather_nd) {
382
  const int B = 4;
383 384 385 386 387 388
  const int H_IN1 = 11;
  const int H_IN2 = 14;

  NetBuilder builder("net_builder");
  Placeholder input1 = builder.CreateInput(Float(32), {B, H_IN1}, "In1");
  Placeholder input2 = builder.CreateInput(Int(32), {B, H_IN2, 1}, "In2");
389 390
  Variable output = builder.GatherNd(input1, input2);
  auto program = builder.Build();
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input1.id()));
  scope->Var<hlir::framework::Tensor>(std::string(input2.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input1_tensor = scope->GetTensor(std::string(input1.id()));
  SetRandData<float>(input1_tensor, target);
  std::vector<float> input1_data = GetTensorData<float>(input1_tensor, target);

  auto input2_tensor = scope->GetTensor(std::string(input2.id()));
  SetRandInt(input2_tensor, target, -1, 0, B);
  std::vector<int> input2_data = GetTensorData<int>(input2_tensor, target);

  runtime_program->Execute();

419
  auto output_tensor = scope->GetTensor(std::string(output->id));
420 421 422 423 424 425 426 427 428 429 430 431 432
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_tensor->type(), Float(32));
  EXPECT_EQ(output_shape.size(), 3UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H_IN2);
  EXPECT_EQ(output_shape[2], H_IN1);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int h = 0; h < H_IN2; ++h) {
      std::string line;
      for (int c = 0; c < H_IN1; ++c) {
433 434
        float in_data = input1_data[input2_data[b * H_IN2 + h] * H_IN1 + c];
        int out_index = c + h * H_IN1 + H_IN1 * H_IN2 * b;
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        float out_data = output_data[out_index];
        line += (std::to_string(out_data) + ", ");
        EXPECT_EQ(in_data, out_data);
      }
      VLOG(6) << line;
    }
  }
}

TEST(net_build, program_execute_cast) {
  const int B = 4;
  const int H = 7;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Int(32), {B, H}, "In");
450 451
  Variable output = builder.Cast(input, "float");
  auto program = builder.Build();
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandInt(input_tensor, target);
  std::vector<int> input_data = GetTensorData<int>(input_tensor, target);

  runtime_program->Execute();

474
  auto output_tensor = scope->GetTensor(std::string(output->id));
475 476 477 478 479 480 481 482 483 484 485
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_tensor->type(), Float(32));
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int h = 0; h < H; ++h) {
      std::string line;
486 487
      int index = h + H * b;
      float in_data = (float)input_data[index];
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      float out_data = output_data[index];
      line += (std::to_string(out_data) + ", ");
      EXPECT_EQ(in_data, out_data);
      VLOG(6) << line;
    }
  }
}

TEST(net_build, program_execute_squeeze_case0) {
  const int B = 4;
  const int C = 1;
  const int H = 7;
  const int W = 1;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "In");
504 505
  Variable output = builder.Squeeze(input, {1});
  auto program = builder.Build();
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

528
  auto output_tensor = scope->GetTensor(std::string(output->id));
529 530 531 532 533 534 535 536 537 538 539 540 541 542
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 3UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);
  EXPECT_EQ(output_shape[2], W);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int c = 0; c < C; ++c) {
      VLOG(6) << "b = " << b << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
543 544
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
          float out_data = output_data[index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_EQ(in_data, out_data);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_execute_squeeze_case1) {
  const int B = 4;
  const int C = 1;
  const int H = 7;
  const int W = 1;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "In");
563 564
  Variable output = builder.Squeeze(input, {-3});
  auto program = builder.Build();
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

587
  auto output_tensor = scope->GetTensor(std::string(output->id));
588 589 590 591 592 593 594 595 596 597 598 599 600 601
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 3UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);
  EXPECT_EQ(output_shape[2], W);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int c = 0; c < C; ++c) {
      VLOG(6) << "b = " << b << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
602 603
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
          float out_data = output_data[index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_EQ(in_data, out_data);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_execute_squeeze_case2) {
  const int B = 4;
  const int C = 1;
  const int H = 7;
  const int W = 1;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "In");
622 623
  Variable output = builder.Squeeze(input, {1, 3});
  auto program = builder.Build();
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

646
  auto output_tensor = scope->GetTensor(std::string(output->id));
647 648 649 650 651 652 653 654 655 656 657 658 659
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int c = 0; c < C; ++c) {
      VLOG(6) << "b = " << b << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
660 661
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
          float out_data = output_data[index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_EQ(in_data, out_data);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_execute_squeeze_case3) {
  const int B = 4;
  const int C = 1;
  const int H = 7;
  const int W = 1;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "In");
680 681
  Variable output = builder.Squeeze(input, {1, -1});
  auto program = builder.Build();
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

704
  auto output_tensor = scope->GetTensor(std::string(output->id));
705 706 707 708 709 710 711 712 713 714 715 716 717
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int c = 0; c < C; ++c) {
      VLOG(6) << "b = " << b << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
718 719
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
          float out_data = output_data[index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_EQ(in_data, out_data);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_execute_squeeze_case4) {
  const int B = 4;
  const int C = 1;
  const int H = 7;
  const int W = 1;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "In");
738 739
  Variable output = builder.Squeeze(input, {});
  auto program = builder.Build();
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

762
  auto output_tensor = scope->GetTensor(std::string(output->id));
763 764 765 766 767 768 769 770 771 772 773 774 775
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int b = 0; b < B; ++b) {
    for (int c = 0; c < C; ++c) {
      VLOG(6) << "b = " << b << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
776 777
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
          float out_data = output_data[index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_EQ(in_data, out_data);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_execute_argsort) {
  const int B = 4;
  const int H = 7;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, H}, "In");
794 795
  Variable output = builder.ArgSort(input, 0, true).at(0);
  auto program = builder.Build();
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

818
  auto output_tensor = scope->GetTensor(std::string(output->id));
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_tensor->type(), Int(32));
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);

  std::vector<int> output_data = GetTensorData<int>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int h = 0; h < H; ++h) {
    std::vector<float> sorted_data;
    std::vector<float> out_sorted_data(H);
    for (int b = 0; b < B; ++b) {
      int index = h + H * b;
      sorted_data.push_back(input_data[index]);
      out_sorted_data[b] = input_data[h + H * output_data[index]];
    }
    std::sort(sorted_data.begin(), sorted_data.begin() + B);

    for (int b = 0; b < B; ++b) {
      std::string line;
839
      int index = h + H * b;
840
      float true_data = sorted_data[b];
841
      float out_data = out_sorted_data[b];
842 843 844 845 846 847 848 849 850 851 852 853 854
      line += (std::to_string(out_data) + ", ");
      EXPECT_EQ(true_data, out_data);
      VLOG(6) << line;
    }
  }
}

TEST(net_build, program_execute_sort) {
  const int B = 4;
  const int H = 7;

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {B, H}, "In");
855 856
  Variable output = builder.Sort(input, 0, true);
  auto program = builder.Build();
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

879
  auto output_tensor = scope->GetTensor(std::string(output->id));
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_tensor->type(), Float(32));
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], B);
  EXPECT_EQ(output_shape[1], H);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int h = 0; h < H; ++h) {
    std::vector<float> sorted_data;
    for (int b = 0; b < B; ++b) {
      int index = h + H * b;
      sorted_data.push_back(input_data[index]);
    }
    std::sort(sorted_data.begin(), sorted_data.begin() + B);

    for (int b = 0; b < B; ++b) {
      std::string line;
898
      int index = h + H * b;
899
      float true_data = sorted_data[b];
900
      float out_data = output_data[index];
901 902 903 904 905 906 907 908
      line += (std::to_string(out_data) + ", ");
      EXPECT_EQ(true_data, out_data);
      VLOG(6) << line;
    }
  }
}

TEST(net_build, program_execute_arange_float) {
909 910 911
  const float start = 1.5F;
  const float stop = 31.5F;
  const float step = 2.0F;
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  const std::string dtype = "float32";

  NetBuilder builder("net_builder");
  Variable out = builder.Arange(start, stop, step, dtype);
  auto program = builder.Build();

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(out->id));

  runtime_program->Execute();

934
  auto out_tensor = scope->GetTensor(std::string(out->id));
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  const std::vector<int>& out_tensor_shape = out_tensor->shape().data();
  EXPECT_EQ(out_tensor->type(), Float(32));
  EXPECT_EQ(out_tensor_shape.size(), 1UL);

  int num_elem = static_cast<int>(std::ceil((stop - start) / step));
  EXPECT_EQ(out_tensor_shape[0], num_elem);

  std::vector<float> out_data = GetTensorData<float>(out_tensor, target);
  for (int i = 0; i < out_tensor_shape[0]; ++i) {
    EXPECT_NEAR(out_data[i], start + step * i, 1e-5);
    VLOG(6) << out_data[i];
  }
}

TEST(net_build, program_execute_arange_int) {
950 951 952
  const float start = 1.5F;
  const float stop = 31.5F;
  const float step = 1.6F;
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
  const std::string dtype = "int32";

  NetBuilder builder("net_builder");
  Variable out = builder.Arange(start, stop, step, dtype);
  auto program = builder.Build();

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(out->id));

  runtime_program->Execute();

975
  auto out_tensor = scope->GetTensor(std::string(out->id));
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
  const std::vector<int>& out_tensor_shape = out_tensor->shape().data();
  EXPECT_EQ(out_tensor->type(), Int(32));
  EXPECT_EQ(out_tensor_shape.size(), 1UL);

  int num_elem = static_cast<int>(std::ceil((stop - start) / step));
  EXPECT_EQ(out_tensor_shape[0], num_elem);

  std::vector<int> out_data = GetTensorData<int>(out_tensor, target);
  for (int i = 0; i < out_tensor_shape[0]; ++i) {
    EXPECT_EQ(out_data[i], static_cast<int32_t>(start + step * i));
    VLOG(6) << out_data[i];
  }
}

TEST(net_build, program_argmax_case1) {
991 992
  const int N = 4;
  const int IN_C = 3;
993
  const int OUT_C = 1;
994 995
  const int H = 7;
  const int W = 7;
996 997 998

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
999 1000
  Variable output = builder.Argmax(input, 1, true);
  auto program = builder.Build();
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);
  VLOG(6) << "Visualize input_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
          int index = w + W * (h + H * (c + IN_C * n));
          line += (std::to_string(input_data[index]) + ", ");
        }
        VLOG(6) << line;
      }
    }
  }
  runtime_program->Execute();

1036
  auto output_tensor = scope->GetTensor(std::string(output->id));
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 4UL);
  EXPECT_EQ(output_shape[0], N);
  EXPECT_EQ(output_shape[1], OUT_C);
  EXPECT_EQ(output_shape[2], H);
  EXPECT_EQ(output_shape[3], W);

  std::vector<int> output_data = GetTensorData<int>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
1052
          int index = w + W * (h + H * (c + IN_C * n));
1053 1054
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1055
          int out_data = output_data[out_index];
1056 1057
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1058
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
          float max_value = input_data[max_index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_LE(in_data, max_value);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_argmax_case2) {
1070
  const int N = 4;
1071
  const int IN_C = 3;
1072 1073
  const int H = 7;
  const int W = 7;
1074 1075 1076

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1077 1078
  Variable output = builder.Argmax(input, 1, false);
  auto program = builder.Build();
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

  Target target = common::DefaultHostTarget();
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  float* input_data = input_tensor->mutable_data<float>(target);
  VLOG(6) << "Visualize input_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
          int index = w + W * (h + H * (c + IN_C * n));
          line += (std::to_string(input_data[index]) + ", ");
        }
        VLOG(6) << line;
      }
    }
  }
  runtime_program->Execute();

1110
  auto output_tensor = scope->GetTensor(std::string(output->id));
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 3UL);
  EXPECT_EQ(output_shape[0], N);
  EXPECT_EQ(output_shape[1], H);
  EXPECT_EQ(output_shape[2], W);

  int* output_data = output_tensor->mutable_data<int>(target);
  VLOG(6) << "Visualize output_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
1125
          int index = w + W * (h + H * (c + IN_C * n));
1126 1127
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1128
          int out_data = output_data[out_index];
1129 1130
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1131
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
          float max_value = input_data[max_index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_LE(in_data, max_value);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_argmin_case1) {
1143 1144
  const int N = 4;
  const int IN_C = 3;
1145
  const int OUT_C = 1;
1146 1147
  const int H = 7;
  const int W = 7;
1148 1149 1150

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1151 1152
  Variable output = builder.Argmin(input, 1, true);
  auto program = builder.Build();
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif

  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);
  VLOG(6) << "Visualize input_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
          int index = w + W * (h + H * (c + IN_C * n));
          line += (std::to_string(input_data[index]) + ", ");
        }
        VLOG(6) << line;
      }
    }
  }
  runtime_program->Execute();

1188
  auto output_tensor = scope->GetTensor(std::string(output->id));
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 4UL);
  EXPECT_EQ(output_shape[0], N);
  EXPECT_EQ(output_shape[1], OUT_C);
  EXPECT_EQ(output_shape[2], H);
  EXPECT_EQ(output_shape[3], W);

  std::vector<int> output_data = GetTensorData<int>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
1204
          int index = w + W * (h + H * (c + IN_C * n));
1205 1206
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1207
          int out_data = output_data[out_index];
1208 1209
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1210
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
          float max_value = input_data[max_index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_GE(in_data, max_value);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_argmin_case2) {
1222
  const int N = 4;
1223
  const int IN_C = 3;
1224 1225
  const int H = 7;
  const int W = 7;
1226 1227 1228

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1229 1230
  Variable output = builder.Argmin(input, 1, false);
  auto program = builder.Build();
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);
  VLOG(6) << "Visualize input_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
          int index = w + W * (h + H * (c + IN_C * n));
          line += (std::to_string(input_data[index]) + ", ");
        }
        VLOG(6) << line;
      }
    }
  }
  runtime_program->Execute();

1265
  auto output_tensor = scope->GetTensor(std::string(output->id));
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
  const std::vector<int>& output_shape = output_tensor->shape().data();
  EXPECT_EQ(output_shape.size(), 3UL);
  EXPECT_EQ(output_shape[0], N);
  EXPECT_EQ(output_shape[1], H);
  EXPECT_EQ(output_shape[2], W);

  std::vector<int> output_data = GetTensorData<int>(output_tensor, target);
  VLOG(6) << "Visualize output_data";
  for (int n = 0; n < N; ++n) {
    for (int c = 0; c < IN_C; ++c) {
      VLOG(6) << "n = " << n << ", c = " << c;
      for (int h = 0; h < H; ++h) {
        std::string line;
        for (int w = 0; w < W; ++w) {
1280
          int index = w + W * (h + H * (c + IN_C * n));
1281 1282
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1283
          int out_data = output_data[out_index];
1284 1285
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1286
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
          float max_value = input_data[max_index];
          line += (std::to_string(out_data) + ", ");
          EXPECT_GE(in_data, max_value);
        }
        VLOG(6) << line;
      }
    }
  }
}

TEST(net_build, program_execute_repeat_axis_0) {
1298 1299
  const int M = 4;
  const int N = 4;
1300
  const int repeats = 3;
1301
  const int axis = 0;
1302 1303 1304

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {M, N}, "In");
1305 1306
  Variable output = builder.Repeat(input, repeats, axis);
  auto program = builder.Build();
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

1329
  auto output_tensor = scope->GetTensor(std::string(output->id));
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
  const std::vector<int>& output_shape = output_tensor->shape().data();

  const int new_M = M * repeats;
  const int new_N = N;
  EXPECT_EQ(output_tensor->type(), Float(32));
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], new_M);
  EXPECT_EQ(output_shape[1], new_N);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  for (int m = 0; m < new_M; ++m) {
    for (int n = 0; n < new_N; ++n) {
1342 1343 1344
      int in_index = n + N * static_cast<int>(std::floor((float)m / repeats));
      int out_index = n + new_N * m;
      float in_data = input_data[in_index];
1345 1346 1347 1348 1349 1350 1351
      float out_data = output_data[out_index];
      EXPECT_EQ(in_data, out_data);
    }
  }
}

TEST(net_build, program_execute_repeat_axis_1) {
1352 1353
  const int M = 4;
  const int N = 4;
1354
  const int repeats = 3;
1355
  const int axis = 1;
1356 1357 1358

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {M, N}, "In");
1359 1360
  Variable output = builder.Repeat(input, repeats, axis);
  auto program = builder.Build();
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

  auto input_tensor = scope->GetTensor(std::string(input.id()));
  SetRandData<float>(input_tensor, target);
  std::vector<float> input_data = GetTensorData<float>(input_tensor, target);

  runtime_program->Execute();

1383
  auto output_tensor = scope->GetTensor(std::string(output->id));
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
  const std::vector<int>& output_shape = output_tensor->shape().data();

  const int new_M = M;
  const int new_N = N * repeats;
  EXPECT_EQ(output_tensor->type(), Float(32));
  EXPECT_EQ(output_shape.size(), 2UL);
  EXPECT_EQ(output_shape[0], new_M);
  EXPECT_EQ(output_shape[1], new_N);

  std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
  for (int m = 0; m < new_M; ++m) {
    for (int n = 0; n < new_N; ++n) {
1396 1397 1398
      int in_index = N * m + static_cast<int>(std::floor((float)n / repeats));
      int out_index = n + new_N * m;
      float in_data = input_data[in_index];
1399 1400 1401 1402 1403 1404 1405
      float out_data = output_data[out_index];
      EXPECT_EQ(in_data, out_data);
    }
  }
}

TEST(net_build, program_execute_one_hot) {
1406 1407 1408 1409 1410 1411
  const int M = 4;
  const int N = 4;
  const int on_value = 1;
  const int off_value = 0;
  const int depth = 11;
  const int axis = 0;  // [-1 , M]
1412 1413 1414
  const std::string dtype = "int32";

  NetBuilder builder("net_builder");
1415 1416
  Placeholder input = builder.CreateInput(Int(32), {M, N}, "In");
  Placeholder on_value_input = builder.CreateInput(Int(32), {1}, "OnValue");
1417
  Placeholder off_value_input = builder.CreateInput(Int(32), {1}, "OffValue");
1418 1419 1420
  Variable output = builder.OneHot(
      input, on_value_input, off_value_input, depth, axis, dtype);
  auto program = builder.Build();
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

#ifdef CINN_WITH_CUDA
  Target target = common::DefaultNVGPUTarget();
#else
  Target target = common::DefaultHostTarget();
#endif
  std::unordered_set<std::string> fetch_ids;
  auto graph = Optimize(&program, fetch_ids, target);

  auto scope = BuildScope(target, graph);
  hlir::framework::GraphCompiler gc(target, scope, graph);
  auto runtime_program = gc.Build();

  scope->Var<hlir::framework::Tensor>(std::string(input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(on_value_input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(off_value_input.id()));
  scope->Var<hlir::framework::Tensor>(std::string(output->id));

1439
  auto input_tensor = scope->GetTensor(std::string(input.id()));
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
  const std::vector<int>& intput_shape = input_tensor->shape().data();
  SetRandInt(input_tensor, target);
  std::vector<int> input_data = GetTensorData<int>(input_tensor, target);

  auto on_value_tensor = scope->GetTensor(std::string(on_value_input.id()));
  SetRandInt(on_value_tensor, target, -1, on_value, on_value + 1);

  auto off_value_tensor = scope->GetTensor(std::string(off_value_input.id()));
  SetRandInt(off_value_tensor, target, -1, off_value, off_value + 1);

  runtime_program->Execute();

1452
  auto output_tensor = scope->GetTensor(std::string(output->id));
1453
  const std::vector<int>& output_shape = output_tensor->shape().data();
1454
  std::vector<int> output_data = GetTensorData<int>(output_tensor, target);
1455 1456 1457 1458

  EXPECT_EQ(output_tensor->type(), Int(32));
  EXPECT_EQ(output_shape.size(), intput_shape.size() + 1);

1459
  const int true_axis = axis == -1 ? M : axis;
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
  int input_shape_index = 0;

  for (int i = 0; i < output_shape.size(); i++) {
    LOG(INFO) << output_shape[i];
    if (i == true_axis) {
      EXPECT_EQ(output_shape[i], depth);
    } else {
      EXPECT_EQ(output_shape[i], intput_shape[input_shape_index++]);
    }
  }

  for (int i = 0; i < output_shape[0]; ++i) {
    for (int j = 0; j < output_shape[1]; ++j) {
      for (int k = 0; k < output_shape[2]; ++k) {
        std::vector<int> s = {i, j, k};
1475 1476 1477
        int input_index = 0;
        int output_index = 0;
        int base = 1;
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

        for (int x = s.size() - 1; x >= 0; --x) {
          if (x == true_axis) {
            continue;
          }
          input_index += base * s[x];
          base = base * output_shape[x];
        }

        base = 1;

        for (int x = s.size() - 1; x >= 0; --x) {
          output_index += base * s[x];
          base = base * output_shape[x];
        }

        if (s[true_axis] == input_data[input_index]) {
          EXPECT_EQ(output_data[output_index], on_value);
        } else {
          EXPECT_EQ(output_data[output_index], off_value);
        }
      }
    }
  }
}

}  // namespace frontend
}  // namespace cinn