From 1558668af0a1ce3965ecbcb4143193b08c71355a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 9 Jan 2024 11:40:52 -0800 Subject: [PATCH] style fixing --- count_nnz_tiling.py | 8 +-- find_max_tilesize.py | 4 +- generate_spmv_sparsity_sweep.py | 69 ++++++++++--------- maximum_tiling.py | 49 ++++++++----- sam/onyx/synthetic/generate_fixed_nnz_mats.py | 4 +- sam/sim/src/tiling/tile.py | 10 +-- sam/util.py | 4 ++ scripts/datastructure_tns_old.py | 2 +- .../formatting/datastructure_suitesparse.py | 5 +- scripts/formatting/datastructure_tns.py | 34 ++++----- scripts/tiling/generate_gold_matmul_tiled.py | 1 + scripts/tiling/generate_gold_mattransmul.py | 11 ++- scripts/util/util.py | 2 +- setup_tiling_mat.py | 58 ++++++++-------- setup_tiling_tensors.py | 4 +- tile_pairing.py | 55 +++++++-------- 16 files changed, 170 insertions(+), 150 deletions(-) diff --git a/count_nnz_tiling.py b/count_nnz_tiling.py index c013fa4f..77dda214 100644 --- a/count_nnz_tiling.py +++ b/count_nnz_tiling.py @@ -1,5 +1,6 @@ import glob -import sys + + def count_nonzeros(matrix_values_file): with open(matrix_values_file, 'r') as values_file: matrix_values = [float(val) for val in values_file.readlines()] @@ -18,7 +19,7 @@ def count_nonzeros(matrix_values_file): sparsity_C = 0 # tilesize=int(sys.argv[1])**2 tot_num_nonzeros = 0 -for tile_num in range(0,num_tiles): +for tile_num in range(0, num_tiles): tot_num_nonzeros = 0 tensor_C_values_file = f'SPARSE_TESTS/MAT_TMP_DIR/tile{tile_num}/tensor_C_mode_vals' @@ -39,7 +40,6 @@ def count_nonzeros(matrix_values_file): print("error! too many nonzeros in INPUT matrices") raise Exception - if tot_num_nonzeros >= limit: print("tot_num_nonzeros: ", tot_num_nonzeros) print("error! too many nonzeros in matrices") @@ -49,4 +49,4 @@ def count_nonzeros(matrix_values_file): sparsity_C /= num_tiles print("sparsity_B: ", sparsity_B) -print("sparsity_C: ", sparsity_C) \ No newline at end of file +print("sparsity_C: ", sparsity_C) diff --git a/find_max_tilesize.py b/find_max_tilesize.py index bab4549a..e2c21812 100644 --- a/find_max_tilesize.py +++ b/find_max_tilesize.py @@ -2,6 +2,7 @@ import sys import glob + def write_to_line(file_path, line_number, new_content): with open(file_path, 'r') as file: lines = file.readlines() @@ -15,6 +16,7 @@ def write_to_line(file_path, line_number, new_content): with open(file_path, 'w') as file: file.writelines(lines) + def check_keyword_in_output(command, keyword): # Run the command and redirect the output to a file os.system(f'{command} > output.txt') @@ -74,5 +76,5 @@ def check_keyword_in_output(command, keyword): step = 10 else: break - + print("max tile size: ", tile_size) diff --git a/generate_spmv_sparsity_sweep.py b/generate_spmv_sparsity_sweep.py index 3d2f00e0..165e6894 100644 --- a/generate_spmv_sparsity_sweep.py +++ b/generate_spmv_sparsity_sweep.py @@ -1,4 +1,4 @@ -#script to generate 50 random 3D tensors (seeded, produces same 50 each time) +# script to generate 50 random 3D tensors (seeded, produces same 50 each time) import numpy as np import random import os @@ -7,15 +7,15 @@ # from scipy.io import mmread # Set the seed value -#previously used to be this: seed_value = 42 +# previously used to be this: seed_value = 42 seed_value = 100 random.seed(seed_value) np.random.seed(seed_value) -#generating matrix dimensions and storing results in an array, array size is 2, 1 matrix and 2 dimensions per matrix +# generating matrix dimensions and storing results in an array, array size is 2, 1 matrix and 2 dimensions per matrix -#conditions which need to be met for each set of 3 tensor dimensions: no dimension can't be 0, and can't have a tensor with more than 900 elements (meaning dimension1*dimension2*dimension3 <= 900) -#note try to make it so no dimension is 1 or 2 (gives slight issues later, esp 2nd and 3rd dimensions) +# conditions which need to be met for each set of 3 tensor dimensions: no dimension can't be 0, and can't have a tensor with more than 900 elements (meaning dimension1*dimension2*dimension3 <= 900) +# note try to make it so no dimension is 1 or 2 (gives slight issues later, esp 2nd and 3rd dimensions) dimensions = [0] * 2 dimensions_onematrix = [0] * 2 @@ -31,57 +31,59 @@ dimensions_onematrix[0] = 10 dimensions_onematrix[1] = 10 - dimensions[x*3] = dimensions_onematrix[0] - dimensions[(x*3)+1] = dimensions_onematrix[1] + dimensions[x * 3] = dimensions_onematrix[0] + dimensions[(x * 3) + 1] = dimensions_onematrix[1] dimensions_onematrix[0] = 0 dimensions_onematrix[1] = 0 - #print('\n') + # print('\n') -#Generating matrix values based on the dimensions now stored in the dimensions (2 elem) array -#i goes from 0 to __ (before 0 to 49) +# Generating matrix values based on the dimensions now stored in the dimensions (2 elem) array +# i goes from 0 to __ (before 0 to 49) matrix_num = 1 randomNumber = 0 numToInsert = 0 countnnz = 0 -#can add in as many sparsity numbers here (num elements in the sparsities array = num matrices being generated) +# can add in as many sparsity numbers here (num elements in the sparsities array = num matrices being generated) sparsities = [0.5] # NEED TO CHANGE suitesparse_path for this to work: frostt_path = os.environ['FROSTT_PATH'] ss_path = '' for i in range(1): - filename = os.path.join(ss_path, "rand_matrix"+str(matrix_num)+".mtx") + filename = os.path.join(ss_path, "rand_matrix" + str(matrix_num) + ".mtx") sparsity = sparsities[i] f = open(filename, "w") f.write('\n') lineToAddInFile = "" - arr = np.zeros([dimensions[i*3],dimensions[(i*3)+1]], dtype=int) + arr = np.zeros([dimensions[i * 3], dimensions[(i * 3) + 1]], dtype=int) for x in range(len(arr)): for y in range(len(arr[x])): - #TO CHANGE SPARSITY: generate random number from 1 to 9; if 1,2,3,7,8,9 don't add a num in, only add if 4,5,6 + # TO CHANGE SPARSITY: generate random number from 1 to 9; if 1,2,3,7,8,9 don't add a num in, only add if 4,5,6 # randomNumber = random.randint(1,9) randomNumber = random.random() - if(randomNumber>sparsity): - numToInsert = random.randint(1,100) + if (randomNumber > sparsity): + numToInsert = random.randint(1, 100) arr[x][y] = numToInsert numToInsert = 0 randomNumber = 0 - #print(arr[x][y][z]) - if(arr[x][y]!=0): - #tensor files are not 0 indexed - say want to insert a point at (0,0,0), then need to feed in (1,1,1) to the tensor file to insert at the (0,0,0) location - lineToAddInFile="" + str(x+1) + " " + str(y+1) + " " + str(arr[x][y]) + # print(arr[x][y][z]) + if (arr[x][y] != 0): + # tensor files are not 0 indexed - say want to insert a point at (0,0,0), + # then need to feed in (1,1,1) to the tensor file to insert at the (0,0,0) + # location + lineToAddInFile = "" + str(x + 1) + " " + str(y + 1) + " " + str(arr[x][y]) countnnz += 1 f.write(lineToAddInFile + '\n') # writing in first line in file: with open(filename, 'r') as f: content = f.read() - updated_content = ""+str(dimensions[i*3]) + " " + str(dimensions[i*3+1]) + " " + str(countnnz) + content + updated_content = "" + str(dimensions[i * 3]) + " " + str(dimensions[i * 3 + 1]) + " " + str(countnnz) + content with open(filename, 'w') as f: f.write(updated_content) with open(filename, 'r') as file: data = file.readlines() - + header = data.pop(0) num_rows, num_cols, num_nonzeros = map(int, header.strip().split()) matrix_data = [] @@ -93,15 +95,14 @@ col_indices.append(int(col) - 1) # Convert to 0-based indexing matrix_data.append(value) matrix = sps.coo_matrix((matrix_data, (row_indices, col_indices)), shape=(num_rows, num_cols)) - output_file = os.path.join(ss_path, "rand_matrix"+str(matrix_num)+".mat") + output_file = os.path.join(ss_path, "rand_matrix" + str(matrix_num) + ".mat") sio.savemat(output_file, {'matrix': matrix}, do_compression=True) # vec = sps.random(dimensions[i*3+1], 1, 0, data_rvs=np.ones) - vec = np.ones(dimensions[i*3+1]) - output_file1 = os.path.join(ss_path, "rand_vector"+str(matrix_num)+".mat") + vec = np.ones(dimensions[i * 3 + 1]) + output_file1 = os.path.join(ss_path, "rand_vector" + str(matrix_num) + ".mat") sio.savemat(output_file1, {'vector': vec}, do_compression=True) - # f.close() # a = mmread(filename) # a.toarray() @@ -112,14 +113,14 @@ matrix_num = matrix_num + 1 -#first step: one randomly generated 3D tensor given first set dimensions -#Note: generally if 2/3 elems in a tensor is 0, it can be considered sparse -#approach: 2/3 of the time add in a 0, 1/3 of the time add in an integer from 0 to 100 (use randint to generate num from 1 to 9 inclusive, and depending on where the num is, insert number or not) -#print('dimensions:') -#print(dimensions[0]) -#print(dimensions[1]) -#print(dimensions[2]) -#print('tensor vals') +# first step: one randomly generated 3D tensor given first set dimensions +# Note: generally if 2/3 elems in a tensor is 0, it can be considered sparse +# approach: 2/3 of the time add in a 0, 1/3 of the time add in an integer from 0 to 100 (use randint to generate num from 1 to 9 inclusive, and depending on where the num is, insert number or not) +# print('dimensions:') +# print(dimensions[0]) +# print(dimensions[1]) +# print(dimensions[2]) +# print('tensor vals') """ arr = np.zeros([dimensions[0],dimensions[1],dimensions[2]], dtype=int) diff --git a/maximum_tiling.py b/maximum_tiling.py index 2390ed21..578e42ac 100644 --- a/maximum_tiling.py +++ b/maximum_tiling.py @@ -5,15 +5,19 @@ import os import re + class EarlyReturn(): pass + def get_files_from_dir(path, operands): operand_files = {} for operand in operands: operand_files[operand] = glob.glob(os.path.join(path, f"*{operand}*.mtx")) return operand_files + + def get_tile_id(string): indices = [m.start() for m in re.finditer("tile", string)] if len(indices) >= 2: @@ -22,6 +26,7 @@ def get_tile_id(string): numbers = substring.split("_") return numbers + def pair_tiles(app_name): path = f"tiles/{app_name}/mtx" tile_pairing = {} @@ -32,7 +37,7 @@ def pair_tiles(app_name): operand_files = get_files_from_dir(path, operands) b_tensors = operand_files["B"] c_tensors = operand_files["C"] - + tile = 0 for b in b_tensors: for c in c_tensors: @@ -93,7 +98,8 @@ def pair_tiles(app_name): for d in d_tensors: d_loc = get_tile_id(d) - if(c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and b_loc[1] == d_loc[1] and b_loc[3] == d_loc[3] and b_loc[0] == c_loc[0] and b_loc[2] == c_loc[2]): + if (c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and b_loc[1] == d_loc[1] and + b_loc[3] == d_loc[3] and b_loc[0] == c_loc[0] and b_loc[2] == c_loc[2]): tile_pairing[tile] = [b, c, d] tile += 1 elif "mat_vecmul_iter" in app_name: @@ -125,20 +131,20 @@ def pair_tiles(app_name): continue for f in f_tensors: f_loc = get_tile_id(f) - if (d_loc[1] == e_loc[0] and d_loc[3] == e_loc[2] and c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2] and e_loc[1] == f_loc[0] and e_loc[3] == f_loc[1]): + if (d_loc[1] == e_loc[0] and d_loc[3] == e_loc[2] and c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2] and + b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2] and e_loc[1] == f_loc[0] and e_loc[3] == f_loc[1]): tile_pairing[tile] = [b, c, d, e, f] tile += 1 - - - return tile_pairing + def read_mtx(mtx_path): matrix = scipy.io.mmread(mtx_path) arr = np.array(matrix.todense()) return arr + def compute_outputs(tile_pairing, app_name, limit=900): for key, value in tile_pairing.items(): if "matmul" in app_name: @@ -147,7 +153,7 @@ def compute_outputs(tile_pairing, app_name, limit=900): C_mat = np.transpose(C_mat) out = np.matmul(B_mat, C_mat) if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit: - # if np.any(out): + # if np.any(out): print("tile = ", key) print("B_tile_ID = ", value[0]) print("C_tile_ID = ", value[1]) @@ -160,7 +166,7 @@ def compute_outputs(tile_pairing, app_name, limit=900): out = np.multiply(B_mat, C_mat) # if np.any(out): if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit: - # if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit: + # if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit: print("tile = ", key) print("B_tile_ID = ", value[0]) print("C_tile_ID = ", value[1]) @@ -174,8 +180,9 @@ def compute_outputs(tile_pairing, app_name, limit=900): out = np.add(np.add(B_mat, C_mat), D_mat) # if np.any(out): - if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit: - # if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit: + if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero( + C_mat) > limit or np.count_nonzero(D_mat) > limit: + # if np.count_nonzero(out) > limit or (np.count_nonzero(B_mat) + np.count_nonzero(C_mat)) > limit: print("tile = ", key) print("B_tile_ID = ", value[0]) print("C_tile_ID = ", value[1]) @@ -189,7 +196,8 @@ def compute_outputs(tile_pairing, app_name, limit=900): D_mat = read_mtx(value[2]) D_mat = np.transpose(D_mat) out = np.sum(np.multiply(np.matmul(C_mat, D_mat), B_mat)) - if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit: + if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero( + C_mat) > limit or np.count_nonzero(D_mat) > limit: print("tile = ", key) print("B_tile_ID = ", value[0]) print("C_tile_ID = ", value[1]) @@ -207,7 +215,9 @@ def compute_outputs(tile_pairing, app_name, limit=900): f_mat = np.transpose(f_mat) out = np.matmul(np.matmul(np.matmul(np.matmul(B_mat, C_mat), D_mat), E_mat), f_mat) if np.any(out): - # if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit or np.count_nonzero(E_mat) > limit or np.count_nonzero(f_mat) > limit: + # if np.count_nonzero(out) > limit or np.count_nonzero(B_mat) > limit or + # np.count_nonzero(C_mat) > limit or np.count_nonzero(D_mat) > limit or + # np.count_nonzero(E_mat) > limit or np.count_nonzero(f_mat) > limit: print("tile = ", key) print("B_tile_ID = ", value[0]) print("C_tile_ID = ", value[1]) @@ -219,7 +229,8 @@ def compute_outputs(tile_pairing, app_name, limit=900): breakpoint() return EarlyReturn() return None - + + def find_optimal_tilesize(app_name, datum, initial=30, step_size=10): tile_size = initial max_tile_size = initial @@ -236,15 +247,15 @@ def find_optimal_tilesize(app_name, datum, initial=30, step_size=10): if isinstance(exit_status, EarlyReturn): max_tile_size = tile_size - step_size return max_tile_size, prev_tile_pairing - + tile_size += step_size print("***********************") print("tile size = ", tile_size) print("***********************") prev_tile_pairing = tile_pairing - + return tile_size, prev_tile_pairing - + if __name__ == "__main__": max_list = {} @@ -256,10 +267,10 @@ def find_optimal_tilesize(app_name, datum, initial=30, step_size=10): # compute_outputs(tile_pairing, app_name) max_tile_size, tile_pairing = find_optimal_tilesize(app_name, datum, initial=40, step_size=10) - print("-"*20) + print("-" * 20) print(f"MAX TILESIZE for {app_name}, {datum}: {max_tile_size}") print(f"NUMBER OF TILES: {len(tile_pairing.keys())}") - print("-"*20) + print("-" * 20) max_list[datum] = [max_tile_size, len(tile_pairing.keys())] @@ -267,4 +278,4 @@ def find_optimal_tilesize(app_name, datum, initial=30, step_size=10): os.system(call_tiling) print(call_tiling) - # print(max_list) \ No newline at end of file + # print(max_list) diff --git a/sam/onyx/synthetic/generate_fixed_nnz_mats.py b/sam/onyx/synthetic/generate_fixed_nnz_mats.py index 6671fcc2..d5ec2282 100644 --- a/sam/onyx/synthetic/generate_fixed_nnz_mats.py +++ b/sam/onyx/synthetic/generate_fixed_nnz_mats.py @@ -3,6 +3,7 @@ import numpy as np import argparse + def generate_mat(nnz, dim): return scipy.sparse.random(dim, dim, nnz / (dim**2), data_rvs=np.ones) @@ -18,9 +19,8 @@ def write_mtx(path, t): parser = argparse.ArgumentParser(description="Create some random matrices of given nnz and dim") parser.add_argument('--nnz', type=int, nargs='+', help='nnz') parser.add_argument('--dim', type=int, nargs='+', help='dim') - parser.add_argument('--extensor', action='store_true', help='generate extensor dims and nnzs') + parser.add_argument('--extensor', action='store_true', help='generate extensor dims and nnzs') args = parser.parse_args() - if args.extensor: dims = list(range(1024, 15721, 1336)) diff --git a/sam/sim/src/tiling/tile.py b/sam/sim/src/tiling/tile.py index 47cf15a0..b605c2b3 100644 --- a/sam/sim/src/tiling/tile.py +++ b/sam/sim/src/tiling/tile.py @@ -27,7 +27,7 @@ "mat_elemadd": "X(i,j)=B(i,j)+C(i,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)", "mat_elemmul": "X(i,j)=B(i,j)*C(i,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)", "mat_mattransmul": "X(i,j)=B(j,i)*c(j)+d(i) -f=X:ss -f=B:ss -f=c:ss:0 -f=d:ss:0 -s=reorder(i,j)", - "mat_vecmul_ij" : "X(i,j)=B(i,j)*c(j) -f=X:ss -f=B:ss -f=c:ss:0 -s=reorder(i,j)", + "mat_vecmul_ij": "X(i,j)=B(i,j)*c(j) -f=X:ss -f=B:ss -f=c:ss:0 -s=reorder(i,j)", "mat_residual": "X(i,j)=b(i)-C(i,j)*d(j) -f=X:ss -f=C:ss -f=b:ss:0 -f=d:ss:0 -s=reorder(i,j)", "mat_sddmm": "X(i,j)=B(i,j)*C(i,k)*D(k,j) -f=X:ss -f=B:ss -f=C:dd -f=D:dd:1,0 -s=reorder(i,j,k)", "mat_elemadd3": "X(i,j)=B(i,j)+C(i,j)+D(i,j) -f=X:ss -f=B:ss -f=C:ss -f=D:ss", @@ -322,7 +322,7 @@ def get_other_tensors(app_str, tensor, other_nonempty=True): print("writing other vector...") tensorName = args.input_tensor variant = "mode1" - path = constructOtherVecKey(tensorName,variant) + path = constructOtherVecKey(tensorName, variant) tensor_c_from_path = FrosttTensor(path) tensor_c = tensor_c_from_path.load().todense() @@ -354,7 +354,7 @@ def get_other_tensors(app_str, tensor, other_nonempty=True): if other_nonempty: tensor_b[0] = 1 tensor_d[0] = 1 - + tensors.insert(0, tensor_b) tensors.append(tensor_d) @@ -362,7 +362,7 @@ def get_other_tensors(app_str, tensor, other_nonempty=True): print("Writing other tensors...") tensorName = args.input_tensor # c(j) use mode1 - + # variant = "mode1" # path = constructOtherVecKey(tensorName,variant) # tensor_c_from_path = FrosttTensor(path) @@ -370,7 +370,7 @@ def get_other_tensors(app_str, tensor, other_nonempty=True): # print("TENSOR SHAPE: ", tensor.shape) # print("TENSOR_C SHAPE: ", tensor_c.shape) - + rows, cols = tensor.shape tensor_c = scipy.sparse.random(cols, 1, data_rvs=np.ones).toarray().flatten() diff --git a/sam/util.py b/sam/util.py index ae70dee1..c143e175 100644 --- a/sam/util.py +++ b/sam/util.py @@ -37,16 +37,20 @@ def safeCastScipyTensorToInts(tensor): data[i] = round_sparse(tensor.data[i]) return scipy.sparse.coo_matrix(tensor.coords, data, tensor.shape) + def constructOtherVecKey(tensorName, variant, sparsity=0.001): path = os.getenv('TACO_TENSOR_PATH') return f"{path}/{tensorName}-vec_{variant}-{sparsity}.tns" + def constructOtherMatKey(tensorName, variant, sparsity=0.001): path = os.getenv('TACO_TENSOR_PATH') return f"{path}/../suitesparse/{tensorName}_{variant}.mtx" # ScipyTensorShifter shifts all elements in the last mode # of the input scipy/sparse tensor by one. + + class ScipyTensorShifter: def __init__(self): pass diff --git a/scripts/datastructure_tns_old.py b/scripts/datastructure_tns_old.py index 863f1d0b..54a91ae2 100644 --- a/scripts/datastructure_tns_old.py +++ b/scripts/datastructure_tns_old.py @@ -58,7 +58,7 @@ levels = args.format[:-3] if args.other: assert args.bench is not None - + print("here to get other file names\n") otherfileNames = [f for f in os.listdir(taco_format_dirname) if diff --git a/scripts/formatting/datastructure_suitesparse.py b/scripts/formatting/datastructure_suitesparse.py index d2e586c2..209f52dd 100644 --- a/scripts/formatting/datastructure_suitesparse.py +++ b/scripts/formatting/datastructure_suitesparse.py @@ -1,3 +1,5 @@ +from sam.util import SUITESPARSE_FORMATTED_PATH, ScipyTensorShifter +from util.util import FormatWriter, SuiteSparseTensor, InputCacheSuiteSparse import argparse import os import shutil @@ -10,8 +12,6 @@ # the mock-0.3.1 dir contains testcase.py, testutils.py & mock.py sys.path.append('/home/avb03/sam/scripts') -from util.util import FormatWriter, SuiteSparseTensor, InputCacheSuiteSparse -from sam.util import SUITESPARSE_FORMATTED_PATH, ScipyTensorShifter all_formats = ["coo", "cooT", "csr", "dcsr", "dcsc", "csc", "dense", "denseT"] formats = ["coo", "cooT", "csr", "dcsr", "dcsc", "csc", "dense"] @@ -36,7 +36,6 @@ def write_datastructure_tiles(args, tensor, out_path, tile_name): # formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss01", hw=args.hw) - def write_datastructure_bench(args, tensor, out_path, tiles=None): shifter = ScipyTensorShifter() diff --git a/scripts/formatting/datastructure_tns.py b/scripts/formatting/datastructure_tns.py index 9e5d743e..dd8b7c81 100644 --- a/scripts/formatting/datastructure_tns.py +++ b/scripts/formatting/datastructure_tns.py @@ -62,17 +62,17 @@ print("Please enter a tensor name") exit() -#breakpoint() +# breakpoint() if args.format is not None: assert args.format in formats levels = args.format[:-3] if os.path.exists('sam/FROST_FORMATTED/rand_tensor*'): shutil.rmtree('sam/FROST_FORMATTED/rand_tensor*') - + if args.bench != "tensor3_elemadd" and args.bench != "tensor3_innerprod": assert args.bench is not None - #$FROSTT_FORMATTED_TACO_PATH + # $FROSTT_FORMATTED_TACO_PATH taco_format_orig_filename = "/home/avb03/sam/FROST_FORMATTED_TACO" outdir_other_name = os.path.join(outdir_name, args.name, args.bench) # outdir_other_name = os.path.join(outdir_name, args.name, 'other', otherfile[:-4]) @@ -91,10 +91,10 @@ taco_format_orig_filename = "/home/avb03/sam/FROST_FORMATTED_TACO/" + args.name + "_" + levels + '.txt' parse_taco_format(taco_format_orig_filename, outdir_orig_name, 'B', args.format) - #Need this line? formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + # Need this line? formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") file_path_name = os.path.join(outdir_orig_name, "tensor_B_mode_shape") file1 = open(file_path_name, 'r') - shape = [0]*3 + shape = [0] * 3 lines = file1.readlines() count = 0 @@ -103,7 +103,7 @@ shape[count] = int(line) count += 1 # coo = inputCache.load(tensor, False) - + # formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") tensorname = 'c' vec = scipy.sparse.random(shape[2], 1, density=args.density, data_rvs=np.ones) @@ -114,8 +114,8 @@ # vec = scipy.sparse.random(shape[2], 1, data_rvs=np.ones) # vec = vec.toarray().flatten() # formatWriter.writeout_separate_vec(vec, out_path, tensorname) - #FormatWriter.writeout_separate_vec(vec, out_path, tensorname, tensorname) - #formatWriter.writeout_separate_sparse_only() + # FormatWriter.writeout_separate_vec(vec, out_path, tensorname, tensorname) + # formatWriter.writeout_separate_sparse_only() elif args.bench == "tensor3_ttm": outdir_orig_name = os.path.join(outdir_name, args.name, args.bench, args.format) outdir_orig_path = Path(outdir_orig_name) @@ -123,10 +123,10 @@ taco_format_orig_filename = "/home/avb03/sam/FROST_FORMATTED_TACO/" + args.name + "_" + levels + '.txt' parse_taco_format(taco_format_orig_filename, outdir_orig_name, 'B', args.format) - #Need this line? formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + # Need this line? formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") file_path_name = os.path.join(outdir_orig_name, "tensor_B_mode_shape") file1 = open(file_path_name, 'r') - shape = [0]*3 + shape = [0] * 3 lines = file1.readlines() count = 0 @@ -136,7 +136,7 @@ count += 1 # coo = inputCache.load(tensor, False) dimension_k = random.randint(min(shape), 10) - dimension_l = shape[2] + dimension_l = shape[2] dimension_j = shape[1] # formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") @@ -148,8 +148,8 @@ # vec = scipy.sparse.random(shape[2], 1, data_rvs=np.ones) # vec = vec.toarray().flatten() # formatWriter.writeout_separate_vec(vec, out_path, tensorname) - #FormatWriter.writeout_separate_vec(vec, out_path, tensorname, tensorname) - #formatWriter.writeout_separate_sparse_only() + # FormatWriter.writeout_separate_vec(vec, out_path, tensorname, tensorname) + # formatWriter.writeout_separate_sparse_only() elif args.bench == "tensor3_mttkrp": outdir_orig_name = os.path.join(outdir_name, args.name, args.bench, args.format) outdir_orig_path = Path(outdir_orig_name) @@ -157,10 +157,10 @@ taco_format_orig_filename = "/home/avb03/sam/FROST_FORMATTED_TACO/" + args.name + "_" + levels + '.txt' parse_taco_format(taco_format_orig_filename, outdir_orig_name, 'B', args.format) - + file_path_name = os.path.join(outdir_orig_name, "tensor_B_mode_shape") file1 = open(file_path_name, 'r') - shape = [0]*3 + shape = [0] * 3 lines = file1.readlines() count = 0 @@ -168,7 +168,7 @@ for line in lines: shape[count] = int(line) count += 1 - + dimension_i = shape[0] dimension_k = shape[1] dimension_l = shape[2] @@ -191,7 +191,7 @@ # parse_taco_format(taco_format_orig_filename, outdir_other_name, tensorname, args.format, hw_filename=args.hw) else: - #this code is used for: tensor3_elemadd, tensor3_innerprod + # this code is used for: tensor3_elemadd, tensor3_innerprod taco_format_orig_filename = os.path.join(taco_format_dirname, args.name + "_" + levels + '.txt') taco_format_shift_filename = os.path.join(taco_format_dirname, args.name + '_shift_' + levels + '.txt') diff --git a/scripts/tiling/generate_gold_matmul_tiled.py b/scripts/tiling/generate_gold_matmul_tiled.py index 4b585a19..fbe68e08 100644 --- a/scripts/tiling/generate_gold_matmul_tiled.py +++ b/scripts/tiling/generate_gold_matmul_tiled.py @@ -13,6 +13,7 @@ app_name = "mat_mattransmul" + def generate_gold_matmul_tiled(tile_crd_b, tile_crd_c, dirname, out_format="ss01"): # CSR formatted_dir = f"./tiles/{app_name}/mtx" diff --git a/scripts/tiling/generate_gold_mattransmul.py b/scripts/tiling/generate_gold_mattransmul.py index b044a949..a298e2bb 100644 --- a/scripts/tiling/generate_gold_mattransmul.py +++ b/scripts/tiling/generate_gold_mattransmul.py @@ -11,6 +11,7 @@ from pathlib import Path from scripts.util.util import round_sparse + def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname, out_format="ss01"): # CSR formatted_dir = f"./tiles/mat_mattransmul/mtx" @@ -54,7 +55,6 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname, itr += 1 B_scipy = B_scipy.tocsr() - C_scipy = scipy.io.mmread(C_filename) # print(C_filename) # print("\nC_scipy: ", C_scipy) @@ -81,7 +81,7 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname, # gold_nd = (B_scipy @ C_scipy) # gold_nd = B_scipy.dot(C_scipy) - #constants + # constants alpha = 2 beta = 2 @@ -89,7 +89,7 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname, print("C_scipy.shape: ", C_scipy.shape) print("d_scipy.shape: ", d_scipy.shape) - gold_nd = alpha*(B_scipy @ C_scipy) + beta * d_scipy + gold_nd = alpha * (B_scipy @ C_scipy) + beta * d_scipy # print(gold_nd) gold_out = gold_nd.tocoo() @@ -113,14 +113,14 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname, # gold_nd = (B_scipy @ C_scipy) # gold_nd = B_scipy.dot(C_scipy) - #constants + # constants alpha = 2 beta = 2 # print(d_scipy.todense()) gold_nd = beta * d_scipy # print(gold_nd) - if(np.count_nonzero(gold_nd.todense()) == 0): + if (np.count_nonzero(gold_nd.todense()) == 0): print("output is all zero") return @@ -132,7 +132,6 @@ def generate_gold_mattransmul_tiled(tile_crd_b, tile_crd_c, tile_crd_d, dirname, tile_crd_c[0]) + "_" + str(tile_crd_c[1]) + "_" + str(tile_crd_d[0]) + "_" + str(tile_crd_d[1]) + ".mtx", gold_out) - if __name__ == "__main__": parser = argparse.ArgumentParser(description="Generate tiled output gold") parser.add_argument("--yaml_name", type=str, default="memory_config_real.yaml") diff --git a/scripts/util/util.py b/scripts/util/util.py index 8332ba1b..6eda9b1d 100644 --- a/scripts/util/util.py +++ b/scripts/util/util.py @@ -535,7 +535,7 @@ def writeout_separate_vec(self, vec, dir_path, tensorname, format_str="s0", hw=T ofile.write(array_newline_str(vec_shape)) def writeout_separate_sparse_only(self, coo, dir_path, tensorname, format_str="ss01", hw=True): - + if format_str == "ss01": dcsr_dir = Path(dir_path) dcsr_dir.mkdir(parents=True, exist_ok=True, mode=0o777) diff --git a/setup_tiling_mat.py b/setup_tiling_mat.py index da8b082f..febdfc87 100644 --- a/setup_tiling_mat.py +++ b/setup_tiling_mat.py @@ -7,7 +7,7 @@ from sam.util import SUITESPARSE_PATH -#Usage: python3 setup_tiling_mat.py +# Usage: python3 setup_tiling_mat.py ## PARAMS ###################################################################### @@ -20,6 +20,7 @@ print("DATA: ", data) ############################################################################### + def write_to_line(file_path, line_number, new_content): with open(file_path, 'r') as file: lines = file.readlines() @@ -33,6 +34,7 @@ def write_to_line(file_path, line_number, new_content): with open(file_path, 'w') as file: file.writelines(lines) + def replace_ones_with_zeros(mtx_file): with open(mtx_file, 'r') as file: lines = file.readlines() @@ -50,30 +52,30 @@ def replace_ones_with_zeros(mtx_file): i = 0 for datum in data: - tilesize = tilesizes[i] - - yaml_file = "sam/sim/src/tiling/memory_config_onyx.yaml" - mem_tile_line = f"Mem_tile_size: {tilesize}" - print(mem_tile_line) - write_to_line(yaml_file, 19, mem_tile_line) - - rmdir = f"rm -rf tiles/{app_name}" - os.system(rmdir) - - print(f"{SUITESPARSE_PATH}/{datum}.mtx") - mtx_file = glob.glob(f"{SUITESPARSE_PATH}/{datum}.mtx")[0] - os.makedirs("extensor_mtx", exist_ok=True) - shutil.copy(mtx_file,f"extensor_mtx/{datum}.mtx") - - command = f"./scripts/suitesparse_memory_model_runner.sh {datum} {app_name}" - os.system(command) - - docker_clean = f"docker exec {docker_path} rm -r /aha/garnet/tiles_{app_name}_{datum}" - print(docker_clean) - os.system(docker_clean) - - docker_copy_command = f"docker cp tiles {docker_path}:/aha/garnet/tiles_{app_name}_{datum}" - print(docker_copy_command) - os.system(docker_copy_command) - - i = i+1 + tilesize = tilesizes[i] + + yaml_file = "sam/sim/src/tiling/memory_config_onyx.yaml" + mem_tile_line = f"Mem_tile_size: {tilesize}" + print(mem_tile_line) + write_to_line(yaml_file, 19, mem_tile_line) + + rmdir = f"rm -rf tiles/{app_name}" + os.system(rmdir) + + print(f"{SUITESPARSE_PATH}/{datum}.mtx") + mtx_file = glob.glob(f"{SUITESPARSE_PATH}/{datum}.mtx")[0] + os.makedirs("extensor_mtx", exist_ok=True) + shutil.copy(mtx_file, f"extensor_mtx/{datum}.mtx") + + command = f"./scripts/suitesparse_memory_model_runner.sh {datum} {app_name}" + os.system(command) + + docker_clean = f"docker exec {docker_path} rm -r /aha/garnet/tiles_{app_name}_{datum}" + print(docker_clean) + os.system(docker_clean) + + docker_copy_command = f"docker cp tiles {docker_path}:/aha/garnet/tiles_{app_name}_{datum}" + print(docker_copy_command) + os.system(docker_copy_command) + + i = i + 1 diff --git a/setup_tiling_tensors.py b/setup_tiling_tensors.py index 15d73c98..25642121 100644 --- a/setup_tiling_tensors.py +++ b/setup_tiling_tensors.py @@ -16,8 +16,8 @@ for tensor in tiled_tensors: if any(x in tensor for x in vector_names): - #vector + # vector inputCache.load(tensor) formatwriter.writeout_separate_sparse_only() else: - print("regular 3d tensors can be packed and tiled") \ No newline at end of file + print("regular 3d tensors can be packed and tiled") diff --git a/tile_pairing.py b/tile_pairing.py index 8af7000b..d2a5c40f 100644 --- a/tile_pairing.py +++ b/tile_pairing.py @@ -4,7 +4,7 @@ import os import json -# test = "bcsstm26" +# test = "bcsstm26" # test = "rel5" test = "qiulp" # test = "adder_dcop_30" @@ -14,7 +14,7 @@ # app_name = "matmul_ijk" # app_name = "mat_mattrpython3ansmul" app_name = "mat_elemmul" -const_val = 2 # only for mat_mattransmul +const_val = 2 # only for mat_mattransmul tiles_accumulation = {} @@ -56,7 +56,7 @@ c_loc = c[-7:] b_loc = b_loc.split("_") c_loc = c_loc.split("_") - if(b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2]): + if (b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2]): print(b, c) if b_loc[2] not in tiles_accumulation: @@ -66,7 +66,7 @@ if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{b}/B0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_0_crd") shutil.copy(f"{b}/B0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_0_seg") @@ -96,7 +96,7 @@ c_loc = c[-7:] b_loc = b_loc.split("_") c_loc = c_loc.split("_") - if(b_loc == c_loc): + if (b_loc == c_loc): print(b, c) if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") @@ -142,18 +142,18 @@ c_loc = c_loc.split("_") d_loc = d_loc.split("_") - if(b_loc[1] == c_loc[0] and b_loc[3] == c_loc[1] and b_loc[0] == d_loc[0] and b_loc[2] == d_loc[1]): - # if(b_loc[1] == d_loc[0] and b_loc[3] == d_loc[1] and b_loc[0] == c_loc[0] and b_loc[2] == c_loc[1]): + if (b_loc[1] == c_loc[0] and b_loc[3] == c_loc[1] and b_loc[0] == d_loc[0] and b_loc[2] == d_loc[1]): + # if(b_loc[1] == d_loc[0] and b_loc[3] == d_loc[1] and b_loc[0] == c_loc[0] and b_loc[2] == c_loc[1]): d_loc_paired.append(d_loc) print(f"\n ----- TILE {tile} ----- \n") - print("B is: ", b) #in build_tb, B == C, c == d, d == f. (#FIXME: change build_tb) + print("B is: ", b) # in build_tb, B == C, c == d, d == f. (#FIXME: change build_tb) print("C is: ", c) print("d is: ", d) print(f"\n ----- TILE {tile} ----- \n") if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{b}/B0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") shutil.copy(f"{b}/B0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") @@ -184,23 +184,24 @@ with open(f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_vals", 'w') as file: file.write(str(const_val)) - + with open(f"./MAT_TMP_DIR/{tile_str}/tensor_e_mode_vals", 'w') as file: file.write(str(const_val)) - + tile = tile + 1 elif d_loc not in d_loc_paired: - # case: B and c tiles are zero but d is nonzero. We have all d tiles. Just take a B and c tile, copy it and make it zero.' + # case: B and c tiles are zero but d is nonzero. We have all d tiles. Just + # take a B and c tile, copy it and make it zero.' d_loc_paired.append(d_loc) print(f"\n ----- TILE D-unpaired {tile} ----- \n") - print("B (zero tile) is: ", b) #in build_tb, B == C, c == d, d == f. (#FIXME: change build_tb) + print("B (zero tile) is: ", b) # in build_tb, B == C, c == d, d == f. (#FIXME: change build_tb) print("C (zero tile) is: ", c) print("d is: ", d) print(f"\n ----- TILE D-unpaired {tile} ----- \n") if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{b}/B0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") shutil.copy(f"{b}/B0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") @@ -246,7 +247,7 @@ with open(f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_vals", 'w') as file: file.write(str(const_val)) - + with open(f"./MAT_TMP_DIR/{tile_str}/tensor_e_mode_vals", 'w') as file: file.write(str(const_val)) @@ -263,11 +264,11 @@ c_loc = c_loc.split("_") # if(b_loc[1] == c_loc[0] and b_loc[3] == c_loc[1] and b_loc[0] == d_loc[0] and b_loc[2] == d_loc[1]): - if(b_loc[1] == c_loc[0] and b_loc[3] == c_loc[1]): - print(b,c) + if (b_loc[1] == c_loc[0] and b_loc[3] == c_loc[1]): + print(b, c) if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{b}/B0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_0_crd") shutil.copy(f"{b}/B0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_0_seg") @@ -302,13 +303,13 @@ d_loc = d_loc.split("_") # if(b_loc[1] == c_loc[0] and b_loc[3] == c_loc[1] and b_loc[0] == d_loc[0] and b_loc[2] == d_loc[1]): - if(c_loc[0] == b_loc[0] and c_loc[2] == b_loc[1] and c_loc[1] == d_loc[0] and c_loc[3] == d_loc[1]): + if (c_loc[0] == b_loc[0] and c_loc[2] == b_loc[1] and c_loc[1] == d_loc[0] and c_loc[3] == d_loc[1]): print(b, c, d) b_loc_paired.append(b_loc) - + if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{c}/C0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_crd") shutil.copy(f"{c}/C0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_seg") @@ -340,10 +341,10 @@ tile = tile + 1 elif b_loc not in b_loc_paired: b_loc_paired.append(b_loc) - + if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{c}/C0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_crd") shutil.copy(f"{c}/C0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_seg") @@ -403,11 +404,11 @@ d_loc = d_loc.split("_") # first j, then i (k is a free coordinate) - if(b_loc[0] == d_loc[1] and b_loc[2] == d_loc[3] and b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2]): + if (b_loc[0] == d_loc[1] and b_loc[2] == d_loc[3] and b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2]): print(b, c, d) if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") - + shutil.copy(f"{b}/B0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_0_crd") shutil.copy(f"{b}/B0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_0_seg") @@ -449,7 +450,7 @@ # if(b_loc == d_loc and b_loc != c_loc): # b_equal_d_no_c += 1 - if(b_loc == c_loc and b_loc == d_loc): + if (b_loc == c_loc and b_loc == d_loc): print(b, c, d) if not os.path.exists(f"./MAT_TMP_DIR/{tile_str}"): os.mkdir(f"./MAT_TMP_DIR/{tile_str}") @@ -498,4 +499,4 @@ with open("../tiles_accumulation.json", "w") as file: json.dump(tiles_accumulation, file) -print("there are ", tile, " tiles") \ No newline at end of file +print("there are ", tile, " tiles")