Skip to content

Commit 7385854

Browse files
authored
Fix more CPPLint issues in fluid/operators/math (#10276)
* Fix CPPLint issues in lstm_cpu_kernel.h * Fix CPPLint issues in math/math_function_test * Fix CPPLint issues in math/math_function_test * Fix CPPLint issues in math/concat.cc * Fix CPPLint issues in math/concat.cc * Fix CPPLint issues in math/concat.cc * Fix CPPLint issues in math/gru_cpu_kernel * Fix CPPLint issues in math/selected_rows_functor_test.cu * Fix compile error * Fix compile error
1 parent fb7ca48 commit 7385854

File tree

8 files changed

+313
-285
lines changed

8 files changed

+313
-285
lines changed

paddle/fluid/operators/concat_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ConcatGradKernel : public framework::OpKernel<T> {
8787
auto& dev_ctx = ctx.template device_context<DeviceContext>();
8888
paddle::operators::math::ConcatGradFunctor<DeviceContext, T>
8989
concat_grad_functor;
90-
concat_grad_functor(dev_ctx, *in, static_cast<int>(axis), outputs);
90+
concat_grad_functor(dev_ctx, *in, static_cast<int>(axis), &outputs);
9191
}
9292
}
9393
};

paddle/fluid/operators/math/concat.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/operators/math/concat.h"
16+
#include <vector>
1617

