From cb38fb21cc8f3a50edce64a0731f8a29f26e42dc Mon Sep 17 00:00:00 2001 From: Bo-Wun Cheng Date: Thu, 18 Apr 2024 15:15:11 -0700 Subject: [PATCH] complete support for dumping out dense matrices for hw --- scripts/util/util.py | 67 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/scripts/util/util.py b/scripts/util/util.py index 408ef553..e6a2f2c2 100644 --- a/scripts/util/util.py +++ b/scripts/util/util.py @@ -127,9 +127,10 @@ class NumpyNPYArrayLoader: def __init__(self): pass def load(self, path): - np_byte_array = numpy.load(path) - np_fp_array = convert_numpy_byte_array_to_fp_array(np_byte_array) - coo = scipy.sparse.coo_array(np_fp_array) + np_array = numpy.load(path) + if (np_array.dtype == numpy.dtype('S16')): + np_array = convert_numpy_byte_array_to_fp_array(np_array) + coo = scipy.sparse.coo_array(np_array) return coo def shape_str(shape): @@ -138,19 +139,11 @@ def shape_str(shape): # FIXME: This fixed point number of decimals may not be enough def array_str(array): - if isinstance(array[0], float): - return ' '.join([str(item) for item in array]) - # return ' '.join(['{:5.5f}'.format(item) for item in array]) - - return ' '.join([str(item) for item in array]) + return ' '.join([str(item) for _, item in numpy.ndenumerate(array)]) def array_newline_str(array): - if isinstance(array[0], float): - return '\n'.join([str(item) for item in array]) - # return '\n'.join(['{:5.5f}'.format(item) for item in array]) - - return '\n'.join([str(item) for item in array]) + return '\n'.join([str(item) for _, item in numpy.ndenumerate(array)]) # InputCacheSuiteSparse attempts to avoid reading the same tensor from disk multiple @@ -194,7 +187,7 @@ def convert_format(self, coo, format_str): if format_str == "dense": return coo.todense() elif format_str == "denseT": - return coo.todense().getT() + return numpy.transpose(coo.todense()) if format_str == "dcsr": csr = scipy.sparse.csr_matrix(coo) has_row = [rc > 0 for rc in csr.getnnz(1)] @@ -641,6 +634,52 @@ def writeout_separate_sparse_only(self, coo, dir_path, tensorname, format_str="s with open(filename, "w") as ofile: ofile.write(array_newline_str(dcsc.data)) + elif format_str == "dd01": + dense_dir = Path(dir_path) + dense_dir.mkdir(parents=True, exist_ok=True, mode=0o777) + + dense = self.convert_format(coo, "dense") + + if not hw: + filename = os.path.join(dense_dir, tensorname + "_shape.txt") + else: + filename = os.path.join(dir_path, "tensor_" + tensorname + "_mode_shape") + with open(filename, "w") as ofile: + ofile.write(array_newline_str(dense.shape)) + + if not hw: + filename = os.path.join(dense_dir, tensorname + "_vals.txt") + else: + filename = os.path.join(dir_path, "tensor_" + tensorname + "_mode_vals") + with open(filename, "w") as ofile: + ofile.write(array_newline_str(dense.data)) + + elif format_str == "dd10": + denseT_dir = Path(dir_path) + denseT_dir = denseT_dir.mkdir(parents=True, exist_ok=True, mode =0o777) + + # here, the ndarray will be transpose, so we can get the data in column major order + denseT = self.convert_format(coo, "denseT") + + if not hw: + filename = os.path.join(denseT_dir, tensorname + "_shape.txt") + else: + filename = os.path.join(dir_path, "tensor_" + tensorname + "_mode_shape") + with open(filename, "w") as ofile: + # the transposition in convert_format will change the shape of the matrix, + # need to transpose it back to get the correct shape here + ofile.write(array_newline_str(numpy.transpose(denseT).shape)) + + if not hw: + filename = os.path.join(denseT_dir, tensorname + "_vals.txt") + else: + filename = os.path.join(dir_path, "tensor_" + tensorname + "_mode_vals") + with open(filename, "w") as ofile: + ofile.write(array_newline_str(denseT.data)) + + + + # UfuncInputCache attempts to avoid reading the same tensor from disk multiple # times in a benchmark run.