LodTensor lod含义的疑问
在sequence_expand的例子中有一个
x is a LoDTensor:
x.lod = [[0, 2, 4]]
x.data = [[a], [b], [c], [d]]
x.dims = [4, 1]
不太理解这儿的lod = [[[0,2,4]]是什么意思,它对应的batch每个input是什么样的呢?我理解的不应该是batch里每个input的sequence长度吗?像设计文档中提到的。
3 1 2
||| | ||
0,2,4 是存储的相邻下一层的 offset.
如果是一层序列,序列长度分别为 1,2,3,那存储的offset就是 0, (0+1), (0+1+2), (0+1+2+3),也就是 0, 1, 2, 6 ; 文档里的是比这个简单的例子。
那也就是说,这个batch一共有2个input。分别是[a],[b]、[c][d]??
对,这个batch有3个序列。
1、那么x的lod_level是1?? 2、另外对于这个也不太理解。这个LodTensor没有任何数据?而[0, 2, 4],[0, 3, 6, 7, 8]分别指的是啥呢?
y is a LoDTensor:
y.lod = [[0, 2, 4],
[0, 3, 6, 7, 8]]
我举的例子 level=1
上面的y level = 2,所谓level是序列层次.
y.lod = [[0, 2, 4],
[0, 3, 6, 7, 8]]
这里包含的序列含义是
- 最低一层的 offset 是
[0, 3, 6, 7, 8] - 上一层是
[0, 2, 4]
先看最低一层:
表示这个一层包含了长度分别为 3-0, 6-3, 7-6, 8-7 也就是 3, 3, 1, 1 的4个序列
再看高一层, 高一层的offset表示的是第一层的偏移,也就是包含了低一层 2-0, 4-2 也就是 2, 2 个序列
画成图

层次的意思,比如NLP里面一个语料可能有 4 层的 LoD
- 文集
- 书
- 章节
- 句子
其中,文集包括多本书,书包括多个章节,章节包括多个句子
理论上LoD可以存无限多层,任意两层的关系如上面y.lod例子里那样。
多谢~~解释的非常详细,我还有一点疑问。
def create_lod_tensor(data, lod, place):
res = fluid.LoDTensor()
res.set(data, place)
res.set_lod(lod)
return res
x = fluid.layers.data(name='x', shape=[1], dtype='float32', lod_level=0)
y = fluid.layers.data(name='y', shape=[5], dtype='float32', lod_level=1)
out = fluid.layers.sequence_expand(x=x, y=y, ref_level=0)
place = fluid.CPUPlace()
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
x_d = create_lod_tensor(np.array([[0], [1], [2], [3]]), [[0, 2, 4]], place)
y_d = create_lod_tensor(np.array([[0]]), [[0, 2, 4], [0, 3, 6, 7, 8]], place)
results = exe.run(fluid.default_main_program(),
feed={'x':x_d, 'y': y_d },
fetch_list=[out],return_numpy=False)
这里y_d是一个lod_level为2的数据,但其data部分只有[[0]],也就是说lodtensor的数据部分和offset部分不必是对应的?
是对应的。
最低一层的offsets表示的是data,比如
这里 level-1 里的offset直接表示data,对应的data也有8个元素:
| | | | | | | |
序列长度分别为 3, 3, 1, 1
如果是必须对应。那y_d的最低一层lod是[0, 3, 6, 7, 8]。则其对应的data至少应该有8个长度,这里
y_d = create_lod_tensor(np.array([[0]]), [[0, 2, 4], [0, 3, 6, 7, 8]], place)
只给了一个np.array([[0]])。这样也是合法的?
对,那应该是不合理的。 从原理上讲的话
上面的示意图很直观,但level的标注可能会引起误解?重新标了一下:

Saving offsets in LoDTensor is an implementation-oriented decision; it makes it difficult to understand the idea behind LoDTensor.
To better grasp the idea, please follow my design doc: https://github.com/PaddlePaddle/Paddle/blob/develop/doc/fluid/design/concepts/lod_tensor.md, which starts from explaining the idea, assuming that LoD contains "lengths", instead of "offsets", of sub-sequences, before it goes deeper into the implementation.
The key problem posed by @seanxh is that we SHOULD NOT HAVE exposed the implementation via our Python API. @kexinzhao has been working on exposing "lengths" instead of "offset" in our Python API.
As is mentioned by @wangkuiyi, currently our LoD is both implemented in C++ and exposed in Python using "offsets".
We want to encapsulate the offset implementation detail in C++ and only expose length based interface in Python.
The LoDTensor class defined here is wrapped and exposed to Python as the class of the same name using Pybind: https://github.com/PaddlePaddle/Paddle/blob/c79ec9f0026387001a13ae43e728f8902301630b/paddle/fluid/pybind/pybind.cc#L141-L166
There will be an effort to modify the definition of the set_lod() and lod() so that user on the Python side can interact with LoDTensor using length-based LoD.
Also we have provided some utility function here to create LoDTensor from Numpy array, a list or a existing LoDTensor, by providing length-based LoD info.
Below is an example of how to use the utility function to obtain LoDTensor: https://github.com/PaddlePaddle/Paddle/blob/c79ec9f0026387001a13ae43e728f8902301630b/python/paddle/fluid/tests/book/high-level-api/recommender_system/test_recommender_system_newapi.py#L208-L223
如果是必须对应。那y_d的最低一层lod是[0, 3, 6, 7, 8]。则其对应的data至少应该有8个长度,这里
y_d = create_lod_tensor(np.array([[0]]), [[0, 2, 4], [0, 3, 6, 7, 8]], place) 只给了一个np.array([[0]])。这样也是合法的?
对,那应该是不合理的。 从原理上讲的话
@Superjomn 如果不合理,那这个LoDTensor里到底存了什么东西呢?用的时候会发生什么呢
另外
x is a LoDTensor:
x.lod = [[0, 2, 4]]
x.data = [[a], [b], [c], [d]]
x.dims = [4, 1]
如果这个batch有两个input,为什么dims是[4, 1]呢?还存在batch dim吗
啊,我大概理解现在版本的create_lod_tensor的含义了:
recursive_seq_lens表示的是每个层次的嵌套维度:
假设是2篇文档,
第一篇有2个句子,句子分别长度是3,2
第二篇有1个句子,句子长度是4;
那么这个recursive_seq_lens = [[2, 1], [3, 2, 4]]
上面也就是lod-level=2的情况:其中 [3, 2, 4]是句子的长度,对应到设计文档的第二层;[2, 1]是句子的个数(可以当做文档的长度),对应到设计文档的第一层。
所以这个recursive_seq_lens 永远是2层嵌套列表,也就是列表的列表,外层列表的size表示嵌套的个数,也就是lod-level的大小;内部的每个列表,对应表示每个lod-level下,每个元素的大小。 在feed的条件下,len( recursive_seq_lens[0] ) 就是 batch size;sum( recursive_seq_lens[-1] ) 表示最基础元素(一般是词的id)的个数,这个值应该和 data.shape[0]一致,且data.shape[1] = 1;