Skip to content

Commit

Permalink
fix riscv requantize/dequantize packing
Browse files Browse the repository at this point in the history
Signed-off-by: Molly Sophia <[email protected]>
  • Loading branch information
MollySophia committed Mar 1, 2024
1 parent cc24397 commit 4936e80
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
24 changes: 12 additions & 12 deletions src/layer/riscv/dequantize_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int Dequantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
float* ptr = (float*)top_blob + i * packn;

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = vfmacc_vf_f32m4(_v, scale, _bias, vl);
_v = vfmacc_vf_f32m4(_bias, scale, _v, vl);
vse32_v_f32m4(ptr, _v, vl);
}
}
Expand All @@ -97,7 +97,7 @@ int Dequantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio

vfloat32m4_t _bias = vle32_v_f32m4((const float*)bias_data + i * packn, vl);
vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = vfmacc_vf_f32m4(_v, scale, _bias, vl);
_v = vfmacc_vf_f32m4(_bias, scale, _v, vl);
vse32_v_f32m4(ptr, _v, vl);
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ int Dequantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio

vfloat32m4_t _scale = vle32_v_f32m4((const float*)scale_data + i * packn, vl);
vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = vfmacc_vv_f32m4(_v, _scale, _bias, vl);
_v = vfmacc_vv_f32m4(_bias, _scale, _v, vl);
vse32_v_f32m4(ptr, _v, vl);
}
}
Expand All @@ -145,7 +145,7 @@ int Dequantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _scale = vle32_v_f32m4((const float*)scale_data + i * packn, vl);
vfloat32m4_t _bias = vle32_v_f32m4((const float*)bias_data + i * packn, vl);
vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = vfmacc_vv_f32m4(_v, _scale, _bias, vl);
_v = vfmacc_vv_f32m4(_bias, _scale, _v, vl);
vse32_v_f32m4(ptr, _v, vl);
}
}
Expand Down Expand Up @@ -227,10 +227,10 @@ int Dequantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _v1 = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr + packn, vl), vl);
vfloat32m4_t _v2 = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr + packn * 2, vl), vl);
vfloat32m4_t _v3 = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr + packn * 3, vl), vl);
_v0 = vfmacc_vv_f32m4(_v0, _scale0, _bias0, vl);
_v1 = vfmacc_vv_f32m4(_v1, _scale1, _bias1, vl);
_v2 = vfmacc_vv_f32m4(_v2, _scale2, _bias2, vl);
_v3 = vfmacc_vv_f32m4(_v3, _scale3, _bias3, vl);
_v0 = vfmacc_vv_f32m4(_bias0, _scale0, _v0, vl);
_v1 = vfmacc_vv_f32m4(_bias1, _scale1, _v1, vl);
_v2 = vfmacc_vv_f32m4(_bias2, _scale2, _v2, vl);
_v3 = vfmacc_vv_f32m4(_bias3, _scale3, _v3, vl);
vse32_v_f32m4(ptr0, _v0, vl);
vse32_v_f32m4(ptr1, _v1, vl);
vse32_v_f32m4(ptr2, _v2, vl);
Expand Down Expand Up @@ -323,10 +323,10 @@ int Dequantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _v1 = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr + packn, vl), vl);
vfloat32m4_t _v2 = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr + packn * 2, vl), vl);
vfloat32m4_t _v3 = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr + packn * 3, vl), vl);
_v0 = vfmacc_vv_f32m4(_v0, _scale0, _bias0, vl);
_v1 = vfmacc_vv_f32m4(_v1, _scale1, _bias1, vl);
_v2 = vfmacc_vv_f32m4(_v2, _scale2, _bias2, vl);
_v3 = vfmacc_vv_f32m4(_v3, _scale3, _bias3, vl);
_v0 = vfmacc_vv_f32m4(_bias0, _scale0, _v0, vl);
_v1 = vfmacc_vv_f32m4(_bias1, _scale1, _v1, vl);
_v2 = vfmacc_vv_f32m4(_bias2, _scale2, _v2, vl);
_v3 = vfmacc_vv_f32m4(_bias3, _scale3, _v3, vl);
vse32_v_f32m4(ptr0, _v0, vl);
vse32_v_f32m4(ptr1, _v1, vl);
vse32_v_f32m4(ptr2, _v2, vl);
Expand Down
2 changes: 0 additions & 2 deletions src/layer/riscv/quantize_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ int Quantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Option&
int size = w * h;
int out_elempack = opt.use_packing_layout && channels * elempack % out_packn == 0 ? out_packn : 1;
int outc = channels * elempack / out_elempack;
NCNN_LOGE("out_elempack:%d", out_elempack);
NCNN_LOGE("outc:%d", outc);

