diff --git a/lite/core/tensor.cc b/lite/core/tensor.cc index ea4b64763a1ab72c4b74456cb146707e0b80d10b..9f318693650d9ba3448f0bcec213162d763ff5d6 100644 --- a/lite/core/tensor.cc +++ b/lite/core/tensor.cc @@ -47,9 +47,11 @@ value_type DDimLite::count(int start, int end) const { DDimLite DDimLite::Slice(int start, int end) const { start = std::max(start, 0); end = std::min(end, static_cast(data_.size())); - value_type arr[kMaxDimLength]; - memcpy(arr, data_.data() + start, (end - start) * sizeof(value_type)); - return DDimLite(arr, end - start); + DDimLite new_dim(end - start); + for (int i = start; i < end; ++i) { + new_dim[i - start] = data_[i]; + } + return new_dim; } std::string DDimLite::repr() const { diff --git a/lite/core/tensor.h b/lite/core/tensor.h index 40b0970c40891728f5b2aca99633e1877d441911..5852922c01b06e253c7ca726eadd3132a53a3954 100644 --- a/lite/core/tensor.h +++ b/lite/core/tensor.h @@ -73,10 +73,7 @@ class DDimLite { DDimLite() = default; explicit DDimLite(const std::vector &x) { ConstructFrom(x); } - explicit DDimLite(const value_type *arr, size_t size) { - data_.resize(size); - memcpy(data_.data(), arr, data_.size() * sizeof(value_type)); - } + explicit DDimLite(size_t size) { data_.resize(size); } void ConstructFrom(const std::vector &x) { data_.resize(x.size()); diff --git a/lite/kernels/x86/fc_compute.h b/lite/kernels/x86/fc_compute.h index 971f5dfa2f407ac3bd87c47c02af1750eaa21acf..bf17b4778894c019ff785bd2c8f330b6ad71383d 100644 --- a/lite/kernels/x86/fc_compute.h +++ b/lite/kernels/x86/fc_compute.h @@ -83,12 +83,12 @@ class FCFunctor { // NOTE: here need to mutable_data for temporary Tensor X1 and Y1, // the overhead is unmeasured. - lite::Tensor X1; - X1.Resize({M * KK}); + Tensor X1; + X1.Resize(std::vector({M * KK})); T* X1_data = X1.mutable_data(); - lite::Tensor Y1; - Y1.Resize({M * (N + 4)}); + Tensor Y1; + Y1.Resize(std::vector({M * NN})); Y1_data = Y1.mutable_data(); auto parallel_memcpy_x = [&](int64_t begin, int64_t end) { @@ -115,7 +115,7 @@ class FCFunctor { if (!B) { auto parallel_memcpy_y = [&](int64_t begin, int64_t end) { for (int64_t i = begin; i < end; i++) { - memcpy(Y + i * N, Y1_data + i * (N + 4), N * sizeof(T)); + memcpy(Y + i * N, Y1_data + i * NN, N * sizeof(T)); } }; lite::x86::RunParallelFor(0, M, parallel_memcpy_y); diff --git a/lite/operators/fc_op.cc b/lite/operators/fc_op.cc index ad3fcf79a3c9333d5525114e7b8f6abed4ae96d3..4963ee2578deb173d94095ab0e0a8464f028e0e8 100644 --- a/lite/operators/fc_op.cc +++ b/lite/operators/fc_op.cc @@ -49,16 +49,17 @@ bool FcOpLite::CheckShape() const { } bool FcOpLite::InferShape() const { - const auto input_dims = param_.input->dims(); - const auto w_dims = param_.w->dims(); + const auto &input_dims = param_.input->dims(); + const auto &w_dims = param_.w->dims(); + int in_num_col_dims = param_.in_num_col_dims; // Set output dims - std::vector output_dims(param_.in_num_col_dims + 1, 0); - for (int i = 0; i < param_.in_num_col_dims; ++i) { + DDim output_dims(in_num_col_dims + 1); + for (int i = 0; i < in_num_col_dims; ++i) { output_dims[i] = input_dims[i]; } - output_dims.back() = w_dims[1]; - param_.output->Resize(lite::DDim(output_dims)); + output_dims[in_num_col_dims] = w_dims[1]; + param_.output->Resize(output_dims); // share LoD // param_.output->set_lod(param_.input->lod()); diff --git a/lite/operators/gru_op.cc b/lite/operators/gru_op.cc index 3ddeb5b73463ab7502e81ce82e348c08e23dc421..55935dd7a49ceb98d80fd897be4d0585ca4e35af 100644 --- a/lite/operators/gru_op.cc +++ b/lite/operators/gru_op.cc @@ -52,15 +52,17 @@ bool GRUOpLite::CheckShape() const { } bool GRUOpLite::InferShape() const { - auto input_dims = param_.input->dims(); - auto weight_dims = param_.weight->dims(); + auto &input_dims = param_.input->dims(); + auto &weight_dims = param_.weight->dims(); int frame_size = weight_dims[0]; auto batch_size = input_dims[0]; param_.batch_gate->Resize(input_dims); - param_.batch_reset_hidden_prev->Resize(lite::DDim({batch_size, frame_size})); - param_.batch_hidden->Resize(lite::DDim({batch_size, frame_size})); - param_.hidden->Resize(lite::DDim({batch_size, frame_size})); + + auto out_dims = DDim({batch_size, frame_size}); + param_.batch_reset_hidden_prev->Resize(out_dims); + param_.batch_hidden->Resize(out_dims); + param_.hidden->Resize(out_dims); *(param_.hidden->mutable_lod()) = param_.input->lod(); return true; diff --git a/lite/operators/lookup_table_op.cc b/lite/operators/lookup_table_op.cc index 46ff3153e93afa317f68a0bbf26ab61e7534a20b..98f8062294f3606542ac110e7a838a93ffeda0cf 100644 --- a/lite/operators/lookup_table_op.cc +++ b/lite/operators/lookup_table_op.cc @@ -37,17 +37,14 @@ bool LookupTableOpLite::CheckShape() const { } bool LookupTableOpLite::InferShape() const { - auto table_dims = param_.W->dims(); - auto ids_dims = param_.Ids->dims(); + auto &table_dims = param_.W->dims(); + auto &ids_dims = param_.Ids->dims(); + auto out_dims = ids_dims; int ids_rank = ids_dims.size(); + out_dims[ids_rank - 1] = table_dims[1]; - std::vector out_dims; - for (int i = 0; i < ids_rank - 1; ++i) { - out_dims.push_back(ids_dims[i]); - } - out_dims.push_back(table_dims[1]); - param_.Out->Resize(lite::DDim{out_dims}); + param_.Out->Resize(out_dims); param_.Out->set_lod(param_.Ids->lod()); return true; }