diff --git a/.gitignore b/.gitignore index 0df30951..a0b61c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,13 @@ suitesparse-bench/ sam-outputs/ compiler/benchmark/ +# Generated SAM simulator tests +*/sim/test/apps/test_*.py + +# Tensor files +*.mtx +*.tns + # Temporary or generated tensor directories tmp_mat*/ tiles/ diff --git a/Makefile b/Makefile index d0e29ccb..c55abca2 100644 --- a/Makefile +++ b/Makefile @@ -31,20 +31,20 @@ endif ifeq ("$(NEVA)","ON") CMD := OMP_PROC_BIND=true LD_LIBRARY_PATH=compiler/build/lib/:$(LD_LIBRARY_PATH) numactl -C 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 -m 0 compiler/build/taco-bench $(BENCHFLAGS) - export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/ - export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/ - export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted - export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor - export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted - export TACO_TENSOR_PATH=/nobackup/owhsu/sparse-datasets + # export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/ + # export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/ + # export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted + # export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor + # export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted + # export TACO_TENSOR_PATH=/nobackup/owhsu/sparse-datasets else ifeq ("$(LANKA)", "ON") CMD := OMP_PROC_BIND=true LD_LIBRARY_PATH=compiler/build/lib/:$(LD_LIBRARY_PATH) numactl -C 0,2,4,6,8,10,24,26,28,30,32,34 -m 0 compiler/build/taco-bench $(BENCHFLAGS) - export SUITESPARSE_PATH=/data/scratch/changwan/florida_all - export FROSTT_PATH=/data/scratch/owhsu/datasets/frostt - export TACO_TENSOR_PATH=/data/scratch/owhsu/datasets - export SUITESPARSE_FORMATTED_PATH=/data/scratch/owhsu/datasets/suitesparse-formatted - export FROSTT_FORMATTED_TACO_PATH=/data/scratch/owhsu/datasets/frostt-formatted/taco-tensor - export FROSTT_FORMATTED_PATH=/data/scratch/owhsu/datasets/frostt-formatted + # export SUITESPARSE_PATH=/data/scratch/changwan/florida_all + # export FROSTT_PATH=/data/scratch/owhsu/datasets/frostt + # export TACO_TENSOR_PATH=/data/scratch/owhsu/datasets + # export SUITESPARSE_FORMATTED_PATH=/data/scratch/owhsu/datasets/suitesparse-formatted + # export FROSTT_FORMATTED_TACO_PATH=/data/scratch/owhsu/datasets/frostt-formatted/taco-tensor + # export FROSTT_FORMATTED_PATH=/data/scratch/owhsu/datasets/frostt-formatted else CMD := LD_LIBRARY_PATH=compiler/build/lib/:$(LD_LIBRARY_PATH) compiler/build/taco-bench $(BENCHFLAGS) endif @@ -114,14 +114,6 @@ suitesparse-formats: guard-SUITESPARSE_FORMATTED_PATH guard-SUITESPARSE_PATH frostt-formats: taco/build guard-FROSTT_FORMATTED_PATH guard-FROSTT_PATH ./scripts/formatting/generate_frostt_formats.sh -.PHONY: env -env: - export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/ - export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/ - export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted - export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor - export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted - .PHONY: pydepends pydepends: conda env export > environment.yml diff --git a/compiler/baseline.cpp b/compiler/baseline.cpp index 9919a762..f911d9b0 100644 --- a/compiler/baseline.cpp +++ b/compiler/baseline.cpp @@ -315,7 +315,8 @@ enum SuiteSparseOp { SDDMM = 4, MATTRANSMUL = 5, RESIDUAL = 6, - MMADD = 7 + MMADD = 7, + MMMUL = 8 }; std::string opName(SuiteSparseOp op) { @@ -341,6 +342,9 @@ std::string opName(SuiteSparseOp op) { case MMADD: { return "mmadd"; } + case MMMUL: { + return "mmmul" + } default: return ""; } @@ -467,6 +471,13 @@ static void bench_suitesparse(benchmark::State &state, SuiteSparseOp op, int fil result(i, j) = ssTensor(i, j) + otherShifted(i, j); break; } + case MMMUL: { + result = Tensor("result", ssTensor.getDimensions(), ssTensor.getFormat(), fill_value); + + IndexVar i, j, k; + result(i, j) = ssTensor(i, j) * otherShifted(i, j); + break; + } case MATTRANSMUL: { result = Tensor("result", {DIM1}, Format(Sparse), fill_value); @@ -516,4 +527,5 @@ static void bench_suitesparse(benchmark::State &state, SuiteSparseOp op, int fil // TODO: need to fix for DCSC for this TACO_BENCH_ARGS(bench_suitesparse, mat_mattransmul, MATTRANSMUL); TACO_BENCH_ARGS(bench_suitesparse, matmul_spmm, SPMM); + TACO_BENCH_ARGS(bench_suitesparse, mat_elemmul, MMMUL); diff --git a/count_nnz_tiling.py b/count_nnz_tiling.py new file mode 100644 index 00000000..c013fa4f --- /dev/null +++ b/count_nnz_tiling.py @@ -0,0 +1,52 @@ +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()] + + nonzeros = sum(1 for val in matrix_values if val != 0) + + return nonzeros + + +tile_dirs = glob.glob("SPARSE_TESTS/MAT_TMP_DIR/tile*") +num_tiles = len(tile_dirs) +limit = 900 +print("there are ", num_tiles, "tiles") + +sparsity_B = 0 +sparsity_C = 0 +# tilesize=int(sys.argv[1])**2 +tot_num_nonzeros = 0 +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' + + num_nonzeros = count_nonzeros(tensor_C_values_file) + tot_num_nonzeros += num_nonzeros + if num_nonzeros >= limit: + print("num_nonzeros: ", num_nonzeros) + print("error! too many nonzeros in INPUT matrices") + raise Exception + + tensor_C_values_file = f'SPARSE_TESTS/MAT_TMP_DIR/tile{tile_num}/tensor_B_mode_vals' + + num_nonzeros = count_nonzeros(tensor_C_values_file) + tot_num_nonzeros += num_nonzeros + if num_nonzeros >= limit: + print("num_nonzeros: ", num_nonzeros) + 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") + raise Exception + +sparsity_B /= num_tiles +sparsity_C /= num_tiles + +print("sparsity_B: ", sparsity_B) +print("sparsity_C: ", sparsity_C) \ No newline at end of file diff --git a/find_max_tilesize.py b/find_max_tilesize.py new file mode 100644 index 00000000..bab4549a --- /dev/null +++ b/find_max_tilesize.py @@ -0,0 +1,78 @@ +import os +import sys +import glob + +def write_to_line(file_path, line_number, new_content): + with open(file_path, 'r') as file: + lines = file.readlines() + + if line_number > len(lines) or line_number < 1: + # Line number is out of range + return + + lines[line_number - 1] = new_content + '\n' + + 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') + + # Read the contents of the file + with open('output.txt', 'r') as file: + output = file.read() + + # Check if the keyword is present in the output + if keyword in output: + # Optionally, you can delete the output file + os.remove('output.txt') + return True + else: + # Optionally, you can delete the output file + os.remove('output.txt') + return False + + +tile_size = 300 +step = 10 + +for _ in range(20): + print("********************") + print("tile size: ", tile_size) + print("step: ", step) + + yaml_file = "sam/sim/src/tiling/memory_config_onyx.yaml" + mem_tile_line = f"Mem_tile_size: {tile_size}" + print(mem_tile_line) + write_to_line(yaml_file, 19, mem_tile_line) + + run_setup_script = "python3 setup_tiling_mat.py > temp.txt" + os.system(run_setup_script) + print(run_setup_script) + + run_tile_pairing = "python3 tile_pairing.py > temp.txt" + os.system(run_tile_pairing) + print(run_tile_pairing) + + run_count = "python3 count_nnz_tiling.py" + print(run_count) + + if (check_keyword_in_output(run_count, "error")) == False: + tile_size += step + step *= 2 + else: + print("****************Tile broken!") + tile_size -= step + step //= 2 + + if tile_size == 450: + break + + if step == 0: + if _ >= 15: + step = 10 + else: + break + +print("max tile size: ", tile_size) diff --git a/generate_spmv_sparsity_sweep.py b/generate_spmv_sparsity_sweep.py new file mode 100644 index 00000000..3d2f00e0 --- /dev/null +++ b/generate_spmv_sparsity_sweep.py @@ -0,0 +1,164 @@ +#script to generate 50 random 3D tensors (seeded, produces same 50 each time) +import numpy as np +import random +import os +import scipy.io as sio +import scipy.sparse as sps +# from scipy.io import mmread + +# Set the seed value +#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 + +#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 + +# x goes from 0 to __ (before 0 to 49) +for x in range(1): + # dimensions_onematrix[0] = random.randint(1,60) + # dimensions_onematrix[1] = random.randint(3,60) + + # while((dimensions_onetensor[0]*dimensions_onetensor[1]*dimensions_onetensor[2])>901): + # dimensions_onematrix[0] = random.randint(1,60) + # dimensions_onematrix[1] = random.randint(3,60) + # dimensions_onematrix[2] = random.randint(3,60) + dimensions_onematrix[0] = 10 + dimensions_onematrix[1] = 10 + + dimensions[x*3] = dimensions_onematrix[0] + dimensions[(x*3)+1] = dimensions_onematrix[1] + + dimensions_onematrix[0] = 0 + dimensions_onematrix[1] = 0 + #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) +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) +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") + sparsity = sparsities[i] + f = open(filename, "w") + f.write('\n') + lineToAddInFile = "" + 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 + # randomNumber = random.randint(1,9) + randomNumber = random.random() + 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]) + 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 + 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 = [] + row_indices = [] + col_indices = [] + for line in data: + row, col, value = map(float, line.strip().split()) + row_indices.append(int(row) - 1) # Convert to 0-based indexing + 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") + 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") + sio.savemat(output_file1, {'vector': vec}, do_compression=True) + + + # f.close() + # a = mmread(filename) + # a.toarray() + # scipy.io.savemat("rand_matrix"+str(matrix_num)+".mat", {'mydata': a}) + + # f.write(""+str(dimensions[i*3]) + " " + str(dimensions[i*3+1]) + " " + str(countnnz)) + # f.write("\n") + 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') + +""" +arr = np.zeros([dimensions[0],dimensions[1],dimensions[2]], dtype=int) +randomNumber = 0 +numToInsert = 0 +for x in range(len(arr)): + for y in range(len(arr[x])): + for z in range(len(arr[x][y])): + #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) + if(randomNumber==4 or randomNumber==5 or randomNumber==6): + numToInsert = random.randint(1,100) + arr[x][y][z] = numToInsert + numToInsert = 0 + print(arr[x][y][z]) + + #lineToAddInFile="" + str(x) + " " + str(y) + " " + str(z) + " " + str(arr[x][y][z]) + #f.write(lineToAddInFile + '\n') + +print('dimensions:') +print(dimensions[3]) +print(dimensions[4]) +print(dimensions[5]) +print('tensor vals') +arr = np.zeros([dimensions[3],dimensions[4],dimensions[5]], dtype=int) +randomNumber = 0 +numToInsert = 0 +for x in range(len(arr)): + for y in range(len(arr[x])): + for z in range(len(arr[x][y])): + #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) + if(randomNumber==4 or randomNumber==5 or randomNumber==6): + numToInsert = random.randint(1,100) + arr[x][y][z] = numToInsert + numToInsert = 0 + randomNumber = 0 + print(arr[x][y][z]) + + #lineToAddInFile="" + str(x) + " " + str(y) + " " + str(z) + " " + str(arr[x][y][z]) + #f.write(lineToAddInFile + '\n') +""" diff --git a/maximum_tiling.py b/maximum_tiling.py new file mode 100644 index 00000000..2390ed21 --- /dev/null +++ b/maximum_tiling.py @@ -0,0 +1,270 @@ +import glob +import sys +import numpy as np +import scipy +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: + substring = string[indices[1] + len("tile") + 1:] + substring = substring.rstrip(".mtx") + numbers = substring.split("_") + return numbers + +def pair_tiles(app_name): + path = f"tiles/{app_name}/mtx" + tile_pairing = {} + + operands = [] + if "matmul" in app_name: + operands = ["B", "C"] + 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: + b_loc = get_tile_id(b) + c_loc = get_tile_id(c) + if (b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2]): + tile_pairing[tile] = [b, c] + tile += 1 + elif "elemmul" in app_name: + operands = ["B", "C"] + 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: + b_loc = get_tile_id(b) + c_loc = get_tile_id(c) + if (b_loc == c_loc): + tile_pairing[tile] = [b, c] + tile += 1 + elif "elemadd3" in app_name: + operands = ["B", "C", "D"] + operand_files = get_files_from_dir(path, operands) + b_tensors = operand_files["B"] + c_tensors = operand_files["C"] + d_tensors = operand_files["D"] + + tile = 0 + for b in b_tensors: + for c in c_tensors: + b_loc = get_tile_id(b) + c_loc = get_tile_id(c) + if (b_loc != c_loc): + continue + + for d in d_tensors: + d_loc = get_tile_id(d) + if (b_loc == c_loc and c_loc == d_loc): + tile_pairing[tile] = [b, c, d] + tile += 1 + + elif "mat_mask_tri" in app_name: + operands = ["B", "C", "D"] + operand_files = get_files_from_dir(path, operands) + b_tensors = operand_files["B"] + c_tensors = operand_files["C"] + d_tensors = operand_files["D"] + + tile = 0 + for b in b_tensors: + for c in c_tensors: + b_loc = get_tile_id(b) + c_loc = get_tile_id(c) + if not (b_loc[0] == c_loc[0] and b_loc[2] == c_loc[2]): + continue + + 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]): + tile_pairing[tile] = [b, c, d] + tile += 1 + elif "mat_vecmul_iter" in app_name: + operands = ["B", "C", "D", "E", "f"] + operand_files = get_files_from_dir(path, operands) + b_tensors = operand_files["B"] + c_tensors = operand_files["C"] + d_tensors = operand_files["D"] + e_tensors = operand_files["E"] + f_tensors = operand_files["f"] + + tile = 0 + + for b in b_tensors: + for c in c_tensors: + b_loc = get_tile_id(b) + c_loc = get_tile_id(c) + if not (b_loc[1] == c_loc[0] and b_loc[3] == c_loc[2]): + continue + for d in d_tensors: + d_loc = get_tile_id(d) + # check k coord + if not (c_loc[1] == d_loc[0] and c_loc[3] == d_loc[2]): + continue + for e in e_tensors: + e_loc = get_tile_id(e) + # check l coord + if not (d_loc[1] == e_loc[0] and d_loc[3] == e_loc[2]): + 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]): + 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: + B_mat = read_mtx(value[0]) + C_mat = read_mtx(value[1]) + 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): + print("tile = ", key) + print("B_tile_ID = ", value[0]) + print("C_tile_ID = ", value[1]) + print("out = ", out) + print("count = ", np.count_nonzero(out)) + return EarlyReturn() + elif "elemmul" in app_name: + B_mat = read_mtx(value[0]) + C_mat = read_mtx(value[1]) + 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: + print("tile = ", key) + print("B_tile_ID = ", value[0]) + print("C_tile_ID = ", value[1]) + print("out = ", out) + print("count = ", np.count_nonzero(out)) + return EarlyReturn() + elif "elemadd3" in app_name: + B_mat = read_mtx(value[0]) + C_mat = read_mtx(value[1]) + D_mat = read_mtx(value[2]) + + 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: + print("tile = ", key) + print("B_tile_ID = ", value[0]) + print("C_tile_ID = ", value[1]) + print("D_tile_ID = ", value[2]) + print("out = ", out) + print("count = ", np.count_nonzero(out)) + return EarlyReturn() + elif "mat_mask_tri" in app_name: + B_mat = read_mtx(value[0]) + C_mat = read_mtx(value[1]) + 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: + print("tile = ", key) + print("B_tile_ID = ", value[0]) + print("C_tile_ID = ", value[1]) + print("D_tile_ID = ", value[2]) + print("out = ", out) + print("count = ", np.count_nonzero(out)) + return EarlyReturn() + elif "mat_vecmul_iter" in app_name: + B_mat = read_mtx(value[0]) + C_mat = read_mtx(value[1]) + D_mat = read_mtx(value[2]) + E_mat = read_mtx(value[3]) + f_mat = read_mtx(value[4]) + # we transpose bc we swap in copy formatted + 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: + print("tile = ", key) + print("B_tile_ID = ", value[0]) + print("C_tile_ID = ", value[1]) + print("D_tile_ID = ", value[2]) + print("E_tile_ID = ", value[3]) + print("f_tile_ID = ", value[4]) + print("out = ", out) + print("count = ", np.count_nonzero(out)) + breakpoint() + return EarlyReturn() + return None + +def find_optimal_tilesize(app_name, datum, initial=30, step_size=10): + tile_size = initial + max_tile_size = initial + prev_tile_pairing = None + + # while True: + for _ in range(50): + call_tiling = f"python3 setup_tiling_mat.py {app_name} {datum} {tile_size} > temp.txt" + os.system(call_tiling) + print(call_tiling) + + tile_pairing = pair_tiles(app_name) + exit_status = compute_outputs(tile_pairing, app_name) + 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 = {} + # for i in range(1, 11): + app_name = "matmul_ijk" + datum = "N_biocarta" + + # tile_pairing = pair_tiles(app_name) + # 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(f"MAX TILESIZE for {app_name}, {datum}: {max_tile_size}") + print(f"NUMBER OF TILES: {len(tile_pairing.keys())}") + print("-"*20) + + max_list[datum] = [max_tile_size, len(tile_pairing.keys())] + + call_tiling = f"python3 setup_tiling_mat.py {app_name} {datum} {max_tile_size} > temp.txt" + os.system(call_tiling) + print(call_tiling) + + # print(max_list) \ No newline at end of file diff --git a/sam/onyx/synthetic/generate_fixed_nnz_mats.py b/sam/onyx/synthetic/generate_fixed_nnz_mats.py index ec099dfb..6671fcc2 100644 --- a/sam/onyx/synthetic/generate_fixed_nnz_mats.py +++ b/sam/onyx/synthetic/generate_fixed_nnz_mats.py @@ -1,7 +1,7 @@ import scipy.io import scipy.sparse import numpy as np - +import argparse def generate_mat(nnz, dim): return scipy.sparse.random(dim, dim, nnz / (dim**2), data_rvs=np.ones) @@ -14,9 +14,21 @@ def write_mtx(path, t): if __name__ == "__main__": seed = 0 np.random.seed(seed) - # 1024 - dims = list(range(1024, 15721, 1336)) - nnzs = [5000, 10000, 25000, 50000] + + 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') + args = parser.parse_args() + + + if args.extensor: + dims = list(range(1024, 15721, 1336)) + nnzs = [5000, 10000, 25000, 50000] + else: + dims = args.dim + nnzs = args.nnz + print("RUNNING:", dims, nnzs) for nnz in nnzs: for dim in dims: diff --git a/sam/sim/src/channel.py b/sam/sim/src/channel.py index 87fd36fe..6e20222f 100644 --- a/sam/sim/src/channel.py +++ b/sam/sim/src/channel.py @@ -219,6 +219,7 @@ def input_token_(self, token): self.downstream_token = token +# FIXME: Follow code style and fix class naming convention and make sure it's base is primitive... class memory_block(): def __init__(self, name="B", skip_blocks=False, element_size=2, level=None, indexes=2, size=1000 * 2, nbuffer=False, latency=10, debug=False, bandwidth=2, diff --git a/sam/sim/src/tiling/memory_config_onyx.yaml b/sam/sim/src/tiling/memory_config_onyx.yaml index befb9cad..a6fa9d35 100644 --- a/sam/sim/src/tiling/memory_config_onyx.yaml +++ b/sam/sim/src/tiling/memory_config_onyx.yaml @@ -15,5 +15,5 @@ Bytes_per_element: 2 # Number n_levels: 3 level_names: ["Main", "Glb", "Mem"] Main_tile_size: None -Glb_tile_size: 16 # 16 # 120 # n = (nxn) elements -Mem_tile_size: 128 # Size of one dense dimension. 8 = (8x8) +Glb_tile_size: 8 # 8 = (8x8) = 64 elements +Mem_tile_size: 30 diff --git a/sam/sim/src/tiling/tile.py b/sam/sim/src/tiling/tile.py index 2ff1afe4..47cf15a0 100644 --- a/sam/sim/src/tiling/tile.py +++ b/sam/sim/src/tiling/tile.py @@ -1,18 +1,38 @@ import numpy as np import scipy.sparse +import scipy.io import os import argparse import ast import yaml import copy import pickle +import random +import sparse +import sys -from itertools import compress from pathlib import Path -from sam.util import SuiteSparseTensor, InputCacheSuiteSparse, ScipyTensorShifter -from sam.sim.src.tiling.process_expr import parse_all, update_dict -SAM_STRS = {"matmul_ikj": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss:1,0 -f=C:ss -s=reorder(k,i,j)"} +from sam.util import SUITESPARSE_PATH, SuiteSparseTensor, InputCacheSuiteSparse, ScipyTensorShifter, \ + FROSTT_PATH, FrosttTensor, PydataSparseTensorDumper, InputCacheTensor, constructOtherMatKey, constructOtherVecKey +from sam.sim.src.tiling.process_expr import parse_all + +# FIXME: This should not be here... Set your SAM_HOME directory +custom_path = '/home/avb03/sam' +sys.path.append(custom_path) + +SAM_STRS = {"matmul_kij": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss:1,0 -f=C:ss -s=reorder(k,i,j)", + "matmul_ikj": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss -f=C:ss -s=reorder(i,k,j)", + "matmul_ijk": "X(i,j)=B(i,k)*C(k,j) -f=X:ss -f=B:ss -f=C:ss:1,0 -s=reorder(i,j,k)", + "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_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", + "mat_mask_tri": "X(i,j)=B(i,j)*C(i,k)*D(k,j) -f=X:ss -f=B:ss -f=C:ss -f=D:ss:1,0 -s=reorder(i,j,k)", + "mat_vecmul_iter": "X(i,j)=B(i,j)*C(j,k)*D(k,l)*E(l,m)*f(m) -f=X:ss -f=B:ss -f=C:ss -f=D:ss -f=E:ss -f=f:s -s=reorder(i,j,k,l,m)"} def print_dict(dd): @@ -20,6 +40,12 @@ def print_dict(dd): print(k, ":", v) +def print_ast(node): + for child in ast.iter_child_nodes(node): + print_ast(child) + print(node) + + def get_ivars(names, expr): [lhs, rhs] = expr.split("=") @@ -65,27 +91,102 @@ def parse_sam_input(string): str_arr = sam_str.split(" ") dictionary = parse_all(str_arr, has_quotes=False) + print("dictionary is: ", dictionary) # Assume there are no repeat tensors... tensors = dictionary["rhs_tensors"] + print("tensors are: ", tensors) permutations = [list(map(int, dictionary[tensor]["perm"])) for tensor in tensors] ivars = get_ivars(tensors, str_arr[0]) ivars = [ivars[tensor] for tensor in tensors] + + print("PARSE SAM INPUTS", tensors) return tensors, permutations, ivars +# Outputs Pydata/sparse tensor tiles, given a pydata/sparse tensor (DOK or COO) +# ASSUME: tensor is a scipy.sparse.coo_matrix +# TODO: new_ivar_order right now is assumed to be one fixed order +# In the future, will have to take into acocunt all reorderings +def tile_tensor(tensor, ivar_map, split_map, new_ivar_order=None, tensor_name=""): + human_readable = False + + tiles = dict() + tile_sizes = dict() + order = len(tensor.shape) + + tensor_coo = sparse.COO(tensor) + tensor_points = sparse.DOK.from_coo(tensor_coo) + + print("ivar_map: ", ivar_map) + print("split_map: ", split_map) + print("order = ", order) + + new_shape = [] + for lvl in range(order): + ivar = ivar_map[lvl] + sf = split_map[ivar] + new_shape.append(sf) + + for crds, val in tensor_points.data.items(): + point = list(crds) + + new_point = [] + tile_id = [] + for lvl in range(order): + ivar = ivar_map[lvl] + sf = split_map[ivar] + + new_point.append(point[lvl] % sf) + tile_id.append(int(point[lvl] / sf)) + + # Add in value to the new_point as well + new_point.append(val) + tile_id = tuple(tile_id) + + if tile_id in tiles: + tiles[tile_id].append(new_point) + else: + tiles[tile_id] = [new_point] + + # sort the new coo lists + for key, val in tiles.items(): + if human_readable: + dok = sorted(val) + else: + dok = sparse.DOK(tuple(new_shape)) + for point in val: + dok[tuple(point[0:-1])] = point[-1] + + tiles[key] = dok + + for tile_id, tile_dok in tiles.items(): + tile = tile_dok.to_coo() + # FIXME: This size number isn't correct for tensor tiles + nonempty_rows = tile.nnz + nonempty_row_ind = np.where(nonempty_rows > 0)[0] + tile_sizes[tile_id] = tile.nnz * 2 + 2 * len(nonempty_row_ind) + 3 + + return tiles, tile_sizes + + # Outputs COO tiles, given a COO tensor # ASSUME: tensor is a scipy.sparse.coo_matrix # TODO: new_ivar_order right now is assumed to be one fixed order # In the future, will have to take into acocunt all reorderings -def tile_coo(tensor, ivar_map, split_map, new_ivar_order=None): +def tile_coo(tensor, ivar_map, split_map, new_ivar_order=None, tensor_name=""): human_readable = False tiles = dict() tile_sizes = dict() order = len(tensor.shape) - tensor_points = tensor.todok() + tensor_coo = scipy.sparse.coo_matrix(tensor) + tensor_points = tensor_coo.todok() + + print("ivar_map: ", ivar_map) + print("split_map: ", split_map) + print("order = ", order) new_shape = [] for lvl in range(order): @@ -135,34 +236,45 @@ def tile_coo(tensor, ivar_map, split_map, new_ivar_order=None): # tensor_names: list of tensor names [B,C,D] (from SAM) -# tensors: list of scipy.sparse.coo_matrix following tensor_names (from SAM) +# tensors: list of sparse COO tensors (either Scipy or Pydata/Sparse) following tensor_names (from SAM) # permutation_strs: list of permutation_strs [ss01, ss10] following tensor_names (from SAM) # ivar_strs: list of ivar_strs ["ik", "kj"] following tensor_names (from SAM) # split_map: dictionary of split factors (from hardware) -def cotile_coo(tensor_names, tensors, permutation_strs, ivar_strs, split_map): +def cotile_coo(tensor_names, tensors, permutation_strs, ivar_strs, split_map, higher_order=False): tiled_tensors = dict() tiled_tensor_sizes = dict() + print(tensor_names, tensors, permutation_strs, ivar_strs, split_map) for i, tensor in enumerate(tensors): tensor_name = tensor_names[i] tensor_format = permutation_strs[i] ivar_map = dict() order = len(tensor.shape) + print("order is ", order) for dim in range(order): + print("tensor format: ", tensor_format) + + print("dim is ", dim) + print("tensor_format[dim:dim+1] is ", tensor_format[dim:dim + 1]) + print("tensor name is ", tensor_name) lvl_permutation = tensor_format[dim:dim + 1][0] ivar = ivar_strs[i][dim] ivar_map[lvl_permutation] = ivar + print("ivar_map is ", ivar_map) + + if higher_order: + tiles, tile_sizes = tile_tensor(tensor, ivar_map, split_map, tensor_name=tensor_name) + else: + tiles, tile_sizes = tile_coo(tensor, tensor_name, ivar_map, split_map, tensor_name=tensor_name) - tiles, tile_sizes = tile_coo(tensor, ivar_map, split_map) tiled_tensors[tensor_name] = tiles tiled_tensor_sizes[tensor_name] = tile_sizes return tiled_tensors, tiled_tensor_sizes -def get_other_tensors(app_str, tensor): - tensors = [] - tensors.append(tensor) +def get_other_tensors(app_str, tensor, other_nonempty=True): + tensors = [tensor] if "matmul" in app_str: print("Writing shifted...") @@ -184,24 +296,116 @@ def get_other_tensors(app_str, tensor): tensors.append(shifted) elif "mat_sddmm" in app_str: - pass - elif "mat_mattransmul" in app_str or "mat_residual" in app_str: - pass + print("Writing other tensors, shifted...") + print("Writing shifted...") + shifted = ScipyTensorShifter().shiftLastMode(tensor) + tensors.append(shifted) + + print("Writing shifted2...") + shifted2 = ScipyTensorShifter().shiftLastMode(shifted) + tensors.append(shifted2) + + elif "mat_mask_tri" in app_str: + print("Writing other tensor 1...") + shifted = ScipyTensorShifter().shiftLastMode(tensor) + tensors.append(shifted) + + print("Writing shifted2...") + shifted2 = ScipyTensorShifter().shiftLastMode(shifted) + tensors.append(shifted2) + elif "mat_vecmul_iter" in app_str: + print("Writing other tensor 1...") + tensors.append(tensor) + tensors.append(tensor) + tensors.append(tensor) + + print("writing other vector...") + tensorName = args.input_tensor + variant = "mode1" + path = constructOtherVecKey(tensorName,variant) + tensor_c_from_path = FrosttTensor(path) + tensor_c = tensor_c_from_path.load().todense() + + # breakpoint() + tensors.append(tensor_c) + + elif "mat_mattransmul" in app_str: + print("Writing other tensors...") + rows, cols = tensor.shape # i,j + tensor_c = scipy.sparse.random(cols, 1, data_rvs=np.ones).toarray().flatten() + # tensor_d = scipy.sparse.random(rows, 1, density=1.0, data_rvs=np.ones).toarray().flatten() + tensor_d = scipy.sparse.random(rows, 1, data_rvs=np.ones).toarray().flatten() + + if other_nonempty: + tensor_c[0] = 1 + tensor_d[0] = 1 + + # import pdb; pdb.set_trace() + + tensors.append(tensor_c) + tensors.append(tensor_d) + + elif "mat_residual" in app_str: + print("Writing other tensors...") + rows, cols = tensor.shape + tensor_b = scipy.sparse.random(rows, 1, data_rvs=np.ones).toarray().flatten() + tensor_d = scipy.sparse.random(cols, 1, data_rvs=np.ones).toarray().flatten() + + if other_nonempty: + tensor_b[0] = 1 + tensor_d[0] = 1 + + tensors.insert(0, tensor_b) + tensors.append(tensor_d) + elif "mat_vecmul" in app_str: - pass + print("Writing other tensors...") + tensorName = args.input_tensor + # c(j) use mode1 + + # variant = "mode1" + # path = constructOtherVecKey(tensorName,variant) + # tensor_c_from_path = FrosttTensor(path) + # tensor_c = tensor_c_from_path.load().todense() + + # 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() + + if other_nonempty: + tensor_c[0] = 1 + + tensors.append(tensor_c) + + elif "tensor3_ttv" in app_str: + print("Writing other tensors...") + size_i, size_j, size_k = tensor.shape # i,j,k + tensor_c = scipy.sparse.random(size_k, 1, data_rvs=np.ones).toarray().flatten() + + if other_nonempty: + tensor_c[0] = 1 + + tensors.append(tensor_c) else: - tensor2 = scipy.sparse.random(tensor.shape[0], tensor.shape[1]) - tensors.append(tensor2) - # raise NotImplementedError + # tensor2 = scipy.sparse.random(tensor.shape[0], tensor.shape[1]) + # tensors.append(tensor2) + raise NotImplementedError return tensors -def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path): +def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path, higher_order=False): tensors = get_other_tensors(app_str, tensors[0]) names, format_permutations, ivars = parse_sam_input(args.cotile) + print("cotile_multilevel_coo tensors: ", names, "\n", tensors) + + # import pdb + # pdb.set_trace() + sizes_dict = {} for i, name in enumerate(names): tensor = tensors[i] @@ -237,7 +441,8 @@ def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path): if cotiled is None: # First iteration of tiling - cotiled, cotiled_sizes = cotile_coo(names, tensors, format_permutations, ivars, split_map) + cotiled, cotiled_sizes = cotile_coo(names, tensors, format_permutations, ivars, split_map, + higher_order) else: # recursively tile the blocks new_cotiled = {} @@ -247,9 +452,13 @@ def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path): new_cotiled[name] = {} new_cotiled_sizes[name] = {} for tile_id, tile in cotiled[name].items(): - new_cotiled_temp, new_cotiled_sizes_temp = cotile_coo(name, [tile.tocoo()], + if higher_order: + tile_in_coo = tile.to_coo() + else: + tile_in_coo = tile.tocoo() + new_cotiled_temp, new_cotiled_sizes_temp = cotile_coo(name, [tile_in_coo], [format_permutations[i]], [ivars[i]], - split_map) + split_map, higher_order) for kk, vv in copy.deepcopy(new_cotiled_temp)[name].items(): new_tile_id = tuple(list(tile_id) + list(kk)) @@ -269,34 +478,77 @@ def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path): print(exc) -inputCache = InputCacheSuiteSparse() +inputCacheSuiteSparse = InputCacheSuiteSparse() +inputCacheTensor = InputCacheTensor() if __name__ == "__main__": - parser = argparse.ArgumentParser(description='Tile matrices') - parser.add_argument("--input_tensor", type=str, default=None) - parser.add_argument("--gen_tensor", action="store_true") - parser.add_argument("--cotile", type=str, default=None) - parser.add_argument("--output_dir_path", type=str, default="./tiles") - parser.add_argument("--hw_config", type=str, default=None) - parser.add_argument("--multilevel", action="store_true") - parser.add_argument("--input_path", type=str, default=None) - parser.add_argument("--extensor", action="store_true") + parser = argparse.ArgumentParser(description='script that tiles tensors') + parser.add_argument("--tensor_type", choices=['ex', 'gen', 'file', 'ss', 'frostt'], help='The \ + tiles, tile_sizes = tile_coo(tensor, ivar_map, split_map) \ + type of tensor to tile: extensor(ex), generated (gen), \ + SuiteSparse (ss), FROSTT (frostt), or input file (file)') + parser.add_argument("--higher_order", action="store_true", help="If \ + true then we want to process a higher-order tensor. With higher-order set to true, if \ + 'tensor_type' is: \ + \n 'gen' then a 3-tensor is generated instead of matrix. \ + \n 'file' then a .tns file is read instead of a .mtx file. \ + \n 'ss' then other matrices used with SuiteSparse are .tns instead of .mtx files. \ + \n 'frostt' should always have 'higher_order' set as true.") + + parser.add_argument("--input_tensor", type=str, default=None, + help="Input tensor NAME if tensor_type is set to 'file'. \ + This is for use with SuiteSparse or FROSTT") + parser.add_argument("--input_path", type=str, default=None, help="Input tensor path") + parser.add_argument("--output_dir_path", type=str, default="./tiles", + help='Output path, directory where tiles get written to') + parser.add_argument("--hw_config", type=str, default=None, + help='Path to the hardware config yaml') + + parser.add_argument("--cotile", type=str, default=None, help='If \ + this is true cotile multiple tensors, else tile one tensor only') + parser.add_argument("--multilevel", action="store_true", help='If \ + multilevel is true there will exist more than one level of tiles, \ + else only tile once') + parser.add_argument("--seed", type=int, default=0, help="Random seed") + parser.add_argument("--other_nonempty", action="store_true", + help="If this is enabled, the 'other' tensors will have at least one nonzero value") args = parser.parse_args() + random.seed(args.seed) + np.random.seed(args.seed) + tensor = None cwd = os.getcwd() - if args.gen_tensor: - tensor = scipy.sparse.random(16, 16) - elif args.extensor: + inputCache = None + + if args.tensor_type == "gen": + if args.higher_order: + tensor = sparse.COO(sparse.random((16, 16, 16))) + else: + tensor = scipy.sparse.random(16, 16) + elif args.tensor_type == "ex": tensor = scipy.io.mmread(args.input_path) - else: + elif args.tensor_type == "ss": assert args.input_tensor is not None - SS_PATH = os.getenv('SUITESPARSE_PATH', default=os.path.join(cwd, 'suitesparse')) - # print("PATH:", SS_PATH) - tensor_path = os.path.join(SS_PATH, args.input_tensor + ".mtx") + + inputCache = inputCacheSuiteSparse + tensor_path = os.path.join(SUITESPARSE_PATH, args.input_tensor + ".mtx") ss_tensor = SuiteSparseTensor(tensor_path) tensor = inputCache.load(ss_tensor, False) + elif args.tensor_type == "frostt": + assert args.input_tensor is not None + assert args.higher_order + + inputCache = inputCacheTensor + tensor_path = os.path.join(FROSTT_PATH, args.input_tensor + ".tns") + + # FIXME: This is broken + frostt_tensor = FrosttTensor(tensor_path) + tensor = inputCache.load(frostt_tensor, False) + + else: + raise ValueError("This choice of 'tensor_type' is unreachable") split_map = {"i": 16, "j": 16, "k": 16} @@ -308,7 +560,6 @@ def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path): print("TILES:") print_dict(tiles) else: - output_mtx_name = os.path.join(args.output_dir_path, args.cotile, "mtx") output_mtx_path = Path(output_mtx_name) output_mtx_path.mkdir(parents=True, exist_ok=True) @@ -317,21 +568,43 @@ def cotile_multilevel_coo(app_str, hw_config_fname, tensors, output_dir_path): if args.multilevel: assert args.cotile is not None cotiled_tensors = cotile_multilevel_coo(args.cotile, args.hw_config, [tensor], - os.path.join(args.output_dir_path, args.cotile)) + os.path.join(args.output_dir_path, + args.cotile), + args.higher_order) elif args.cotile is not None: tensor2 = scipy.sparse.random(tensor.shape[0], tensor.shape[1]) names, format_permutations, ivars = parse_sam_input(args.cotile) - cotiled_tensors = cotile_coo(names, [tensor, tensor2], format_permutations, ivars, split_map) + cotiled_tensors = cotile_coo(names, [tensor, tensor2], + format_permutations, ivars, split_map, args.higher_order) # print(cotiled_tensors) names = cotiled_tensors.keys() for name in names: for tile_id, tile in cotiled_tensors[name].items(): [str(item) for item in tile_id] - filename = "tensor_" + name + "_tile_" + "_".join([str(item) for item in tile_id]) + ".mtx" + filename = "tensor_" + name + "_tile_" + "_".join([str(item) for item in tile_id]) + # filename += ".tns" if args.higher_order else ".mtx" + filename += ".mtx" mtx_path_name = os.path.join(output_mtx_name, filename) print(tile) - print(mtx_path_name, cwd) - scipy.io.mmwrite(mtx_path_name, tile) - print(os.path.exists(mtx_path_name)) + print("Output path:", mtx_path_name) + + if args.higher_order: + if args.tensor_type == "frostt": + tns_dumper = PydataSparseTensorDumper() + print(tile.shape) + print(tile) + tns_dumper.dump(tile, mtx_path_name) + # FIXME: (owhsu) Why did avb03 add this in? + elif len(tile.shape) == 1: + real_shape = tile.shape[0] + # print(np.array(tile.todense()).reshape(1,-1)) + # scipy.io.mmwrite(mtx_path_name, scipy.sparse.coo_matrix(tile.todense()).reshape((real_shape,1))) + scipy.io.mmwrite(mtx_path_name, scipy.sparse.coo_matrix(tile.todense())) + else: + # print(tile.todense()) + scipy.io.mmwrite(mtx_path_name, scipy.sparse.coo_matrix(tile.todense())) + + else: + scipy.io.mmwrite(mtx_path_name, tile) diff --git a/sam/util.py b/sam/util.py index b023147d..ae70dee1 100644 --- a/sam/util.py +++ b/sam/util.py @@ -1,24 +1,22 @@ -import scipy.sparse -import scipy.io -import os import glob -import numpy import itertools -import shutil -import numpy as np import math import sparse - from pathlib import Path +import os +import shutil from dataclasses import dataclass +from pathlib import Path -import os -import math import numpy +import numpy as np +import scipy.io +import scipy.sparse +import sparse # All environment variables for SAM should live here or in make file cwd = os.getcwd() -SAM_HOME = os.getenv('HOSTNAME', default=cwd) +SAM_HOME = os.getenv('SAM_HOME', default=cwd) HOSTNAME = os.getenv('HOSTNAME', default="local") SUITESPARSE_PATH = os.getenv('SUITESPARSE_PATH', default=os.path.join(SAM_HOME, "data", "suitesparse")) SUITESPARSE_FORMATTED_PATH = os.getenv('SUITESPARSE_FORMATTED_PATH', default=os.path.join(SAM_HOME, "data", @@ -39,6 +37,13 @@ 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. @@ -141,23 +146,26 @@ def load(self, path): assert False -# PydataSparseTensorLoader loads a sparse tensor from a file into -# a sparse tensor. -# class PydataSparseTensorLoader: -# def __init__(self): -# self.loader = TnsFileLoader() -# -# def load(self, path): -# dims, coords, values = self.loader.load(path) -# return sparse.COO(coords, values, tuple(dims)) -# -# # PydataSparseTensorDumper dumps a sparse tensor to a the desired file. -# class PydataSparseTensorDumper: -# def __init__(self): -# self.dumper = TnsFileDumper() -# -# def dump(self, tensor, path): -# self.dumper.dump_dict_to_file(tensor.shape, sparse.DOK(tensor).data, path) +# a pydata.sparse tensor. +class PydataSparseTensorLoader: + def __init__(self): + self.loader = TnsFileLoader() + + def load(self, path): + dims, coords, values = self.loader.load(path) + return sparse.COO(coords, values, tuple(dims)) + + +# PydataSparseTensorDumper dumps a sparse tensor to a the desired file. +class PydataSparseTensorDumper: + def __init__(self): + self.dumper = TnsFileDumper() + + def dump(self, tensor, path): + assert isinstance(tensor, sparse.DOK), "The tensor needs to be a pydata/sparse DOK format" + self.dumper.dump_dict_to_file(tensor.shape, tensor.data, path) + + # # # @@ -207,12 +215,13 @@ def shiftLastMode(self, tensor): @dataclass class DoublyCompressedMatrix: - shape: (int) - seg0: [int] - crd0: [int] - seg1: [int] - crd1: [int] - data: [float] + # shape: (int) + shape = [int] + seg0 = [int] + crd0 = [int] + seg1 = [int] + crd1 = [int] + data = [float] # ScipyMatrixMarketTensorLoader loads tensors in the matrix market format @@ -533,7 +542,7 @@ def __init__(self): self.lastName = None self.tensor = None - def load(self, tensor, suiteSparse, cast, format_str): + def load(self, tensor, cast): if self.lastName == str(tensor): return self.tensor else: @@ -546,6 +555,20 @@ def load(self, tensor, suiteSparse, cast, format_str): return self.tensor +# FrosttTensor represents a tensor in the FROSTT dataset. +class FrosttTensor: + def __init__(self, path): + self.path = path + self.__name__ = self.__str__() + + def __str__(self): + f = os.path.split(self.path)[1] + return f.replace(".tns", "") + + def load(self): + return PydataSparseTensorLoader().load(self.path) + + # PydataMatrixMarketTensorLoader loads tensors in the matrix market format # into sparse matrices. # class PydataMatrixMarketTensorLoader: @@ -602,3 +625,54 @@ def safeCastPydataTensorToInts(tensor): # data[i] = int(tensor.data[i]) data[i] = round_sparse(tensor.data[i]) return sparse.COO(tensor.coords, data, tensor.shape) + + +def parse_taco_format(infilename, outdir, tensorname, format_str): + with open(infilename, 'r') as inf: + level = -1 + count = 0 + seg = True + level_done = False + for line in inf: + if count == 0: + dim_start = line.find('(') + 1 + dim_end = line.find(')') + dims = line[dim_start: dim_end] + dims = dims.split('x') + + shapefile = os.path.join(outdir, tensorname + '_shape.txt') + with open(shapefile, 'w+') as shapef: + shapef.write(array_newline_str(dims)) + else: + if line.find(':') > -1: + level += 1 + seg = True + level_done = False + else: + start = line.find('[') + 1 + end = line.find(']') + line = line[start: end] + line = line.split(', ') + + if level_done: + # This is a values array + valfile = os.path.join(outdir, tensorname + '_vals.txt') + with open(valfile, 'w+') as valf: + valf.write(array_newline_str(line)) + else: + level_format = format_str[level] + if level_format == 's': + if seg: + segfile = os.path.join(outdir, tensorname + str(level) + + '_seg.txt') + with open(segfile, 'w+') as segf: + segf.write(array_newline_str(line)) + seg = False + else: + crdfile = os.path.join(outdir, tensorname + str(level) + + '_crd.txt') + with open(crdfile, 'w+') as crdf: + crdf.write(array_newline_str(line)) + level_done = True + + count += 1 diff --git a/scripts/datastructure_suitesparse.py b/scripts/datastructure_suitesparse.py new file mode 100644 index 00000000..cb24ec39 --- /dev/null +++ b/scripts/datastructure_suitesparse.py @@ -0,0 +1,281 @@ +import argparse +import os +import shutil +import scipy.sparse +import numpy as np + +from pathlib import Path + +from 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"] +scipy_formats = ["coo", "csr", "csc"] + + +def write_datastructure_tiles(args, tensor, out_path, tile_name): + print("Writing " + args.name + " for test " + args.benchname + "...") + + dirname = args.output_dir_path if args.output_dir_path is not None else os.path.join(out_path, args.name, args.benchname) + dirname = os.path.join(dirname, tile_name) + dirpath = Path(dirname) + if os.path.exists(dirpath): + shutil.rmtree(dirpath) + dirpath.mkdir(parents=True, exist_ok=True, mode=0o777) + + print(tile_name) + tensorname = tile_name.split("_")[1] + + coo = inputCache.load(tensor, False) + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss01", hw=False) + + +def write_datastructure_bench(args, tensor, out_path, tiles=None): + shifter = ScipyTensorShifter() + + print("Writing " + args.name + " for test " + args.benchname + "...") + + dirname = args.output_dir_path if args.output_dir_path is not None else os.path.join(out_path, args.name, args.benchname) + if tiles is not None: + dirname = os.path.join(dirname, tiles) + dirpath = Path(dirname) + if os.path.exists(dirpath): + shutil.rmtree(dirpath) + dirpath.mkdir(parents=True, exist_ok=True, mode=0o777) + + if "mat_mattransmul" in args.benchname or "mat_residual" in args.benchname: + tensorname = "C" + else: + tensorname = "B" + + coo = inputCache.load(tensor, False) + shape = coo.shape + + # These benchmarks need format_str == "ss10" + if args.benchname not in ["matmul_kij", "matmul_kji", "matmul_jki", "mat_vecmul", "mat_vecmul_ji", "mat_mattransmul"]: + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss01") + + if "matmul_ijk" in args.benchname: + shifted = shifter.shiftLastMode(coo) + + print("Writing " + args.name + " shifted and transposed...") + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate_sparse_only(trans_shifted, dirname, tensorname, format_str="ss10") + + elif "matmul_jik" in args.benchname: + shifted = shifter.shiftLastMode(coo) + + print("Writing " + args.name + " shifted and transposed...") + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate_sparse_only(trans_shifted, dirname, tensorname, format_str="ss10") + elif "matmul_ikj" in args.benchname: + shifted = shifter.shiftLastMode(coo) + + print("Writing " + args.name + " shifted and transposed...") + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate_sparse_only(trans_shifted, dirname, tensorname, format_str="ss01") + + elif "matmul_jki" in args.benchname: + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + + shifted = shifter.shiftLastMode(coo) + + print("Writing " + args.name + " shifted and transposed...") + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate_sparse_only(trans_shifted, dirname, tensorname, format_str="ss10") + + elif "matmul_kij" in args.benchname: + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + + shifted = shifter.shiftLastMode(coo) + + print("Writing " + args.name + " shifted and transposed...") + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate_sparse_only(trans_shifted, dirname, tensorname, format_str="ss01") + + elif "matmul_kji" in args.benchname: + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + + shifted = shifter.shiftLastMode(coo) + + print("Writing " + args.name + " shifted and transposed...") + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate_sparse_only(trans_shifted, dirname, tensorname, format_str="ss01") + + elif "mat_elemadd3" in args.benchname: + print("Writing " + args.name + " shifted...") + tensorname = "C" + shifted = shifter.shiftLastMode(coo) + formatWriter.writeout_separate_sparse_only(shifted, dirname, tensorname, format_str="ss01") + + print("Writing " + args.name + " shifted2...") + tensorname = "D" + shifted2 = shifter.shiftLastMode(shifted) + formatWriter.writeout_separate_sparse_only(shifted2, dirname, tensorname, format_str="ss01") + + elif "mat_elemadd" in args.benchname or "mat_elemmul" in args.benchname: + print("Writing " + args.name + " shifted...") + tensorname = "C" + shifted = shifter.shiftLastMode(coo) + formatWriter.writeout_separate_sparse_only(shifted, dirname, tensorname, format_str="ss01") + + elif "mat_mattransmul" in args.benchname: + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + if not args.no_gen_other: + tensorname = 'd' + vec = scipy.sparse.random(shape[0], 1, density=args.density, data_rvs=np.ones) + vec = vec.toarray().flatten() + formatWriter.writeout_separate_vec(vec, dirname, tensorname) + + tensorname = 'f' + vec = scipy.sparse.random(shape[1], 1, density=args.density, data_rvs=np.ones) + vec = vec.toarray().flatten() + formatWriter.writeout_separate_vec(vec, dirname, tensorname) + elif "mat_vecmul" == args.benchname or "mat_vecmul_ji" in args.benchname: + formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + if not args.no_gen_other: + tensorname = 'c' + vec = scipy.sparse.random(shape[1], 1, density=args.density, data_rvs=np.ones) + vec = vec.toarray().flatten() + formatWriter.writeout_separate_vec(vec, dirname, tensorname) + elif "mat_vecmul_ij" in args.benchname: + pass + elif "mat_sddmm" in args.benchname: + pass + elif "mat_residual" in args.benchname: + if not args.no_gen_other: + tensorname = 'b' + vec = scipy.sparse.random(shape[0], 1, density=args.density, data_rvs=np.ones) + vec = vec.toarray().flatten() + formatWriter.writeout_separate_vec(vec, dirname, tensorname) + + tensorname = 'd' + vec = scipy.sparse.random(shape[1], 1, density=args.density, data_rvs=np.ones) + vec = vec.toarray().flatten() + formatWriter.writeout_separate_vec(vec, dirname, tensorname) + elif "mat_identity" in args.benchname: + pass + else: + raise NotImplementedError + + +parser = argparse.ArgumentParser(description="Process some suitesparse matrices into per-level datastructures") +parser.add_argument('-n', '--name', metavar='ssname', type=str, action='store', help='tensor name to run format ' + 'conversion on one SS tensor') +parser.add_argument('-f', '--format', metavar='ssformat', type=str, action='store', help='The format that the tensor ' + 'should be converted to') +parser.add_argument('-comb', '--combined', action='store_true', default=False, help='Whether the formatted datastructures ' + 'should be in separate files') +parser.add_argument('-o', '--omit-dense', action='store_true', default=False, help='Do not create fully dense format') +parser.add_argument('-cast', '--cast', action='store_true', default=False, help='Safe sparsity cast to int for values') +parser.add_argument('-hw', '--hw', action='store_true', default=False, + help='Only generate formats used for hardware testing (all sparse' + 'levels, concordant)') +parser.add_argument('-b', '--benchname', type=str, default=None, help='test name to run format ' + 'conversion on') +parser.add_argument('--input_path', type=str, default=None) +parser.add_argument('--output_dir_path', type=str, default=None) +parser.add_argument('--tiles', action='store_true') +parser.add_argument('--no_gen_other', action='store_true', help="Whether this" + "script should generate the randmo 'other' tensors") +parser.add_argument('--seed', type=int, default=0, help='Random seed needed for gen_other') +parser.add_argument('--density', type=int, default=0.25, help='If gen_other, used for density of "other" tensor') +args = parser.parse_args() + +np.random.seed(args.seed) + +inputCache = InputCacheSuiteSparse() +formatWriter = FormatWriter(args.cast) + +cwd = os.getcwd() +if args.output_dir_path is None: + out_dirname = SUITESPARSE_FORMATTED_PATH +else: + out_dirname = args.output_dir_path + +out_path = Path(out_dirname) +out_path.mkdir(parents=True, exist_ok=True, mode=0o777) + +if args.name is None: + print("Please enter a matrix name") + exit() + +if args.input_path is None: + SS_PATH = os.getenv('SUITESPARSE_TENSOR_PATH', default=os.path.join(cwd, 'suitesparse')) + +else: + SS_PATH = args.input_path + +tensor = None +mtx_files = None +if args.tiles: + # get all mtx tile files from args.input_path + mtx_files = [os.path.join(args.input_path, fname) for fname in os.listdir(args.input_path) if fname.endswith(".mtx")] + + tensor = [SuiteSparseTensor(mtx_file) for mtx_file in mtx_files] +elif args.input_path is not None: + tensor = SuiteSparseTensor(args.input_path) +else: + print(SS_PATH) + tensor = SuiteSparseTensor(SS_PATH) + +if args.format is not None: + assert args.format in formats + filename = os.path.join(out_path, args.name + "_" + args.format + ".txt") + + coo = inputCache.load(tensor, False) + formatWriter.writeout(coo, args.format, filename) +elif args.combined: + for format_str in formats: + filename = os.path.join(out_path, args.name + "_" + format_str + ".txt") + print("Writing " + args.name + " " + format_str + "...") + + coo = inputCache.load(tensor, False) + formatWriter.writeout(coo, format_str, filename) + + shifted_filename = os.path.join(out_path, args.name + "_shifted_" + format_str + ".txt") + shifted = ScipyTensorShifter().shiftLastMode(coo) + formatWriter.writeout(shifted, format_str, shifted_filename) + + trans_filename = os.path.join(out_path, args.name + "_trans_shifted_" + format_str + ".txt") + trans_shifted = shifted.transpose() + formatWriter.writeout(trans_shifted, format_str, trans_filename) +elif args.hw: + if args.tiles and tensor is not None: + for i, ten in enumerate(tensor): + tile_name = os.path.split(mtx_files[i])[1].split(".")[0] + write_datastructure_tiles(args, ten, out_path, tile_name) + else: + write_datastructure_bench(args, tensor, out_path) + +else: + print("Writing " + args.name + " original...") + dirname = os.path.join(out_path, args.name, "orig") + dirpath = Path(dirname) + dirpath.mkdir(parents=True, exist_ok=True, mode=0o777) + tensorname = "B" + coo = inputCache.load(tensor, False) + formatWriter.writeout_separate(coo, dirname, tensorname, omit_dense=args.omit_dense) + + print("Writing " + args.name + " shifted...") + dirname = os.path.join(out_path, args.name, "shift") + dirpath = Path(dirname) + dirpath.mkdir(parents=True, exist_ok=True, mode=0o777) + tensorname = "C" + shifted = ScipyTensorShifter().shiftLastMode(coo) + formatWriter.writeout_separate(shifted, dirname, tensorname, omit_dense=args.omit_dense) + + print("Writing " + args.name + " shifted and transposed...") + dirname = os.path.join(out_path, args.name, "shift-trans") + dirpath = Path(dirname) + dirpath.mkdir(parents=True, exist_ok=True, mode=0o777) + tensorname = "C" + trans_shifted = shifted.transpose() + formatWriter.writeout_separate(trans_shifted, dirname, tensorname, omit_dense=args.omit_dense) diff --git a/scripts/datastructure_tns_old.py b/scripts/datastructure_tns_old.py new file mode 100644 index 00000000..863f1d0b --- /dev/null +++ b/scripts/datastructure_tns_old.py @@ -0,0 +1,127 @@ +import argparse +import os +from pathlib import Path +from util import parse_taco_format + +cwd = os.getcwd() + + +formats = ["sss012", "ss01", "dss", "dds", "ddd", "dsd", "sdd", "sds", "ssd"] + +parser = argparse.ArgumentParser(description="Process some Frostt tensors into per-level datastructures") +parser.add_argument('-n', '--name', metavar='fname', type=str, action='store', + help='tensor name to run format conversion on one frostt tensor') +parser.add_argument('-f', '--format', metavar='fformat', type=str, action='store', + help='The format that the tensor should be converted to') +parser.add_argument('-i', '--int', action='store_false', default=True, help='Safe sparsity cast to int for values') +parser.add_argument('-s', '--shift', action='store_false', default=True, help='Also format shifted tensor') +parser.add_argument('-o', '--other', action='store_true', default=False, help='Format other tensor') +parser.add_argument('-ss', '--suitesparse', action='store_true', default=False, help='Format suitesparse other tensor') +parser.add_argument('-hw', '--hw', action='store_true', default=False, + help='Format filenames as in AHA SCGRA _mode_') +parser.add_argument('-np', '--numpy', action='store_true', default=False, help='Format numpy tensors') +parser.add_argument('-b', '--bench', type=str, default=None, help='Name of benchmark') + +args = parser.parse_args() + +if args.other: + if args.suitesparse: + outdir_name = os.getenv('SUITESPARSE_FORMATTED_PATH', default=os.path.join(cwd, 'mode-formats')) + else: + outdir_name = os.getenv('FROSTT_FORMATTED_PATH', default=os.path.join(cwd, 'mode-formats')) + taco_format_dirname = os.getenv('TACO_TENSOR_PATH') + if taco_format_dirname is None: + print("Please set the TACO_TENSOR_PATH environment variable") + exit() + taco_format_dirname = os.path.join(taco_format_dirname, "other") + # taco_format_dirname = os.path.join(taco_format_dirname, "other-formatted-taco") +else: + outdir_name = os.getenv('FROSTT_FORMATTED_PATH', default=os.path.join(cwd, 'mode-formats')) + taco_format_dirname = os.getenv('FROSTT_FORMATTED_TACO_PATH') + if taco_format_dirname is None: + print("Please set the FROSTT_FORMATTED_TACO_PATH environment variable") + exit() + +out_path = Path(outdir_name) +out_path.mkdir(parents=True, exist_ok=True) + +print("args.name is ", args.name) + +if args.name is None: + print("Please enter a tensor name") + exit() + +print("\nhere after Please enter tensor name\n") + +if args.format is not None: + assert args.format in formats + 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 + os.path.isfile(os.path.join(taco_format_dirname, f)) and args.name in f] + + print("have otherfileNames\n") + print(os.listdir(outdir_name)) + print("length of otherfilenames is: ", len(otherfileNames), "\n") + + for otherfile in otherfileNames: + print("iterate thru otherfileNames\n") + taco_format_orig_filename = os.path.join(taco_format_dirname, otherfile) + # 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]) + outdir_orig_path = Path(outdir_other_name) + outdir_orig_path.mkdir(parents=True, exist_ok=True) + + name = None + if args.bench == "mat_residual": + if "mode0" in otherfile: + name = 'b' + elif "mode1" in otherfile: + name = 'd' + else: + raise NotImplementedError + elif args.bench == "mat_mattransmul": + if "mode0" in otherfile: + name = 'd' + elif "mode1" in otherfile: + name = 'f' + else: + raise NotImplementedError + elif "mat_vecmul" in args.bench: + if "mode1" in otherfile: + name = 'c' + elif "mode0" in otherfile: + continue + else: + raise NotImplementedError + else: + raise NotImplementedError + + assert name is not None, "Other tensor name was not set properly and is None" + parse_taco_format(taco_format_orig_filename, outdir_other_name, name, args.format, hw_filename=args.hw) + + else: + print("in else statement\n") + 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') + + # Original + outdir_orig_name = os.path.join(outdir_name, args.name, 'orig', args.format) + outdir_orig_path = Path(outdir_orig_name) + outdir_orig_path.mkdir(parents=True, exist_ok=True) + + print("parse taco format\n") + + parse_taco_format(taco_format_orig_filename, outdir_orig_name, 'B', args.format, hw_filename=args.hw) + + # Shifted + if args.shift: + outdir_shift_name = os.path.join(outdir_name, args.name, 'shift', args.format) + outdir_shift_path = Path(outdir_shift_name) + outdir_shift_path.mkdir(parents=True, exist_ok=True) + + parse_taco_format(taco_format_shift_filename, outdir_shift_name, 'C', args.format, hw_filename=args.hw) diff --git a/scripts/formatting/datastructure_suitesparse.py b/scripts/formatting/datastructure_suitesparse.py index 6e89d628..d2e586c2 100644 --- a/scripts/formatting/datastructure_suitesparse.py +++ b/scripts/formatting/datastructure_suitesparse.py @@ -6,7 +6,11 @@ from pathlib import Path -from scripts.util.util import FormatWriter, SuiteSparseTensor, InputCacheSuiteSparse +import sys +# 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"] @@ -29,6 +33,8 @@ def write_datastructure_tiles(args, tensor, out_path, tile_name): coo = inputCache.load(tensor, False) formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss01", hw=False) + # formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss01", hw=args.hw) + def write_datastructure_bench(args, tensor, out_path, tiles=None): @@ -37,6 +43,7 @@ def write_datastructure_bench(args, tensor, out_path, tiles=None): print("Writing " + args.name + " for test " + args.benchname + "...") dirname = args.output_dir_path if args.output_dir_path is not None else os.path.join(out_path, args.name, args.benchname) + print("dirname: " + dirname) if tiles is not None: dirname = os.path.join(dirname, tiles) dirpath = Path(dirname) @@ -128,7 +135,8 @@ def write_datastructure_bench(args, tensor, out_path, tiles=None): elif "mat_mattransmul" in args.benchname: formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") - if not args.no_gen_other: + # if not args.no_gen_other: + if False: tensorname = 'd' vec = scipy.sparse.random(shape[0], 1, density=args.density, data_rvs=np.ones) vec = vec.toarray().flatten() @@ -184,7 +192,7 @@ def write_datastructure_bench(args, tensor, out_path, tiles=None): parser.add_argument('--output_dir_path', type=str, default=None) parser.add_argument('--tiles', action='store_true') parser.add_argument('--no_gen_other', action='store_true', help="Whether this" - "script should generate the randmo 'other' tensors") + "script should generate the random 'other' tensors") parser.add_argument('--seed', type=int, default=0, help='Random seed needed for gen_other') parser.add_argument('--density', type=int, default=0.25, help='If gen_other, used for density of "other" tensor') args = parser.parse_args() @@ -217,7 +225,8 @@ def write_datastructure_bench(args, tensor, out_path, tiles=None): mtx_files = None if args.tiles: # get all mtx tile files from args.input_path - mtx_files = [os.path.join(args.input_path, fname) for fname in os.listdir(args.input_path) if fname.endswith(".mtx")] + # mtx_files = [os.path.join(args.input_path, fname) for fname in os.listdir(args.input_path) if fname.endswith(".mtx")] + mtx_files = [os.path.join(args.input_path, fname) for fname in os.listdir(args.input_path)] tensor = [SuiteSparseTensor(mtx_file) for mtx_file in mtx_files] elif args.input_path is not None: @@ -249,6 +258,7 @@ def write_datastructure_bench(args, tensor, out_path, tiles=None): formatWriter.writeout(trans_shifted, format_str, trans_filename) elif args.hw: if args.tiles and tensor is not None: + print("tensor lengths = ", len(tensor)) for i, ten in enumerate(tensor): tile_name = os.path.split(mtx_files[i])[1].split(".")[0] write_datastructure_tiles(args, ten, out_path, tile_name) diff --git a/scripts/formatting/datastructure_tns.py b/scripts/formatting/datastructure_tns.py index 7ff46056..9e5d743e 100644 --- a/scripts/formatting/datastructure_tns.py +++ b/scripts/formatting/datastructure_tns.py @@ -1,10 +1,21 @@ import argparse import os +import shutil +import scipy.sparse +import numpy as np +import sys +import random +import shutil + from pathlib import Path from scripts.util.util import parse_taco_format -cwd = os.getcwd() +from scripts.util.util import FormatWriter, SuiteSparseTensor, InputCacheSuiteSparse +# custom_path = '/nobackup/jadivara/sam/sam/util.py' +# sys.path.append(custom_path) +# from import SUITESPARSE_FORMATTED_PATH, ScipyTensorShifter +cwd = os.getcwd() formats = ["sss012", "ss01", "dss", "dds", "ddd", "dsd", "sdd", "sds", "ssd"] @@ -21,9 +32,10 @@ help='Format filenames as in AHA SCGRA _mode_') parser.add_argument('-np', '--numpy', action='store_true', default=False, help='Format numpy tensors') parser.add_argument('-b', '--bench', type=str, default=None, help='Name of benchmark') +parser.add_argument('--density', type=int, default=0.25, help='If gen_other, used for density of "other" tensor') +parser.add_argument('-cast', '--cast', action='store_true', default=False, help='Safe sparsity cast to int for values') args = parser.parse_args() - if args.other: if args.suitesparse: outdir_name = os.getenv('SUITESPARSE_FORMATTED_PATH', default=os.path.join(cwd, 'mode-formats')) @@ -44,70 +56,158 @@ out_path = Path(outdir_name) out_path.mkdir(parents=True, exist_ok=True) +formatWriter = FormatWriter(args.cast) + if args.name is None: print("Please enter a tensor name") exit() - +#breakpoint() if args.format is not None: assert args.format in formats levels = args.format[:-3] - if args.other: + + 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 + 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]) + outdir_orig_path = Path(outdir_other_name) + outdir_orig_path.mkdir(parents=True, exist_ok=True) + + name = None + taco_format_orig_filename = os.path.join(taco_format_dirname, args.name + "_" + levels + '.txt') + + inputCache = InputCacheSuiteSparse() - otherfileNames = [f for f in os.listdir(taco_format_dirname) if - os.path.isfile(os.path.join(taco_format_dirname, f)) and args.name in f] + if args.bench == "tensor3_ttv": + outdir_orig_name = os.path.join(outdir_name, args.name, args.bench, args.format) + outdir_orig_path = Path(outdir_orig_name) + outdir_orig_path.mkdir(parents=True, exist_ok=True) + + 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") + file_path_name = os.path.join(outdir_orig_name, "tensor_B_mode_shape") + file1 = open(file_path_name, 'r') + shape = [0]*3 + lines = file1.readlines() + count = 0 + + # Strips the newline character + for line in lines: + 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) + vec = vec.toarray().flatten() + tensor_out_path = os.path.join(out_path, args.name, args.bench, args.format) + formatWriter.writeout_separate_vec(vec, tensor_out_path, tensorname) + + # 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() + 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) + outdir_orig_path.mkdir(parents=True, exist_ok=True) - for otherfile in otherfileNames: - taco_format_orig_filename = os.path.join(taco_format_dirname, otherfile) - 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]) - outdir_orig_path = Path(outdir_other_name) + 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") + file_path_name = os.path.join(outdir_orig_name, "tensor_B_mode_shape") + file1 = open(file_path_name, 'r') + shape = [0]*3 + lines = file1.readlines() + count = 0 + + # Strips the newline character + for line in lines: + shape[count] = int(line) + count += 1 + # coo = inputCache.load(tensor, False) + dimension_k = random.randint(min(shape), 10) + dimension_l = shape[2] + dimension_j = shape[1] + + # formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + tensorname = 'C' + matrix = scipy.sparse.random(dimension_k, dimension_l, density=args.density, data_rvs=np.ones).toarray() + tensor_out_path = os.path.join(out_path, args.name, args.bench, args.format) + formatWriter.writeout_separate_sparse_only(matrix, tensor_out_path, tensorname) + + # 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() + 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) outdir_orig_path.mkdir(parents=True, exist_ok=True) - name = None - if args.bench == "mat_residual": - if "mode0" in otherfile: - name = 'b' - elif "mode1" in otherfile: - name = 'd' - else: - raise NotImplementedError - elif args.bench == "mat_mattransmul": - if "mode0" in otherfile: - name = 'd' - elif "mode1" in otherfile: - name = 'f' - else: - raise NotImplementedError - elif "mat_vecmul" in args.bench: - if "mode1" in otherfile: - name = 'c' - elif "mode0" in otherfile: - continue - else: - raise NotImplementedError - else: - raise NotImplementedError - - assert name is not None, "Other tensor name was not set properly and is None" - parse_taco_format(taco_format_orig_filename, outdir_other_name, name, args.format, hw_filename=args.hw) + 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 + lines = file1.readlines() + count = 0 + + # Strips the newline character + for line in lines: + shape[count] = int(line) + count += 1 + + dimension_i = shape[0] + dimension_k = shape[1] + dimension_l = shape[2] + dimension_j = random.randint(min(shape), 10) + + # formatWriter.writeout_separate_sparse_only(coo, dirname, tensorname, format_str="ss10") + tensorname = 'C' + matrix = scipy.sparse.random(dimension_j, dimension_k, density=args.density, data_rvs=np.ones).toarray() + tensor_out_path = os.path.join(out_path, args.name, args.bench, args.format) + formatWriter.writeout_separate_sparse_only(matrix, tensor_out_path, tensorname) + + tensorname = 'D' + matrix = scipy.sparse.random(dimension_j, dimension_l, density=args.density, data_rvs=np.ones).toarray() + tensor_out_path = os.path.join(out_path, args.name, args.bench, args.format) + formatWriter.writeout_separate_sparse_only(matrix, tensor_out_path, tensorname) + else: + raise NotImplementedError + + assert tensorname is not None, "Other tensor name was not set properly and is None" + # 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 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') # Original - outdir_orig_name = os.path.join(outdir_name, args.name, 'orig', args.format) + outdir_orig_name = os.path.join(outdir_name, args.name, args.bench, args.format) outdir_orig_path = Path(outdir_orig_name) outdir_orig_path.mkdir(parents=True, exist_ok=True) - parse_taco_format(taco_format_orig_filename, outdir_orig_name, 'B', args.format, hw_filename=args.hw) + 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) # Shifted if args.shift: - outdir_shift_name = os.path.join(outdir_name, args.name, 'shift', args.format) + outdir_shift_name = os.path.join(outdir_name, args.name, args.bench, args.format) outdir_shift_path = Path(outdir_shift_name) outdir_shift_path.mkdir(parents=True, exist_ok=True) - parse_taco_format(taco_format_shift_filename, outdir_shift_name, 'C', args.format, hw_filename=args.hw) + taco_format_shift_filename = "/home/avb03/sam/FROST_FORMATTED_TACO/" + args.name + "_shift_" + levels + '.txt' + parse_taco_format(taco_format_shift_filename, outdir_shift_name, 'C', args.format) diff --git a/scripts/formatting/generate_suitesparse_formats.sh b/scripts/formatting/generate_suitesparse_formats.sh index 3776800c..dedd9308 100755 --- a/scripts/formatting/generate_suitesparse_formats.sh +++ b/scripts/formatting/generate_suitesparse_formats.sh @@ -5,15 +5,17 @@ # Command: ./scripts/formatting/generate_suitesparse_formats.sh BENCHMARKS=( - matmul_ikj - matmul_ijk - matmul_kij - mat_elemmul - mat_elemadd - mat_elemadd3 +# matmul_ikj + matmul_ijk +# matmul_kij + mat_elemmul + mat_elemadd + mat_elemadd3 mat_residual mat_mattransmul - mat_identity + mat_vecmul +# mat_identity + mat_sddmm ) # This is a list of benchmarks that have "other" tensors that are generated @@ -34,10 +36,10 @@ for b in ${!BENCHMARKS[@]}; do sspath=${SUITESPARSE_PATH}/$name echo "Generating input format files for $name..." - SUITESPARSE_TENSOR_PATH=$sspath python $basedir/scripts/formatting/datastructure_suitesparse.py -n $name -hw -b $bench + SUITESPARSE_TENSOR_PATH=$sspath python3 $basedir/scripts/formatting/datastructure_suitesparse.py -n $name -hw -b $bench if [[ $OTHERBENCHES =~ "$bench" ]]; then echo "Generating format of 'other' tensor" - python $basedir/scripts/formatting/datastructure_tns.py -n $line -f ss01 --other -ss -b $bench -hw + python3 $basedir/scripts/formatting/datastructure_tns.py -n $line -f ss01 --other -ss -b $bench -hw fi done <$textfile diff --git a/scripts/generate_frostt_formats_onyx.sh b/scripts/generate_frostt_formats_onyx.sh new file mode 100755 index 00000000..9a84bde2 --- /dev/null +++ b/scripts/generate_frostt_formats_onyx.sh @@ -0,0 +1,59 @@ +#!/bin/bash +#SBATCH -N 1 +#SBATCH -t 360 + +# ./scripts/generate_frostt_formats_onyx.sh + +FORMATS=( + sss012 +) + +BENCHMARKS=( + #using all tensor apps except elemmul here** + # tensor3_elemadd + # tensor3_innerprod + tensor3_ttv + # tensor3_elemmul + # tensor3_mttkrp + # tensor3_ttm + # using tensor3_ttm +) + +# OTHERBENCHES='["tensor3_ttv"]' +# export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/ +# export FROSTT_PATH=/nobackup/owhsu/sparse-datasets/frostt/ +# export SUITESPARSE_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/suitesparse-formatted +# export FROSTT_FORMATTED_TACO_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted/taco-tensor +# export FROSTT_FORMATTED_PATH=/nobackup/owhsu/sparse-datasets/frostt-formatted + +export SUITESPARSE_PATH=/nobackup/owhsu/sparse-datasets/suitesparse/ +export FROSTT_PATH=/home/avb03/sparse-datasets/tensors +export SUITESPARSE_FORMATTED_PATH=/home/avb03/sam/SUITESPARSE_FORMATTED +export FROSTT_FORMATTED_TACO_PATH=/home/avb03/sam/FROST_FORMATTED_TACO +export FROSTT_FORMATTED_PATH=/home/avb03/sam/FROST_FORMATTED +export TACO_TENSOR_PATH=/home/avb03/sam/TACO_TENSOR + +basedir=$(pwd) + +for i in ${!FORMATS[@]}; do + format=${FORMATS[@]}; + echo "Generating files for format $format..." + + $basedir/compiler/taco/build/bin/taco-test sam.pack_$format + $basedir/compiler/taco/build/bin/taco-test sam.pack_other_frostt + for b in ${!BENCHMARKS[@]}; do + bench=${BENCHMARKS[$b]} + while read line; do + + name=$line + echo "Generating input format files for $name..." + python3 $basedir/scripts/formatting/datastructure_tns.py -n $name -f $format -b $bench -hw + python3 $basedir/scripts/formatting/datastructure_tns.py -n $name -f $format --other -b $bench -hw + # if [[ $OTHERBENCHES =~ "$bench" ]]; then + # echo "Generating format of 'other' tensor" + # python3 $basedir/scripts/datastructure_tns_old.py -n $line -f ss01 --other -ss -b $bench -hw + # fi + chmod -R 775 $FROSTT_FORMATTED_PATH + done <$1 + done +done diff --git a/scripts/prepare_tiles_onyx.sh b/scripts/prepare_tiles_onyx.sh new file mode 100755 index 00000000..7bea7baf --- /dev/null +++ b/scripts/prepare_tiles_onyx.sh @@ -0,0 +1,33 @@ +#!/bin/bash +#sbatch -n 1 +#sbatch --mem 120000 +#sbatch -p lanka-v3 +#sbatch --exclusive + + +basedir=$(pwd) +yaml_fname=memory_config_onyx.yaml +line=random_sparsity + +nnz=$1 +dim=$2 +echo "running for point nnz=$nnz and dimsize=$dim" + +export sam_home=$basedir +export tiled_suitesparse_formatted_path=${sam_home}/tiles/matmul_ikj/formatted +export tiled_output_path=${sam_home}/tiles/matmul_ikj/output/ + +pushd . + +mkdir extensor_mtx +cd extensor_mtx +python ../sam/onyx/synthetic/generate_fixed_nnz_mats.py --nnz $nnz --dim $dim +cd .. + +mkdir -p $path + +mkdir -p $basedir/tiles/ +rm -rf $basedir/tiles/* + +./scripts/prepare_files.sh extensor_${nnz}_${dim}.mtx $yaml_fname + diff --git a/scripts/run_cpu/suitesparse_runner.sh b/scripts/run_cpu/suitesparse_runner.sh index 3bcc864f..1e11e6ae 100755 --- a/scripts/run_cpu/suitesparse_runner.sh +++ b/scripts/run_cpu/suitesparse_runner.sh @@ -12,17 +12,18 @@ set -u cwd=$(pwd) sspath=$SUITESPARSE_PATH + # LANKA -if [ $2 -eq 1 ]; then - lanka=ON - neva=OFF -elif [ $2 -eq 2 ]; then - lanka=OFF - neva=ON -else - lanka=OFF - neva=OFF -fi + if [ $2 -eq 1 ]; then + lanka=ON + neva=OFF + elif [ $2 -eq 2 ]; then + lanka=OFF + neva=ON + else + lanka=OFF + neva=OFF + fi out=suitesparse-bench/taco @@ -31,9 +32,7 @@ mkdir -p "$out" while read line; do if [ $2 -eq 1 ]; then matrix="$sspath/$line/$line.mtx" - elif [ $2 -eq 2 ]; then - matrix="$sspath/$line.mtx" - else + else matrix="$sspath/$line.mtx" fi csvout="$out/result-$line.csv" diff --git a/scripts/suitesparse_memory_model_runner.sh b/scripts/suitesparse_memory_model_runner.sh new file mode 100755 index 00000000..e715a508 --- /dev/null +++ b/scripts/suitesparse_memory_model_runner.sh @@ -0,0 +1,44 @@ +#!/bin/bash +#SBATCH -N 1 +#SBATCH --mem 120000 +#SBATCH -p lanka-v3 +#SBATCH --exclusive + +benchout=memory_model_out + +basedir=$(pwd) +# bench=matmul_ijk_tile_pipeline_final +yaml_fname=memory_config_onyx.yaml +path=$basedir/$benchout + +fname=$1 + +appname=$2 + +echo "Running for suitesparse $fname" + +export SAM_HOME=$basedir +# export TILED_SUITESPARSE_FORMATTED_PATH=${SAM_HOME}/tiles/matmul_ijk/formatted +# export TILED_OUTPUT_PATH=${SAM_HOME}/tiles/matmul_ijk/output/ + +export TILED_SUITESPARSE_FORMATTED_PATH=${SAM_HOME}/tiles/${appname}/formatted +export TILED_OUTPUT_PATH=${SAM_HOME}/tiles/${appname}/output/ + +pushd . + +mkdir -p $path + +mkdir -p $basedir/tiles/ +rm -rf $basedir/tiles/* + +./scripts/tiling/prepare_files.sh $fname.mtx $yaml_fname $fname + +cd $basedir/sam/sim +# python3 -m pytest test/advanced-simulator/test_$bench.py --ssname $fname -s --check-gold --skip-empty --nbuffer --yaml_name=$yaml_fname --benchmark-json=$path/mem_model_$fname.json +# pytest test/advanced-simulator/test_$bench.py --ssname $fname -s --check-gold --skip-empty --nbuffer --yaml_name=$yaml_fname --benchmark-json=$path/mem_model_$fname.json + +# python3 $basedir/scripts/converter.py --json_name $path/mem_model_$fname.json + +python3 $basedir/scripts/util/bench_csv_aggregator.py $path $basedir/$benchout/$bench.csv + +popd diff --git a/scripts/tensor_names/spmv_iter_matrices.txt b/scripts/tensor_names/spmv_iter_matrices.txt new file mode 100644 index 00000000..f113329a --- /dev/null +++ b/scripts/tensor_names/spmv_iter_matrices.txt @@ -0,0 +1,9 @@ +bcsstm26 +tols2000 +west2021 +adder_dcop_30 +adder_trans_02 +watt_2 +rajat12 +G42 +G30 diff --git a/scripts/tiling/generate_gold_matmul_tiled.py b/scripts/tiling/generate_gold_matmul_tiled.py index 4fa3bfe3..4b585a19 100644 --- a/scripts/tiling/generate_gold_matmul_tiled.py +++ b/scripts/tiling/generate_gold_matmul_tiled.py @@ -11,10 +11,11 @@ from pathlib import Path from scripts.util.util import round_sparse +app_name = "mat_mattransmul" def generate_gold_matmul_tiled(tile_crd_b, tile_crd_c, dirname, out_format="ss01"): # CSR - formatted_dir = "./tiles/matmul_ikj/mtx" + formatted_dir = f"./tiles/{app_name}/mtx" B_dir = "tensor_B_tile_" for a in tile_crd_b: B_dir += str(a) + "_" @@ -58,6 +59,7 @@ def generate_gold_matmul_tiled(tile_crd_b, tile_crd_c, dirname, out_format="ss01 itr += 1 C_scipy = C_scipy.tocsc() gold_nd = (B_scipy @ C_scipy) + # gold_nd = B_scipy.dot(C_scipy) gold_out = gold_nd.tocoo() assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[2] scipy.io.mmwrite( @@ -69,9 +71,9 @@ def generate_gold_matmul_tiled(tile_crd_b, tile_crd_c, dirname, out_format="ss01 parser = argparse.ArgumentParser(description="Generate tiled output gold") parser.add_argument("--yaml_name", type=str, default="memory_config_onyx.yaml") args = parser.parse_args() - outdir = "./tiles/matmul_ikj/output/" + outdir = f"./tiles/{app_name}/output/" outpath = Path(outdir) - outpath.mkdir(parents=True, exist_ok=True) + outpath.mkdir(parents=True) with open("./tiles/matmul_ikj/tensor_sizes", "rb") as ff: sizes_dict_level_full = pickle.load(ff) @@ -79,10 +81,11 @@ def generate_gold_matmul_tiled(tile_crd_b, tile_crd_c, dirname, out_format="ss01 with open("./sam/sim/src/tiling/" + args.yaml_name, "r") as stream: loop_config = yaml.safe_load(stream) + print("sizes_dict_level_full", sizes_dict_level_full) struct = { "i00": 1 + int(sizes_dict_level_full["B"][0]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), - "k00": 1 + int(sizes_dict_level_full["B"][1]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), - "j00": 1 + int(sizes_dict_level_full["C"][1]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), + "k00": 1 + int(sizes_dict_level_full["c"][0]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), + "j00": 1 + int(sizes_dict_level_full["d"][0]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), "i0": loop_config["Glb_tile_size"], "k0": loop_config["Glb_tile_size"], "j0": loop_config["Glb_tile_size"]} print(struct) # quit() diff --git a/scripts/tiling/generate_gold_mattransmul.py b/scripts/tiling/generate_gold_mattransmul.py new file mode 100644 index 00000000..b044a949 --- /dev/null +++ b/scripts/tiling/generate_gold_mattransmul.py @@ -0,0 +1,174 @@ +import scipy +import scipy.sparse +import os +import scipy.io +import numpy as np +import yaml +import math +import pickle +import argparse + +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" + + B_dir = "tensor_B_tile_" + for a in tile_crd_b: + B_dir += str(a) + "_" + C_dir = "tensor_c_tile_" + for a in tile_crd_c: + C_dir += str(a) + "_" + d_dir = "tensor_d_tile_" + for a in tile_crd_d: + d_dir += str(a) + "_" + + B_dir = B_dir[0:-1] + ".mtx" + C_dir = C_dir[0:-1] + ".mtx" + d_dir = d_dir[0:-1] + ".mtx" + # print(B_dir, " ", C_dir) + B_filename = os.path.join(formatted_dir, B_dir) + C_filename = os.path.join(formatted_dir, C_dir) + d_filename = os.path.join(formatted_dir, d_dir) + # print() + # print(B_filename) + # print(C_filename) + # print(d_filename) + # print() + if os.path.exists(B_filename) and os.path.exists(C_filename) and os.path.exists(d_filename): + B_scipy = scipy.io.mmread(B_filename) + itr = 0 + # print("\nB_scipy: ", B_scipy) + for i, j, v in zip(B_scipy.row, B_scipy.col, B_scipy.data): + # print(B_scipy.data) + # print(i, " ", j, " ", v) + B_scipy.data[itr] = round_sparse(B_scipy.data[itr]) + # if B_scipy.data[itr] < 1 and B_scipy.data[itr] > 0: + # B_scipy.data[itr] = 1 + # elif B_scipy.data[itr] < 0 and B_scipy.data[itr] > -1: + # B_scipy.data[itr] = -1 + # else: + # B_scipy.data[itr] = int(B_scipy.data[itr]) + itr += 1 + B_scipy = B_scipy.tocsr() + + + C_scipy = scipy.io.mmread(C_filename) + # print(C_filename) + # print("\nC_scipy: ", C_scipy) + # print("___________________") + # print(B_scipy) + itr = 0 + for i, j, v in zip(C_scipy.row, C_scipy.col, C_scipy.data): + C_scipy.data[itr] = round_sparse(C_scipy.data[itr]) + itr += 1 + C_scipy = C_scipy.tocsr() + C_scipy = np.transpose(C_scipy) + + d_scipy = scipy.io.mmread(d_filename) + # print("\nd_scipy: ", d_scipy) + + itr = 0 + for i, j, v in zip(d_scipy.row, d_scipy.col, d_scipy.data): + d_scipy.data[itr] = round_sparse(d_scipy.data[itr]) + + itr += 1 + d_scipy = d_scipy.tocsr() + d_scipy = np.transpose(d_scipy) + + # gold_nd = (B_scipy @ C_scipy) + # gold_nd = B_scipy.dot(C_scipy) + + #constants + alpha = 2 + beta = 2 + + print("B_scipy.shape: ", B_scipy.shape) + print("C_scipy.shape: ", C_scipy.shape) + print("d_scipy.shape: ", d_scipy.shape) + + gold_nd = alpha*(B_scipy @ C_scipy) + beta * d_scipy + # print(gold_nd) + + gold_out = gold_nd.tocoo() + assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[1] and tile_crd_b[0] == tile_crd_d[0] and tile_crd_b[2] == tile_crd_d[1] + # assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[2] + scipy.io.mmwrite( + dirname + "out_" + str(tile_crd_b[0]) + "_" + str(tile_crd_b[1]) + "_" + str(tile_crd_b[3]) + "_" + str(tile_crd_b[2]) + "_" + str( + tile_crd_c[0]) + "_" + str(tile_crd_c[1]) + "_" + str(tile_crd_d[0]) + "_" + str(tile_crd_d[1]) + ".mtx", gold_out) + elif os.path.exists(d_filename): + d_scipy = scipy.io.mmread(d_filename) + # print("\nd_scipy: ", d_scipy) + + itr = 0 + for i, j, v in zip(d_scipy.row, d_scipy.col, d_scipy.data): + d_scipy.data[itr] = d_scipy.data[itr] + + itr += 1 + d_scipy = d_scipy.tocsr() + # d_scipy = np.transpose(d_scipy) + + # gold_nd = (B_scipy @ C_scipy) + # gold_nd = B_scipy.dot(C_scipy) + + #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): + print("output is all zero") + return + + gold_out = gold_nd.tocoo() + # assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[1] and tile_crd_b[0] == tile_crd_d[0] and tile_crd_b[2] == tile_crd_d[1] + # assert tile_crd_b[1] == tile_crd_c[0] and tile_crd_b[3] == tile_crd_c[2] + scipy.io.mmwrite( + dirname + "out_" + str(tile_crd_b[0]) + "_" + str(tile_crd_b[1]) + "_" + str(tile_crd_b[3]) + "_" + str(tile_crd_b[2]) + "_" + str( + 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") + args = parser.parse_args() + outdir = f"./tiles/mat_mattransmul/output/" + outpath = Path(outdir) + outpath.mkdir(parents=True) + + # generate_gold_matmul_tiled([0, 1, 2, 9], [1, 0, 9, 0], outdir) + + # generate_gold_matmul_tiled([0, 1, 0, 7], [1, 0, 7, 0], outdir) + # quit() with open("/nobackup/rsharma3/Sparsity/simulator/old_sam/sam/tiles/matmul_ikj/tensor_sizes", "rb") as ff: + + with open(f"./tiles/mat_mattransmul/tensor_sizes", "rb") as ff: + sizes_dict_level_full = pickle.load(ff) + + with open("./sam/sim/src/tiling/" + args.yaml_name, "r") as stream: + loop_config = yaml.safe_load(stream) + + print() + print("sizes_dict_level_full", sizes_dict_level_full) + print() + print("loop_config", loop_config) + + struct = { + "j00": 1 + int(sizes_dict_level_full["B"][0]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), + "i00": 1 + int(sizes_dict_level_full["c"][0]) // (loop_config["Glb_tile_size"] * loop_config["Mem_tile_size"]), + "i0": loop_config["Glb_tile_size"], "j0": loop_config["Glb_tile_size"]} + + print() + print(struct) + + # print(struct) + # # quit() + for i00 in range(struct["i00"]): + for j00 in range(struct["j00"]): + for i0 in range(struct["i0"]): + for j0 in range(struct["j0"]): + generate_gold_mattransmul_tiled([j00, i00, j0, i0], [i00, i0], [j00, j0], outdir) diff --git a/scripts/tiling/generate_sparsity_sweep_mem_model.sh b/scripts/tiling/generate_sparsity_sweep_mem_model.sh index 35bb6d8a..bdd7cdfc 100755 --- a/scripts/tiling/generate_sparsity_sweep_mem_model.sh +++ b/scripts/tiling/generate_sparsity_sweep_mem_model.sh @@ -4,7 +4,7 @@ SECONDS=0 mkdir extensor_mtx cd extensor_mtx -python ../sam/onyx/synthetic/generate_fixed_nnz_mats.py +python ../sam/onyx/synthetic/generate_fixed_nnz_mats.py --extensor cd .. ELAPSED="Elapsed: $(($SECONDS / 3600))hrs $((($SECONDS / 60) % 60))min $(($SECONDS % 60))sec" printf "$ELAPSED" diff --git a/scripts/tiling/prepare_files.sh b/scripts/tiling/prepare_files.sh index 5aece84f..d255f317 100755 --- a/scripts/tiling/prepare_files.sh +++ b/scripts/tiling/prepare_files.sh @@ -6,10 +6,12 @@ # ./scripts/tiling/prepare_files.sh extensor__.mtx +appname=$3 + basedir=$(pwd) rm -rf $basedir/tiles/* -./scripts/tiling/tile_ext.sh $1 memory_config_extensor_17M_llb.yaml +./scripts/tiling/tile_ext.sh $1 memory_config_extensor_17M_llb.yaml $appname -python scripts/tiling/generate_gold_matmul_tiled.py --yaml_name memory_config_extensor_17M_llb.yaml +# python3 scripts/tiling/generate_gold_matmul_tiled.py --yaml_name memory_config_extensor_17M_llb.yaml diff --git a/scripts/tiling/tile_ext.sh b/scripts/tiling/tile_ext.sh index 4548f466..082c1c38 100755 --- a/scripts/tiling/tile_ext.sh +++ b/scripts/tiling/tile_ext.sh @@ -4,9 +4,21 @@ # ./scripts/tiling/tile_ext.sh BENCHMARKS=( - matmul_ikj +# matmul_ikj + # mat_mattransmul + # mat_sddmm + # mat_vecmul_ij + # mat_vecmul_ij + # mat_residual + # mat_elemadd3 + # matmul_ijk + # mat_mask_tri + # mat_vecmul_iter + mat_elemadd ) +appname=$3 + sspath=$SUITESPARSE_PATH basedir=$(pwd) @@ -24,10 +36,13 @@ for b in ${!BENCHMARKS[@]}; do rm -rf $basedir/tiles/* echo "Tiling mtx file" - python $basedir/sam/sim/src/tiling/tile.py --extensor --input_path $tiles_path --cotile $bench --multilevel --hw_config $basedir/sam/sim/src/tiling/$2 + # python $basedir/sam/sim/src/tiling/tile.py --extensor --input_path $tiles_path --cotile $bench --multilevel --hw_config $basedir/sam/sim/src/tiling/$2 + python3 ./sam/sim/src/tiling/tile.py --tensor_type ss --input_tensor $appname --cotile $bench --multilevel --hw_config ./sam/sim/src/tiling/memory_config_onyx.yaml --higher_order echo "Generating input format files for $tiles_path..." - python $basedir/scripts/formatting/datastructure_suitesparse.py -n temp -hw -b $bench --input $basedir/tiles/$bench/mtx/ --output_dir_path $basedir/tiles/$bench/formatted --tiles + python3 $basedir/scripts/formatting/datastructure_suitesparse.py -n temp -hw -b $bench --input $basedir/tiles/$bench/mtx/ --output_dir_path $basedir/tiles/$bench/formatted --tiles + # $basedir/compiler/taco/build/bin/taco-test sam.pack_ss01 + # python3 $basedir/scripts/formatting/datastructure_tns.py -n rel5 -f ss01 -b $bench -hw done diff --git a/scripts/util/bench_csv_aggregator.py b/scripts/util/bench_csv_aggregator.py index 58a6f1f1..c24b7789 100644 --- a/scripts/util/bench_csv_aggregator.py +++ b/scripts/util/bench_csv_aggregator.py @@ -24,7 +24,7 @@ def aggregateTacoBenches(folder, outfile, taco=False, labelSet=None): # Discard the first 9 lines. This corresponds to the # google-benchmark generated header. if taco: - for i in range(0, 10): + for i in range(0, 9): f.readline() # Open the rest of the file as a CSV. reader = csv.reader(f) diff --git a/setup_tiling_mat.py b/setup_tiling_mat.py new file mode 100644 index 00000000..b153eec0 --- /dev/null +++ b/setup_tiling_mat.py @@ -0,0 +1,174 @@ +import subprocess +import glob +import shutil +import os +import re +import sys + +from sam.util import SUITESPARSE_PATH + +## PARAMS ###################################################################### + +# data = ['rajat12'] + +data = [sys.argv[2]] +tilesizes = [int(sys.argv[3])] +# app_name = "mat_elemadd" +# app_name = "mat_elemmul" +# app_name = "mat_sddmm" +# app_name = "matmul_ijk" +app_name = sys.argv[1] +# app_name = "mat_elemmul" +# app_name = "mat_residual" + +# data = [] +# tilesizes = [] +# sparsities = [60, 80, 90, 95, 98] +# for sparsity in sparsities: +# for i in range(5): +# data.append(f"matrix_sp{str(sparsity)}_sm_{i+1}") +# tilesizes.append(30) + +# data_file = open("onyx_final_eval_mid50_tensor_names.txt") +# data_file_lines = data_file.readlines() +# for line in data_file_lines: +# data.append(line[:-1]) + +# with open('matmul_tilesize_list.txt', 'r') as file: +# lines = file.readlines() + +# tilesizes = [int(line.strip()) for line in lines] +print("TILESIZES: ", tilesizes) +print("DATA: ", data) + +mode_to_exclude = 0 +addition_vector_name = "d" #mattransmul (d) and residual (b) only + +other_tensors = ["c"] +samples_directory = f"samples/{app_name}" +docker_path = f"avb03-sparse-tiling" +use_dataset_files = False + +############################################################################### + +def write_to_line(file_path, line_number, new_content): + with open(file_path, 'r') as file: + lines = file.readlines() + + if line_number > len(lines) or line_number < 1: + # Line number is out of range + return + + lines[line_number - 1] = new_content + '\n' + + 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() + + new_lines = [] + for line in lines: + values = line.split() + if len(values) >= 3: + values[2] = '0' + new_lines.append(' '.join(values)) + + with open(mtx_file, 'w') as file: + file.writelines(new_lines) + + +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) + + directories = glob.glob(f'tiles/{app_name}/formatted/tensor_[a-z]*') + + #for vectors, do cleanup + for directory in directories: + print(directory) + match = re.search(r'tensor_([a-z])', directory) + if match: + lowercase_letter = match.group(1) + + crd_file = os.path.join(directory, f"{lowercase_letter}{mode_to_exclude}_crd.txt") + seg_file = os.path.join(directory, f"{lowercase_letter}{mode_to_exclude}_seg.txt") + + # if os.path.exists(crd_file): + # os.remove(crd_file) + + # if os.path.exists(seg_file): + # os.remove(seg_file) + + samples_with_addition_vector = None + + # dense tile replacement for addition + if app_name == "mat_mattransmul" or app_name == "mat_residual": + # samples_with_addition_vector = glob.glob(f"{samples_directory}/*[{addition_vector_name}]*") + # samples_with_addition_vector = glob.glob(f"{samples_directory}/mtm_w_0_1/tensor_d_tile_0_0") + samples_with_addition_vector = glob.glob(f"{samples_directory}/mtm_w_0_1_BAK") + + + print(samples_with_addition_vector) + #fill in missing tiles with blanks + for sample in samples_with_addition_vector: + file_path = os.path.join(sample, f"{addition_vector_name}_vals.txt") + + with open(file_path, "r") as file: + file_contents = file.read() + + file_contents = file_contents.replace("1", "0") + + with open(file_path, "w") as file: + file.write(file_contents) + + tile_range = [(0,i) for i in range(8)] + [(1,i) for i in range(4)] + + for i,j in tile_range: + tile_dir = f"tiles/{app_name}/formatted/tensor_{addition_vector_name}_tile_{i}_{j}" + + if not os.path.exists(tile_dir): + # replace_ones_with_zeros("samples/mat_mattransmul/tensor_d_dense_mtx.mtx") + + # copy_over_to_mtx_dir = f"cp samples/mat_mattransmul/tensor_d_dense_gold_stash.mtx tiles/{app_name}/mtx/tensor_{addition_vector_name}_tile_{i}_{j}.mtx" + # os.system(copy_over_to_mtx_dir) + + sample_tile_dir = samples_with_addition_vector[0] + + if os.path.exists(sample_tile_dir): + shutil.copytree(sample_tile_dir, tile_dir) + + dump_gold_tiles = f"python3 scripts/tiling/generate_gold_mattransmul.py --yaml_name memory_config_extensor_17M_llb.yaml" + os.system(dump_gold_tiles) + + # os.makedirs("tiles_compiled", exist_ok=True) + # copy_rename = f"cp -r tiles/{app_name} tiles_compiled/{app_name}_{datum}" + # print(copy_rename) + # os.system(copy_rename) + + 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 new file mode 100644 index 00000000..15d73c98 --- /dev/null +++ b/setup_tiling_tensors.py @@ -0,0 +1,23 @@ +import numpy as np +import os +import glob +import shutil +from scripts.util.util import FormatWriter, InputCacheSuiteSparse + +#### PARAMS #### +tile = True +app_name = "tensor3_ttv" +vector_names = ['c'] +############## + +tiled_tensors = glob.glob(f"tiles/{app_name}/mtx/*.tns") +formatwriter = FormatWriter() +inputCache = InputCacheSuiteSparse() + +for tensor in tiled_tensors: + if any(x in tensor for x in vector_names): + #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 diff --git a/spmv_iter_matrices.txt b/spmv_iter_matrices.txt new file mode 100644 index 00000000..f113329a --- /dev/null +++ b/spmv_iter_matrices.txt @@ -0,0 +1,9 @@ +bcsstm26 +tols2000 +west2021 +adder_dcop_30 +adder_trans_02 +watt_2 +rajat12 +G42 +G30 diff --git a/spmv_sparsity_sweep.py b/spmv_sparsity_sweep.py new file mode 100644 index 00000000..8847e00b --- /dev/null +++ b/spmv_sparsity_sweep.py @@ -0,0 +1,42 @@ +import numpy as np +import scipy.io as sio +import scipy.sparse as sp +import os +import random + +num_rows = 10 +num_cols = 10 +density = 0.1 + +seed_value = 100 +random.seed(seed_value) +np.random.seed(seed_value) + +if not os.path.exists('spmv_sparsity_sweep'): + os.makedirs('spmv_sparsity_sweep') +else: + os.system("rm -rf spmv_sparsity_sweep/*") + +if not os.path.exists('spmv_sparsity_sweep/MAT_FILES'): + os.makedirs('spmv_sparsity_sweep/MAT_FILES') +else: + os.system("rm -rf spmv_sparsity_sweep/MAT_FILES/*") + os.makedirs('spmv_sparsity_sweep/MAT_FILES') + +if not os.path.exists('spmv_sparsity_sweep/MTX_FILES'): + os.makedirs('spmv_sparsity_sweep/MTX_FILES') +else: + os.system("rm -rf spmv_sparsity_sweep/MTX_FILES/*") + os.makedirs('spmv_sparsity_sweep/MTX_FILES') + +matrix = sp.random(num_rows, num_cols, density, data_rvs=np.ones, random_state=seed_value) +print(matrix) + +probability = 0.7 # Adjust this value to control the ratio of 1s to 0s in vector +vector = np.random.choice([0, 1], size=num_cols, p=[1 - probability, probability]) +print(vector) + +sio.mmwrite('spmv_sparsity_sweep/MTX_FILES/matrix.mtx', matrix) + +sio.savemat('spmv_sparsity_sweep/MAT_FILES/matrix.mat', {'matrix': matrix}) +sio.savemat('spmv_sparsity_sweep/MAT_FILES/vector.mat', {'vector': vector}) diff --git a/tile_pairing.py b/tile_pairing.py new file mode 100644 index 00000000..8af7000b --- /dev/null +++ b/tile_pairing.py @@ -0,0 +1,501 @@ +import shutil +import glob +import subprocess +import os +import json + +# test = "bcsstm26" +# test = "rel5" +test = "qiulp" +# test = "adder_dcop_30" +# test = "n4c6-b1" +# app_name = "mat_residual" +# app_name = "matmul_ijk" +# app_name = "matmul_ijk" +# app_name = "mat_mattrpython3ansmul" +app_name = "mat_elemmul" +const_val = 2 # only for mat_mattransmul + + +tiles_accumulation = {} + +b_tensors = glob.glob(f"/home/avb03/sam/tiles/{app_name}/formatted/tensor_B*") +c_tensors = glob.glob(f"/home/avb03/sam/tiles/{app_name}/formatted/tensor_C*") +d_tensors = glob.glob(f"/home/avb03/sam/tiles/{app_name}/formatted/tensor_D*") + +print("b_tensors: ", b_tensors) +print("c_tensors: ", c_tensors) +print("d_tensors: ", d_tensors) + +# b_tensors = glob.glob(f"/aha/garnet/tiles_{app_name}_{test}/formatted/tensor_B*") +# c_tensors = glob.glob(f"/aha/garnet/tiles_{app_name}_{test}/formatted/tensor_C*") + +b_vec_tensors = glob.glob(f"/aha/garnet/tiles_{app_name}_{test}/{app_name}/formatted/tensor_b*") +c_vec_tensors = glob.glob(f"/home/avb03/sam/tiles/{app_name}/formatted/tensor_c*") +print("c_vec_tensors: ", c_vec_tensors) +d_vec_tensors = glob.glob(f"/aha/garnet/tiles_{app_name}_{test}/{app_name}/formatted/tensor_d*") + +d_loc_paired = [] +b_loc_paired = [] + +if not os.path.exists("SPARSE_TESTS/MAT_TMP_DIR"): + os.makedirs("SPARSE_TESTS/MAT_TMP_DIR") + +os.system(f"rm -rf SPARSE_TESTS/{app_name}*") +os.system(f"rm -rf SPARSE_TESTS/MAT_TMP_DIR/tile*") + +tile = 0 + +os.chdir("SPARSE_TESTS") + +if app_name == "matmul_ijk": + for b in b_tensors: + for c in c_tensors: + tile_str = "tile" + str(tile) + b_loc = b[-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]): + print(b, c) + + if b_loc[2] not in tiles_accumulation: + tiles_accumulation[b_loc[2]] = [] + + tiles_accumulation[b_loc[2]].append(tile_str) + + 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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_vals") + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_shape") + + shutil.copy(f"{c}/C0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") + shutil.copy(f"{c}/C0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") + + shutil.copy(f"{c}/C1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_crd") + shutil.copy(f"{c}/C1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_seg") + + shutil.copy(f"{c}/C_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + shutil.copy(f"{c}/C_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + tile = tile + 1 +elif app_name == "mat_elemadd" or app_name == "mat_elemmul": + for b in b_tensors: + for c in c_tensors: + tile_str = "tile" + str(tile) + b_loc = b[-7:] + c_loc = c[-7:] + b_loc = b_loc.split("_") + c_loc = c_loc.split("_") + 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}") + 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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_vals") + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_shape") + + 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") + + shutil.copy(f"{c}/C1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") + shutil.copy(f"{c}/C1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") + + shutil.copy(f"{c}/C_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + shutil.copy(f"{c}/C_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + # subprocess.call(["aha", + # "regress", + # "fast"], + # text=True) + + # shutil.copy("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/output_gold.npy", "/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin") + # shutil.copytree("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin", f"/aha/garnet/SPARSE_TESTS/{tile_str}") + tile = tile + 1 + # print("we are on tile ", tile) +elif app_name == "mat_mattransmul": + for b in b_tensors: + for c in c_vec_tensors: + for d in d_vec_tensors: + tile_str = "tile" + str(tile) + b_loc = b[-7:] + c_loc = c[-3:] + d_loc = d[-3:] + + b_loc = b_loc.split("_") + 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]): + 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("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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + shutil.copy(f"{c}/c1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_crd") + shutil.copy(f"{c}/c1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_seg") + + shutil.copy(f"{c}/c0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_crd") + shutil.copy(f"{c}/c0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_seg") + + shutil.copy(f"{d}/d1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_1_crd") + shutil.copy(f"{d}/d1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_1_seg") + + shutil.copy(f"{d}/d0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_0_crd") + shutil.copy(f"{d}/d0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_0_seg") + + shutil.copy(f"{c}/c_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_vals") + shutil.copy(f"{c}/c_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_shape") + + shutil.copy(f"{d}/d_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_vals") + shutil.copy(f"{d}/d_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_shape") + + 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.' + 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("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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + # clear out C vals + with open(f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals", 'r+') as file: + contents = file.read() + contents = contents.replace(contents, str(0)) + file.seek(0) + file.write(contents) + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + shutil.copy(f"{c}/c1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_crd") + shutil.copy(f"{c}/c1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_seg") + + shutil.copy(f"{c}/c0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_crd") + shutil.copy(f"{c}/c0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_seg") + + shutil.copy(f"{d}/d1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_1_crd") + shutil.copy(f"{d}/d1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_1_seg") + + shutil.copy(f"{d}/d0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_0_crd") + shutil.copy(f"{d}/d0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_0_seg") + + shutil.copy(f"{c}/c_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_vals") + + # clear out d vals + with open(f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_vals", 'r+') as file: + contents = file.read() + contents = contents.replace(contents, str(0)) + file.seek(0) + file.write(contents) + + shutil.copy(f"{c}/c_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_shape") + + shutil.copy(f"{d}/d_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_vals") + shutil.copy(f"{d}/d_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_f_mode_shape") + + 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 + print("d_loc_paired: ", d_loc_paired) +elif app_name == "mat_vecmul_ij": + for b in b_tensors: + for c in c_vec_tensors: + tile_str = "tile" + str(tile) + b_loc = b[-7:] + c_loc = c[-3:] + + b_loc = b_loc.split("_") + 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 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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_vals") + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_shape") + + # shutil.copy(f"{c}/c1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_c_mode_1_crd") + # shutil.copy(f"{c}/c1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_c_mode_1_seg") + + 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") + + shutil.copy(f"{c}/c_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_c_mode_vals") + shutil.copy(f"{c}/c_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_c_mode_shape") + + tile = tile + 1 +elif app_name == "mat_residual": + for b in b_vec_tensors: + for c in c_tensors: + for d in d_vec_tensors: + tile_str = "tile" + str(tile) + b_loc = b[-3:] + c_loc = c[-7:] + d_loc = d[-3:] + + b_loc = b_loc.split("_") + 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(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") + + shutil.copy(f"{c}/C1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") + shutil.copy(f"{c}/C1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") + + shutil.copy(f"{c}/C_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + shutil.copy(f"{c}/C_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + shutil.copy(f"{b}/b1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_1_crd") + shutil.copy(f"{b}/b1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_1_seg") + + 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") + + shutil.copy(f"{d}/d1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_crd") + shutil.copy(f"{d}/d1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_seg") + + shutil.copy(f"{d}/d0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_crd") + shutil.copy(f"{d}/d0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_seg") + + shutil.copy(f"{b}/b_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_vals") + shutil.copy(f"{b}/b_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_shape") + + shutil.copy(f"{d}/d_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_vals") + shutil.copy(f"{d}/d_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_shape") + + 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") + + shutil.copy(f"{c}/C1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") + shutil.copy(f"{c}/C1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") + + shutil.copy(f"{c}/C_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + # clear out C vals + with open(f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals", 'r+') as file: + contents = file.read() + contents = contents.replace(contents, str(0)) + file.seek(0) + file.write(contents) + + shutil.copy(f"{c}/C_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + shutil.copy(f"{b}/b1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_1_crd") + shutil.copy(f"{b}/b1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_1_seg") + + 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") + + shutil.copy(f"{d}/d1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_crd") + shutil.copy(f"{d}/d1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_1_seg") + + shutil.copy(f"{d}/d0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_crd") + shutil.copy(f"{d}/d0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_0_seg") + + shutil.copy(f"{b}/b_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_vals") + shutil.copy(f"{b}/b_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_b_mode_shape") + + shutil.copy(f"{d}/d_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_vals") + shutil.copy(f"{d}/d_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_shape") + + # clear out d vals + with open(f"./MAT_TMP_DIR/{tile_str}/tensor_d_mode_vals", 'r+') as file: + contents = file.read() + contents = contents.replace(contents, str(0)) + file.seek(0) + file.write(contents) + + tile = tile + 1 + +elif app_name == "mat_sddmm": + for b in b_tensors: + for c in c_tensors: + for d in d_tensors: + tile_str = "tile" + str(tile) + + b_loc = b[-7:] + c_loc = c[-7:] + d_loc = d[-7:] + + b_loc = b_loc.split("_") + c_loc = c_loc.split("_") + 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]): + 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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_vals") + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_shape") + + shutil.copy(f"{c}/C0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") + shutil.copy(f"{c}/C0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") + + shutil.copy(f"{c}/C1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_crd") + shutil.copy(f"{c}/C1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_0_seg") + + shutil.copy(f"{c}/C_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + shutil.copy(f"{c}/C_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + tile = tile + 1 + +elif app_name == "mat_elemadd3": + for b in b_tensors: + for c in c_tensors: + for d in d_tensors: + tile_str = "tile" + str(tile) + b_loc = b[-7:] + c_loc = c[-7:] + b_loc = b_loc.split("_") + c_loc = c_loc.split("_") + d_loc = d[-7:] + d_loc = d_loc.split("_") + + # if(b_loc == c_loc and b_loc != d_loc): + # b_equal_c_no_d += 1 + # if(c_loc == d_loc and b_loc != c_loc): + # c_equal_d_no_b += 1 + # 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): + 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") + + shutil.copy(f"{b}/B1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_crd") + shutil.copy(f"{b}/B1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_1_seg") + + shutil.copy(f"{b}/B_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_vals") + + shutil.copy(f"{b}/B_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_B_mode_shape") + + 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") + + shutil.copy(f"{c}/C1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_crd") + shutil.copy(f"{c}/C1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_1_seg") + + shutil.copy(f"{c}/C_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_vals") + + shutil.copy(f"{c}/C_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_C_mode_shape") + + shutil.copy(f"{d}/D0_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_D_mode_0_crd") + shutil.copy(f"{d}/D0_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_D_mode_0_seg") + + shutil.copy(f"{d}/D1_crd.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_D_mode_1_crd") + shutil.copy(f"{d}/D1_seg.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_D_mode_1_seg") + + shutil.copy(f"{d}/D_vals.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_D_mode_vals") + + shutil.copy(f"{d}/D_shape.txt", f"./MAT_TMP_DIR/{tile_str}/tensor_D_mode_shape") + + # subprocess.call(["aha", + # "regress", + # "fast"], + # text=True) + + # shutil.copy("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/output_gold.npy", "/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin") + # shutil.copytree("/aha/garnet/SPARSE_TESTS/GLB_DIR/matmul_ijk_combined_seed_tile1/bin", f"/aha/garnet/SPARSE_TESTS/{tile_str}") + tile = tile + 1 + # print("we are on tile ", tile) + +print("tiles_accumulation: ", tiles_accumulation) + +with open("../tiles_accumulation.json", "w") as file: + json.dump(tiles_accumulation, file) + +print("there are ", tile, " tiles") \ No newline at end of file