Skip to content

Commit

Permalink
complete support for dumping out dense matrices for hw
Browse files Browse the repository at this point in the history
  • Loading branch information
bobcheng15 committed Apr 18, 2024
1 parent 11e6047 commit cb38fb2
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions scripts/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit cb38fb2

Please sign in to comment.