Skip to content

Commit

Permalink
Merge pull request #29 from negishubham/cnn-support
Browse files Browse the repository at this point in the history
New feature added - weight and input quantization.
  • Loading branch information
Aayush-Ankit authored Jun 2, 2020
2 parents 090feed + 5f013fe commit 4a5ab26
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 6 deletions.
16 changes: 16 additions & 0 deletions include/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,41 @@
adc_lat_dict = {'1' : 12.5,
'2' : 25,
'4' : 50,
'5' : 62.5,
'6' : 75,
'7' : 87.5,
'8' : 100,
'9' : 112.5,
'16': 200}

adc_pow_dyn_dict = {'1' : 0.225,
'2' : 0.45,
'4' : 0.9,
'5' : 1.125,
'6' : 1.35,
'7' : 1.575,
'8' : 1.8,
'9' : 2.025,
'16': 3.6}

adc_pow_leak_dict = {'1' : 0.025,
'2' : 0.05,
'4' : 0.1,
'5' : 0.125,
'6' : 0.150,
'7' : 0.175,
'8' : 0.2,
'9' : 0.225,
'16': 0.4}

adc_area_dict = {'1' : 0.0012,
'2' : 0.0012,
'4' : 0.0012,
'5' : 0.0012,
'6' : 0.0012,
'7' : 0.0012,
'8' : 0.0012,
'9' : 0.0012,
'16': 0.0012}

# SNH (MVM pipeline)
Expand Down
6 changes: 4 additions & 2 deletions include/example-configs/config-cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
# Fixed parameters
addr_width = 22 # Added to address larger address space for conv layers (#TODO: Compiler needs to fix shared memory reuse)
data_width = num_bits # (in bits)
xbdata_width = data_width # (in bits)
xbdata_width = data_width # (in bits), equivalent to input_prec
instrn_width = 48 # (in bits)

# Input and Weight parameters
input_prec = 16
weight_width = 16
# Change here - Specify the IMA parameters here
xbar_bits = 2
num_matrix = 2 # each matrix is 1-fw logical xbar for inference and 1-fw, 1-bw, and 1 delta logical xbar for training. Each logical xbar for inference is 8-fw physical xbar and for training 8-fw, 8-bw and 16-delta physical xbars.
Expand Down
4 changes: 3 additions & 1 deletion include/example-configs/config-mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
data_width = num_bits # (in bits)
xbdata_width = data_width # (in bits)
instrn_width = 48 # (in bits)

# Input and Weight parameters
input_prec = 16
weight_width = 16
# Change here - Specify the IMA parameters here
xbar_bits = 2
num_matrix = 2 # each matrix is 1-fw logical xbar for inference and 1-fw, 1-bw, and 1 delta logical xbar for training. Each logical xbar for inference is 8-fw physical xbar and for training 8-fw, 8-bw and 16-delta physical xbars.
Expand Down
7 changes: 4 additions & 3 deletions src/ima.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def inner_product (mat_id, key):
self.xb_outMem_list[mat_id][key].reset ()

## Loop to cover all bits of inputs
for k in xrange (cfg.xbdata_width / cfg.dac_res):
for k in xrange (int(math.ceil(cfg.input_prec / cfg.dac_res))): #quantization affects the # of streams
#for k in xrange (1):
# read the values from the xbar's input register
out_xb_inMem = self.xb_inMem_list[mat_id][key].read (cfg.dac_res)
Expand All @@ -612,7 +612,7 @@ def inner_product (mat_id, key):
out_dac = self.dacArray_list[mat_id][key].propagate_dummy(out_xb_inMem) #pass through

