Skip to content

Commit 88728c4

Browse files
jxgxxxlyd1992
andcommitted
The issues raised in the comments have been addressed.
Co-authored-by: typer-J <2236066784@qq.com> Co-authored-by: Sherlockzhangjinge <zjgzhangjinge@outlook.com> Co-authored-by: lyd1992 <liuyudong@iscas.ac.cn>
1 parent 32729f9 commit 88728c4

12 files changed

Lines changed: 711 additions & 791 deletions

.clang-format

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
# MNN C++ Code Style
3+
# Based on Google style with project-specific overrides
4+
# Usage: clang-format -i -style=file <file>
5+
6+
BasedOnStyle: Google
7+
Language: Cpp
8+
9+
# Indentation
10+
IndentWidth: 4
11+
ContinuationIndentWidth: 4
12+
AccessModifierOffset: -4
13+
ConstructorInitializerIndentWidth: 4
14+
15+
# Line width
16+
ColumnLimit: 120
17+
18+
# Braces
19+
BreakBeforeBraces: Attach
20+
AllowShortFunctionsOnASingleLine: Inline
21+
AllowShortIfStatementsOnASingleLine: false
22+
AllowShortLoopsOnASingleLine: false
23+
AllowShortBlocksOnASingleLine: false
24+
25+
# Includes
26+
SortIncludes: Never
27+
IncludeBlocks: Preserve
28+
29+
# Alignment
30+
# Note: The codebase has some manual consecutive-assignment alignment, but enabling
31+
# AlignConsecutiveAssignments globally causes more harm than good (misaligns isolated lines).
32+
# Leave it off; manually aligned blocks are preserved as-is when untouched.
33+
AlignConsecutiveAssignments: false
34+
AlignConsecutiveDeclarations: false
35+
AlignTrailingComments: true
36+
37+
# Pointers & References
38+
DerivePointerAlignment: false
39+
PointerAlignment: Left
40+
41+
# Misc
42+
SpaceAfterCStyleCast: false
43+
SpaceBeforeParens: ControlStatements
44+
SpacesBeforeTrailingComments: 1
45+
Standard: c++11
46+
TabWidth: 4
47+
UseTab: Never
48+
MaxEmptyLinesToKeep: 1
49+
NamespaceIndentation: None
50+
FixNamespaceComments: true
51+
ReflowComments: true
52+
53+
# Penalty tuning (prefer not breaking certain constructs)
54+
PenaltyBreakBeforeFirstCallParameter: 19
55+
PenaltyBreakComment: 300
56+
PenaltyBreakString: 1000
57+
PenaltyExcessCharacter: 1000000
58+
PenaltyReturnTypeOnItsOwnLine: 60
59+
---
60+

source/backend/cpu/compute/CommonOptFunction.cpp

Lines changed: 609 additions & 589 deletions
Large diffs are not rendered by default.

source/backend/cpu/riscv/rvv/MNNC3ToBGR555.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

source/backend/cpu/riscv/rvv/MNNDynamicUpdateConvBiasScale.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ void MNNDynamicUpdateConvBiasScale_RVV(float* newbias, float* oldbias, float* we
1818

1919
__riscv_vse32_v_f32m4(newbias + i, v_new, vl);
2020
}
21-
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdint.h>
2+
void MNNGetMatMulPackMode_RVV(int* eP, int* lP, int* hP) {
3+
*eP = 16;
4+
*lP = 1;
5+
*hP = 4;
6+
}

source/backend/cpu/riscv/rvv/MNNPackC4ForMatMul_A.cpp

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <riscv_vector.h>
2-
#include <algorithm>
2+
#include <stdint.h>
33

