schedule.cc 96.4 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 47 48 49
// 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/hlir/pe/schedule.h"

#include <absl/container/flat_hash_map.h>
#include <isl/cpp.h>
#include <math.h>

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <numeric>
#include <utility>

#include "paddle/cinn/common/cas.h"
#include "paddle/cinn/hlir/pe/load_x86_params.h"
#include "paddle/cinn/optim/ir_simplify.h"
#include "paddle/cinn/poly/isl_utils.h"
#include "paddle/cinn/utils/string.h"

DECLARE_bool(cinn_use_cuda_vectorize);
namespace cinn {
namespace hlir {
namespace pe {

ScheduleParam::ScheduleParam(common::Target::Arch arch) {
  switch (arch) {
    case common::Target::Arch::X86: {
      param_data = CreateX86Params();
      break;
    }
    case common::Target::Arch::NVGPU: {
      param_data = CreateCudaParams();
      break;
    }
    default: {
50 51
      LOG(FATAL)
          << "Schedule params must be initialized with target x86 or nvgpu.";
52 53 54 55 56 57 58 59 60 61 62 63 64
    }
  }
}

ScheduleParam::~ScheduleParam() {}

int GetInnerSplitter(int origin, int other_axis) {
  if (origin <= 1) return 1;
  int two_exp = 1;
  while (origin % two_exp == 0) {
    two_exp *= 2;
  }
  two_exp = two_exp / 2;
65 66
  int a = SplitEven(two_exp);
  int b = two_exp / a;
67 68
  while (a * other_axis >= 1024 || b * other_axis >= 1024) {
    two_exp = two_exp / 2;
69 70
    a = SplitEven(two_exp);
    b = two_exp / a;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  }
  if (origin == two_exp) {
    return 2;
  }
  return origin / two_exp;
}

int SplitEven(int origin) {
  if (origin <= 1) return 1;
  int res = 1;
  while (origin % res == 0 && res * res < origin) {
    res *= 2;
  }
  res = res / 2;
  return res;
}

int GetBasicFactor(const Type &type, const common::Target &target) {
  int target_native_vector_bits = target.get_target_bits() * 8;
90
  int type_bits = type.bits();
91 92 93 94 95 96 97 98
  return target_native_vector_bits / type_bits;
}

int GetBetterSplitFactor(int shape, int split_factor) {
  int better_factor = split_factor;
  while (better_factor > shape) {
    better_factor /= 2;
  }
99 100
  if (better_factor < shape && better_factor != split_factor)
    return better_factor * 2;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  return better_factor;
}

int GetVectorizeFactor(int shape, int split_factor) {
  int better_factor = 1;
  for (int i = split_factor; i > 1; i--) {
    if (shape % i == 0) {
      better_factor = i;
      break;
    }
  }
  return better_factor;
}

void ScheduleInjectiveCPU(poly::Stage *stage,
                          const std::vector<int> &output_shape,
                          const common::Target &target,
                          bool vectorizable) {
119 120
  int dims = stage->n_out_dims();
  int factor = GetBasicFactor(stage->tensor()->type(), target);
121 122 123 124 125 126 127 128 129 130 131 132
  poly::Iterator fused = stage->axis(0);
  if (dims >= 5) {
    fused = stage->Fuse({0, 1, 2});
  } else if (dims >= 3) {
    fused = stage->Fuse({0, 1});
  }
  stage->Parallel(fused);
  dims = stage->n_out_dims();

  if (vectorizable) {
    poly::Iterator lo;
    poly::Iterator li;
133 134
    int last_shape = stage->GetDimRange(dims - 1);
    factor = GetVectorizeFactor(last_shape, factor);
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    std::tie(lo, li) = stage->Split(stage->axis(dims - 1), factor);
    stage->Vectorize(li, factor);
    if (dims == 1) {
      stage->Parallel(0);
    }
  }
}

void ScheduleInjectiveCPU1(poly::Stage *stage,
                           const std::vector<int> &output_shape,
                           const common::Target &target,
                           bool vectorizable) {
  int dims = stage->n_out_dims();
  if (dims > 1) {
149 150
    CHECK_EQ(stage->n_out_dims(), stage->n_in_dims())
        << "The dims of op are not equal";
151 152
    CHECK_EQ(stage->n_out_dims(), output_shape.size())
        << "The origin stage out dims should be same with output_shape sizes";
153
    poly::Iterator fused = stage->axis(dims - 1);
154
    int target_native_vector_bits = target.get_target_bits() * 8;
155 156 157 158 159
    int type_bits = stage->tensor()->type().bits();
    int prod_size = output_shape.back();
    // fuse conservatively for the complex index from poly and may not benefit a
    // lot compared with llvm optimization, only fuse the last two dims when the
    // last dimension is too small and can split and vectorize Todo: try reorder
160
    if (output_shape.back() * type_bits < target_native_vector_bits) {
161 162
      int last_two_dim_bits =
          output_shape[dims - 2] * output_shape[dims - 1] * type_bits;
163 164 165 166 167 168 169 170 171 172 173 174 175
      if (last_two_dim_bits % target_native_vector_bits == 0) {
        fused = stage->Fuse(dims - 2, dims - 1);
        prod_size *= output_shape[dims - 2];
      }
    }
    int split_factor = target_native_vector_bits / type_bits;
    if (vectorizable) {
      if (prod_size <= split_factor) {
        split_factor = GetBetterSplitFactor(prod_size, split_factor);
        if (split_factor >= 8) {
          stage->Vectorize(fused, split_factor);
        }
      } else {
176
        auto ssplit = stage->Split(fused, split_factor);
177 178 179 180 181 182 183 184 185 186 187
        auto &j_outer = std::get<0>(ssplit);
        auto &j_inner = std::get<1>(ssplit);
        stage->Vectorize(j_inner, split_factor);
      }
    }
  }
  if (stage->n_out_dims() > 1) {
    stage->Parallel(0);
  }
}

188 189 190 191
int GetArrayPackingFactor(int shape,
                          const Type &type,
                          const common::Target &target) {
  int split_base = GetBasicFactor(type, target);
192 193 194 195 196 197 198 199 200 201 202 203
  int split_factor = 1;
  // temporily use shape-1 instead of shape for isl wrong for1 elimination
  int i = split_base * split_base < shape ? split_base * split_base : shape;
  for (; i > 1; i--) {
    if (shape % i == 0) {
      split_factor = i;
      break;
    }
  }
  return split_factor;
}

204 205 206
void MatmulScheduleCUDA(poly::StageMap stages,
                        const ir::Tensor &output,
                        const common::Target &target) {
207 208 209 210 211 212 213 214 215 216 217 218
  stages[output]->Split(1, 2);
  stages[output]->Bind(0, "blockIdx.x");
  stages[output]->Bind(1, "threadIdx.x");
}

void MatmulScheduleCPU(poly::StageMap stages,
                       const ir::Tensor &output,
                       const ir::Tensor &packedB,
                       const common::Target &target) {
  CHECK_EQ(output->type(), packedB->type());
  int basic_split_factor = GetBasicFactor(packedB->type(), target);
  // packedB
219 220 221 222
  int packedB_dims = stages[packedB]->axis_names().size();
  int packed_last_dim = packedB->shape[packedB_dims - 1].as_int32();
  int packedB_split_factor =
      GetBetterSplitFactor(packed_last_dim, basic_split_factor);
223
  // tempory solution for indivisible case
224 225
  if (packedB_split_factor >= 8 &&
      packed_last_dim % packedB_split_factor == 0) {
226 227 228 229 230
    stages[packedB]->Vectorize(packedB_dims - 1, packedB_split_factor);
  }
  // output
  int output_size = output->shape.size();
  // M, N
231 232 233 234
  int M = output->shape[output_size - 2].as_int32();
  int N = output->shape[output_size - 1].as_int32();
  int bm = GetArrayPackingFactor(M, output->type(), target);
  int bn = GetArrayPackingFactor(N, output->type(), target);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  int out_axis_dims = stages[output]->axis_names().size();
  CHECK_GE(out_axis_dims, 3U) << "output tensor's size should be at least 3";
  poly::Iterator i_axis = stages[output]->axis(out_axis_dims - 3);
  poly::Iterator j_axis = stages[output]->axis(out_axis_dims - 2);
  poly::Iterator i_outer, i_inner, j_outer, j_inner;
  std::vector<poly::Iterator> i_axes, j_axes, k_axes;
  std::vector<poly::Iterator> all_axes;
  std::vector<poly::Iterator> all_axes_outer;
  std::vector<poly::Iterator> all_axes_inner;
  bool is_m_splited = false;
  bool is_n_splited = false;
  // tempory solution for isl for1 wrong elimination
  if (bm >= 4 && M != bm) {
    auto axes = stages[output]->Split(i_axis, bm);
    all_axes_outer.push_back(std::get<0>(axes));
    all_axes_inner.push_back(std::get<1>(axes));
    is_m_splited = true;
  } else {
    all_axes_outer.push_back(i_axis);
  }
  out_axis_dims = stages[output]->axis_names().size();
  // temp solution for isl for1 wrong elimination
  if (bn >= 4 && N != bn) {
    auto axes = stages[output]->Split(j_axis, bn);
    all_axes_outer.push_back(std::get<0>(axes));
    all_axes_inner.push_back(std::get<1>(axes));
    is_n_splited = true;
  } else {
    all_axes_outer.push_back(j_axis);
  }
  // K
266
  int K = packedB->shape[packedB->shape.size() - 2].as_int32();
267
  int k_split_factor = GetBetterSplitFactor(K, basic_split_factor);
268 269 270
  out_axis_dims = stages[output]->axis_names().size();
  auto k_axis = stages[output]->axis(out_axis_dims - 1);
  bool is_k_splited = false;
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  if (k_split_factor >= 4) {
    auto axes = stages[output]->Split(k_axis, k_split_factor);
    k_axes.push_back(std::get<0>(axes));
    k_axes.push_back(std::get<1>(axes));
    all_axes_outer.push_back(std::get<0>(axes));
    all_axes_inner.push_back(std::get<1>(axes));
    is_k_splited = true;
  } else {
    all_axes_outer.push_back(k_axis);
  }
  std::vector<poly::Iterator> new_order;
  out_axis_dims = stages[output]->axis_names().size();
  if (output_size > 2) {
    // batch
    all_axes.push_back(stages[output]->axis(0));
  }
  for (int i = 0; i < all_axes_outer.size(); ++i) {
    all_axes.push_back(all_axes_outer[i]);
  }
  for (int i = 0; i < all_axes_inner.size(); ++i) {
    all_axes.push_back(all_axes_inner[i]);
  }
  // int axies
  CHECK_EQ(all_axes.size(), out_axis_dims);
  if (is_k_splited) {
    if (is_m_splited || is_n_splited) {
      // swap k_inner and j_inner/i_inner
      std::swap(all_axes[out_axis_dims - 1], all_axes[out_axis_dims - 2]);
    } else {
      // swap k_inner and j
      std::swap(all_axes[out_axis_dims - 1], all_axes[out_axis_dims - 3]);
    }
  } else {
    // swap k and j
    std::swap(all_axes[out_axis_dims - 1], all_axes[out_axis_dims - 2]);
  }
  stages[output]->Reorder(all_axes);
  // vectorize output's last dimemsion
  auto out_domain = stages[output]->transformed_domain();
310 311 312 313
  auto range =
      poly::isl_set_get_axis_range(out_domain.get(), out_axis_dims - 1);
  auto &min = std::get<0>(range);
  auto &max = std::get<1>(range);
314
  CHECK_EQ(min.get_num_si(), 0) << "axis range should begin from zero";
315 316 317
  int out_last_dim = max.get_num_si() + 1;
  int output_split_factor =
      GetBetterSplitFactor(out_last_dim, basic_split_factor);
318 319 320 321 322 323 324 325 326 327
  // tempory solution for indivisible case
  if (output_split_factor >= 8 && packed_last_dim % output_split_factor == 0) {
    stages[output]->Vectorize(out_axis_dims - 1, output_split_factor);
  }
}

void MulScheduleCPU(poly::StageMap stages,
                    const ir::Tensor &output,
                    const ir::Tensor &reduce_first,
                    const common::Target &target) {
328 329
  int split_factor = GetBasicFactor(output->type(), target);
  auto out_reduce_axis = output->reduce_axis;
330
  std::vector<Expr> reduce_first_shape = reduce_first->shape;
331
  std::vector<Expr> output_shape = output->shape;
332 333 334 335
  CHECK_EQ(reduce_first_shape.size(), 3U);
  CHECK_EQ(output_shape.size(), 2U);

  // reduce_first init
336
  auto reduce_first_init = reduce_first->GetInitTensor(stages, target);
337
  int reduce_first_init_dim = stages[reduce_first_init]->axis_names().size();
338 339
  stages[reduce_first_init]->ComputeAt2(stages[reduce_first],
                                        reduce_first_init_dim - 2);
340
  // output init
341
  auto out_init = output->GetInitTensor(stages, target);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  int out_init_dim = stages[out_init]->axis_names().size();
  stages[out_init]->ComputeAt2(stages[output], out_init_dim - 1);
  // reduce_first
  int reduce_first_dim = stages[reduce_first]->axis_names().size();
  stages[reduce_first]->Reorder({reduce_first_dim - 1, reduce_first_dim - 2});
  int reduce_first_last_shape = reduce_first_shape.back().as_int32();
  // output
  int out_dims = stages[output]->n_out_dims();
  if (reduce_first_last_shape > 1) {
    stages[output]->Unroll(out_dims - 1);
  }
}

int GetThreadBindAxis(const std::vector<ir::Expr> &shape) {
  int thread_axis = shape.size() - 1;
  for (int idx = thread_axis; idx >= 0; --idx) {
    if (shape[idx].as_int32() > 1) {
      thread_axis = idx;
      break;
    }
  }
  return thread_axis;
}

366 367
int GetBlockBindAxis(const std::vector<ir::Expr> &shape,
                     const int thread_axis) {
368 369 370 371 372
  int block_axis = 0, max_dim_size = shape[0].as_int32();
  for (int idx = 0; idx <= thread_axis; ++idx) {
    if (max_dim_size < shape[idx].as_int32()) {
      if (idx < thread_axis) {
        max_dim_size = shape[idx].as_int32();
373
        block_axis = idx;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
      } else {
        if (max_dim_size == 1) {
          block_axis = thread_axis;
        }
      }
    }
  }
  return block_axis;
}

void CudaReduceSchedule(poly::StageMap stages,
                        ir::Tensor output,
                        int last_dimension_num,
                        const common::Target &target) {
  int parallel_thread_num = 1;
389 390 391
  for (int idx = output->shape.size() - 1;
       idx >= static_cast<int>(output->shape.size()) - last_dimension_num;
       --idx) {
392 393 394 395
    parallel_thread_num *= output->shape[idx].as_int32();
  }

  int index = output->shape.size() - last_dimension_num;
396 397 398
  for (int idx = output->shape.size() - last_dimension_num;
       idx < static_cast<int>(output->shape.size()) - 1;
       ++idx) {
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    stages[output]->Fuse(index, index + 1);
  }

  int max_block_size = 1024;
  if (parallel_thread_num > max_block_size) {
    stages[output]->Split(index, max_block_size);
    stages[output]->Bind(index + 1, "threadIdx.x");
  } else {
    stages[output]->Bind(index, "threadIdx.x");
  }

  for (int idx = 0; idx < index - 1; ++idx) {
    stages[output]->Fuse(0, 1);
  }

  if (index > 0) {
    stages[output]->Bind(0, "blockIdx.x");
  }
}

419 420 421 422
void CudaWarpReduceSchedule(poly::StageMap stages,
                            ir::Tensor tmp_out,
                            ir::Tensor out,
                            const common::Target &target) {
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
  int sum_out_dim = 1;
  for (int idx = 0; idx < static_cast<int>(tmp_out->shape.size()) - 2; ++idx) {
    stages[out]->Fuse(0, 1);
    stages[tmp_out]->Fuse(0, 1);
    sum_out_dim *= out->shape[idx].as_int32();
  }
  sum_out_dim *= out->shape.back().as_int32();

  // out_shape = {1} tmp_shape = {32}
  if (tmp_out->shape.size() == 1) {
    stages[out]->Split(0, 1);
    stages[tmp_out]->Split(0, tmp_out->shape[0].as_int32());
  }

  if (sum_out_dim <= 16) {
    stages[out]->Bind(0, "threadIdx.y");

    stages[tmp_out]->Bind(0, "threadIdx.y");
    stages[tmp_out]->Bind(1, "threadIdx.x");
    stages[tmp_out]->SimpleComputeAt(stages[out], 0);
    stages[tmp_out]->SetBuffer("local");
  } else {
    stages[out]->Split(0, 8);
    stages[out]->Bind(0, "blockIdx.x");
    stages[out]->Bind(1, "threadIdx.y");

    stages[tmp_out]->Split(0, 8);
    stages[tmp_out]->Bind(2, "threadIdx.x");
    stages[tmp_out]->SimpleComputeAt(stages[out], 1);
    stages[tmp_out]->SetBuffer("local");
  }
}

void CudaBlockReduceInternalSchedule(poly::StageMap stages,
                                     ir::Tensor tmp_out,
                                     ir::Tensor out,
                                     const common::Target &target) {
  for (int idx = 0; idx < static_cast<int>(tmp_out->shape.size()) - 2; ++idx) {
    stages[tmp_out]->Fuse(0, 1);
    stages[out]->Fuse(0, 1);
  }

  if (tmp_out->shape.size() == 1) {
    stages[tmp_out]->Split(0, stages[tmp_out]->GetDimRange(0));
    stages[out]->Split(0, stages[out]->GetDimRange(0));
  }

  stages[tmp_out]->Bind(0, "blockIdx.x");
  stages[tmp_out]->Bind(1, "threadIdx.x");
  stages[tmp_out]->SetBuffer("local");
  stages[tmp_out]->SimpleComputeAt(stages[out], 0);

  stages[out]->Bind(0, "blockIdx.x");
}

void CudaBlockReduceSchedule(poly::StageMap stages,
                             ir::Tensor reduce_tmp_out,
                             ir::Tensor tmp_out,
                             ir::Tensor out,
                             const common::Target &target) {
  int output_shape_size_without_reduce = tmp_out->shape.size() - 1;
  // fuse last parallel dimension
485 486 487 488
  for (int idx = 0; idx < reduce_tmp_out->shape.size() - tmp_out->shape.size();
       ++idx) {
    stages[reduce_tmp_out]->Fuse(output_shape_size_without_reduce,
                                 output_shape_size_without_reduce + 1);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  }

  // fuse parallel dimension
  for (int idx = 0; idx < output_shape_size_without_reduce - 1; ++idx) {
    stages[out]->Fuse(0, 1);
    stages[tmp_out]->Fuse(0, 1);
    stages[reduce_tmp_out]->Fuse(0, 1);
  }

  if (tmp_out->shape.size() == 1) {
    stages[reduce_tmp_out]->Split(0, stages[reduce_tmp_out]->GetDimRange(0));
    stages[tmp_out]->Split(0, stages[tmp_out]->GetDimRange(0));
    stages[out]->Split(0, stages[out]->GetDimRange(0));
  }

  stages[reduce_tmp_out]->Bind(0, "blockIdx.x");
  stages[reduce_tmp_out]->Bind(1, "threadIdx.x");
  stages[reduce_tmp_out]->SetBuffer("local");
  stages[reduce_tmp_out]->SimpleComputeAt(stages[tmp_out], 0);

  stages[tmp_out]->Bind(0, "blockIdx.x");
  stages[tmp_out]->Bind(1, "threadIdx.x");
  stages[tmp_out]->SetBuffer("local");
  stages[tmp_out]->SimpleComputeAt(stages[out], 0);

  stages[out]->Bind(0, "blockIdx.x");
}

517 518 519 520 521
void CudaBlockShuffleReduceSchedule(poly::StageMap stages,
                                    ir::Tensor reshape,
                                    ir::Tensor internal,
                                    ir::Tensor out,
                                    const common::Target &target) {
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  int fuse_times = internal->shape.size() - 2;
  for (int idx = 0; idx < fuse_times; ++idx) {
    stages[internal]->Fuse(0, 1);
    stages[out]->Fuse(0, 1);
  }

  fuse_times = out->shape.size() - internal->shape.size();
  for (int idx = 0; idx < fuse_times; ++idx) {
    if (internal->shape.size() == 1) {
      stages[out]->Fuse(0, 1);
    } else {
      stages[out]->Fuse(1, 2);
    }
  }

  if (stages[out]->n_out_dims() == 1) {
    stages[internal]->Split(0, stages[internal]->GetDimRange(0));
    stages[out]->Split(0, stages[out]->GetDimRange(0));
  }

  stages[reshape]->ComputeInline();
  stages[internal]->SetBuffer("shared");

  stages[internal]->Bind(0, "blockIdx.x");
  stages[internal]->Bind(1, "threadIdx.x");

  stages[out]->Bind(0, "blockIdx.x");
  stages[out]->Bind(1, "threadIdx.x");
  stages[internal]->SimpleComputeAt(stages[out], 0);

  stages[out]->SyncThreads(0, {internal}, stages);
}

void CudaTwoStepReduceSchedule(poly::StageMap stages,
                               ir::Tensor reshape,
                               ir::Tensor internal,
                               ir::Tensor tmp_out,
                               ir::Tensor out,
                               const common::Target &target) {
  // fuse axis
  for (int idx = 0; idx < static_cast<int>(internal->shape.size()) - 2; ++idx) {
    stages[internal]->Fuse(0, 1);
    stages[tmp_out]->Fuse(0, 1);
    stages[out]->Fuse(0, 1);
  }

  if (stages[tmp_out]->n_out_dims() == 1) {
    stages[internal]->Split(0, stages[internal]->GetDimRange(0));
    stages[tmp_out]->Split(0, stages[tmp_out]->GetDimRange(0));
    stages[out]->Split(0, stages[out]->GetDimRange(0));
  }

  stages[reshape]->ComputeInline();

  stages[internal]->Bind(0, "blockIdx.x");
  stages[internal]->Bind(1, "threadIdx.x");
  stages[internal]->SetBuffer("local");
  stages[internal]->SimpleComputeAt(stages[tmp_out], 0);

  stages[tmp_out]->Bind(0, "blockIdx.x");
  stages[tmp_out]->Bind(1, "threadIdx.x");
  stages[tmp_out]->SetBuffer("local");
  stages[tmp_out]->SimpleComputeAt(stages[out], 0);

  stages[out]->Bind(0, "blockIdx.x");
}

589 590 591 592
void SoftmaxScheduleCPU(poly::StageMap stage,
                        const ir::Tensor &output,
                        const ir::Tensor &temp,
                        int axis) {
593 594 595 596 597 598 599 600 601 602 603 604
  if (axis == -1) {
    axis += output->shape.size();
  }
  poly::Iterator fused = stage[output]->axis(0);
  stage[output]->Parallel(fused);
  for (int i = 1; i < axis; i++) {
    fused = stage[output]->Fuse(0, 1);
  }
  CHECK_GT(stage[output]->n_out_dims(), 1);
  stage[temp]->ComputeAt(stage[output], 0);
}

605 606 607 608
void GlobalPoolScheduleGPU(poly::StageMap stages,
                           const std::vector<ir::Tensor> &output,
                           const common::Target &target) {
  auto &out = output[0];
609 610 611 612 613 614 615 616 617
  auto &reduce = output[1];
  stages[out]->Fuse(0, 1);
  stages[out]->Split(0, 32);
  stages[out]->Bind(0, "blockIdx.x");
  stages[out]->Bind(1, "threadIdx.y");
  stages[reduce]->ComputeAt2(stages[out], 1);
  stages[reduce]->SetBuffer("local");
  stages[reduce]->Bind(2, "threadIdx.x");
}
618 619 620
void PoolScheduleCPU(poly::StageMap stages,
                     const ir::Tensor &output,
                     const common::Target &target) {
621 622 623 624 625
  CHECK_GE(stages[output]->n_out_dims(), 2);
  stages[output]->Fuse({0, 1});
  stages[output]->Parallel(0);
}

626 627 628
void PoolScheduleGPU(poly::StageMap stages,
                     ir::Tensor &output,
                     const common::Target &target) {
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  CHECK_GE(stages[output]->axis_names().size(), 4);
  stages[output]->Fuse({0, 1, 2, 3});
  stages[output]->Split(0, 1024);
  stages[output]->Bind(0, "blockIdx.x");
  stages[output]->Bind(1, "threadIdx.x");
}

void GetConv2dFactors(absl::flat_hash_map<std::string, int> *factors,
                      int oc,
                      int ic,
                      int fc,
                      int oh,
                      int ow,
                      const Type &type,
                      const common::Target &target,
                      const std::string &key,
                      bool import_params) {
  if (import_params) {
    auto &params = ScheduleParam::get_x86_instance().GetParam();
    if (params.count(key)) {
      VLOG(3) << "find saved param, key is: " << key;
      CHECK(!params[key]["oc_bn"].empty());
      CHECK(!params[key]["ic_bn"].empty());
      CHECK(!params[key]["ow_bn"].empty());
      (*factors)["oc_bn"] = params[key]["oc_bn"].back();
      (*factors)["ic_bn"] = params[key]["ic_bn"].back();
      (*factors)["ow_bn"] = params[key]["ow_bn"].back();
      if (!params[key]["oh_bn"].empty()) {
        (*factors)["oh_bn"] = params[key]["oh_bn"].back();
      }
      if (!params[key]["unroll_kw"].empty()) {
        (*factors)["unroll_kw"] = params[key]["unroll_kw"].back();
      }
      if (ic == fc) {
        (*factors)["fc_bn"] = (*factors)["ic_bn"];
      } else {
        int fc_bn = 1;
        for (int i = (*factors)["oc_bn"]; i > 1; i--) {
          if (fc < 1) break;
          if (fc % i == 0) {
            fc_bn = i;
            break;
          }
        }
        (*factors)["fc_bn"] = fc_bn;
      }
      return;
    } else {
      VLOG(3) << "Can not find saved param, key is: " << key;
    }
  }
  int bn_base = GetBasicFactor(type, target);
681
  int oc_bn = 1;
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  for (int i = bn_base; i > 1; i--) {
    if (oc < 1) break;
    if (oc % i == 0) {
      oc_bn = i;
      break;
    }
  }
  int ic_bn = 1;
  for (int i = oc_bn; i > 1; i--) {
    if (ic < 1) break;
    if (ic % i == 0) {
      ic_bn = i;
      break;
    }
  }
  int fc_bn = 1;
  for (int i = oc_bn; i > 1; i--) {
    if (fc < 1) break;
    if (fc % i == 0) {
      fc_bn = i;
      break;
    }
  }
  (*factors)["oc_bn"] = oc_bn;
  (*factors)["ic_bn"] = ic_bn;
  (*factors)["fc_bn"] = fc_bn;
708
  int ow_bn = 1;
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

  if (oh < 1) {
    for (int i = bn_base; i > 1; i--) {
      if (ow < 1) break;
      if (ow % i == 0) {
        ow_bn = i;
        break;
      }
    }
    (*factors)["ow_bn"] = ow_bn;
  } else {
    int oh_bn = 1;
    int begin = std::min(ow, bn_base);
    for (int i = begin; i >= 1; i--) {
      if (ow < 1) break;
      if (ow % i == 0) {
        ow_bn = i;
        for (int j = oh; j >= 1; j--) {
          if (oh % j == 0 && j * ow_bn <= 16) {
728
            oh_bn = j;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
            (*factors)["oh_bn"] = oh_bn;
            (*factors)["ow_bn"] = ow_bn;
            return;
          }
        }
      }
    }
  }
}

void GetConv2d1x1Factors(absl::flat_hash_map<std::string, int> *factors,
                         int oc,
                         int ic,
                         int oh,
                         int ow,
                         const Type &type,
                         const common::Target &target) {
  int bn_base = GetBasicFactor(type, target);
747
  int oc_bn = 1;
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
  for (int i = bn_base; i > 1; i--) {
    if (oc < 1) break;
    if (oc % i == 0) {
      oc_bn = i;
      break;
    }
  }
  int ic_bn = 1;
  for (int i = oc_bn; i > 1; i--) {
    if (ic < 1) break;
    if (ic % i == 0) {
      ic_bn = i;
      break;
    }
  }
  (*factors)["oc_bn"] = oc_bn;
  (*factors)["ic_bn"] = ic_bn;
765 766 767
  int ow_bn = 1;
  int oh_bn = 1;
  int begin = std::min(ow, bn_base);
768 769 770 771 772 773
  for (int i = begin; i >= 1; i--) {
    if (ow < 1) break;
    if (ow % i == 0) {
      ow_bn = i;
      for (int j = oh; j >= 1; j--) {
        if (oh % j == 0 && j * ow_bn <= 16) {
774
          oh_bn = j;
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
          (*factors)["oh_bn"] = oh_bn;
          (*factors)["ow_bn"] = ow_bn;
          return;
        }
      }
    }
  }
}

std::string GenerateX86ConvKey(const std::vector<Expr> &input_shape,
                               const std::vector<Expr> &weight_shape,
                               const std::vector<int> &strides,
                               const std::vector<int> &paddings,
                               const std::vector<int> &dilations,
                               const int &index,
                               const std::string &model_name) {
791 792 793
  // format: (model_name + index +)schedule_name + input_shape + weight_shape +
  // strides + paddings + dilations e.g. resnet18 0 X86ScheduleConv input 1 3
  // 224 224 weight 64 3 7 7 stride 2 2 padding 3 3 dilation 1 1
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  std::string key;
  if (model_name != "") {
    key = model_name + " index " + std::to_string(index) + " ";
  }
  key += "X86ScheduleConv input";
  for (auto &shape : input_shape) {
    key += " " + std::to_string(shape.as_int32());
  }
  key += " weight";
  for (auto &shape : weight_shape) {
    key += " " + std::to_string(shape.as_int32());
  }
  key += " stride";
  for (auto &stride : strides) {
    key += " " + std::to_string(stride);
  }
  key += " padding";
  for (auto &padding : paddings) {
    key += " " + std::to_string(padding);
  }
  key += " dilation";
  for (auto &dilation : dilations) {
    key += " " + std::to_string(dilation);
  }
  VLOG(3) << "key: " << key;
  return key;
}

std::string GenerateX86ConvKey(const std::vector<int> &input_shape,
                               const std::vector<int> &weight_shape,
                               const std::vector<int> &strides,
                               const std::vector<int> &paddings,
                               const std::vector<int> &dilations,
                               const int &index,
                               const std::string &model_name) {
829 830
  // format: (model_name + index +)schedule_name + input_shape + weight_shape +
  // strides + paddings + dilations
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
  std::string key;
  if (model_name != "") {
    key = model_name + " index " + std::to_string(index) + " ";
  }
  key += "X86ScheduleConv input";
  for (auto &shape : input_shape) {
    key += " " + std::to_string(shape);
  }
  key += " weight";
  for (auto &shape : weight_shape) {
    key += " " + std::to_string(shape);
  }
  key += " stride";
  for (auto &stride : strides) {
    key += " " + std::to_string(stride);
  }
  key += " padding";
  for (auto &padding : paddings) {
    key += " " + std::to_string(padding);
  }
  key += " dilation";
  for (auto &dilation : dilations) {
    key += " " + std::to_string(dilation);
  }
  VLOG(3) << "key: " << key;
  return key;
}

void CreateX86SerialData(const std::string &file_name) {
  /** The format of serial data is:
861 862
   * hash_key: schedule_name + shape of input + shape of weights + stride +
   * padding + dilation value: vector of params
863 864 865 866 867 868 869 870 871 872 873 874 875
   */
  SaveSerialData(CreateX86Params(), file_name);
}

void Conv2d_NCHWc_1X1_Schedule_CPU(poly::StageMap stages,
                                   const ir::Tensor &res,
                                   ir::Tensor &packed_out,
                                   const ir::Tensor &input_pad,
                                   const ir::Tensor &weights_dilation,
                                   const ir::Tensor &data,
                                   const common::Target &target,
                                   const std::string &key,
                                   bool do_padding) {
876 877
  CHECK(target.arch == Target::Arch::X86)
      << "Conv2d_NCHWc_1X1_Schedule_CPU schedule only used in x86";
878 879 880 881
  CHECK(packed_out.defined());
  CHECK(input_pad.defined());
  auto type = packed_out->type();
  absl::flat_hash_map<std::string, int> conv2d_factors;
882 883 884 885 886 887
  CHECK_EQ(packed_out->shape.size(), 5U)
      << "packed_out's shape size should be 5";
  Expr h_out = common::AutoSimplify(packed_out->shape[2]);
  Expr w_out = common::AutoSimplify(packed_out->shape[3]);
  int oh = h_out.as_int32();
  int ow = w_out.as_int32();
888 889 890 891 892 893 894
  int basic_split_factor = GetBasicFactor(type, target);
  GetConv2dFactors(&conv2d_factors, -1, -1, -1, oh, ow, type, target, key);
  int oh_bn_size = conv2d_factors["oh_bn"];
  int ow_bn_size = conv2d_factors["ow_bn"];

  auto input_shape = input_pad->shape;
  CHECK_EQ(input_shape.size(), 5U) << "input shape size should be 5";
895 896
  Expr oc_bn = common::AutoSimplify(packed_out->shape.back());
  Expr ic_bn = common::AutoSimplify(input_shape.back());
897 898 899 900 901 902 903 904 905
  int oc_bn_size = oc_bn.as_int32();
  int ic_bn_size = ic_bn.as_int32();
  VLOG(3) << "oh_bn_size " << oh_bn_size;
  VLOG(3) << "ow_bn_size " << ow_bn_size;
  VLOG(3) << "oc_bn_size " << oc_bn_size;
  VLOG(3) << "ic_bn_size " << ic_bn_size;

  // data
  if (data.defined()) {
906 907
    CHECK_GE(stages[data]->n_out_dims(), 3U)
        << "data's out_dims should be more than 3";
908 909 910 911 912
    stages[data]->Fuse({0, 1, 2});
    stages[data]->ComputeInline();
  }
  // input_pad
  if (do_padding) {
913 914
    CHECK_GE(stages[input_pad]->n_out_dims(), 3U)
        << "input_pad's out_dims should be more than 3";
915
    stages[input_pad]->Fuse({0, 1, 2});
916 917
    stages[input_pad]->Vectorize(stages[input_pad]->n_out_dims() - 1,
                                 input_pad->shape.back().as_int32());
918 919 920 921 922 923
  } else {
    stages[input_pad]->ComputeInline();
  }

  // weights
  if (weights_dilation.defined()) {
924 925 926 927
    CHECK_GE(stages[weights_dilation]->n_out_dims(), 3U)
        << "weights_dilation's out_dims should be more than 3";
    // oc_outer, ic_outer, oh, ow, ic_inner, oc_inner -> oc_outer, oh, ic_outer,
    // ow, ic_inner, oc_inner
928 929 930 931 932 933 934 935 936 937 938 939 940
    stages[weights_dilation]->Reorder({2, 1});
    stages[weights_dilation]->Fuse({0, 1});
  }

  // packed_out
  auto CC = stages[packed_out]->CacheWrite("global", stages, packed_out);
  // packed_out: [batch, oc_outer, oh, ow, oc_inner]
  // split oh, ow
  stages[packed_out]->Split(2, oh_bn_size);
  stages[packed_out]->Split(4, ow_bn_size);
  // [batch, oc_outer, oh_outer, oh_inner, ow_outer, ow_inner, oc_inner] ->
  // [batch_oc_outer_oh_outer_fused, oh_inner, ow_outer, ow_inner, oc_inner]
  stages[packed_out]->Fuse({0, 1, 2});
941 942
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
943

944 945
  // CC: [batch, oh, ow, oc, ic, kh, kw] -> [batch_oc_outer_oh_outer_fused,
  // oh_inner, ow, oc_inner, ic, kh, kw]
946 947 948
  stages[CC]->ComputeAt2(stages[packed_out], 0);
  VLOG(3) << "cache write shape: " << utils::Join(CC->shape, ", ");
  // tempory solution because reorder may be wrong before ComputeAt
949 950 951
  // reorder: [batch_oc_outer_oh_outer_fused, oh_inner, ow_outer, ow_inner,
  // oc_inner] -> [batch_oc_outer_oh_outer_fused, ow_outer, oh_inner, ow_inner,
  // oc_inner]
952
  stages[packed_out]->Reorder({2, 1});
953 954 955 956 957 958
  stages[packed_out]->Vectorize(stages[packed_out]->n_out_dims() - 1,
                                packed_out->shape.back().as_int32());
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
959 960 961 962

  // CC: [batch_oc_outer_oh_outer_fused, oh_inner, ow, oc_inner, ic, kh, kw]
  // split ow
  stages[CC]->Split(2, ow_bn_size);
963 964 965
  // reorder: [batch_oc_outer_oh_outer_fused, oh_inner, ow_outer, ow_inner,
  // oc_inner, ic, kh, kw] -> [batch_oc_outer_oh_outer_fused, oh_inner,
  // ow_outer, ow_inner, oc_inner, ic, kh, kw]
966 967 968
  stages[CC]->Reorder({2, 1});

  // split ic
969 970
  // CC: [batch_oc_outer_oh_outer_fused, ow_outer, oh_inner, ow_inner, oc_inner,
  // ic, kh, kw]
971
  stages[CC]->Split(5, ic_bn_size);
972 973 974
  // reorder: [batch_oc_outer_oh_outer_fused, ow_outer, oh_inner, ow_inner,
  // oc_inner, ic_outer, ic_inner, kh, kw] -> [batch_oc_outer_oh_outer_fused,
  // ow_outer, ic_outer, ic_inner, oh_inner, ow_inner, oc_inner, kh, kw]
975 976 977 978 979 980
  auto oh_inner = stages[CC]->axis(2);
  auto ow_inner = stages[CC]->axis(3);
  auto oc_inner = stages[CC]->axis(4);
  auto ic_outer = stages[CC]->axis(5);
  auto ic_inner = stages[CC]->axis(6);
  stages[CC]->Reorder({ic_outer, ic_inner, oh_inner, ow_inner, oc_inner});
981 982 983 984
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
  stages[CC]->Vectorize(stages[CC]->n_out_dims() - 3,
                        CC->shape.back().as_int32());
985 986 987 988
  // unroll ow_inner, oh_inner
  VLOG(3) << stages[CC]->transformed_domain();
  // CC_init
  auto CC_init = CC->GetInitTensor(stages, target);
989 990
  stages[CC_init]->Vectorize(stages[CC_init]->n_out_dims() - 1,
                             CC_init->shape.back().as_int32());
991 992 993 994 995 996 997 998 999 1000
  stages[CC]->Unroll(stages[CC]->n_out_dims() - 4);
  stages[CC]->Unroll(stages[CC]->n_out_dims() - 5);
  stages[CC_init]->Unroll(stages[CC_init]->n_out_dims() - 2);

  // res
  // n, oc, oh, ow
  if (res.defined()) {
    stages[res]->Split(1, oc_bn_size);
    stages[res]->Split(3, oh_bn_size);
    stages[res]->Split(5, ow_bn_size);
1001 1002
    // reorder: [n, oc_outer, oc_inner, oh_outer, oh_inner, ow_outer, ow_inner]
    // -> [n, oc_outer, oh_outer, ow_outer, oh_inner, ow_inner, oc_inner]
1003 1004 1005 1006 1007
    auto oc_inner1 = stages[res]->axis(2);
    auto oh_outer1 = stages[res]->axis(3);
    auto oh_inner1 = stages[res]->axis(4);
    auto ow_outer1 = stages[res]->axis(5);
    auto ow_inner1 = stages[res]->axis(6);
1008 1009
    stages[res]->Reorder(
        {oh_outer1, ow_outer1, oh_inner1, ow_inner1, oc_inner1});
1010 1011 1012
    // stages[res]->Fuse({0, 1, 2});
    // Todo: computeAt according to forloops' range
    // stages[packed_out]->ComputeAt2(stages[res], 2);
1013 1014
    VLOG(3) << "stages[res]->transformed_domain()"
            << stages[res]->transformed_domain();
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  }
}

void Conv2d_NCHWc_1X1_Schedule_CPU_Nofuse(poly::StageMap stages,
                                          const ir::Tensor &res,
                                          ir::Tensor &packed_out,
                                          const ir::Tensor &input_pad,
                                          const ir::Tensor &weights_dilation,
                                          const ir::Tensor &data,
                                          const common::Target &target) {
1025 1026
  CHECK(target.arch == Target::Arch::X86)
      << "Conv2d_NCHWc_1X1_Schedule_CPU_Nofuse schedule only used in x86";
1027 1028 1029 1030
  CHECK(packed_out.defined());
  CHECK(input_pad.defined());
  auto type = packed_out->type();
  absl::flat_hash_map<std::string, int> conv2d_factors;
1031 1032 1033 1034 1035 1036
  CHECK_EQ(packed_out->shape.size(), 5U)
      << "packed_out's shape size should be 5";
  Expr h_out = common::AutoSimplify(packed_out->shape[2]);
  Expr w_out = common::AutoSimplify(packed_out->shape[3]);
  int oh = h_out.as_int32();
  int ow = w_out.as_int32();
1037 1038 1039 1040 1041 1042
  int basic_split_factor = GetBasicFactor(type, target);
  GetConv2d1x1Factors(&conv2d_factors, -1, -1, oh, ow, type, target);
  int oh_bn_size = conv2d_factors["oh_bn"];
  int ow_bn_size = conv2d_factors["ow_bn"];

  auto input_shape = input_pad->shape;
1043
  int shape_size = input_shape.size();
1044
  CHECK_EQ(shape_size, 5U) << "input shape size should be 5";
1045 1046
  Expr oc_bn = common::AutoSimplify(packed_out->shape.back());
  Expr ic_bn = common::AutoSimplify(input_shape.back());
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  int oc_bn_size = oc_bn.as_int32();
  int ic_bn_size = ic_bn.as_int32();
  VLOG(3) << "ow_bn_size" << ow_bn_size;
  VLOG(3) << "oc_bn_size" << oc_bn_size;
  VLOG(3) << "ic_bn_size" << ic_bn_size;

  // data
  if (data.defined()) {
    stages[data]->ComputeInline();
  }
  // weights
  if (weights_dilation.defined()) {
1059 1060
    CHECK_GE(stages[weights_dilation]->n_out_dims(), 3U)
        << "weights_dilation's out_dims should be more than 3";
1061 1062 1063 1064 1065 1066 1067
    // Reorder: [oc_outer, ic_outer, oh, ow, ic_inner, oc_inner] ->
    // [oc_outer, oh, ic_outer, ow, ic_inner, oc_inner]
    stages[weights_dilation]->Reorder({2, 1});
  }

  // packed_out
  auto CC = stages[packed_out]->CacheWrite("global", stages, packed_out);
1068 1069 1070 1071
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1072 1073 1074 1075 1076 1077
  // packed_out: [batch, oc_outer, oh, ow, oc_inner]
  // split oh, ow
  stages[packed_out]->Split(2, oh_bn_size);
  stages[packed_out]->Split(4, ow_bn_size);

  // CC: [batch, oc_outer, oh, ow, oc_inner]
1078 1079
  // packed_out: [batch, oc_outer, oh_outer, oh_inner, ow_outer, ow_inner,
  // oc_inner]
1080
  stages[CC]->ComputeAt2(stages[packed_out], 2);
1081 1082 1083 1084
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1085
  // tempory solution because reordering before computeAt may be wrong
1086 1087 1088
  // reorder: [batch, oc_outer, oh_outer, oh_inner, ow_outer, ow_inner,
  // oc_inner] -> [batch, oc_outer, oh_outer, ow_outer, oh_inner, ow_inner,
  // oc_inner]
1089
  stages[packed_out]->Reorder({4, 3});
1090 1091
  stages[packed_out]->Vectorize(stages[packed_out]->n_out_dims() - 1,
                                packed_out->shape.back().as_int32());
1092 1093 1094 1095

  // split oh, ow
  // CC: [batch, oc_outer, oh_outer, oh_inner, ow, oc_inner, ic, kh, kw]
  stages[CC]->Split(4, ow_bn_size);
1096 1097
  // CC: [batch, oc_outer, oh_outer, oh_inner, ow_outer, ow_inner, oc_inner, ic,
  // kh, kw] split ic
1098 1099
  stages[CC]->Split(7, ic_bn_size);

1100 1101 1102
  // reorder: [batch, oc_outer, oh_outer, oh_inner, ow_outer, ow_inner,
  // oc_inner, ic_outer, ic_inner, kh, kw] -> [batch, oc_outer, oh_outer,
  // ow_outer, ic_outer, ic_inner, oh_inner, ow_inner, oc_inner, kh, kw]
1103 1104 1105 1106 1107 1108
  auto oh_inner = stages[CC]->axis(3);
  auto ow_outer = stages[CC]->axis(4);
  auto ow_inner = stages[CC]->axis(5);
  auto oc_inner = stages[CC]->axis(6);
  auto ic_outer = stages[CC]->axis(7);
  auto ic_inner = stages[CC]->axis(8);
1109 1110 1111 1112 1113 1114
  stages[CC]->Reorder(
      {ow_outer, ic_outer, ic_inner, oh_inner, ow_inner, oc_inner});
  stages[CC]->Vectorize(stages[CC]->n_out_dims() - 3,
                        CC->shape.back().as_int32());
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1115 1116
  // CC_init
  auto CC_init = CC->GetInitTensor(stages, target);
1117 1118
  stages[CC_init]->Vectorize(stages[CC_init]->n_out_dims() - 1,
                             CC_init->shape.back().as_int32());
1119 1120 1121 1122 1123 1124 1125

  // res
  // n, oc, oh, ow
  if (res.defined()) {
    stages[res]->Split(1, oc_bn_size);
    stages[res]->Split(3, oh_bn_size);
    stages[res]->Split(5, ow_bn_size);
1126 1127
    // reorder: [n, oc_outer, oc_inner, oh_outer, oh_inner, ow_outer, ow_inner]
    // -> [n, oc_outer, oh_outer, ow_outer, oh_inner, ow_inner, oc_inner]
1128 1129 1130 1131 1132
    auto oc_inner1 = stages[res]->axis(2);
    auto oh_outer1 = stages[res]->axis(3);
    auto oh_inner1 = stages[res]->axis(4);
    auto ow_outer1 = stages[res]->axis(5);
    auto ow_inner1 = stages[res]->axis(6);
1133 1134 1135 1136
    stages[res]->Reorder(
        {oh_outer1, ow_outer1, oh_inner1, ow_inner1, oc_inner1});
    VLOG(3) << "stages[res]->transformed_domain()"
            << stages[res]->transformed_domain();
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
  }
}

void Conv2d_NCHWc_Schedule_CPU_Nofuse(poly::StageMap stages,
                                      const ir::Tensor &res,
                                      ir::Tensor &packed_out,
                                      const ir::Tensor &input_pad,
                                      const ir::Tensor &weights_dilation,
                                      const ir::Tensor &data,
                                      const common::Target &target) {
1147 1148
  CHECK(target.arch == Target::Arch::X86)
      << "Conv2d_NCHWc_Schedule_CPU_Nofuse schedule only used in x86";
1149 1150 1151 1152
  CHECK(packed_out.defined());
  CHECK(input_pad.defined());
  auto type = packed_out->type();
  absl::flat_hash_map<std::string, int> conv2d_factors;
1153 1154 1155 1156
  CHECK_EQ(packed_out->shape.size(), 5U)
      << "packed_out's shape size should be 5";
  Expr w_out = common::AutoSimplify(packed_out->shape[3]);
  int ow = w_out.as_int32();
1157 1158 1159 1160 1161
  int basic_split_factor = GetBasicFactor(type, target);
  GetConv2dFactors(&conv2d_factors, -1, -1, -1, -1, ow, type, target);
  int ow_bn_size = conv2d_factors["ow_bn"];

  auto input_shape = input_pad->shape;
1162
  int shape_size = input_shape.size();
1163
  CHECK_EQ(shape_size, 5U) << "input shape size should be 5";
1164 1165
  Expr oc_bn = common::AutoSimplify(packed_out->shape.back());
  Expr ic_bn = common::AutoSimplify(input_shape.back());
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
  int oc_bn_size = oc_bn.as_int32();
  int ic_bn_size = ic_bn.as_int32();
  VLOG(3) << "ow_bn_size " << ow_bn_size;
  VLOG(3) << "oc_bn_size " << oc_bn_size;
  VLOG(3) << "ic_bn_size " << ic_bn_size;

  // data
  if (data.defined()) {
    stages[data]->ComputeInline();
  }
  // weights
  if (weights_dilation.defined()) {
1178 1179
    CHECK_GE(stages[weights_dilation]->n_out_dims(), 3U)
        << "weights_dilation's out_dims should be more than 3";
1180 1181 1182 1183 1184 1185
    // Reorder: [oc_outer, ic_outer, oh, ow, ic_inner, oc_inner] ->
    // [oc_outer, oh, ic_outer, ow, ic_inner, oc_inner]
    stages[weights_dilation]->Reorder({2, 1});
  }
  // packed_out
  auto CC = stages[packed_out]->CacheWrite("global", stages, packed_out);
1186 1187 1188 1189
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1190 1191 1192
  // packed_out: [batch, oc_outer, oh, ow, oc_inner]
  // split ow
  stages[packed_out]->Split(3, ow_bn_size);
1193 1194
  stages[packed_out]->Vectorize(stages[packed_out]->n_out_dims() - 1,
                                packed_out->shape.back().as_int32());
1195 1196 1197 1198 1199

  // CC: [batch, oc_outer, oh, ow, oc_inner]
  // packed_out: [batch, oc_outer, oh, ow_outer, ow_inner, oc_inner]
  // not computeAt ow_outer but oh
  stages[CC]->ComputeAt2(stages[packed_out], 2);
1200 1201 1202 1203
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1204 1205 1206 1207 1208
  // split ow
  stages[CC]->Split(3, ow_bn_size);
  // CC: [batch, oc_outer, oh, ow_outer, ow_inner, oc_inner, ic, kh, kw]
  // split ic
  stages[CC]->Split(6, ic_bn_size);
1209 1210 1211
  // reorder: [batch, oc_outer, oh, ow_outer, ow_inner, oc_inner, ic_outer,
  // ic_inner, kh, kw] -> [batch, oc_outer, oh, ow_outer, ic_outer, kh, kw,
  // ic_inner, ow_inner, oc_inner]
1212 1213 1214 1215
  auto ow_inner = stages[CC]->axis(4);
  auto oc_inner = stages[CC]->axis(5);
  auto ic_outer = stages[CC]->axis(6);
  auto ic_inner = stages[CC]->axis(7);
1216 1217
  auto kh = stages[CC]->axis(8);
  auto kw = stages[CC]->axis(9);
1218
  stages[CC]->Reorder({ic_outer, kh, kw, ic_inner, ow_inner, oc_inner});
1219 1220 1221 1222
  stages[CC]->Vectorize(stages[CC]->n_out_dims() - 1,
                        CC->shape.back().as_int32());
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1223 1224
  // CC_init
  auto CC_init = CC->GetInitTensor(stages, target);
1225 1226
  stages[CC_init]->Vectorize(stages[CC_init]->n_out_dims() - 1,
                             CC_init->shape.back().as_int32());
1227 1228 1229 1230 1231 1232 1233 1234 1235

  // res
  // n, oc, oh, ow
  if (res.defined()) {
    stages[res]->Split(1, oc_bn_size);
    stages[res]->Split(4, ow_bn_size);
    // Reorder: [n, oc_outer, oc_inner, oh, ow_outer, ow_inner] ->
    // [n, oc_outer, oh, ow_outer, ow_inner, oc_inner]
    auto oc_inner1 = stages[res]->axis(2);
1236
    auto oh1 = stages[res]->axis(3);
1237 1238 1239
    auto ow_outer1 = stages[res]->axis(4);
    auto ow_inner1 = stages[res]->axis(5);
    stages[res]->Reorder({oh1, ow_outer1, ow_inner1, oc_inner1});
1240 1241
    VLOG(3) << "stages[res]->transformed_domain()"
            << stages[res]->transformed_domain();
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  }
}

void Conv2d_NCHWc_Schedule_CPU(poly::StageMap stages,
                               const ir::Tensor &res,
                               ir::Tensor &packed_out,
                               const ir::Tensor &input_pad,
                               const ir::Tensor &weights_dilation,
                               const ir::Tensor &data,
                               const common::Target &target,
                               const std::string &key,
                               bool do_padding) {
1254 1255
  CHECK(target.arch == Target::Arch::X86)
      << "Conv2d_NCHWc_Schedule_CPU schedule only used in x86";
1256 1257 1258
  CHECK(packed_out.defined());
  CHECK(input_pad.defined());
  auto type = packed_out->type();
1259 1260 1261 1262
  CHECK_EQ(packed_out->shape.size(), 5U)
      << "packed_out's shape size should be 5";
  Expr w_out = common::AutoSimplify(packed_out->shape[3]);
  int ow = w_out.as_int32();
1263
  auto input_shape = input_pad->shape;
1264
  int shape_size = input_shape.size();
1265
  CHECK_EQ(shape_size, 5U) << "input shape size should be 5";
1266 1267
  Expr oc_bn = common::AutoSimplify(packed_out->shape.back());
  Expr ic_bn = common::AutoSimplify(input_shape.back());
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
  int oc_bn_size = oc_bn.as_int32();
  int ic_bn_size = ic_bn.as_int32();

  absl::flat_hash_map<std::string, int> conv2d_factors;
  GetConv2dFactors(&conv2d_factors, -1, -1, -1, -1, ow, type, target, key);
  int ow_bn_size = conv2d_factors["ow_bn"];
  VLOG(3) << "ow_bn_size " << ow_bn_size;
  VLOG(3) << "oc_bn_size " << oc_bn_size;
  VLOG(3) << "ic_bn_size " << ic_bn_size;
  int unroll_kw = 0;
  if (conv2d_factors.count("unroll_kw")) {
    unroll_kw = conv2d_factors["unroll_kw"];
  }
  VLOG(3) << "unroll_kw " << unroll_kw;
  // data
  if (data.defined()) {
1284 1285
    CHECK_GE(stages[data]->n_out_dims(), 3U)
        << "data's out_dims should be more than 3";
1286 1287 1288 1289 1290
    stages[data]->Fuse({0, 1, 2});
    stages[data]->ComputeInline();
  }
  // input_pad
  if (do_padding) {
1291 1292
    CHECK_GE(stages[input_pad]->n_out_dims(), 3U)
        << "input_pad's out_dims should be more than 3";
1293
    stages[input_pad]->Fuse({0, 1, 2});
1294 1295
    stages[input_pad]->Vectorize(stages[input_pad]->n_out_dims() - 1,
                                 input_pad->shape.back().as_int32());
1296 1297 1298 1299 1300
  } else {
    stages[input_pad]->ComputeInline();
  }
  // weights
  if (weights_dilation.defined()) {
1301 1302 1303 1304
    CHECK_GE(stages[weights_dilation]->n_out_dims(), 3U)
        << "weights_dilation's out_dims should be more than 3";
    // oc_outer, ic_outer, oh, ow, ic_inner, oc_inner -> oc_outer, oh, ic_outer,
    // ow, ic_inner, oc_inner
1305 1306 1307 1308 1309
    stages[weights_dilation]->Reorder({2, 1});
    stages[weights_dilation]->Fuse({0, 1});
  }
  // packed_out
  auto CC = stages[packed_out]->CacheWrite("global", stages, packed_out);
1310 1311 1312 1313
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1314 1315 1316 1317
  // packed_out: [batch, oc_outer, oh, ow, oc_inner]
  // split ow
  stages[packed_out]->Split(3, ow_bn_size);
  stages[packed_out]->Fuse({0, 1, 2});
1318 1319
  stages[packed_out]->Vectorize(stages[packed_out]->n_out_dims() - 1,
                                packed_out->shape.back().as_int32());
1320 1321 1322 1323

  // CC
  stages[CC]->ComputeAt2(stages[packed_out], 1);
  VLOG(3) << "cache write shape: " << utils::Join(CC->shape, ", ");
1324 1325 1326 1327
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1328 1329 1330 1331
  // CC: [batch_oc_outer_oh_fused, ow_outer, ow_inner, oc_inner, ic, kh, kw]
  // for fused_axes' copy transform, not split ow again
  // split ic
  stages[CC]->Split(4, ic_bn_size);
1332 1333 1334
  // reorder: [batch_oc_outer_oh_fused, ow_outer, ow_inner, oc_inner, ic_outer,
  // ic_inner, kh, kw] -> [batch_oc_outer_oh_fused, ow_outer, ic_outer, kh, kw,
  // ic_inner, ow_inner, oc_inner]
1335 1336 1337 1338
  auto ow_inner = stages[CC]->axis(2);
  auto oc_inner = stages[CC]->axis(3);
  auto ic_outer = stages[CC]->axis(4);
  auto ic_inner = stages[CC]->axis(5);
1339 1340
  auto kh = stages[CC]->axis(6);
  auto kw = stages[CC]->axis(7);
1341 1342 1343 1344 1345 1346
  if (unroll_kw) {
    stages[CC]->Reorder({ic_outer, kh, ic_inner, kw, ow_inner, oc_inner});
    stages[CC]->Unroll(kw);
  } else {
    stages[CC]->Reorder({ic_outer, kh, kw, ic_inner, ow_inner, oc_inner});
  }
1347 1348 1349 1350
  stages[CC]->Vectorize(stages[CC]->n_out_dims() - 1,
                        CC->shape.back().as_int32());
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1351 1352
  // CC_init
  auto CC_init = CC->GetInitTensor(stages, target);
1353 1354
  stages[CC_init]->Vectorize(stages[CC_init]->n_out_dims() - 1,
                             CC_init->shape.back().as_int32());
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  // unroll ow_inner
  stages[CC]->Unroll(stages[CC]->n_out_dims() - 2);
  stages[CC_init]->Unroll(stages[CC_init]->n_out_dims() - 2);

  // res
  // n, oc, oh, ow
  if (res.defined()) {
    stages[res]->Split(1, oc_bn_size);
    stages[res]->Split(4, ow_bn_size);
    // Reorder: [n, oc_outer, oc_inner, oh, ow_outer, ow_inner] ->
    // [n, oc_outer, oh, ow_outer, ow_inner, oc_inner]
    auto oc_inner1 = stages[res]->axis(2);
1367
    auto oh1 = stages[res]->axis(3);
1368 1369 1370 1371 1372 1373 1374 1375 1376
    auto ow_outer1 = stages[res]->axis(4);
    auto ow_inner1 = stages[res]->axis(5);
    stages[res]->Reorder({oh1, ow_outer1, ow_inner1, oc_inner1});
    // stages[res]->Fuse({0, 1, 2});
    // Todo: computeAt according to forloops' range
    // stages[packed_out]->ComputeAt2(stages[res], 2);
  }
}

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
void Depthwise_Conv2d_NCHWc_Schedule_CPU_Nofuse(
    poly::StageMap stages,
    const ir::Tensor &res,
    ir::Tensor &packed_out,
    const ir::Tensor &input_pad,
    const ir::Tensor &weights_dilation,
    const ir::Tensor &data,
    const common::Target &target,
    bool do_padding) {
  CHECK(target.arch == Target::Arch::X86)
      << "Depthwise_Conv2d_NCHWc_Schedule_CPU_Nofuse schedule only used in x86";
1388 1389 1390 1391
  CHECK(packed_out.defined());
  CHECK(input_pad.defined());
  auto type = packed_out->type();
  absl::flat_hash_map<std::string, int> conv2d_factors;
1392 1393 1394 1395
  CHECK_EQ(packed_out->shape.size(), 5U)
      << "packed_out's shape size should be 5";
  Expr w_out = common::AutoSimplify(packed_out->shape[3]);
  int ow = w_out.as_int32();
1396 1397 1398 1399 1400
  int basic_split_factor = GetBasicFactor(type, target);
  GetConv2dFactors(&conv2d_factors, -1, -1, -1, -1, ow, type, target);
  int ow_bn_size = conv2d_factors["ow_bn"];

  auto input_shape = input_pad->shape;
1401
  int shape_size = input_shape.size();
1402
  CHECK_EQ(shape_size, 5U) << "input shape size should be 5";
1403 1404
  Expr oc_bn = common::AutoSimplify(packed_out->shape.back());
  Expr ic_bn = common::AutoSimplify(input_shape.back());
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
  int oc_bn_size = oc_bn.as_int32();
  int ic_bn_size = ic_bn.as_int32();
  VLOG(3) << "ow_bn_size " << ow_bn_size;
  VLOG(3) << "oc_bn_size " << oc_bn_size;
  VLOG(3) << "ic_bn_size " << ic_bn_size;

  // data
  if (data.defined()) {
    stages[data]->ComputeInline();
  }
  // input_pad
  if (!do_padding) {
    stages[input_pad]->ComputeInline();
  }
  // weights
  if (weights_dilation.defined()) {
1421 1422
    CHECK_GE(stages[weights_dilation]->n_out_dims(), 3U)
        << "weights_dilation's out_dims should be more than 3";
1423 1424 1425 1426 1427 1428 1429
    // Reorder: [oc_outer, ic_outer, oh, ow, ic_inner, oc_inner] ->
    // [oc_outer, oh, ic_outer, ow, ic_inner, oc_inner]
    stages[weights_dilation]->Reorder({2, 1});
  }

  // packed_out
  auto CC = stages[packed_out]->CacheWrite("global", stages, packed_out);
1430 1431 1432 1433
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1434 1435 1436
  // packed_out: [batch, oc_outer, oh, ow, oc_inner]
  // split ow
  stages[packed_out]->Split(3, ow_bn_size);
1437 1438
  stages[packed_out]->Vectorize(stages[packed_out]->n_out_dims() - 1,
                                packed_out->shape.back().as_int32());
1439 1440 1441 1442

  // CC: [batch, oc_outer, oh, ow, oc_inner]
  // packed_out: [batch, oc_outer, oh, ow_outer, ow_inner, oc_inner]
  stages[CC]->ComputeAt2(stages[packed_out], 3);
1443 1444 1445 1446
  VLOG(3) << "stages[packed_out]->transformed_domain()"
          << stages[packed_out]->transformed_domain();
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1447 1448 1449 1450 1451

  // CC: [batch, oc_outer, oh, ow_outer, ow_inner, oc_inner, fc, kh, kw]
  // batch, oc_outer, oh, ow_outer, kh, kw, ow_inner, oc_inner
  auto CC_ow_inner = stages[CC]->axis(4);
  auto CC_oc_inner = stages[CC]->axis(5);
1452 1453 1454
  auto CC_fc = stages[CC]->axis(6);
  auto CC_kh = stages[CC]->axis(7);
  auto CC_kw = stages[CC]->axis(8);
1455
  stages[CC]->Reorder({CC_fc, CC_kh, CC_kw, CC_ow_inner, CC_oc_inner});
1456 1457 1458 1459
  stages[CC]->Vectorize(stages[CC]->n_out_dims() - 1,
                        CC->shape.back().as_int32());
  VLOG(3) << "stages[CC]->transformed_domain()"
          << stages[CC]->transformed_domain();
1460 1461
  // CC_init
  auto CC_init = CC->GetInitTensor(stages, target);
1462 1463
  stages[CC_init]->Vectorize(stages[CC_init]->n_out_dims() - 1,
                             CC_init->shape.back().as_int32());
1464 1465 1466 1467 1468 1469 1470 1471 1472

  // res
  // n, oc, oh, ow
  if (res.defined()) {
    stages[res]->Split(1, oc_bn_size);
    stages[res]->Split(4, ow_bn_size);
    // Reorder: [n, oc_outer, oc_inner, oh, ow_outer, ow_inner] ->
    // [n, oc_outer, oh, ow_outer, ow_inner, oc_inner]
    auto oc_inner1 = stages[res]->axis(2);
1473
    auto oh1 = stages[res]->axis(3);
1474 1475 1476
    auto ow_outer1 = stages[res]->axis(4);
    auto ow_inner1 = stages[res]->axis(5);
    stages[res]->Reorder({oh1, ow_outer1, ow_inner1, oc_inner1});
1477 1478
    VLOG(3) << "stages[res]->transformed_domain()"
            << stages[res]->transformed_domain();
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
  }
}

void CudaScheduleMul(poly::StageMap stages,
                     ir::Tensor output,
                     const std::vector<int> &output_shape,
                     const common::Target &target) {
  stages[output]->Split(1, 2);
  stages[output]->Bind(0, "blockIdx.x");
  stages[output]->Bind(1, "threadIdx.x");
}

inline void InputDirectConvCudaParam(
1492 1493 1494
    absl::flat_hash_map<std::string,
                        absl::flat_hash_map<std::string, std::vector<int>>>
        &model_data,
1495 1496 1497 1498 1499 1500 1501
    const std::string &key,
    const std::vector<std::vector<int>> &int_data) {
  CHECK_EQ(int_data.size(), 6UL);
  absl::flat_hash_map<std::string, std::vector<int>> schedule_data;
  schedule_data["rc"] = int_data[0];
  schedule_data["ry"] = int_data[1];
  schedule_data["rx"] = int_data[2];
1502 1503 1504 1505 1506
  schedule_data["f"] = int_data[3];
  schedule_data["y"] = int_data[4];
  schedule_data["x"] = int_data[5];
  CHECK(model_data.count(key) == 0)
      << "Key " << key << "in conv cuda param already exists.";
1507 1508 1509 1510
  model_data[key] = schedule_data;
}

inline void InputWinogradConvCudaParam(
1511 1512 1513
    absl::flat_hash_map<std::string,
                        absl::flat_hash_map<std::string, std::vector<int>>>
        &model_data,
1514 1515 1516 1517 1518
    const std::string &key,
    const std::vector<std::vector<int>> &int_data) {
  CHECK_EQ(int_data.size(), 4UL);
  absl::flat_hash_map<std::string, std::vector<int>> schedule_data;
  schedule_data["rc"] = int_data[0];
1519 1520 1521 1522
  schedule_data["x"] = int_data[1];
  schedule_data["y"] = int_data[2];
  schedule_data["b"] = int_data[3];
  model_data[key] = schedule_data;
1523 1524
}

1525 1526 1527 1528 1529 1530
absl::flat_hash_map<std::string,
                    absl::flat_hash_map<std::string, std::vector<int>>>
CreateCudaParams() {
  absl::flat_hash_map<std::string,
                      absl::flat_hash_map<std::string, std::vector<int>>>
      model_data;
1531
  // The format of serial data is:
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
  // hash_key: string = name of schedule + shape of input_pad + shape of weights
  // + shape of output value: vector of params
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 3 230 230 64 3 7 7 1 64 112 112",
      {{3, 1}, {7, 1}, {1, 7}, {1, 4, 8, 2}, {112, 1, 1, 1}, {1, 7, 16, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 56 56 64 64 1 1 1 64 56 56",
      {{4, 16}, {1, 1}, {1, 1}, {1, 8, 8, 1}, {56, 1, 1, 1}, {1, 2, 28, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 58 58 128 64 3 3 1 128 28 28",
      {{32, 2}, {1, 3}, {1, 3}, {4, 2, 16, 1}, {28, 1, 1, 1}, {1, 2, 14, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 56 56 128 64 1 1 1 128 28 28",
      {{4, 16}, {1, 1}, {1, 1}, {2, 2, 32, 1}, {28, 1, 1, 1}, {1, 2, 14, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 30 30 256 128 3 3 1 256 14 14",
      {{32, 4}, {1, 3}, {1, 3}, {8, 1, 16, 2}, {7, 1, 2, 1}, {1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 28 28 256 128 1 1 1 256 14 14",
      {{16, 8}, {1, 1}, {1, 1}, {8, 1, 16, 2}, {14, 1, 1, 1}, {1, 1, 14, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 16 16 512 256 3 3 1 512 7 7",
      {{64, 4}, {1, 3}, {1, 3}, {32, 1, 16, 1}, {7, 1, 1, 1}, {1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 14 14 512 256 1 1 1 512 7 7",
      {{16, 16}, {1, 1}, {1, 1}, {16, 1, 32, 1}, {7, 1, 1, 1}, {1, 1, 7, 1}});
1566 1567

  // winograd
1568 1569 1570 1571
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 58 58 64 64 3 3 1 64 56 56",
      {{32, 2}, {1, 3}, {1, 3}, {4, 1, 8, 2}, {28, 1, 2, 1}, {1, 2, 7, 4}});
1572
  // winograd
1573 1574 1575 1576
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 512 9 9 512 512 3 3 1 512 7 7",
      {{64, 8}, {1, 3}, {1, 3}, {32, 1, 16, 1}, {7, 1, 1, 1}, {1, 1, 7, 1}});
1577
  // winograd
1578 1579 1580 1581
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 16 16 256 256 3 3 1 256 14 14",
      {{64, 4}, {1, 3}, {1, 3}, {16, 1, 16, 1}, {14, 1, 1, 1}, {1, 1, 14, 1}});
1582
  // winograd
1583 1584 1585 1586
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 30 30 128 128 3 3 1 128 28 28",
      {{32, 4}, {1, 3}, {1, 3}, {8, 1, 16, 1}, {14, 1, 2, 1}, {1, 1, 7, 4}});
1587 1588 1589

  // MobileNetV2 schedule params
  /*   InputDirectConvCudaParam(model_data,
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
                             "CudaDirectConvSchedule 1 3 226 226 32 3 3 3 1 32
     112 112",
                             {{3, 1}, {1, 3}, {1, 3}, {-1, 2, 8, 2}, {-1, 1, 1,
     7}, {-1, 1, 16, 1}}); */
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 32 112 112 16 32 1 1 1 16 112 112",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 2, 2, 4},
       {-1, 1, 2, 1},
       {-1, 1, 56, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 32 112 112 32 32 1 1 1 32 112 112",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 1, 4, 8},
       {-1, 1, 2, 1},
       {-1, 7, 16, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 16 112 112 96 16 1 1 1 96 112 112",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 4, 4, 2},
       {-1, 2, 2, 1},
       {-1, 1, 16, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 96 56 56 24 96 1 1 1 24 56 56",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 3, 4, 2},
       {-1, 1, 1, 1},
       {-1, 1, 28, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 24 56 56 144 24 1 1 1 144 56 56",
      {{-1, 6},
       {-1, 1},
       {-1, 1},
       {-1, 9, 4, 2},
       {-1, 2, 1, 1},
       {-1, 1, 56, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 144 56 56 24 144 1 1 1 24 56 56",
      {{-1, 12},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 3},
       {-1, 1, 1, 1},
       {-1, 2, 14, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 144 28 28 32 144 1 1 1 32 28 28",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 4, 8, 1},
       {-1, 1, 1, 1},
       {-1, 1, 14, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 32 28 28 192 32 1 1 1 192 28 28",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 6, 4, 1},
       {-1, 2, 1, 2},
       {-1, 1, 28, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 192 28 28 32 192 1 1 1 32 28 28",
      {{-1, 48},
       {-1, 1},
       {-1, 1},
       {-1, 4, 8, 1},
       {-1, 1, 1, 1},
       {-1, 1, 28, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 192 14 14 64 192 1 1 1 64 14 14",
      {{-1, 12},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 2},
       {-1, 2, 1, 1},
       {-1, 1, 14, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 14 14 384 64 1 1 1 384 14 14",
      {{-1, 4}, {-1, 1}, {-1, 1}, {-1, 2, 4, 3}, {-1, 1, 7, 1}, {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 384 14 14 64 384 1 1 1 64 14 14",
      {{-1, 48},
       {-1, 1},
       {-1, 1},
       {-1, 2, 16, 1},
       {-1, 1, 2, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 384 14 14 96 384 1 1 1 96 14 14",
      {{-1, 12},
       {-1, 1},
       {-1, 1},
       {-1, 2, 6, 1},
       {-1, 1, 2, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 96 14 14 576 96 1 1 1 576 14 14",
      {{-1, 6}, {-1, 1}, {-1, 1}, {-1, 1, 6, 6}, {-1, 1, 7, 1}, {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 576 14 14 96 576 1 1 1 96 14 14",
      {{-1, 24},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 3},
       {-1, 1, 2, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 576 7 7 160 576 1 1 1 160 7 7",
      {{-1, 36},
       {-1, 1},
       {-1, 1},
       {-1, 2, 2, 2},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 160 7 7 960 160 1 1 1 960 7 7",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 6, 4, 1},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 960 7 7 160 960 1 1 1 160 7 7",
      {{-1, 60},
       {-1, 1},
       {-1, 1},
       {-1, 2, 4, 1},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 960 7 7 320 960 1 1 1 320 7 7",
      {{-1, 20},
       {-1, 1},
       {-1, 1},
       {-1, 2, 2, 2},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 320 7 7 1280 320 1 1 1 1280 7 7",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 2, 16, 1},
       {-1, 7, 1, 1},
       {-1, 1, 7, 1}});
1764 1765

  // EfficientNet schedule params
1766 1767 1768 1769 1770 1771 1772 1773 1774
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 3 228 228 32 3 3 3 1 32 113 113",
      {{-1, 1},
       {-1, 1},
       {-1, 3},
       {-1, 32, 1, 1},
       {-1, 1, 1, 1},
       {-1, 1, 113, 1}});
1775 1776
  InputDirectConvCudaParam(model_data,
                           "CudaDirectConvSchedule 1 32 1 1 8 32 1 1 1 8 1 1",
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
                           {{-1, 16},
                            {-1, 1},
                            {-1, 1},
                            {-1, 1, 4, 1},
                            {-1, 1, 1, 1},
                            {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 8 1 1 32 8 1 1 1 32 1 1",
      {{-1, 8}, {-1, 1}, {-1, 1}, {-1, 1, 8, 4}, {-1, 1, 1, 1}, {-1, 1, 1, 1}});
1787 1788
  InputDirectConvCudaParam(model_data,
                           "CudaDirectConvSchedule 1 96 1 1 4 96 1 1 1 4 1 1",
1789 1790 1791 1792 1793 1794
                           {{-1, 48},
                            {-1, 1},
                            {-1, 1},
                            {-1, 1, 4, 1},
                            {-1, 1, 1, 1},
                            {-1, 1, 1, 1}});
1795 1796
  InputDirectConvCudaParam(model_data,
                           "CudaDirectConvSchedule 1 4 1 1 96 4 1 1 1 96 1 1",
1797 1798 1799 1800 1801 1802
                           {{-1, 2},
                            {-1, 1},
                            {-1, 1},
                            {-1, 12, 1, 1},
                            {-1, 1, 1, 1},
                            {-1, 1, 1, 1}});
1803 1804
  InputDirectConvCudaParam(model_data,
                           "CudaDirectConvSchedule 1 144 1 1 6 144 1 1 1 6 1 1",
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
                           {{-1, 48},
                            {-1, 1},
                            {-1, 1},
                            {-1, 1, 6, 1},
                            {-1, 1, 1, 1},
                            {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 6 1 1 144 6 1 1 1 144 1 1",
      {{-1, 2}, {-1, 1}, {-1, 1}, {-1, 2, 8, 1}, {-1, 1, 1, 1}, {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 144 28 28 40 144 1 1 1 40 28 28",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 5, 8, 1},
       {-1, 1, 1, 1},
       {-1, 1, 28, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 40 28 28 240 40 1 1 1 240 28 28",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 2, 8, 3},
       {-1, 4, 1, 1},
       {-1, 1, 28, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 240 1 1 10 240 1 1 1 10 1 1",
      {{-1, 60},
       {-1, 1},
       {-1, 1},
       {-1, 1, 5, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 10 1 1 240 10 1 1 1 240 1 1",
      {{-1, 10},
       {-1, 1},
       {-1, 1},
       {-1, 1, 40, 3},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 240 28 28 40 240 1 1 1 40 28 28",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 5},
       {-1, 1, 1, 1},
       {-1, 1, 28, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 240 14 14 80 240 1 1 1 80 14 14",
      {{-1, 20},
       {-1, 1},
       {-1, 1},
       {-1, 2, 8, 1},
       {-1, 1, 2, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 80 14 14 480 80 1 1 1 480 14 14",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 2, 8, 3},
       {-1, 1, 7, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 480 1 1 20 480 1 1 1 20 1 1",
      {{-1, 60},
       {-1, 1},
       {-1, 1},
       {-1, 1, 4, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 20 1 1 480 20 1 1 1 480 1 1",
      {{-1, 5},
       {-1, 1},
       {-1, 1},
       {-1, 1, 32, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 480 14 14 80 480 1 1 1 80 14 14",
      {{-1, 40},
       {-1, 1},
       {-1, 1},
       {-1, 2, 8, 1},
       {-1, 1, 2, 1},
       {-1, 1, 14, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 480 14 14 112 480 1 1 1 112 14 14",
      {{-1, 20},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 2},
       {-1, 1, 2, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 112 14 14 672 112 1 1 1 672 14 14",
      {{-1, 14},
       {-1, 1},
       {-1, 1},
       {-1, 1, 7, 6},
       {-1, 1, 7, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 672 1 1 28 672 1 1 1 28 1 1",
      {{-1, 28},
       {-1, 1},
       {-1, 1},
       {-1, 1, 7, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 28 1 1 672 28 1 1 1 672 1 1",
      {{-1, 28},
       {-1, 1},
       {-1, 1},
       {-1, 1, 16, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 672 14 14 112 672 1 1 1 112 14 14",
      {{-1, 14},
       {-1, 1},
       {-1, 1},
       {-1, 2, 4, 2},
       {-1, 1, 2, 1},
       {-1, 1, 7, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 672 7 7 192 672 1 1 1 192 7 7",
      {{-1, 28},
       {-1, 1},
       {-1, 1},
       {-1, 1, 2, 3},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 192 7 7 1152 192 1 1 1 1152 7 7",
      {{-1, 24},
       {-1, 1},
       {-1, 1},
       {-1, 1, 12, 3},
       {-1, 7, 1, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 1152 1 1 48 1152 1 1 1 48 1 1",
      {{-1, 576},
       {-1, 1},
       {-1, 1},
       {-1, 1, 3, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 48 1 1 1152 48 1 1 1 1152 1 1",
      {{-1, 12},
       {-1, 1},
       {-1, 1},
       {-1, 1, 32, 1},
       {-1, 1, 1, 1},
       {-1, 1, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 1152 7 7 192 1152 1 1 1 192 7 7",
      {{-1, 36},
       {-1, 1},
       {-1, 1},
       {-1, 1, 2, 6},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 1152 7 7 320 1152 1 1 1 320 7 7",
      {{-1, 12},
       {-1, 1},
       {-1, 1},
       {-1, 1, 2, 4},
       {-1, 1, 7, 1},
       {-1, 1, 7, 1}});
2004 2005 2006

  // FaceDet schedule params
  /*   InputDirectConvCudaParam(model_data,
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
                                 "CudaDirectConvSchedule 1 3 242 322 16 3 3 3 1
     16 120 160",
                                 {{-1, 1}, {-1, 3}, {-1, 3}, {-1, 2, 4, 2}, {-1,
     1, 1, 5}, {-1, 1, 32, 1}}); */
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 16 120 160 32 16 1 1 1 32 120 160",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 8, 4, 1},
       {-1, 1, 1, 1},
       {-1, 5, 32, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 32 60 80 32 32 1 1 1 32 60 80",
      {{-1, 4},
       {-1, 1},
       {-1, 1},
       {-1, 8, 4, 1},
       {-1, 3, 1, 1},
       {-1, 1, 40, 1}});
2029
  /*   InputDirectConvCudaParam(model_data,
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
                                 "CudaDirectConvSchedule 1 32 30 40 64 32 1 1 1
     64 30 40",
                                 {{-1, 8}, {-1, 1}, {-1, 1}, {-1, 2, 8, 2}, {-1,
     1, 1, 3}, {-1, 1, 20, 1}}); */
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 30 40 64 64 1 1 1 64 30 40",
      {{-1, 8}, {-1, 1}, {-1, 1}, {-1, 2, 8, 2}, {-1, 1, 2, 1}, {-1, 5, 8, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 30 40 8 64 1 1 1 8 30 40",
      {{-1, 8}, {-1, 1}, {-1, 1}, {-1, 2, 4, 1}, {-1, 1, 2, 1}, {-1, 1, 8, 1}});
2042
  /*   InputDirectConvCudaParam(model_data,
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
                                 "CudaDirectConvSchedule 1 8 32 42 12 8 3 3 1 12
    30 40",
                                 {{-1, 4}, {-1, 3}, {-1, 3}, {-1, 1, 12, 1},
    {-1, 1, 1, 3}, {-1, 1, 10, 1}}); InputDirectConvCudaParam(model_data,
                                 "CudaDirectConvSchedule 1 8 32 42 16 8 3 3 1 16
    30 40",
                                 {{-1, 8}, {-1, 3}, {-1, 3}, {-1, 1, 16, 1},
    {-1, 3, 1, 2}, {-1, 1, 4, 2}}); */
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 16 36 46 16 16 3 3 1 16 30 40",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 2, 8, 1},
       {-1, 1, 2, 1},
       {-1, 1, 8, 1}});
2060
  /*   InputDirectConvCudaParam(model_data,
2061 2062 2063 2064
                             "CudaDirectConvSchedule 1 16 34 44 16 16 3 3 1 16
     30 40",
                             {{-1, 4}, {-1, 3}, {-1, 3}, {-1, 1, 4, 2}, {-1, 3,
     2, 1}, {-1, 1, 20, 1}}); */
2065
  /*   InputDirectConvCudaParam(model_data,
2066 2067 2068 2069
                                 "CudaDirectConvSchedule 1 12 32 42 16 12 3 3 1
     16 30 40",
                                 {{-1, 4}, {-1, 3}, {-1, 3}, {-1, 1, 16, 1},
     {-1, 1, 2, 3}, {-1, 1, 2, 2}}); */
2070
  /*   InputDirectConvCudaParam(model_data,
2071 2072 2073 2074
                             "CudaDirectConvSchedule 1 16 40 50 16 16 3 3 1 16
     30 40",
                             {{-1, 4}, {-1, 1}, {-1, 3}, {-1, 1, 1, 8}, {-1, 1,
     3, 1}, {-1, 1, 40, 1}}); */
2075
  /*   InputDirectConvCudaParam(model_data,
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
                               "CudaDirectConvSchedule 1 48 30 40 64 48 1 1 1 64
     30 40",
                               {{-1, 8}, {-1, 1}, {-1, 1}, {-1, 2, 8, 2}, {-1,
     1, 1, 3}, {-1, 1, 20, 1}}); */
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 30 40 12 64 1 1 1 12 30 40",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 1, 4, 3},
       {-1, 1, 3, 1},
       {-1, 1, 10, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 30 40 6 64 1 1 1 6 30 40",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 3, 2, 1},
       {-1, 1, 3, 1},
       {-1, 1, 10, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 15 20 128 64 1 1 1 128 15 20",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 2, 8, 2},
       {-1, 1, 3, 1},
       {-1, 1, 10, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 15 20 128 128 1 1 1 128 15 20",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 4, 8, 1},
       {-1, 1, 3, 1},
       {-1, 1, 10, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 15 20 8 128 1 1 1 8 15 20",
      {{-1, 8},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 1},
       {-1, 1, 1, 1},
       {-1, 1, 10, 2}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 15 20 4 128 1 1 1 4 15 20",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 1, 4, 1},
       {-1, 1, 1, 1},
       {-1, 1, 20, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 128 8 10 256 128 1 1 1 256 8 10",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 1, 16, 2},
       {-1, 1, 8, 1},
       {-1, 1, 2, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 8 10 256 256 1 1 1 256 8 10",
      {{-1, 8}, {-1, 1}, {-1, 1}, {-1, 4, 8, 1}, {-1, 1, 8, 1}, {-1, 1, 2, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 8 10 64 256 1 1 1 64 8 10",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 1, 16, 1},
       {-1, 1, 8, 1},
       {-1, 2, 1, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 8 10 8 256 1 1 1 8 8 10",
      {{-1, 32},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 1},
       {-1, 1, 2, 1},
       {-1, 1, 2, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 8 10 4 256 1 1 1 4 8 10",
      {{-1, 32},
       {-1, 1},
       {-1, 1},
       {-1, 1, 4, 1},
       {-1, 1, 4, 1},
       {-1, 1, 2, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 64 4 5 256 64 1 1 1 256 4 5",
      {{-1, 16},
       {-1, 1},
       {-1, 1},
       {-1, 1, 8, 1},
       {-1, 1, 4, 1},
       {-1, 1, 5, 1}});
  InputDirectConvCudaParam(
      model_data,
      "CudaDirectConvSchedule 1 256 6 7 12 256 3 3 1 12 4 5",
      {{-1, 32},
       {-1, 3},
       {-1, 3},
       {-1, 1, 4, 1},
       {-1, 1, 4, 1},
       {-1, 1, 1, 1}});
2192 2193
  InputDirectConvCudaParam(model_data,
                           "CudaDirectConvSchedule 1 256 6 7 6 256 3 3 1 6 4 5",
2194 2195 2196 2197 2198 2199
                           {{-1, 32},
                            {-1, 3},
                            {-1, 3},
                            {-1, 1, 2, 1},
                            {-1, 1, 4, 1},
                            {-1, 1, 1, 1}});
2200 2201

#ifndef CINN_WITH_CUDNN
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
  InputWinogradConvCudaParam(
      model_data,
      "CudaWinogradConvSchedule 1 512 9 9 512 512 3 3 1 512 7 7",
      {{32, 16}, {1, 1, 8, 2}, {8, 1, 16, 4}, {16, 1, 1, 1}});
  InputWinogradConvCudaParam(
      model_data,
      "CudaWinogradConvSchedule 1 256 6 7 12 256 3 3 1 12 4 5",
      {{-1, 256}, {-1, 1, 6, 1}, {-1, 1, 6, 1}, {-1, 1, 1, 1}});
  InputWinogradConvCudaParam(
      model_data,
      "CudaWinogradConvSchedule 1 256 6 7 6 256 3 3 1 12 4 5",
      {{-1, 256}, {-1, 1, 6, 1}, {-1, 1, 6, 1}, {-1, 1, 1, 1}});
  InputWinogradConvCudaParam(
      model_data,
      "CudaWinogradConvSchedule 1 12 32 42 16 12 3 3 1 16 30 40",
      {{-1, 12}, {-1, 2, 30, 1}, {-1, 4, 2, 2}, {-1, 1, 1, 1}});
  InputWinogradConvCudaParam(
      model_data,
      "CudaWinogradConvSchedule 1 8 32 42 12 8 3 3 1 12 30 40",
      {{-1, 8}, {-1, 2, 30, 1}, {-1, 1, 2, 6}, {-1, 1, 1, 1}});
  InputWinogradConvCudaParam(
      model_data,
      "CudaWinogradConvSchedule 1 8 32 42 16 8 3 3 1 16 30 40",
      {{-1, 4}, {-1, 2, 30, 1}, {-1, 1, 4, 4}, {-1, 1, 1, 1}});
2226 2227 2228 2229
#endif
  return model_data;
}

2230 2231 2232
void CreateCudaSerialData(const std::string &file_name) {
  SaveSerialData(CreateCudaParams(), file_name);
}
2233 2234 2235 2236 2237 2238 2239 2240

int GetMaxSplitter(int a, int b) {
  while (a % b > 0) {
    b--;
  }
  return b;
}

2241 2242 2243 2244 2245
void LoadSerialData(
    absl::flat_hash_map<std::string,
                        absl::flat_hash_map<std::string, std::vector<int>>>
        *params,
    const std::string &file_name) {
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
  proto::ModelData read_model_data;
  std::fstream input(file_name, std::ios::in | std::ios::binary);
  if (!read_model_data.ParseFromIstream(&input)) {
    std::cerr << "Failed to parse address book." << std::endl;
    exit(-1);
  }
  input.close();
  std::string test_write3;
  read_model_data.SerializeToString(&test_write3);
  auto read_model_map = read_model_data.data();
  for (auto &i : read_model_map) {
    auto read_schedule_map = i.second.data();
    absl::flat_hash_map<std::string, std::vector<int>> param_data;
    for (auto &j : read_schedule_map) {
      std::vector<int> temp_data;
      for (int k = 0; k < j.second.data_size(); k++) {
        temp_data.push_back(std::stoi(j.second.data(k)));
      }
      param_data[j.first] = temp_data;
    }
    (*params)[i.first] = param_data;
  }
}

void SaveSerialData(
2271 2272 2273
    const absl::flat_hash_map<
        std::string,
        absl::flat_hash_map<std::string, std::vector<int>>> &model_data,
2274 2275 2276 2277 2278 2279 2280 2281 2282
    const std::string &file_name) {
  proto::ModelData write_model_data;
  for (auto &i : model_data) {
    proto::ScheduleData write_schedule_data;
    for (auto &j : i.second) {
      proto::StringData write_vector_data;
      for (auto &k : j.second) {
        write_vector_data.add_data(std::to_string(k));
      }
2283
      auto data_map = write_schedule_data.mutable_data();
2284 2285
      (*data_map)[j.first] = write_vector_data;
    }
2286
    auto model_map = write_model_data.mutable_data();
2287 2288 2289 2290
    (*model_map)[i.first] = write_schedule_data;
    std::string test_write1;
    write_schedule_data.SerializeToString(&test_write1);
  }
2291 2292
  std::fstream output(file_name,
                      std::ios::out | std::ios::trunc | std::ios::binary);
2293 2294 2295 2296 2297 2298 2299 2300 2301
  std::string test_write;
  write_model_data.SerializeToString(&test_write);
  if (!write_model_data.SerializeToOstream(&output)) {
    std::cerr << "Failed to write test_serial.log" << std::endl;
    exit(-1);
  }
  output.close();
}

2302 2303 2304
void CudaScheduleDepthwiseConv(poly::StageMap stages,
                               ir::Tensor &output,
                               const common::Target &target) {
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
  auto OL = stages[output]->CacheWrite("local", stages, output);
  stages[output]->Bind(0, "blockIdx.x");
  stages[output]->Bind(1, "blockIdx.y");
  stages[output]->Bind(2, "blockIdx.z");
  stages[output]->Bind(3, "threadIdx.x");
  stages[OL]->ComputeAt(stages[output], 3);
  stages[OL]->Unroll(4);
  stages[OL]->Unroll(5);
}

void CudaScheduleConv(poly::StageMap stages,
                      ir::Tensor &input_pad,
                      ir::Tensor &weights,
                      ir::Tensor &output,
                      const common::Target &target) {
  auto &res = ScheduleParam::get_cuda_instance().GetParam();
2321 2322
  int n = output->shape[0].as_int32();
  int c = output->shape[1].as_int32();
2323 2324 2325
  optim::Simplify(&(output->shape[2]));
  int h = output->shape[2].as_int32();
  optim::Simplify(&(output->shape[3]));
2326
  int w = output->shape[3].as_int32();
2327 2328
  int rc = input_pad->shape[1].as_int32();

2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
  std::string key = "CudaDirectConvSchedule " +
                    std::to_string(input_pad->shape[0].as_int32()) + " " +
                    std::to_string(input_pad->shape[1].as_int32()) + " " +
                    std::to_string(input_pad->shape[2].as_int32()) + " " +
                    std::to_string(input_pad->shape[3].as_int32()) + " " +
                    std::to_string(weights->shape[0].as_int32()) + " " +
                    std::to_string(weights->shape[1].as_int32()) + " " +
                    std::to_string(weights->shape[2].as_int32()) + " " +
                    std::to_string(weights->shape[3].as_int32()) + " " +
                    std::to_string(output->shape[0].as_int32()) + " " +
                    std::to_string(output->shape[1].as_int32()) + " " +
                    std::to_string(output->shape[2].as_int32()) + " " +
                    std::to_string(output->shape[3].as_int32());
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
  if (res.count(key) == 0) {
    VLOG(3) << "Didn't find saved param, key is: " << key;
  } else {
    VLOG(3) << "Find saved param! key is: " << key;
    CudaScheduleConv2(stages, input_pad, weights, output, target, key);
    return;
  }
  stages[input_pad]->ComputeInline();
  if (stages[weights]->has_expression()) {
    stages[weights]->ComputeInline();
  }
2353 2354
  int f_inner = GetInnerSplitter(c, h);
  int block_z = SplitEven(c / f_inner);
2355 2356 2357 2358 2359
  int thread_z = c / f_inner / block_z;

  int rc_factor = SplitEven(rc);
  while (w * thread_z > 1024 && thread_z % 2 == 0) {
    thread_z = thread_z / 2;
2360
    f_inner = f_inner * 2;
2361 2362 2363 2364
  }
  CHECK_LE(w * thread_z, 1024) << "Wrong Param of Conv2d!";
  auto OL = stages[output]->CacheWrite("local", stages, output);

2365 2366
  auto tx = stages[output]->axis(3);
  auto by = stages[output]->axis(2);
2367
  auto tem_fi = stages[output]->Split(1, f_inner);
2368 2369
  auto &tem = std::get<0>(tem_fi);
  auto &fi = std::get<1>(tem_fi);
2370 2371

  auto bz_tz = stages[output]->Split(1, thread_z);
2372 2373
  auto &bz = std::get<0>(bz_tz);
  auto &tz = std::get<1>(bz_tz);
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399

  stages[output]->Reorder({bz, by, tz, tx, fi});
  stages[output]->Bind(1, "blockIdx.z");
  stages[output]->Bind(2, "blockIdx.y");
  stages[output]->Bind(3, "threadIdx.z");
  stages[output]->Bind(4, "threadIdx.x");
  stages[OL]->ComputeAt(stages[output], 4);
  stages[OL]->Split(6, rc_factor);
}

void CudaScheduleConv2(poly::StageMap stages,
                       ir::Tensor &input_pad,
                       ir::Tensor &weights,
                       ir::Tensor &output,
                       const common::Target &target,
                       const std::string &key) {
  auto &res = ScheduleParam::get_cuda_instance().GetParam();
  stages[input_pad]->ComputeInline();
  optim::Simplify(&(output->shape[2]));
  optim::Simplify(&(output->shape[3]));

  std::vector<ir::Tensor> readers{output};
  auto PR = stages[input_pad]->CacheRead("shared", readers, stages);
  auto KR = stages[weights]->CacheRead("shared", readers, stages);
  auto OL = stages[output]->CacheWrite("local", stages, output);

2400 2401 2402
  auto &x_param = res[key]["x"];
  auto &y_param = res[key]["y"];
  auto &f_param = res[key]["f"];
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
  auto &rx_param = res[key]["rx"];
  auto &ry_param = res[key]["ry"];
  auto &rc_param = res[key]["rc"];

  // x param is :  [1, 7, 16, 1]
  stages[output]->Split(3, x_param[3]);
  stages[output]->Split(3, x_param[2]);
  stages[output]->Split(3, x_param[1]);

  // y param is :  [112, 1, 1, 1]
  stages[output]->Split(2, y_param[3]);
  stages[output]->Split(2, y_param[2]);
  stages[output]->Split(2, y_param[1]);

  // f param is :  [1, 4, 8, 2]
  stages[output]->Split(1, f_param[3]);
  stages[output]->Split(1, f_param[2]);
  stages[output]->Split(1, f_param[1]);

  stages[output]->Reorder({0, 1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12});
  stages[output]->Bind(1, "blockIdx.z");
  stages[output]->Bind(2, "blockIdx.y");
  stages[output]->Bind(3, "blockIdx.x");
  stages[output]->Bind(7, "threadIdx.z");
  stages[output]->Bind(8, "threadIdx.y");
  stages[output]->Bind(9, "threadIdx.x");
  stages[output]->Unroll(10);
  stages[output]->Unroll(11);
  stages[output]->Unroll(12);

  stages[OL]->ComputeAt(stages[output], 9);

  // rx param is :  [1, 7]
  stages[OL]->Split(15, rx_param[1]);
  // ry param is :  [7, 1]
  stages[OL]->Split(14, ry_param[1]);
  // rc param is :  [3, 1]
  stages[OL]->Split(13, rc_param[1]);

  stages[OL]->Reorder({13, 15, 17, 14, 16, 18, 10, 11, 12});

  auto OL_init = OL->GetInitTensor(stages, target);
  stages[PR]->ComputeAt(stages[OL], 12);
  stages[KR]->ComputeAt(stages[OL], 12);

  stages[PR]->SyncThreads(12, {OL_init}, stages);
  stages[KR]->CtrlDepend(PR);
  stages[KR]->SyncThreads(stages);

  if (stages[PR]->n_out_dims() == 18) {
    stages[PR]->Fuse({13, 14, 15, 16, 17});
  } else if (stages[PR]->n_out_dims() == 19) {
    stages[PR]->Fuse({13, 14, 15, 16, 17, 18});
  } else {
2457 2458
    LOG(FATAL) << "PR number of output dims is wrong: "
               << stages[PR]->n_out_dims();
2459 2460 2461 2462 2463 2464 2465
  }

  if (stages[KR]->n_out_dims() == 18) {
    stages[KR]->Fuse({13, 14, 15, 16, 17});
  } else if (stages[KR]->n_out_dims() == 19) {
    stages[KR]->Fuse({13, 14, 15, 16, 17, 18});
  } else {
2466 2467
    LOG(FATAL) << "KR number of output dims is wrong: "
               << stages[KR]->n_out_dims();
2468 2469 2470 2471 2472 2473
  }
  int thread_z = f_param[2];
  int thread_x = x_param[2];
  if (stages[PR]->GetDimRange(13) <= thread_z) {
    stages[PR]->Bind(13, "threadIdx.z");
  } else {
2474 2475
    stages[PR]->Split(13,
                      GetMaxSplitter(stages[PR]->GetDimRange(13), thread_z));
2476 2477 2478 2479 2480 2481
    stages[PR]->Bind(14, "threadIdx.z");
    stages[PR]->Unroll(13);
  }
  if (stages[KR]->GetDimRange(13) <= thread_x) {
    stages[KR]->Bind(13, "threadIdx.x");
  } else {
2482 2483
    stages[KR]->Split(13,
                      GetMaxSplitter(stages[KR]->GetDimRange(13), thread_x));
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
    stages[KR]->Bind(14, "threadIdx.x");
    stages[KR]->Unroll(13);
  }
  stages[output]->Unroll(4);
  stages[output]->Unroll(5);
  stages[output]->Unroll(6);

  stages[OL]->Unroll(4);
  stages[OL]->Unroll(5);
  stages[OL]->Unroll(6);
  stages[OL]->Unroll(10);
  stages[OL]->Unroll(11);
  stages[OL]->Unroll(12);
  stages[OL]->Unroll(13);
  stages[OL]->Unroll(14);
  stages[OL]->Unroll(15);
  stages[OL]->Unroll(16);
  stages[OL]->Unroll(17);

  stages[PR]->Unroll(4);
  stages[PR]->Unroll(5);
  stages[PR]->Unroll(6);
  stages[PR]->Unroll(10);
  stages[PR]->Unroll(11);
  stages[PR]->Unroll(12);

  stages[KR]->Unroll(4);
  stages[KR]->Unroll(5);
  stages[KR]->Unroll(6);
  stages[KR]->Unroll(10);
  stages[KR]->Unroll(11);
  stages[KR]->Unroll(12);
}

void CudaScheduleWinogradConv(poly::StageMap wino_stages,
                              std::vector<ir::Tensor> &all_tensors,
                              const common::Target &target) {
2521
  auto &res = ScheduleParam::get_cuda_instance().GetParam();
2522
  auto &wino_weights_dilation = all_tensors[0];
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
  auto &wino_input_pad = all_tensors[1];
  auto &wino_A = all_tensors[2];
  auto &wino_B = all_tensors[3];
  auto &wino_G = all_tensors[4];
  auto &kernel_pack = all_tensors[5];
  auto &input_tile = all_tensors[6];
  auto &data_pack = all_tensors[7];
  auto &bgemm = all_tensors[8];
  auto &inverse = all_tensors[9];
  auto &wino_conv = all_tensors[10];
2533
  std::string key =
2534 2535 2536 2537 2538
      "CudaWinogradConvSchedule " +
      std::to_string(wino_input_pad->shape[0].as_int32()) + " " +
      std::to_string(wino_input_pad->shape[1].as_int32()) + " " +
      std::to_string(wino_input_pad->shape[2].as_int32()) + " " +
      std::to_string(wino_input_pad->shape[3].as_int32()) + " " +
2539 2540 2541 2542
      std::to_string(wino_weights_dilation->shape[0].as_int32()) + " " +
      std::to_string(wino_weights_dilation->shape[1].as_int32()) + " " +
      std::to_string(wino_weights_dilation->shape[2].as_int32()) + " " +
      std::to_string(wino_weights_dilation->shape[3].as_int32()) + " " +
2543 2544 2545 2546
      std::to_string(wino_conv->shape[0].as_int32()) + " " +
      std::to_string(wino_conv->shape[1].as_int32()) + " " +
      std::to_string(wino_conv->shape[2].as_int32()) + " " +
      std::to_string(wino_conv->shape[3].as_int32());
2547 2548 2549
  VLOG(1) << "Key in CudaScheduleWinogradConv is : " << key;
  CHECK_GT(res.count(key), 0);
  auto &rc_param = res[key]["rc"];
2550 2551 2552
  auto &x_param = res[key]["x"];
  auto &y_param = res[key]["y"];
  auto &b_param = res[key]["b"];
2553 2554 2555

  wino_stages[wino_B]->ComputeInline();

2556 2557
  auto data_l =
      wino_stages[data_pack]->CacheWrite("local", wino_stages, data_pack);
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676

  wino_stages[data_pack]->Split(3, 1);
  wino_stages[data_pack]->Fuse({2, 3});
  wino_stages[data_pack]->Split(2, 128);
  wino_stages[data_pack]->Reorder({2, 3, 4, 0, 1});
  wino_stages[data_pack]->Bind(0, "blockIdx.x");
  wino_stages[data_pack]->Bind(1, "threadIdx.x");
  wino_stages[input_tile]->SetBuffer("local");
  wino_stages[data_l]->ComputeAt(wino_stages[data_pack], 2);
  wino_stages[input_tile]->ComputeAt(wino_stages[data_l], 2);
  wino_stages[wino_input_pad]->ComputeInline();

  wino_stages[wino_G]->ComputeInline();

  wino_stages[kernel_pack]->Fuse({2, 3});
  wino_stages[kernel_pack]->Split(2, 128);
  wino_stages[kernel_pack]->Reorder({2, 3, 0, 1});

  wino_stages[kernel_pack]->Bind(0, "blockIdx.x");
  wino_stages[kernel_pack]->Bind(1, "threadIdx.x");

  wino_stages[wino_weights_dilation]->ComputeInline();

  auto wino_OL = wino_stages[bgemm]->CacheWrite("local", wino_stages, bgemm);
  std::vector<ir::Tensor> readers{wino_OL};
  auto AA = wino_stages[kernel_pack]->CacheRead("shared", readers, wino_stages);
  auto BB = wino_stages[data_pack]->CacheRead("shared", readers, wino_stages);

  wino_stages[bgemm]->Fuse({0, 1});

  wino_stages[bgemm]->SplitOuter(0, 1);

  // x param is :  [1, 1, 8, 2]
  wino_stages[bgemm]->Split(3, x_param[3]);
  wino_stages[bgemm]->Split(3, x_param[2]);
  wino_stages[bgemm]->Split(3, x_param[1]);
  // y param is :  [8, 1, 16, 4]
  wino_stages[bgemm]->Split(2, y_param[3]);
  wino_stages[bgemm]->Split(2, y_param[2]);
  wino_stages[bgemm]->Split(2, y_param[1]);
  // b param is :  [16, 1, 1, 1]
  wino_stages[bgemm]->Split(1, b_param[3]);
  wino_stages[bgemm]->Split(1, b_param[2]);
  wino_stages[bgemm]->Split(1, b_param[1]);

  wino_stages[bgemm]->Reorder({0, 1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12});

  wino_stages[bgemm]->Bind(1, "blockIdx.z");
  wino_stages[bgemm]->Bind(2, "blockIdx.y");
  wino_stages[bgemm]->Bind(3, "blockIdx.x");
  wino_stages[bgemm]->Bind(7, "threadIdx.z");
  wino_stages[bgemm]->Bind(8, "threadIdx.y");
  wino_stages[bgemm]->Bind(9, "threadIdx.x");

  wino_stages[wino_OL]->ComputeAt(wino_stages[bgemm], 9);

  // rc param is :  [32, 16]
  wino_stages[wino_OL]->Split(12, rc_param[1]);

  wino_stages[wino_OL]->Reorder({12, 13, 10, 11});

  auto bgemm_init = wino_OL->GetInitTensor(wino_stages, target);
  wino_stages[AA]->ComputeAt(wino_stages[wino_OL], 10);
  wino_stages[BB]->ComputeAt(wino_stages[wino_OL], 10);

  wino_stages[AA]->Fuse(11, 12);
  wino_stages[AA]->SplitOuter(11, x_param[2]);
  wino_stages[AA]->Bind(11, "threadIdx.x");

  wino_stages[BB]->Fuse(11, 12);
  wino_stages[BB]->SplitOuter(11, y_param[2]);
  wino_stages[BB]->Bind(11, "threadIdx.y");

  wino_stages[AA]->SyncThreads(10, {bgemm_init}, wino_stages);
  wino_stages[BB]->SyncThreads(wino_stages);

  int m = 2;

  wino_stages[wino_conv]->Tile(2, 3, m, m);
  wino_stages[wino_conv]->Reorder({2, 4, 3, 5});
  wino_stages[wino_conv]->FuseDirect({0, 1, 2, 3});
  wino_stages[wino_conv]->Split(0, 128);
  wino_stages[wino_conv]->Bind(0, "blockIdx.x");
  wino_stages[wino_conv]->Bind(1, "threadIdx.x");
  wino_stages[wino_A]->ComputeInline();

  wino_stages[inverse]->SetBuffer("local");
  wino_stages[inverse]->Fuse(0, 1);
  wino_stages[inverse]->Split(0, 128);
  wino_stages[inverse]->Bind(0, "blockIdx.x");
  wino_stages[inverse]->Bind(1, "threadIdx.x");
  wino_stages[inverse]->SimpleComputeAt(wino_stages[wino_conv], 1);
}

int gcd(int a, int b) {
  int r;
  while (b > 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int MaxFactorLessThan(int a, int b) {
  CHECK_GT(a, b);
  int res = 1;
  for (int i = 2; i <= (int)sqrt((double)a); i++) {
    if (a % i == 0) {
      if (i <= b) res = std::max(res, i);
      if (a / i <= b) res = std::max(res, a / i);
    }
  }
  return res;
}

void CudaScheduleInjectiveWithVectorize(poly::Stage *stage,
                                        const std::vector<int> &output_shape,
                                        const common::Target &target) {
2677 2678 2679
  int dims = stage->n_out_dims();
  int prod_size = std::accumulate(
      output_shape.begin(), output_shape.end(), 1, std::multiplies<int>());
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
  int num_thread = target.max_num_threads();
  int last_shape = stage->GetDimRange(stage->n_out_dims() - 1);
  // determine the factor of vectorize
  int vector_width = 1;
  if (last_shape % 4 == 0) {
    vector_width = 4;
  } else if (last_shape % 2 == 0) {
    vector_width = 2;
  }

  // print range of stage for debug
  auto range_str_fn = [stage]() {
    std::vector<int> dim_ranges;
    for (int i = 0; i < stage->n_out_dims(); ++i) {
      dim_ranges.push_back(stage->GetDimRange(i));
    }
    std::string res = "[" + utils::Join(dim_ranges, ",") + "]";
    return res;
  };

  // the first bind position from tail
  int bind_idx = stage->n_out_dims() - 1;
  // it will add a new dim by split before vectorize, but the new dim will
  // be eleminated when vectorizng, so the bind_idx does't need to increase
  if (vector_width > 1) {
    stage->Split(bind_idx, vector_width);
  }
  VLOG(5) << "vectorize result:" << range_str_fn();

  // revise dim for binding threadIdx.x, here only use the x of threadIdx
  if (stage->GetDimRange(bind_idx) > num_thread) {
    stage->Split(bind_idx, gcd(stage->GetDimRange(bind_idx), num_thread));
    ++bind_idx;
  }
2714 2715 2716
  while (bind_idx > 0 &&
         stage->GetDimRange(bind_idx - 1) * stage->GetDimRange(bind_idx) <
             num_thread) {
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
    stage->Fuse(bind_idx - 1, bind_idx);
    --bind_idx;
  }
  // call vectorize on the last dim
  if (vector_width > 1) {
    stage->Vectorize(stage->n_out_dims() - 1, vector_width);
  }
  stage->Bind(bind_idx, "threadIdx.x");
  --bind_idx;
  VLOG(5) << "bind threadIdx.x result:" << range_str_fn();

  // revise dim for binding blockIdx, at most 3 indexes can be used
  while (bind_idx > 2) {
    stage->Fuse(bind_idx - 1, bind_idx);
    --bind_idx;
  }
  std::string block_idx = "blockIdx.x";
  for (int j = 0; bind_idx >= 0; ++j) {
    block_idx.back() = 'x' + j;
    stage->Bind(bind_idx, block_idx);
    --bind_idx;
  }
2739 2740 2741 2742
  VLOG(5) << "CudaScheduleInjectiveWithVectorize tensor:"
          << stage->tensor()->name << ", vector_width:" << vector_width
          << ", prod_size:" << prod_size << ", shape:["
          << utils::Join(output_shape, ",") << "]"
2743 2744 2745
          << ", range:" << range_str_fn();
}

2746 2747 2748 2749 2750
void CudaScheduleInjective(poly::Stage *stage,
                           const std::vector<int> &output_shape,
                           const common::Target &target) {
  CHECK_EQ(stage->n_out_dims(), stage->n_in_dims())
      << "The dims of op are not equal";
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
  if (FLAGS_cinn_use_cuda_vectorize) {
    CudaScheduleInjectiveWithVectorize(stage, output_shape, target);
    return;
  }
  int dims = stage->n_out_dims();
  for (int i = 1; i < dims; i++) {
    stage->Fuse(0, 1);
  }

  int num_thread = target.max_num_threads();
2761 2762
  int prod_size = std::accumulate(
      output_shape.begin(), output_shape.end(), 1, std::multiplies<int>());
2763 2764 2765 2766 2767 2768 2769 2770
  if (prod_size <= num_thread) {
    stage->Bind(0, "threadIdx.x");
    return;
  }
  int new_num_thread = gcd(prod_size, num_thread);
  if (new_num_thread % 32 != 0) {
    new_num_thread = MaxFactorLessThan(prod_size, num_thread);
  }
2771 2772
  if (new_num_thread == 1)
    LOG(FATAL) << "prod_size out of range: " << prod_size;
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816

  CHECK_GT(prod_size, new_num_thread);
  stage->Split(0, new_num_thread);
  stage->Bind(0, "blockIdx.x");
  stage->Bind(1, "threadIdx.x");
}

void CudaSplitSchedule(common::CINNValuePack *arg_pack,
                       const std::vector<std::vector<int>> &output_shapes,
                       int axis,
                       const common::Target &target) {
  poly::StageMap stages = arg_pack->back();
  std::vector<ir::Tensor> out_tensors;
  int dims = output_shapes[0].size();
  for (int i = 0; i < arg_pack->size() - 1; ++i) {
    Expr Out = (*arg_pack)[i];
    CHECK(Out.as_tensor());
    out_tensors.push_back(Out.as_tensor_ref());
  }
  std::vector<int> reorders;
  for (int i = 0; i < dims; i++) {
    reorders.push_back(i);
  }
  reorders.erase(reorders.begin() + axis);
  reorders.push_back(axis);
  for (auto &out : out_tensors) {
    stages[out]->Reorder(reorders);
  }
  auto last_output = out_tensors.back();

  std::vector<int> fuse_index;
  for (int i = 0; i < dims - 1; i++) fuse_index.push_back(i);
  for (auto &out : out_tensors) stages[out]->Fuse(fuse_index);
  int fused_shape = 1;
  for (int i = 0; i < dims; i++) {
    if (i != axis) fused_shape = fused_shape * output_shapes[0][i];
  }
  int compute_at_level = 0;
  if (target.arch == Target::Arch::NVGPU) {
    if (fused_shape > target.max_num_threads()) {
      stages[last_output]->Split(0, target.max_num_threads());
      stages[last_output]->Bind(0, "blockIdx.x");
      stages[last_output]->Bind(1, "threadIdx.x");
      compute_at_level++;
2817
    } else {
2818
      stages[last_output]->Bind(0, "threadIdx.x");
2819
    }
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
  }

  for (int i = 0; i < out_tensors.size() - 1; i++) {
    stages[out_tensors[i]]->ComputeAt2(stages[last_output], compute_at_level);
  }
}

}  // namespace pe
}  // namespace hlir
}  // namespace cinn