Skip to content

Commit 3dcf1f5

Browse files
committed
Data Handler: fixed bug on z and nz indeces when num_zeto_tiles==0. Removed support for sds_alloc() and sds_free().
1 parent 5321570 commit 3dcf1f5

File tree

5 files changed

+335
-175
lines changed

5 files changed

+335
-175
lines changed

include/layers/lstm/lstm_data_handler.h

Lines changed: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ class AcceleratorBlob {
290290
int s_size_;
291291
std::unordered_map<std::string, SvdVecType*> cur_gates_;
292292
std::unordered_map<std::string, SvdVecType*> rec_gates_;
293-
FixType* fix_u_cur_;
294-
FixType* fix_u_rec_;
295-
FixType* fix_v_;
293+
std::vector<FixType> fix_u_cur_;
294+
std::vector<FixType> fix_u_rec_;
295+
std::vector<FixType> fix_v_;
296296
std::vector<std::vector<FloatType> > x_;
297297
std::vector<std::vector<FloatType> > h_;
298298
std::vector<std::vector<FloatType> > c_;
@@ -301,22 +301,22 @@ class AcceleratorBlob {
301301
std::vector<std::vector<FloatType> > h_curr_;
302302
std::vector<std::vector<FloatType> > c_curr_;
303303
std::vector<std::vector<FloatType> > bias_;
304-
std::vector<FixType*> fix_x_;
305-
std::vector<FixType*> fix_h_;
306-
std::vector<FixType*> fix_c_;
307-
std::vector<FixType*> fix_h_prev_;
308-
std::vector<FixType*> fix_c_prev_;
309-
std::vector<FixType*> fix_h_curr_;
310-
std::vector<FixType*> fix_c_curr_;
311-
std::vector<FixType*> fix_bias_;
312-
std::vector<FixType*> fix_s_;
313-
ap_uint<NumTilesU>* fix_nz_u_;
314-
ap_uint<NumTilesV>* fix_nz_v_;
304+
std::vector<std::vector<FixType> > fix_x_;
305+
std::vector<std::vector<FixType> > fix_h_;
306+
std::vector<std::vector<FixType> > fix_c_;
307+
std::vector<std::vector<FixType> > fix_h_prev_;
308+
std::vector<std::vector<FixType> > fix_c_prev_;
309+
std::vector<std::vector<FixType> > fix_h_curr_;
310+
std::vector<std::vector<FixType> > fix_c_curr_;
311+
std::vector<std::vector<FixType> > fix_bias_;
312+
std::vector<std::vector<FixType> > fix_s_;
313+
std::vector<ap_uint<NumTilesU> > fix_nz_u_;
314+
std::vector<ap_uint<NumTilesV> > fix_nz_v_;
315315

316316
void InitVector(const bool init_random, const int num_inputs, const int size,
317-
std::vector<FixType*>& fix_y, std::vector<std::vector<FloatType> >& y) {
317+
std::vector<std::vector<FixType> >& fix_y, std::vector<std::vector<FloatType> >& y) {
318318
for (int i = 0; i < num_inputs; ++i) {
319-
fix_y[i] = svd::AllocateContiguously<FixType>(size);
319+
// fix_y[i] = new FixType[size]; // svd::AllocateContiguously<FixType>(size);
320320
for (int j = 0; j < size; ++j) {
321321
FloatType tmp = init_random ? 0.00001 * rand() : 0;
322322
y[i][j] = tmp;
@@ -381,8 +381,18 @@ class AcceleratorBlob {
381381
AcceleratorBlob(const int num_inputs, const int refinement_steps,
382382
const int u_cur_size, const int u_rec_size, const int v_size,
383383
const int num_tiles_u, const int num_zero_tiles_u, const int num_tiles_v,
384-
const int num_zero_tiles_v) {
385-
srand(time(NULL));
384+
const int num_zero_tiles_v)
385+
// : fix_x_(num_inputs, nullptr),
386+
// fix_h_(num_inputs, nullptr),
387+
// fix_c_(num_inputs, nullptr),
388+
// fix_h_prev_(num_inputs, nullptr),
389+
// fix_c_prev_(num_inputs, nullptr),
390+
// fix_h_curr_(num_inputs, nullptr),
391+
// fix_c_curr_(num_inputs, nullptr),
392+
// fix_bias_(num_inputs, nullptr),
393+
// fix_s_(num_inputs, nullptr)
394+
{
395+
// srand(time(NULL));
386396
this->lstm_num_inputs_ = num_inputs;
387397
this->lstm_input_size_ = u_cur_size;
388398
this->lstm_output_size_ = v_size;
@@ -401,15 +411,20 @@ class AcceleratorBlob {
401411
const int kV_TotalSize = kNumGates * this->cur_gates_["i"]->get_v()->get_pruned_total_size();
402412
const int kS_TotalSize = kNumGates * refinement_steps;
403413
std::cout << "setting fix_u_cur_" << std::endl;
404-
this->fix_u_cur_ = svd::AllocateContiguously<FixType>(kU_CurTotalSize);
405-
this->fix_u_rec_ = svd::AllocateContiguously<FixType>(kU_RecTotalSize);
406-
this->fix_v_ = svd::AllocateContiguously<FixType>(kV_TotalSize);
414+
// this->fix_u_cur_ = new FixType[kU_CurTotalSize]; // svd::AllocateContiguously<FixType>(kU_CurTotalSize);
415+
// this->fix_u_rec_ = new FixType[kU_RecTotalSize]; // svd::AllocateContiguously<FixType>(kU_RecTotalSize);
416+
// this->fix_v_ = new FixType[kV_TotalSize]; // svd::AllocateContiguously<FixType>(kV_TotalSize);
417+
this->fix_u_cur_.resize(kU_CurTotalSize);
418+
this->fix_u_rec_.resize(kU_RecTotalSize);
419+
this->fix_v_.resize(kV_TotalSize);
407420
this->u_cur_size_ = kU_CurTotalSize;
408421
this->u_rec_size_ = kU_RecTotalSize;
409422
this->v_size_ = kV_TotalSize;
410423
this->s_size_ = kS_TotalSize;
411-
this->fix_nz_u_ = svd::AllocateContiguously<ap_uint<NumTilesU> >(kS_TotalSize);
412-
this->fix_nz_v_ = svd::AllocateContiguously<ap_uint<NumTilesV> >(kS_TotalSize);
424+
// this->fix_nz_u_ = new ap_uint<NumTilesU>[kS_TotalSize]; // svd::AllocateContiguously<ap_uint<NumTilesU> >(kS_TotalSize);
425+
// this->fix_nz_v_ = new ap_uint<NumTilesV>[kS_TotalSize]; // svd::AllocateContiguously<ap_uint<NumTilesV> >(kS_TotalSize);
426+
this->fix_nz_u_.resize(kS_TotalSize);
427+
this->fix_nz_v_.resize(kS_TotalSize);
413428
std::cout << "kS_TotalSize: " << kS_TotalSize << std::endl;
414429
std::cout << "kS_TotalSize / 8: " << kS_TotalSize / 8 << std::endl;
415430
// NOTE: the following arrangement is: (R, E, G)
@@ -424,14 +439,14 @@ class AcceleratorBlob {
424439
this->cur_gates_["f"]->get_u()->fix_pruned_data(),
425440
this->cur_gates_["c"]->get_u()->fix_pruned_data(),
426441
this->cur_gates_["o"]->get_u()->fix_pruned_data(),
427-
this->fix_u_cur_);
442+
this->fix_u_cur_.data());
428443
std::cout << "setting ArrangeWeights" << std::endl;
429444
svd::ArrangeWeights(kArrangementTypeREG, refinement_steps, kU_RecLengthPruned,
430445
this->rec_gates_["i"]->get_u()->fix_pruned_data(),
431446
this->rec_gates_["f"]->get_u()->fix_pruned_data(),
432447
this->rec_gates_["c"]->get_u()->fix_pruned_data(),
433448
this->rec_gates_["o"]->get_u()->fix_pruned_data(),
434-
this->fix_u_rec_);
449+
this->fix_u_rec_.data());
435450
std::cout << "setting ArrangeWeights S" << std::endl;
436451
svd::ArrangeWeights(kArrangementTypeREG, refinement_steps, kV_LengthPruned,
437452
kV_LengthPruned,
@@ -443,7 +458,7 @@ class AcceleratorBlob {
443458
this->rec_gates_["f"]->get_v()->fix_pruned_data(),
444459
this->rec_gates_["c"]->get_v()->fix_pruned_data(),
445460
this->rec_gates_["o"]->get_v()->fix_pruned_data(),
446-
this->fix_v_);
461+
this->fix_v_.data());
447462
std::cout << "setting ArrangeWeights NZu" << std::endl;
448463
svd::ArrangeWeights(kArrangementTypeRGE, refinement_steps, 1, 1,
449464
this->cur_gates_["i"]->get_u()->get_fix_nz_idx(),
@@ -454,7 +469,7 @@ class AcceleratorBlob {
454469
this->rec_gates_["f"]->get_u()->get_fix_nz_idx(),
455470
this->rec_gates_["c"]->get_u()->get_fix_nz_idx(),
456471
this->rec_gates_["o"]->get_u()->get_fix_nz_idx(),
457-
this->fix_nz_u_);
472+
this->fix_nz_u_.data());
458473
std::cout << "setting ArrangeWeights NZv" << std::endl;
459474
svd::ArrangeWeights(kArrangementTypeRGE, refinement_steps, 1, 1,
460475
this->cur_gates_["i"]->get_v()->get_fix_nz_idx(),
@@ -465,15 +480,15 @@ class AcceleratorBlob {
465480
this->rec_gates_["f"]->get_v()->get_fix_nz_idx(),
466481
this->rec_gates_["c"]->get_v()->get_fix_nz_idx(),
467482
this->rec_gates_["o"]->get_v()->get_fix_nz_idx(),
468-
this->fix_nz_v_);
469-
this->fix_x_.resize(num_inputs);
470-
this->fix_h_.resize(num_inputs);
471-
this->fix_c_.resize(num_inputs);
472-
this->fix_h_curr_.resize(num_inputs);
473-
this->fix_c_curr_.resize(num_inputs);
474-
this->fix_h_prev_.resize(num_inputs);
475-
this->fix_c_prev_.resize(num_inputs);
476-
this->fix_bias_.resize(num_inputs);
483+
this->fix_nz_v_.data());
484+
this->fix_x_.resize(num_inputs, std::vector<FixType>(this->lstm_input_size_));
485+
this->fix_h_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
486+
this->fix_c_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
487+
this->fix_h_curr_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
488+
this->fix_c_curr_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
489+
this->fix_h_prev_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
490+
this->fix_c_prev_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
491+
this->fix_bias_.resize(num_inputs, std::vector<FixType>(this->lstm_output_size_));
477492
this->x_.resize(num_inputs, std::vector<FloatType>(this->lstm_input_size_));
478493
this->h_.resize(num_inputs, std::vector<FloatType>(this->lstm_output_size_));
479494
this->c_.resize(num_inputs, std::vector<FloatType>(this->lstm_output_size_));
@@ -491,9 +506,12 @@ class AcceleratorBlob {
491506
this->InitVector(!init_random, num_inputs, this->lstm_output_size_, this->fix_h_prev_, this->h_prev_);
492507
this->InitVector(!init_random, num_inputs, this->lstm_output_size_, this->fix_c_prev_, this->c_prev_);
493508
this->InitVector(init_random, num_inputs, kNumGates / 2 * this->lstm_output_size_, this->fix_bias_, this->bias_);
494-
for (int i = 0; i < num_inputs; ++i) {
495-
this->fix_s_.push_back(svd::AllocateContiguously<FixType>(kS_TotalSize));
496-
}
509+
// for (int i = 0; i < num_inputs; ++i) {
510+
// this->fix_s_[i] = new FixType[kS_TotalSize]; // svd::AllocateContiguously<FixType>(kS_TotalSize);
511+
// }
512+
this->fix_s_.resize(num_inputs, std::vector<FixType>(kS_TotalSize));
513+
514+
497515
int idx = 0;
498516
for (int i = 0; i < num_inputs; ++i) {
499517
for (int j = 0; j < refinement_steps; ++j) {
@@ -511,28 +529,32 @@ class AcceleratorBlob {
511529
}
512530

513531
~AcceleratorBlob() {
514-
svd::FreeContiguously(this->fix_u_cur_);
515-
svd::FreeContiguously(this->fix_u_rec_);
516-
svd::FreeContiguously(this->fix_v_);
517-
for (int i = 0; i < this->lstm_num_inputs_; ++i) {
518-
svd::FreeContiguously(this->fix_s_[i]);
519-
svd::FreeContiguously(this->fix_x_[i]);
520-
svd::FreeContiguously(this->fix_h_[i]);
521-
svd::FreeContiguously(this->fix_c_[i]);
522-
svd::FreeContiguously(this->fix_h_curr_[i]);
523-
svd::FreeContiguously(this->fix_c_curr_[i]);
524-
svd::FreeContiguously(this->fix_h_prev_[i]);
525-
svd::FreeContiguously(this->fix_c_prev_[i]);
526-
svd::FreeContiguously(this->fix_bias_[i]);
527-
}
528-
svd::FreeContiguously(this->fix_nz_u_);
529-
svd::FreeContiguously(this->fix_nz_v_);
532+
std::cout << "[INFO] Starting ~AcceleratorBlob()." << std::endl;
533+
// delete[] this->fix_nz_u_; // FREE(this->fix_nz_u_);
534+
// delete[] this->fix_nz_v_; // FREE(this->fix_nz_v_);
535+
std::cout << "[INFO] Freed this->fix_nz_u_ and this->fix_nz_v_." << std::endl;
536+
// for (int i = 0; i < this->lstm_num_inputs_; ++i) {
537+
// delete[] this->fix_x_[i]; // FREE(this->fix_x_[i]);
538+
// delete[] this->fix_h_[i]; // FREE(this->fix_h_[i]);
539+
// delete[] this->fix_c_[i]; // FREE(this->fix_c_[i]);
540+
// delete[] this->fix_h_curr_[i]; // FREE(this->fix_h_curr_[i]);
541+
// delete[] this->fix_c_curr_[i]; // FREE(this->fix_c_curr_[i]);
542+
// delete[] this->fix_h_prev_[i]; // FREE(this->fix_h_prev_[i]);
543+
// delete[] this->fix_c_prev_[i]; // FREE(this->fix_c_prev_[i]);
544+
// delete[] this->fix_bias_[i]; // FREE(this->fix_bias_[i]);
545+
// delete[] this->fix_s_[i]; // FREE(this->fix_s_[i]);
546+
// }
547+
// delete[] this->fix_u_cur_; // FREE(this->fix_u_cur_);
548+
// delete[] this->fix_u_rec_; // FREE(this->fix_u_rec_);
549+
// delete[] this->fix_v_; // FREE(this->fix_v_);
550+
std::cout << "[INFO] Freed this->fix_u_cur_, this->fix_u_rec_ and this->fix_v_." << std::endl;
530551
for (auto g : this->cur_gates_) {
531552
delete g.second;
532553
}
533554
for (auto g : this->rec_gates_) {
534555
delete g.second;
535556
}
557+
std::cout << "[INFO] ~AcceleratorBlob() completed." << std::endl;
536558
}
537559

538560
void ResetLstmOutputs() {
@@ -564,19 +586,19 @@ class AcceleratorBlob {
564586

565587

566588
FixType* get_fix_u_cur() {
567-
return this->fix_u_cur_;
589+
return this->fix_u_cur_.data();
568590
}
569591

570592
FixType* get_fix_u_rec() {
571-
return this->fix_u_rec_;
593+
return this->fix_u_rec_.data();
572594
}
573595

574596
FixType* get_fix_v() {
575-
return this->fix_v_;
597+
return this->fix_v_.data();
576598
}
577599

578600
FixType* get_fix_s(const int i) {
579-
return this->fix_s_[i];
601+
return this->fix_s_[i].data();
580602
}
581603

582604
std::unordered_map<std::string, SvdVecType*> get_cur_gates() {
@@ -596,51 +618,51 @@ class AcceleratorBlob {
596618
}
597619

598620
FixType* get_fix_x(const int i) {
599-
return this->fix_x_[i];
621+
return this->fix_x_[i].data();
600622
}
601623

602624
FixType* get_fix_h(const int i) {
603-
return this->fix_h_[i];
625+
return this->fix_h_[i].data();
604626
}
605627

606628
FloatType* get_h(const int i) {
607629
return this->h_[i].data();
608630
}
609631

610632
FixType* get_fix_h_curr(const int i) {
611-
return this->fix_h_curr_[i];
633+
return this->fix_h_curr_[i].data();
612634
}
613635

614636
FixType* get_fix_h_prev(const int i) {
615-
return this->fix_h_prev_[i];
637+
return this->fix_h_prev_[i].data();
616638
}
617639

618640
FixType* get_fix_c(const int i) {
619-
return this->fix_c_[i];
641+
return this->fix_c_[i].data();
620642
}
621643

622644
FixType* get_fix_c_curr(const int i) {
623-
return this->fix_c_curr_[i];
645+
return this->fix_c_curr_[i].data();
624646
}
625647

626648
FixType* get_fix_c_prev(const int i) {
627-
return this->fix_c_prev_[i];
649+
return this->fix_c_prev_[i].data();
628650
}
629651

630652
FixType* get_fix_bias(const int i) {
631-
return this->fix_bias_[i];
653+
return this->fix_bias_[i].data();
632654
}
633655

634656
FloatType* get_bias(const int i) {
635657
return this->bias_[i].data();
636658
}
637659

638660
ap_uint<NumTilesU>* get_fix_nz_u() {
639-
return this->fix_nz_u_;
661+
return this->fix_nz_u_.data();
640662
}
641663

642664
ap_uint<NumTilesV>* get_fix_nz_v() {
643-
return this->fix_nz_v_;
665+
return this->fix_nz_v_.data();
644666
}
645667

646668
int get_u_cur_size() {

include/math_utils/data_handler.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ T* AllocateContiguously(const int size) {
6464
}
6565

6666
template <typename T>
67-
void FreeContiguously(T* x) {
68-
FREE(x);
67+
void FreeContiguously(T** x) {
68+
#ifdef SDS_DESIGN
69+
std::cout << "[INFO] Calling FreeContiguously sds_free()." << std::endl;
70+
#else
71+
std::cout << "[INFO] Calling FreeContiguously free()." << std::endl;
72+
#endif
73+
FREE(*x);
74+
*x = nullptr;
6975
}
7076

7177
template<typename FloatType, typename FixType, int NumTiles = 1>
@@ -107,6 +113,8 @@ class VectorBlob {
107113
for (int i = 0; i < refinement_steps; ++i) {
108114
this->fix_nz_idx_.push_back(~IdxType(0));
109115
this->fix_z_idx_.push_back(~IdxType(0));
116+
this->nz_idx_.push_back(-1);
117+
this->z_idx_.push_back(-1);
110118
}
111119
if (num_zero_tiles > 0) {
112120
for (int i = 0; i < refinement_steps; ++i) {
@@ -152,6 +160,7 @@ class VectorBlob {
152160
this->fix_data_.push_back(FixType(tmp));
153161
this->fix_pruned_data_.push_back(FixType(tmp));
154162
}
163+
std::cout << "this->data_.size(): " << this->data_.size() << std::endl;
155164
}
156165
}
157166

@@ -195,7 +204,7 @@ class VectorBlob {
195204
}
196205

197206
int get_z_idx(const int i) {
198-
return this->z_idx_[i];
207+
return this->z_idx_.at(i);
199208
}
200209

201210

@@ -209,11 +218,11 @@ class VectorBlob {
209218
}
210219

211220
int get_nz_idx(const int i) {
212-
return this->nz_idx_[i];
221+
return this->nz_idx_.at(i);
213222
}
214223

215224
int get_nz_idx(const int r, const int t) {
216-
return this->nz_idx_[r * this->num_tiles_ + t];
225+
return this->nz_idx_.at(r * this->num_tiles_ + t);
217226
}
218227

219228
IdxType* get_fix_z_idx() {
@@ -229,7 +238,7 @@ class VectorBlob {
229238
}
230239

231240
IdxType get_fix_nz_idx(const int refinement_step) {
232-
return this->fix_nz_idx_[refinement_step];
241+
return this->fix_nz_idx_.at(refinement_step);
233242
}
234243

235244
int get_refinement_steps() {

0 commit comments

Comments
 (0)