# Do for (data_width/xbar_bits) xbars
num_xb = cfg.data_width / cfg.xbar_bits
num_xb = int(math.ceil(float(cfg.weight_width) / cfg.xbar_bits)) # # of XBs change with quantization
out_xbar = [[] for x in range(num_xb)]
out_snh = [[] for x in range(num_xb)]
for m in range (num_xb):
Expand Down Expand Up @@ -793,7 +793,8 @@ def xbComputeLatency (self, mask):
print("adccccc.adc_res", adccccc.adc_res)
print("---")
'''
latency_ip = lat_temp * ((cfg.xbdata_width / cfg.dac_res) + num_stage - 1) * float(int(fb_found>0))
latency_ip = lat_temp * ((cfg.input_prec / cfg.dac_res) + num_stage - 1) * float(int(fb_found>0))*(math.ceil(float(cfg.weight_width)/ \
cfg.xbar_bits) /math.ceil(float(cfg.data_width)/cfg.xbar_bits)) # last term to account for the effect of quantization on latency
## MVM outer product occurs in 4 cycles to take care of all i/o polarities (++, +-, -+, --)
num_phase = 4
lat_temp = self.matrix_list[0]['f'][0].getOpLatency()
Expand Down
57 changes: 57 additions & 0 deletions test/mlp_l4_mnist/fully-connected-layer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2019 IMPACT Research Group, University of Illinois.
* All rights reserved.
*
* This file is covered by the LICENSE.txt license file in the root directory.
*
*/

#include <assert.h>
#include <string>
#include <vector>
#include <iostream>

#include "puma.h"
#include "fully-connected-layer.h"

int main(int argc, char** argv) {

//Model model = Model::create("fully-connected-layer");

// Process parameters
unsigned int in_size;
unsigned int out_size;
if(argc == 4) {
in_size = atoi(argv[1]);
out_size = atoi(argv[2]);
}

std:: string str=std::string("fully") + argv[3] + std::string("-connected-layer");
Model model = Model::create(str);

// Input
auto in = InputVector::create(model, "in", in_size);

// Output
auto out = OutputVector::create(model, "out", out_size);

// Layer
out = fully_connected_layer(model, "", in_size, out_size, in);

// Compile
model.compile();

// Bind data
ModelInstance modelInstance = ModelInstance::create(model);
float* weights = new float[in_size*out_size];
fully_connected_layer_bind(modelInstance, "", weights);
modelInstance.generateData();

// Destroy model
model.destroy();
delete[] weights;

return 0;

}

27 changes: 27 additions & 0 deletions test/mlp_l4_mnist/fully-connected-layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2019 IMPACT Research Group, University of Illinois.
* All rights reserved.
*
* This file is covered by the LICENSE.txt license file in the root directory.
*
*/

#ifndef _PUMA_TEST_FULLY_CONNECTED_LAYER_
#define _PUMA_TEST_FULLY_CONNECTED_LAYER_

#include "puma.h"

static Vector fully_connected_layer(Model model, std::string layerName, unsigned int in_size, unsigned int out_size, Vector in) {

ConstantMatrix mat = ConstantMatrix::create(model, layerName + "mat", in_size, out_size);

return sig(mat*in);

}

static void fully_connected_layer_bind(ModelInstance modelInstance, std::string layerName, float* weights) {
modelInstance.bind(layerName + "mat", weights);
}

#endif

46 changes: 46 additions & 0 deletions test/utils/run-mlp-layer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
set -v
set -e
path=`pwd` #path to your puma directory
echo $path
cppfile=fully-connected-layer #name for cpp file that you want to compile ex- mlp_l4_mnist.cpp, conv-layer.cpp, convmax-layer.cpp
name=fully #name for the folder generated by compiler
pumaenv=pumaenv #name for the environment
fileno=0
name=$name$fileno

#layer parameter
in=64
out=10


#copying mlp config file
rm ${path}/puma-simulator/include/config.py #remove existing config file
cp ${path}/puma-simulator/include/example-configs/config-mlp.py ${path}/puma-simulator/include/config.py #copy the mlp config file to include
#copying model file
rm ${path}/puma-compiler/test/${cppfile}.cpp ${path}/puma-compiler/test/${cppfile}.h
cp ${path}/puma-simulator/test/mlp_l4_mnist/${cppfile}.cpp ${path}/puma-compiler/test/${cppfile}.cpp #copy the mlp config file to include
cp ${path}/puma-simulator/test/mlp_l4_mnist/${cppfile}.h ${path}/puma-compiler/test/${cppfile}.h #copy the mlp config file to include

cd ${path}/puma-compiler/src
source ~/.bashrc
conda activate ${pumaenv}

make clean
make

cd ${path}/puma-compiler/test
make clean
make ${cppfile}.test
export LD_LIBRARY_PATH=`pwd`/../src:$LD_LIBRARY_PATH
./${cppfile}.test ${in} ${out} ${fileno}
echo $cppfile
./generate-py.sh
cp -r ${name} ../../puma-simulator/test/testasm

cd ${path}/puma-simulator/src


python dpe.py -n ${name}



0 comments on commit 4a5ab26

Please sign in to comment.