-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change lod tensor to absolute offsets #4952
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,8 @@ class LoDTensorTester : public ::testing::Test { | |
// 0 5 10 15 20 | ||
// 0 2 5 7 10 12 15 20 | ||
LoD lod; | ||
lod.push_back(std::vector<size_t>{0, 10, 20}); | ||
lod.push_back(std::vector<size_t>{0, 5, 10, 15, 20}); | ||
lod.push_back(std::vector<size_t>{0, 2, 3}); | ||
lod.push_back(std::vector<size_t>{0, 2, 5, 8}); | ||
lod.push_back(std::vector<size_t>{0, 2, 5, 7, 10, 12, 15, 17, 20}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 请问这里33行,能从35行直接来么?就是除了最底层的lod,上层的lod都直接根据最底层的来算,而不是根据下一层来算。这样要是取某一层,只需要依赖两层,而不是依赖这一层往下的所有层。
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 应该会有问题,那样应该是和之前一样,不能表示空序列。 空序列会在beam search里用,beam search的每个 time step 的候选集(当成变长序列)和翻译结果都会用 LoDTensor 存,如果有空的候选集,也需要存储。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为这个PR中,第二层是直接根据最底层的相对位置的来算。那么如果第二层的计算没问题的话,最顶层直接根据最底层的相对位置来算,为什么不能表示空序列呢?
4,是35行 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 也可以,但比较trick。 现在的方案,相对偏移从上往下是统一的,而且最底层也是相对偏移,但因为tensor自身instance认为长度为1,所以绝对偏移和相对偏移在最底层表达一致。 这个概念是统一的,不需要绝对和相对偏移的混用。 感觉相比于矩阵的计算,这个耗时不算太大,到后面成为瓶颈的时候再考虑优化吧。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好的。而且 @qingqing01 说目前这样表示,很容易拿到每层的句子数,比如 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用 |
||
|
||
ASSERT_EQ(lod.size(), 3UL); | ||
|
@@ -52,14 +52,14 @@ TEST_F(LoDTensorTester, NumLevels) { ASSERT_EQ(lod_tensor_.NumLevels(), 3UL); } | |
|
||
TEST_F(LoDTensorTester, NumElements) { | ||
ASSERT_EQ(lod_tensor_.NumElements(0), 2UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(1), 4UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(1), 3UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(2), 8UL); | ||
} | ||
|
||
TEST_F(LoDTensorTester, NumElements2) { | ||
ASSERT_EQ(lod_tensor_.NumElements(0, 0), 2UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(0, 1), 2UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(1, 1), 2UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(0, 1), 1UL); | ||
ASSERT_EQ(lod_tensor_.NumElements(1, 1), 3UL); | ||
} | ||
|
||
TEST_F(LoDTensorTester, ShrinkLevels) { | ||
|
@@ -68,37 +68,36 @@ TEST_F(LoDTensorTester, ShrinkLevels) { | |
LoDTensor new_lod_tensor = lod_tensor_; | ||
new_lod_tensor.ShrinkLevels(level, level + 1); | ||
ASSERT_EQ(new_lod_tensor.NumLevels(), 1UL); | ||
ASSERT_EQ(new_lod_tensor.NumElements(0), lod_tensor_.NumElements(level)); | ||
ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>()); | ||
} | ||
// shrink 2 level | ||
for (size_t level = 0; level < 2UL; ++level) { | ||
LoDTensor new_lod_tensor = lod_tensor_; | ||
new_lod_tensor.ShrinkLevels(level, level + 2); | ||
// the lowest level's last element should be the tensor's batch_size. | ||
ASSERT_EQ(new_lod_tensor.lod().back().back(), | ||
lod_tensor_.lod().back().back()); | ||
ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL); | ||
ASSERT_EQ(new_lod_tensor.NumElements(0), lod_tensor_.NumElements(level)); | ||
ASSERT_EQ(new_lod_tensor.NumElements(1), | ||
lod_tensor_.NumElements(level + 1)); | ||
ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>()); | ||
} | ||
} | ||
|
||
TEST_F(LoDTensorTester, ShrinkInLevel) { | ||
size_t level = 0; | ||
LoDTensor new_lod_tensor = lod_tensor_; | ||
new_lod_tensor.ShrinkInLevel(level, 0, 2); | ||
new_lod_tensor.ShrinkInLevel(level, 0, 1); | ||
EXPECT_EQ(new_lod_tensor.NumLevels(), 3UL); | ||
EXPECT_EQ(new_lod_tensor.NumElements(0), 2UL); | ||
EXPECT_EQ(new_lod_tensor.NumElements(1), 4UL); | ||
EXPECT_EQ(new_lod_tensor.NumElements(2), 8UL); | ||
EXPECT_EQ(new_lod_tensor.NumElements(0), 1UL); | ||
EXPECT_EQ(new_lod_tensor.NumElements(1), 2UL); | ||
EXPECT_EQ(new_lod_tensor.NumElements(2), 5UL); | ||
ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>()); | ||
|
||
level = 1; | ||
new_lod_tensor = lod_tensor_; | ||
new_lod_tensor.ShrinkInLevel(level, 0, 2); | ||
new_lod_tensor.ShrinkInLevel(level, 1, 2); | ||
ASSERT_EQ(new_lod_tensor.NumLevels(), 2UL); | ||
ASSERT_EQ(new_lod_tensor.NumElements(0), 2UL); | ||
ASSERT_EQ(new_lod_tensor.NumElements(1), 4UL); | ||
ASSERT_EQ(new_lod_tensor.NumElements(0), 1UL); | ||
ASSERT_EQ(new_lod_tensor.NumElements(1), 3UL); | ||
ASSERT_EQ(new_lod_tensor.data<float>(), lod_tensor_.data<float>()); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a detailed explanation here? About what's the meaning of this example, 3-level LoD.