top_blob.create(w, h, outc, (size_t)out_elempack, out_elempack, opt.blob_allocator);
if (top_blob.empty())
Expand Down
20 changes: 10 additions & 10 deletions src/layer/riscv/requantize_riscv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
signed char* ptr = (signed char*)top_blob + i * packn;

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vf_f32m4(_v, scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vf_f32m4(_bias, scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vf_f32m4(_v, scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand All @@ -98,7 +98,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio

vfloat32m4_t _bias = vle32_v_f32m4((const float*)bias_data + i * packn, vl);
vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vf_f32m4(_v, scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vf_f32m4(_bias, scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vf_f32m4(_v, scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _scale_out = vle32_v_f32m4((const float*)scale_out_data + i * packn, vl);

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vf_f32m4(_v, scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vf_f32m4(_bias, scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vv_f32m4(_v, _scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand All @@ -154,7 +154,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _bias = vle32_v_f32m4((const float*)bias_data + i * packn, vl);

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vf_f32m4(_v, scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vf_f32m4(_bias, scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vv_f32m4(_v, _scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _scale_in = vle32_v_f32m4((const float*)scale_in_data + i * packn, vl);

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vv_f32m4(_v, _scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vv_f32m4(_bias, _scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vf_f32m4(_v, scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand All @@ -210,7 +210,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _bias = vle32_v_f32m4((const float*)bias_data + i * packn, vl);

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vv_f32m4(_v, _scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vv_f32m4(_bias, _scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vf_f32m4(_v, scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand Down Expand Up @@ -249,7 +249,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _scale_out = vle32_v_f32m4((const float*)scale_out_data + i * packn, vl);

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vv_f32m4(_v, _scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vv_f32m4(_bias, _scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vv_f32m4(_v, _scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand All @@ -267,7 +267,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
vfloat32m4_t _bias = vle32_v_f32m4((const float*)bias_data + i * packn, vl);

vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vv_f32m4(_v, _scale_in, _bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vv_f32m4(_bias, _scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vv_f32m4(_v, _scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);
}
Expand Down Expand Up @@ -322,7 +322,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
for (int j = 0; j < w; j++)
{
vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vv_f32m4(_v, scale_in, bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vv_f32m4(bias, scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vv_f32m4(_v, scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);

Expand Down Expand Up @@ -394,7 +394,7 @@ int Requantize_riscv::forward(const Mat& bottom_blob, Mat& top_blob, const Optio
for (int i = 0; i < size; i++)
{
vfloat32m4_t _v = vfcvt_f_x_v_f32m4(vle32_v_i32m4(intptr, vl), vl);
_v = activation_ps(vfmacc_vv_f32m4(_v, scale_in, bias, vl), activation_type, activation_params, vl);
_v = activation_ps(vfmacc_vv_f32m4(bias, scale_in, _v, vl), activation_type, activation_params, vl);
vint8m1_t _out = float2int8(vfmul_vv_f32m4(_v, scale_out, vl), vl);
vse8_v_i8m1(ptr, _out, vl);

Expand Down

0 comments on commit 4936e80

Please sign in to comment.