You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

build_module.py 7.4 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3. # Copyright 2019 Huawei Technologies Co., Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """
  17. The build utils in python.
  18. This module provides the functions to transform schedule to
  19. LoweredFunc and compiled Module.
  20. """
  21. from __future__ import absolute_import as _abs
  22. import sys
  23. import logging
  24. from akg.utils import validation_check as vc_util
  25. import akg.tvm
  26. from akg.tvm import _api_internal
  27. from akg.tvm import schedule
  28. tuning_spaces = None
  29. help_tiling_level = {
  30. "None": 0, "General": 1, "Candidates": 2, "Tuning": 3
  31. }
  32. EMPTY_CODE = 0
  33. L0_DEFAULT_TILING = 1
  34. def dump_tiling_info(level):
  35. """Dump tiling info."""
  36. if tuning_spaces is None:
  37. return
  38. logging.getLogger().setLevel(logging.INFO)
  39. if level >= help_tiling_level["General"]:
  40. logging.info("==========General tiling help info=============")
  41. indice = tuning_spaces["index"]
  42. if isinstance(indice, list):
  43. for i in range(len(indice)):
  44. info = "index %d, axis %d, l1_tile_ranges [%d, %d](jump by %d),l0_tile_ranges [%d, %d](jump by %d)"
  45. logging.info(info, tuning_spaces["index"][i][0], tuning_spaces["index"][i][1],
  46. tuning_spaces["c1_range"][i][0], tuning_spaces["c1_range"][i][1],
  47. tuning_spaces["c1_mod"][i][0], tuning_spaces["c0_range"][i][0],
  48. tuning_spaces["c0_range"][i][1], tuning_spaces["c0_mod"][i][0],
  49. )
  50. idx_to_str = {0: "x", 1: "y", 2: "z"}
  51. for i in range(len(tuning_spaces["thread_range"])):
  52. info = "[thread.%s] range [%d, %d](jump by %d), "
  53. logging.info(info, idx_to_str[i], tuning_spaces["thread_range"][i][0], tuning_spaces["thread_range"][i][1],
  54. tuning_spaces['thread_mod'][i][0], )
  55. for i in range(len(tuning_spaces["block_range"])):
  56. info = "[block.%s] range [%d, %d](jump by %d)"
  57. logging.info(info, idx_to_str[i], tuning_spaces["block_range"][i][0],
  58. tuning_spaces["block_range"][i][1], tuning_spaces['block_mod'][i][0],)
  59. logging.info("===============================================")
  60. elif isinstance(indice, int) and indice == EMPTY_CODE:
  61. logging.info("Empty tiling space.")
  62. if level >= help_tiling_level["Candidates"]:
  63. logging.info("")
  64. logging.info("==========Detailed tiling help info(Only L1)=============")
  65. logging.info("index 0 has %d candidate(s) tiling factors", len(tuning_spaces["tuning_space"]))
  66. tuning_spaces_len = len(tuning_spaces["tuning_space"])
  67. for i in range(tuning_spaces_len):
  68. info = "candidate %d:("
  69. for l1_candidate in tuning_spaces["tuning_space"][i]:
  70. info += ("(" + str(l1_candidate) + ", " + str(L0_DEFAULT_TILING) + "),")
  71. info += ")"
  72. logging.info(info, i)
  73. logging.info("=============================================================")
  74. logging.info("")
  75. logging.info("Please read this tiling help info and set tiling factor.")
  76. logging.info("And then set attr \"help_tiling\" value to 0 and re-run.")
  77. logging.info("Exit.")
  78. sys.exit()
  79. def build_config(**kwargs):
  80. """build config."""
  81. return akg.tvm.build_config(**kwargs)
  82. @vc_util.check_input_type(schedule.Schedule, (list, tuple), (list, tuple), str,
  83. (dict, type(None)), (dict, type(None)), bool, bool, bool, str)
  84. def lower(sch, args, shape_params=None, name="default_function", binds=None, attrs=None,
  85. simple_mode=False, polyhedral=False, tuning=False, target="cce"):
  86. """Lowering function."""
  87. tmp_binds = None
  88. if binds is not None:
  89. tmp_binds = None if not bool(binds) else binds
  90. tmp_attrs = None
  91. if attrs is not None:
  92. tmp_attrs = None if not bool(attrs) else attrs
  93. if shape_params is None:
  94. shape_params = []
  95. cfg = _api_internal._GetCurrentBuildConfig()
  96. ret = _api_internal._Lower(sch, args, shape_params, name,
  97. tmp_binds, tmp_attrs, simple_mode,
  98. polyhedral, tuning, target, cfg)
  99. level = tmp_attrs.get("help_tiling")
  100. if tuning or (level is not None and level > help_tiling_level['None']):
  101. level = help_tiling_level['Tuning'] if tuning else level
  102. global tuning_spaces
  103. tuning_spaces = {}
  104. tuning_spaces["index"] = ret.index_table.asnumpy().tolist()
  105. tuning_spaces["c1_range"] = ret.c1_tile_range_table.asnumpy().tolist()
  106. tuning_spaces["c0_range"] = ret.c0_tile_range_table.asnumpy().tolist()
  107. tuning_spaces["c1_mod"] = ret.c1_tile_mod_table.asnumpy().tolist()
  108. tuning_spaces["c0_mod"] = ret.c0_tile_mod_table.asnumpy().tolist()
  109. tuning_spaces["thread_range"] = ret.gpu_thread_range_table.asnumpy().tolist()
  110. tuning_spaces["block_range"] = ret.gpu_block_range_table.asnumpy().tolist()
  111. tuning_spaces["thread_mod"] = ret.gpu_thread_mod_table.asnumpy().tolist()
  112. tuning_spaces["block_mod"] = ret.gpu_block_mod_table.asnumpy().tolist()
  113. if level >= help_tiling_level["Candidates"]:
  114. tuning_spaces["tuning_space"] = ret.tiling_candidate.asnumpy().tolist()
  115. if not tuning:
  116. dump_tiling_info(level)
  117. return ret
  118. @vc_util.check_input_type(schedule.Schedule, (list, tuple), (list, tuple, type(None)), str,
  119. (dict, akg.tvm.container.Map, type(None)), (dict, type(None)), bool, str)
  120. def build_to_func(inputs, args, shape_params=None, name="default_function",
  121. binds=None, attrs=None, polyhedral=False, target="cce"):
  122. """Build module."""
  123. tmp_binds = None
  124. if binds is not None:
  125. tmp_binds = None if not bool(binds) else binds
  126. tmp_attrs = None
  127. if attrs is not None:
  128. tmp_attrs = None if not bool(attrs) else attrs
  129. for arg in args:
  130. vc_util.tensor_max_size_check(arg)
  131. if shape_params is None:
  132. shape_params = []
  133. cfg = _api_internal._GetCurrentBuildConfig()
  134. return _api_internal._BuildToFunc(inputs, args, shape_params, name, tmp_binds, tmp_attrs,
  135. polyhedral, target, cfg)
  136. @vc_util.check_input_type(schedule.Schedule, (list, tuple), str, (list, tuple), str,
  137. (dict, akg.tvm.container.Map, type(None)), (dict, type(None)), bool)
  138. def build(inputs, args, target='cce', shape_params=None, name="default_function",
  139. binds=None, attrs=None, polyhedral=False):
  140. tmp_rst = build_to_func(inputs, args, shape_params=shape_params, name=name, binds=binds,
  141. attrs=attrs, polyhedral=polyhedral, target=target)
  142. return _api_internal._BuildToModule(tmp_rst, target)