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 34 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. skip_unity_kernel = True
  133. else:
  134. min_cc = 61
  135. skip_unity_kernel = False
  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. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  148. dst_layout, dst_type, min_cc, 32, 32, 32,
  149. skip_unity_kernel)
  150. return operations
  151. def GenerateConv2d_TensorOp_8816(args):
  152. operations = []
  153. layouts = [
  154. (LayoutType.TensorNC32HW32, LayoutType.TensorC32RSK32),
  155. ]
  156. math_instructions = [
  157. MathInstruction( \
  158. [8, 8, 16], \
  159. DataType.s8, DataType.s8, DataType.s32, \
  160. OpcodeClass.TensorOp, \
  161. MathOperation.multiply_add_saturate),
  162. ]
  163. dst_layouts = [
  164. LayoutType.TensorNC32HW32,
  165. LayoutType.TensorNC4HW4,
  166. ]
  167. dst_types = [
  168. DataType.s8,
  169. DataType.s8,
  170. ]
  171. min_cc = 75
  172. max_cc = 1024
  173. cuda_major = 10
  174. cuda_minor = 2
  175. for math_inst in math_instructions:
  176. for layout in layouts:
  177. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  178. if dst_layout == LayoutType.TensorNC32HW32:
  179. tile_descriptions = [
  180. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  181. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  182. TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  183. TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  184. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  185. TileDescription([128, 64, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  186. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  187. TileDescription([ 64, 128, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  188. TileDescription([ 32, 128, 32], 1, [1, 2, 1], math_inst, min_cc, max_cc),
  189. ]
  190. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  191. dst_layout, dst_type, min_cc, 128, 128, 64,
  192. False, ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  193. else:
  194. assert dst_layout == LayoutType.TensorNC4HW4
  195. tile_descriptions = [
  196. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  197. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  198. TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  199. TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  200. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  201. TileDescription([128, 64, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  202. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  203. TileDescription([ 64, 128, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  204. TileDescription([ 32, 128, 32], 1, [1, 2, 1], math_inst, min_cc, max_cc),
  205. ]
  206. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  207. dst_layout, dst_type, min_cc, 128, 128, 64,
  208. False, ImplicitGemmMode.GemmNT, False, cuda_major, cuda_minor)
  209. return operations
  210. def GenerateConv2d_TensorOp_8832(args):
  211. operations = []
  212. layouts = [
  213. (LayoutType.TensorNC64HW64, LayoutType.TensorC64RSK64),
  214. ]
  215. math_instructions = [
  216. MathInstruction( \
  217. [8, 8, 32], \
  218. DataType.s4, DataType.s4, DataType.s32, \
  219. OpcodeClass.TensorOp, \
  220. MathOperation.multiply_add_saturate), \
  221. MathInstruction( \
  222. [8, 8, 32], \
  223. DataType.s4, DataType.u4, DataType.s32, \
  224. OpcodeClass.TensorOp, \
  225. MathOperation.multiply_add_saturate)
  226. ]
  227. dst_layouts = [
  228. LayoutType.TensorNC64HW64,
  229. ]
  230. min_cc = 75
  231. max_cc = 1024
  232. cuda_major = 10
  233. cuda_minor = 2
  234. for math_inst in math_instructions:
  235. for layout in layouts:
  236. for dst_layout in dst_layouts:
  237. dst_type = math_inst.element_b
  238. tile_descriptions = [
  239. TileDescription([128, 256, 128], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  240. TileDescription([128, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  241. TileDescription([128, 64, 128], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  242. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  243. ]
  244. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  245. dst_layout, dst_type, min_cc, 128, 128, 64,
  246. False, ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  247. layouts_nhwc = [
  248. (LayoutType.TensorNHWC, LayoutType.TensorNC8HW8, 32),
  249. (LayoutType.TensorNHWC, LayoutType.TensorNC16HW16, 64),
  250. (LayoutType.TensorNHWC, LayoutType.TensorNC32HW32, 128),
  251. ]
  252. dst_layouts_nhwc = [
  253. LayoutType.TensorNHWC,
  254. ]
  255. for math_inst in math_instructions:
  256. for layout in layouts_nhwc:
  257. for dst_layout in dst_layouts_nhwc:
  258. dst_type = math_inst.element_b
  259. tile_descriptions = [
  260. TileDescription([128, 32, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  261. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  262. ]
  263. for tile in tile_descriptions:
  264. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1],
  265. dst_layout, dst_type, min_cc, layout[2], layout[2], 32,
  266. False, ImplicitGemmMode.GemmTN, False, cuda_major, cuda_minor)
  267. if tile.threadblock_shape[1] == 32 or tile.threadblock_shape[1] == 64:
  268. dst_align = 32 if tile.threadblock_shape[1] == 32 else 64
  269. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1],
  270. dst_layout, dst_type, min_cc, layout[2], layout[2], dst_align,
  271. False, ImplicitGemmMode.GemmTN, True, cuda_major, cuda_minor)
  272. return operations
  273. def GenerateDeconv_Simt(args):
  274. operations = []
  275. layouts = [
  276. (LayoutType.TensorNC4HW4, LayoutType.TensorK4RSC4),
  277. ]
  278. math_instructions = [
  279. MathInstruction( \
  280. [1, 1, 4], \
  281. DataType.s8, DataType.s8, DataType.s32, \
  282. OpcodeClass.Simt, \
  283. MathOperation.multiply_add),
  284. ]
  285. dst_layouts = [
  286. LayoutType.TensorNC4HW4,
  287. ]
  288. dst_types = [
  289. DataType.s8,
  290. ]
  291. min_cc = 61
  292. max_cc = 1024
  293. for math_inst in math_instructions:
  294. for layout in layouts:
  295. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  296. tile_descriptions = [
  297. TileDescription([64, 128, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc),
  298. TileDescription([32, 128, 32], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  299. TileDescription([16, 128, 16], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  300. TileDescription([16, 128, 16], 1, [1, 1, 1], math_inst, min_cc, max_cc),
  301. TileDescription([16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  302. ]
  303. operations += GenerateConv2d(ConvKind.Dgrad, tile_descriptions, layout[0], layout[1],
  304. dst_layout, dst_type, min_cc, 32, 32, 32,
  305. True)
  306. return operations
  307. ################################################################################
  308. # parameters
  309. # Edge - for tiles, the edges represent the length of one side
  310. # Ratio - the maximum ratio between 2 edges, limits the skinnyness of tiles
  311. # MaxEdge - maximum length of each edge
  312. # Min/Max - minimum/maximum of the product of edge lengths
  313. ################################################################################
  314. warpsPerThreadblockEdge = [1, 2, 4, 8, 16]
  315. warpsPerThreadblockRatio = 2
  316. warpsPerThreadblockMax = 16
  317. # NOTE 1x32 and 2x16 warp tile shapes fail validation for ~10% of cases
  318. warpShapeEdges = [8, 16, 32, 64, 128, 256]
  319. warpShapeRatio = 4
  320. warpShapeMax = 64*64
  321. warpShapeMin = 8*8
  322. threadblockEdgeMax = 256
  323. # char, type bits/elem, max tile, L0 threadblock tiles
  324. precisions = {
  325. "c" : [ "cutlass::complex<float>", 64, 64*128, [ [ 64, 128], [ 64, 32] ] ],
  326. "d" : [ "double", 64, 64*64, [ [ 64, 64], [ 32, 32] ] ],
  327. "h" : [ "cutlass::half_t", 16, 128*256, [ [256, 128], [ 64, 128], [ 64, 32] ] ],
  328. "i" : [ "int", 32, 128*128, [ [128, 64], [ 16, 32] ] ],
  329. "s" : [ "float", 32, 128*128, [ [128, 256], [128, 128], [ 64, 64] ] ],
  330. "z" : [ "cutlass::complex<double>", 128, 64*64, [ [ 32, 64], [ 16, 32] ] ],
  331. }
  332. # L1 will have a single kernel for every unique shape
  333. # L2 will have everything else
  334. def GenerateGemm_Simt(args):
  335. ################################################################################
  336. # warps per threadblock
  337. ################################################################################
  338. warpsPerThreadblocks = []
  339. for warpsPerThreadblock0 in warpsPerThreadblockEdge:
  340. for warpsPerThreadblock1 in warpsPerThreadblockEdge:
  341. if warpsPerThreadblock0 / warpsPerThreadblock1 <= warpsPerThreadblockRatio \
  342. and warpsPerThreadblock1 / warpsPerThreadblock0 <= warpsPerThreadblockRatio \
  343. and warpsPerThreadblock0 * warpsPerThreadblock1 <= warpsPerThreadblockMax:
  344. warpsPerThreadblocks.append([warpsPerThreadblock0,
  345. warpsPerThreadblock1])
  346. ################################################################################
  347. # warp shapes
  348. ################################################################################
  349. warpNumThreads = 32
  350. warpShapes = []
  351. for warp0 in warpShapeEdges:
  352. for warp1 in warpShapeEdges:
  353. if warp0 / warp1 <= warpShapeRatio \
  354. and warp1 / warp0 <= warpShapeRatio \
  355. and warp0 * warp1 <= warpShapeMax \
  356. and warp0*warp1 > warpShapeMin:
  357. warpShapes.append([warp0, warp1])
  358. # sgemm
  359. precisionType, precisionBits, threadblockMaxElements, threadblockTilesL0 = precisions["s"]
  360. layouts = [
  361. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  362. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  363. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  364. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  365. ]
  366. math_instructions = [
  367. MathInstruction( \
  368. [1, 1, 1], \
  369. DataType.f32, DataType.f32, DataType.f32, \
  370. OpcodeClass.Simt, \
  371. MathOperation.multiply_add),
  372. ]
  373. min_cc = 50
  374. max_cc = 1024
  375. operations = []
  376. for math_inst in math_instructions:
  377. for layout in layouts:
  378. data_type = [
  379. math_inst.element_a,
  380. math_inst.element_b,
  381. math_inst.element_accumulator,
  382. math_inst.element_accumulator,
  383. ]
  384. tile_descriptions = [
  385. TileDescription([64, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  386. TileDescription([256, 64, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  387. TileDescription([ 32, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  388. TileDescription([256, 32, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  389. TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  390. TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  391. TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  392. TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  393. TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  394. TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  395. TileDescription([ 32, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  396. TileDescription([ 64, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  397. TileDescription([ 32, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  398. TileDescription([ 8, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  399. TileDescription([ 16, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  400. TileDescription([ 16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  401. TileDescription([ 16, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  402. ]
  403. for warpsPerThreadblock in warpsPerThreadblocks:
  404. for warpShape in warpShapes:
  405. warpThreadsM = 0
  406. if warpShape[0] > warpShape[1]:
  407. warpThreadsM = 8
  408. else:
  409. warpThreadsM = 4
  410. warpThreadsN = warpNumThreads / warpThreadsM
  411. # skip shapes with conflicting rectangularity
  412. # they are unlikely to be fastest
  413. blockG = warpsPerThreadblock[0] > warpsPerThreadblock[1]
  414. blockL = warpsPerThreadblock[0] < warpsPerThreadblock[1]
  415. warpG = warpShape[0] > warpShape[1]
  416. warpL = warpShape[0] < warpShape[1]
  417. blockG2 = warpsPerThreadblock[0] > warpsPerThreadblock[1]*2
  418. blockL2 = warpsPerThreadblock[0]*2 < warpsPerThreadblock[1]
  419. warpG2 = warpShape[0] > warpShape[1]*2
  420. warpL2 = warpShape[0]*2 < warpShape[1]
  421. if blockG2 and warpL: continue
  422. if blockL2 and warpG: continue
  423. if warpG2 and blockL: continue
  424. if warpL2 and blockG: continue
  425. # check threadblock ratios and max
  426. threadblockTile = [warpShape[0]*warpsPerThreadblock[0],
  427. warpShape[1]*warpsPerThreadblock[1]]
  428. if threadblockTile[0] * threadblockTile[1] > threadblockMaxElements: continue
  429. if threadblockTile[0] > threadblockEdgeMax: continue
  430. if threadblockTile[1] > threadblockEdgeMax: continue
  431. totalThreads = warpNumThreads*warpsPerThreadblock[0]*warpsPerThreadblock[1]
  432. # calculate unroll
  433. # ensure that every iteration at least a full load of A,B are done
  434. unrollMin = 8
  435. unrollMin0 = totalThreads // threadblockTile[0]
  436. unrollMin1 = totalThreads // threadblockTile[1]
  437. unroll = max(unrollMin, unrollMin0, unrollMin1)
  438. threadTileM = warpShape[0] // warpThreadsM
  439. threadTileN = warpShape[1] // warpThreadsN
  440. if threadTileM < 2 or threadTileN < 2: continue
  441. if threadTileM*threadTileN*precisionBits > 8*8*32: continue
  442. # epilogue currently only supports N < WarpNumThreads
  443. if threadblockTile[1] < warpNumThreads: continue
  444. # limit smem
  445. smemBitsA = threadblockTile[0]*unroll*2*precisionBits
  446. smemBitsB = threadblockTile[1]*unroll*2*precisionBits
  447. smemKBytes = (smemBitsA+smemBitsB)/8/1024
  448. if (smemKBytes > 48): continue
  449. tile = TileDescription([threadblockTile[0], threadblockTile[1], unroll], \
  450. 2, \
  451. [threadblockTile[0]//warpShape[0], threadblockTile[1]//warpShape[1], 1], \
  452. math_inst, min_cc, max_cc)
  453. def filter(t: TileDescription) -> bool:
  454. nonlocal tile
  455. return t.threadblock_shape[0] == tile.threadblock_shape[0] and \
  456. t.threadblock_shape[1] == tile.threadblock_shape[1] and \
  457. t.threadblock_shape[2] == tile.threadblock_shape[2] and \
  458. t.warp_count[0] == tile.warp_count[0] and \
  459. t.warp_count[1] == tile.warp_count[1] and \
  460. t.warp_count[2] == tile.warp_count[2] and \
  461. t.stages == tile.stages
  462. if not any(t for t in tile_descriptions if filter(t)): continue
  463. operations += GeneratesGemm(tile, data_type, layout[0], layout[1], layout[2], min_cc)
  464. return operations
  465. #
  466. def GenerateGemv_Simt(args):
  467. threadBlockShape_N = [128, 64, 32]
  468. ldgBits_A = [128, 64, 32]
  469. ldgBits_B = [128, 64, 32]
  470. layouts = [
  471. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor),
  472. ]
  473. math_instructions = [
  474. MathInstruction( \
  475. [1, 1, 1], \
  476. DataType.f32, DataType.f32, DataType.f32, \
  477. OpcodeClass.Simt, \
  478. MathOperation.multiply_add),
  479. ]
  480. min_cc = 50
  481. operations = []
  482. for math_inst in math_instructions:
  483. for layout in layouts:
  484. data_type = [
  485. math_inst.element_a,
  486. math_inst.element_b,
  487. math_inst.element_accumulator,
  488. math_inst.element_accumulator,
  489. ]
  490. for threadblock_shape_n in threadBlockShape_N:
  491. for align_a in ldgBits_A:
  492. for align_b in ldgBits_B:
  493. ldg_elements_a = align_a // DataTypeSize[math_inst.element_a]
  494. ldg_elements_b = align_b // DataTypeSize[math_inst.element_b]
  495. threadblock_shape_k = (256 * ldg_elements_a) // (threadblock_shape_n // ldg_elements_b)
  496. threadblock_shape = [1, threadblock_shape_n, threadblock_shape_k]
  497. thread_shape = [1, ldg_elements_b, ldg_elements_a]
  498. operations.append(GeneratesGemv(math_inst, \
  499. threadblock_shape, \
  500. thread_shape, \
  501. data_type, \
  502. layout[0], \
  503. layout[1], \
  504. layout[2], \
  505. min_cc, \
  506. align_a, \
  507. align_b))
  508. return operations
  509. #
  510. def GeneratesGemm_TensorOp_1688(args):
  511. layouts = [
  512. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  513. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  514. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  515. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  516. ]
  517. math_instructions = [
  518. MathInstruction( \
  519. [16, 8, 8], \
  520. DataType.f16, DataType.f16, DataType.f32, \
  521. OpcodeClass.TensorOp, \
  522. MathOperation.multiply_add),
  523. MathInstruction( \
  524. [16, 8, 8], \
  525. DataType.f16, DataType.f16, DataType.f16, \
  526. OpcodeClass.TensorOp, \
  527. MathOperation.multiply_add),
  528. ]
  529. min_cc = 75
  530. max_cc = 1024
  531. alignment_constraints = [8, 4, 2,
  532. #1
  533. ]
  534. cuda_major = 10
  535. cuda_minor = 2
  536. operations = []
  537. for math_inst in math_instructions:
  538. for layout in layouts:
  539. for align in alignment_constraints:
  540. tile_descriptions = [
  541. TileDescription([256, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  542. TileDescription([128, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  543. TileDescription([128, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  544. ## comment some configuration to reduce compilation time and binary size
  545. # TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  546. # TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  547. # TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  548. ]
  549. data_type = [
  550. math_inst.element_a,
  551. math_inst.element_b,
  552. math_inst.element_a,
  553. math_inst.element_accumulator,
  554. ]
  555. for tile in tile_descriptions:
  556. operations += GeneratesGemm(tile, \
  557. data_type, \
  558. layout[0], \
  559. layout[1], \
  560. layout[2], \
  561. min_cc, \
  562. align * 16, \
  563. align * 16, \
  564. align * 16, \
  565. cuda_major, \
  566. cuda_minor)
  567. return operations
  568. #
  569. def GeneratesGemm_TensorOp_884(args):
  570. layouts = [
  571. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  572. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  573. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  574. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  575. ]
  576. math_instructions = [
  577. MathInstruction( \
  578. [8, 8, 4], \
  579. DataType.f16, DataType.f16, DataType.f32, \
  580. OpcodeClass.TensorOp, \
  581. MathOperation.multiply_add),
  582. MathInstruction( \
  583. [8, 8, 4], \
  584. DataType.f16, DataType.f16, DataType.f16, \
  585. OpcodeClass.TensorOp, \
  586. MathOperation.multiply_add),
  587. ]
  588. min_cc = 70
  589. max_cc = 75
  590. alignment_constraints = [8, 4, 2,
  591. # 1
  592. ]
  593. cuda_major = 10
  594. cuda_minor = 2
  595. operations = []
  596. for math_inst in math_instructions:
  597. for layout in layouts:
  598. for align in alignment_constraints:
  599. tile_descriptions = [
  600. TileDescription([256, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  601. TileDescription([128, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  602. TileDescription([128, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  603. ## comment some configuration to reduce compilation time and binary size
  604. # TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  605. # TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  606. # TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  607. ]
  608. data_type = [
  609. math_inst.element_a,
  610. math_inst.element_b,
  611. math_inst.element_a,
  612. math_inst.element_accumulator,
  613. ]
  614. for tile in tile_descriptions:
  615. operations += GeneratesGemm(tile, \
  616. data_type, \
  617. layout[0], \
  618. layout[1], \
  619. layout[2], \
  620. min_cc, \
  621. align * 16, \
  622. align * 16, \
  623. align * 16, \
  624. cuda_major, \
  625. cuda_minor)
  626. return operations
  627. #
  628. def GenerateConv2dOperations(args):
  629. if args.type == "simt":
  630. return GenerateConv2d_Simt(args)
  631. elif args.type == "tensorop8816":
  632. return GenerateConv2d_TensorOp_8816(args)
  633. else:
  634. assert args.type == "tensorop8832", "operation conv2d only support" \
  635. "simt, tensorop8816 and tensorop8832. (got:{})".format(args.type)
  636. return GenerateConv2d_TensorOp_8832(args)
  637. def GenerateDeconvOperations(args):
  638. assert args.type == "simt", "operation deconv only support" \
  639. "simt. (got:{})".format(args.type)
  640. return GenerateDeconv_Simt(args)
  641. def GenerateGemmOperations(args):
  642. if args.type == "tensorop884":
  643. return GeneratesGemm_TensorOp_884(args)
  644. elif args.type == "tensorop1688":
  645. return GeneratesGemm_TensorOp_1688(args)
  646. else:
  647. assert args.type == "simt", "operation gemm only support" \
  648. "simt. (got:{})".format(args.type)
  649. return GenerateGemm_Simt(args)
  650. def GenerateGemvOperations(args):
  651. assert args.type == "simt", "operation gemv only support" \
  652. "simt. (got:{})".format(args.type)
  653. return GenerateGemv_Simt(args)
  654. ###################################################################################################
  655. ###################################################################################################
  656. if __name__ == "__main__":
  657. parser = argparse.ArgumentParser(description="Generates device kernel registration code for CUTLASS Kernels")
  658. parser.add_argument("--operations", type=str, choices=['gemm', 'gemv', 'conv2d', 'deconv'],
  659. required=True, help="Specifies the operation to generate (gemm, gemv, conv2d, deconv)")
  660. parser.add_argument("output", type=str, help="output directory for CUTLASS kernel files")
  661. parser.add_argument("--type", type=str, choices=['simt', 'tensorop8816', 'tensorop8832', 'tensorop884', 'tensorop1688'],
  662. default='simt', help="kernel type of CUTLASS kernel generator")
  663. gemv_wrapper_path = "src/cuda/matrix_mul/cutlass_matrix_mul_wrapper_batched_gemv_strided.cuinl"
  664. short_path = (platform.system() == "Windows" or platform.system().find('NT') >= 0) and ('true'!= os.getenv("CUTLASS_WITH_LONG_PATH", default='False').lower())
  665. args = parser.parse_args()
  666. if args.operations == "gemm":
  667. operations = GenerateGemmOperations(args)
  668. elif args.operations == "gemv":
  669. operations = GenerateGemvOperations(args)
  670. elif args.operations == "conv2d":
  671. operations = GenerateConv2dOperations(args)
  672. elif args.operations == "deconv":
  673. operations = GenerateDeconvOperations(args)
  674. if args.operations == "conv2d" or args.operations == "deconv":
  675. for operation in operations:
  676. with EmitConvSingleKernelWrapper(args.output, operation, short_path) as emitter:
  677. emitter.emit()
  678. elif args.operations == "gemm":
  679. for operation in operations:
  680. with EmitGemmSingleKernelWrapper(args.output, operation, short_path) as emitter:
  681. emitter.emit()
  682. elif args.operations == "gemv":
  683. for operation in operations:
  684. with EmitGemvSingleKernelWrapper(args.output, operation, gemv_wrapper_path, short_path) as emitter:
  685. emitter.emit()
  686. if args.operations != "gemv":
  687. GenerateManifest(args, operations, args.output)
  688. #
  689. ###################################################################################################

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