1718
namespace paddle {
1819
namespace operators {
@@ -70,20 +71,20 @@ class ConcatGradFunctor<platform::CPUDeviceContext, T> {
7071
public:
7172
void operator()(const platform::CPUDeviceContext& context,
7273
const framework::Tensor& input, const int axis,
73-
std::vector<framework::Tensor>& outputs) {
74+
std::vector<framework::Tensor>* outputs) {
7475
// TODO(zcd): Add input data validity checking
75-
int num = outputs.size();
76+
int num = outputs->size();
7677

7778
int input_rows = 1;
78-
auto dim_0 = outputs[0].dims();
79+
auto dim_0 = outputs->at(0).dims();
7980
for (int i = 0; i < axis; ++i) {
8081
input_rows *= dim_0[i];
8182
}
8283
int input_cols = 0;
8384

84-
std::vector<int64_t> output_cols(outputs.size());
85+
std::vector<int64_t> output_cols(outputs->size());
8586
for (int i = 0; i < num; ++i) {
86-
int t_cols = outputs[i].numel() / input_rows;
87+
int t_cols = outputs->at(i).numel() / input_rows;
8788
input_cols += t_cols;
8889
output_cols[i] = t_cols;
8990
}
@@ -95,7 +96,7 @@ class ConcatGradFunctor<platform::CPUDeviceContext, T> {
9596
int col_idx = 0;
9697
for (int j = 0; j < num; ++j) {
9798
int col_len = output_cols[j];
98-
T* dst_ptr = outputs[j].data<T>() + k * col_len;
99+
T* dst_ptr = outputs->at(j).data<T>() + k * col_len;
99100
memory::Copy(cpu_place, dst_ptr, cpu_place, src_ptr + col_idx,
100101
sizeof(T) * col_len);
101102
col_idx += col_len;

paddle/fluid/operators/math/concat.cu

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15+
#include <algorithm>
16+
#include <vector>
1517
#include "paddle/fluid/framework/mixed_vector.h"
1618
#include "paddle/fluid/operators/math/concat.h"
1719
#include "paddle/fluid/platform/cuda_primitives.h"
@@ -202,16 +204,16 @@ class ConcatGradFunctor<platform::CUDADeviceContext, T> {
202204
public:
203205
void operator()(const platform::CUDADeviceContext& context,
204206
const framework::Tensor& input, const int axis,
205-
std::vector<framework::Tensor>& outputs) {
207+
std::vector<framework::Tensor>* outputs) {
206208
// TODO(zcd): Add input data validity checking
207-
int o_num = outputs.size();
209+
int o_num = outputs->size();
208210
int out_row = 1;
209-
auto dim_0 = outputs[0].dims();
211+
auto dim_0 = outputs->at(0).dims();
210212
for (int i = 0; i < axis; ++i) {
211213
out_row *= dim_0[i];
212214
}
213215

214-
int out_col = outputs[0].numel() / out_row;
216+
int out_col = outputs->at(0).numel() / out_row;
215217
int in_col = 0, in_row = out_row;
216218
bool sameShape = true;
217219

@@ -221,13 +223,13 @@ class ConcatGradFunctor<platform::CUDADeviceContext, T> {
221223

222224
outputs_cols[0] = 0;
223225
for (int i = 0; i < o_num; ++i) {
224-
int t_col = outputs[i].numel() / out_row;
226+
int t_col = outputs->at(i).numel() / out_row;
225227
if (sameShape) {
226228
if (t_col != out_col) sameShape = false;
227229
}
228230
in_col += t_col;
229231
outputs_cols[i + 1] = in_col;
230-
outputs_ptr[i] = outputs[i].data<T>();
232+
outputs_ptr[i] = outputs->at(i).data<T>();
231233
}
232234

233235
T** dev_out_gpu_data =

paddle/fluid/operators/math/concat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#pragma once
16+
#include <vector>
1617
#include "paddle/fluid/framework/data_type.h"
1718
#include "paddle/fluid/framework/tensor.h"
1819

@@ -56,7 +57,7 @@ template <typename DeviceContext, typename T>
5657
class ConcatGradFunctor {
5758
public:
5859
void operator()(const DeviceContext& context, const framework::Tensor& input,
59-
const int axis, std::vector<framework::Tensor>& outputs);
60+
const int axis, std::vector<framework::Tensor>* outputs);
6061
};
6162

6263
} // namespace math

paddle/fluid/operators/math/detail/gru_cpu_kernel.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,22 @@ void hl_avx_gru_forward_reset_output(OpResetOutput op_reset_output,
8989
__m256 r_value_reset_gate;
9090
__m256 r_value_reset_output;
9191
__m256 r_prev_out = _mm256_set1_ps(0.0f);
92-
__m256 *update_gate = (__m256 *)gate_value;
93-
__m256 *reset_gate = (__m256 *)(gate_value + frame_size);
92+
__m256 *update_gate = reinterpret_cast<__m256 *>(gate_value);
93+
__m256 *reset_gate = reinterpret_cast<__m256 *>(gate_value + frame_size);
9494

9595
for (int i = 0; i < frame_size / 8; i++) {
9696
r_value_update_gate = update_gate[i];
9797
r_value_reset_gate = reset_gate[i];
9898
if (prev_output_value) {
99-
r_prev_out = ((__m256 *)prev_output_value)[i];
99+
r_prev_out = (reinterpret_cast<__m256 *>(prev_output_value))[i];
100100
}
101101

102102
op_reset_output(r_value_update_gate, r_value_reset_gate, r_prev_out,
103103
r_value_reset_output, active_gate);
104104

105105
update_gate[i] = r_value_update_gate;
106106
reset_gate[i] = r_value_reset_gate;
107-
((__m256 *)reset_output_value)[i] = r_value_reset_output;
107+
(reinterpret_cast<__m256 *>(reset_output_value))[i] = r_value_reset_output;
108108
}
109109
#endif
110110
}
@@ -119,21 +119,21 @@ void hl_avx_gru_forward_final_output(OpFinalOutput op_final_output,
119119
__m256 r_value_frame_state;
120120
__m256 r_prev_out = _mm256_set1_ps(0.0f);
121121
__m256 r_output;
122-
__m256 *update_gate = (__m256 *)gate_value;
123-
__m256 *frame_state = (__m256 *)(gate_value + frame_size * 2);
122+
__m256 *update_gate = reinterpret_cast<__m256 *>(gate_value);
123+
__m256 *frame_state = reinterpret_cast<__m256 *>(gate_value + frame_size * 2);
124124

125125
for (int i = 0; i < frame_size / 8; i++) {
126126
r_value_update_gate = update_gate[i];
127127
r_value_frame_state = frame_state[i];
128128
if (prev_output_value) {
129-
r_prev_out = ((__m256 *)prev_output_value)[i];
129+
r_prev_out = (reinterpret_cast<__m256 *>(prev_output_value))[i];
130130
}
131131

132132
op_final_output(r_value_update_gate, r_value_frame_state, r_prev_out,
133133
r_output, active_node);
134134

135135
frame_state[i] = r_value_frame_state;
136-
((__m256 *)output_value)[i] = r_output;
136+
(reinterpret_cast<__m256 *>(output_value))[i] = r_output;
137137
}
138138
#endif
139139
}
@@ -284,20 +284,22 @@ void hl_avx_gru_backward_state_grad(OpStateGrad op_state_grad, T *gate_value,
284284
__m256 r_out_grad;
285285
__m256 r_prev_out_value = _mm256_set1_ps(0.0f);
286286
__m256 r_prev_out_grad = _mm256_set1_ps(0.0f);
287-
__m256 *update_gate_value = (__m256 *)gate_value;
288-
__m256 *update_gate_grad = (__m256 *)gate_grad;
289-
__m256 *frame_state_value = (__m256 *)(gate_value + frame_size * 2);
290-
__m256 *frame_state_grad = (__m256 *)(gate_grad + frame_size * 2);
287+
__m256 *update_gate_value = reinterpret_cast<__m256 *>(gate_value);
288+
__m256 *update_gate_grad = reinterpret_cast<__m256 *>(gate_grad);
289+
__m256 *frame_state_value =
290+
reinterpret_cast<__m256 *>(gate_value + frame_size * 2);
291+
__m256 *frame_state_grad =
292+
reinterpret_cast<__m256 *>(gate_grad + frame_size * 2);
291293

292294
for (int i = 0; i < frame_size / 8; i++) {
293295
r_update_gate_value = update_gate_value[i];
294296
r_frame_state_value = frame_state_value[i];
295-
r_out_grad = ((__m256 *)output_grad)[i];
297+
r_out_grad = (reinterpret_cast<__m256 *>(output_grad))[i];
296298
if (prev_out_value) {
297-
r_prev_out_value = ((__m256 *)prev_out_value)[i];
299+
r_prev_out_value = (reinterpret_cast<__m256 *>(prev_out_value))[i];
298300
}
299301
if (prev_out_grad) {
300-
r_prev_out_grad = ((__m256 *)prev_out_grad)[i];
302+
r_prev_out_grad = (reinterpret_cast<__m256 *>(prev_out_grad))[i];
301303
}
302304

303305
op_state_grad(r_update_gate_value, r_update_gate_grad, r_frame_state_value,
@@ -307,7 +309,7 @@ void hl_avx_gru_backward_state_grad(OpStateGrad op_state_grad, T *gate_value,
307309
update_gate_grad[i] = r_update_gate_grad;
308310
frame_state_grad[i] = r_frame_state_grad;
309311
if (prev_out_grad) {
310-
((__m256 *)prev_out_grad)[i] = r_prev_out_grad;
312+
(reinterpret_cast<__m256 *>(prev_out_grad))[i] = r_prev_out_grad;
311313
}
312314
}
313315
#endif
@@ -327,24 +329,25 @@ void hl_avx_gru_backward_reset_grad(OpResetGrad op_reset_grad, T *gate_value,
327329
__m256 r_reset_output_grad = _mm256_set1_ps(0.0f);
328330
__m256 r_prev_out_value = _mm256_set1_ps(0.0f);
329331
__m256 r_prev_out_grad = _mm256_set1_ps(0.0f);
330-
__m256 *update_gate_value = (__m256 *)gate_value;
331-
__m256 *update_gate_grad = (__m256 *)gate_grad;
332-
__m256 *reset_gate_value = (__m256 *)(gate_value + frame_size);
333-
__m256 *reset_gate_grad = (__m256 *)(gate_grad + frame_size);
332+
__m256 *update_gate_value = reinterpret_cast<__m256 *>(gate_value);
333+
__m256 *update_gate_grad = reinterpret_cast<__m256 *>(gate_grad);
334+
__m256 *reset_gate_value =
335+
reinterpret_cast<__m256 *>(gate_value + frame_size);
336+
__m256 *reset_gate_grad = reinterpret_cast<__m256 *>(gate_grad + frame_size);
334337

335338
for (int i = 0; i < frame_size / 8; i++) {
336339
r_update_gate_value = update_gate_value[i];
337340
r_update_gate_grad = update_gate_grad[i];
338341
r_reset_gate_value = reset_gate_value[i];
339342

340343
if (prev_out_value && prev_out_grad) {
341-
r_reset_output_grad = ((__m256 *)reset_output_grad)[i];
344+
r_reset_output_grad = (reinterpret_cast<__m256 *>(reset_output_grad))[i];
342345
}
343346
if (prev_out_value) {
344-
r_prev_out_value = ((__m256 *)prev_out_value)[i];
347+
r_prev_out_value = (reinterpret_cast<__m256 *>(prev_out_value))[i];
345348
}
346349
if (prev_out_grad) {
347-
r_prev_out_grad = ((__m256 *)prev_out_grad)[i];
350+
r_prev_out_grad = (reinterpret_cast<__m256 *>(prev_out_grad))[i];
348351
}
349352

350353
op_reset_grad(r_update_gate_value, r_update_gate_grad, r_reset_gate_value,
@@ -354,7 +357,7 @@ void hl_avx_gru_backward_reset_grad(OpResetGrad op_reset_grad, T *gate_value,
354357
update_gate_grad[i] = r_update_gate_grad;
355358
reset_gate_grad[i] = r_reset_gate_grad;
356359
if (prev_out_grad) {
357-
((__m256 *)prev_out_grad)[i] = r_prev_out_grad;
360+
(reinterpret_cast<__m256 *>(prev_out_grad))[i] = r_prev_out_grad;
358361
}
359362
}
360363
#endif

paddle/fluid/operators/math/detail/lstm_cpu_kernel.h

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,26 @@ void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value,
164164
__m256 r_state_atv;
165165
__m256 r_out;
166166

167-
__m256 *value_in = (__m256 *)value.gate_value;
168-
__m256 *value_ig = (__m256 *)(value.gate_value + frame_size);
169-
__m256 *value_fg = (__m256 *)(value.gate_value + frame_size * 2);
170-
__m256 *value_og = (__m256 *)(value.gate_value + frame_size * 3);
167+
__m256 *value_in = reinterpret_cast<__m256 *>(value.gate_value);
168+
__m256 *value_ig = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
169+
__m256 *value_fg =
170+
reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
171+
__m256 *value_og =
172+
reinterpret_cast<__m256 *>(value.gate_value + frame_size * 3);
171173

172174
for (int i = 0; i < frame_size / 8; i++) {
173175
r_value_in = value_in[i];
174176
r_value_ig = value_ig[i];
175177
r_value_fg = value_fg[i];
176178
r_value_og = value_og[i];
177179
if (value.check_ig) {
178-
r_checkI = ((__m256 *)value.check_ig)[i];
179-
r_checkF = ((__m256 *)value.check_fg)[i];
180-
r_checkO = ((__m256 *)value.check_og)[i];
180+
r_checkI = (reinterpret_cast<__m256 *>(value.check_ig))[i];
181+
r_checkF = (reinterpret_cast<__m256 *>(value.check_fg))[i];
182+
r_checkO = (reinterpret_cast<__m256 *>(value.check_og))[i];
181183
}
182184

183185
if (value.prev_state_value) {
184-
r_prev_state = ((__m256 *)value.prev_state_value)[i];
186+
r_prev_state = (reinterpret_cast<__m256 *>(value.prev_state_value))[i];
185187
}
186188

187189
op(r_value_in, r_value_ig, r_value_fg, r_value_og, r_prev_state, r_state,
@@ -192,9 +194,9 @@ void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value,
192194
value_ig[i] = r_value_ig;
193195
value_fg[i] = r_value_fg;
194196
value_og[i] = r_value_og;
195-
((__m256 *)value.state_value)[i] = r_state;
196-
((__m256 *)value.state_active_value)[i] = r_state_atv;
197-
((__m256 *)value.output_value)[i] = r_out;
197+
(reinterpret_cast<__m256 *>(value.state_value))[i] = r_state;
198+
(reinterpret_cast<__m256 *>(value.state_active_value))[i] = r_state_atv;
199+
(reinterpret_cast<__m256 *>(value.output_value))[i] = r_out;
198200
}
199201
#endif
200202
}
@@ -227,31 +229,33 @@ void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
227229
__m256 r_checkFGrad;
228230
__m256 r_checkOGrad;
229231

230-
__m256 *value_in = (__m256 *)value.gate_value;
231-
__m256 *value_ig = (__m256 *)(value.gate_value + frame_size);
232-
__m256 *value_fg = (__m256 *)(value.gate_value + frame_size * 2);
233-
__m256 *value_og = (__m256 *)(value.gate_value + frame_size * 3);
234-
__m256 *grad_in = (__m256 *)grad.gate_grad;
235-
__m256 *grad_ig = (__m256 *)(grad.gate_grad + frame_size);
236-
__m256 *grad_fg = (__m256 *)(grad.gate_grad + frame_size * 2);
237-
__m256 *grad_og = (__m256 *)(grad.gate_grad + frame_size * 3);
232+
__m256 *value_in = reinterpret_cast<__m256 *>(value.gate_value);
233+
__m256 *value_ig = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
234+
__m256 *value_fg =
235+
reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
236+
__m256 *value_og =
237+
reinterpret_cast<__m256 *>(value.gate_value + frame_size * 3);
238+
__m256 *grad_in = reinterpret_cast<__m256 *>(grad.gate_grad);
239+
__m256 *grad_ig = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size);
240+
__m256 *grad_fg = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 2);
241+
__m256 *grad_og = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 3);
238242

239243
for (int i = 0; i < frame_size / 8; i++) {
240244
r_value_in = value_in[i];
241245
r_value_ig = value_ig[i];
242246
r_value_fg = value_fg[i];
243247
r_value_og = value_og[i];
244248
if (value.check_ig) {
245-
r_checkI = ((__m256 *)value.check_ig)[i];
246-
r_checkF = ((__m256 *)value.check_fg)[i];
247-
r_checkO = ((__m256 *)value.check_og)[i];
249+
r_checkI = (reinterpret_cast<__m256 *>(value.check_ig))[i];
250+
r_checkF = (reinterpret_cast<__m256 *>(value.check_fg))[i];
251+
r_checkO = (reinterpret_cast<__m256 *>(value.check_og))[i];
248252
}
249-
r_state = ((__m256 *)value.state_value)[i];
250-
r_state_atv = ((__m256 *)value.state_active_value)[i];
251-
r_output_grad = ((__m256 *)grad.output_grad)[i];
252-
r_state_grad = ((__m256 *)grad.state_grad)[i];
253+
r_state = (reinterpret_cast<__m256 *>(value.state_value))[i];
254+
r_state_atv = (reinterpret_cast<__m256 *>(value.state_active_value))[i];
255+
r_output_grad = (reinterpret_cast<__m256 *>(grad.output_grad))[i];
256+
r_state_grad = (reinterpret_cast<__m256 *>(grad.state_grad))[i];
253257
if (value.prev_state_value) {
254-
r_prev_state = ((__m256 *)value.prev_state_value)[i];
258+
r_prev_state = (reinterpret_cast<__m256 *>(value.prev_state_value))[i];
255259
}
256260

257261
op(r_value_in, r_value_ig, r_value_fg, r_value_og, r_grad_in, r_grad_ig,
@@ -264,15 +268,18 @@ void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
264268
grad_ig[i] = r_grad_ig;
265269
grad_fg[i] = r_grad_fg;
266270
grad_og[i] = r_grad_og;
267-
((__m256 *)grad.state_grad)[i] = r_state_grad;
271+
(reinterpret_cast<__m256 *>(grad.state_grad))[i] = r_state_grad;
268272

269273
if (grad.prev_state_grad)
270-
((__m256 *)grad.prev_state_grad)[i] = r_prev_state_grad;
274+
(reinterpret_cast<__m256 *>(grad.prev_state_grad))[i] = r_prev_state_grad;
271275
if (value.prev_state_value) {
272-
if (grad.check_ig_grad) ((__m256 *)grad.check_ig_grad)[i] += r_checkIGrad;
273-
if (grad.check_fg_grad) ((__m256 *)grad.check_fg_grad)[i] += r_checkFGrad;
276+
if (grad.check_ig_grad)
277+
(reinterpret_cast<__m256 *>(grad.check_ig_grad))[i] += r_checkIGrad;
278+
if (grad.check_fg_grad)
279+
(reinterpret_cast<__m256 *>(grad.check_fg_grad))[i] += r_checkFGrad;
274280
}
275-
if (grad.check_og_grad) ((__m256 *)grad.check_og_grad)[i] += r_checkOGrad;
281+
if (grad.check_og_grad)
282+
(reinterpret_cast<__m256 *>(grad.check_og_grad))[i] += r_checkOGrad;
276283
}
277284
#endif
278285
}

0 commit comments

Comments
 (0)