Skip to content

Commit

Permalink
Merge pull request #110 from pulp-platform/ne16-update
Browse files Browse the repository at this point in the history
[NE16] Update model with several bugfixes
  • Loading branch information
FrancescoConti committed Oct 27, 2021
2 parents 6aaa646 + d820f3f commit b9532d1
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 62 deletions.
19 changes: 14 additions & 5 deletions tools/gvsoc/pulp/models/pulp/ne16/include/ne16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@
#define NE16_NB_REG 24

#define NE16_SPECIAL_TRACE_REG NE16_NB_REG
#define DEFAULT_TRACE_LEVEL L0_JOB_START_END
#define NE16_SPECIAL_FORMAT_TRACE_REG NE16_NB_REG+1
#define DEFAULT_TRACE_LEVEL L0_CONFIG

enum Ne16State {
IDLE,
START,
START_STREAMIN,
STREAMIN_LOAD,
Expand All @@ -86,9 +88,9 @@ enum Ne16State {
};

enum Ne16TraceLevel {
L0_JOB_START_END,
L1_CONFIG,
L2_ACTIV_INOUT,
L0_CONFIG,
L1_ACTIV_INOUT,
L2_DEBUG,
L3_ALL
};

Expand Down Expand Up @@ -193,7 +195,10 @@ class Ne16 : public vp::component
vp::io_req io_req;
vp::trace trace;
vp::io_master out;
vp::reg_32 state;
vp::reg_8 activity;
Ne16TraceLevel trace_level;
int trace_format;

private:

Expand All @@ -220,6 +225,7 @@ class Ne16 : public vp::component
// DEBUG settings
bool fsm_traces;
bool accum_traces;
bool accum_traces_poststreamin;
bool accum_traces_postmatrixvec;
bool accum_traces_normquant;
bool accum_traces_streamout;
Expand All @@ -240,7 +246,7 @@ class Ne16 : public vp::component
// MAIN FSM and LOOP
int fsm();
void fsm_loop();
Ne16State state;
//Ne16State state;

// REGISTER FILE member functions
int regfile_rd(int);
Expand All @@ -259,6 +265,9 @@ class Ne16 : public vp::component
int job_pending;
int job_state;
unsigned char job_id;
int cxt_job_id[2];
char running_job_id;
int job_running;

// REGISTER FILE configuration parameters
int weights_ptr;
Expand Down
49 changes: 35 additions & 14 deletions tools/gvsoc/pulp/models/pulp/ne16/src/ne16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ void Ne16::reset(bool active)
this->x_array = xt::zeros<uint8_t>({this->NR_COLUMN, this->COLUMN_SIZE, this->TP_IN});
this->weight = xt::zeros<uint8_t>({this->FILTER_SIZE*this->FILTER_SIZE, 2});
this->nqs = xt::zeros<uint8_t>({this->TP_OUT});
this->job_id = 0;
this->cxt_job_id[0] = this->cxt_job_id[1] = -1;
this->running_job_id = 0;
this->job_running = 0;
}

