diff --git a/paddle/fluid/framework/lod_tensor.cc b/paddle/fluid/framework/lod_tensor.cc index 1e8f6c42d1..8fbbc6584e 100644 --- a/paddle/fluid/framework/lod_tensor.cc +++ b/paddle/fluid/framework/lod_tensor.cc @@ -157,17 +157,9 @@ bool CheckLoD(const LoD &in, int tensor_height) { if (level.size() < 2) return false; // check: the first offset(the begin offset) of each level should be 0. if (level.front() != 0) return false; - // check: all the offsets in a level should be ascending(no same items - // allows). - auto beg = level.begin(); - auto end = level.end(); - // Do not use std::is_sorted, because we need strictly sorted lod - if (beg != end) { - for (auto it = beg + 1; it != end; ++it) { - if (*(it - 1) >= *it) { - return false; - } - } + // check: all the offsets in a level should be ascending(allow same items) + if (!std::is_sorted(level.begin(), level.end())) { + return false; } } // check: the lowest level's last offset should equals `tensor_height` if diff --git a/paddle/fluid/framework/lod_tensor_test.cc b/paddle/fluid/framework/lod_tensor_test.cc index 838b174343..15928c18d3 100644 --- a/paddle/fluid/framework/lod_tensor_test.cc +++ b/paddle/fluid/framework/lod_tensor_test.cc @@ -218,9 +218,10 @@ TEST(LoD, CheckLoD) { ASSERT_TRUE(CheckLoD(relative_lod, 5)); ASSERT_FALSE(CheckLoD(relative_lod, 9)); - // check strictly sorted lod + // check whether lod is ascending-sorted (allow same items) ASSERT_TRUE(CheckLoD({{0, 1, 2, 3, 4, 5}}, 5)); - ASSERT_FALSE(CheckLoD({{0, 1, 3, 3, 4, 5}}, 5)); + ASSERT_TRUE(CheckLoD({{0, 1, 3, 3, 4, 5}}, 5)); + ASSERT_FALSE(CheckLoD({{0, 1, 3, 2, 5}}, 5)); } TEST(LoD, CheckAbsLoD) {