net_builder_test.cc 50.6 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
// 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"
29
#include "paddle/cinn/hlir/framework/graph_compiler_util.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#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");
48 49 50 51
  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);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  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) {
69 70 71 72
  LOG(INFO) << "The size of registered operators: "
            << OpRegistry::Global()->ListAllNames().size();
  LOG(INFO) << "Registered operators:\n"
            << OpRegistry::Global()->ListAllNames();
73 74 75 76 77 78 79
  auto program = CreateAddProgram();
  // output program
  for (int i = 0; i < program.size(); i++) {
    LOG(INFO) << "instruction: " << program[i];
  }
}

M
Minner Wang 已提交
80 81 82 83 84 85 86 87 88 89
TEST(net_build, TestTransValidVarName) {
  std::string a_val_id = "A";
  std::string b_val_id = "B___";
  frontend::NetBuilder builder("net_builder");
  auto a = builder.CreateInput(Float(32), {1, 64, 112, 112}, "@A");
  auto b = builder.CreateInput(Float(32), {64}, "B/");
  EXPECT_EQ(a.id(), a_val_id);
  EXPECT_EQ(b.id(), b_val_id);
}

90 91 92 93 94 95 96 97 98 99 100 101 102
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);
103 104
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
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 140 141 142 143
  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);
144 145
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
146 147 148 149 150 151 152
  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));

153 154 155
  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()));
156
  auto fake_out_ten = scope->GetTensor(std::string(mul_out->id));
157
  auto add_out_ten = scope->GetTensor(std::string(add_out->id));
158 159 160 161 162 163 164 165 166 167 168 169 170 171
  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");
172 173 174 175
  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);
176 177 178 179 180 181 182 183 184 185 186 187 188
  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);
189 190
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  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");
212 213 214
  auto w =
      builder.CreateInput(cinn::common::BFloat16(), {K, N}, "W");    // weight
  auto b = builder.CreateInput(cinn::common::BFloat16(), {N}, "B");  // bias
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

  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);
231 232
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
233 234 235 236 237 238 239
  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));

240 241 242
  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()));
243
  auto fake_out_ten = scope->GetTensor(std::string(mul_out->id));
244
  auto add_out_ten = scope->GetTensor(std::string(add_out->id));
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  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");
260
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "Img");
261 262 263 264
  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};
265 266 267 268 269
  bool ceil_mode = false;
  bool exclusive = true;
  bool global_pooling = false;
  std::string data_format = "NCHW";
  bool adaptive = false;
270
  std::string padding_algorithm = "EXPLICIT";
271
  Variable pool_out = builder.Pool2d(input,
272 273 274 275 276 277 278 279 280 281
                                     pooling_type,
                                     ksize,
                                     strides,
                                     paddings,
                                     ceil_mode,
                                     exclusive,
                                     global_pooling,
                                     data_format,
                                     adaptive,
                                     padding_algorithm);
282
  auto program = builder.Build();
283 284 285 286 287 288 289 290 291 292

#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);
293 294
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  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");
312
  Placeholder input = builder.CreateInput(Float(32), {B, C, H, W}, "Img");
313
  Variable reverse_out = builder.Reverse(input, {2, 3});
314
  auto program = builder.Build();
315 316 317 318 319 320 321 322 323 324 325 326

#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);
327 328
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
329 330 331 332 333 334 335 336 337 338 339
  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) {
340
  const int B = 4;
341 342 343 344 345 346
  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");
347 348
  Variable output = builder.Gather(input1, input2, 1);
  auto program = builder.Build();
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);
359 360
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  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();

377
  auto output_tensor = scope->GetTensor(std::string(output->id));
378 379 380 381 382 383 384 385 386 387 388
  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;
389 390
      int index = h + H_IN2 * b;
      float in_data = input1_data[input2_data[h] + H_IN1 * b];
