Skip to content

Commit 63bf82d

Browse files
authored
Merge branch 'develop' into add-async-listen-and-serv-op
2 parents 8081e15 + ee0497c commit 63bf82d

File tree

82 files changed

+2402
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2402
-343
lines changed

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ ENV PATH=${PATH}:${GOROOT}/bin:${GOPATH}/bin
4949
RUN curl -s -q https://glide.sh/get | sh
5050

5151
# Install TensorRT
52-
# The unnecessary files has been removed to make the library small. It only contains include and lib now.
52+
# following TensorRT.tar.gz is not the default official one, we do two miny changes:
53+
# 1. Remove the unnecessary files to make the library small. TensorRT.tar.gz only contains include and lib now,
54+
# and its size is only one-third of the official one.
55+
# 2. Manually add ~IPluginFactory() in IPluginFactory class of NvInfer.h, otherwise, it couldn't work in paddle.
56+
# See https://github.com/PaddlePaddle/Paddle/issues/10129 for details.
5357
RUN wget -qO- http://paddlepaddledeps.bj.bcebos.com/TensorRT-4.0.0.3.Ubuntu-16.04.4.x86_64-gnu.cuda-8.0.cudnn7.0.tar.gz | \
5458
tar -xz -C /usr/local && \
5559
cp -rf /usr/local/TensorRT/include /usr && \

cmake/tensorrt.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ if(TENSORRT_FOUND)
3030

3131
message(STATUS "Current TensorRT header is ${TENSORRT_INCLUDE_DIR}/NvInfer.h. "
3232
"Current TensorRT version is v${TENSORRT_MAJOR_VERSION}. ")
33+
include_directories(${TENSORRT_INCLUDE_DIR})
34+
list(APPEND EXTERNAL_LIBS ${TENSORRT_LIBRARY})
3335
endif()

doc/fluid/api/data.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
==================================
2+
Data Reader Interface and DataSets
3+
==================================
4+
5+
.. toctree::
6+
:maxdepth: 1
7+
8+
data/data_reader.rst
9+
data/image.rst
10+
data/dataset.rst

doc/fluid/api/data/data_reader.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
=====================
2+
Data Reader Interface
3+
=====================
4+
5+
6+
DataTypes
7+
=========
8+
9+
.. autofunction:: paddle.v2.data_type.dense_array
10+
:noindex:
11+
12+
.. autofunction:: paddle.v2.data_type.integer_value
13+
:noindex:
14+
15+
.. autofunction:: paddle.v2.data_type.integer_value_sequence
16+
:noindex:
17+
18+
.. autofunction:: paddle.v2.data_type.integer_value_sub_sequence
19+
:noindex:
20+
21+
.. autofunction:: paddle.v2.data_type.sparse_binary_vector
22+
:noindex:
23+
24+
.. autofunction:: paddle.v2.data_type.sparse_binary_vector_sequence
25+
:noindex:
26+
27+
.. autofunction:: paddle.v2.data_type.sparse_binary_vector_sub_sequence
28+
:noindex:
29+
30+
.. autofunction:: paddle.v2.data_type.sparse_float_vector
31+
:noindex:
32+
33+
.. autofunction:: paddle.v2.data_type.sparse_float_vector_sequence
34+
:noindex:
35+
36+
.. autofunction:: paddle.v2.data_type.sparse_float_vector_sub_sequence
37+
:noindex:
38+
39+
.. autofunction:: paddle.v2.data_type.sparse_non_value_slot
40+
:noindex:
41+
42+
.. autofunction:: paddle.v2.data_type.sparse_value_slot
43+
:noindex:
44+
45+
.. autoclass:: paddle.v2.data_type.InputType
46+
:members:
47+
:noindex:
48+
49+
DataFeeder
50+
==========
51+
52+
.. automodule:: paddle.v2.data_feeder
53+
:members:
54+
:noindex:
55+
56+
Reader
57+
======
58+
59+
.. automodule:: paddle.v2.reader
60+
:members:
61+
:noindex:
62+
63+
.. automodule:: paddle.v2.reader.creator
64+
:members:
65+
:noindex:
66+
67+
minibatch
68+
=========
69+
70+
.. automodule:: paddle.v2.minibatch
71+
:members:
72+
:noindex:

