Skip to content

Commit ff5b554

Browse files
authored
restrict one dim quantize scale size, test quantize oom (Tencent#5892)
* restrict one dim quantize scale size * sse2 requantize pack8
1 parent 956bccd commit ff5b554

File tree

9 files changed

+2061
-2943
lines changed

9 files changed

+2061
-2943
lines changed

src/layer/arm/quantize_arm.cpp

Lines changed: 597 additions & 739 deletions
Large diffs are not rendered by default.

src/layer/arm/quantize_arm_asimdhp.cpp

Lines changed: 475 additions & 727 deletions
Large diffs are not rendered by default.

src/layer/loongarch/quantize_loongarch.cpp

Lines changed: 226 additions & 398 deletions
Large diffs are not rendered by default.

src/layer/mips/quantize_mips.cpp

Lines changed: 226 additions & 398 deletions
Large diffs are not rendered by default.

src/layer/quantize.cpp

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,72 +46,59 @@ static inline signed char float2int8(float v)
4646
return (signed char)int32;
4747
}
4848

49+
static void quantize(const float* ptr, signed char* s8ptr, float scale, int size)
50+
{
51+
for (int i = 0; i < size; i++)
52+
{
53+
*s8ptr = float2int8(*ptr * scale);
54+
ptr++;
55+
s8ptr++;
56+
}
57+
}
58+
4959
int Quantize::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
5060
{
51-
int dims = bottom_blob.dims;
61+
const int dims = bottom_blob.dims;
62+
const int w = bottom_blob.w;
63+
const int h = bottom_blob.h;
64+
const int channels = bottom_blob.c;
5265

5366
if (dims == 1)
5467
{
55-
int w = bottom_blob.w;
56-
5768
top_blob.create(w, (size_t)1u, opt.blob_allocator);
5869
if (top_blob.empty())
5970
return -100;
6071

72+
// assert scale_data_size == 1
73+
6174
const float* ptr = bottom_blob;
62-
signed char* outptr = top_blob;
75+
signed char* s8ptr = top_blob;
6376

64-
if (scale_data_size == 1)
65-
{
66-
const float scale = scale_data[0];
77+
const float scale = scale_data[0];
6778

68-
#pragma omp parallel for num_threads(opt.num_threads)
69-
for (int i = 0; i < w; i++)
70-
{
71-
outptr[i] = float2int8(ptr[i] * scale);
72-
}
73-
}
74-
else
75-
{
76-
#pragma omp parallel for num_threads(opt.num_threads)
77-
for (int i = 0; i < w; i++)
78-
{
79-
outptr[i] = float2int8(ptr[i] * scale_data[i]);
80-
}
81-
}
79+
quantize(ptr, s8ptr, scale, w);
8280
}
8381

8482
if (dims == 2)
8583
{
86-
int w = bottom_blob.w;
87-
int h = bottom_blob.h;
88-
8984
top_blob.create(w, h, (size_t)1u, opt.blob_allocator);
9085
if (top_blob.empty())
9186
return -100;
9287

9388
#pragma omp parallel for num_threads(opt.num_threads)
9489
for (int i = 0; i < h; i++)
9590
{
96-
const float* ptr0 = bottom_blob.row(i);
97-
signed char* outptr0 = top_blob.row<signed char>(i);
91+
const float* ptr = bottom_blob.row(i);
92+
signed char* s8ptr = top_blob.row<signed char>(i);
9893

9994
const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
10095

101-
for (int j = 0; j < w; j++)
102-
{
103-
outptr0[j] = float2int8(ptr0[j] * scale);
104-
}
96+
quantize(ptr, s8ptr, scale, w);
10597
}
10698
}
10799

108100
if (dims == 3)
109101
{
110-
int w = bottom_blob.w;
111-
int h = bottom_blob.h;
112-
int channels = bottom_blob.c;
113-
int size = w * h;
114-
115102
top_blob.create(w, h, channels, (size_t)1u, opt.blob_allocator);
116103
if (top_blob.empty())
117104
return -100;
@@ -120,14 +107,11 @@ int Quantize::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
120107
for (int q = 0; q < channels; q++)
121108
{
122109
const float* ptr = bottom_blob.channel(q);
123-
signed char* outptr = top_blob.channel(q);
110+
signed char* s8ptr = top_blob.channel(q);
124111

125112
const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
126113

127-
for (int i = 0; i < size; i++)
128-
{
129-
outptr[i] = float2int8(ptr[i] * scale);
130-
}
114+
quantize(ptr, s8ptr, scale, w * h);
131115
}
132116
}
133117

0 commit comments

Comments
 (0)