391 392 393 394 395 396 397 398 399
      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) {
400
  const int B = 4;
401 402 403 404 405 406
  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");
407 408
  Variable output = builder.GatherNd(input1, input2);
  auto program = builder.Build();
409 410 411 412 413 414 415 416 417 418 419

#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);
420 421
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  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();

438
  auto output_tensor = scope->GetTensor(std::string(output->id));
439 440 441 442 443 444 445 446 447 448 449 450 451
  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) {
452 453
        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;
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        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");
469 470
  Variable output = builder.Cast(input, "float");
  auto program = builder.Build();
471 472 473 474 475 476 477 478 479 480

#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);
481 482
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
483 484 485 486 487 488 489 490 491 492 493
  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();

494
  auto output_tensor = scope->GetTensor(std::string(output->id));
495 496 497 498 499 500 501 502 503 504 505
  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;
506
      int index = h + H * b;
507
      float in_data = static_cast<float>(input_data[index]);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
      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");
524 525
  Variable output = builder.Squeeze(input, {1});
  auto program = builder.Build();
526 527 528 529 530 531 532 533 534 535

#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);
536 537
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
538 539 540 541 542 543 544 545 546 547 548
  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();

549
  auto output_tensor = scope->GetTensor(std::string(output->id));
550 551 552 553 554 555 556 557 558 559 560 561 562 563
  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) {
564 565
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
          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");
584 585
  Variable output = builder.Squeeze(input, {-3});
  auto program = builder.Build();
586 587 588 589 590 591 592 593 594 595

#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);
596 597
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
598 599 600 601 602 603 604 605 606 607 608
  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();

609
  auto output_tensor = scope->GetTensor(std::string(output->id));
610 611 612 613 614 615 616 617 618 619 620 621 622 623
  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) {
624 625
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
          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");
644 645
  Variable output = builder.Squeeze(input, {1, 3});
  auto program = builder.Build();
646 647 648 649 650 651 652 653 654 655

#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);
656 657
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
658 659 660 661 662 663 664 665 666 667 668
  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();

669
  auto output_tensor = scope->GetTensor(std::string(output->id));
670 671 672 673 674 675 676 677 678 679 680 681 682
  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) {
683 684
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
          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");
703 704
  Variable output = builder.Squeeze(input, {1, -1});
  auto program = builder.Build();
705 706 707 708 709 710 711 712 713 714

#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);
715 716
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
717 718 719 720 721 722 723 724 725 726 727
  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();

728
  auto output_tensor = scope->GetTensor(std::string(output->id));
729 730 731 732 733 734 735 736 737 738 739 740 741
  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) {
742 743
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
          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");
762 763
  Variable output = builder.Squeeze(input, {});
  auto program = builder.Build();
764 765 766 767 768 769 770 771 772 773

#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);
774 775
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
776 777 778 779 780 781 782 783 784 785 786
  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();

787
  auto output_tensor = scope->GetTensor(std::string(output->id));
788 789 790 791 792 793 794 795 796 797 798 799 800
  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) {
801 802
          int index = w + W * (h + H * (c + C * b));
          float in_data = input_data[index];
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
          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");
819 820
  Variable output = builder.ArgSort(input, 0, true).at(0);
  auto program = builder.Build();
821 822 823 824 825 826 827 828 829 830

#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);
831 832
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
833 834 835 836 837 838 839 840 841 842 843
  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();

844
  auto output_tensor = scope->GetTensor(std::string(output->id));
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  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;
865
      int index = h + H * b;
866
      float true_data = sorted_data[b];
867
      float out_data = out_sorted_data[b];
868 869 870 871 872 873 874 875 876 877 878 879 880
      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");
881 882
  Variable output = builder.Sort(input, 0, true);
  auto program = builder.Build();
883 884 885 886 887 888 889 890 891 892

#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);
893 894
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
895 896 897 898 899 900 901 902 903 904 905
  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();

906
  auto output_tensor = scope->GetTensor(std::string(output->id));
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
  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;
925
      int index = h + H * b;
926
      float true_data = sorted_data[b];
927
      float out_data = output_data[index];
928 929 930 931 932 933 934 935
      line += (std::to_string(out_data) + ", ");
      EXPECT_EQ(true_data, out_data);
      VLOG(6) << line;
    }
  }
}

TEST(net_build, program_execute_arange_float) {
936 937 938
  const float start = 1.5F;
  const float stop = 31.5F;
  const float step = 2.0F;
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
  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);
954 955
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
956 957 958 959 960 961
  auto runtime_program = gc.Build();

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

  runtime_program->Execute();

962
  auto out_tensor = scope->GetTensor(std::string(out->id));
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
  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) {
978 979 980
  const float start = 1.5F;
  const float stop = 31.5F;
  const float step = 1.6F;
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
  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);
996 997
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
998 999 1000 1001 1002 1003
  auto runtime_program = gc.Build();

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

  runtime_program->Execute();

