feat: 切换后端至PaddleOCR-NCNN,切换工程为CMake

1.项目后端整体迁移至PaddleOCR-NCNN算法,已通过基本的兼容性测试
2.工程改为使用CMake组织,后续为了更好地兼容第三方库,不再提供QMake工程
3.重整权利声明文件,重整代码工程,确保最小化侵权风险

Log: 切换后端至PaddleOCR-NCNN,切换工程为CMake
Change-Id: I4d5d2c5d37505a4a24b389b1a4c5d12f17bfa38c
This commit is contained in:
wangzhengyang
2022-05-10 09:54:44 +08:00
parent ecdd171c6f
commit 718c41634f
10018 changed files with 3593797 additions and 186748 deletions

192
3rdparty/ncnn/python/tests/benchmark.py vendored Normal file
View File

@ -0,0 +1,192 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 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.
import sys
import time
import ncnn
param_root = "../benchmark/"
g_warmup_loop_count = 8
g_loop_count = 4
g_enable_cooling_down = True
g_vkdev = None
g_blob_vkallocator = None
g_staging_vkallocator = None
g_blob_pool_allocator = ncnn.UnlockedPoolAllocator()
g_workspace_pool_allocator = ncnn.PoolAllocator()
def benchmark(comment, _in, opt):
_in.fill(0.01)
g_blob_pool_allocator.clear()
g_workspace_pool_allocator.clear()
if opt.use_vulkan_compute:
g_blob_vkallocator.clear()
g_staging_vkallocator.clear()
net = ncnn.Net()
net.opt = opt
if net.opt.use_vulkan_compute:
net.set_vulkan_device(g_vkdev)
net.load_param(param_root + comment + ".param")
dr = ncnn.DataReaderFromEmpty()
net.load_model(dr)
input_names = net.input_names()
output_names = net.output_names()
if g_enable_cooling_down:
time.sleep(10)
# warm up
for i in range(g_warmup_loop_count):
# test with statement
with net.create_extractor() as ex:
ex.input(input_names[0], _in)
ex.extract(output_names[0])
time_min = sys.float_info.max
time_max = -sys.float_info.max
time_avg = 0.0
for i in range(g_loop_count):
start = time.time()
# test net keep alive until ex freed
ex = net.create_extractor()
ex.input(input_names[0], _in)
ex.extract(output_names[0])
end = time.time()
timespan = end - start
time_min = timespan if timespan < time_min else time_min
time_max = timespan if timespan > time_max else time_max
time_avg += timespan
time_avg /= g_loop_count
print(
"%20s min = %7.2f max = %7.2f avg = %7.2f"
% (comment, time_min * 1000, time_max * 1000, time_avg * 1000)
)
if __name__ == "__main__":
loop_count = 4
num_threads = ncnn.get_cpu_count()
powersave = 0
gpu_device = -1
cooling_down = 1
argc = len(sys.argv)
if argc >= 2:
loop_count = int(sys.argv[1])
if argc >= 3:
num_threads = int(sys.argv[2])
if argc >= 4:
powersave = int(sys.argv[3])
if argc >= 5:
gpu_device = int(sys.argv[4])
if argc >= 6:
cooling_down = int(sys.argv[5])
use_vulkan_compute = gpu_device != -1
g_enable_cooling_down = cooling_down != 0
g_loop_count = loop_count
g_blob_pool_allocator.set_size_compare_ratio(0.0)
g_workspace_pool_allocator.set_size_compare_ratio(0.5)
if use_vulkan_compute:
g_warmup_loop_count = 10
g_vkdev = ncnn.get_gpu_device(gpu_device)
g_blob_vkallocator = ncnn.VkBlobAllocator(g_vkdev)
g_staging_vkallocator = ncnn.VkStagingAllocator(g_vkdev)
opt = ncnn.Option()
opt.lightmode = True
opt.num_threads = num_threads
opt.blob_allocator = g_blob_pool_allocator
opt.workspace_allocator = g_workspace_pool_allocator
if use_vulkan_compute:
opt.blob_vkallocator = g_blob_vkallocator
opt.workspace_vkallocator = g_blob_vkallocator
opt.staging_vkallocator = g_staging_vkallocator
opt.use_winograd_convolution = True
opt.use_sgemm_convolution = True
opt.use_int8_inference = True
opt.use_vulkan_compute = use_vulkan_compute
opt.use_fp16_packed = True
opt.use_fp16_storage = True
opt.use_fp16_arithmetic = True
opt.use_int8_storage = True
opt.use_int8_arithmetic = True
opt.use_packing_layout = True
opt.use_shader_pack8 = False
opt.use_image_storage = False
ncnn.set_cpu_powersave(powersave)
ncnn.set_omp_dynamic(0)
ncnn.set_omp_num_threads(num_threads)
print("loop_count =", loop_count)
print("num_threads =", num_threads)
print("powersave =", ncnn.get_cpu_powersave())
print("gpu_device =", gpu_device)
print("cooling_down =", g_enable_cooling_down)
benchmark("squeezenet", ncnn.Mat((227, 227, 3)), opt)
benchmark("squeezenet_int8", ncnn.Mat((227, 227, 3)), opt)
benchmark("mobilenet", ncnn.Mat((224, 224, 3)), opt)
benchmark("mobilenet_int8", ncnn.Mat((224, 224, 3)), opt)
benchmark("mobilenet_v2", ncnn.Mat((224, 224, 3)), opt)
# benchmark("mobilenet_v2_int8", ncnn.Mat(w=224, h=224, c=3), opt)
benchmark("mobilenet_v3", ncnn.Mat((224, 224, 3)), opt)
benchmark("shufflenet", ncnn.Mat((224, 224, 3)), opt)
benchmark("shufflenet_v2", ncnn.Mat((224, 224, 3)), opt)
benchmark("mnasnet", ncnn.Mat((224, 224, 3)), opt)
benchmark("proxylessnasnet", ncnn.Mat((224, 224, 3)), opt)
benchmark("efficientnet_b0", ncnn.Mat((224, 224, 3)), opt)
benchmark("regnety_400m", ncnn.Mat((224, 224, 3)), opt)
benchmark("blazeface", ncnn.Mat((128, 128, 3)), opt)
benchmark("googlenet", ncnn.Mat((224, 224, 3)), opt)
benchmark("googlenet_int8", ncnn.Mat((224, 224, 3)), opt)
benchmark("resnet18", ncnn.Mat((224, 224, 3)), opt)
benchmark("resnet18_int8", ncnn.Mat((224, 224, 3)), opt)
benchmark("alexnet", ncnn.Mat((227, 227, 3)), opt)
benchmark("vgg16", ncnn.Mat((224, 224, 3)), opt)
benchmark("vgg16_int8", ncnn.Mat((224, 224, 3)), opt)
benchmark("resnet50", ncnn.Mat((224, 224, 3)), opt)
benchmark("resnet50_int8", ncnn.Mat((224, 224, 3)), opt)
benchmark("squeezenet_ssd", ncnn.Mat((300, 300, 3)), opt)
benchmark("squeezenet_ssd_int8", ncnn.Mat((300, 300, 3)), opt)
benchmark("mobilenet_ssd", ncnn.Mat((300, 300, 3)), opt)
benchmark("mobilenet_ssd_int8", ncnn.Mat((300, 300, 3)), opt)
benchmark("mobilenet_yolo", ncnn.Mat((416, 416, 3)), opt)
benchmark("mobilenetv2_yolov3", ncnn.Mat((352, 352, 3)), opt)
benchmark("yolov4-tiny", ncnn.Mat((416, 416, 3)), opt)

