Skip to content

Commit 326221a

Browse files
committed
Merge branch 'develop' into tr_convert_init
2 parents c4e3010 + b8b2e89 commit 326221a

Some content is hidden

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

58 files changed

+2203
-297
lines changed

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
29 KB
Loading
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Background
2+
3+
[ONNX (Open Neural Network Exchange)](https://github.com/onnx/onnx) bridges different deep learning frameworks by providing an open source graph format for models. The models trained in other frameworks can be converted into the ONNX format to execute inference by utilizing the built-in operators in ONNX - this is called a **frontend**. With the inverse conversion (called a **backend**), different frameworks can share any models supported by ONNX in principle. Now most mainstream frameworks have joined the ONNX community, e.g. Caffe2, PyTorch, and MXNet etc. And there is a momentum driving more and more vendors to begin supporting ONNX or even choose ONNX as the only machine learning runtime in their devices.
4+
5+
Therefore, it is necessary to enable the conversion between PaddlePaddle and ONNX. This design doc is aimed at implementing a convertor, mainly for converting between **Fluid** models and ONNX (it is very likely that we may support older v2 models in the future). A complete convertor should be bidirectional - with a frontend AND a backend, but considering the importance, the we will start with the frontend i.e. Fluid models to ONNX models.
6+
7+
8+
# How it works
9+
10+
ONNX has a [working list of operators](https://github.com/onnx/onnx/blob/master/docs/Operators.md) which is versioned.
11+
12+
When prioritizing implementation of a frontend over a backend, choice of coverage of Fluid -> ONNX operators comes down to choices of models to be supported (see section `Supported models`). Eventually, this will allow us to reach a really-wide coverage of all operators.
13+
14+
Here are a few major considerations when it comes to converting models:
15+
16+
- **Op-level conversion**: How to map the inputs, attributes, and outputs of each Paddle operator to those of the ONNX operator. In several cases, these require transformations. For each direction (frontend vs. backend), a different conversion mapping is needed.
17+
- **Parameters (weights) initialization**: Setting initial parameters on different nodes.
18+
- **Tensor data type mapping** (Note: Some ONNX data types are not supported in Fluid)
19+
- **Network representation adaption**: Fluid `ProgramDesc` include nested blocks. Since ONNX is free of nesting, the `ProgramDesc` ops need to be traversed to only include ops from the global scope in the root block. The variables used as inputs and outputs should also be in this scope.
20+
- **Model validation**: There are two kinds of validations that are necessary:
21+
1. We need to ensure that the inference outputs of the ops in run inside a model are the same as those when running the ONNX converted ops through an alternative ONNX backend.
22+
2. Checking to see if the generated nodes on the graph are validated by the internal ONNX checkers.
23+
- **Versioning**: ONNX versions its op listing over versions. In fact, it has versioning on 3 different levels: ops, graphs, and ONNX models. This requires that we are conscious about versioning the convertor and updating tests and op convertor logic for each release. It also implies that we release pre-trained ONNX models upon each version release.
24+
25+
One thing that makes this conversion more feasible in Fluid's case is the use of a static IR - the `ProgramDesc` - as opposed to a dynamic graph, as created in the cases of frameworks like PyTorch.
26+
27+
28+
# Project structure
29+
30+
<p align="center">
31+
<img src="./images/project_structure.png"/>
32+
</p>
33+
34+
The project contains four important parts:
35+
36+
* **fluid**: The directory that contains wrappers for fluid related APIs. Fluid has provided some low-level APIs to parse or generate the inference model. However, directly using these low-level APIs makes the code tediously long. This module wraps low-level APIs to provide simplified interfaces.
37+
38+
* **onnx**: This is a Python package provided by ONNX containing helpers for creating nodes, graphs, and eventually binary protobuf models with initializer parameters.
39+
40+
* **onnx_fluid**: Contains two-way mapping (Fluid -> ONNX ops and ONNX -> Fluid ops). Called from `convert.py`, the program uses this mapping along with modifier functions to construct ONNX nodes with the help of ONNX's `make_node` helper. It also contains mapping between datatypes and tensor deprecation / amplification logic.
41+
42+
* **convert.py**: The interface exposed to users. This will traverse the global program blocks/variables and construct the write-able model.
43+
44+
45+
# Usage
46+
The converter should be designed to very easy-to-use. Bidirectional conversion between a Fluid inference model and an ONNX binary model will be supported. Model validation will also provided to verify the correctness of converted model.
47+
48+
* Convert Fluid inference model to ONNX binary model
49+
50+
```
51+
python convert.py --fluid_model <fluid inference model> --onnx_model <ONNX model> validate True
52+
```
53+
54+
* Validate the converted model
55+
56+
```
57+
python validate.py --fluid_model <fluid inference model> --onnx_model <ONNX model>
58+
```
59+
60+
The conversion and model validation will be completed consecutively, finally output a readable model structure description. And for the converse conversion, users only need to exchange the input and output.
61+
62+
63+
# Challenges and mitigation
64+
65+
## Cycles
66+
67+
Cycles are unsupported in ONNX. In Paddle, the `while` op is the most prominent example of a cycle.
68+
69+
*Resolution*: We won't support models with `while`s which can't be substituted until ONNX adds support for such ops.
70+
71+
## Sequences
72+
73+
Sequence processing operators like `sequence_expand`, `sequence_reshape`, `sequence_concat`, and `sequence_pool` are not supported by ONNX as well, because they do not support non-padded datatypes like LoDTensors.
74+
75+
*Resolution*: Since the runtimes using our ONNX exported graphs won't be using LoDTensors in the first place, such sequence operators should be mapped to ONNX ops that will do the necessary transposing ops with the knowledge of the padding and shape of the Tensors.
76+
77+
## Ops that can't easily be mapped
78+
79+
There are ops that just aren't possible to map today:
80+
81+
**Control flow operators**
82+
83+
Paddle supports control flow ops like `If/Else` and `Switch` (if we ignore the CSP operations like `select` for now). ONNX has `If` support in the experimental phase.
84+
85+
*Resolution*: Map Paddle's `If/Else` to ONNX's `If`, but ignore other control flow operators until ONNX brings support for them.
86+
87+
88+
**Non-existent in Fluid**
89+
90+
There are several ONNX operators that are not available in Fluid today, e.g. `InstanceNormalization`, `RandomUniform`, `Unsqueeze`, etc.
91+
92+
*Resolution*: For the initial phase, we can choose to not support ops that our models don't care for and are subsequently not available in Fluid. However, for ops that we think might be necessary for Fluid users also, we must implement them on our side and support the ONNX conversion to them. This list is TBD.
93+
94+
95+
**Concurrency**
96+
97+
ONNX does not have any considerations for concurrency right now.
98+
99+
*Resolution*: There are two ways to approach this:
100+
101+
a. We choose to not support concurrent models.
102+
b. We only support `go_op`s (basically threads) shallowly. This could mean that we enqueue `go_op` ops prior to gradient calculations OR even prior to the entire graph, and that's it - since `go_op`s do not have support for backprop anyways. One of the core target use cases of `go_op`: batch reading - can be handled through this approach.
103+
104+
105+
**Overloaded in Fluid**
106+
107+
There are ops in ONNX whose job can't be accomplished by a single corresponding Paddle operator (e.g. ), but a collection of operators.
108+
109+
*Resolution*: Chain multiple Paddle operators.
110+
111+
112+
## Lack of LoDTensors
113+
114+
As stated above, ONNX only supports simple Tensor values.
115+
116+
*Resolution*: Deprecate to plain old numpy-able tensors.
117+
118+
119+
## Reconstruction from deprecated ONNX ops
120+
121+
For higher-level Fluid ops, such as a few offered by the `nn` layer that do not have direct corresponding mappings but can be converted to ONNX by chaining a series of ops without cycles, it would be useful to map them back to the higher-level Fluid ops once converted back from the deprecated ONNX graphs.
122+
123+
*Resolution*: Graphs that have the deprecation from Paddle -> ONNX. When converting back from ONNX, if we encounter the identical graphs by doing a forward search, we can replace the subgraphs with the matching ONNX op.
124+
125+
126+
# Supported models
127+
128+
As mentioned above, potential risks may come from the conversion of sequence-related models, including the LodTensor, ```if/else``` and ```while``` operator. So a good choice is to focus on some important feedforward models first, then implement some simple recurrent models.
129+
130+
- Feedforward models: common models selected in PaddleBook, e.g. VGG, ResNet and some other models proposed by application teams.
131+
- Recurrent models: language model, stacked LSTMs etc.
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
Use different clusters
22
======================
33

4-
PaddlePaddle supports running jobs on several platforms including:
5-
- `Kubernetes <http://kubernetes.io>`_ open-source system for automating deployment, scaling, and management of containerized applications from Google.
6-
- `OpenMPI <https://www.open-mpi.org>`_ Mature high performance parallel computing framework.
7-
- `Fabric <http://www.fabfile.org>`_ A cluster management tool. Write scripts to submit jobs or manage the cluster.
4+
The user's cluster environment is not the same. To facilitate everyone's deployment, we provide a variety of cluster deployment methods to facilitate the submission of cluster training tasks, which will be introduced as follows:
85

9-
We'll introduce cluster job management on these platforms. The examples can be found under `cluster_train_v2 <https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/scripts/cluster_train_v2>`_ .
6+
`Kubernetes <http://kubernetes.io>`_ is a scheduling framework of Google open source container cluster, supporting a complete cluster solution for large-scale cluster production environment. The following guidelines show PaddlePaddle's support for Kubernetes:
107

11-
These cluster platforms provide API or environment variables for training processes, when the job is dispatched to different nodes. Like node ID, IP or total number of nodes etc.
8+
.. toctree::
9+
:maxdepth: 1
10+
11+
k8s_cn.md
12+
k8s_distributed_cn.md
13+
14+
`OpenMPI <https://www.open-mpi.org>`_ is a mature high-performance parallel computing framework, which is widely used in the field of HPC. The following guide describes how to use OpenMPI to build PaddlePaddle's cluster training task:
1215

1316
.. toctree::
1417
:maxdepth: 1
1518

16-
fabric_en.md
17-
openmpi_en.md
18-
k8s_en.md
19-
k8s_aws_en.md
19+
openmpi_cn.md
20+
21+
`Fabric <http://www.fabfile.org>`_ is a convenient tool for program deployment and management. We provide a way to deploy and manage with Fabric. If you want to know more about it, please read the following guidelines:
22+
23+
.. toctree::
24+
:maxdepth: 1
25+
26+
fabric_cn.md
27+
28+
We also support the deployment of PaddlePaddle on AWS. Learn more about:
29+
30+
.. toctree::
31+
:maxdepth: 1
32+
33+
k8s_aws_cn.md
34+
35+
The examples can be found under `cluster_train_v2 <https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/scripts/cluster_train_v2>`_ .

0 commit comments

Comments
 (0)