Skip to content

Commit

Permalink
Adding old files from final step-6 of workshop (Low_Power stage)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucafiore committed Nov 22, 2019
1 parent 0f75cb5 commit fa1507c
Show file tree
Hide file tree
Showing 973 changed files with 1,019,808 additions and 16 deletions.
18 changes: 2 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
# Build and Release Folders
bin-debug/
bin-release/
[Oo]bj/
[Bb]in/
#Git will ignore these files:

# Other files and folders
.settings/
*.sdf

# Executables
*.swf
*.air
*.ipa
*.apk

# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
# should NOT be excluded as they contain compiler settings and other important
# information for Eclipse / Flash Builder.
66 changes: 66 additions & 0 deletions MATLAB_Test/CONV1_function.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
function [Out_conv1] = CONV1_function(parallelism,Inputs_fixed)
%FIRST CONVOLUTIONAL LAYER EMULATING THE VHDL

%% PARAMETERS AND INITIALIZATION
inputSize = 32;
numInputs = 1;
filterSize = 5;
numFilters = 6;
integer_part= 5;

%uscite del moltiplicatore
parallelism_out_mult=2*parallelism-1;
decimal_part_mult=parallelism_out_mult-integer_part;

%uscite del sommatore e MAC
parallelism_out=parallelism;
%decimal_part=parallelism_out-1;

Out_conv1 = fi(zeros(inputSize-filterSize+1, inputSize-filterSize+1, numFilters), 1, parallelism, parallelism-1);
Weights = zeros(filterSize,filterSize,numFilters);

%% READING WEIGHTS AND BIAS
fileW = fopen('ColumnWeights_conv2d_1.txt','r');
fileB = fopen('ColumnBias_conv2d_1.txt','r');
%lettura totale del file dei pesi
fromatSpec = '%f';
Weights_read = fscanf(fileW,fromatSpec);
Bias = fscanf(fileB,fromatSpec);

y=1;
for r=1:filterSize
for c=1:filterSize
for i=1:numFilters
Weights(r,c,i) = Weights_read(y);
y=y+1;
end
end
end

fclose(fileW);
fclose(fileB);

%% VARIABLES
in_matrix_fixed = fi(Inputs_fixed, 1, parallelism, parallelism-1,'RoundingMethod', 'Floor','OverflowAction', 'Wrap');
in_matrix_fixed = fi(in_matrix_fixed, 1, parallelism, parallelism-1,'RoundingMethod', 'Convergent','OverflowAction', 'Wrap');