doc/fluid/api/data/dataset.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
Dataset
2+
=======
3+
4+
.. automodule:: paddle.dataset
5+
:members:
6+
:noindex:
7+
8+
mnist
9+
+++++
10+
11+
.. automodule:: paddle.dataset.mnist
12+
:members:
13+
:noindex:
14+
15+
cifar
16+
+++++
17+
18+
.. automodule:: paddle.dataset.cifar
19+
:members:
20+
:noindex:
21+
22+
conll05
23+
+++++++
24+
25+
.. automodule:: paddle.dataset.conll05
26+
:members: get_dict,get_embedding,test
27+
:noindex:
28+
29+
imdb
30+
++++
31+
32+
.. automodule:: paddle.dataset.imdb
33+
:members:
34+
:noindex:
35+
36+
imikolov
37+
++++++++
38+
39+
.. automodule:: paddle.dataset.imikolov
40+
:members:
41+
:noindex:
42+
43+
movielens
44+
+++++++++
45+
46+
.. automodule:: paddle.dataset.movielens
47+
:members:
48+
:noindex:
49+
50+
.. autoclass:: paddle.dataset.movielens.MovieInfo
51+
:noindex:
52+
53+
.. autoclass:: paddle.dataset.movielens.UserInfo
54+
:noindex:
55+
56+
sentiment
57+
+++++++++
58+
59+
.. automodule:: paddle.dataset.sentiment
60+
:members:
61+
:noindex:
62+
63+
uci_housing
64+
+++++++++++
65+
66+
.. automodule:: paddle.dataset.uci_housing
67+
:members:
68+
:noindex:
69+
70+
wmt14
71+
+++++
72+
73+
.. automodule:: paddle.dataset.wmt14
74+
:members:
75+
:noindex:
76+
77+
wmt16
78+
+++++
79+
80+
.. automodule:: paddle.dataset.wmt16
81+
:members:
82+
:noindex:

doc/fluid/api/data/image.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Image Interface
2+
===============
3+
4+
.. automodule:: paddle.v2.image
5+
:members:

