2024-12-12 17:36:15 -05:00
|
|
|
from data_stat import Stat, Format, MatrixType
|
2024-12-04 22:47:16 -05:00
|
|
|
|
2024-11-28 00:04:57 -05:00
|
|
|
import torch, scipy
|
|
|
|
import numpy as np
|
|
|
|
import argparse
|
|
|
|
import time
|
2024-12-02 23:32:33 -05:00
|
|
|
import json
|
2024-12-03 08:53:39 -05:00
|
|
|
import sys, os
|
2024-11-28 00:04:57 -05:00
|
|
|
|
2024-12-14 17:22:42 -05:00
|
|
|
device = 'cpu'
|
|
|
|
|
2024-11-28 00:04:57 -05:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-12-12 17:36:15 -05:00
|
|
|
parser.add_argument('matrix_type', type=str,
|
|
|
|
choices=[t.name.lower() for t in MatrixType],
|
|
|
|
help='the type of matrix')
|
2024-12-12 01:18:26 -05:00
|
|
|
parser.add_argument('format', type=str,
|
|
|
|
choices=[fmt.name.lower() for fmt in Format],
|
|
|
|
help='the sparse format to use')
|
2024-12-12 17:36:15 -05:00
|
|
|
parser.add_argument('iterations', type=int, help='the number of iterations of multiplication to perform')
|
|
|
|
parser.add_argument('-m', '--matrix_file', type=str,
|
|
|
|
help='the input matrix (.mtx) file')
|
2024-12-12 01:18:26 -05:00
|
|
|
parser.add_argument('-ss', '--synthetic_size', type=int,
|
|
|
|
help='the synthetic matrix parameters size (rows)')
|
|
|
|
parser.add_argument('-sd', '--synthetic_density', type=float,
|
2024-12-12 17:36:15 -05:00
|
|
|
help='the synthetic matrix density')
|
2024-12-14 17:22:42 -05:00
|
|
|
parser.add_argument('-c', '--cores', type=int,
|
|
|
|
help='the number of cores to use')
|
2024-11-28 00:04:57 -05:00
|
|
|
args = parser.parse_args()
|
2024-12-12 17:36:15 -05:00
|
|
|
args.matrix_type = MatrixType[args.matrix_type.upper()]
|
2024-12-12 01:18:26 -05:00
|
|
|
args.format = Format[args.format.upper()]
|
2024-11-28 00:04:57 -05:00
|
|
|
|
2024-12-14 17:22:42 -05:00
|
|
|
if args.cores is not None:
|
|
|
|
torch.set_num_threads(args.cores)
|
2024-11-28 00:04:57 -05:00
|
|
|
|
2024-12-12 17:36:15 -05:00
|
|
|
if args.matrix_type == MatrixType.SUITESPARSE:
|
|
|
|
if args.matrix_file is None:
|
|
|
|
exit("Matrix file not specified!")
|
|
|
|
|
2024-12-12 01:18:26 -05:00
|
|
|
matrix = scipy.io.mmread(args.matrix_file)
|
|
|
|
matrix = torch.sparse_coo_tensor(
|
|
|
|
np.vstack((matrix.row, matrix.col)),
|
|
|
|
matrix.data, matrix.shape,
|
|
|
|
device=device, dtype=torch.float32)
|
2024-12-12 17:36:15 -05:00
|
|
|
elif args.matrix_type == MatrixType.SYNTHETIC:
|
|
|
|
if args.synthetic_size is None and args.synthetic_density is None:
|
|
|
|
exit("Synthetic matrix parameters not specified!")
|
|
|
|
|
2024-12-15 15:04:09 -05:00
|
|
|
matrix = scipy.sparse.random(
|
|
|
|
args.synthetic_size, args.synthetic_size,
|
|
|
|
density=args.synthetic_density,
|
|
|
|
format='coo', dtype=np.float32,
|
|
|
|
random_state=np.random.default_rng())
|
|
|
|
indices = torch.tensor(np.vstack([matrix.row, matrix.col]),
|
|
|
|
dtype=torch.float32, device=device)
|
|
|
|
values = torch.tensor(matrix.data,
|
|
|
|
dtype=torch.float32, device=device)
|
2024-12-12 01:18:26 -05:00
|
|
|
matrix = torch.sparse_coo_tensor(
|
2024-12-15 15:04:09 -05:00
|
|
|
indices, values, size=matrix.shape,
|
|
|
|
dtype=torch.float32, device=device)
|
|
|
|
|
|
|
|
# nnz = int((args.synthetic_size ** 2) * args.synthetic_density)
|
|
|
|
# row_indices = torch.randint(0, args.synthetic_size, (nnz,))
|
|
|
|
# col_indices = torch.randint(0, args.synthetic_size, (nnz,))
|
|
|
|
# indices = torch.stack([row_indices, col_indices])
|
|
|
|
# values = torch.randn(nnz)
|
|
|
|
#
|
|
|
|
# matrix = torch.sparse_coo_tensor(
|
|
|
|
# indices, values,
|
|
|
|
# size=(args.synthetic_size, args.synthetic_size),
|
|
|
|
# device=device, dtype=torch.float32)
|
2024-12-12 01:18:26 -05:00
|
|
|
else:
|
2024-12-12 17:36:15 -05:00
|
|
|
exit("Unrecognized matrix type!")
|
2024-12-12 01:18:26 -05:00
|
|
|
|
|
|
|
if args.format == Format.CSR:
|
|
|
|
matrix = matrix.to_sparse_csr().type(torch.float32)
|
|
|
|
elif args.format == Format.COO:
|
|
|
|
pass
|
|
|
|
else:
|
2024-12-12 17:36:15 -05:00
|
|
|
exit("Unrecognized format!")
|
2024-11-28 00:04:57 -05:00
|
|
|
|
|
|
|
vector = torch.rand(matrix.shape[1], device=device)
|
|
|
|
|
2024-12-02 23:32:33 -05:00
|
|
|
print(matrix, file=sys.stderr)
|
|
|
|
print(vector, file=sys.stderr)
|
2024-11-28 00:04:57 -05:00
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
for i in range(0, args.iterations):
|
2024-12-15 15:04:09 -05:00
|
|
|
torch.mm(matrix, vector.unsqueeze(-1))
|
|
|
|
#torch.mv(matrix, vector)
|
2024-12-09 15:06:46 -05:00
|
|
|
#torch.sparse.mm(matrix, vector.unsqueeze(-1)).squeeze(-1)
|
|
|
|
#print(i)
|
2024-11-28 00:04:57 -05:00
|
|
|
end = time.time()
|
|
|
|
|
2024-12-02 23:32:33 -05:00
|
|
|
result = dict()
|
2024-11-28 00:04:57 -05:00
|
|
|
|
2024-12-12 17:36:15 -05:00
|
|
|
result[Stat.MATRIX_TYPE.name] = args.matrix_type.value
|
2024-12-14 18:19:33 -05:00
|
|
|
print(f"Matrix Type: {result[Stat.MATRIX_TYPE.name]}", file=sys.stderr)
|
2024-12-12 17:36:15 -05:00
|
|
|
|
|
|
|
if args.matrix_type == MatrixType.SUITESPARSE:
|
2024-12-12 01:18:26 -05:00
|
|
|
result[Stat.MATRIX_FILE.name] = os.path.splitext(os.path.basename(args.matrix_file))[0]
|
2024-12-12 17:36:15 -05:00
|
|
|
print(f"Matrix: {result[Stat.MATRIX_FILE.name]}", file=sys.stderr)
|
|
|
|
|
|
|
|
result[Stat.MATRIX_FORMAT.name] = args.format.value
|
2024-12-15 15:04:09 -05:00
|
|
|
print(f"Matrix Format: {result[Stat.MATRIX_FORMAT.name]}", file=sys.stderr)
|
2024-12-04 22:47:16 -05:00
|
|
|
|
2024-12-05 14:49:05 -05:00
|
|
|
result[Stat.MATRIX_SHAPE.name] = matrix.shape
|
|
|
|
print(f"Shape: {result[Stat.MATRIX_SHAPE.name]}", file=sys.stderr)
|
2024-12-03 08:53:39 -05:00
|
|
|
|
2024-12-14 18:19:33 -05:00
|
|
|
result[Stat.MATRIX_ROWS.name] = matrix.shape[0]
|
|
|
|
print(f"Rows: {result[Stat.MATRIX_ROWS.name]}", file=sys.stderr)
|
|
|
|
|
2024-12-05 14:49:05 -05:00
|
|
|
result[Stat.MATRIX_SIZE.name] = matrix.shape[0] * matrix.shape[1]
|
|
|
|
print(f"Size: {result[Stat.MATRIX_SIZE.name]}", file=sys.stderr)
|
2024-12-02 23:32:33 -05:00
|
|
|
|
2024-12-15 15:04:09 -05:00
|
|
|
if args.format == Format.CSR:
|
|
|
|
rows = matrix.values().shape[0]
|
|
|
|
elif args.format == Format.COO:
|
|
|
|
rows = matrix.coalesce().values().shape[0]
|
|
|
|
else:
|
|
|
|
exit("Unrecognized format!")
|
|
|
|
|
|
|
|
result[Stat.MATRIX_NNZ.name] = rows
|
2024-12-05 14:49:05 -05:00
|
|
|
print(f"NNZ: {result[Stat.MATRIX_NNZ.name]}", file=sys.stderr)
|
2024-12-02 23:32:33 -05:00
|
|
|
|
2024-12-15 15:04:09 -05:00
|
|
|
result[Stat.MATRIX_DENSITY.name] = rows / result[Stat.MATRIX_SIZE.name]
|
2024-12-05 14:49:05 -05:00
|
|
|
print(f"Density: {result[Stat.MATRIX_DENSITY.name]}", file=sys.stderr)
|
2024-12-02 23:32:33 -05:00
|
|
|
|
2024-12-11 22:47:11 -05:00
|
|
|
result[Stat.TIME_S.name] = end - start
|
|
|
|
print(f"Time: {result[Stat.TIME_S.name]} seconds", file=sys.stderr)
|
2024-12-02 23:32:33 -05:00
|
|
|
|
|
|
|
print(json.dumps(result))
|