Skip to content

Commit

Permalink
update with files
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuke Wang committed May 5, 2023
0 parents commit 6d72df1
Show file tree
Hide file tree
Showing 22 changed files with 1,418 additions and 0 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Pruning in Neural Network

## Improving the computation performances using sparse matrix multiplications

This repository contains all you need to repeat the experiments in the article : https://medium.com/@yanis.chaigneau/pruning-in-neural-networks-541af4f9a899

It is separated in three parts :

# The matrices multiplication with pytorch.sparse

To repeat this experiment, all you need to do is to change the code in the two files gpu_torch.py and plot_results.txt where you want to store the logs, you need as well to install AIPowerMeter with :

`git clone https://github.com/GreenAI-Uppa/AIPowerMeter`

in the same repository as pruningSPMM.

Then you can install the requirements.txt with pip in a virtual env :

`pip install -r requirements.txt`

and then

`python gpu_torch.py` to start the experiment

`python plot_results.py` to save the results aftewards as a plot on a file


# cuSPARSE :

This experiment compares the execution time of a sparse matrix multiplication between cuSparse with blocked-ELL format and cuBlas hGEMM.

To launch the experiment with Python, simply run the following script:

`python cusparse_test.py`

To compile the Cpp script, run:

g++ -Wall cusparse_blocked_ell_spmm.c -o ccusparse_blocked_ell_spmm.o -I $path_CUDA_include -L $path_CUDA_lib -lcudart -lcuda -lcusparse -lcublas -fopenmp -g -O2 -std=c++11

with
$path_CUDA_include the path to the include directory of your CUDA version (minimum 11.4) (traditionaly /usr/local/cuda-11.4/targets/x86_64-linux/include/ )
$path_CUDA_lib the path to the lib directory of your CUDA version (minimum 11.4) (traditionaly /usr/local/cuda-11.4/targets/x86_64-linux/lib )

Then run it with:

`./cusparse_blocked_ell_spmm.o $size_A $nb_rows_B $blocksize $number_blocks_per_row $number_repetitions`

Arguments:
size_A: number of rows and columns of A
nb_rows_B: Number of rows of the dense matrix
blocksize: The size of the individual blocks of the sparse matrix (square blocks)
number_blocks_per_row: Number blocks per row of the sparse matrix A. If 1, a block diagonal matrix is built.
number_repetitions: Number of time to compute the matrix multiplication. It can be useful for energy consumption measures.

# SparseLinear

Launch:

`python test_snip_sparse.py`

Analyze the results with:

`python test_results.py`
28 changes: 28 additions & 0 deletions cusparse/1_log2csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
import re
import sys

if len(sys.argv) < 2:
raise ValueError("Usage: ./1_log2csv.py result.log")

fp = open(sys.argv[1], "r")

dataset_li = []
time_li = []
for line in fp:
if "dataset=" in line:
data = line.split("=")[1].rstrip("\n").lstrip()
print(data)
dataset_li.append(data)
if " Bmp Device |" in line:
time = line.split("|")[1].rstrip("\n").lstrip()
print(time)
time_li.append(time.strip("ms"))
fp.close()

fout = open(sys.argv[1].strip(".log")+".csv", 'w')
fout.write("Dataset, Time (ms)\n")
for data, time in zip(dataset_li, time_li):
fout.write("{},{}\n".format(data, time))
fout.close()
print("\n\n=>Check [{}] for results\n\n".format(sys.argv[1].strip(".log")+".csv"))
24 changes: 24 additions & 0 deletions cusparse/1_run_bSpMM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
import os
os.environ["PYTHONWARNINGS"] = "ignore"
import numpy as np
from scipy import sparse, io
import subprocess

hidden = 16

dataset = [
( 'amazon0505' , 410236 , 22),
( 'artist' , 50515 , 12),
( 'com-amazon' , 548551 , 22),
( 'soc-BlogCatalog' , 88784 , 39),
( 'amazon0601' , 403394 , 22),
]

for data, node, _ in dataset:
print("dataset={}".format(data))
ntimes = node * node / (4096 * 4096)
result = subprocess.run(["python", "2_cusparse_test.py"], stdout=subprocess.PIPE)
output = result.stdout.decode()
res = float(output.split()[9]) * ntimes
print("{:.3f}".format(res))
102 changes: 102 additions & 0 deletions cusparse/2_cusparse_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python3

