| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright © Advanced Micro Devices, Inc., or its affiliates. |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
import argparse |
| 7 |
import origami |
| 8 |
import csv |
| 9 |
import os |
| 10 |
|
| 11 |
# Usage: |
| 12 |
# individual size: $ python3 origami_test.py -m 2048 -n 2048 -k 2048 --trans_a T --trans_b N --debug --print |
| 13 |
# read sizes from a file: $ python3 origami_test.py -m 1 -n 1 -k 1 -b 1 --trans_a T --trans_b N --type_a bf16 --type_b bf16 --type_d bf16 --sizes True --path ./sizes.csv --arch "gfx950" --debug --print |
| 14 |
|
| 15 |
# legend for datatypes (from hardware.hpp): |
| 16 |
# Float/SGEMM == "f32") |
| 17 |
# ComplexFloat/CGEMM== "c32") |
| 18 |
# ComplexDouble/ZGEMM == "c64" |
| 19 |
# Double/DGEMM == "f64" |
| 20 |
# Half/HHS/HSS== "f16" |
| 21 |
# Int32 == "i32" --> Is it i8II? |
| 22 |
# BFloat16/BBS/BSS == "bf16" |
| 23 |
# Int8/I8I8I == "i8" --> Is it i8II? |
| 24 |
# XFloat32/TF32 == "xf32" |
| 25 |
# Float8/ all F8 GEMMs == "f8" |
| 26 |
# BFloat8 == "bf8" |
| 27 |
# Float6 == "f6" |
| 28 |
# BFloat6 == "bf6" |
| 29 |
# Float4 == "f4" |
| 30 |
|
| 31 |
MatInst = {'gfx950': {}, 'gfx942': {}} |
| 32 |
MatInst['gfx950']["f32"] = [ |
| 33 |
(16,16,4,1), |
| 34 |
(32,32,2,1) |
| 35 |
] |
| 36 |
# MatInst['gfx950']["c32"] = [] |
| 37 |
# MatInst['gfx950']["c64"] = [] |
| 38 |
MatInst['gfx950']["f64"] = [ |
| 39 |
(16,16,4,1) |
| 40 |
] |
| 41 |
MatInst['gfx950']["f16"] = [ |
| 42 |
# (4,4,4,16), #gfx942 |
| 43 |
#[16,16,4,4] # never use 16x16x4x4 |
| 44 |
#[16,16,16,1] #gfx942 |
| 45 |
#[32,32,4,2] # never use 32x32x4x2 |
| 46 |
#[32,32,8,1] #gfx942 |
| 47 |
(16,16,32,1), #gfx950 |
| 48 |
(32,32,16,1) #gfx950 |
| 49 |
] |
| 50 |
# MatInst['gfx950']["i32"] =[] |
| 51 |
MatInst['gfx950']["bf16"] = MatInst['gfx950']["f16"] |
| 52 |
# MatInst['gfx950']["i8"] =[ |
| 53 |
# (32,32,16,1), |
| 54 |
# (16,16,32,1), |
| 55 |
# (4,4,4,16) |
| 56 |
# ] |
| 57 |
MatInst['gfx950']["xf32"] = [ |
| 58 |
# (4,4,4,16), #gfx942 |
| 59 |
#[16,16,4,4] # never use 16x16x4x4 |
| 60 |
#[16,16,16,1] #gfx942 |
| 61 |
#[32,32,4,2] # never use 32x32x4x2 |
| 62 |
#[32,32,8,1] #gfx942 |
| 63 |
(16,16,32,1), #gfx950 |
| 64 |
(32,32,16,1) #gfx950 |
| 65 |
] |
| 66 |
MatInst['gfx950']["f8"] = [ |
| 67 |
(4,4,4,16), #gfx950, gfx942 |
| 68 |
(16,16,128,1), #gfx950 |
| 69 |
(32,32,64,1) #gfx950 |
| 70 |
] |
| 71 |
MatInst['gfx950']["bf8"] = MatInst['gfx950']["f8"] |
| 72 |
|
| 73 |
def parseArguments(): |
| 74 |
parser = argparse.ArgumentParser(description="""Get hypothetical Origami MTxDU selection for a size""") |
| 75 |
parser.add_argument("-m", type=int, default=8192) |
| 76 |
parser.add_argument("-n", type=int, default=8192) |
| 77 |
parser.add_argument("-b", type=int, default=1) |
| 78 |
parser.add_argument("-k", type=int, default=8192) |
| 79 |
parser.add_argument("--trans_a", type=bool, default=True) |
| 80 |
parser.add_argument("--trans_b", type=bool, default=False) |
| 81 |
parser.add_argument("--device", type=int, default=0) # to get hardware specs |
| 82 |
parser.add_argument("--type_a", type=str, default="f16") |
| 83 |
parser.add_argument("--type_b", type=str, default="f16") |
| 84 |
parser.add_argument("--type_d", type=str, default="f16") |
| 85 |
parser.add_argument("--scale_block_size", type=int, default=0) |
| 86 |
parser.add_argument("--wgm", type=int, default=6) |
| 87 |
parser.add_argument("--sizes", type=bool, default=False) # to load the sizes from a csv file. -m/-n/-b/-k will be ignored if True. |
| 88 |
parser.add_argument("--path", type=str, default="./sizes.csv") # path to the csv file. Fails if sizes is True, and path or file does not exist. |
| 89 |
parser.add_argument("--arch", type=str, default="gfx950") |
| 90 |
parser.add_argument("--print", action="store_true") |
| 91 |
|
| 92 |
return parser.parse_args() |
| 93 |
|
| 94 |
def createTileList(arch, gemmType): |
| 95 |
|
| 96 |
LIST_OF_WAVEs_TO_INCLUDE = [[4, 1], [2, 2], [1, 4], [1, 2], [2, 1], [1, 1]] |
| 97 |
MIN_MT0 = MIN_MT1 = 16 |
| 98 |
MAX_MT0 = MAX_MT1 = 512 |
| 99 |
|
| 100 |
# generate all MTs for each datatype: |
| 101 |
bm_max = 0 |
| 102 |
tile_list = set() |
| 103 |
for MI in MatInst[arch][gemmType]: |
| 104 |
for bm in range(bm_max + 1): |
| 105 |
MIBlockM = 2 ** bm |
| 106 |
|
| 107 |
for wave in LIST_OF_WAVEs_TO_INCLUDE: |
| 108 |
waveTileM = 0 |
| 109 |
waveTileN = 0 |
| 110 |
|
| 111 |
while True: |
| 112 |
waveTileM+=1 |
| 113 |
waveTileN=0 |
| 114 |
MatrixInstM = MI[0] * MIBlockM |
| 115 |
MT0 = MatrixInstM * waveTileM * wave[0] |
| 116 |
if MT0 < MIN_MT0: |
| 117 |
continue |
| 118 |
if MT0 > MAX_MT0: |
| 119 |
break |
| 120 |
|
| 121 |
while True: |
| 122 |
waveTileN+=1 |
| 123 |
MatrixInstN = MI[1] / MIBlockM * MI[3] |
| 124 |
MT1 = int(MatrixInstN * waveTileN * wave[1]) |
| 125 |
|
| 126 |
if MT1 < MIN_MT1: |
| 127 |
continue |
| 128 |
if MT1 > MAX_MT1: |
| 129 |
break |
| 130 |
|
| 131 |
# LDS size check for LSU |
| 132 |
LSU = max(1, 4//wave[0]//wave[1]) |
| 133 |
if LSU > 1 and MT0*MT1*4*LSU > 256*256: |
| 134 |
continue |
| 135 |
|
| 136 |
if MT0*MT1 > 256*256: |
| 137 |
continue |
| 138 |
for DU in [16, 32, 64, 128, 256, 512, 1024]: |
| 139 |
tile_list.add((MT0, MT1, DU, MI[0], MI[1], MI[2], 1, 6, 0, 0)) |
| 140 |
|
| 141 |
return [tile for tile in tile_list] |
| 142 |
|
| 143 |
def main(): |
| 144 |
args = parseArguments() |
| 145 |
|
| 146 |
hardware = origami.get_hardware_for_device(args.device) |
| 147 |
|
| 148 |
tile_list = createTileList(args.arch, args.type_a) |
| 149 |
|
| 150 |
print(" Number of unique MTxDU: ", len(tile_list)) |
| 151 |
|
| 152 |
if args.sizes: # sizes from a file |
| 153 |
try: |
| 154 |
with open(args.path, 'r') as csvfile: |
| 155 |
csv_reader = csv.reader(csvfile) |
| 156 |
print(f"M,N,Batch,K,MT0,MT1,DU,MI0,MI1,MI2,MI3,latency") |
| 157 |
for row in csv_reader: |
| 158 |
M = int(row[0]) |
| 159 |
N = int(row[1]) |
| 160 |
B = int(row[2]) |
| 161 |
K = int(row[3]) |
| 162 |
ret = origami.select_best_macro_tile_size( |
| 163 |
M, |
| 164 |
N, |
| 165 |
K, |
| 166 |
B, |
| 167 |
args.trans_a, |
| 168 |
args.trans_b, |
| 169 |
hardware, |
| 170 |
tile_list, |
| 171 |
origami.datatype_to_bits(origami.string_to_datatype(args.type_a)), |
| 172 |
origami.datatype_to_bits(origami.string_to_datatype(args.type_b)), |
| 173 |
origami.datatype_to_bits(origami.string_to_datatype(args.type_d)), |
| 174 |
origami.string_to_datatype(args.type_a), |
| 175 |
args.scale_block_size, |
| 176 |
0.8, |
| 177 |
args.print, |
| 178 |
args.wgm, |
| 179 |
) |
| 180 |
#MxNxBxK, MT0xMT1xDU, MI0xMI1xMI2xMI3, latency/cycles |
| 181 |
print(f"{M},{N},{B},{K},{ret[0][1]},{ret[0][2]},{ret[0][3]},{ret[0][4]},{ret[0][5]},{ret[0][6]},{ret[0][7]},{ret[0][0]:0.3f}") |
| 182 |
except FileNotFoundError: |
| 183 |
raise FileNotFoundError(f"Error: The size file: '{args.path}' does not exist.") |
| 184 |
else: # one size from the command line. |
| 185 |
ret = origami.select_best_macro_tile_size( |
| 186 |
args.m, |
| 187 |
args.n, |
| 188 |
args.k, |
| 189 |
args.b, |
| 190 |
args.trans_a, |
| 191 |
args.trans_b, |
| 192 |
hardware, |
| 193 |
tile_list, |
| 194 |
origami.datatype_to_bits(origami.string_to_datatype(args.type_a)), |
| 195 |
origami.datatype_to_bits(origami.string_to_datatype(args.type_b)), |
| 196 |
origami.datatype_to_bits(origami.string_to_datatype(args.type_d)), |
| 197 |
origami.string_to_datatype(args.type_a), |
| 198 |
args.scale_block_size, |
| 199 |
0.8, |
| 200 |
args.print, |
| 201 |
args.wgm, |
| 202 |
) |
| 203 |
print(f"The best MTxDU for [{args.m}, {args.n}, {args.b}, {args.k}] is: {ret[0]}") # Match this with the top condition |
| 204 |
# add an option to list top 10~15 |
| 205 |
print(" full list of MTs: \n", ret) |
| 206 |
|
| 207 |
if args.print: |
| 208 |
hardware.print() |
| 209 |
hardware.print_debug_info() |
| 210 |
with open("MTxDU.log",'w') as file: |
| 211 |
for tile in tile_list: |
| 212 |
file.write(f'{tile}\n') |
| 213 |
|
| 214 |
return 0 |
| 215 |
|
| 216 |
if __name__ == "__main__": |
| 217 |
exit(main()) |