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.

save_gpu_param.py 3.2 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. """save gpu param"""
  17. import os
  18. import hashlib
  19. import akg.tvm
  20. from akg.tvm import schedule
  21. from akg.utils import validation_check as vc_util
  22. def get_dim(dim, axis=True):
  23. """get dim info"""
  24. dims_str = {
  25. "grid_dim0": "// attr [iter_var(blockIdx.x, , blockIdx.x)] thread_extent = ",
  26. "grid_dim1": "// attr [iter_var(blockIdx.y, , blockIdx.y)] thread_extent = ",
  27. "grid_dim2": "// attr [iter_var(blockIdx.z, , blockIdx.z)] thread_extent = ",
  28. "block_dim0": "// attr [iter_var(threadIdx.x, , threadIdx.x)] thread_extent = ",
  29. "block_dim1": "// attr [iter_var(threadIdx.y, , threadIdx.y)] thread_extent = ",
  30. "block_dim2": "// attr [iter_var(threadIdx.z, , threadIdx.z)] thread_extent = "
  31. }
  32. dim_to_axis = {
  33. "grid_dim0": '"blockIdx.x" : ',
  34. "grid_dim1": '"blockIdx.y" : ',
  35. "grid_dim2": '"blockIdx.z" : ',
  36. "block_dim0": '"threadIdx.x" : ',
  37. "block_dim1": '"threadIdx.y" : ',
  38. "block_dim2": '"threadIdx.z" : '
  39. }
  40. if axis:
  41. return dim_to_axis.get(dim)
  42. return dims_str.get(dim)
  43. def parse_params(file, dim, ir):
  44. """parse parameters"""
  45. dim_str = get_dim(dim, axis=False)
  46. pos = ir.find(dim_str)
  47. if pos != -1:
  48. index = pos + len(dim_str)
  49. param_temp = get_dim(dim)
  50. while ir[index].isdigit():
  51. param_temp += ir[index]
  52. index += 1
  53. file.write(param_temp + ",\n")
  54. else:
  55. param_temp = get_dim(dim) + '1'
  56. file.write(param_temp + ",\n")
  57. @vc_util.check_input_type(schedule.Schedule, (list, tuple), tuple)
  58. def save_gpu_params(s, args, kernel_info):
  59. """save gpu parameters"""
  60. ptx_code = kernel_info[0]
  61. file_name = kernel_info[1]
  62. kernel_name = kernel_info[2]
  63. ir = str(akg.tvm.lower(s, args, simple_mode=True))
  64. file_path = os.path.realpath(file_name)
  65. if os.path.exists(file_path):
  66. os.remove(file_path)
  67. sha256 = hashlib.sha256()
  68. sha256.update(ptx_code.encode("utf-8"))
  69. hash_str = sha256.hexdigest()
  70. with os.fdopen(os.open(file_path, os.O_WRONLY | os.O_CREAT, 0o400), 'w') as fo:
  71. fo.write("{\n")
  72. fo.write('"kernelName" : ' + '"' + kernel_name + "_kernel0" + '",\n')
  73. parse_params(fo, "grid_dim0", ir)
  74. parse_params(fo, "grid_dim1", ir)
  75. parse_params(fo, "grid_dim2", ir)
  76. parse_params(fo, "block_dim0", ir)
  77. parse_params(fo, "block_dim1", ir)
  78. parse_params(fo, "block_dim2", ir)
  79. fo.write('"sha256" : ' + '"' + hash_str + '"\n')
  80. fo.write("}\n")