1004
  auto out_tensor = scope->GetTensor(std::string(out->id));
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  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) {
1020 1021
  const int N = 4;
  const int IN_C = 3;
1022
  const int OUT_C = 1;
1023 1024
  const int H = 7;
  const int W = 7;
1025 1026 1027

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1028 1029
  Variable output = builder.Argmax(input, 1, true);
  auto program = builder.Build();
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

#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);
1040 1041
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  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();

1066
  auto output_tensor = scope->GetTensor(std::string(output->id));
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  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) {
1082
          int index = w + W * (h + H * (c + IN_C * n));
1083 1084
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1085
          int out_data = output_data[out_index];
1086 1087
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1088
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
          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) {
1100
  const int N = 4;
1101
  const int IN_C = 3;
1102 1103
  const int H = 7;
  const int W = 7;
1104 1105 1106

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1107 1108
  Variable output = builder.Argmax(input, 1, false);
  auto program = builder.Build();
1109 1110 1111 1112 1113 1114

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

  auto scope = BuildScope(target, graph);
1115 1116
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
  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();

1141
  auto output_tensor = scope->GetTensor(std::string(output->id));
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  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) {
1156
          int index = w + W * (h + H * (c + IN_C * n));
1157 1158
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1159
          int out_data = output_data[out_index];
1160 1161
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1162
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
          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) {
1174 1175
  const int N = 4;
  const int IN_C = 3;
1176
  const int OUT_C = 1;
1177 1178
  const int H = 7;
  const int W = 7;
1179 1180 1181

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1182 1183
  Variable output = builder.Argmin(input, 1, true);
  auto program = builder.Build();
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
#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);
1194 1195
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  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();

1220
  auto output_tensor = scope->GetTensor(std::string(output->id));
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
  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) {
1236
          int index = w + W * (h + H * (c + IN_C * n));
1237 1238
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1239
          int out_data = output_data[out_index];
1240 1241
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1242
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
          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) {
1254
  const int N = 4;
1255
  const int IN_C = 3;
1256 1257
  const int H = 7;
  const int W = 7;
1258 1259 1260

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {N, IN_C, H, W}, "In");
1261 1262
  Variable output = builder.Argmin(input, 1, false);
  auto program = builder.Build();
1263 1264 1265 1266 1267 1268 1269 1270 1271
#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);
1272 1273
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
  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();

1298
  auto output_tensor = scope->GetTensor(std::string(output->id));
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  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) {
1313
          int index = w + W * (h + H * (c + IN_C * n));
1314 1315
          int out_index = w + W * (h + H * n);
          float in_data = input_data[index];
1316
          int out_data = output_data[out_index];
1317 1318
          EXPECT_LE(0, out_data);
          EXPECT_LT(out_data, IN_C);
1319
          int max_index = w + W * (h + H * (out_data + IN_C * n));
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
          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) {
1331 1332
  const int M = 4;
  const int N = 4;
1333
  const int repeats = 3;
1334
  const int axis = 0;
1335 1336 1337

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {M, N}, "In");
1338 1339
  Variable output = builder.Repeat(input, repeats, axis);
  auto program = builder.Build();
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

#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);
1350 1351
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
  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();

1363
  auto output_tensor = scope->GetTensor(std::string(output->id));
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
  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) {
1376 1377
      int in_index =
          n + N * static_cast<int>(std::floor(static_cast<float>(m) / repeats));
1378 1379
      int out_index = n + new_N * m;
      float in_data = input_data[in_index];
1380 1381 1382 1383 1384 1385 1386
      float out_data = output_data[out_index];
      EXPECT_EQ(in_data, out_data);
    }
  }
}

TEST(net_build, program_execute_repeat_axis_1) {
1387 1388
  const int M = 4;
  const int N = 4;
1389
  const int repeats = 3;
1390
  const int axis = 1;
1391 1392 1393

  NetBuilder builder("net_builder");
  Placeholder input = builder.CreateInput(Float(32), {M, N}, "In");
1394 1395
  Variable output = builder.Repeat(input, repeats, axis);
  auto program = builder.Build();
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

#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);
1406 1407
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  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();

1419
  auto output_tensor = scope->GetTensor(std::string(output->id));
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
  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) {
1432 1433
      int in_index =
          N * m + static_cast<int>(std::floor(static_cast<float>(n) / repeats));
1434 1435
      int out_index = n + new_N * m;
      float in_data = input_data[in_index];
1436 1437 1438 1439 1440 1441 1442
      float out_data = output_data[out_index];
      EXPECT_EQ(in_data, out_data);
    }
  }
}

TEST(net_build, program_execute_one_hot) {
1443 1444 1445 1446 1447 1448
  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]
1449 1450 1451
  const std::string dtype = "int32";

  NetBuilder builder("net_builder");
1452 1453
  Placeholder input = builder.CreateInput(Int(32), {M, N}, "In");
  Placeholder on_value_input = builder.CreateInput(Int(32), {1}, "OnValue");
1454
  Placeholder off_value_input = builder.CreateInput(Int(32), {1}, "OffValue");
1455 1456 1457
  Variable output = builder.OneHot(
      input, on_value_input, off_value_input, depth, axis, dtype);
  auto program = builder.Build();
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467

#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);
1468 1469
  hlir::framework::CompilationContext context(graph, scope, target);
  hlir::framework::GraphCompiler gc(context);
1470 1471 1472 1473 1474 1475 1476
  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));

1477
  auto input_tensor = scope->GetTensor(std::string(input.id()));
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
  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();

1490
  auto output_tensor = scope->GetTensor(std::string(output->id));
1491
  const std::vector<int>& output_shape = output_tensor->shape().data();
1492
  std::vector<int> output_data = GetTensorData<int>(output_tensor, target);
1493 1494 1495 1496

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

1497
  const int true_axis = axis == -1 ? M : axis;
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
  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};
1513 1514 1515
        int input_index = 0;
        int output_index = 0;
        int base = 1;
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543

        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