w_matrix_fixed = fi(Weights, 1, parallelism, parallelism-1, 'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');
bias_fixed = fi(Bias, 1, parallelism, parallelism-1, 'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');
bias_fixed = fi(bias_fixed, 1, parallelism, parallelism-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');

%% NOSTRA CONVOLUZIONE
for i=1:numFilters
for r=1:28
for c=1:28
a = sum(sum(fi(in_matrix_fixed(r:r+4,c:c+4).*w_matrix_fixed(:,:,i),1,parallelism_out_mult,decimal_part_mult,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap'))) + bias_fixed(i);
if a > 0
%normalize the output
Out_conv1(r,c,i) = fi((double(a)/2^4),1,parallelism_out,parallelism_out-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
else
Out_conv1(r,c,i) = fi(0, 1, parallelism_out,parallelism_out-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
end
end
end

end
157 changes: 157 additions & 0 deletions MATLAB_Test/CONV1_function_OPT.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
function [Out_conv1,conv_op,conv_n] = CONV1_function_OPT(parallelism,Inputs_fixed)
%FIRST CONVOLUTIONAL LAYER EMULATING THE VHDL

%% PARAMETERS AND INITIALIZATION
inputSize = 32;
numInputs = 1;
filterSize = 5;
numFilters = 6;
IntegerPart = 5;
parallelism_out_mult = 2*parallelism-1;
threshold=50;
%preallocation
Out_conv1 = fi(zeros(inputSize-filterSize+1, inputSize-filterSize+1, numFilters), 1, parallelism, parallelism-1);
Weights = zeros(filterSize,filterSize,numFilters);

%% READING WEIGHTS AND BIAS
fileW = fopen('ColumnWeights_conv2d_1.txt','r');
fileB = fopen('ColumnBias_conv2d_1.txt','r');

%lettura totale del file dei pesi
fromatSpec = '%f';
Weights_read = fscanf(fileW,fromatSpec);
Bias = fscanf(fileB,fromatSpec);

% % AAA= sum(Weights_read)/length(Weights_read);
% % BB= sum(Weights_read(Weights_read>0))/length(Weights_read(Weights_read>0));
% % CC= sum(Weights_read(Weights_read<0))/length(Weights_read(Weights_read<0));
% % BBB = max(Weights_read(Weights_read>0))-BB;
% % B= min(Weights_read(Weights_read>0))-BB;
% % CCC = min(Weights_read(Weights_read<0))-CC;
% % C= max(Weights_read(Weights_read<0))-CC;

%riordinamento pesi
y=1;
for r=1:filterSize
for c=1:filterSize
for i=1:numFilters
Weights(r,c,i) = Weights_read(y);
y=y+1;
end
end
end

fclose(fileW);
fclose(fileB);

%% VARIABLES
in_matrix_fixed = fi(Inputs_fixed, 1, parallelism, parallelism-1,'RoundingMethod', 'Floor','OverflowAction', 'Wrap');
in_matrix_fixed = fi(in_matrix_fixed, 1, parallelism, parallelism-1,'RoundingMethod', 'Convergent','OverflowAction', 'Wrap');

w_matrix_fixed = fi(Weights, 1, parallelism, parallelism-1, 'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');
bias_fixed = fi(Bias, 1, parallelism, parallelism-1, 'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');
bias_fixed = fi(bias_fixed, 1, parallelism, parallelism-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');

%% VARIABLE FOR OPTIMIZATION

%preallocation
x2_matrix = zeros(filterSize,filterSize,numFilters);

inputs_opt = fi(Inputs_fixed, 0, 1, 1,'RoundingMethod', 'Floor'); %MSB inputs
w_opt = fi(w_matrix_fixed, 1, 2, 1,'RoundingMethod', 'Floor'); %MSB weights

for i=1:numFilters
for r=1:filterSize
for c=1:filterSize
if w_opt(r,c,i)<0
x2_matrix(r,c,i)=1;
else
x2_matrix(r,c,i)=0;
end
end
end
end

%% NOSTRA CONVOLUZIONE

conv_n=0; %conta numero di convoluzioni totali effettivamente svolte (risparmio potenza)
conv_op=0; %conta in quanti gruppi da 24 c'è almeno una convoluzione da effettuare (risparmio velocita)

%threshold=46;

for r=1:14
for c=1:14
convv=0;
for i=1:numFilters
x2=x2_matrix(:,:,i);

%1-matrix
x1 = inputs_opt(r*2-1:r*2+4-1, c*2-1:c*2+4-1);
G_pos = x1 & not(x2);
P_pos = not(x1) & not(x2);
P_neg = not(x1) & x2;
prec = -threshold + sum(sum(G_pos))*3 + sum(sum(P_pos))*2 + sum(sum(P_neg))*1;
if prec >= 0
convv=convv+1;
a = sum(sum(fi(in_matrix_fixed(r*2-1:r*2+4-1,c*2-1:c*2+4-1).*w_matrix_fixed(:,:,i),1,parallelism_out_mult, parallelism_out_mult-IntegerPart,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap'))) + bias_fixed(i);
if a > 0
Out_conv1(r*2-1,c*2-1,i) = fi(double(a)/2^4, 1, parallelism, parallelism-1,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
end

%2-matrix
x1 = inputs_opt(r*2-1:r*2+4-1, c*2:c*2+4);
G_pos = x1 & not(x2);
P_pos = not(x1) & not(x2);
P_neg = not(x1) & x2;
prec = -threshold + sum(sum(G_pos))*3 + sum(sum(P_pos))*2 + sum(sum(P_neg))*1;
if prec >= 0
convv=convv+1;
a = sum(sum(fi(in_matrix_fixed(r*2-1:r*2+4-1, c*2:c*2+4).*w_matrix_fixed(:,:,i),1,parallelism_out_mult, parallelism_out_mult-IntegerPart,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap'))) + bias_fixed(i);
if a > 0
Out_conv1(2*r-1,2*c,i) = fi(double(a)/2^4, 1, parallelism, parallelism-1,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
end
%3-matrix
x1 = inputs_opt(r*2:r*2+4, c*2-1:c*2+4-1);
G_pos = x1 & not(x2);
P_pos = not(x1) & not(x2);
P_neg = not(x1) & x2;
prec = -threshold + sum(sum(G_pos))*3 + sum(sum(P_pos))*2 + sum(sum(P_neg))*1;
if prec >= 0
convv=convv+1;
a = sum(sum(fi(in_matrix_fixed(r*2:r*2+4, c*2-1:c*2+4-1).*w_matrix_fixed(:,:,i),1,parallelism_out_mult, parallelism_out_mult-IntegerPart,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap'))) + bias_fixed(i);
if a > 0
Out_conv1(2*r,2*c-1,i) = fi(double(a)/2^4, 1, parallelism, parallelism-1,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
end

%4-matrix
x1 = inputs_opt(r*2:r*2+4, c*2:c*2+4);
G_pos = x1 & not(x2);
P_pos = not(x1) & not(x2);
P_neg = not(x1) & x2;
prec = -threshold + sum(sum(G_pos))*3 + sum(sum(P_pos))*2 + sum(sum(P_neg))*1;
if prec >= 0
convv=convv+1;
a = sum(sum(fi(in_matrix_fixed(r*2:r*2+4, c*2:c*2+4).*w_matrix_fixed(:,:,i),1,parallelism_out_mult, parallelism_out_mult-IntegerPart,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap'))) + bias_fixed(i);
if a > 0
Out_conv1(2*r,2*c,i) = fi(double(a)/2^4, 1, parallelism, parallelism-1,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
end
end

if convv~=0
conv_op=conv_op+1;
conv_n=conv_n+convv;
end

end
end

%display information
fprintf("[(SPEED) Convolution performed: %d out of 196]\n", conv_op)
fprintf("[(POWER) Convolution performed: %d out of 4704]\n", conv_n)



77 changes: 77 additions & 0 deletions MATLAB_Test/CONV2_function.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
%% This function is perfect!

function [Out_conv2] = CONV2_function(parallelism,Inputs_fixed)
%FIRST CONVOLUTIONAL LAYER EMULATING THE VHDL

%% PARAMETERS AND INITIALIZATION
inputSize = 14;
numInputs = 6;
filterSize = 5;
numFilters = 16;
integer_part= 5;

%uscite del moltiplicatore
parallelism_out_mult=2*parallelism-1;
decimal_part_mult=parallelism_out_mult-integer_part;

%uscite del sommatore e MAC
parallelism_out=parallelism;

%preallocation
Weights = zeros(filterSize,filterSize,numFilters,numInputs);
Out_conv2 = zeros(inputSize-filterSize+1,inputSize-filterSize+1,numFilters);

%% READING WEIGHTS AND BIAS
fileW = fopen('ColumnWeights_conv2d_2.txt','r');
fileB = fopen('ColumnBias_conv2d_2.txt','r');
%lettura totale del file dei pesi
fromatSpec = '%f';
Weights_read = fscanf(fileW,fromatSpec);
Bias = fscanf(fileB,fromatSpec);

y=1;
for r=1:filterSize
for c=1:filterSize
for n=1:numInputs
for i=1:numFilters
Weights(r,c,i,n) = Weights_read(y);
y=y+1;
end
end
end
end

fclose(fileW);
fclose(fileB);

%% VARIABLES

in_matrix_fixed = fi(Inputs_fixed, 1, parallelism_out, parallelism_out-1,'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');
w_matrix_fixed = fi(Weights, 1, parallelism_out, parallelism_out-1,'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');
bias_fixed = fi(Bias, 1, parallelism_out, parallelism_out-1, 'RoundingMethod', 'Convergent', 'OverflowAction', 'Wrap');

bias_fixed = fi(bias_fixed, 1, parallelism_out, parallelism_out-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
%% NOSTRA CONVOLUZIONE
for i=1:numFilters
for r=1:10
for c=1:10
a = bias_fixed(i);
for n=1:numInputs
a = a + fi(sum(sum(double(2^4*in_matrix_fixed(r:r+4,c:c+4,n).*w_matrix_fixed(:,:,i,n)))),1,parallelism_out_mult,decimal_part_mult,'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
if a > 0
%normalize the output
Out_conv2(r,c,i) = fi(double(a)./2^4,1,parallelism_out,parallelism_out-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');

else
Out_conv2(r,c,i) = fi(0, 1, parallelism_out,parallelism_out-1, 'RoundingMethod', 'Floor', 'OverflowAction', 'Wrap');
end
end
end
end


end



6 changes: 6 additions & 0 deletions MATLAB_Test/ColumnBias_conv2d_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0.00898796
0.0761198
-0.0147848
-0.0201447
0.0349664
0.0786912
16 changes: 16 additions & 0 deletions MATLAB_Test/ColumnBias_conv2d_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
0.00473564
-0.000151541
0.045757
0.0108148
-0.0332738
0.0800577
-0.0125601
0.0429264
0.00579609
0.00834937
0.00356025
0.0425244
0.0227917
0.0264721
-0.0143068
0.00427361
Loading

0 comments on commit fa1507c

Please sign in to comment.