View File

@ -0,0 +1,4 @@
7767517
2 2
Input data 0 1 data
CustomLayer cl_fwd 1 1 data output

5
3rdparty/ncnn/python/tests/test.param vendored Normal file
View File

@ -0,0 +1,5 @@
7767517
3 3
Input data 0 1 data
Convolution conv0_fwd 1 1 data conv0_fwd 0=3 1=3 11=3 2=1 12=1 3=1 13=1 4=0 14=0 5=1 6=81
InnerProduct dense0_fwd 1 1 conv0_fwd output 0=1 1=1 2=151875

View File

@ -0,0 +1,37 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 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.
import pytest
import ncnn
def test_pool_allocator():
pa = ncnn.PoolAllocator()
assert pa is not None
pa.set_size_compare_ratio(0.5)
buf = pa.fastMalloc(10 * 1024)
assert buf is not None
pa.fastFree(buf)
pa.clear()
def test_unlocked_pool_allocator():
upa = ncnn.UnlockedPoolAllocator()
assert upa is not None
upa.set_size_compare_ratio(0.5)
buf = upa.fastMalloc(10 * 1024)
assert buf is not None
upa.fastFree(buf)
upa.clear()

33
3rdparty/ncnn/python/tests/test_blob.py vendored Normal file
View File

@ -0,0 +1,33 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 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.
import pytest
import ncnn
def test_blob():
blob = ncnn.Blob()
blob.name = "myblob"
assert blob.name == "myblob"
blob.producer = 0
assert blob.producer == 0
blob.consumer = 0
assert blob.consumer == 0
blob.shape = ncnn.Mat(1)
assert blob.shape.dims == 1 and blob.shape.w == 1

View File

@ -0,0 +1,86 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2021 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.
import pytest
import ncnn
alloctor = ncnn.PoolAllocator()
def test_extractor():
with pytest.raises(TypeError, match="No constructor"):
ex = ncnn.Extractor()
dr = ncnn.DataReaderFromEmpty()
net = ncnn.Net()
net.load_param("tests/test.param")
net.load_model(dr)
in_mat = ncnn.Mat((227, 227, 3))
with net.create_extractor() as ex:
ex.set_light_mode(True)
ex.set_num_threads(2)
ex.set_blob_allocator(alloctor)
ex.set_workspace_allocator(alloctor)
ex.input("data", in_mat)
ret, out_mat = ex.extract("conv0_fwd")
assert (
ret == 0
and out_mat.dims == 3
and out_mat.w == 225
and out_mat.h == 225
and out_mat.c == 3
)
ret, out_mat = ex.extract("output")
assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
def test_extractor_index():
with pytest.raises(TypeError, match="No constructor"):
ex = ncnn.Extractor()
dr = ncnn.DataReaderFromEmpty()
net = ncnn.Net()
net.load_param("tests/test.param")
net.load_model(dr)
in_mat = ncnn.Mat((227, 227, 3))
ex = net.create_extractor()
ex.set_light_mode(True)
ex.set_num_threads(2)
ex.set_blob_allocator(alloctor)
ex.set_workspace_allocator(alloctor)
ex.input(0, in_mat)
ret, out_mat = ex.extract(1)
assert (
ret == 0
and out_mat.dims == 3
and out_mat.w == 225
and out_mat.h == 225
and out_mat.c == 3
)
ret, out_mat = ex.extract(2)
assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
# not use with sentence, call clear manually to ensure ex destruct before net
ex.clear()

