ampere_research/pytorch/spmv.py

56 lines
1.7 KiB
Python
Raw Normal View History

2024-12-04 22:47:16 -05:00
from data_stat import Stat
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
parser = argparse.ArgumentParser()
parser.add_argument('matrix_file', help='the input matrix (.mtx) file')
parser.add_argument('iterations', type=int, help='the number of iterations of multiplication to perform')
args = parser.parse_args()
device = 'cpu'
matrix = scipy.io.mmread(args.matrix_file)
matrix = torch.sparse_coo_tensor(
np.vstack((matrix.row, matrix.col)),
matrix.data, matrix.shape,
device=device
).to_sparse_csr().type(torch.float)
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):
torch.sparse.mm(matrix, vector.unsqueeze(-1)).squeeze(-1)
#print(i)
end = time.time()
2024-12-02 23:32:33 -05:00
result = dict()
2024-11-28 00:04:57 -05:00
2024-12-05 14:49:05 -05:00
result[Stat.MATRIX_FILE.name] = os.path.splitext(os.path.basename(args.matrix_file))[0]
print(f"Matrix: {result[Stat.MATRIX_FILE.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-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-05 14:49:05 -05:00
result[Stat.MATRIX_NNZ.name] = matrix.values().shape[0]
print(f"NNZ: {result[Stat.MATRIX_NNZ.name]}", file=sys.stderr)
2024-12-02 23:32:33 -05:00
2024-12-05 14:49:05 -05:00
result[Stat.MATRIX_DENSITY.name] = matrix.values().shape[0] / (matrix.shape[0] * matrix.shape[1])
print(f"Density: {result[Stat.MATRIX_DENSITY.name]}", file=sys.stderr)
2024-12-02 23:32:33 -05:00
2024-12-05 14:49:05 -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))