import argparse
import numpy as np
import os
from AIPowerMeter.deep_learning_power_measure.power_measure import experiment, parsers
from datetime import datetime
import warnings
import pandas as pd
import pickle

warnings.filterwarnings("ignore")

parser = argparse.ArgumentParser(
description='To illustrate how the consumption can be recorded for non python program, here is the measure the consumption of a small R script. R must be previously installed.'
)
parser.add_argument('--output_folder',
help='directory to save the energy consumption records',
default='measure_power', type=str)

parser.add_argument('--repetitions', help='number of repetitions of the algorithm', default=200, type=int)

parser.add_argument('--name_exe', help='Executable name', default='cusparse_blocked_ell_spmm.o', type=str)

#parser.add_argument('--repetitions', help='number of repetitions of the algorithm', default=20, type=int)

args = parser.parse_args()

size_matrices = [
# 512,
# 2048,
4096
]
number_blocks = 32
block_sizes = [16]
dim = 16
number_repetitions = vars(args)['repetitions']
NAME_EXE = vars(args)['name_exe']

# mean_time = {}
# mean_time_cublas = {}
# dict_results = {}

# driver = parsers.JsonParser(args.output_folder)
# #instantiating the experiment.
# exp = experiment.Experiment(driver)
# # starting the record, and wait two seconds between each energy consumption measurement
# p, q = exp.measure_yourself(period=2)



for size_matrix in size_matrices:
# block_sizes = [2**i for i in range(5, int(np.log(size_matrix)/np.log(2))+1)]

# print(block_sizes)
# print('*************Beginning sparse multiplications with matrices of size****************'+str(size_matrix))
# dict_block_sizes = {}
# dict_block_sizes_cublas = {}
for block_size in block_sizes:
print('A_row/col: {}, B_col: {}, Blocksize: {}'.format(size_matrix, dim, block_size))

A_ell_cols = block_size * number_blocks
A_num_blocks = A_ell_cols * size_matrix /(block_size * block_size);
total_params = A_num_blocks*block_size*block_size;
total_elems = (size_matrix*size_matrix);
sparsity = (total_elems - total_params) * 100 / total_elems
print('Sparsity: '+str(sparsity))

# os.system('./'+NAME_EXE+' '+str(size_matrix)+' '+str(size_matrix)+' '+str(block_size)+ ' '+str(number_blocks)+' '+str(number_repetitions)+' >/dev/null 2>&1')
os.system('./'+NAME_EXE+' '+str(size_matrix)+' '+str(dim)+' '+str(block_size)+ ' '+str(number_blocks)+' '+str(number_repetitions))

#read the computation times from a text file generated by the C program.
# with open('results.txt') as f:
# lines = f.readlines()

# dict_block_sizes[block_size] = float(lines[0].rstrip("\n"))
# dict_block_sizes_cublas[block_size] = float(lines[1].rstrip("\n"))
# mean_time[size_matrix] = dict_block_sizes
# mean_time_cublas[size_matrix] = dict_block_sizes_cublas
# q.put(experiment.STOP_MESSAGE)

# driver = parsers.JsonParser(args.output_folder)
# exp_result = experiment.ExpResults(driver)
# exp_result.print()

#print(dict_results)
# print()
# print('Dict results with cuSparse:')
# print(mean_time)

# print()
# print('Dict results with cuBlas: ')
# print(mean_time_cublas)

# #print(pd.DataFrame(mean_time))

# #Saving the results in the directory
# with open('results_cusparse.pickle', 'wb') as handle:
# pickle.dump(mean_time, handle, protocol=pickle.HIGHEST_PROTOCOL)

# with open('results_cublas.pickle', 'wb') as handle:
# pickle.dump(mean_time_cublas, handle, protocol=pickle.HIGHEST_PROTOCOL)
1 change: 1 addition & 0 deletions cusparse/AIPowerMeter
Submodule AIPowerMeter added at df6f1c
1 change: 1 addition & 0 deletions cusparse/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g++ -Wall cusparse_blocked_ell_spmm.c -o cusparse_blocked_ell_spmm.o -I /usr/local/cuda-12.0/include/ -L /usr/local/cuda-12.0/lib64 -lcudart -lcuda -lcusparse -lcublas -fopenmp -g -O2 -std=c++11
Loading

0 comments on commit 6d72df1

Please sign in to comment.