762
3rdparty/ncnn/python/tests/test_mat.py vendored Normal file
View File

@ -0,0 +1,762 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 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.
import sys
import numpy as np
import pytest
import ncnn
def test_mat_dims1():
mat = ncnn.Mat(1)
assert mat.dims == 1 and mat.w == 1
mat = ncnn.Mat(2, elemsize=4)
assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
mat = ncnn.Mat(3, elemsize=4, elempack=1)
assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
mat = ncnn.Mat(4, elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 1
and mat.w == 4
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
mat = ncnn.Mat((1,))
assert mat.dims == 1 and mat.w == 1
mat = ncnn.Mat((2,), elemsize=4)
assert mat.dims == 1 and mat.w == 2 and mat.elemsize == 4
mat = ncnn.Mat((3,), elemsize=4, elempack=1)
assert mat.dims == 1 and mat.w == 3 and mat.elemsize == 4 and mat.elempack == 1
mat = ncnn.Mat((4,), elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 1
and mat.w == 4
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
def test_mat_dims2():
mat = ncnn.Mat(1, 2)
assert mat.dims == 2 and mat.w == 1 and mat.h == 2
mat = ncnn.Mat(3, 4, elemsize=4)
assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
mat = ncnn.Mat(5, 6, elemsize=4, elempack=1)
assert (
mat.dims == 2
and mat.w == 5
and mat.h == 6
and mat.elemsize == 4
and mat.elempack == 1
)
mat = ncnn.Mat(7, 8, elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 2
and mat.w == 7
and mat.h == 8
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
mat = ncnn.Mat((1, 2))
assert mat.dims == 2 and mat.w == 1 and mat.h == 2
mat = ncnn.Mat((3, 4), elemsize=4)
assert mat.dims == 2 and mat.w == 3 and mat.h == 4 and mat.elemsize == 4
mat = ncnn.Mat((5, 6), elemsize=4, elempack=1)
assert (
mat.dims == 2
and mat.w == 5
and mat.h == 6
and mat.elemsize == 4
and mat.elempack == 1
)
mat = ncnn.Mat((7, 8), elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 2
and mat.w == 7
and mat.h == 8
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
def test_mat_dims3():
mat = ncnn.Mat(1, 2, 3)
assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
mat = ncnn.Mat(4, 5, 6, elemsize=4)
assert (
mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
)
mat = ncnn.Mat(7, 8, 9, elemsize=4, elempack=1)
assert (
mat.dims == 3
and mat.w == 7
and mat.h == 8
and mat.c == 9
and mat.elemsize == 4
and mat.elempack == 1
)
mat = ncnn.Mat(10, 11, 12, elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 3
and mat.w == 10
and mat.h == 11
and mat.c == 12
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
mat = ncnn.Mat((1, 2, 3))
assert mat.dims == 3 and mat.w == 1 and mat.h == 2 and mat.c == 3
mat = ncnn.Mat((4, 5, 6), elemsize=4)
assert (
mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6 and mat.elemsize == 4
)
mat = ncnn.Mat((7, 8, 9), elemsize=4, elempack=1)
assert (
mat.dims == 3
and mat.w == 7
and mat.h == 8
and mat.c == 9
and mat.elemsize == 4
and mat.elempack == 1
)
mat = ncnn.Mat((10, 11, 12), elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 3
and mat.w == 10
and mat.h == 11
and mat.c == 12
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
def test_mat_dims4():
mat = ncnn.Mat(1, 2, 3, 4)
assert mat.dims == 4 and mat.w == 1 and mat.h == 2 and mat.d == 3 and mat.c == 4
mat = ncnn.Mat(4, 5, 6, 7, elemsize=4)
assert (
mat.dims == 4 and mat.w == 4 and mat.h == 5 and mat.d == 6 and mat.c == 7 and mat.elemsize == 4
)
mat = ncnn.Mat(7, 8, 9, 10, elemsize=4, elempack=1)
assert (
mat.dims == 4
and mat.w == 7
and mat.h == 8
and mat.d == 9
and mat.c == 10
and mat.elemsize == 4
and mat.elempack == 1
)
mat = ncnn.Mat(10, 11, 12, 13, elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 4
and mat.w == 10
and mat.h == 11
and mat.d == 12
and mat.c == 13
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
mat = ncnn.Mat((1, 2, 3, 4))
assert mat.dims == 4 and mat.w == 1 and mat.h == 2 and mat.d == 3 and mat.c == 4
mat = ncnn.Mat((4, 5, 6, 7), elemsize=4)
assert (
mat.dims == 4 and mat.w == 4 and mat.h == 5 and mat.d == 6 and mat.c == 7 and mat.elemsize == 4
)
mat = ncnn.Mat((7, 8, 9, 10), elemsize=4, elempack=1)
assert (
mat.dims == 4
and mat.w == 7
and mat.h == 8
and mat.d == 9
and mat.c == 10
and mat.elemsize == 4
and mat.elempack == 1
)
mat = ncnn.Mat((10, 11, 12, 13), elemsize=4, elempack=1, allocator=None)
assert (
mat.dims == 4
and mat.w == 10
and mat.h == 11
and mat.d == 12
and mat.c == 13
and mat.elemsize == 4
and mat.elempack == 1
and mat.allocator == None
)
def test_numpy():
mat = ncnn.Mat(1)
array = np.array(mat)
assert mat.dims == array.ndim and mat.w == array.shape[0]
mat = ncnn.Mat(2, 3)
array = np.array(mat)
assert (
mat.dims == array.ndim and mat.w == array.shape[1] and mat.h == array.shape[0]
)
mat = ncnn.Mat(4, 5, 6)
array = np.array(mat)
assert (
mat.dims == array.ndim
and mat.w == array.shape[2]
and mat.h == array.shape[1]
and mat.c == array.shape[0]
)
mat = ncnn.Mat(7, 8, 9, 10)
array = np.array(mat)
assert (
mat.dims == array.ndim
and mat.w == array.shape[3]
and mat.h == array.shape[2]
and mat.d == array.shape[1]
and mat.c == array.shape[0]
)
mat = ncnn.Mat(1, elemsize=1)
array = np.array(mat)
assert array.dtype == np.int8
mat = ncnn.Mat(1, elemsize=2)
array = np.array(mat)
assert array.dtype == np.float16
# pybind11 def_buffer throw bug
# with pytest.raises(RuntimeError) as execinfo:
# mat = ncnn.Mat(1, elemsize=3)
# array = np.array(mat)
# assert "convert ncnn.Mat to numpy.ndarray only elemsize 1, 2, 4 support now, but given 3" in str(
# execinfo.value
# )
assert array.dtype == np.float16
mat = ncnn.Mat(1, elemsize=4)
array = np.array(mat)
assert array.dtype == np.float32
mat = np.random.randint(0, 128, size=(12,)).astype(np.uint8)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.rand(12).astype(np.float32)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.randint(0, 128, size=(12, 11)).astype(np.uint8)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.rand(12, 11).astype(np.float32)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.randint(0, 256, size=(12, 11, 3)).astype(np.uint8)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.rand(12, 11, 3).astype(np.float32)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.randint(0, 256, size=(12, 11, 7, 3)).astype(np.uint8)
array = np.array(mat)
assert (mat == array).all()
mat = np.random.rand(12, 11, 7, 3).astype(np.float32)
array = np.array(mat)
assert (mat == array).all()
def test_fill():
mat = ncnn.Mat(1)
mat.fill(1.0)
array = np.array(mat)
assert np.abs(array[0] - 1.0) < sys.float_info.min
def test_clone():
mat1 = ncnn.Mat(1)
mat2 = mat1.clone()
assert mat1.dims == mat2.dims and mat1.w == mat2.w
mat1 = ncnn.Mat(2, 3)
mat2 = mat1.clone()
assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
mat1 = ncnn.Mat(4, 5, 6)
mat2 = mat1.clone()
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.c == mat2.c
)
mat1 = ncnn.Mat(7, 8, 9, 10)
mat2 = mat1.clone()
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.d == mat2.d
and mat1.c == mat2.c
)
mat1 = ncnn.Mat((1,))
mat2 = mat1.clone()
assert mat1.dims == mat2.dims and mat1.w == mat2.w
mat1 = ncnn.Mat((2, 3))
mat2 = mat1.clone()
assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
mat1 = ncnn.Mat((4, 5, 6))
mat2 = mat1.clone()
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.c == mat2.c
)
mat1 = ncnn.Mat((7, 8, 9, 10))
mat2 = mat1.clone()
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.d == mat2.d
and mat1.c == mat2.c
)
def test_clone_from():
mat2 = ncnn.Mat()
mat1 = ncnn.Mat(1)
mat2.clone_from(mat1)
assert mat1.dims == mat2.dims and mat1.w == mat2.w
mat1 = ncnn.Mat(2, 3)
mat2.clone_from(mat1)
assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
mat1 = ncnn.Mat(4, 5, 6)
mat2.clone_from(mat1)
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.c == mat2.c
)
mat1 = ncnn.Mat(7, 8, 9, 10)
mat2.clone_from(mat1)
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.d == mat2.d
and mat1.c == mat2.c
)
mat1 = ncnn.Mat((1,))
mat2.clone_from(mat1)
assert mat1.dims == mat2.dims and mat1.w == mat2.w
mat1 = ncnn.Mat((2, 3))
mat2.clone_from(mat1)
assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
mat1 = ncnn.Mat((4, 5, 6))
mat2.clone_from(mat1)
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.c == mat2.c
)
mat1 = ncnn.Mat((7, 8, 9, 10))
mat2.clone_from(mat1)
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.d == mat2.d
and mat1.c == mat2.c
)
def test_reshape():
mat1 = ncnn.Mat()
mat2 = mat1.reshape(1)
assert mat2.dims == 0
mat2 = mat1.reshape(1, 1)
assert mat2.dims == 0
mat2 = mat1.reshape(1, 1, 1)
assert mat2.dims == 0
mat2 = mat1.reshape(1, 1, 1, 1)
assert mat2.dims == 0
mat1 = ncnn.Mat(1)
mat2 = mat1.reshape(1, 1)
assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
mat2 = mat1.reshape(1, 1, 1)
assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
mat2 = mat1.reshape(1, 1, 1, 1)
assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
mat1 = ncnn.Mat(1, 2)
mat2 = mat1.reshape(2)
assert mat2.dims == 1 and mat2.w == 2
mat2 = mat1.reshape(2, 1)
assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
mat2 = mat1.reshape(2, 1, 1)
assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
mat2 = mat1.reshape(2, 1, 1, 1)
assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
mat1 = ncnn.Mat(1, 2, 3)
mat2 = mat1.reshape(6)
assert mat2.dims == 1 and mat2.w == 6
mat2 = mat1.reshape(2, 3)
assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3
mat2 = mat1.reshape(2, 3, 1)
assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
mat2 = mat1.reshape(2, 1, 3, 1)
assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1
mat1 = ncnn.Mat((1,))
mat2 = mat1.reshape((1, 1))
assert mat2.dims == 2 and mat2.w == 1 and mat2.h == 1
mat2 = mat1.reshape((1, 1, 1))
assert mat2.dims == 3 and mat2.w == 1 and mat2.h == 1 and mat2.c == 1
mat2 = mat1.reshape((1, 1, 1, 1))
assert mat2.dims == 4 and mat2.w == 1 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
mat1 = ncnn.Mat((1, 2))
mat2 = mat1.reshape((2,))
assert mat2.dims == 1 and mat2.w == 2
mat2 = mat1.reshape((2, 1))
assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 1
mat2 = mat1.reshape((2, 1, 1))
assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 1 and mat2.c == 1
mat2 = mat1.reshape((2, 1, 1, 1))
assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 1 and mat2.c == 1
mat1 = ncnn.Mat((1, 2, 3))
mat2 = mat1.reshape((6,))
assert mat2.dims == 1 and mat2.w == 6
mat2 = mat1.reshape((2, 3))
assert mat2.dims == 2 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
mat2 = mat1.reshape((2, 3, 1))
assert mat2.dims == 3 and mat2.w == 2 and mat2.h == 3 and mat2.c == 1
mat2 = mat1.reshape((2, 1, 3, 1))
assert mat2.dims == 4 and mat2.w == 2 and mat2.h == 1 and mat2.d == 3 and mat2.c == 1
with pytest.raises(RuntimeError) as execinfo:
mat1.reshape((1, 1, 1, 1, 1))
assert "shape must be 1, 2, 3 or 4 dims, not 5" in str(execinfo.value)
def test_create():
mat = ncnn.Mat()
mat.create(1)
assert mat.dims == 1 and mat.w == 1
mat.create(2, 3)
assert mat.dims == 2 and mat.w == 2 and mat.h == 3
mat.create(4, 5, 6)
assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
mat.create(7, 8, 9, 10)
assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10
mat.create((1,))
assert mat.dims == 1 and mat.w == 1
mat.create((2, 3))
assert mat.dims == 2 and mat.w == 2 and mat.h == 3
mat.create((4, 5, 6))
assert mat.dims == 3 and mat.w == 4 and mat.h == 5 and mat.c == 6
mat.create((7, 8, 9, 10))
assert mat.dims == 4 and mat.w == 7 and mat.h == 8 and mat.d == 9 and mat.c == 10
def test_create_like():
mat2 = ncnn.Mat()
mat1 = ncnn.Mat(1)
mat2.create_like(mat1)
assert mat1.dims == mat2.dims and mat1.w == mat2.w
mat1 = ncnn.Mat(2, 3)
mat2.create_like(mat1)
assert mat1.dims == mat2.dims and mat1.w == mat2.w and mat1.h == mat2.h
mat1 = ncnn.Mat(4, 5, 6)
mat2.create_like(mat1)
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.c == mat2.c
)
mat1 = ncnn.Mat(7, 8, 9, 10)
mat2.create_like(mat1)
assert (
mat1.dims == mat2.dims
and mat1.w == mat2.w
and mat1.h == mat2.h
and mat1.d == mat2.d
and mat1.c == mat2.c
)
def test_addref_release():
mat = ncnn.Mat(1)
assert mat.refcount == 1
mat.addref()
assert mat.refcount == 2
mat.release()
assert mat.refcount == None
def test_empty():
mat = ncnn.Mat()
assert mat.empty() == True
mat = ncnn.Mat(1)
assert mat.empty() == False
def test_total():
mat = ncnn.Mat(1)
assert mat.total() == 1
mat = ncnn.Mat(2, 3)
assert mat.total() == 2 * 3
mat = ncnn.Mat(4, 5, 6)
assert mat.total() == 4 * 5 * 6
mat = ncnn.Mat(7, 8, 9, 10)
assert mat.total() == 7 * 8 * 9 * 10
def test_elembits():
mat = ncnn.Mat(1, elemsize=1, elempack=1)
assert mat.elembits() == 8
mat = ncnn.Mat(2, elemsize=2, elempack=1)
assert mat.elembits() == 16
mat = ncnn.Mat(3, elemsize=4, elempack=1)
assert mat.elembits() == 32
def test_shape():
mat = ncnn.Mat(1)
shape = mat.shape()
assert shape.dims == 1 and shape.w == 1
mat = ncnn.Mat(2, 3)
shape = mat.shape()
assert shape.dims == 2 and shape.w == 2 and shape.h == 3
mat = ncnn.Mat(4, 5, 6)
shape = mat.shape()
assert shape.dims == 3 and shape.w == 4 and shape.h == 5 and shape.c == 6
mat = ncnn.Mat(7, 8, 9, 10)
shape = mat.shape()
assert shape.dims == 4 and shape.w == 7 and shape.h == 8 and shape.d == 9 and shape.c == 10
def test_channel_depth_row():
mat = ncnn.Mat(2, 3, 4, 5)
mat.fill(6.0)
channel = mat.channel(1)
assert channel.dims == 3 and channel.w == 2 and channel.h == 3 and channel.c == 4
depth = channel.depth(1)
assert depth.dims == 2 and depth.w == 2 and depth.h == 3
row = depth.row(1)
assert len(row) == 2 and np.abs(row[0] - 6.0) < sys.float_info.min
def test_channel_row():
mat = ncnn.Mat(2, 3, 4)
mat.fill(4.0)
channel = mat.channel(1)
assert channel.dims == 2 and channel.w == 2 and channel.h == 3 and channel.c == 1
row = channel.row(1)
assert len(row) == 2 and np.abs(row[0] - 4.0) < sys.float_info.min
def test_channel_range():
mat = ncnn.Mat(1, 2, 3)
channel_range = mat.channel_range(0, 2)
assert (
channel_range.dims == 3
and channel_range.w == 1
and channel_range.h == 2
and channel_range.c == 2
)
def test_depth_range():
mat = ncnn.Mat(1, 2, 3, 4)
depth_range = mat.channel(1).depth_range(1, 2)
assert (
depth_range.dims == 3
and depth_range.w == 1
and depth_range.h == 2
and depth_range.c == 2
)
def test_row_range():
mat = ncnn.Mat(1, 2)
row_range = mat.row_range(0, 2)
assert row_range.dims == 2 and row_range.w == 1 and row_range.h == 2
def test_range():
mat = ncnn.Mat(2)
range = mat.range(0, 2)
assert range.dims == 1 and range.w == 2
def test_getitem_setitem():
mat = ncnn.Mat(2)
mat.fill(1)
assert (
np.abs(mat[0] - 1.0) < sys.float_info.min
and np.abs(mat[1] - 1.0) < sys.float_info.min
)
mat[0] = 2.0
assert (
np.abs(mat[0] - 2.0) < sys.float_info.min
and np.abs(mat[1] - 1.0) < sys.float_info.min
)
def test_from_pixels():
pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels(
pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, stride=500 * 3
) # chw
assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
assert pixels[0, 0, 0] == mat.channel(0).row(0)[0]
assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
assert pixels[299, 399, 2] == mat.channel(2).row(299)[399]
def test_from_pixels_resize():
pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_resize(
pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 200, 150
) # chw
assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_resize(
pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 400, 300
) # chw
assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_resize(
pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 200, 150
) # chw
assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_resize(
pixels, ncnn.Mat.PixelType.PIXEL_BGR2RGB, 400, 300, 500 * 3, 400, 300
) # chw
assert mat.dims == 3 and mat.w == 400 and mat.h == 300 and mat.c == 3
assert pixels[0, 0, 0] == mat.channel(2).row(0)[0]
assert pixels[200, 150, 1] == mat.channel(1).row(200)[150]
assert pixels[299, 399, 2] == mat.channel(0).row(299)[399]
def test_from_pixels_roi():
pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_roi(
pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150
) # chw
assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_roi(
pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 500 * 3, 100, 75, 200, 150
) # chw
assert mat.dims == 3 and mat.w == 200 and mat.h == 150 and mat.c == 3
assert pixels[75, 100, 0] == mat.channel(0).row(0)[0]
assert pixels[150, 200, 1] == mat.channel(1).row(75)[100]
assert pixels[224, 299, 2] == mat.channel(2).row(149)[199]
def test_from_pixels_roi_resize():
pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_roi_resize(
pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300, 100, 75, 200, 150, 100, 75
) # chw
assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
pixels = np.random.randint(0, 256, size=(300, 500, 3)).astype(np.uint8) # hwc
mat = ncnn.Mat.from_pixels_roi_resize(
pixels,
ncnn.Mat.PixelType.PIXEL_RGB,
400,
300,
500 * 3,
100,
75,
200,
150,
100,
75,
) # chw
assert mat.dims == 3 and mat.w == 100 and mat.h == 75 and mat.c == 3
def test_substract_mean_normalize():
pixels = np.random.randint(0, 256, size=(300, 400, 3)).astype(np.uint8) # hwc
mean_vals = [127.5, 127.5, 127.5]
norm_vals = [0.007843, 0.007843, 0.007843]
mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
mat.substract_mean_normalize([], norm_vals)
assert np.abs(pixels[0, 0, 0] * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
mat.substract_mean_normalize(mean_vals, [])
assert np.abs((pixels[0, 0, 0] - 127.5) - mat.channel(0).row(0)[0]) < 1e-5
mat = ncnn.Mat.from_pixels(pixels, ncnn.Mat.PixelType.PIXEL_RGB, 400, 300) # chw
mat.substract_mean_normalize(mean_vals, norm_vals)
assert (
np.abs((pixels[0, 0, 0] - 127.5) * 0.007843 - mat.channel(0).row(0)[0]) < 1e-5
)

144
3rdparty/ncnn/python/tests/test_net.py vendored Normal file
View File

@ -0,0 +1,144 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2021 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.
import numpy as np
import pytest
import ncnn
def test_net():
dr = ncnn.DataReaderFromEmpty()
with ncnn.Net() as net:
ret = net.load_param("tests/test.param")
net.load_model(dr)
assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3
input_names = net.input_names()
output_names = net.output_names()
assert len(input_names) > 0 and len(output_names) > 0
in_mat = ncnn.Mat((227, 227, 3))
with net.create_extractor() as ex:
ex.input("data", in_mat)
ret, out_mat = ex.extract("output")
assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
net.clear()
assert len(net.blobs()) == 0 and len(net.layers()) == 0
def test_net_vulkan():
if not hasattr(ncnn, "get_gpu_count"):
return
dr = ncnn.DataReaderFromEmpty()
net = ncnn.Net()
net.opt.use_vulkan_compute = True
ret = net.load_param("tests/test.param")
net.load_model(dr)
assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3
in_mat = ncnn.Mat((227, 227, 3))
ex = net.create_extractor()
ex.input("data", in_mat)
ret, out_mat = ex.extract("output")
assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
ex.clear()
net.clear()
assert len(net.blobs()) == 0 and len(net.layers()) == 0
def test_custom_layer():
class CustomLayer(ncnn.Layer):
customLayers = []
def __init__(self):
ncnn.Layer.__init__(self)
self.one_blob_only = True
self.customLayers.append(self)
def forward(self, bottom_blob, top_blob, opt):
x = np.array(bottom_blob)
x += 1
top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator)
if top_blob.empty():
return -100
return 0
def CustomLayer_layer_creator():
return CustomLayer()
def CustomLayer_layer_destroyer(layer):
for i in range(len(CustomLayer.customLayers)):
if CustomLayer.customLayers[i] == layer:
del CustomLayer.customLayers[i]
break
dr = ncnn.DataReaderFromEmpty()
net = ncnn.Net()
net.register_custom_layer(
"CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer
)
ret = net.load_param("tests/custom_layer.param")
net.load_model(dr)
assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2
in_mat = ncnn.Mat(1)
in_mat.fill(1.0)
ex = net.create_extractor()
ex.input("data", in_mat)
ret, out_mat = ex.extract("output")
assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[0] == 2.0
ex.clear()
net.clear()
assert len(net.blobs()) == 0 and len(net.layers()) == 0
def test_vulkan_device_index():
if not hasattr(ncnn, "get_gpu_count"):
return
net = ncnn.Net()
assert net.vulkan_device() is None
net.set_vulkan_device(0)
assert net.vulkan_device() is not None
def test_vulkan_device_vkdev():
if not hasattr(ncnn, "get_gpu_count"):
return
net = ncnn.Net()
assert net.vulkan_device() is None
vkdev = ncnn.get_gpu_device(0)
net.set_vulkan_device(vkdev)
assert net.vulkan_device() is not None

