var_desc.md 2.8 KB
Newer Older
W
weixing 已提交
1 2
# Design Doc: Var_desc

Q
qiaolongfei 已提交
3
## Background
4
PaddlePaddle divides the description of neural network computation into two stages: compile time and runtime. At compile time, the neural network computation is described as a `ProgramDesc` whereas at runtime an `Executor` interprets the `ProgramDesc` to compute the operations.
Q
qiaolongfei 已提交
5

6
PaddlePaddle uses proto message to describe compile time program because :
Q
qiaolongfei 已提交
7

8
1. The computation program description must be serializable and saved in a file.
9
1. During distributed training, the serialized program will be sent to multiple workers. It should also be possible to break the program into different components, each of which can be executed on a different worker.
Q
qiaolongfei 已提交
10

11
The computation `Program` consists of nested `Blocks`. Each `Block` will consist of data(i.e. `Variable`)  and  `Operations`. The concept to represent them is in the table below.
Q
qiaolongfei 已提交
12

_青葱's avatar
_青葱 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<table>
<thead>
<tr>
<th></th>
<th>compile time</th>
<th>runtime</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data </td>
<td>VarDesc(proto) </td>
<td>Variable(cpp) </td>
</tr>
<tr>
<td>Operation </td>
<td>OpDesc(proto) </td>
<td>Operator(cpp) </td>
</tr>
</tbody>
</table>
Q
qiaolongfei 已提交
34 35


36
## Definition of VarType
Q
qiaolongfei 已提交
37

38
A VarDesc should have a name, type and whether or not it is persistable. There are different kinds of variable types supported in PaddlePaddle, apart from the POD_Types like: `LOD_TENSOR`, `SELECTED_ROWS`, `FEED_MINIBATCH`, `FETCH_LIST`, `STEP_SCOPES`, `LOD_RANK_TABLE`, `LOD_TENSOR_ARRAY`, `PLACE_LIST`, `READER` and `CHANNEL`. These are declared inside `VarType`. A `VarDesc` then looks as the following:
Q
qiaolongfei 已提交
39 40 41 42

```proto
message VarDesc {
  required string name = 1;
Y
Yu Yang 已提交
43
  required VarType type = 2;
44
  optional bool persistable = 3 [ default = false ];
Q
qiaolongfei 已提交
45
}
Q
qiaolongfei 已提交
46
```
Q
qiaolongfei 已提交
47

Y
Yu Yang 已提交
48
## Definition of TensorDesc
Q
qiaolongfei 已提交
49 50

```proto
51 52 53 54 55 56 57 58 59 60 61 62
message TensorDesc {
  // Should only be PODType. Is enforced in C++
  required Type data_type = 1;
  repeated int64 dims = 2; // [UNK, 640, 480] is saved as [-1, 640, 480]
}
```

The `Type` here comes from the enum defined inside of `VarType` :

```proto
enum Type {
  // Pod Types
Q
qiaolongfei 已提交
63 64 65 66 67 68 69 70
  BOOL = 0;
  INT16 = 1;
  INT32 = 2;
  INT64 = 3;
  FP16 = 4;
  FP32 = 5;
  FP64 = 6;

71 72 73 74 75 76 77 78 79 80 81
  // Other types that may need additional descriptions
  LOD_TENSOR = 7;
  SELECTED_ROWS = 8;
  FEED_MINIBATCH = 9;
  FETCH_LIST = 10;
  STEP_SCOPES = 11;
  LOD_RANK_TABLE = 12;
  LOD_TENSOR_ARRAY = 13;
  PLACE_LIST = 14;
  READER = 15;
  CHANNEL = 16;
Q
qiaolongfei 已提交
82 83 84
}
```

Y
Yu Yang 已提交
85
A TensorDesc describes `SelectedRows` and `LoDTensor`. For details of `SelectedRows`, please reference [`SelectedRows`](./selected_rows.md).
Q
qiaolongfei 已提交
86

Y
Yu Yang 已提交
87
## Definition of LodTensorDesc
Q
qiaolongfei 已提交
88

Y
Yu Yang 已提交
89 90 91
```proto
message LoDTensorDesc {
  required TensorDesc tensor = 1;
92
  optional int32 lod_level = 2 [ default = 0 ];
Y
Yu Yang 已提交
93
}
Q
qiaolongfei 已提交
94 95
```

Y
Yu Yang 已提交
96
A LoDTensorDesc contains a tensor and a lod_level.
Q
qiaolongfei 已提交
97

Y
Yu Yang 已提交
98
## Definition of Variable in Python
Q
qiaolongfei 已提交
99

Y
Yu Yang 已提交
100
For Variable in Python, please reference [`Python API`](./python_api.md).