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.

generator.py 38 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. #
  2. # \file generator.py
  3. #
  4. # \brief Generates the CUTLASS Library's instances
  5. #
  6. import enum
  7. import os.path
  8. import shutil
  9. import argparse
  10. import platform
  11. from library import *
  12. from manifest import *
  13. ###################################################################################################
  14. #
  15. def CudaToolkitVersionSatisfies(semantic_ver_string, major, minor, patch = 0):
  16. # by default, use the latest CUDA Toolkit version
  17. cuda_version = [11, 0, 132]
  18. # Update cuda_version based on parsed string
  19. if semantic_ver_string != '':
  20. for i, x in enumerate([int(x) for x in semantic_ver_string.split('.')]):
  21. if i < len(cuda_version):
  22. cuda_version[i] = x
  23. else:
  24. cuda_version.append(x)
  25. return cuda_version >= [major, minor, patch]
  26. ###################################################################################################
  27. ###################################################################################################
  28. #
  29. def CreateGemmOperator(manifest, layouts, tile_descriptions, data_type, \
  30. alignment_constraints, complex_transforms = None, epilogue_functor = EpilogueFunctor.LinearCombination, \
  31. swizzling_functor = SwizzlingFunctor.Identity8):
  32. if complex_transforms is None:
  33. complex_transforms = [(ComplexTransform.none, ComplexTransform.none),]
  34. element_a, element_b, element_c, element_epilogue = data_type
  35. operations = []
  36. # by default, only generate the largest tile and largest alignment
  37. if manifest.args.kernels == '':
  38. tile_descriptions = [tile_descriptions[0],]
  39. alignment_constraints = [alignment_constraints[0],]
  40. for layout in layouts:
  41. for tile_description in tile_descriptions:
  42. for alignment in alignment_constraints:
  43. for complex_transform in complex_transforms:
  44. alignment_c = min(8, alignment)
  45. A = TensorDescription(element_a, layout[0], alignment, complex_transform[0])
  46. B = TensorDescription(element_b, layout[1], alignment, complex_transform[1])
  47. C = TensorDescription(element_c, layout[2], alignment_c)
  48. new_operation = GemmOperation(GemmKind.Universal, tile_description.minimum_compute_capability, \
  49. tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor)
  50. manifest.append(new_operation)
  51. operations.append(new_operation)
  52. return operations
  53. ###########################################################################################################
  54. # ConvolutionOperator support variations
  55. # ____________________________________________________________________
  56. # ConvolutionalOperator | Analytic | Optimized
  57. # ____________________________________________________________________
  58. # | Fprop | (strided) | (strided)
  59. # | Dgrad | (strided, unity*) | (unity)
  60. # | Wgrad | (strided) | (strided)
  61. # ____________________________________________________________________
  62. #
  63. # Note : Operator marked (*) are supported but not generated to keep the instantiated kernel count low
  64. ###########################################################################################################
  65. # Convolution for 2D operations
  66. def CreateConv2dOperator(manifest, layout, tile_descriptions, data_type, alignment, \
  67. conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], epilogue_functor = EpilogueFunctor.LinearCombination):
  68. element_a, element_b, element_c, element_epilogue = data_type
  69. # one exceptional case
  70. alignment_c = min(8, alignment)
  71. # iterator algorithm (analytic and optimized)
  72. iterator_algorithms = [IteratorAlgorithm.Analytic, IteratorAlgorithm.Optimized]
  73. # by default, only generate the largest tile size
  74. if manifest.args.kernels == '':
  75. tile_descriptions = [tile_descriptions[0],]
  76. operations = []
  77. for tile in tile_descriptions:
  78. for conv_kind in conv_kinds:
  79. for iterator_algorithm in iterator_algorithms:
  80. A = TensorDescription(element_a, layout[0], alignment)
  81. B = TensorDescription(element_b, layout[1], alignment)
  82. C = TensorDescription(element_c, layout[2], alignment_c)
  83. # unity stride only for Optimized Dgrad
  84. if (iterator_algorithm == IteratorAlgorithm.Optimized) and (conv_kind == ConvKind.Dgrad):
  85. new_operation = Conv2dOperation(conv_kind, iterator_algorithm, tile.minimum_compute_capability, tile,\
  86. A, B, C, element_epilogue, StrideSupport.Unity, epilogue_functor)
  87. manifest.append(new_operation)
  88. operations.append(new_operation)
  89. # strided dgrad is not supported by Optimized Dgrad
  90. if (iterator_algorithm == IteratorAlgorithm.Optimized) and (conv_kind == ConvKind.Dgrad):
  91. continue
  92. # strided support for Fprop (Analytic/Optimized), Dgrad (Analytic), and Wgrad (Analytic)
  93. new_operation = Conv2dOperation(conv_kind, iterator_algorithm, tile.minimum_compute_capability, tile,\
  94. A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor)
  95. manifest.append(new_operation)
  96. operations.append(new_operation)
  97. return operations
  98. ###################################################################################################
  99. ###################################################################################################
  100. def GenerateConv2d_Simt(args):
  101. operations = []
  102. layouts = [
  103. (LayoutType.TensorNC4HW4, LayoutType.TensorC4RSK4),
  104. ]
  105. math_instructions = [
  106. MathInstruction( \
  107. [1, 1, 4], \
  108. DataType.s8, DataType.s8, DataType.s32, \
  109. OpcodeClass.Simt, \
  110. MathOperation.multiply_add),
  111. ]
  112. dst_layouts = [
  113. LayoutType.TensorNC4HW4,
  114. LayoutType.TensorNC32HW32,
  115. LayoutType.TensorNHWC,
  116. LayoutType.TensorNHWC,
  117. LayoutType.TensorNCHW
  118. ]
  119. dst_types = [
  120. DataType.s8,
  121. DataType.s8,
  122. DataType.u4,
  123. DataType.s4,
  124. DataType.f32,
  125. ]
  126. max_cc = 1024
  127. for math_inst in math_instructions:
  128. for layout in layouts:
  129. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  130. if dst_type == DataType.s4 or dst_type == DataType.u4:
  131. min_cc = 75
  132. use_special_optimization = SpecialOptimizeDesc.NoneSpecialOpt
  133. else:
  134. min_cc = 61
  135. use_special_optimization = SpecialOptimizeDesc.ConvFilterUnity
  136. tile_descriptions = [
  137. TileDescription([128, 128, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  138. TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  139. TileDescription([ 64, 128, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc),
  140. TileDescription([128, 32, 32], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  141. TileDescription([ 32, 128, 32], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  142. TileDescription([ 32, 64, 32], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  143. TileDescription([ 64, 32, 32], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  144. TileDescription([ 16, 128, 16], 1, [1, 1, 1], math_inst, min_cc, max_cc),
  145. TileDescription([ 16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  146. ]
  147. for tile in tile_descriptions:
  148. if dst_layout == LayoutType.TensorNC32HW32 and tile.threadblock_shape[0] > 32:
  149. continue
  150. if (dst_layout == LayoutType.TensorNCHW or dst_layout == LayoutType.TensorNHWC) \
  151. and tile.threadblock_shape[0] > 16:
  152. continue
  153. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1],
  154. dst_layout, dst_type, min_cc, 32, 32, 32,
  155. use_special_optimization)
  156. return operations
  157. def GenerateConv2d_TensorOp_8816(args):
  158. operations = []
  159. layouts = [
  160. (LayoutType.TensorNC32HW32, LayoutType.TensorC32RSK32),
  161. ]
  162. math_instructions = [
  163. MathInstruction( \
  164. [8, 8, 16], \
  165. DataType.s8, DataType.s8, DataType.s32, \
  166. OpcodeClass.TensorOp, \
  167. MathOperation.multiply_add_saturate),
  168. ]
  169. dst_layouts = [
  170. LayoutType.TensorNC32HW32,
  171. LayoutType.TensorNC4HW4,
  172. ]
  173. dst_types = [
  174. DataType.s8,
  175. DataType.s8,
  176. ]
  177. use_special_optimization = SpecialOptimizeDesc.ConvFilterUnity
  178. min_cc = 75
  179. max_cc = 1024
  180. cuda_major = 10
  181. cuda_minor = 2
  182. for math_inst in math_instructions:
  183. for layout in layouts:
  184. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  185. if dst_layout == LayoutType.TensorNC32HW32:
  186. tile_descriptions = [
  187. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  188. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  189. TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  190. TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  191. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  192. TileDescription([128, 64, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  193. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  194. ]
  195. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  196. dst_layout, dst_type, min_cc, 128, 128, 64, use_special_optimization,
  197. ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  198. else:
  199. assert dst_layout == LayoutType.TensorNC4HW4
  200. tile_descriptions = [
  201. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  202. TileDescription([ 32, 128, 32], 1, [1, 2, 1], math_inst, min_cc, max_cc),
  203. ]
  204. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  205. dst_layout, dst_type, min_cc, 128, 128, 64, use_special_optimization,
  206. ImplicitGemmMode.GemmNT, False, cuda_major, cuda_minor)
  207. layouts_nhwc = [
  208. (LayoutType.TensorNHWC, LayoutType.TensorNC4HW4, 32),
  209. (LayoutType.TensorNHWC, LayoutType.TensorNC8HW8, 64),
  210. (LayoutType.TensorNHWC, LayoutType.TensorNC16HW16, 128),
  211. ]
  212. dst_layouts_nhwc = [
  213. LayoutType.TensorNHWC,
  214. ]
  215. for math_inst in math_instructions:
  216. for layout in layouts_nhwc:
  217. for dst_layout in dst_layouts_nhwc:
  218. dst_type = math_inst.element_b
  219. tile_descriptions = [
  220. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  221. TileDescription([64, 16, 32], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  222. ]
  223. for tile in tile_descriptions:
  224. dst_align = 32 if tile.threadblock_shape[1] == 16 else 64
  225. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  226. dst_type, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  227. ImplicitGemmMode.GemmTN, False, cuda_major, cuda_minor)
  228. if tile.threadblock_shape[1] == 16 or tile.threadblock_shape[1] == 32:
  229. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  230. dst_type, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  231. ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  232. out_dtypes = [DataType.s4, DataType.u4, DataType.f32]
  233. #INT8x8x4 and INT8x8x32
  234. for math_inst in math_instructions:
  235. for layout in layouts_nhwc:
  236. for dst_layout in dst_layouts_nhwc:
  237. for out_dtype in out_dtypes:
  238. tile_descriptions = [
  239. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  240. TileDescription([64, 16, 32], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  241. ]
  242. for tile in tile_descriptions:
  243. dst_align = 4 * DataTypeSize[out_dtype] if tile.threadblock_shape[1] == 16 or out_dtype == DataType.f32 \
  244. else 8 * DataTypeSize[out_dtype]
  245. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  246. out_dtype, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  247. ImplicitGemmMode.GemmTN, False, cuda_major, cuda_minor)
  248. if tile.threadblock_shape[1] == 16 or (tile.threadblock_shape[1] == 32 and out_dtype != DataType.f32):
  249. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  250. out_dtype, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  251. ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  252. return operations
  253. def GenerateConv2d_TensorOp_8832(args):
  254. operations = []
  255. layouts = [
  256. (LayoutType.TensorNC64HW64, LayoutType.TensorC64RSK64),
  257. ]
  258. math_instructions = [
  259. MathInstruction( \
  260. [8, 8, 32], \
  261. DataType.s4, DataType.s4, DataType.s32, \
  262. OpcodeClass.TensorOp, \
  263. MathOperation.multiply_add_saturate), \
  264. MathInstruction( \
  265. [8, 8, 32], \
  266. DataType.s4, DataType.u4, DataType.s32, \
  267. OpcodeClass.TensorOp, \
  268. MathOperation.multiply_add_saturate)
  269. ]
  270. dst_layouts = [
  271. LayoutType.TensorNC64HW64,
  272. ]
  273. use_special_optimization = SpecialOptimizeDesc.ConvFilterUnity
  274. min_cc = 75
  275. max_cc = 1024
  276. cuda_major = 10
  277. cuda_minor = 2
  278. for math_inst in math_instructions:
  279. for layout in layouts:
  280. for dst_layout in dst_layouts:
  281. dst_type = math_inst.element_b
  282. tile_descriptions = [
  283. TileDescription([128, 256, 128], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  284. TileDescription([128, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  285. TileDescription([128, 64, 128], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  286. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  287. ]
  288. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  289. dst_layout, dst_type, min_cc, 128, 128, 64, use_special_optimization,
  290. ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  291. layouts_nhwc = [
  292. (LayoutType.TensorNHWC, LayoutType.TensorNC8HW8, 32),
  293. (LayoutType.TensorNHWC, LayoutType.TensorNC16HW16, 64),
  294. (LayoutType.TensorNHWC, LayoutType.TensorNC32HW32, 128),
  295. ]
  296. dst_layouts_nhwc = [
  297. LayoutType.TensorNHWC,
  298. ]
  299. for math_inst in math_instructions:
  300. for layout in layouts_nhwc:
  301. for dst_layout in dst_layouts_nhwc:
  302. dst_type = math_inst.element_b
  303. tile_descriptions = [
  304. TileDescription([128, 16, 64], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  305. TileDescription([128, 32, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  306. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  307. ]
  308. for tile in tile_descriptions:
  309. dst_align = 16 if tile.threadblock_shape[1] == 16 else 32
  310. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  311. dst_type, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  312. ImplicitGemmMode.GemmTN, False, cuda_major, cuda_minor)
  313. if tile.threadblock_shape[1] == 32 or tile.threadblock_shape[1] == 64:
  314. dst_align = 32 if tile.threadblock_shape[1] == 32 else 64
  315. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  316. dst_type, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  317. ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  318. # INT4x4x8
  319. for math_inst in math_instructions:
  320. for layout in layouts_nhwc:
  321. for dst_layout in dst_layouts_nhwc:
  322. tile_descriptions = [
  323. TileDescription([128, 16, 64], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  324. TileDescription([128, 32, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  325. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  326. ]
  327. for tile in tile_descriptions:
  328. dst_align = 32 if tile.threadblock_shape[1] == 16 else 64
  329. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  330. DataType.s8, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  331. ImplicitGemmMode.GemmTN, False, cuda_major, cuda_minor)
  332. if tile.threadblock_shape[1] == 32 or tile.threadblock_shape[1] == 64:
  333. dst_align = 64 if tile.threadblock_shape[1] == 32 else 128
  334. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1], dst_layout,
  335. DataType.s8, min_cc, layout[2], layout[2], dst_align, use_special_optimization,
  336. ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  337. return operations
  338. def GenerateDeconv_Simt(args):
  339. operations = []
  340. layouts = [
  341. (LayoutType.TensorNC4HW4, LayoutType.TensorK4RSC4),
  342. ]
  343. math_instructions = [
  344. MathInstruction( \
  345. [1, 1, 4], \
  346. DataType.s8, DataType.s8, DataType.s32, \
  347. OpcodeClass.Simt, \
  348. MathOperation.multiply_add),
  349. ]
  350. dst_layouts = [
  351. LayoutType.TensorNC4HW4,
  352. ]
  353. dst_types = [
  354. DataType.s8,
  355. ]
  356. use_special_optimization = SpecialOptimizeDesc.DeconvDoubleUpsampling
  357. min_cc = 61
  358. max_cc = 1024
  359. for math_inst in math_instructions:
  360. for layout in layouts:
  361. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  362. tile_descriptions = [
  363. TileDescription([32, 128, 32], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  364. TileDescription([16, 128, 16], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  365. TileDescription([16, 128, 16], 1, [1, 1, 1], math_inst, min_cc, max_cc),
  366. TileDescription([16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  367. ]
  368. operations += GenerateConv2d(ConvKind.Dgrad, tile_descriptions, layout[0], layout[1],
  369. dst_layout, dst_type, min_cc, 32, 32, 32,
  370. use_special_optimization)
  371. return operations
  372. ################################################################################
  373. # parameters
  374. # Edge - for tiles, the edges represent the length of one side
  375. # Ratio - the maximum ratio between 2 edges, limits the skinnyness of tiles
  376. # MaxEdge - maximum length of each edge
  377. # Min/Max - minimum/maximum of the product of edge lengths
  378. ################################################################################
  379. warpsPerThreadblockEdge = [1, 2, 4, 8, 16]
  380. warpsPerThreadblockRatio = 2
  381. warpsPerThreadblockMax = 16
  382. # NOTE 1x32 and 2x16 warp tile shapes fail validation for ~10% of cases
  383. warpShapeEdges = [8, 16, 32, 64, 128, 256]
  384. warpShapeRatio = 4
  385. warpShapeMax = 64*64
  386. warpShapeMin = 8*8
  387. threadblockEdgeMax = 256
  388. # char, type bits/elem, max tile, L0 threadblock tiles
  389. precisions = {
  390. "c" : [ "cutlass::complex<float>", 64, 64*128, [ [ 64, 128], [ 64, 32] ] ],
  391. "d" : [ "double", 64, 64*64, [ [ 64, 64], [ 32, 32] ] ],
  392. "h" : [ "cutlass::half_t", 16, 128*256, [ [256, 128], [ 64, 128], [ 64, 32] ] ],
  393. "i" : [ "int", 32, 128*128, [ [128, 64], [ 16, 32] ] ],
  394. "s" : [ "float", 32, 128*128, [ [128, 256], [128, 128], [ 64, 64] ] ],
  395. "z" : [ "cutlass::complex<double>", 128, 64*64, [ [ 32, 64], [ 16, 32] ] ],
  396. }
  397. # L1 will have a single kernel for every unique shape
  398. # L2 will have everything else
  399. def GenerateGemm_Simt(args):
  400. ################################################################################
  401. # warps per threadblock
  402. ################################################################################
  403. warpsPerThreadblocks = []
  404. for warpsPerThreadblock0 in warpsPerThreadblockEdge:
  405. for warpsPerThreadblock1 in warpsPerThreadblockEdge:
  406. if warpsPerThreadblock0 / warpsPerThreadblock1 <= warpsPerThreadblockRatio \
  407. and warpsPerThreadblock1 / warpsPerThreadblock0 <= warpsPerThreadblockRatio \
  408. and warpsPerThreadblock0 * warpsPerThreadblock1 <= warpsPerThreadblockMax:
  409. warpsPerThreadblocks.append([warpsPerThreadblock0,
  410. warpsPerThreadblock1])
  411. ################################################################################
  412. # warp shapes
  413. ################################################################################
  414. warpNumThreads = 32
  415. warpShapes = []
  416. for warp0 in warpShapeEdges:
  417. for warp1 in warpShapeEdges:
  418. if warp0 / warp1 <= warpShapeRatio \
  419. and warp1 / warp0 <= warpShapeRatio \
  420. and warp0 * warp1 <= warpShapeMax \
  421. and warp0*warp1 > warpShapeMin:
  422. warpShapes.append([warp0, warp1])
  423. # sgemm
  424. precisionType, precisionBits, threadblockMaxElements, threadblockTilesL0 = precisions["s"]
  425. layouts = [
  426. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  427. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  428. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  429. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  430. ]
  431. math_instructions = [
  432. MathInstruction( \
  433. [1, 1, 1], \
  434. DataType.f32, DataType.f32, DataType.f32, \
  435. OpcodeClass.Simt, \
  436. MathOperation.multiply_add),
  437. ]
  438. min_cc = 50
  439. max_cc = 1024
  440. operations = []
  441. for math_inst in math_instructions:
  442. for layout in layouts:
  443. data_type = [
  444. math_inst.element_a,
  445. math_inst.element_b,
  446. math_inst.element_accumulator,
  447. math_inst.element_accumulator,
  448. ]
  449. tile_descriptions = [
  450. TileDescription([64, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  451. TileDescription([256, 64, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  452. TileDescription([ 32, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  453. TileDescription([256, 32, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  454. TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  455. TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  456. TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  457. TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  458. TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  459. TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  460. TileDescription([ 32, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  461. TileDescription([ 64, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  462. TileDescription([ 32, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  463. TileDescription([ 8, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  464. TileDescription([ 16, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  465. TileDescription([ 16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  466. TileDescription([ 16, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  467. ]
  468. for warpsPerThreadblock in warpsPerThreadblocks:
  469. for warpShape in warpShapes:
  470. warpThreadsM = 0
  471. if warpShape[0] > warpShape[1]:
  472. warpThreadsM = 8
  473. else:
  474. warpThreadsM = 4
  475. warpThreadsN = warpNumThreads / warpThreadsM
  476. # skip shapes with conflicting rectangularity
  477. # they are unlikely to be fastest
  478. blockG = warpsPerThreadblock[0] > warpsPerThreadblock[1]
  479. blockL = warpsPerThreadblock[0] < warpsPerThreadblock[1]
  480. warpG = warpShape[0] > warpShape[1]
  481. warpL = warpShape[0] < warpShape[1]
  482. blockG2 = warpsPerThreadblock[0] > warpsPerThreadblock[1]*2
  483. blockL2 = warpsPerThreadblock[0]*2 < warpsPerThreadblock[1]
  484. warpG2 = warpShape[0] > warpShape[1]*2
  485. warpL2 = warpShape[0]*2 < warpShape[1]
  486. if blockG2 and warpL: continue
  487. if blockL2 and warpG: continue
  488. if warpG2 and blockL: continue
  489. if warpL2 and blockG: continue
  490. # check threadblock ratios and max
  491. threadblockTile = [warpShape[0]*warpsPerThreadblock[0],
  492. warpShape[1]*warpsPerThreadblock[1]]
  493. if threadblockTile[0] * threadblockTile[1] > threadblockMaxElements: continue
  494. if threadblockTile[0] > threadblockEdgeMax: continue
  495. if threadblockTile[1] > threadblockEdgeMax: continue
  496. totalThreads = warpNumThreads*warpsPerThreadblock[0]*warpsPerThreadblock[1]
  497. # calculate unroll
  498. # ensure that every iteration at least a full load of A,B are done
  499. unrollMin = 8
  500. unrollMin0 = totalThreads // threadblockTile[0]
  501. unrollMin1 = totalThreads // threadblockTile[1]
  502. unroll = max(unrollMin, unrollMin0, unrollMin1)
  503. threadTileM = warpShape[0] // warpThreadsM
  504. threadTileN = warpShape[1] // warpThreadsN
  505. if threadTileM < 2 or threadTileN < 2: continue
  506. if threadTileM*threadTileN*precisionBits > 8*8*32: continue
  507. # epilogue currently only supports N < WarpNumThreads
  508. if threadblockTile[1] < warpNumThreads: continue
  509. # limit smem
  510. smemBitsA = threadblockTile[0]*unroll*2*precisionBits
  511. smemBitsB = threadblockTile[1]*unroll*2*precisionBits
  512. smemKBytes = (smemBitsA+smemBitsB)/8/1024
  513. if (smemKBytes > 48): continue
  514. tile = TileDescription([threadblockTile[0], threadblockTile[1], unroll], \
  515. 2, \
  516. [threadblockTile[0]//warpShape[0], threadblockTile[1]//warpShape[1], 1], \
  517. math_inst, min_cc, max_cc)
  518. def filter(t: TileDescription) -> bool:
  519. nonlocal tile
  520. return t.threadblock_shape[0] == tile.threadblock_shape[0] and \
  521. t.threadblock_shape[1] == tile.threadblock_shape[1] and \
  522. t.threadblock_shape[2] == tile.threadblock_shape[2] and \
  523. t.warp_count[0] == tile.warp_count[0] and \
  524. t.warp_count[1] == tile.warp_count[1] and \
  525. t.warp_count[2] == tile.warp_count[2] and \
  526. t.stages == tile.stages
  527. if not any(t for t in tile_descriptions if filter(t)): continue
  528. operations += GeneratesGemm(tile, data_type, layout[0], layout[1], layout[2], min_cc)
  529. return operations
  530. #
  531. def GenerateGemv_Simt(args):
  532. threadBlockShape_N = [128, 64, 32]
  533. ldgBits_A = [128, 64, 32]
  534. ldgBits_B = [128, 64, 32]
  535. layouts = [
  536. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor),
  537. ]
  538. math_instructions = [
  539. MathInstruction( \
  540. [1, 1, 1], \
  541. DataType.f32, DataType.f32, DataType.f32, \
  542. OpcodeClass.Simt, \
  543. MathOperation.multiply_add),
  544. ]
  545. min_cc = 50
  546. operations = []
  547. for math_inst in math_instructions:
  548. for layout in layouts:
  549. data_type = [
  550. math_inst.element_a,
  551. math_inst.element_b,
  552. math_inst.element_accumulator,
  553. math_inst.element_accumulator,
  554. ]
  555. for threadblock_shape_n in threadBlockShape_N:
  556. for align_a in ldgBits_A:
  557. for align_b in ldgBits_B:
  558. ldg_elements_a = align_a // DataTypeSize[math_inst.element_a]
  559. ldg_elements_b = align_b // DataTypeSize[math_inst.element_b]
  560. threadblock_shape_k = (256 * ldg_elements_a) // (threadblock_shape_n // ldg_elements_b)
  561. threadblock_shape = [1, threadblock_shape_n, threadblock_shape_k]
  562. thread_shape = [1, ldg_elements_b, ldg_elements_a]
  563. operations.append(GeneratesGemv(math_inst, \
  564. threadblock_shape, \
  565. thread_shape, \
  566. data_type, \
  567. layout[0], \
  568. layout[1], \
  569. layout[2], \
  570. min_cc, \
  571. align_a, \
  572. align_b))
  573. return operations
  574. #
  575. def GeneratesGemm_TensorOp_1688(args):
  576. layouts = [
  577. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  578. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  579. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  580. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  581. ]
  582. math_instructions = [
  583. MathInstruction( \
  584. [16, 8, 8], \
  585. DataType.f16, DataType.f16, DataType.f32, \
  586. OpcodeClass.TensorOp, \
  587. MathOperation.multiply_add),
  588. MathInstruction( \
  589. [16, 8, 8], \
  590. DataType.f16, DataType.f16, DataType.f16, \
  591. OpcodeClass.TensorOp, \
  592. MathOperation.multiply_add),
  593. ]
  594. min_cc = 75
  595. max_cc = 1024
  596. alignment_constraints = [8, 4, 2,
  597. #1
  598. ]
  599. cuda_major = 10
  600. cuda_minor = 2
  601. operations = []
  602. for math_inst in math_instructions:
  603. for layout in layouts:
  604. for align in alignment_constraints:
  605. tile_descriptions = [
  606. TileDescription([256, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  607. TileDescription([128, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  608. TileDescription([128, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  609. ## comment some configuration to reduce compilation time and binary size
  610. # TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  611. # TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  612. # TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  613. ]
  614. data_type = [
  615. math_inst.element_a,
  616. math_inst.element_b,
  617. math_inst.element_a,
  618. math_inst.element_accumulator,
  619. ]
  620. for tile in tile_descriptions:
  621. operations += GeneratesGemm(tile, \
  622. data_type, \
  623. layout[0], \
  624. layout[1], \
  625. layout[2], \
  626. min_cc, \
  627. align * 16, \
  628. align * 16, \
  629. align * 16, \
  630. cuda_major, \
  631. cuda_minor)
  632. return operations
  633. #
  634. def GeneratesGemm_TensorOp_884(args):
  635. layouts = [
  636. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  637. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  638. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  639. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  640. ]
  641. math_instructions = [
  642. MathInstruction( \
  643. [8, 8, 4], \
  644. DataType.f16, DataType.f16, DataType.f32, \
  645. OpcodeClass.TensorOp, \
  646. MathOperation.multiply_add),
  647. MathInstruction( \
  648. [8, 8, 4], \
  649. DataType.f16, DataType.f16, DataType.f16, \
  650. OpcodeClass.TensorOp, \
  651. MathOperation.multiply_add),
  652. ]
  653. min_cc = 70
  654. max_cc = 75
  655. alignment_constraints = [8, 4, 2,
  656. # 1
  657. ]
  658. cuda_major = 10
  659. cuda_minor = 2
  660. operations = []
  661. for math_inst in math_instructions:
  662. for layout in layouts:
  663. for align in alignment_constraints:
  664. tile_descriptions = [
  665. TileDescription([256, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  666. TileDescription([128, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  667. TileDescription([128, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  668. ## comment some configuration to reduce compilation time and binary size
  669. # TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  670. # TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  671. # TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  672. ]
  673. data_type = [
  674. math_inst.element_a,
  675. math_inst.element_b,
  676. math_inst.element_a,
  677. math_inst.element_accumulator,
  678. ]
  679. for tile in tile_descriptions:
  680. operations += GeneratesGemm(tile, \
  681. data_type, \
  682. layout[0], \
  683. layout[1], \
  684. layout[2], \
  685. min_cc, \
  686. align * 16, \
  687. align * 16, \
  688. align * 16, \
  689. cuda_major, \
  690. cuda_minor)
  691. return operations
  692. #
  693. def GenerateConv2dOperations(args):
  694. if args.type == "simt":
  695. return GenerateConv2d_Simt(args)
  696. elif args.type == "tensorop8816":
  697. return GenerateConv2d_TensorOp_8816(args)
  698. else:
  699. assert args.type == "tensorop8832", "operation conv2d only support" \
  700. "simt, tensorop8816 and tensorop8832. (got:{})".format(args.type)
  701. return GenerateConv2d_TensorOp_8832(args)
  702. def GenerateDeconvOperations(args):
  703. assert args.type == "simt", "operation deconv only support" \
  704. "simt. (got:{})".format(args.type)
  705. return GenerateDeconv_Simt(args)
  706. def GenerateGemmOperations(args):
  707. if args.type == "tensorop884":
  708. return GeneratesGemm_TensorOp_884(args)
  709. elif args.type == "tensorop1688":
  710. return GeneratesGemm_TensorOp_1688(args)
  711. else:
  712. assert args.type == "simt", "operation gemm only support" \
  713. "simt. (got:{})".format(args.type)
  714. return GenerateGemm_Simt(args)
  715. def GenerateGemvOperations(args):
  716. assert args.type == "simt", "operation gemv only support" \
  717. "simt. (got:{})".format(args.type)
  718. return GenerateGemv_Simt(args)
  719. ###################################################################################################
  720. ###################################################################################################
  721. if __name__ == "__main__":
  722. parser = argparse.ArgumentParser(description="Generates device kernel registration code for CUTLASS Kernels")
  723. parser.add_argument("--operations", type=str, choices=['gemm', 'gemv', 'conv2d', 'deconv'],
  724. required=True, help="Specifies the operation to generate (gemm, gemv, conv2d, deconv)")
  725. parser.add_argument("output", type=str, help="output directory for CUTLASS kernel files")
  726. parser.add_argument("--type", type=str, choices=['simt', 'tensorop8816', 'tensorop8832', 'tensorop884', 'tensorop1688'],
  727. default='simt', help="kernel type of CUTLASS kernel generator")
  728. gemv_wrapper_path = "src/cuda/matrix_mul/cutlass_matrix_mul_wrapper_batched_gemv_strided.cuinl"
  729. short_path = (platform.system() == "Windows" or platform.system().find('NT') >= 0) and ('true'!= os.getenv("CUTLASS_WITH_LONG_PATH", default='False').lower())
  730. args = parser.parse_args()
  731. if args.operations == "gemm":
  732. operations = GenerateGemmOperations(args)
  733. elif args.operations == "gemv":
  734. operations = GenerateGemvOperations(args)
  735. elif args.operations == "conv2d":
  736. operations = GenerateConv2dOperations(args)
  737. elif args.operations == "deconv":
  738. operations = GenerateDeconvOperations(args)
  739. if args.operations == "conv2d" or args.operations == "deconv":
  740. for operation in operations:
  741. with EmitConvSingleKernelWrapper(args.output, operation, short_path) as emitter:
  742. emitter.emit()
  743. elif args.operations == "gemm":
  744. for operation in operations:
  745. with EmitGemmSingleKernelWrapper(args.output, operation, short_path) as emitter:
  746. emitter.emit()
  747. elif args.operations == "gemv":
  748. for operation in operations:
  749. with EmitGemvSingleKernelWrapper(args.output, operation, gemv_wrapper_path, short_path) as emitter:
  750. emitter.emit()
  751. if args.operations != "gemv":
  752. GenerateManifest(args, operations, args.output)
  753. #
  754. ###################################################################################################

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