@@ -12,8 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
See the License for the specific language governing permissions and
13
13
limitations under the License. */
14
14
15
- #include " paddle/fluid/inference/tensorrt/convert/op_converter.h"
16
15
#include < math.h>
16
+ #include " paddle/fluid/inference/tensorrt/convert/op_converter.h"
17
17
18
18
namespace paddle {
19
19
namespace inference {
@@ -23,15 +23,15 @@ class BatchNormOpConverter : public OpConverter {
23
23
public:
24
24
void operator ()(const framework::proto::OpDesc& op,
25
25
const framework::Scope& scope, bool test_mode) override {
26
- LOG (INFO)
27
- << " convert a fluid batch norm op to tensorrt batch_norm" ;
26
+ LOG (INFO) << " convert a fluid batch norm op to tensorrt batch_norm" ;
28
27
29
28
framework::OpDesc op_desc (op, nullptr );
30
29
PADDLE_ENFORCE_EQ (op_desc.Input (" X" ).size (), 1 );
31
- PADDLE_ENFORCE_EQ (op_desc.Input (" Bias" ).size (), 1 ); // Bias is a weight
32
- PADDLE_ENFORCE_EQ (op_desc.Input (" Mean" ).size (), 1 ); // Mean is a weight
30
+ PADDLE_ENFORCE_EQ (op_desc.Input (" Bias" ).size (), 1 ); // Bias is a weight
31
+ PADDLE_ENFORCE_EQ (op_desc.Input (" Mean" ).size (), 1 ); // Mean is a weight
33
32
PADDLE_ENFORCE_EQ (op_desc.Input (" Scale" ).size (), 1 ); // Scale is a weight
34
- PADDLE_ENFORCE_EQ (op_desc.Input (" Variance" ).size (), 1 ); // Variance is a weight
33
+ PADDLE_ENFORCE_EQ (op_desc.Input (" Variance" ).size (),
34
+ 1 ); // Variance is a weight
35
35
PADDLE_ENFORCE_EQ (op_desc.Output (" Y" ).size (), 1 );
36
36
37
37
auto * X = engine_->GetITensor (op_desc.Input (" X" ).front ());
@@ -53,7 +53,6 @@ class BatchNormOpConverter : public OpConverter {
53
53
auto * Scale_t = Scale_v->GetMutable <framework::LoDTensor>();
54
54
auto * Variance_t = Variance_v->GetMutable <framework::LoDTensor>();
55
55
56
-
57
56
// create temp tensor for weights
58
57
framework::LoDTensor bias_tensor;
59
58
framework::LoDTensor mean_tensor;
@@ -64,9 +63,9 @@ class BatchNormOpConverter : public OpConverter {
64
63
mean_tensor.Resize (Mean_t->dims ());
65
64
scale_tensor.Resize (Scale_t->dims ());
66
65
variance_tensor.Resize (Variance_t->dims ());
67
-
66
+
68
67
platform::CPUPlace cpu_place;
69
- // copy data from gpu to cpu
68
+ // copy data from gpu to cpu
70
69
TensorCopySync ((*Bias_t), cpu_place, &bias_tensor);
71
70
TensorCopySync ((*Mean_t), cpu_place, &mean_tensor);
72
71
TensorCopySync ((*Scale_t), cpu_place, &scale_tensor);
@@ -75,47 +74,53 @@ class BatchNormOpConverter : public OpConverter {
75
74
auto * bias_data = bias_tensor.mutable_data <float >(platform::CPUPlace ());
76
75
auto * mean_data = mean_tensor.mutable_data <float >(platform::CPUPlace ());
77
76
auto * scale_data = scale_tensor.mutable_data <float >(platform::CPUPlace ());
78
- auto * variance_data = variance_tensor.mutable_data <float >(platform::CPUPlace ());
79
-
80
- framework::LoDTensor *combile_scale_tensor = new framework::LoDTensor ();
81
- framework::LoDTensor *combile_bias_tensor = new framework::LoDTensor ();
77
+ auto * variance_data =
78
+ variance_tensor.mutable_data <float >(platform::CPUPlace ());
79
+
80
+ std::unique_ptr<framework::LoDTensor> combile_scale_tensor (
81
+ new framework::LoDTensor ());
82
+ std::unique_ptr<framework::LoDTensor> combile_bias_tensor (
83
+ new framework::LoDTensor ());
82
84
83
85
combile_scale_tensor->Resize (scale_tensor.dims ());
84
86
combile_bias_tensor->Resize (bias_tensor.dims ());
85
87
86
- auto * combile_scale_data = combile_scale_tensor->mutable_data <float >(platform::CPUPlace ());
87
- auto * combile_bias_data = combile_bias_tensor->mutable_data <float >(platform::CPUPlace ());
88
+ auto * combile_scale_data =
89
+ combile_scale_tensor->mutable_data <float >(platform::CPUPlace ());
90
+ auto * combile_bias_data =
91
+ combile_bias_tensor->mutable_data <float >(platform::CPUPlace ());
92
+
93
+ size_t ele_num = combile_scale_tensor->memory_size () / sizeof (float );
88
94
89
- engine_->weight_map_ [op_desc.Input (" Bias" ).front ()] = std::move (std::unique_ptr<framework::Tensor>(combile_bias_tensor));
90
- engine_->weight_map_ [op_desc.Input (" Scale" ).front ()] = std::move (std::unique_ptr<framework::Tensor>(combile_scale_tensor));
91
-
92
- size_t ele_num = combile_scale_tensor->memory_size ()/sizeof (float );
93
-
94
95
for (size_t i = 0 ; i < ele_num; i++) {
95
- float scale = scale_data[i];
96
- float bias = bias_data[i];
97
- float mean = mean_data[i];
98
- float variance = variance_data[i];
99
- combile_scale_data[i] = scale / sqrtf (variance + eps);
100
- combile_bias_data[i] = bias - mean * combile_scale_data[i];
96
+ float scale = scale_data[i];
97
+ float bias = bias_data[i];
98
+ float mean = mean_data[i];
99
+ float variance = variance_data[i];
100
+ combile_scale_data[i] = scale / sqrtf (variance + eps);
101
+ combile_bias_data[i] = bias - mean * combile_scale_data[i];
101
102
}
102
103
103
-
104
- TensorRTEngine::Weight scale_weights{nvinfer1::DataType::kFLOAT ,
105
- static_cast <void *>(combile_scale_data),
106
- combile_scale_tensor->memory_size () / sizeof (float )};
107
- TensorRTEngine::Weight shift_weights{nvinfer1::DataType::kFLOAT ,
108
- static_cast <void *>(combile_bias_data),
109
- combile_bias_tensor->memory_size ()/ sizeof (float )};
104
+ TensorRTEngine::Weight scale_weights{
105
+ nvinfer1::DataType::kFLOAT , static_cast <void *>(combile_scale_data),
106
+ combile_scale_tensor->memory_size () / sizeof (float )};
107
+ TensorRTEngine::Weight shift_weights{
108
+ nvinfer1::DataType::kFLOAT , static_cast <void *>(combile_bias_data),
109
+ combile_bias_tensor->memory_size () / sizeof (float )};
110
110
TensorRTEngine::Weight power_weights{nvinfer1::DataType::kFLOAT , nullptr ,
111
111
0 };
112
112
113
-
114
- nvinfer1::IScaleLayer* layer = TRT_ENGINE_ADD_LAYER (
115
- engine_, Scale, * const_cast <nvinfer1::ITensor*>(X), nvinfer1::ScaleMode::kCHANNEL ,
116
- shift_weights. get (), scale_weights.get (), power_weights.get ());
113
+ nvinfer1::IScaleLayer* layer =
114
+ TRT_ENGINE_ADD_LAYER (engine_, Scale, * const_cast <nvinfer1::ITensor*>(X),
115
+ nvinfer1::ScaleMode::kCHANNEL , shift_weights. get () ,
116
+ scale_weights.get (), power_weights.get ());
117
117
118
118
auto output_name = op_desc.Output (" Y" ).front ();
119
+ engine_->weight_map [op_desc.Input (" Bias" ).front ()] =
120
+ std::move (combile_bias_tensor);
121
+ engine_->weight_map [op_desc.Input (" Scale" ).front ()] =
122
+ std::move (combile_scale_tensor);
123
+
119
124
engine_->SetITensor (output_name, layer->getOutput (0 ));
120
125
121
126
if (test_mode) {
0 commit comments