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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. for math_inst in math_instructions:
  174. for layout in layouts:
  175. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  176. if dst_layout == LayoutType.TensorNC32HW32:
  177. tile_descriptions = [
  178. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  179. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  180. TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  181. TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  182. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  183. TileDescription([128, 64, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  184. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  185. TileDescription([ 64, 128, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  186. TileDescription([ 32, 128, 32], 1, [1, 2, 1], math_inst, min_cc, max_cc),
  187. ]
  188. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  189. dst_layout, dst_type, min_cc, 128, 128, 64,
  190. False, ImplicitGemmMode.GemmTN, True)
  191. else:
  192. assert dst_layout == LayoutType.TensorNC4HW4
  193. tile_descriptions = [
  194. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  195. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  196. TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  197. TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  198. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  199. TileDescription([128, 64, 32], 1, [2, 2, 1], math_inst, min_cc, max_cc),
  200. TileDescription([128, 32, 32], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  201. TileDescription([ 64, 128, 32], 1, [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,
  206. False)
  207. return operations
  208. def GenerateConv2d_TensorOp_8832(args):
  209. operations = []
  210. layouts = [
  211. (LayoutType.TensorNC64HW64, LayoutType.TensorC64RSK64),
  212. ]
  213. math_instructions = [
  214. MathInstruction( \
  215. [8, 8, 32], \
  216. DataType.s4, DataType.s4, DataType.s32, \
  217. OpcodeClass.TensorOp, \
  218. MathOperation.multiply_add_saturate), \
  219. MathInstruction( \
  220. [8, 8, 32], \
  221. DataType.s4, DataType.u4, DataType.s32, \
  222. OpcodeClass.TensorOp, \
  223. MathOperation.multiply_add_saturate)
  224. ]
  225. dst_layouts = [
  226. LayoutType.TensorNC64HW64,
  227. ]
  228. min_cc = 75
  229. max_cc = 1024
  230. for math_inst in math_instructions:
  231. for layout in layouts:
  232. for dst_layout in dst_layouts:
  233. dst_type = math_inst.element_b
  234. tile_descriptions = [
  235. TileDescription([128, 256, 128], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  236. TileDescription([128, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  237. TileDescription([128, 64, 128], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  238. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  239. ]
  240. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  241. dst_layout, dst_type, min_cc, 128, 128, 64,
  242. False, ImplicitGemmMode.GemmTN, True)
  243. layouts_nhwc = [
  244. (LayoutType.TensorNHWC, LayoutType.TensorNC8HW8, 32),
  245. (LayoutType.TensorNHWC, LayoutType.TensorNC16HW16, 64),
  246. (LayoutType.TensorNHWC, LayoutType.TensorNC32HW32, 128),
  247. ]
  248. dst_layouts_nhwc = [
  249. LayoutType.TensorNHWC,
  250. ]
  251. for math_inst in math_instructions:
  252. for layout in layouts_nhwc:
  253. for dst_layout in dst_layouts_nhwc:
  254. dst_type = math_inst.element_b
  255. tile_descriptions = [
  256. TileDescription([128, 32, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  257. TileDescription([128, 64, 64], 1, [2, 1, 1], math_inst, min_cc, max_cc),
  258. ]
  259. for tile in tile_descriptions:
  260. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1],
  261. dst_layout, dst_type, min_cc, layout[2], layout[2], 32,
  262. False, ImplicitGemmMode.GemmTN, False)
  263. if tile.threadblock_shape[1] == 32 or tile.threadblock_shape[1] == 64:
  264. dst_align = 32 if tile.threadblock_shape[1] == 32 else 64
  265. operations += GenerateConv2d(ConvKind.Fprop, [tile], layout[0], layout[1],
  266. dst_layout, dst_type, min_cc, layout[2], layout[2], dst_align,
  267. False, ImplicitGemmMode.GemmTN, True)
  268. return operations
  269. def GenerateDeconv_Simt(args):
  270. operations = []
  271. layouts = [
  272. (LayoutType.TensorNC4HW4, LayoutType.TensorK4RSC4),
  273. ]
  274. math_instructions = [
  275. MathInstruction( \
  276. [1, 1, 4], \
  277. DataType.s8, DataType.s8, DataType.s32, \
  278. OpcodeClass.Simt, \
  279. MathOperation.multiply_add),
  280. ]
  281. dst_layouts = [
  282. LayoutType.TensorNC4HW4,
  283. ]
  284. dst_types = [
  285. DataType.s8,
  286. ]
  287. min_cc = 61
  288. max_cc = 1024
  289. for math_inst in math_instructions:
  290. for layout in layouts:
  291. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  292. tile_descriptions = [
  293. TileDescription([64, 128, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc),
  294. TileDescription([32, 128, 32], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  295. TileDescription([16, 128, 16], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  296. TileDescription([16, 128, 16], 1, [1, 1, 1], math_inst, min_cc, max_cc),
  297. TileDescription([16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  298. ]
  299. operations += GenerateConv2d(ConvKind.Dgrad, tile_descriptions, layout[0], layout[1],
  300. dst_layout, dst_type, min_cc, 32, 32, 32,
  301. True)
  302. return operations
  303. ################################################################################
  304. # parameters
  305. # Edge - for tiles, the edges represent the length of one side
  306. # Ratio - the maximum ratio between 2 edges, limits the skinnyness of tiles
  307. # MaxEdge - maximum length of each edge
  308. # Min/Max - minimum/maximum of the product of edge lengths
  309. ################################################################################
  310. warpsPerThreadblockEdge = [1, 2, 4, 8, 16]
  311. warpsPerThreadblockRatio = 2
  312. warpsPerThreadblockMax = 16
  313. # NOTE 1x32 and 2x16 warp tile shapes fail validation for ~10% of cases
  314. warpShapeEdges = [8, 16, 32, 64, 128, 256]
  315. warpShapeRatio = 4
  316. warpShapeMax = 64*64
  317. warpShapeMin = 8*8
  318. threadblockEdgeMax = 256
  319. # char, type bits/elem, max tile, L0 threadblock tiles
  320. precisions = {
  321. "c" : [ "cutlass::complex<float>", 64, 64*128, [ [ 64, 128], [ 64, 32] ] ],
  322. "d" : [ "double", 64, 64*64, [ [ 64, 64], [ 32, 32] ] ],
  323. "h" : [ "cutlass::half_t", 16, 128*256, [ [256, 128], [ 64, 128], [ 64, 32] ] ],
  324. "i" : [ "int", 32, 128*128, [ [128, 64], [ 16, 32] ] ],
  325. "s" : [ "float", 32, 128*128, [ [128, 256], [128, 128], [ 64, 64] ] ],
  326. "z" : [ "cutlass::complex<double>", 128, 64*64, [ [ 32, 64], [ 16, 32] ] ],
  327. }
  328. # L1 will have a single kernel for every unique shape
  329. # L2 will have everything else
  330. def GenerateGemm_Simt(args):
  331. ################################################################################
  332. # warps per threadblock
  333. ################################################################################
  334. warpsPerThreadblocks = []
  335. for warpsPerThreadblock0 in warpsPerThreadblockEdge:
  336. for warpsPerThreadblock1 in warpsPerThreadblockEdge:
  337. if warpsPerThreadblock0 / warpsPerThreadblock1 <= warpsPerThreadblockRatio \
  338. and warpsPerThreadblock1 / warpsPerThreadblock0 <= warpsPerThreadblockRatio \
  339. and warpsPerThreadblock0 * warpsPerThreadblock1 <= warpsPerThreadblockMax:
  340. warpsPerThreadblocks.append([warpsPerThreadblock0,
  341. warpsPerThreadblock1])
  342. ################################################################################
  343. # warp shapes
  344. ################################################################################
  345. warpNumThreads = 32
  346. warpShapes = []
  347. for warp0 in warpShapeEdges:
  348. for warp1 in warpShapeEdges:
  349. if warp0 / warp1 <= warpShapeRatio \
  350. and warp1 / warp0 <= warpShapeRatio \
  351. and warp0 * warp1 <= warpShapeMax \
  352. and warp0*warp1 > warpShapeMin:
  353. warpShapes.append([warp0, warp1])
  354. # sgemm
  355. precisionType, precisionBits, threadblockMaxElements, threadblockTilesL0 = precisions["s"]
  356. layouts = [
  357. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  358. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  359. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  360. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  361. ]
  362. math_instructions = [
  363. MathInstruction( \
  364. [1, 1, 1], \
  365. DataType.f32, DataType.f32, DataType.f32, \
  366. OpcodeClass.Simt, \
  367. MathOperation.multiply_add),
  368. ]
  369. min_cc = 50
  370. max_cc = 1024
  371. operations = []
  372. for math_inst in math_instructions:
  373. for layout in layouts:
  374. data_type = [
  375. math_inst.element_a,
  376. math_inst.element_b,
  377. math_inst.element_accumulator,
  378. math_inst.element_accumulator,
  379. ]
  380. tile_descriptions = [
  381. TileDescription([64, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  382. TileDescription([256, 64, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  383. TileDescription([ 32, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  384. TileDescription([256, 32, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  385. TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  386. TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  387. TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  388. TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  389. TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  390. TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  391. TileDescription([ 32, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  392. TileDescription([ 64, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  393. TileDescription([ 32, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  394. TileDescription([ 8, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  395. TileDescription([ 16, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  396. TileDescription([ 16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  397. TileDescription([ 16, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  398. ]
  399. for warpsPerThreadblock in warpsPerThreadblocks:
  400. for warpShape in warpShapes:
  401. warpThreadsM = 0
  402. if warpShape[0] > warpShape[1]:
  403. warpThreadsM = 8
  404. else:
  405. warpThreadsM = 4
  406. warpThreadsN = warpNumThreads / warpThreadsM
  407. # skip shapes with conflicting rectangularity
  408. # they are unlikely to be fastest
  409. blockG = warpsPerThreadblock[0] > warpsPerThreadblock[1]
  410. blockL = warpsPerThreadblock[0] < warpsPerThreadblock[1]
  411. warpG = warpShape[0] > warpShape[1]
  412. warpL = warpShape[0] < warpShape[1]
  413. blockG2 = warpsPerThreadblock[0] > warpsPerThreadblock[1]*2
  414. blockL2 = warpsPerThreadblock[0]*2 < warpsPerThreadblock[1]
  415. warpG2 = warpShape[0] > warpShape[1]*2
  416. warpL2 = warpShape[0]*2 < warpShape[1]
  417. if blockG2 and warpL: continue
  418. if blockL2 and warpG: continue
  419. if warpG2 and blockL: continue
  420. if warpL2 and blockG: continue
  421. # check threadblock ratios and max
  422. threadblockTile = [warpShape[0]*warpsPerThreadblock[0],
  423. warpShape[1]*warpsPerThreadblock[1]]
  424. if threadblockTile[0] * threadblockTile[1] > threadblockMaxElements: continue
  425. if threadblockTile[0] > threadblockEdgeMax: continue
  426. if threadblockTile[1] > threadblockEdgeMax: continue
  427. totalThreads = warpNumThreads*warpsPerThreadblock[0]*warpsPerThreadblock[1]
  428. # calculate unroll
  429. # ensure that every iteration at least a full load of A,B are done
  430. unrollMin = 8
  431. unrollMin0 = totalThreads // threadblockTile[0]
  432. unrollMin1 = totalThreads // threadblockTile[1]
  433. unroll = max(unrollMin, unrollMin0, unrollMin1)
  434. threadTileM = warpShape[0] // warpThreadsM
  435. threadTileN = warpShape[1] // warpThreadsN
  436. if threadTileM < 2 or threadTileN < 2: continue
  437. if threadTileM*threadTileN*precisionBits > 8*8*32: continue
  438. # epilogue currently only supports N < WarpNumThreads
  439. if threadblockTile[1] < warpNumThreads: continue
  440. # limit smem
  441. smemBitsA = threadblockTile[0]*unroll*2*precisionBits
  442. smemBitsB = threadblockTile[1]*unroll*2*precisionBits
  443. smemKBytes = (smemBitsA+smemBitsB)/8/1024
  444. if (smemKBytes > 48): continue
  445. tile = TileDescription([threadblockTile[0], threadblockTile[1], unroll], \
  446. 2, \
  447. [threadblockTile[0]//warpShape[0], threadblockTile[1]//warpShape[1], 1], \
  448. math_inst, min_cc, max_cc)
  449. def filter(t: TileDescription) -> bool:
  450. nonlocal tile
  451. return t.threadblock_shape[0] == tile.threadblock_shape[0] and \
  452. t.threadblock_shape[1] == tile.threadblock_shape[1] and \
  453. t.threadblock_shape[2] == tile.threadblock_shape[2] and \
  454. t.warp_count[0] == tile.warp_count[0] and \
  455. t.warp_count[1] == tile.warp_count[1] and \
  456. t.warp_count[2] == tile.warp_count[2] and \
  457. t.stages == tile.stages
  458. if not any(t for t in tile_descriptions if filter(t)): continue
  459. operations += GeneratesGemm(tile, data_type, layout[0], layout[1], layout[2], min_cc)
  460. return operations
  461. #
  462. def GenerateGemv_Simt(args):
  463. threadBlockShape_N = [128, 64, 32]
  464. ldgBits_A = [128, 64, 32]
  465. ldgBits_B = [128, 64, 32]
  466. layouts = [
  467. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor),
  468. ]
  469. math_instructions = [
  470. MathInstruction( \
  471. [1, 1, 1], \
  472. DataType.f32, DataType.f32, DataType.f32, \
  473. OpcodeClass.Simt, \
  474. MathOperation.multiply_add),
  475. ]
  476. min_cc = 50
  477. operations = []
  478. for math_inst in math_instructions:
  479. for layout in layouts:
  480. data_type = [
  481. math_inst.element_a,
  482. math_inst.element_b,
  483. math_inst.element_accumulator,
  484. math_inst.element_accumulator,
  485. ]
  486. for threadblock_shape_n in threadBlockShape_N:
  487. for align_a in ldgBits_A:
  488. for align_b in ldgBits_B:
  489. ldg_elements_a = align_a // DataTypeSize[math_inst.element_a]
  490. ldg_elements_b = align_b // DataTypeSize[math_inst.element_b]
  491. threadblock_shape_k = (256 * ldg_elements_a) // (threadblock_shape_n // ldg_elements_b)
  492. threadblock_shape = [1, threadblock_shape_n, threadblock_shape_k]
  493. thread_shape = [1, ldg_elements_b, ldg_elements_a]
  494. operations.append(GeneratesGemv(math_inst, \
  495. threadblock_shape, \
  496. thread_shape, \
  497. data_type, \
  498. layout[0], \
  499. layout[1], \
  500. layout[2], \
  501. min_cc, \
  502. align_a, \
  503. align_b))
  504. return operations
  505. #
  506. def GenerateConv2dOperations(args):
  507. if args.type == "simt":
  508. return GenerateConv2d_Simt(args)
  509. elif args.type == "tensorop8816":
  510. return GenerateConv2d_TensorOp_8816(args)
  511. else:
  512. assert args.type == "tensorop8832", "operation conv2d only support" \
  513. "simt, tensorop8816 and tensorop8832. (got:{})".format(args.type)
  514. return GenerateConv2d_TensorOp_8832(args)
  515. def GenerateDeconvOperations(args):
  516. assert args.type == "simt", "operation deconv only support" \
  517. "simt. (got:{})".format(args.type)
  518. return GenerateDeconv_Simt(args)
  519. def GenerateGemmOperations(args):
  520. assert args.type == "simt", "operation gemm only support" \
  521. "simt. (got:{})".format(args.type)
  522. return GenerateGemm_Simt(args)
  523. def GenerateGemvOperations(args):
  524. assert args.type == "simt", "operation gemv only support" \
  525. "simt. (got:{})".format(args.type)
  526. return GenerateGemv_Simt(args)
  527. ###################################################################################################
  528. ###################################################################################################
  529. if __name__ == "__main__":
  530. parser = argparse.ArgumentParser(description="Generates device kernel registration code for CUTLASS Kernels")
  531. parser.add_argument("--operations", type=str, choices=['gemm', 'gemv', 'conv2d', 'deconv'],
  532. required=True, help="Specifies the operation to generate (gemm, gemv, conv2d, deconv)")
  533. parser.add_argument("output", type=str, help="output directory for CUTLASS kernel files")
  534. parser.add_argument("--type", type=str, choices=['simt', 'tensorop8816', 'tensorop8832'],
  535. default='simt', help="kernel type of CUTLASS kernel generator")
  536. gemv_wrapper_path = "src/cuda/matrix_mul/cutlass_matrix_mul_wrapper_batched_gemv_strided.cuinl"
  537. short_path = (platform.system() == "Windows" or platform.system().find('NT') >= 0) and ('true'!= os.getenv("CUTLASS_WITH_LONG_PATH", default='False').lower())
  538. args = parser.parse_args()
  539. if args.operations == "gemm":
  540. operations = GenerateGemmOperations(args)
  541. elif args.operations == "gemv":
  542. operations = GenerateGemvOperations(args)
  543. elif args.operations == "conv2d":
  544. operations = GenerateConv2dOperations(args)
  545. elif args.operations == "deconv":
  546. operations = GenerateDeconvOperations(args)
  547. if args.operations == "conv2d" or args.operations == "deconv":
  548. for operation in operations:
  549. with EmitConvSingleKernelWrapper(args.output, operation, short_path) as emitter:
  550. emitter.emit()
  551. elif args.operations == "gemm":
  552. for operation in operations:
  553. with EmitGemmSingleKernelWrapper(args.output, operation, short_path) as emitter:
  554. emitter.emit()
  555. elif args.operations == "gemv":
  556. for operation in operations:
  557. with EmitGemvSingleKernelWrapper(args.output, operation, gemv_wrapper_path, short_path) as emitter:
  558. emitter.emit()
  559. if args.operations != "gemv":
  560. GenerateManifest(args, operations, args.output)
  561. #
  562. ###################################################################################################

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