Skip to content

Commit

Permalink
test x86 arm convolution oom (#5492)
Browse files Browse the repository at this point in the history
* skip mips loongarch riscv oom test atm

* test softmax oom
  • Loading branch information
nihui authored Jun 11, 2024
1 parent 03ca905 commit da7d1a1
Show file tree
Hide file tree
Showing 6 changed files with 789 additions and 337 deletions.
54 changes: 27 additions & 27 deletions .ci/test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_AVX2=ON -DNCNN_AVX512=OFF -DNCNN_XOP=OFF -DNCNN_OPENMP=OFF -DNCNN_VULKAN=ON -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
printf "[Processor]\nThreadCount=4\n" > build/tests/SwiftShader.ini
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_AVX2=ON -DNCNN_AVX512=OFF -DNCNN_XOP=OFF -DNCNN_OPENMP=OFF -DNCNN_VULKAN=ON -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export LP_NUM_THREADS=4
Expand Down Expand Up @@ -230,9 +230,9 @@ jobs:
-DNCNN_AVX512BF16=${{matrix.AVX512BF16}} \
-DNCNN_AVX512FP16=${{matrix.AVX512FP16}} \
..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: cd build && ctest --output-on-failure -j $(nproc)
run: cd build && ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -309,12 +309,12 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabi.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_GNU_INLINE_ASM=${{matrix.GNU_INLINE_ASM}} -DNCNN_VFPV4=ON -DNCNN_ARM82=OFF -DNCNN_OPENMP=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-arm TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/arm-linux-gnueabi" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-arm TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/arm-linux-gnueabi" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand All @@ -327,12 +327,12 @@ jobs:
run: |
mkdir build-armhf-vfpv3-d16 && cd build-armhf-vfpv3-d16
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf-vfpv3-d16.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_GNU_INLINE_ASM=${{matrix.GNU_INLINE_ASM}} -DNCNN_VFPV4=OFF -DNCNN_ARM82=OFF -DNCNN_OPENMP=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test-armhf-vfpv3-d16
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build-armhf-vfpv3-d16
TESTS_EXECUTABLE_LOADER=qemu-arm TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/arm-linux-gnueabihf" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-arm TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/arm-linux-gnueabihf" ctest --output-on-failure -j 4
- name: lcov-collect-armhf-vfpv3-d16
run: |
cd build-armhf-vfpv3-d16
Expand Down Expand Up @@ -423,12 +423,12 @@ jobs:
-DNCNN_ARM84BF16=${{matrix.ARM84BF16}} \
-DNCNN_ARM84I8MM=${{matrix.ARM84I8MM}} \
..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-aarch64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/aarch64-linux-gnu" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-aarch64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/aarch64-linux-gnu" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -502,12 +502,12 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/mipsisa32r6el-linux-gnu.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_MSA=OFF -DNCNN_MMI=OFF -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-mipsel TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/mipsisa32r6el-linux-gnu" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-mipsel TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/mipsisa32r6el-linux-gnu" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -581,12 +581,12 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/mipsisa64r6el-linux-gnuabi64.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_MSA=ON -DNCNN_MMI=OFF -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-mips64el TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/mipsisa64r6el-linux-gnuabi64" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-mips64el TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/mipsisa64r6el-linux-gnuabi64" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -660,12 +660,12 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/powerpc-linux-gnu.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-ppc TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/powerpc-linux-gnu" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-ppc TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/powerpc-linux-gnu" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -739,12 +739,12 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/powerpc64le-linux-gnu.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-ppc64le TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/powerpc64le-linux-gnu" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-ppc64le TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/powerpc64le-linux-gnu" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -824,12 +824,12 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/riscv64-linux-gnu.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-riscv64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/riscv64-linux-gnu" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-riscv64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;/usr/riscv64-linux-gnu" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -951,12 +951,12 @@ jobs:
export RISCV_ROOT_PATH=${{ci.workspace}}/rv64gcv-install
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/riscv64-unknown-linux-gnu.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DCMAKE_C_FLAGS="-O1" -DCMAKE_CXX_FLAGS="-O1" -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_RVV=ON -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test-vlen128
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-riscv64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-cpu;rv64,v=true,Zfh=true,x-zvfh=true,vlen=128,elen=64,vext_spec=v1.0;-L;${{ci.workspace}}/rv64gcv-install/sysroot" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-riscv64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-cpu;rv64,v=true,Zfh=true,x-zvfh=true,vlen=128,elen=64,vext_spec=v1.0;-L;${{ci.workspace}}/rv64gcv-install/sysroot" ctest --output-on-failure -j 4
- name: lcov-collect-vlen128
run: |
cd build
Expand All @@ -971,7 +971,7 @@ jobs:
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-riscv64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-cpu;rv64,v=true,Zfh=true,x-zvfh=true,vlen=256,elen=64,vext_spec=v1.0;-L;${{ci.workspace}}/rv64gcv-install/sysroot" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-riscv64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-cpu;rv64,v=true,Zfh=true,x-zvfh=true,vlen=256,elen=64,vext_spec=v1.0;-L;${{ci.workspace}}/rv64gcv-install/sysroot" ctest --output-on-failure -j 4
- name: lcov-collect-vlen256
run: |
cd build
Expand Down Expand Up @@ -1051,12 +1051,12 @@ jobs:
export LOONGARCH64_ROOT_PATH=${{ci.workspace}}/cross-tools
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/loongarch64-unknown-linux-gnu.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_RUNTIME_CPU=OFF -DNCNN_LSX=ON -DNCNN_LASX=OFF -DNCNN_OPENMP=${{matrix.OPENMP}} -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF -DNCNN_BUILD_TESTS=ON ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: |
export PATH=${{ci.workspace}}/qemu-install/bin:$PATH
cd build
TESTS_EXECUTABLE_LOADER=qemu-loongarch64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;${{ci.workspace}}/cross-tools/target" ctest --output-on-failure -j $(nproc)
TESTS_EXECUTABLE_LOADER=qemu-loongarch64 TESTS_EXECUTABLE_LOADER_ARGUMENTS="-L;${{ci.workspace}}/cross-tools/target" ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down Expand Up @@ -1099,9 +1099,9 @@ jobs:
run: |
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/host-c.gcc.toolchain.cmake -DCMAKE_BUILD_TYPE=debug -DNCNN_COVERAGE=ON -DNCNN_STDIO=ON -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_SIMPLEMATH=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF ..
cmake --build . -j $(nproc)
cmake --build . -j 4
- name: test
run: cd build && ctest --output-on-failure -j $(nproc)
run: cd build && ctest --output-on-failure -j 4
- name: lcov-collect
run: |
cd build
Expand Down
6 changes: 4 additions & 2 deletions src/layer/convolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ int Convolution::forward(const Mat& bottom_blob, Mat& top_blob, const Option& op
op->create_pipeline(opt);

// forward
op->forward(bottom_blob, top_blob, opt);
int ret = op->forward(bottom_blob, top_blob, opt);

op->destroy_pipeline(opt);

delete op;

return 0;
return ret;
}
}

