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.

gen_cuda_batch_conv_bias_kern_impls.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import argparse
  5. import itertools
  6. PREFIXES = {"dp4a": [("batch_conv_bias_int8_implicit_gemm_precomp_ncdiv4hw4", True), ("batch_conv_bias_int8_gemm_ncdiv4hw4", False), ("batch_conv_bias_int8_gemm_ncdiv4hw4_ldg_128", False)]}
  7. ACTIVATIONS = {1: ("IDENTITY", "_id"),
  8. 2: ("RELU", "_relu"),
  9. 3: ("H_SWISH", "_hswish")}
  10. BIASES = {1: ("PerElementBiasVisitor", "_per_elem"),
  11. 2: ("PerChannelBiasVisitor", "_per_chan")}
  12. SUFFIXES = {"dp4a": [""],
  13. "imma": [""]}
  14. def main():
  15. parser = argparse.ArgumentParser(
  16. description='generate cuda batch conv bias (dp4a/imma) kern impl files',
  17. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  18. parser.add_argument('--type', type=str, choices=['dp4a',
  19. 'imma'],
  20. default='dp4a', help='generate cuda conv bias kernel file')
  21. parser.add_argument('output', help='output directory')
  22. args = parser.parse_args()
  23. if not os.path.isdir(args.output):
  24. os.makedirs(args.output)
  25. inst = '''
  26. template void megdnn::cuda::batch_conv_bias::do_PREFIXSUFFIX<BIAS,
  27. IConvEpilogue<Activation<megdnn::param_enumv::BatchConvBias::NonlineMode::ACTIVATION>>>(
  28. const int8_t* d_src,
  29. const int8_t* d_filter, WORKSPACE
  30. BIAS bias,
  31. IConvEpilogue<Activation<megdnn::param_enumv::BatchConvBias::NonlineMode::ACTIVATION>> epilogue,
  32. const ConvParam& param,
  33. float alpha,
  34. float beta,
  35. cudaStream_t stream);'''
  36. for prefix in PREFIXES[args.type]:
  37. for suffix in SUFFIXES[args.type]:
  38. for _, act in ACTIVATIONS.items():
  39. has_workspace = prefix[1]
  40. bias = BIASES[2]
  41. fname = "{}{}{}{}.cu".format(prefix[0], suffix, bias[1], act[1])
  42. fname = os.path.join(args.output, fname)
  43. with open(fname, "w") as fout:
  44. w = lambda s: print(s, file=fout)
  45. w('// generated by gen_batch_cuda_conv_bias_kern_impls.py')
  46. cur_inst = inst.replace("PREFIX", prefix[0]).replace("SUFFIX", suffix).replace("BIAS", bias[0]).replace("ACTIVATION", act[0])
  47. if has_workspace:
  48. cur_inst = cur_inst.replace("WORKSPACE", "\nint* d_workspace, ")
  49. else:
  50. cur_inst = cur_inst.replace("WORKSPACE", "")
  51. w('#include "../{}{}.cuinl"'.format(prefix[0], suffix))
  52. w(cur_inst)
  53. print('generated {}'.format(fname))
  54. os.utime(args.output)
  55. if __name__ == '__main__':
  56. main()

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台