doc/fluid/api/index_en.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Fluid
1616
profiler.rst
1717
regularizer.rst
1818
io.rst
19+
data.rst
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Varient Length supported RNN Design
2+
For the learning of variable length sequences, the existing mainstream frameworks such as tensorflow, pytorch, caffe2, mxnet and so on all use padding.
3+
4+
Different-length sequences in a mini-batch will be padded with zeros and transformed to same length.
5+
6+
The existing RNN implementations of the PaddlePaddle is `RecurrentLayerGroup`,
7+
which supports the variable length sequences without padding.
8+
This doc will design fluid's RNN based on this idea.
9+
10+
## Multi-layer sequence data format `LODTensor`
11+
At present, Paddle stores data in one mini-batch in one-dimensional array.
12+
13+
`Argument.sequenceStartPositions` is used to store information for each sentence.
14+
15+
In Paddle, `Argument.subSequenceStartPositions` is used to store 2 levels of sequence information, while higher dimensional sequences can not be supported.
16+
17+
In order to support the storage of `N-level` sequences, we define sequence information as the following data structure.
18+
19+
20+
```c++
21+
std::shared_ptr<std::vector<std::vector<int>>> lod_start_pos_;
22+
```
23+
24+
Or more clearly defined here
25+
26+
```c++
27+
typedef std::vector<int> level_t;
28+
std::vector<level_t> lod_start_pos;
29+
```
30+
Each `level_t` here stores a level of offset information consistent with paddle's current practice.
31+
32+
In order to transmit sequence information more transparently, we have introduced a new tensor called `LODTensor`[1].
33+
Its tensor-related interfaces all inherit directly from `Tensor`, but it also adds serial-related interfaces.
34+
Thus, when working with a `LODTensor`, ordinary `Op` is used directly as `Tensor`.
35+
The `Op` of the operation sequence will additionally operate the relevant interface of the `LODTensor` variable-length sequence operation.
36+
37+
The definition of `LODTensor` is as follows:
38+
39+
40+
```c++
41+
class LODTensor : public Tensor {
42+
public:
43+
size_t Levels() const { return seq_start_positions_.size(); }
44+
size_t Elements(int level = 0) const {
45+
return seq_start_positions_[level].size();
46+
}
47+
// slice of level[elem_begin: elem_end]
48+
// NOTE low performance in slice seq_start_positions_.
49+
// TODO should call Tensor's Slice.
50+
LODTensor LODSlice(int level, int elem_begin, int elem_end) const;
51+
52+
// slice with tensor's data shared with this.
53+
LODTensor LODSliceShared(int level, int elem_begin, int elem_end) const;
54+
55+
// copy other's lod_start_pos_, to share LOD info.
56+
// NOTE the LOD info sould not be changed.
57+
void ShareConstLODFrom(const LODTensor &other) {
58+
lod_start_pos_ = other.lod_start_pos_;
59+
}
60+
// copy other's lod_start_pos_'s content, free to mutate.
61+
void ShareMutableLODFrom(const LODTensor &other) {
62+
lod_start_pos_ = std::make_shared <
63+
std::vector<std::vector<int>>(other.lod_start_pos_.begin(),
64+
other.lod_start_pos_.end());
65+
}
66+
67+
private:
68+
std::shared_ptr<std::vector<std::vector<int>>> lod_start_pos_;
69+
};
70+
```
71+
Among them, `lod_start_pos_` uses `shared_ptr` to reduce the cost of storage and replication.
72+
`LODTensor` can be thought as an extension of `Tensor`, which is almost completely compatible with the original `Tensor`.
73+
74+
## How to support the framework
75+
### Replace `Tensor` with `LoDTensor`
76+
To implement the passing of `LODTensor`, most `Tensor` in the framework need to be replaced with `LODTensor`.
77+
Simple implementation, directly **replace all previous `Tensor` with `LODTensor`** , where you can directly modify the `Tensor` interface created in `pybind.cc`.
78+
79+
In addition, the user may need to perceive the existence of a sequence (such as the sequence of the visualization needs to parse the output sequence in the model), so some of the serial operation APIs also need to be exposed to the python layer.
80+
81+
### Transmit `lod_start_pos` along with the Op call chain
82+
`lod_start_pos` is passed along with the Op call chain
83+
The framework needs to support the following features to implement the transmit of `lod_start_pos`:
84+
85+
1. Implement the transfer as `shared_ptr`
86+
- Do not modify the contents of `lod_start_pos` as a consumer
87+
- Modify producer of `lod_start_pos` as producer
88+
- Conventions consumer only needs to copy `shared_ptr` passed over
89+
- producer needs to create its own independent memory to store its own independent modifications and expose `shared_ptr` to subsequent consumer
90+
- Since the transfer process is implemented by copying `shared_ptr`, the framework only needs to pass `lod_start_pos` once.
91+
92+
2. Op is transparent enough not to sense `lod_start_pos`
93+
3. Producer Op that needs to modify `lod_start_pos` can update its `lod_start_pos` data when `Run`
94+
95+
## sorted by length
96+
After sorting by length, the batch size from the forward time step will naturally decrement, and you can directly plug it into Net to do the batch calculation.
97+
98+
For example, the original input:
99+
100+
```
101+
origin:
102+
xxxx
103+
xx
104+
xxx
105+
106+
-> sorted:
107+
xxxx
108+
xxx
109+
xx
110+
```
111+
112+
After `SegmentInputs`, there will be 4 time steps, the input of each time step is as follows (vertical arrangement)
113+
114+
```
115+
0 1 2 3
116+
x x x x
117+
x x x
118+
x x
119+
```
120+
121+
In order to track the changes before and after sorting, use here
122+
123+
```c++
124+
struct SortedSeqItem {
125+
void *start{nullptr};
126+
void *end{nullptr};
127+
};
128+
129+
std::vector<SortedSeqItem> sorted_seqs;
130+
```
131+
To track the position of the sequence after sorting, and add a new interface
132+
133+
```c++
134+
std::vector<SortedSeqItem> SortBySeqLen(const LODTensor& tensor);
135+
```
136+
Due to the sequence of input sequences, the following existing interfaces need to be modified:
137+
138+
- InitMemories, memory needs to be rearranged according to `sorted_seqs`
139+
- SetmentInputs
140+
- ConcatOutputs
141+
142+
In addition, because `sorted_seqs` needs to be multiplexed with `RecurrentGradientOp`, it will become a new output of `RecurrentOp`.
143+
It is passed in as an input to `RecurrentGradientOp`.
144+
145+
## InitMemories
146+
Due to the sequence change, the order of the elements on the `boot_memories` batch also needs to be rearranged accordingly.
147+
148+
## SegmentInputs
149+
150+
`SegmentInputs` relies on the information of `sorted_seqs` to cut the original sequence from the horizontal to the input of each step in the sorted sequence order.
151+
152+
the transition is as follows:
153+
```
154+
origin:
155+
xxxx
156+
xx
157+
xxx
158+
159+
|
160+
|
161+
\ /
162+
!
163+
0 1 2 3
164+
x x x x
165+
x x x
166+
x x
167+
```
168+
## ConcatOutputs
169+
`ConcatOutputs` needs
170+
171+
- Restore the output of each time step back to the original input sequence order (to prevent the order of Infer phase from being upset)
172+
- Concat each sequence as a regular mini-batch representation
173+
174+
## references
175+
1. [Level of details](https://en.wikipedia.org/wiki/Level_of_detail)
29 KB
Loading

0 commit comments

Comments
 (0)