Expand Down Expand Up @@ -401,6 +401,8 @@ int Convolution::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Optio
opt_g.blob_allocator = opt.workspace_allocator;

quantize_to_int8(bottom_blob, bottom_blob_unbordered, bottom_blob_int8_scales, opt_g);
if (bottom_blob_unbordered.empty())
return -100;
}

Mat bottom_blob_bordered;
Expand Down
149 changes: 149 additions & 0 deletions tests/test_convolution_oom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#include "testutil.h"

static int test_convolution_oom(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias)
{
ncnn::Mat a = RandomMat(w, h, c);

ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, kernel);
pd.set(2, dilation);
pd.set(3, stride);
pd.set(4, pad);
pd.set(5, bias);
pd.set(6, outch * c * kernel * kernel);

int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
ncnn::Mat activation_params(2);
activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);

std::vector<ncnn::Mat> weights(bias ? 2 : 1);
weights[0] = RandomMat(outch * c * kernel * kernel);
if (bias)
weights[1] = RandomMat(outch);

int ret = test_layer_oom("Convolution", pd, weights, a);
if (ret != 0)
{
fprintf(stderr, "test_convolution_oom failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
return ret;
}

return ret;
}

static int test_convolution_0()
{
return 0
|| test_convolution_oom(9, 7, 31, 63, 1, 1, 1, 0, 1)
|| test_convolution_oom(9, 7, 31, 63, 3, 1, 1, 1, 1);
}

#if NCNN_INT8
static int test_convolution_oom_int8(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, bool requant = false)
{
ncnn::Mat a = RandomMat(w, h, c);

ncnn::ParamDict pd;
pd.set(0, outch);
pd.set(1, kernel);
pd.set(2, dilation);
pd.set(3, stride);
pd.set(4, pad);
pd.set(5, bias);
pd.set(6, outch * c * kernel * kernel);
pd.set(8, requant ? 101 : 1); // int8_scale_term

int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
ncnn::Mat activation_params(2);
activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
activation_params[1] = RandomFloat(0, 1); // beta
pd.set(9, activation_type);
pd.set(10, activation_params);

std::vector<ncnn::Mat> weights(bias ? 5 : 4);
weights[0] = RandomMat(outch * c * kernel * kernel);

ncnn::Mat weight_scales = scales_mat(weights[0], outch, c * kernel * kernel, c * kernel * kernel);
ncnn::Mat input_scales = scales_mat(a, 1, w * h * c, a.cstep);
ncnn::Mat top_scales = requant ? scales_mat(a, 1, w * h * c, a.cstep) : ncnn::Mat();

if (kernel == 3 && dilation == 1 && stride == 1)
{
// test for 6bit quant
for (int i = 0; i < weight_scales.w; i++)
weight_scales[i] = weight_scales[i] / 4.f;
}

if (bias)
{
weights[1] = RandomMat(outch);
weights[2] = weight_scales;
weights[3] = input_scales;
weights[4] = top_scales;
}
else
{
weights[1] = weight_scales;
weights[2] = input_scales;
weights[3] = top_scales;
}

int flag = TEST_LAYER_DISABLE_GPU_TESTING;
int ret = test_layer_oom("Convolution", pd, weights, a, flag);
if (ret != 0)
{
fprintf(stderr, "test_convolution_oom_int8 failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d requant=%d act=%d actparams=[%f,%f]\n", w, h, c, outch, kernel, dilation, stride, pad, bias, requant, activation_type, activation_params[0], activation_params[1]);
return ret;
}

return ret;
}

static int test_convolution_1()
{
return 0
|| test_convolution_oom_int8(9, 7, 31, 63, 1, 1, 1, 0, 1)
|| test_convolution_oom_int8(9, 7, 31, 63, 3, 1, 1, 1, 1);
}

static int test_convolution_2()
{
return 0
|| test_convolution_oom_int8(9, 7, 31, 63, 1, 1, 1, 0, 1, true)
|| test_convolution_oom_int8(9, 7, 31, 63, 3, 1, 1, 1, 1, true);
}
#endif // NCNN_INT8

int main()
{
SRAND(7767517);

#if __mips__ || __loongarch64 || __riscv
// TODO
return 0;
#endif

#if NCNN_INT8
return test_convolution_0() || test_convolution_1() || test_convolution_2();
#else
return test_convolution_0();
#endif
}
Loading

0 comments on commit da7d1a1

Please sign in to comment.