From d05c182e93194787000659ad0d53e408795c4171 Mon Sep 17 00:00:00 2001 From: fengjiayi Date: Sun, 5 Nov 2017 14:59:54 -0800 Subject: [PATCH] Add LoD's slice and append function (#5368) * Add GetFineGrainedLoDLength and AppendLoD * Follow comments and fix bugs * fix a compile error * fix a compile bug --- paddle/framework/lod_tensor.cc | 38 ++++++++++++++++++++++++++ paddle/framework/lod_tensor.h | 6 +++++ paddle/framework/lod_tensor_test.cc | 42 +++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/paddle/framework/lod_tensor.cc b/paddle/framework/lod_tensor.cc index 584308a53..2bcfffb13 100644 --- a/paddle/framework/lod_tensor.cc +++ b/paddle/framework/lod_tensor.cc @@ -135,5 +135,43 @@ void LoDTensor::ShrinkInLevel(size_t level, size_t elem_begin, PADDLE_ENFORCE_LT(begin, end, "Cannot shrink, the result tensor is empty."); ShareDataWith(Slice(begin, end)); } + +void GetFineGrainedLoDLength(const LoD& lod, size_t start_idx, size_t end_idx, + std::vector>* lod_length, + size_t* start_offset) { + lod_length->clear(); + PADDLE_ENFORCE(start_idx < lod.size() - 1, + "start_idx should be >= 0 and < lod.size() - 1."); + PADDLE_ENFORCE(end_idx < lod.size(), + "end_idx should be >= 0 and < lod.size()."); + PADDLE_ENFORCE_LE(start_idx, end_idx, + "start_idx should be less than end_idx."); + for (size_t level_idx = 0; level_idx < lod.size(); ++level_idx) { + std::vector level_lens; + for (size_t i = start_idx; i < end_idx; ++i) { + level_lens.push_back(lod[level_idx][i + 1] - lod[level_idx][i]); + } + lod_length->emplace_back(level_lens); + start_idx = lod[level_idx][start_idx]; + end_idx = lod[level_idx][end_idx]; + } + *start_offset = start_idx; +} + +void AppendLoD(LoD* lod, const std::vector>& lod_length) { + PADDLE_ENFORCE_EQ( + lod->size(), lod_length.size(), + "The lod_length should has the same size with the appended lod."); + for (size_t i = 0; i < lod->size(); ++i) { + auto& level = (*lod)[i]; + if (level.empty()) { + level.push_back(0); + } + for (size_t len : lod_length[i]) { + level.push_back(level.back() + len); + } + } +} + } // namespace framework } // namespace paddle diff --git a/paddle/framework/lod_tensor.h b/paddle/framework/lod_tensor.h index f4fe4cdac..1437da399 100644 --- a/paddle/framework/lod_tensor.h +++ b/paddle/framework/lod_tensor.h @@ -181,5 +181,11 @@ LoDTensor LodExpand(const LoDTensor& source, const LoD& lod, size_t level, return tensor; } +void GetFineGrainedLoDLength(const LoD& lod, size_t start_idx, size_t end_idx, + std::vector>* lod_length, + size_t* start_offset); + +void AppendLoD(LoD* lod, const std::vector>& lod_length); + } // namespace framework } // namespace paddle diff --git a/paddle/framework/lod_tensor_test.cc b/paddle/framework/lod_tensor_test.cc index aa2f6c993..bf61c9ee7 100644 --- a/paddle/framework/lod_tensor_test.cc +++ b/paddle/framework/lod_tensor_test.cc @@ -144,5 +144,47 @@ TEST(LodExpand, test) { } } +TEST(LoD, GetFineGrainedLoDLength) { + LoD lod; + lod.push_back(std::vector{0, 2, 4, 5}); + lod.push_back(std::vector{0, 1, 6, 8, 10, 11}); + lod.push_back( + std::vector{0, 2, 5, 7, 10, 12, 15, 17, 20, 24, 26, 29}); + + std::vector> lod_length; + size_t start_offset; + paddle::framework::GetFineGrainedLoDLength(lod, 1, 2, &lod_length, + &start_offset); + + std::vector> expected; + expected.push_back(std::vector{2}); + expected.push_back(std::vector{2, 2}); + expected.push_back(std::vector{2, 3, 4, 2}); + EXPECT_EQ(lod_length, expected); + EXPECT_EQ(start_offset, 15UL); +} + +TEST(LoD, AppendLoD) { + std::vector> lod_lens; + lod_lens.push_back(std::vector{2}); + lod_lens.push_back(std::vector{2, 2}); + lod_lens.push_back(std::vector{2, 3, 4, 2}); + + LoD origin; + origin.push_back(std::vector{0, 2}); + origin.push_back(std::vector{0, 1, 6}); + origin.push_back(std::vector{0, 2, 5, 7, 10, 12, 15}); + + paddle::framework::AppendLoD(&origin, lod_lens); + + LoD expected; + expected.push_back(std::vector{0, 2, 4}); + expected.push_back(std::vector{0, 1, 6, 8, 10}); + expected.push_back( + std::vector{0, 2, 5, 7, 10, 12, 15, 17, 20, 24, 26}); + + EXPECT_EQ(origin, expected); +} + } // namespace framework } // namespace paddle -- GitLab