19
19
20
20
21
21
class InferenceTranspiler :
22
+ '''
23
+ Convert the fluid program to optimized inference program.
24
+
25
+ There are several optimizations, only fuse batch normalization is supported now.
26
+
27
+ Examples:
28
+
29
+ .. code-block:: python
30
+
31
+ # As InferenceTranspiler will modify the original program,
32
+ # please clone before use it.
33
+ inference_transpiler_program = program.clone()
34
+ t = fluid.InferenceTranspiler()
35
+ t.transpile(inference_transpiler_program, place)
36
+ '''
37
+
22
38
def transpile (self , program , place , scope = None ):
23
39
'''
24
- Transpile the program. Support only fuse batch normalization now.
25
-
26
- :param program: program to transpile
27
- :type program: Program
28
- :param place: inference place
29
- :type place: Place
30
- :param scope: inference scope
31
- :type scope: Scope or None
40
+ Run the transpiler.
41
+
42
+ Args:
43
+ program (Program): program to transpile
44
+ place (Place): inference place
45
+ scope (Scope|None): inference Scope
32
46
'''
33
47
if not isinstance (program , Program ):
34
48
raise TypeError ("program should be as Program type" )
@@ -49,36 +63,43 @@ def fuse_batch_norm(self, program, place, scope):
49
63
can be integrated with them. Doing so will give us a forward acceleration,
50
64
especially in environments like mobile or embedded.
51
65
52
- For input X:
53
- - Conv process: X = input * W + bias
54
- - Batch norm process: X' = (X - mean) / std
55
- - Scale Process: Y = a * X' + b
66
+ For input :math:`X`:
67
+
68
+ - Conv process: :math:`X = input * W + bias`
69
+ - Batch norm process: :math:`X' = (X - mean) / std`
70
+ - Scale Process: :math:`Y = a * X' + b`
56
71
57
72
After fuse into one operation:
58
73
59
- Y = (input * W + bias - mean) / std * a + b
60
- = input * a * W / std + ((bias - mean) / std * a + b)
74
+ .. math::
75
+
76
+ Y &= (input * W + bias - mean) / std * a + b \\ \\
77
+ &= input * a * W / std + ((bias - mean) / std * a + b)
61
78
62
79
The operator transformation is:
80
+
63
81
- before:
82
+
64
83
- conv->batch_norm->any_other_op (bias == 0)
65
84
- conv->elementwise_add->batch_norm->any_other_op (bias != 0)
85
+
66
86
- after:
87
+
67
88
- conv->elementwise_add->any_other_op
68
89
69
90
The transpile stages are:
91
+
70
92
1. insert elementwise_add op when bias == 0.
71
93
2. fuse the batch_norm's parameters to conv and elementwise_add operators.
72
94
3. remove batch_norm ops which are not used in any other ops.
73
95
4. adjust the input of any_other_op to be the output of elementwise_add operator.
74
96
5. remove unused variables.
75
97
76
- :param program: program to transpile
77
- :type program: Program
78
- :param place: inference place
79
- :type place: Place
80
- :param scope: inference scope
81
- :type scope: Scope
98
+ Args:
99
+ program (Program): program to transpile
100
+ place (Place): inference place
101
+ scope (Scope): inference Scope
102
+
82
103
'''
83
104
self .scope = scope
84
105
self .place = place
0 commit comments