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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. from library import *
  11. from manifest import *
  12. ###################################################################################################
  13. #
  14. def CudaToolkitVersionSatisfies(semantic_ver_string, major, minor, patch = 0):
  15. # by default, use the latest CUDA Toolkit version
  16. cuda_version = [11, 0, 132]
  17. # Update cuda_version based on parsed string
  18. if semantic_ver_string != '':
  19. for i, x in enumerate([int(x) for x in semantic_ver_string.split('.')]):
  20. if i < len(cuda_version):
  21. cuda_version[i] = x
  22. else:
  23. cuda_version.append(x)
  24. return cuda_version >= [major, minor, patch]
  25. ###################################################################################################
  26. ###################################################################################################
  27. #
  28. def CreateGemmOperator(manifest, layouts, tile_descriptions, data_type, \
  29. alignment_constraints, complex_transforms = None, epilogue_functor = EpilogueFunctor.LinearCombination, \
  30. swizzling_functor = SwizzlingFunctor.Identity8):
  31. if complex_transforms is None:
  32. complex_transforms = [(ComplexTransform.none, ComplexTransform.none),]
  33. element_a, element_b, element_c, element_epilogue = data_type
  34. operations = []
  35. # by default, only generate the largest tile and largest alignment
  36. if manifest.args.kernels == '':
  37. tile_descriptions = [tile_descriptions[0],]
  38. alignment_constraints = [alignment_constraints[0],]
  39. for layout in layouts:
  40. for tile_description in tile_descriptions:
  41. for alignment in alignment_constraints:
  42. for complex_transform in complex_transforms:
  43. alignment_c = min(8, alignment)
  44. A = TensorDescription(element_a, layout[0], alignment, complex_transform[0])
  45. B = TensorDescription(element_b, layout[1], alignment, complex_transform[1])
  46. C = TensorDescription(element_c, layout[2], alignment_c)
  47. new_operation = GemmOperation(GemmKind.Universal, tile_description.minimum_compute_capability, \
  48. tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor)
  49. manifest.append(new_operation)
  50. operations.append(new_operation)
  51. return operations
  52. ###########################################################################################################
  53. # ConvolutionOperator support variations
  54. # ____________________________________________________________________
  55. # ConvolutionalOperator | Analytic | Optimized
  56. # ____________________________________________________________________
  57. # | Fprop | (strided) | (strided)
  58. # | Dgrad | (strided, unity*) | (unity)
  59. # | Wgrad | (strided) | (strided)
  60. # ____________________________________________________________________
  61. #
  62. # Note : Operator marked (*) are supported but not generated to keep the instantiated kernel count low
  63. ###########################################################################################################
  64. # Convolution for 2D operations
  65. def CreateConv2dOperator(manifest, layout, tile_descriptions, data_type, alignment, \
  66. conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], epilogue_functor = EpilogueFunctor.LinearCombination):
  67. element_a, element_b, element_c, element_epilogue = data_type
  68. # one exceptional case
  69. alignment_c = min(8, alignment)
  70. # iterator algorithm (analytic and optimized)
  71. iterator_algorithms = [IteratorAlgorithm.Analytic, IteratorAlgorithm.Optimized]
  72. # by default, only generate the largest tile size
  73. if manifest.args.kernels == '':
  74. tile_descriptions = [tile_descriptions[0],]
  75. operations = []
  76. for tile in tile_descriptions:
  77. for conv_kind in conv_kinds:
  78. for iterator_algorithm in iterator_algorithms:
  79. A = TensorDescription(element_a, layout[0], alignment)
  80. B = TensorDescription(element_b, layout[1], alignment)
  81. C = TensorDescription(element_c, layout[2], alignment_c)
  82. # unity stride only for Optimized Dgrad
  83. if (iterator_algorithm == IteratorAlgorithm.Optimized) and (conv_kind == ConvKind.Dgrad):
  84. new_operation = Conv2dOperation(conv_kind, iterator_algorithm, tile.minimum_compute_capability, tile,\
  85. A, B, C, element_epilogue, StrideSupport.Unity, epilogue_functor)
  86. manifest.append(new_operation)
  87. operations.append(new_operation)
  88. # strided dgrad is not supported by Optimized Dgrad
  89. if (iterator_algorithm == IteratorAlgorithm.Optimized) and (conv_kind == ConvKind.Dgrad):
  90. continue
  91. # strided support for Fprop (Analytic/Optimized), Dgrad (Analytic), and Wgrad (Analytic)
  92. new_operation = Conv2dOperation(conv_kind, iterator_algorithm, tile.minimum_compute_capability, tile,\
  93. A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor)
  94. manifest.append(new_operation)
  95. operations.append(new_operation)
  96. return operations
  97. ###################################################################################################
  98. ###################################################################################################
  99. def GenerateConv2d_Simt(args):
  100. operations = []
  101. layouts = [
  102. (LayoutType.TensorNC4HW4, LayoutType.TensorC4RSK4),
  103. ]
  104. math_instructions = [
  105. MathInstruction( \
  106. [1, 1, 4], \
  107. DataType.s8, DataType.s8, DataType.s32, \
  108. OpcodeClass.Simt, \
  109. MathOperation.multiply_add),
  110. ]
  111. dst_layouts = [
  112. LayoutType.TensorNC4HW4,
  113. LayoutType.TensorNC32HW32,
  114. LayoutType.TensorNHWC,
  115. LayoutType.TensorNHWC,
  116. LayoutType.TensorNCHW
  117. ]
  118. dst_types = [
  119. DataType.s8,
  120. DataType.s8,
  121. DataType.u4,
  122. DataType.s4,
  123. DataType.f32,
  124. ]
  125. max_cc = 1024
  126. for math_inst in math_instructions:
  127. for layout in layouts:
  128. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  129. if dst_type == DataType.s4 or dst_type == DataType.u4:
  130. min_cc = 75
  131. skip_unity_kernel = True
  132. else:
  133. min_cc = 61
  134. skip_unity_kernel = False
  135. tile_descriptions = [
  136. TileDescription([128, 128, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  137. TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  138. TileDescription([ 64, 128, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc),
  139. TileDescription([ 64, 64, 32], 2, [1, 2, 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([ 32, 32, 32], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  145. TileDescription([ 16, 128, 16], 1, [1, 1, 1], math_inst, min_cc, max_cc),
  146. TileDescription([ 16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  147. ]
  148. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  149. dst_layout, dst_type, min_cc, 32, 32, 32,
  150. skip_unity_kernel)
  151. return operations
  152. def GenerateConv2d_TensorOp_8816(args):
  153. operations = []
  154. layouts = [
  155. (LayoutType.TensorNC32HW32, LayoutType.TensorC32RSK32),
  156. ]
  157. math_instructions = [
  158. MathInstruction( \
  159. [8, 8, 16], \
  160. DataType.s8, DataType.s8, DataType.s32, \
  161. OpcodeClass.TensorOp, \
  162. MathOperation.multiply_add_saturate),
  163. ]
  164. dst_layouts = [
  165. LayoutType.TensorNC32HW32,
  166. LayoutType.TensorNC4HW4,
  167. ]
  168. dst_types = [
  169. DataType.s8,
  170. DataType.s8,
  171. ]
  172. min_cc = 75
  173. max_cc = 1024
  174. for math_inst in math_instructions:
  175. for layout in layouts:
  176. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  177. if dst_layout == LayoutType.TensorNC32HW32:
  178. tile_descriptions = [
  179. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  180. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  181. TileDescription([128, 128, 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, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  184. TileDescription([ 64, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  185. TileDescription([ 32, 64, 64], 2, [1, 4, 1], math_inst, min_cc, max_cc),
  186. ]
  187. else:
  188. assert dst_layout == LayoutType.TensorNC4HW4
  189. tile_descriptions = [
  190. TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  191. TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  192. TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  193. TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  194. TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  195. TileDescription([ 64, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  196. TileDescription([ 32, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  197. ]
  198. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  199. dst_layout, dst_type, min_cc, 128, 128, 64,
  200. False)
  201. return operations
  202. def GenerateConv2d_TensorOp_8832(args):
  203. operations = []
  204. layouts = [
  205. (LayoutType.TensorNC64HW64, LayoutType.TensorC64RSK64),
  206. ]
  207. math_instructions = [
  208. MathInstruction( \
  209. [8, 8, 32], \
  210. DataType.s4, DataType.s4, DataType.s32, \
  211. OpcodeClass.TensorOp, \
  212. MathOperation.multiply_add_saturate), \
  213. MathInstruction( \
  214. [8, 8, 32], \
  215. DataType.s4, DataType.u4, DataType.s32, \
  216. OpcodeClass.TensorOp, \
  217. MathOperation.multiply_add_saturate)
  218. ]
  219. dst_layouts = [
  220. LayoutType.TensorNC64HW64,
  221. ]
  222. min_cc = 75
  223. max_cc = 1024
  224. for math_inst in math_instructions:
  225. for layout in layouts:
  226. for dst_layout in dst_layouts:
  227. dst_type = math_inst.element_b
  228. tile_descriptions = [
  229. TileDescription([256, 128, 128], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  230. TileDescription([128, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  231. ]
  232. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  233. dst_layout, dst_type, min_cc, 128, 128, 64,
  234. True)
  235. layouts_nhwc = [
  236. (LayoutType.TensorNHWC, LayoutType.TensorNC8HW8, 32),
  237. (LayoutType.TensorNHWC, LayoutType.TensorNC16HW16, 64),
  238. (LayoutType.TensorNHWC, LayoutType.TensorNC32HW32, 128),
  239. ]
  240. dst_layouts_nhwc = [
  241. LayoutType.TensorNHWC,
  242. ]
  243. for math_inst in math_instructions:
  244. for layout in layouts_nhwc:
  245. for dst_layout in dst_layouts_nhwc:
  246. dst_type = math_inst.element_b
  247. tile_descriptions = [
  248. TileDescription([128, 32, 64], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  249. TileDescription([128, 64, 64], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  250. ]
  251. operations += GenerateConv2d(ConvKind.Fprop, tile_descriptions, layout[0], layout[1],
  252. dst_layout, dst_type, min_cc, layout[2], layout[2], 32,
  253. False, ImplicitGemmMode.GemmTn)
  254. return operations
  255. def GenerateDeconv_Simt(args):
  256. operations = []
  257. layouts = [
  258. (LayoutType.TensorNC4HW4, LayoutType.TensorK4RSC4),
  259. ]
  260. math_instructions = [
  261. MathInstruction( \
  262. [1, 1, 4], \
  263. DataType.s8, DataType.s8, DataType.s32, \
  264. OpcodeClass.Simt, \
  265. MathOperation.multiply_add),
  266. ]
  267. dst_layouts = [
  268. LayoutType.TensorNC4HW4,
  269. ]
  270. dst_types = [
  271. DataType.s8,
  272. ]
  273. min_cc = 61
  274. max_cc = 1024
  275. for math_inst in math_instructions:
  276. for layout in layouts:
  277. for dst_type, dst_layout in zip(dst_types, dst_layouts):
  278. tile_descriptions = [
  279. TileDescription([64, 128, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc),
  280. TileDescription([32, 128, 32], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  281. TileDescription([16, 128, 16], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  282. TileDescription([16, 128, 16], 1, [1, 1, 1], math_inst, min_cc, max_cc),
  283. TileDescription([16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  284. ]
  285. operations += GenerateConv2d(ConvKind.Dgrad, tile_descriptions, layout[0], layout[1],
  286. dst_layout, dst_type, min_cc, 32, 32, 32,
  287. True)
  288. return operations
  289. ################################################################################
  290. # parameters
  291. # Edge - for tiles, the edges represent the length of one side
  292. # Ratio - the maximum ratio between 2 edges, limits the skinnyness of tiles
  293. # MaxEdge - maximum length of each edge
  294. # Min/Max - minimum/maximum of the product of edge lengths
  295. ################################################################################
  296. warpsPerThreadblockEdge = [1, 2, 4, 8, 16]
  297. warpsPerThreadblockRatio = 2
  298. warpsPerThreadblockMax = 16
  299. # NOTE 1x32 and 2x16 warp tile shapes fail validation for ~10% of cases
  300. warpShapeEdges = [8, 16, 32, 64, 128, 256]
  301. warpShapeRatio = 4
  302. warpShapeMax = 64*64
  303. warpShapeMin = 8*8
  304. threadblockEdgeMax = 256
  305. # char, type bits/elem, max tile, L0 threadblock tiles
  306. precisions = {
  307. "c" : [ "cutlass::complex<float>", 64, 64*128, [ [ 64, 128], [ 64, 32] ] ],
  308. "d" : [ "double", 64, 64*64, [ [ 64, 64], [ 32, 32] ] ],
  309. "h" : [ "cutlass::half_t", 16, 128*256, [ [256, 128], [ 64, 128], [ 64, 32] ] ],
  310. "i" : [ "int", 32, 128*128, [ [128, 64], [ 16, 32] ] ],
  311. "s" : [ "float", 32, 128*128, [ [128, 256], [128, 128], [ 64, 64] ] ],
  312. "z" : [ "cutlass::complex<double>", 128, 64*64, [ [ 32, 64], [ 16, 32] ] ],
  313. }
  314. # L1 will have a single kernel for every unique shape
  315. # L2 will have everything else
  316. def GenerateGemm_Simt(args):
  317. ################################################################################
  318. # warps per threadblock
  319. ################################################################################
  320. warpsPerThreadblocks = []
  321. for warpsPerThreadblock0 in warpsPerThreadblockEdge:
  322. for warpsPerThreadblock1 in warpsPerThreadblockEdge:
  323. if warpsPerThreadblock0 / warpsPerThreadblock1 <= warpsPerThreadblockRatio \
  324. and warpsPerThreadblock1 / warpsPerThreadblock0 <= warpsPerThreadblockRatio \
  325. and warpsPerThreadblock0 * warpsPerThreadblock1 <= warpsPerThreadblockMax:
  326. warpsPerThreadblocks.append([warpsPerThreadblock0,
  327. warpsPerThreadblock1])
  328. ################################################################################
  329. # warp shapes
  330. ################################################################################
  331. warpNumThreads = 32
  332. warpShapes = []
  333. for warp0 in warpShapeEdges:
  334. for warp1 in warpShapeEdges:
  335. if warp0 / warp1 <= warpShapeRatio \
  336. and warp1 / warp0 <= warpShapeRatio \
  337. and warp0 * warp1 <= warpShapeMax \
  338. and warp0*warp1 > warpShapeMin:
  339. warpShapes.append([warp0, warp1])
  340. # sgemm
  341. precisionType, precisionBits, threadblockMaxElements, threadblockTilesL0 = precisions["s"]
  342. layouts = [
  343. (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # nn
  344. (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), # nt
  345. (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), # tn
  346. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), # tt
  347. ]
  348. math_instructions = [
  349. MathInstruction( \
  350. [1, 1, 1], \
  351. DataType.f32, DataType.f32, DataType.f32, \
  352. OpcodeClass.Simt, \
  353. MathOperation.multiply_add),
  354. ]
  355. min_cc = 50
  356. max_cc = 1024
  357. operations = []
  358. for math_inst in math_instructions:
  359. for layout in layouts:
  360. data_type = [
  361. math_inst.element_a,
  362. math_inst.element_b,
  363. math_inst.element_accumulator,
  364. math_inst.element_accumulator,
  365. ]
  366. tile_descriptions = [
  367. TileDescription([64, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  368. TileDescription([256, 64, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  369. TileDescription([ 32, 256, 8], 2, [2, 4, 1], math_inst, min_cc, max_cc),
  370. TileDescription([256, 32, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  371. TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc),
  372. TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  373. TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc),
  374. TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  375. TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  376. TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc),
  377. TileDescription([ 32, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  378. TileDescription([ 64, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  379. TileDescription([ 32, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  380. TileDescription([ 8, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  381. TileDescription([ 16, 32, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  382. TileDescription([ 16, 64, 8], 2, [1, 1, 1], math_inst, min_cc, max_cc),
  383. TileDescription([ 16, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc),
  384. ]
  385. for warpsPerThreadblock in warpsPerThreadblocks:
  386. for warpShape in warpShapes:
  387. warpThreadsM = 0
  388. if warpShape[0] > warpShape[1]:
  389. warpThreadsM = 8
  390. else:
  391. warpThreadsM = 4
  392. warpThreadsN = warpNumThreads / warpThreadsM
  393. # skip shapes with conflicting rectangularity
  394. # they are unlikely to be fastest
  395. blockG = warpsPerThreadblock[0] > warpsPerThreadblock[1]
  396. blockL = warpsPerThreadblock[0] < warpsPerThreadblock[1]
  397. warpG = warpShape[0] > warpShape[1]
  398. warpL = warpShape[0] < warpShape[1]
  399. blockG2 = warpsPerThreadblock[0] > warpsPerThreadblock[1]*2
  400. blockL2 = warpsPerThreadblock[0]*2 < warpsPerThreadblock[1]
  401. warpG2 = warpShape[0] > warpShape[1]*2
  402. warpL2 = warpShape[0]*2 < warpShape[1]
  403. if blockG2 and warpL: continue
  404. if blockL2 and warpG: continue
  405. if warpG2 and blockL: continue
  406. if warpL2 and blockG: continue
  407. # check threadblock ratios and max
  408. threadblockTile = [warpShape[0]*warpsPerThreadblock[0],
  409. warpShape[1]*warpsPerThreadblock[1]]
  410. if threadblockTile[0] * threadblockTile[1] > threadblockMaxElements: continue
  411. if threadblockTile[0] > threadblockEdgeMax: continue
  412. if threadblockTile[1] > threadblockEdgeMax: continue
  413. totalThreads = warpNumThreads*warpsPerThreadblock[0]*warpsPerThreadblock[1]
  414. # calculate unroll
  415. # ensure that every iteration at least a full load of A,B are done
  416. unrollMin = 8
  417. unrollMin0 = totalThreads // threadblockTile[0]
  418. unrollMin1 = totalThreads // threadblockTile[1]
  419. unroll = max(unrollMin, unrollMin0, unrollMin1)
  420. threadTileM = warpShape[0] // warpThreadsM
  421. threadTileN = warpShape[1] // warpThreadsN
  422. if threadTileM < 2 or threadTileN < 2: continue
  423. if threadTileM*threadTileN*precisionBits > 8*8*32: continue
  424. # epilogue currently only supports N < WarpNumThreads
  425. if threadblockTile[1] < warpNumThreads: continue
  426. # limit smem
  427. smemBitsA = threadblockTile[0]*unroll*2*precisionBits
  428. smemBitsB = threadblockTile[1]*unroll*2*precisionBits
  429. smemKBytes = (smemBitsA+smemBitsB)/8/1024
  430. if (smemKBytes > 48): continue
  431. tile = TileDescription([threadblockTile[0], threadblockTile[1], unroll], \
  432. 2, \
  433. [threadblockTile[0]//warpShape[0], threadblockTile[1]//warpShape[1], 1], \
  434. math_inst, min_cc, max_cc)
  435. def filter(t: TileDescription) -> bool:
  436. nonlocal tile
  437. return t.threadblock_shape[0] == tile.threadblock_shape[0] and \
  438. t.threadblock_shape[1] == tile.threadblock_shape[1] and \
  439. t.threadblock_shape[2] == tile.threadblock_shape[2] and \
  440. t.warp_count[0] == tile.warp_count[0] and \
  441. t.warp_count[1] == tile.warp_count[1] and \
  442. t.warp_count[2] == tile.warp_count[2] and \
  443. t.stages == tile.stages
  444. if not any(t for t in tile_descriptions if filter(t)): continue
  445. operations += GeneratesGemm(tile, data_type, layout[0], layout[1], layout[2], min_cc)
  446. return operations
  447. #
  448. def GenerateGemv_Simt(args):
  449. threadBlockShape_N = [128, 64, 32]
  450. ldgBits_A = [128, 64, 32]
  451. ldgBits_B = [128, 64, 32]
  452. layouts = [
  453. (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor),
  454. ]
  455. math_instructions = [
  456. MathInstruction( \
  457. [1, 1, 1], \
  458. DataType.f32, DataType.f32, DataType.f32, \
  459. OpcodeClass.Simt, \
  460. MathOperation.multiply_add),
  461. ]
  462. min_cc = 50
  463. operations = []
  464. for math_inst in math_instructions:
  465. for layout in layouts:
  466. data_type = [
  467. math_inst.element_a,
  468. math_inst.element_b,
  469. math_inst.element_accumulator,
  470. math_inst.element_accumulator,
  471. ]
  472. for threadblock_shape_n in threadBlockShape_N:
  473. for align_a in ldgBits_A:
  474. for align_b in ldgBits_B:
  475. ldg_elements_a = align_a // DataTypeSize[math_inst.element_a]
  476. ldg_elements_b = align_b // DataTypeSize[math_inst.element_b]
  477. threadblock_shape_k = (256 * ldg_elements_a) // (threadblock_shape_n // ldg_elements_b)
  478. threadblock_shape = [1, threadblock_shape_n, threadblock_shape_k]
  479. thread_shape = [1, ldg_elements_b, ldg_elements_a]
  480. operations.append(GeneratesGemv(math_inst, \
  481. threadblock_shape, \
  482. thread_shape, \
  483. data_type, \
  484. layout[0], \
  485. layout[1], \
  486. layout[2], \
  487. min_cc, \
  488. align_a, \
  489. align_b))
  490. return operations
  491. #
  492. def GenerateConv2dOperations(args):
  493. if args.type == "simt":
  494. return GenerateConv2d_Simt(args)
  495. elif args.type == "tensorop8816":
  496. return GenerateConv2d_TensorOp_8816(args)
  497. else:
  498. assert args.type == "tensorop8832", "operation conv2d only support" \
  499. "simt, tensorop8816 and tensorop8832. (got:{})".format(args.type)
  500. return GenerateConv2d_TensorOp_8832(args)
  501. def GenerateDeconvOperations(args):
  502. assert args.type == "simt", "operation deconv only support" \
  503. "simt. (got:{})".format(args.type)
  504. return GenerateDeconv_Simt(args)
  505. def GenerateGemmOperations(args):
  506. assert args.type == "simt", "operation gemm only support" \
  507. "simt. (got:{})".format(args.type)
  508. return GenerateGemm_Simt(args)
  509. def GenerateGemvOperations(args):
  510. assert args.type == "simt", "operation gemv only support" \
  511. "simt. (got:{})".format(args.type)
  512. return GenerateGemv_Simt(args)
  513. ###################################################################################################
  514. ###################################################################################################
  515. if __name__ == "__main__":
  516. parser = argparse.ArgumentParser(description="Generates device kernel registration code for CUTLASS Kernels")
  517. parser.add_argument("--operations", type=str, choices=['gemm', 'gemv', 'conv2d', 'deconv'],
  518. required=True, help="Specifies the operation to generate (gemm, gemv, conv2d, deconv)")
  519. parser.add_argument("output", type=str, help="output directory for CUTLASS kernel files")
  520. parser.add_argument("--type", type=str, choices=['simt', 'tensorop8816', 'tensorop8832'],
  521. default='simt', help="kernel type of CUTLASS kernel generator")
  522. operation2wrapper_path = {
  523. "gemm": "src/cuda/matrix_mul/cutlass_matrix_mul_wrapper.cuinl", \
  524. "gemv": "src/cuda/matrix_mul/cutlass_matrix_mul_wrapper_batched_gemv_strided.cuinl", \
  525. "conv2d": "src/cuda/conv_bias/implicit_gemm_conv_bias_cutlass_wrapper.cuinl", \
  526. "deconv": "src/cuda/convolution/backward_data/implicit_gemm_deconv_cutlass_wrapper.cuinl", \
  527. }
  528. args = parser.parse_args()
  529. wrapper_path = operation2wrapper_path[args.operations]
  530. if args.operations == "gemm":
  531. operations = GenerateGemmOperations(args)
  532. elif args.operations == "gemv":
  533. operations = GenerateGemvOperations(args)
  534. elif args.operations == "conv2d":
  535. operations = GenerateConv2dOperations(args)
  536. elif args.operations == "deconv":
  537. operations = GenerateDeconvOperations(args)
  538. if args.operations == "conv2d" or args.operations == "deconv":
  539. for operation in operations:
  540. with EmitConvSingleKernelWrapper(args.output, operation, wrapper_path) as emitter:
  541. emitter.emit()
  542. elif args.operations == "gemm" or args.operations == "gemv":
  543. for operation in operations:
  544. with EmitGemmSingleKernelWrapper(args.output, operation, wrapper_path) as emitter:
  545. emitter.emit()
  546. #
  547. ###################################################################################################

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