From 6b115251ceb5bd991aa38005f95f3c9ba43a4cc4 Mon Sep 17 00:00:00 2001 From: Kexin Zhao Date: Thu, 14 Jun 2018 16:28:03 -0700 Subject: [PATCH] address comments --- paddle/fluid/framework/lod_tensor.cc | 4 ++- paddle/fluid/pybind/pybind.cc | 43 +++++++++++++++++++--------- python/paddle/fluid/data_feeder.py | 2 -- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/paddle/fluid/framework/lod_tensor.cc b/paddle/fluid/framework/lod_tensor.cc index db9e68d8ef86b..e331c8128f2e8 100644 --- a/paddle/fluid/framework/lod_tensor.cc +++ b/paddle/fluid/framework/lod_tensor.cc @@ -415,7 +415,9 @@ LoD ConvertToLengthBasedLoD(const LoD &offset_lod) { length_lod.reserve(offset_lod.size()); for (size_t lvl = 0; lvl < offset_lod.size(); ++lvl) { std::vector level; - level.reserve(offset_lod[lvl].size() - 1); + if (offset_lod[lvl].size() > 0) { + level.reserve(offset_lod[lvl].size() - 1); + } for (size_t idx = 0; idx < offset_lod[lvl].size() - 1; ++idx) { level.push_back(offset_lod[lvl][idx + 1] - offset_lod[lvl][idx]); } diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index a692ef27d44e0..79555cfd698d4 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -144,33 +144,48 @@ PYBIND11_PLUGIN(core) { py::class_(m, "LoDTensor") .def_buffer( [](Tensor &self) -> py::buffer_info { return CastToPyBuffer(self); }) - .def( - "__init__", - [](LoDTensor &instance, const std::vector> &lod) { - LoD new_lod; - new_lod.reserve(lod.size()); - std::copy(lod.begin(), lod.end(), std::back_inserter(new_lod)); - new (&instance) LoDTensor(ConvertToOffsetBasedLoD(new_lod)); - }) + .def("__init__", + [](LoDTensor &instance, const std::vector> + &recursive_sequence_lengths) { + LoD new_lod; + new_lod.reserve(recursive_sequence_lengths.size()); + std::copy(recursive_sequence_lengths.begin(), + recursive_sequence_lengths.end(), + std::back_inserter(new_lod)); + LoD new_offset_lod = ConvertToOffsetBasedLoD(new_lod); + PADDLE_ENFORCE(CheckLoD(new_offset_lod, -1), + "the provided lod info is invalid"); + new (&instance) LoDTensor(new_offset_lod); + }) .def("__init__", [](LoDTensor &instance) { new (&instance) LoDTensor(); }) .def("set_lod", [](LoDTensor &self, const std::vector> &lod) { - // the input lod info is offset-based + // the input lod is offset-based level-of-detail info LOG(WARNING) << "set_lod is deprecated and will be removed by 9.2018, " "please switch to set_recursive_sequence_lengths."; LoD new_lod; new_lod.reserve(lod.size()); std::copy(lod.begin(), lod.end(), std::back_inserter(new_lod)); + PADDLE_ENFORCE(CheckLoD(new_lod, vectorize(self.dims()).front()), + "the provided lod info is invalid"); self.set_lod(new_lod); }) .def("set_recursive_sequence_lengths", - [](LoDTensor &self, const std::vector> &lod) { - // the input lod info is length-based + [](LoDTensor &self, const std::vector> + &recursive_sequence_lengths) { + // the input recursive_sequence_lengths is length-based + // level-of-detail info LoD new_lod; - new_lod.reserve(lod.size()); - std::copy(lod.begin(), lod.end(), std::back_inserter(new_lod)); - self.set_lod(ConvertToOffsetBasedLoD(new_lod)); + new_lod.reserve(recursive_sequence_lengths.size()); + std::copy(recursive_sequence_lengths.begin(), + recursive_sequence_lengths.end(), + std::back_inserter(new_lod)); + LoD new_offset_lod = ConvertToOffsetBasedLoD(new_lod); + PADDLE_ENFORCE( + CheckLoD(new_offset_lod, vectorize(self.dims()).front()), + "the provided lod info is invalid"); + self.set_lod(new_offset_lod); }) .def("lod", [](LoDTensor &self) -> std::vector> { diff --git a/python/paddle/fluid/data_feeder.py b/python/paddle/fluid/data_feeder.py index 9785b37e65397..ac396002018d5 100644 --- a/python/paddle/fluid/data_feeder.py +++ b/python/paddle/fluid/data_feeder.py @@ -66,8 +66,6 @@ def done(self): t.set(arr, self.place) if self.lod_level > 0: t.set_recursive_sequence_lengths(self.lod) - assert t.has_valid_recursive_sequence_lengths( - ), "the provided lod info is invalid" return t