4-
#define TILE_E_SIZE 1024
5-
6-
void MNNPackC4ForMatMul_A(float *destOrigin, float const **sourceGroup, const int32_t *info, const int32_t *el) {
4+
void MNNPackC4ForMatMul_A_RVV(float* destOrigin, float const** sourceGroup, const int32_t* info, const int32_t* el) {
75
int number = info[0];
86
int eReal = info[1];
97
int eDest = info[2];
@@ -14,54 +12,27 @@ void MNNPackC4ForMatMul_A(float *destOrigin, float const **sourceGroup, const in
1412
int l = el[4 * n + 1];
1513
int eOffset = el[4 * n + 2];
1614
int lOffset = el[4 * n + 3];
17-
auto destBase = destOrigin + lOffset * eDest + eOffset;
18-
auto source = sourceGroup[n];
19-
int limit = l / 4 * 4;
20-
int x = 0;
21-
22-
for (; x < limit; x += 4) {
23-
auto xC = x / 4;
24-
const float *sourcePtrBase = source + xC * eReal * 4;
25-
float *destPtrCol0 = destBase + (x + 0) * eDest;
26-
float *destPtrCol1 = destBase + (x + 1) * eDest;
27-
float *destPtrCol2 = destBase + (x + 2) * eDest;
28-
float *destPtrCol3 = destBase + (x + 3) * eDest;
15+
auto dest = destOrigin + lOffset * eDest + eOffset;
16+
auto source = sourceGroup[n];
2917

30-
for (int yBase = 0; yBase < e; yBase += TILE_E_SIZE) {
31-
int eBlock = std::min(e - yBase, TILE_E_SIZE);
18+
for (int y = 0; y < e; ++y) {
19+
auto yR = y % eDest;
20+
auto destY = dest + yR;
21+
auto sourceY = source + y * 4 * offset;
3222

33-
for (int yOffset = 0; yOffset < eBlock; ) {
34-
size_t vl = __riscv_vsetvl_e32m8(eBlock - yOffset);
35-
const float *sourceYPtr = sourcePtrBase + (yBase + yOffset) * 4 * offset;
36-
const size_t sourceStride = 4 * offset * sizeof(float);
23+
int x = 0;
24+
size_t vl = __riscv_vsetvl_e32m1(4);
3725

38-
vfloat32m8_t col0 = __riscv_vlse32_v_f32m8(sourceYPtr + 0, sourceStride, vl);
39-
vfloat32m8_t col1 = __riscv_vlse32_v_f32m8(sourceYPtr + 1, sourceStride, vl);
40-
vfloat32m8_t col2 = __riscv_vlse32_v_f32m8(sourceYPtr + 2, sourceStride, vl);
41-
vfloat32m8_t col3 = __riscv_vlse32_v_f32m8(sourceYPtr + 3, sourceStride, vl);
42-
43-
__riscv_vse32_v_f32m8(destPtrCol0 + yBase + yOffset, col0, vl);
44-
__riscv_vse32_v_f32m8(destPtrCol1 + yBase + yOffset, col1, vl);
45-
__riscv_vse32_v_f32m8(destPtrCol2 + yBase + yOffset, col2, vl);
46-
__riscv_vse32_v_f32m8(destPtrCol3 + yBase + yOffset, col3, vl);
47-
48-
yOffset += vl;
49-
}
26+
for (; x <= l - 4; x += 4) {
27+
int xC = x / 4;
28+
vfloat32m1_t v_src = __riscv_vle32_v_f32m1(sourceY + xC * eReal * 4, vl);
29+
__riscv_vsse32_v_f32m1(destY + x * eDest, eDest * sizeof(float), v_src, vl);
5030
}
51-
}
52-
53-
for (; x < l; ++x) {
54-
auto xC = x / 4;
55-
auto xR = x % 4;
56-
const float* sourcePtrBase = source + xC * eReal * 4 + xR;
57-
float* destPtrCol = destBase + x * eDest;
5831

59-
for (int yBase = 0; yBase < e; yBase += TILE_E_SIZE) {
60-
int eBlock = std::min(e - yBase, TILE_E_SIZE);
61-
for (int yOffset = 0; yOffset < eBlock; ++yOffset) {
62-
int y = yBase + yOffset;
63-
destPtrCol[y] = sourcePtrBase[y * 4 * offset];
64-
}
32+
for (; x < l; ++x) {
33+
int xR = x % 4;
34+
int xC = x / 4;
35+
destY[x * eDest] = sourceY[xC * eReal * 4 + xR];
6536
}
6637
}
6738
}

source/backend/cpu/riscv/rvv/MNNPackC4ForMatMul_A_RVV.cpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

source/backend/cpu/riscv/rvv/MNNPackForMatMul_B.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
#include <stdint.h>
44
#include <stddef.h>
55

6-
#define SME2_MATMUL_LP 1
7-
#define SME2_MATMUL_HP 64
8-
#define ROUND_UP(x, y) (((x) + (y) - 1) / (y) * (y))
6+
#define RVV_MATMUL_LP 1
7+
#define RVV_MATMUL_HP 64
98

109
void MNNPackForMatMul_B_RVV(float* destC, const float* sourceC, size_t h, size_t kernelsize, size_t ic,
1110
bool transpose) {
1211
auto dest = (int32_t*)destC;
1312
auto source = (int32_t*)sourceC;
1413

15-
int LP = SME2_MATMUL_LP;
16-
int HP = SME2_MATMUL_HP;
14+
int LP = RVV_MATMUL_LP;
15+
int HP = RVV_MATMUL_HP;
1716
auto l = kernelsize * ic;
1817

1918
size_t dest_size = ROUND_UP(h, HP) * ROUND_UP(ic, LP) * kernelsize * 4;
@@ -66,4 +65,4 @@ void MNNPackForMatMul_B_RVV(float* destC, const float* sourceC, size_t h, size_t
6665
}
6766
}
6867
}
69-
}
68+
}
Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,8 @@
1-
#include <riscv_vector.h>
2-
#include <algorithm>
3-
#include <limits>
41
#include <stddef.h>
52

6-
#define UP_DIV(x, y) (((x) + (y) - 1) / (y))
7-
8-
void MNNPackedMatMulFP32(float* C, const float* A, const float* B, const size_t* parameter, const float* postParameters,
9-
const float* bias, const float* k, const float* b) {
10-
const size_t eSize = 16;
11-
12-
size_t aStride = parameter[0] / sizeof(float);
13-
size_t l = parameter[1];
14-
size_t h = parameter[2];
15-
size_t cStride = parameter[3] / sizeof(float);
16-
size_t bExtraStride = parameter[5] / sizeof(float);
17-
size_t bStride = bExtraStride + l * 4;
18-
19-
size_t hC4 = UP_DIV(h, 4);
20-
21-
float minValue = -std::numeric_limits<float>::max();
22-
float maxValue = std::numeric_limits<float>::max();
23-
if (postParameters != nullptr) {
24-
minValue = postParameters[2];
25-
maxValue = postParameters[3];
26-
}
27-
28-
size_t vl = __riscv_vsetvl_e32m4(eSize);
29-
30-
for (size_t y = 0; y < hC4; ++y) {
31-
float* c_base = C + y * cStride;
32-
const float* b_base = B + y * bStride;
33-
const float* bias_y = bias ? bias + 4 * y : nullptr;
34-
35-
vfloat32m4_t acc0, acc1, acc2, acc3;
36-
if (bias_y) {
37-
acc0 = __riscv_vfmv_v_f_f32m4(bias_y[0], vl);
38-
acc1 = __riscv_vfmv_v_f_f32m4(bias_y[1], vl);
39-
acc2 = __riscv_vfmv_v_f_f32m4(bias_y[2], vl);
40-
acc3 = __riscv_vfmv_v_f_f32m4(bias_y[3], vl);
41-
} else {
42-
acc0 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
43-
acc1 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
44-
acc2 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
45-
acc3 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
46-
}
47-
48-
for (size_t z = 0; z < l; ++z) {
49-
vfloat32m4_t a_vec = __riscv_vle32_v_f32m4(A + z * aStride, vl);
50-
const float* w_ptr = b_base + z * 4;
51-
52-
acc0 = __riscv_vfmacc_vf_f32m4(acc0, w_ptr[0], a_vec, vl);
53-
acc1 = __riscv_vfmacc_vf_f32m4(acc1, w_ptr[1], a_vec, vl);
54-
acc2 = __riscv_vfmacc_vf_f32m4(acc2, w_ptr[2], a_vec, vl);
55-
acc3 = __riscv_vfmacc_vf_f32m4(acc3, w_ptr[3], a_vec, vl);
56-
}
57-
58-
acc0 = __riscv_vfmin_vf_f32m4(__riscv_vfmax_vf_f32m4(acc0, minValue, vl), maxValue, vl);
59-
acc1 = __riscv_vfmin_vf_f32m4(__riscv_vfmax_vf_f32m4(acc1, minValue, vl), maxValue, vl);
60-
acc2 = __riscv_vfmin_vf_f32m4(__riscv_vfmax_vf_f32m4(acc2, minValue, vl), maxValue, vl);
61-
acc3 = __riscv_vfmin_vf_f32m4(__riscv_vfmax_vf_f32m4(acc3, minValue, vl), maxValue, vl);
62-
63-
ptrdiff_t stride = 4 * sizeof(float);
64-
65-
__riscv_vsse32_v_f32m4(c_base + 0, stride, acc0, vl);
66-
__riscv_vsse32_v_f32m4(c_base + 1, stride, acc1, vl);
67-
__riscv_vsse32_v_f32m4(c_base + 2, stride, acc2, vl);
68-
__riscv_vsse32_v_f32m4(c_base + 3, stride, acc3, vl);
69-
}
70-
}
3+
void MNNPackedMatMulRemainFP32_RVV(float* C, const float* A, const float* B, size_t eSize, const size_t* parameter,
4+
const float* postParameters, const float* bias, const float* k, const float* b);
5+
void MNNPackedMatMulFP32_RVV(float* C, const float* A, const float* B, const size_t* parameter,
6+
const float* postParameters, const float* bias, const float* k, const float* b) {
7+
MNNPackedMatMulRemainFP32_RVV(C, A, B, 16, parameter, postParameters, bias, k, b);
8+
}

source/backend/cpu/riscv/rvv/MNNPackedMatMulRemainFP32.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
#include <algorithm>
33
#include <limits>
44
#include <stddef.h>
5-
6-
#define UP_DIV(x, y) (((x) + (y) - 1) / (y))
7-
void MNNPackedMatMulRemainFP32(float* C, const float* A, const float* B, size_t eSize, const size_t* parameter,
8-
const float* postParameters, const float* bias, const float* k, const float* b) {
5+
void MNNPackedMatMulRemainFP32_RVV(float* C, const float* A, const float* B, size_t eSize, const size_t* parameter,
6+
const float* postParameters, const float* bias, const float* k, const float* b) {
97
if (eSize == 0)
108
return;
119

@@ -67,4 +65,4 @@ void MNNPackedMatMulRemainFP32(float* C, const float* A, const float* B, size_t
6765
__riscv_vsse32_v_f32m4(c_base + 2, stride, acc2, vl);
6866
__riscv_vsse32_v_f32m4(c_base + 3, stride, acc3, vl);
6967
}
70-
}
68+
}

0 commit comments

Comments
 (0)