View File

@ -0,0 +1,139 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 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.
import pytest
import ncnn
def test_option():
allocator = ncnn.PoolAllocator()
opt = ncnn.Option()
opt.lightmode = True
assert opt.lightmode == True
opt.lightmode = False
assert opt.lightmode == False
assert opt.num_threads == ncnn.get_cpu_count()
opt.num_threads = 1
assert opt.num_threads == 1
assert opt.blob_allocator is None
opt.blob_allocator = allocator
assert opt.blob_allocator == allocator
assert opt.workspace_allocator is None
opt.workspace_allocator = allocator
assert opt.workspace_allocator == allocator
assert opt.openmp_blocktime == 20
opt.openmp_blocktime = 40
assert opt.openmp_blocktime == 40
opt.use_winograd_convolution = True
assert opt.use_winograd_convolution == True
opt.use_winograd_convolution = False
assert opt.use_winograd_convolution == False
opt.use_sgemm_convolution = True
assert opt.use_sgemm_convolution == True
opt.use_sgemm_convolution = False
assert opt.use_sgemm_convolution == False
opt.use_int8_inference = True
assert opt.use_int8_inference == True
opt.use_int8_inference = False
assert opt.use_int8_inference == False
opt.use_vulkan_compute = True
assert opt.use_vulkan_compute == True
opt.use_vulkan_compute = False
assert opt.use_vulkan_compute == False
opt.use_bf16_storage = True
assert opt.use_bf16_storage == True
opt.use_bf16_storage = False
assert opt.use_bf16_storage == False
opt.use_fp16_packed = True
assert opt.use_fp16_packed == True
opt.use_fp16_packed = False
assert opt.use_fp16_packed == False
opt.use_fp16_storage = True
assert opt.use_fp16_storage == True
opt.use_fp16_storage = False
assert opt.use_fp16_storage == False
opt.use_fp16_arithmetic = True
assert opt.use_fp16_arithmetic == True
opt.use_fp16_arithmetic = False
assert opt.use_fp16_arithmetic == False
opt.use_int8_packed = True
assert opt.use_int8_packed == True
opt.use_int8_packed = False
assert opt.use_int8_packed == False
opt.use_int8_storage = True
assert opt.use_int8_storage == True
opt.use_int8_storage = False
assert opt.use_int8_storage == False
opt.use_int8_arithmetic = True
assert opt.use_int8_arithmetic == True
opt.use_int8_arithmetic = False
assert opt.use_int8_arithmetic == False
opt.use_packing_layout = True
assert opt.use_packing_layout == True
opt.use_packing_layout = False
assert opt.use_packing_layout == False
opt.use_shader_pack8 = True
assert opt.use_shader_pack8 == True
opt.use_shader_pack8 = False
assert opt.use_shader_pack8 == False
opt.use_subgroup_basic = True
assert opt.use_subgroup_basic == True
opt.use_subgroup_basic = False
assert opt.use_subgroup_basic == False
opt.use_subgroup_vote = True
assert opt.use_subgroup_vote == True
opt.use_subgroup_vote = False
assert opt.use_subgroup_vote == False
opt.use_subgroup_ballot = True
assert opt.use_subgroup_ballot == True
opt.use_subgroup_ballot = False
assert opt.use_subgroup_ballot == False
opt.use_subgroup_shuffle = True
assert opt.use_subgroup_shuffle == True
opt.use_subgroup_shuffle = False
assert opt.use_subgroup_shuffle == False
opt.use_image_storage = True
assert opt.use_image_storage == True
opt.use_image_storage = False
assert opt.use_image_storage == False
opt.use_tensor_storage = True
assert opt.use_tensor_storage == True
opt.use_tensor_storage = False
assert opt.use_tensor_storage == False