// The `hwpe_slave` member function models an access to the NE16 SLAVE interface
vp::io_req_status_e Ne16::hwpe_slave(void *__this, vp::io_req *req)
{
Ne16 *_this = (Ne16 *)__this;

if (_this->trace_level == L1_CONFIG || _this->trace_level == L2_ACTIV_INOUT || _this->trace_level == L3_ALL) {
if (_this->trace_level == L1_ACTIV_INOUT || _this->trace_level == L2_DEBUG || _this->trace_level == L3_ALL) {
_this->trace.msg(vp::trace::LEVEL_DEBUG, "Received request (addr: 0x%x, size: 0x%x, is_write: %d, data: %p\n", req->get_addr(), req->get_size(), req->get_is_write(), req->get_data());
}
uint8_t *data = req->get_data(); // size depends on data get_size
Expand All @@ -71,31 +75,36 @@ vp::io_req_status_e Ne16::hwpe_slave(void *__this, vp::io_req *req)
if(req->get_is_write()) {
if(((req->get_addr() & 0xfff) - 0x20) >> 2 == NE16_SPECIAL_TRACE_REG) {
if(*data == 0) {
_this->trace_level = L0_JOB_START_END;
_this->trace.msg("Setting tracing level to L0_JOB_START_END\n");
_this->trace_level = L0_CONFIG;
_this->trace.msg("Setting tracing level to L0_CONFIG\n");
}
else if(*data == 1) {
_this->trace_level = L1_CONFIG;
_this->trace.msg("Setting tracing level to L1_CONFIG\n");
_this->trace_level = L1_ACTIV_INOUT;
_this->trace.msg("Setting tracing level to L1_ACTIV_INOUT\n");
}
else if(*data == 2) {
_this->trace_level = L2_ACTIV_INOUT;
_this->trace.msg("Setting tracing level to L2_ACTIV_INOUT\n");
_this->trace_level = L2_DEBUG;
_this->trace.msg("Setting tracing level to L2_DEBUG\n");
}
else {
_this->trace_level = L3_ALL;
_this->trace.msg("Setting tracing level to L3_ALL\n");
}
return vp::IO_REQ_OK;
}
else if(((req->get_addr() & 0xfff) - 0x20) >> 2 == NE16_SPECIAL_FORMAT_TRACE_REG) {
_this->trace_format = *data;
_this->trace.msg("Setting tracing format to %s\n", *data?"Hex":"Dec");
return vp::IO_REQ_OK;
}
else if((req->get_addr() & 0x17f) == 0x0) {
_this->commit();
if (!_this->fsm_start_event->is_enqueued() && *(uint32_t *) data == 0) {
if (!_this->job_running && !_this->fsm_start_event->is_enqueued() && *(uint32_t *) data == 0) {
_this->event_enqueue(_this->fsm_start_event, 1);
}
}
else {
if (_this->trace_level == L1_CONFIG || _this->trace_level == L2_ACTIV_INOUT || _this->trace_level == L3_ALL) {
if (_this->trace_level == L1_ACTIV_INOUT || _this->trace_level == L2_DEBUG || _this->trace_level == L3_ALL) {
_this->trace.msg(vp::trace::LEVEL_DEBUG, "offset: %d data: %08x\n", ((req->get_addr() & 0x17f) - 0x20) >> 2, *(uint32_t *) data);
}
_this->regfile_wr(((req->get_addr() & 0x17f) - 0x20)>> 2, *(uint32_t *) data);
Expand All @@ -104,19 +113,26 @@ vp::io_req_status_e Ne16::hwpe_slave(void *__this, vp::io_req *req)
else {
if((req->get_addr() & 0x17f) == 0x4) {
*(uint32_t *) data = _this->acquire();
if (_this->trace_level == L1_CONFIG || _this->trace_level == L2_ACTIV_INOUT || _this->trace_level == L3_ALL) {
if (_this->trace_level == L1_ACTIV_INOUT || _this->trace_level == L2_DEBUG || _this->trace_level == L3_ALL) {
_this->trace.msg("Returning %x\n", *(uint32_t *) data);
}
}
else if((req->get_addr() & 0x17f) == 0xc) {
*(uint32_t *) data = _this->status() ? 1 : 0;
if (_this->trace_level == L1_CONFIG || _this->trace_level == L2_ACTIV_INOUT || _this->trace_level == L3_ALL) {
*(uint32_t *) data = ((_this->cxt_job_id[0]>=0?0x1:0)|(_this->cxt_job_id[1]>=0?0x100:0));
if (_this->trace_level == L1_ACTIV_INOUT || _this->trace_level == L2_DEBUG || _this->trace_level == L3_ALL) {
_this->trace.msg("Returning %x\n", *(uint32_t *) data);
}
}
else if((req->get_addr() & 0x17f) == 0x10) {
// Returns the active running job or the last jobid that was run
*(uint32_t *) data = _this->running_job_id;
if (_this->trace_level == L1_ACTIV_INOUT || _this->trace_level == L2_DEBUG || _this->trace_level == L3_ALL) {
_this->trace.msg("Returning %x\n", *(uint32_t *) data);
}
}
else {
*(uint32_t *) data = _this->regfile_rd(((req->get_addr() & 0x17f) - 0x20) >> 2);
if (_this->trace_level == L1_CONFIG || _this->trace_level == L2_ACTIV_INOUT || _this->trace_level == L3_ALL) {
if (_this->trace_level == L1_ACTIV_INOUT || _this->trace_level == L2_DEBUG || _this->trace_level == L3_ALL) {
_this->trace.msg("Returning %x\n", *(uint32_t *) data);
}
}
Expand All @@ -128,6 +144,10 @@ vp::io_req_status_e Ne16::hwpe_slave(void *__this, vp::io_req *req)
int Ne16::build()
{
this->traces.new_trace("trace", &this->trace, vp::DEBUG);
this->new_reg("fsm_state", &this->state, 32);
this->new_reg("ne16_busy", &this->activity, 8);
this->activity.set(0);
this->state.set(IDLE);

this->new_master_port("out", &this->out);

Expand All @@ -140,7 +160,8 @@ int Ne16::build()
this->fsm_event = this->event_new(&Ne16::fsm_handler);
this->fsm_end_event = this->event_new(&Ne16::fsm_end_handler);

this->trace_level = L0_JOB_START_END;
this->trace_level = L0_CONFIG;
this->trace_format = 1;

return 0;
}
Expand Down
12 changes: 6 additions & 6 deletions tools/gvsoc/pulp/models/pulp/ne16/src/ne16_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
void Ne16::debug_x_buffer() {
if(this->mode_linear) {
std::ostringstream stringStream;
stringStream << "x_buffer[32,16] = \n" << std::hex << std::setw(2) << this->x_buffer_linear << std::dec << "\n";
stringStream << "x_buffer[32,16] = \n" << (this->trace_format?std::hex:std::dec) << std::setw(2) << this->x_buffer_linear << std::dec << "\n";
std::string copyOfStr = stringStream.str();
this->trace.msg(vp::trace::LEVEL_DEBUG, copyOfStr.c_str());
}
else {
std::ostringstream stringStream;
stringStream << "x_buffer[5,5,16] = \n" << std::hex << std::setw(2) << this->x_buffer << std::dec << "\n";
stringStream << "x_buffer[5,5,16] = \n" << (this->trace_format?std::hex:std::dec) << std::setw(2) << this->x_buffer << std::dec << "\n";
std::string copyOfStr = stringStream.str();
this->trace.msg(vp::trace::LEVEL_DEBUG, copyOfStr.c_str());
}
Expand All @@ -47,29 +47,29 @@ void Ne16::debug_x_array() {
// }
// else {
std::ostringstream stringStream;
stringStream << "x_array[9,9,16] = \n" << xt::print_options::threshold(10000) << std::hex << std::setw(2) << this->x_array << std::dec << "\n";
stringStream << "x_array[9,9,16] = \n" << xt::print_options::threshold(10000) << (this->trace_format?std::hex:std::dec) << std::setw(2) << this->x_array << std::dec << "\n";
std::string copyOfStr = stringStream.str();
this->trace.msg(vp::trace::LEVEL_DEBUG, copyOfStr.c_str());
// }
}

void Ne16::debug_accum(){
std::ostringstream stringStream;
stringStream << "accum[9,32] = \n" << std::hex << std::setw(8) << xt::cast<int32_t>(this->accum) << std::dec << "\n";
stringStream << "accum[9,32] = \n" << (this->trace_format?std::hex:std::dec) << std::setw(8) << xt::cast<int32_t>(this->accum) << std::dec << "\n";
std::string copyOfStr = stringStream.str();
this->trace.msg(vp::trace::LEVEL_DEBUG, copyOfStr.c_str());
}

// void Ne16::debug_psum_column(){
// std::ostringstream stringStream;
// stringStream << "psum_column[9] = \n" << std::hex << std::setw(8) << xt::cast<int32_t>(this->psum_column) << std::dec << "\n";
// stringStream << "psum_column[9] = \n" << (this->trace_format?std::hex:std::dec) << std::setw(8) << xt::cast<int32_t>(this->psum_column) << std::dec << "\n";
// std::string copyOfStr = stringStream.str();
// this->trace.msg(vp::trace::LEVEL_DEBUG, copyOfStr.c_str());
// }

void Ne16::debug_psum_block(){
std::ostringstream stringStream;
stringStream << "psum_block[9,9] = \n" << std::hex << std::setw(8) << xt::cast<int32_t>(this->psum_block) << std::dec << "\n";
stringStream << "psum_block[9,9] = \n" << (this->trace_format?std::hex:std::dec) << std::setw(8) << xt::cast<int32_t>(this->psum_block) << std::dec << "\n";
std::string copyOfStr = stringStream.str();
this->trace.msg(vp::trace::LEVEL_DEBUG, copyOfStr.c_str());
}
56 changes: 39 additions & 17 deletions tools/gvsoc/pulp/models/pulp/ne16/src/ne16_fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@

void Ne16::fsm_start_handler(void *__this, vp::clock_event *event) {
Ne16 *_this = (Ne16 *)__this;
_this->state = START;
_this->state.set(START);
if(_this->trace_level == L3_ALL) {
_this->trace.msg(vp::trace::LEVEL_DEBUG, "FSM START EVENT\n");
}

// clear state and propagate context
_this->clear_all();
_this->regfile_cxt();
_this->job_running = 1;

// convenience parameters used internally in the model, but not set by register file
_this->h_out = (_this->subtile_nb_ho-(_this->subtile_rem_ho ? 1 : 0)) * _this->FILTER_SIZE + _this->subtile_rem_ho;
Expand All @@ -49,7 +50,7 @@ void Ne16::fsm_start_handler(void *__this, vp::clock_event *event) {
assert(!(_this->streamin && _this->quantization_bits!=32));

// padding is not compatible with FS=1. sorry!
assert((_this->padding_top==0 && _this->padding_right==0 && _this->padding_bottom==0 && _this->padding_left==0) || _this->fs==3);
// assert((_this->padding_top==0 && _this->padding_right==0 && _this->padding_bottom==0 && _this->padding_left==0) || _this->fs==3);

// filter masking is not compatible with FS=1. sorry!
assert((_this->filter_mask_top==0 && _this->filter_mask_right==0 && _this->filter_mask_bottom==0 && _this->filter_mask_left==0) || _this->fs==3);
Expand All @@ -76,10 +77,6 @@ void Ne16::fsm_start_handler(void *__this, vp::clock_event *event) {
_this->w_out = 1;
}

_this->trace.msg(vp::trace::LEVEL_INFO, "Starting a job (id=%d) with the following configuration:\n", _this->job_id);
if(_this->trace_level != L0_JOB_START_END) {
_this->printout();
}
_this->fsm_loop();
}

Expand All @@ -90,22 +87,27 @@ void Ne16::fsm_handler(void *__this, vp::clock_event *event) {

void Ne16::fsm_end_handler(void *__this, vp::clock_event *event) {
Ne16 *_this = (Ne16 *)__this;
int job_id = _this->cxt_job_id[_this->cxt_use_ptr];
_this->job_running = 0;
_this->cxt_job_id[_this->cxt_use_ptr] = -1;
_this->cxt_use_ptr = 1-_this->cxt_use_ptr;
_this->job_pending--;
_this->irq.sync(true);
_this->trace.msg(vp::trace::LEVEL_INFO, "Ending job (id=%d).\n", _this->job_id);
_this->trace.msg(vp::trace::LEVEL_INFO, "Ending job (id=%d).\n", job_id);
if (!_this->fsm_start_event->is_enqueued() && _this->job_pending > 0) {
_this->event_enqueue(_this->fsm_start_event, 1);
_this->trace.msg(vp::trace::LEVEL_INFO, "Starting a new job from the queue.\n");
}
_this->activity.set(0);
_this->state.set(IDLE);
}

void Ne16::fsm_loop() {
auto latency = 0;
do {
latency = this->fsm();
} while(latency == 0 && state != END);
if(state == END && !this->fsm_end_event->is_enqueued()) {
} while(latency == 0 && state.get() != END);
if(state.get() == END && !this->fsm_end_event->is_enqueued()) {
this->event_enqueue(this->fsm_end_event, latency);
}
else if (!this->fsm_event->is_enqueued()) {
Expand All @@ -114,35 +116,49 @@ void Ne16::fsm_loop() {
}

int Ne16::fsm() {
auto state_next = this->state;
auto state_next = this->state.get();
auto latency = 0;

this->x_buffer_traces = false;
this->x_buffer_traces_postload = false;
this->accum_traces_poststreamin = false;
this->accum_traces = false;
this->accum_traces_postmatrixvec =false;
this->accum_traces_normquant = false;
this->accum_traces_streamout = false;
this->psum_block_traces = false;
this->binconv_traces = false;
this->fsm_traces = false;
// if(this->trace_level == L1_CONFIG) {
// }
if(this->trace_level == L2_ACTIV_INOUT) {
if(this->trace_level == L1_ACTIV_INOUT) {
this->x_buffer_traces_postload = true;
this->accum_traces_streamout = true;
}
if(this->trace_level == L2_DEBUG) {
this->x_buffer_traces_postload = true;
this->fsm_traces = true;
this->accum_traces_poststreamin = true;
this->accum_traces_streamout = true;
this->accum_traces_postmatrixvec = true;
this->accum_traces_normquant = true;
}
if(this->trace_level == L3_ALL) {
this->x_buffer_traces = true;
this->x_buffer_traces_postload = true;
this->fsm_traces = true;
this->accum_traces = true;
this->accum_traces_poststreamin = true;
this->accum_traces_streamout = true;
this->accum_traces_postmatrixvec = true;
this->accum_traces_normquant = true;
}

switch(this->state) {
switch(this->state.get()) {

case START:
this->activity.set(1);
this->trace.msg(vp::trace::LEVEL_INFO, "Starting a job (id=%d) with the following configuration:\n", this->cxt_job_id[this->cxt_use_ptr]);
this->printout();

state_next = START_STREAMIN;
break;

Expand All @@ -169,13 +185,19 @@ int Ne16::fsm() {
case STREAMIN:
if(this->fsm_traces) {
this->trace.msg(vp::trace::LEVEL_DEBUG, "State STREAMIN\n");
this->trace.msg(vp::trace::LEVEL_DEBUG, " streamin_ij_out=%d\n", this->streamin_ij_out);
this->trace.msg(vp::trace::LEVEL_DEBUG, " streamin_k_out_iter=%d\n", this->streamin_k_out_iter);
this->trace.msg(vp::trace::LEVEL_DEBUG, " streamin_j_out_iter=%d\n", this->streamin_j_out_iter);
this->trace.msg(vp::trace::LEVEL_DEBUG, " streamin_i_out_iter=%d\n", this->streamin_i_out_iter);
}
latency = this->streamin_cycle();
if(this->accum_traces) {
this->debug_accum();
}
if(this->streamin_exit_idx()) {
if(this->accum_traces_poststreamin) {
this->trace.msg(vp::trace::LEVEL_DEBUG, "State STREAMIN Finished\n");
this->debug_accum();
}
state_next = STREAMIN_LOAD;
}
else {
Expand Down Expand Up @@ -292,7 +314,7 @@ int Ne16::fsm() {
if(this->fsm_traces) {
this->trace.msg(vp::trace::LEVEL_DEBUG, "Exiting MATRIXVEC\n");
}
if(!this->accum_traces && this->accum_traces_postmatrixvec) {
if(this->accum_traces_postmatrixvec) {
this->debug_accum();
}
if(!matrixvec_to_matrixvec_idx()) {
Expand Down Expand Up @@ -411,6 +433,6 @@ int Ne16::fsm() {

}

this->state = state_next;
this->state.set(state_next);
return latency;
}
Loading

0 comments on commit b9532d1

Please sign in to comment.