Skip to content

Commit 3a6883a

Browse files
authored
c++ code format (#4527)
1 parent d7bcedf commit 3a6883a

97 files changed

Lines changed: 8852 additions & 7474 deletions

File tree

Some content is hidden

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

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
---
1717
Language: Cpp
1818
BasedOnStyle: Google
19-
IndentWidth: 4
19+
IndentWidth: 2
2020
TabWidth: 2
2121
ContinuationIndentWidth: 4
2222
AccessModifierOffset: -1 # The private/protected/public has no indent in class

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
exclude: |
2+
(?x)^(
3+
dockerfiles/.+
4+
)$
15
default_install_hook_types:
26
- pre-commit
37
- commit-msg
@@ -27,6 +31,15 @@ repos:
2731
hooks:
2832
- id: ruff
2933
args: [--output-format, github, --fix, --line-length=120, --config, pyproject.toml]
34+
# For C++ files
35+
- repo: local
36+
hooks:
37+
- id: clang-format
38+
name: clang-format
39+
description: Format files with ClangFormat.
40+
entry: clang-format -i
41+
language: system
42+
files: \.(c|cc|cxx|cpp|cu|h|cuh|hpp|hxx|xpu|kps)$
3043
# # 拼写检查
3144
# - repo: https://github.com/codespell-project/codespell
3245
# rev: v2.4.1

custom_ops/cpu_ops/avx_weight_only_fake.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ std::vector<paddle::Tensor> InvokeAvxWeightOnly(const paddle::Tensor &x,
1919
const paddle::Tensor &w_bias,
2020
const std::string &alog,
2121
bool trans) {
22-
auto out_shape = x.shape();
23-
out_shape[out_shape.size() - 1] = weight.shape()[1];
24-
auto out = paddle::empty(out_shape, x.dtype(), paddle::CPUPlace());
25-
return {out};
22+
auto out_shape = x.shape();
23+
out_shape[out_shape.size() - 1] = weight.shape()[1];
24+
auto out = paddle::empty(out_shape, x.dtype(), paddle::CPUPlace());
25+
return {out};
2626
}
2727

2828
std::vector<std::vector<int64_t>> AvxWeightOnlyInferShape(
2929
std::vector<int64_t> x_shape,
3030
std::vector<int64_t> weigh_shape,
3131
std::vector<int64_t> weigh_bias_shape) {
32-
int m = 1;
33-
for (int i = 0; i < x_shape.size() - 1; i++) {
34-
m = m * x_shape[i];
35-
}
36-
return {std::vector<int64_t>{m, weigh_shape[1]}};
32+
int m = 1;
33+
for (int i = 0; i < x_shape.size() - 1; i++) {
34+
m = m * x_shape[i];
35+
}
36+
return {std::vector<int64_t>{m, weigh_shape[1]}};
3737
}
3838

3939
std::vector<paddle::DataType> AvxWeightOnlyInferDtype(
4040
paddle::DataType x_dtype,
4141
paddle::DataType weight_dtype,
4242
paddle::DataType weight_bias_dtype) {
43-
return {x_dtype};
43+
return {x_dtype};
4444
}
4545

4646
PD_BUILD_STATIC_OP(avx_weight_only)

custom_ops/cpu_ops/get_padding_offset.cc

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ void remove_padding(int64_t *output_data,
2020
const int *cum_offsets,
2121
const int sequence_length,
2222
const int bsz) {
23-
for (int bi = 0; bi < bsz; ++bi) {
24-
for (int i = 0; i < seq_lens[bi]; ++i) {
25-
const int tgt_seq_id = bi * sequence_length - cum_offsets[bi] + i;
26-
const int src_seq_id = bi * sequence_length + i;
27-
output_data[tgt_seq_id] = input_data[src_seq_id];
28-
}
23+
for (int bi = 0; bi < bsz; ++bi) {
24+
for (int i = 0; i < seq_lens[bi]; ++i) {
25+
const int tgt_seq_id = bi * sequence_length - cum_offsets[bi] + i;
26+
const int src_seq_id = bi * sequence_length + i;
27+
output_data[tgt_seq_id] = input_data[src_seq_id];
2928
}
29+
}
3030
}
3131

3232
void get_padding_offset_kernel(int *padding_offset,
@@ -37,85 +37,77 @@ void get_padding_offset_kernel(int *padding_offset,
3737
const int *seq_lens,
3838
const int max_seq_len,
3939
const int bsz) {
40-
for (int bi = 0; bi < bsz; ++bi) {
41-
int cum_offset = bi == 0 ? 0 : cum_offsets[bi - 1];
42-
auto seq_len_now = seq_lens[bi];
43-
for (int i = 0; i < seq_len_now; ++i) {
44-
padding_offset[bi * max_seq_len - cum_offset + i] = cum_offset;
45-
}
46-
cum_offsets_out[bi] = cum_offset;
47-
int cum_seq_len = (bi + 1) * max_seq_len - cum_offsets[bi];
48-
cu_seqlens_q[bi + 1] = cum_seq_len;
49-
cu_seqlens_k[bi + 1] = cum_seq_len;
40+
for (int bi = 0; bi < bsz; ++bi) {
41+
int cum_offset = bi == 0 ? 0 : cum_offsets[bi - 1];
42+
auto seq_len_now = seq_lens[bi];
43+
for (int i = 0; i < seq_len_now; ++i) {
44+
padding_offset[bi * max_seq_len - cum_offset + i] = cum_offset;
5045
}
46+
cum_offsets_out[bi] = cum_offset;
47+
int cum_seq_len = (bi + 1) * max_seq_len - cum_offsets[bi];
48+
cu_seqlens_q[bi + 1] = cum_seq_len;
49+
cu_seqlens_k[bi + 1] = cum_seq_len;
50+
}
5151
}
5252

5353
std::vector<paddle::Tensor> GetPaddingOffset(const paddle::Tensor &input_ids,
5454
const paddle::Tensor &cum_offsets,
5555
const paddle::Tensor &token_num,
5656
const paddle::Tensor &seq_len) {
57-
std::vector<int64_t> input_ids_shape = input_ids.shape();
58-
const int bsz = seq_len.shape()[0];
59-
const int seq_length = input_ids_shape[1];
60-
auto cum_offsets_out = cum_offsets.copy_to(paddle::CPUPlace(), false);
61-
auto cpu_token_num = token_num.copy_to(paddle::CPUPlace(), false);
57+
std::vector<int64_t> input_ids_shape = input_ids.shape();
58+
const int bsz = seq_len.shape()[0];
59+
const int seq_length = input_ids_shape[1];
60+
auto cum_offsets_out = cum_offsets.copy_to(paddle::CPUPlace(), false);
61+
auto cpu_token_num = token_num.copy_to(paddle::CPUPlace(), false);
6262

63-
const int token_num_data = cpu_token_num.data<int64_t>()[0];
64-
auto x_remove_padding = paddle::empty(
65-
{token_num_data}, paddle::DataType::INT64, input_ids.place());
66-
auto padding_offset = paddle::empty(
67-
{token_num_data}, paddle::DataType::INT32, input_ids.place());
68-
auto cu_seqlens_q =
69-
paddle::full({bsz + 1}, 0, paddle::DataType::INT32, input_ids.place());
70-
auto cu_seqlens_k =
71-
paddle::full({bsz + 1}, 0, paddle::DataType::INT32, input_ids.place());
72-
get_padding_offset_kernel(padding_offset.data<int>(),
73-
cum_offsets_out.data<int>(),
74-
cu_seqlens_q.data<int>(),
75-
cu_seqlens_k.data<int>(),
76-
cum_offsets.data<int>(),
77-
seq_len.data<int>(),
78-
seq_length,
79-
bsz);
80-
remove_padding(x_remove_padding.data<int64_t>(),
81-
input_ids.data<int64_t>(),
82-
seq_len.data<int>(),
83-
cum_offsets_out.data<int>(),
84-
seq_length,
85-
bsz);
86-
return {x_remove_padding,
87-
padding_offset,
88-
cu_seqlens_q,
89-
cu_seqlens_k};
63+
const int token_num_data = cpu_token_num.data<int64_t>()[0];
64+
auto x_remove_padding = paddle::empty(
65+
{token_num_data}, paddle::DataType::INT64, input_ids.place());
66+
auto padding_offset = paddle::empty(
67+
{token_num_data}, paddle::DataType::INT32, input_ids.place());
68+
auto cu_seqlens_q =
69+
paddle::full({bsz + 1}, 0, paddle::DataType::INT32, input_ids.place());
70+
auto cu_seqlens_k =
71+
paddle::full({bsz + 1}, 0, paddle::DataType::INT32, input_ids.place());
72+
get_padding_offset_kernel(padding_offset.data<int>(),
73+
cum_offsets_out.data<int>(),
74+
cu_seqlens_q.data<int>(),
75+
cu_seqlens_k.data<int>(),
76+
cum_offsets.data<int>(),
77+
seq_len.data<int>(),
78+
seq_length,
79+
bsz);
80+
remove_padding(x_remove_padding.data<int64_t>(),
81+
input_ids.data<int64_t>(),
82+
seq_len.data<int>(),
83+
cum_offsets_out.data<int>(),
84+
seq_length,
85+
bsz);
86+
return {x_remove_padding, padding_offset, cu_seqlens_q, cu_seqlens_k};
9087
}
9188

9289
std::vector<std::vector<int64_t>> GetPaddingOffsetInferShape(
9390
const std::vector<int64_t> &input_ids_shape,
9491
const std::vector<int64_t> &cum_offsets_shape,
9592
const std::vector<int64_t> &token_num_shape,
9693
const std::vector<int64_t> &seq_len_shape) {
97-
int64_t bsz = seq_len_shape[0];
98-
int64_t seq_len = input_ids_shape[1];
99-
return {{-1}, {-1}, {bsz + 1}, {bsz + 1}};
94+
int64_t bsz = seq_len_shape[0];
95+
int64_t seq_len = input_ids_shape[1];
96+
return {{-1}, {-1}, {bsz + 1}, {bsz + 1}};
10097
}
10198

10299
std::vector<paddle::DataType> GetPaddingOffsetInferDtype(
103100
const paddle::DataType &input_ids_dtype,
104101
const paddle::DataType &cum_offsets_dtype,
105102
const paddle::DataType &token_num_dtype,
106103
const paddle::DataType &seq_len_dtype) {
107-
return {input_ids_dtype,
108-
seq_len_dtype,
109-
seq_len_dtype,
110-
seq_len_dtype};
104+
return {input_ids_dtype, seq_len_dtype, seq_len_dtype, seq_len_dtype};
111105
}
112106

113107
PD_BUILD_STATIC_OP(get_padding_offset_cpu)
114108
.Inputs({"input_ids", "cum_offsets", "token_num", "seq_len"})
115-
.Outputs({"x_remove_padding",
116-
"padding_offset",
117-
"cu_seqlens_q",
118-
"cu_seqlens_k"})
109+
.Outputs(
110+
{"x_remove_padding", "padding_offset", "cu_seqlens_q", "cu_seqlens_k"})
119111
.SetKernelFn(PD_KERNEL(GetPaddingOffset))
120112
.SetInferShapeFn(PD_INFER_SHAPE(GetPaddingOffsetInferShape))
121113
.SetInferDtypeFn(PD_INFER_DTYPE(GetPaddingOffsetInferDtype));

0 commit comments

Comments
 (0)