View File

@ -0,0 +1,33 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2020 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.
import pytest
import ncnn
def test_paramdict():
pd = ncnn.ParamDict()
assert pd.type(0) == 0
assert pd.get(0, -1) == -1
pd.set(1, 1)
assert pd.type(1) == 2 and pd.get(1, -1) == 1
pd.set(2, 2.0)
assert pd.type(2) == 3 and pd.get(2, -2.0) == 2.0
mat = ncnn.Mat(1)
pd.set(3, mat)
assert pd.type(3) == 4 and pd.get(3, ncnn.Mat()).dims == mat.dims

View File

@ -0,0 +1,124 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2021 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.
import pytest
import ncnn
def test_vk_blob_allocator():
if not hasattr(ncnn, "get_gpu_count"):
return
vkdev = ncnn.get_gpu_device(0)
assert vkdev is not None
allocator = ncnn.VkBlobAllocator(vkdev)
assert allocator.buffer_memory_type_index >= 0
assert allocator.image_memory_type_index >= 0
mappable = allocator.mappable
allocator.mappable = not mappable
assert allocator.mappable == (not mappable)
coherent = allocator.coherent
allocator.coherent = not coherent
assert allocator.coherent == (not coherent)
bufmem = allocator.fastMalloc(10 * 1024)
assert bufmem is not None
allocator.fastFree(bufmem)
imgmem = allocator.fastMalloc(4, 4, 3, 4, 1)
assert imgmem is not None
allocator.fastFree(imgmem)
def test_vk_weight_allocator():
if not hasattr(ncnn, "get_gpu_count"):
return
vkdev = ncnn.get_gpu_device(0)
assert vkdev is not None
allocator = ncnn.VkWeightAllocator(vkdev)
assert allocator.buffer_memory_type_index >= 0
assert allocator.image_memory_type_index >= 0
mappable = allocator.mappable
allocator.mappable = not mappable
assert allocator.mappable == (not mappable)
coherent = allocator.coherent
allocator.coherent = not coherent
assert allocator.coherent == (not coherent)
bufmem = allocator.fastMalloc(10 * 1024)
assert bufmem is not None
allocator.fastFree(bufmem)
imgmem = allocator.fastMalloc(4, 4, 3, 4, 1)
assert imgmem is not None
allocator.fastFree(imgmem)
def test_vk_staging_allocator():
if not hasattr(ncnn, "get_gpu_count"):
return
vkdev = ncnn.get_gpu_device(0)
assert vkdev is not None
allocator = ncnn.VkStagingAllocator(vkdev)
assert allocator.buffer_memory_type_index >= 0
assert allocator.image_memory_type_index >= 0
mappable = allocator.mappable
allocator.mappable = not mappable
assert allocator.mappable == (not mappable)
coherent = allocator.coherent
allocator.coherent = not coherent
assert allocator.coherent == (not coherent)
bufmem = allocator.fastMalloc(10 * 1024)
assert bufmem is not None
allocator.fastFree(bufmem)
imgmem = allocator.fastMalloc(4, 4, 3, 4, 1)
assert imgmem is not None
allocator.fastFree(imgmem)
def test_vk_weight_staging_allocator():
if not hasattr(ncnn, "get_gpu_count"):
return
vkdev = ncnn.get_gpu_device(0)
assert vkdev is not None
allocator = ncnn.VkWeightStagingAllocator(vkdev)
assert allocator.buffer_memory_type_index >= 0
assert allocator.image_memory_type_index >= 0
mappable = allocator.mappable
allocator.mappable = not mappable
assert allocator.mappable == (not mappable)
coherent = allocator.coherent
allocator.coherent = not coherent
assert allocator.coherent == (not coherent)
bufmem = allocator.fastMalloc(10 * 1024)
assert bufmem is not None
allocator.fastFree(bufmem)
imgmem = allocator.fastMalloc(4, 4, 3, 4, 1)
assert imgmem is None

View File

@ -0,0 +1,55 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2021 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.
import pytest
import ncnn
def check_gpuinfo(gpuinfo):
assert gpuinfo.api_version() > 0
assert gpuinfo.driver_version() > 0
assert gpuinfo.vendor_id() > 0
assert gpuinfo.device_id() > 0
assert gpuinfo.pipeline_cache_uuid() is not None
assert gpuinfo.type() >= 0
def test_gpu_api():
if not hasattr(ncnn, "get_gpu_count"):
return
assert ncnn.create_gpu_instance() == 0
assert ncnn.get_gpu_count() > 0
assert ncnn.get_default_gpu_index() >= 0
gpuinfo = ncnn.get_gpu_info(0)
check_gpuinfo(gpuinfo)
vkdev = ncnn.get_gpu_device(0)
assert vkdev is not None
gpuinfo = vkdev.info()
check_gpuinfo(gpuinfo)
ncnn.destroy_gpu_instance()
def test_vulkan_device():
if not hasattr(ncnn, "get_gpu_count"):
return
vkdev = ncnn.VulkanDevice(0)
assert vkdev is not None
gpuinfo = vkdev.info()
check_gpuinfo(gpuinfo)