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

View File

@ -0,0 +1,223 @@
find_package(Python3 REQUIRED COMPONENTS Interpreter)
macro(pnnx_add_test name)
add_test(NAME test_${name} COMMAND ${CMAKE_COMMAND} -DPYTHON_EXECUTABLE=${Python3_EXECUTABLE} -DPYTHON_SCRIPT=${CMAKE_CURRENT_SOURCE_DIR}/test_${name}.py -P ${CMAKE_CURRENT_SOURCE_DIR}/run_test.cmake)
endmacro()
pnnx_add_test(F_adaptive_avg_pool1d)
pnnx_add_test(F_adaptive_avg_pool2d)
pnnx_add_test(F_adaptive_avg_pool3d)
pnnx_add_test(F_adaptive_max_pool1d)
pnnx_add_test(F_adaptive_max_pool2d)
pnnx_add_test(F_adaptive_max_pool3d)
pnnx_add_test(F_alpha_dropout)
pnnx_add_test(F_affine_grid)
pnnx_add_test(F_avg_pool1d)
pnnx_add_test(F_avg_pool2d)
pnnx_add_test(F_avg_pool3d)
pnnx_add_test(F_batch_norm)
pnnx_add_test(F_celu)
pnnx_add_test(F_conv1d)
pnnx_add_test(F_conv2d)
pnnx_add_test(F_conv3d)
pnnx_add_test(F_conv_transpose1d)
pnnx_add_test(F_conv_transpose2d)
pnnx_add_test(F_conv_transpose3d)
pnnx_add_test(F_dropout)
pnnx_add_test(F_dropout2d)
pnnx_add_test(F_dropout3d)
pnnx_add_test(F_elu)
pnnx_add_test(F_embedding)
pnnx_add_test(F_feature_alpha_dropout)
pnnx_add_test(F_gelu)
pnnx_add_test(F_grid_sample)
pnnx_add_test(F_group_norm)
pnnx_add_test(F_hardshrink)
pnnx_add_test(F_hardsigmoid)
pnnx_add_test(F_hardswish)
pnnx_add_test(F_hardtanh)
pnnx_add_test(F_instance_norm)
pnnx_add_test(F_interpolate)
pnnx_add_test(F_layer_norm)
pnnx_add_test(F_leaky_relu)
pnnx_add_test(F_linear)
pnnx_add_test(F_local_response_norm)
pnnx_add_test(F_log_softmax)
pnnx_add_test(F_logsigmoid)
pnnx_add_test(F_lp_pool1d)
pnnx_add_test(F_lp_pool2d)
pnnx_add_test(F_max_pool1d)
pnnx_add_test(F_max_pool2d)
pnnx_add_test(F_max_pool3d)
pnnx_add_test(F_normalize)
pnnx_add_test(F_pad)
pnnx_add_test(F_pixel_shuffle)
pnnx_add_test(F_pixel_unshuffle)
pnnx_add_test(F_prelu)
pnnx_add_test(F_relu)
pnnx_add_test(F_relu6)
pnnx_add_test(F_rrelu)
pnnx_add_test(F_selu)
pnnx_add_test(F_sigmoid)
pnnx_add_test(F_silu)
pnnx_add_test(F_softmax)
pnnx_add_test(F_softmin)
pnnx_add_test(F_softplus)
pnnx_add_test(F_softshrink)
pnnx_add_test(F_softsign)
pnnx_add_test(F_tanh)
pnnx_add_test(F_tanhshrink)
pnnx_add_test(F_threshold)
pnnx_add_test(F_upsample_bilinear)
pnnx_add_test(F_upsample_nearest)
pnnx_add_test(F_upsample)
pnnx_add_test(nn_AdaptiveAvgPool1d)
pnnx_add_test(nn_AdaptiveAvgPool2d)
pnnx_add_test(nn_AdaptiveAvgPool3d)
pnnx_add_test(nn_AdaptiveMaxPool1d)
pnnx_add_test(nn_AdaptiveMaxPool2d)
pnnx_add_test(nn_AdaptiveMaxPool3d)
pnnx_add_test(nn_AlphaDropout)
pnnx_add_test(nn_AvgPool1d)
pnnx_add_test(nn_AvgPool2d)
pnnx_add_test(nn_AvgPool3d)
pnnx_add_test(nn_BatchNorm1d)
pnnx_add_test(nn_BatchNorm2d)
pnnx_add_test(nn_BatchNorm3d)
pnnx_add_test(nn_CELU)
pnnx_add_test(nn_ChannelShuffle)
pnnx_add_test(nn_ConstantPad1d)
pnnx_add_test(nn_ConstantPad2d)
pnnx_add_test(nn_ConstantPad3d)
pnnx_add_test(nn_Conv1d)
pnnx_add_test(nn_Conv2d)
pnnx_add_test(nn_Conv3d)
pnnx_add_test(nn_ConvTranspose1d)
pnnx_add_test(nn_ConvTranspose2d)
pnnx_add_test(nn_ConvTranspose3d)
pnnx_add_test(nn_Dropout)
pnnx_add_test(nn_Dropout2d)
pnnx_add_test(nn_Dropout3d)
pnnx_add_test(nn_ELU)
pnnx_add_test(nn_Embedding)
pnnx_add_test(nn_GELU)
pnnx_add_test(nn_GroupNorm)
pnnx_add_test(nn_GRU)
pnnx_add_test(nn_Hardshrink)
pnnx_add_test(nn_Hardsigmoid)
pnnx_add_test(nn_Hardswish)
pnnx_add_test(nn_Hardtanh)
pnnx_add_test(nn_InstanceNorm1d)
pnnx_add_test(nn_InstanceNorm2d)
pnnx_add_test(nn_InstanceNorm3d)
pnnx_add_test(nn_LayerNorm)
pnnx_add_test(nn_LeakyReLU)
pnnx_add_test(nn_Linear)
pnnx_add_test(nn_LocalResponseNorm)
pnnx_add_test(nn_LogSigmoid)
pnnx_add_test(nn_LogSoftmax)
pnnx_add_test(nn_LPPool1d)
pnnx_add_test(nn_LPPool2d)
pnnx_add_test(nn_LSTM)
pnnx_add_test(nn_MaxPool1d)
pnnx_add_test(nn_MaxPool2d)
pnnx_add_test(nn_MaxPool3d)
pnnx_add_test(nn_MultiheadAttention)
pnnx_add_test(nn_PixelShuffle)
pnnx_add_test(nn_PixelUnshuffle)
pnnx_add_test(nn_PReLU)
pnnx_add_test(nn_ReflectionPad1d)
pnnx_add_test(nn_ReflectionPad2d)
pnnx_add_test(nn_ReLU)
pnnx_add_test(nn_ReLU6)
pnnx_add_test(nn_ReplicationPad1d)
pnnx_add_test(nn_ReplicationPad2d)
pnnx_add_test(nn_ReplicationPad3d)
pnnx_add_test(nn_RNN)
pnnx_add_test(nn_RReLU)
pnnx_add_test(nn_SELU)
pnnx_add_test(nn_Sigmoid)
pnnx_add_test(nn_SiLU)
pnnx_add_test(nn_Softmax)
pnnx_add_test(nn_Softmin)
pnnx_add_test(nn_Softplus)
pnnx_add_test(nn_Softshrink)
pnnx_add_test(nn_Softsign)
pnnx_add_test(nn_Tanh)
pnnx_add_test(nn_Tanhshrink)
pnnx_add_test(nn_Threshold)
pnnx_add_test(nn_Upsample)
pnnx_add_test(nn_UpsamplingBilinear2d)
pnnx_add_test(nn_UpsamplingNearest2d)
pnnx_add_test(nn_ZeroPad2d)
pnnx_add_test(Tensor_contiguous)
pnnx_add_test(Tensor_index)
pnnx_add_test(Tensor_new_empty)
pnnx_add_test(Tensor_repeat)
pnnx_add_test(Tensor_reshape)
pnnx_add_test(Tensor_select)
pnnx_add_test(Tensor_slice)
pnnx_add_test(Tensor_view)
pnnx_add_test(torch_addmm)
pnnx_add_test(torch_amax)
pnnx_add_test(torch_amin)
pnnx_add_test(torch_argmax)
pnnx_add_test(torch_argmin)
pnnx_add_test(torch_cat)
pnnx_add_test(torch_chunk)
pnnx_add_test(torch_clamp)
pnnx_add_test(torch_clone)
pnnx_add_test(torch_flatten)
pnnx_add_test(torch_full)
pnnx_add_test(torch_full_like)
pnnx_add_test(torch_logsumexp)
pnnx_add_test(torch_matmul)
pnnx_add_test(torch_mean)
pnnx_add_test(torch_norm)
pnnx_add_test(torch_ones)
pnnx_add_test(torch_ones_like)
pnnx_add_test(torch_permute)
pnnx_add_test(torch_prod)
pnnx_add_test(torch_sum)
pnnx_add_test(torch_split)
pnnx_add_test(torch_squeeze)
pnnx_add_test(torch_stack)
pnnx_add_test(torch_transpose)
pnnx_add_test(torch_unbind)
pnnx_add_test(torch_unsqueeze)
pnnx_add_test(torch_zeros)
pnnx_add_test(torch_zeros_like)
pnnx_add_test(mobilenet_v2)
pnnx_add_test(mobilenet_v3_small)
pnnx_add_test(resnet18)
pnnx_add_test(shufflenet_v2_x1_0)
pnnx_add_test(squeezenet1_1)
# TODO enable end2end quantization model test
#pnnx_add_test(quantization_shufflenet_v2_x1_0)
pnnx_add_test(pnnx_eliminate_noop_math)
pnnx_add_test(pnnx_fold_constant)
pnnx_add_test(pnnx_fuse_conv1d_batchnorm1d)
pnnx_add_test(pnnx_fuse_conv2d_batchnorm2d)
pnnx_add_test(pnnx_fuse_convtranspose1d_batchnorm1d)
pnnx_add_test(pnnx_fuse_convtranspose2d_batchnorm2d)
pnnx_add_test(pnnx_fuse_linear_batchnorm1d)
pnnx_add_test(pnnx_fuse_select_to_unbind)
if(Torch_VERSION VERSION_GREATER_EQUAL "1.9")
pnnx_add_test(F_mish)
pnnx_add_test(nn_Mish)
endif()
if(TorchVision_FOUND)
pnnx_add_test(torchvision_DeformConv2d)
pnnx_add_test(torchvision_RoIAlign)
endif()
add_subdirectory(ncnn)

View File

@ -0,0 +1,157 @@
find_package(Python3 REQUIRED COMPONENTS Interpreter)
macro(pnnx_ncnn_add_test name)
add_test(NAME test_ncnn_${name} COMMAND ${CMAKE_COMMAND} -DPYTHON_EXECUTABLE=${Python3_EXECUTABLE} -DPYTHON_SCRIPT=${CMAKE_CURRENT_SOURCE_DIR}/test_${name}.py -P ${CMAKE_CURRENT_SOURCE_DIR}/../run_test.cmake)
endmacro()
pnnx_ncnn_add_test(F_adaptive_avg_pool1d)
pnnx_ncnn_add_test(F_adaptive_avg_pool2d)
pnnx_ncnn_add_test(F_adaptive_avg_pool3d)
pnnx_ncnn_add_test(F_adaptive_max_pool1d)
pnnx_ncnn_add_test(F_adaptive_max_pool2d)
pnnx_ncnn_add_test(F_adaptive_max_pool3d)
pnnx_ncnn_add_test(F_alpha_dropout)
pnnx_ncnn_add_test(F_avg_pool1d)
pnnx_ncnn_add_test(F_avg_pool2d)
pnnx_ncnn_add_test(F_avg_pool3d)
pnnx_ncnn_add_test(F_batch_norm)
pnnx_ncnn_add_test(F_conv_transpose1d)
pnnx_ncnn_add_test(F_conv_transpose2d)
pnnx_ncnn_add_test(F_conv_transpose3d)
pnnx_ncnn_add_test(F_conv1d)
pnnx_ncnn_add_test(F_conv2d)
pnnx_ncnn_add_test(F_conv3d)
pnnx_ncnn_add_test(F_dropout)
pnnx_ncnn_add_test(F_dropout2d)
pnnx_ncnn_add_test(F_dropout3d)
pnnx_ncnn_add_test(F_elu)
pnnx_ncnn_add_test(F_embedding)
pnnx_ncnn_add_test(F_feature_alpha_dropout)
pnnx_ncnn_add_test(F_gelu)
pnnx_ncnn_add_test(F_group_norm)
pnnx_ncnn_add_test(F_hardsigmoid)
pnnx_ncnn_add_test(F_hardswish)
pnnx_ncnn_add_test(F_hardtanh)
pnnx_ncnn_add_test(F_interpolate)
pnnx_ncnn_add_test(F_layer_norm)
pnnx_ncnn_add_test(F_leaky_relu)
pnnx_ncnn_add_test(F_local_response_norm)
pnnx_ncnn_add_test(F_max_pool1d)
pnnx_ncnn_add_test(F_max_pool2d)
pnnx_ncnn_add_test(F_max_pool3d)
pnnx_ncnn_add_test(F_normalize)
pnnx_ncnn_add_test(F_pad)
pnnx_ncnn_add_test(F_pixel_shuffle)
pnnx_ncnn_add_test(F_pixel_unshuffle)
pnnx_ncnn_add_test(F_prelu)
pnnx_ncnn_add_test(F_relu)
pnnx_ncnn_add_test(F_relu6)
pnnx_ncnn_add_test(F_sigmoid)
pnnx_ncnn_add_test(F_silu)
pnnx_ncnn_add_test(F_softmax)
pnnx_ncnn_add_test(F_tanh)
pnnx_ncnn_add_test(F_upsample_bilinear)
pnnx_ncnn_add_test(F_upsample_nearest)
pnnx_ncnn_add_test(F_upsample)
pnnx_ncnn_add_test(nn_AdaptiveAvgPool1d)
pnnx_ncnn_add_test(nn_AdaptiveAvgPool2d)
pnnx_ncnn_add_test(nn_AdaptiveAvgPool3d)
pnnx_ncnn_add_test(nn_AdaptiveMaxPool1d)
pnnx_ncnn_add_test(nn_AdaptiveMaxPool2d)
pnnx_ncnn_add_test(nn_AdaptiveMaxPool3d)
pnnx_ncnn_add_test(nn_AlphaDropout)
pnnx_ncnn_add_test(nn_AvgPool1d)
pnnx_ncnn_add_test(nn_AvgPool2d)
pnnx_ncnn_add_test(nn_AvgPool3d)
pnnx_ncnn_add_test(nn_BatchNorm1d)
pnnx_ncnn_add_test(nn_BatchNorm2d)
pnnx_ncnn_add_test(nn_BatchNorm3d)
pnnx_ncnn_add_test(nn_ChannelShuffle)
pnnx_ncnn_add_test(nn_ConstantPad1d)
pnnx_ncnn_add_test(nn_ConstantPad2d)
pnnx_ncnn_add_test(nn_ConstantPad3d)
pnnx_ncnn_add_test(nn_Conv1d)
pnnx_ncnn_add_test(nn_Conv2d)
pnnx_ncnn_add_test(nn_Conv3d)
pnnx_ncnn_add_test(nn_ConvTranspose1d)
pnnx_ncnn_add_test(nn_ConvTranspose2d)
pnnx_ncnn_add_test(nn_ConvTranspose3d)
pnnx_ncnn_add_test(nn_Dropout)
pnnx_ncnn_add_test(nn_Dropout2d)
pnnx_ncnn_add_test(nn_Dropout3d)
pnnx_ncnn_add_test(nn_ELU)
pnnx_ncnn_add_test(nn_Embedding)
pnnx_ncnn_add_test(nn_GELU)
pnnx_ncnn_add_test(nn_GroupNorm)
pnnx_ncnn_add_test(nn_GRU)
pnnx_ncnn_add_test(nn_Hardsigmoid)
pnnx_ncnn_add_test(nn_Hardswish)
pnnx_ncnn_add_test(nn_Hardtanh)
pnnx_ncnn_add_test(nn_InstanceNorm2d)
pnnx_ncnn_add_test(nn_LayerNorm)
pnnx_ncnn_add_test(nn_LeakyReLU)
pnnx_ncnn_add_test(nn_Linear)
pnnx_ncnn_add_test(nn_LocalResponseNorm)
pnnx_ncnn_add_test(nn_LSTM)
pnnx_ncnn_add_test(nn_MaxPool1d)
pnnx_ncnn_add_test(nn_MaxPool2d)
pnnx_ncnn_add_test(nn_MaxPool3d)
pnnx_ncnn_add_test(nn_MultiheadAttention)
pnnx_ncnn_add_test(nn_PixelShuffle)
pnnx_ncnn_add_test(nn_PixelUnshuffle)
pnnx_ncnn_add_test(nn_PReLU)
pnnx_ncnn_add_test(nn_ReflectionPad1d)
pnnx_ncnn_add_test(nn_ReflectionPad2d)
pnnx_ncnn_add_test(nn_ReLU)
pnnx_ncnn_add_test(nn_ReLU6)
pnnx_ncnn_add_test(nn_ReplicationPad1d)
pnnx_ncnn_add_test(nn_ReplicationPad2d)
pnnx_ncnn_add_test(nn_RNN)
pnnx_ncnn_add_test(nn_SELU)
pnnx_ncnn_add_test(nn_Sigmoid)
pnnx_ncnn_add_test(nn_SiLU)
pnnx_ncnn_add_test(nn_Softmax)
pnnx_ncnn_add_test(nn_Tanh)
pnnx_ncnn_add_test(nn_Upsample)
pnnx_ncnn_add_test(nn_UpsamplingBilinear2d)
pnnx_ncnn_add_test(nn_UpsamplingNearest2d)
pnnx_ncnn_add_test(nn_ZeroPad2d)
pnnx_ncnn_add_test(Tensor_contiguous)
pnnx_ncnn_add_test(Tensor_repeat)
pnnx_ncnn_add_test(Tensor_reshape)
pnnx_ncnn_add_test(Tensor_slice)
pnnx_ncnn_add_test(Tensor_view)
pnnx_ncnn_add_test(torch_addmm)
pnnx_ncnn_add_test(torch_amax)
pnnx_ncnn_add_test(torch_amin)
pnnx_ncnn_add_test(torch_cat)
pnnx_ncnn_add_test(torch_chunk)
pnnx_ncnn_add_test(torch_clamp)
pnnx_ncnn_add_test(torch_clone)
pnnx_ncnn_add_test(torch_logsumexp)
pnnx_ncnn_add_test(torch_matmul)
pnnx_ncnn_add_test(torch_mean)
pnnx_ncnn_add_test(torch_permute)
pnnx_ncnn_add_test(torch_prod)
pnnx_ncnn_add_test(torch_sum)
pnnx_ncnn_add_test(torch_squeeze)
pnnx_ncnn_add_test(torch_transpose)
pnnx_ncnn_add_test(torch_unbind)
pnnx_ncnn_add_test(torch_unsqueeze)
pnnx_ncnn_add_test(mobilenet_v2)
pnnx_ncnn_add_test(mobilenet_v3_small)
pnnx_ncnn_add_test(resnet18)
pnnx_ncnn_add_test(shufflenet_v2_x1_0)
pnnx_ncnn_add_test(squeezenet1_1)
pnnx_ncnn_add_test(ncnn_fuse_transpose_matmul)
if(Torch_VERSION VERSION_GREATER_EQUAL "1.9")
pnnx_ncnn_add_test(F_mish)
pnnx_ncnn_add_test(nn_Mish)
endif()

View File

@ -0,0 +1,57 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.adaptive_avg_pool1d(x, output_size=7)
x = F.adaptive_avg_pool1d(x, output_size=1)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_adaptive_avg_pool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_adaptive_avg_pool1d.pt inputshape=[1,12,24]")
# ncnn inference
import test_F_adaptive_avg_pool1d_ncnn
b = test_F_adaptive_avg_pool1d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,57 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.adaptive_avg_pool2d(x, output_size=(7,6))
x = F.adaptive_avg_pool2d(x, output_size=1)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_adaptive_avg_pool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_adaptive_avg_pool2d.pt inputshape=[1,12,24,64]")
# ncnn inference
import test_F_adaptive_avg_pool2d_ncnn
b = test_F_adaptive_avg_pool2d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,57 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.adaptive_avg_pool3d(x, output_size=(7,6,5))
x = F.adaptive_avg_pool3d(x, output_size=1)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 33, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_adaptive_avg_pool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_adaptive_avg_pool3d.pt inputshape=[1,12,24,33,64]")
# ncnn inference
import test_F_adaptive_avg_pool3d_ncnn
b = test_F_adaptive_avg_pool3d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,57 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.adaptive_max_pool1d(x, output_size=7)
x = F.adaptive_max_pool1d(x, output_size=1)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_adaptive_max_pool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_adaptive_max_pool1d.pt inputshape=[1,12,24]")
# ncnn inference
import test_F_adaptive_max_pool1d_ncnn
b = test_F_adaptive_max_pool1d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,57 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.adaptive_max_pool2d(x, output_size=(7,6))
x = F.adaptive_max_pool2d(x, output_size=1)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_adaptive_max_pool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_adaptive_max_pool2d.pt inputshape=[1,12,24,64]")
# ncnn inference
import test_F_adaptive_max_pool2d_ncnn
b = test_F_adaptive_max_pool2d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,57 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.adaptive_max_pool3d(x, output_size=(7,6,5))
x = F.adaptive_max_pool3d(x, output_size=1)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 33, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_adaptive_max_pool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_adaptive_max_pool3d.pt inputshape=[1,12,24,33,64]")
# ncnn inference
import test_F_adaptive_max_pool3d_ncnn
b = test_F_adaptive_max_pool3d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,67 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z, w):
x = F.alpha_dropout(x, training=False)
y = F.alpha_dropout(y, training=False)
z = F.alpha_dropout(z, p=0.6, training=False)
w = F.alpha_dropout(w, p=0.1, training=False)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
w = F.relu(w)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
w = torch.rand(5, 7, 9, 11)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_F_alpha_dropout.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_alpha_dropout.pt inputshape=[16],[2,16],[3,12,16],[5,7,9,11]")
# ncnn inference
import test_F_alpha_dropout_ncnn
b = test_F_alpha_dropout_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.avg_pool1d(x, kernel_size=3)
x = F.avg_pool1d(x, kernel_size=4, stride=2, padding=2)
x = F.avg_pool1d(x, kernel_size=3, stride=1, padding=(0), ceil_mode=False, count_include_pad=True)
x = F.avg_pool1d(x, kernel_size=5, stride=2, padding=(2), ceil_mode=True, count_include_pad=False)
x = F.avg_pool1d(x, kernel_size=3, stride=2, padding=1, ceil_mode=False, count_include_pad=True)
x = F.avg_pool1d(x, kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
x = F.avg_pool1d(x, kernel_size=4, stride=1, padding=2, ceil_mode=False, count_include_pad=False)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_avg_pool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_avg_pool1d.pt inputshape=[1,12,128]")
# ncnn inference
import test_F_avg_pool1d_ncnn
b = test_F_avg_pool1d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,73 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
y = x.reshape(12, 128, 127)
x = F.avg_pool2d(x, kernel_size=3)
x = F.avg_pool2d(x, kernel_size=4, stride=2, padding=2)
x = F.avg_pool2d(x, kernel_size=(1,3), stride=1, padding=(0,1), ceil_mode=False, count_include_pad=True)
x = F.avg_pool2d(x, kernel_size=(4,5), stride=(1,2), padding=(1,2), ceil_mode=True, count_include_pad=False)
x = F.avg_pool2d(x, kernel_size=(5,3), stride=(2,1), padding=1, ceil_mode=False, count_include_pad=True)
x = F.avg_pool2d(x, kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
#x = F.avg_pool2d(x, kernel_size=(5,4), stride=1, padding=2, ceil_mode=False, count_include_pad=False, divisor_override=18)
y = F.avg_pool2d(y, kernel_size=3)
y = F.avg_pool2d(y, kernel_size=4, stride=2, padding=2)
y = F.avg_pool2d(y, kernel_size=(1,3), stride=1, padding=(0,1), ceil_mode=False, count_include_pad=True)
y = F.avg_pool2d(y, kernel_size=(4,5), stride=(1,2), padding=(1,2), ceil_mode=True, count_include_pad=False)
y = F.avg_pool2d(y, kernel_size=(5,3), stride=(2,1), padding=1, ceil_mode=False, count_include_pad=True)
y = F.avg_pool2d(y, kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
#y = F.avg_pool2d(y, kernel_size=(5,4), stride=1, padding=2, ceil_mode=False, count_include_pad=False, divisor_override=18)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 128, 127)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_avg_pool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_avg_pool2d.pt inputshape=[1,12,128,127]")
# ncnn inference
import test_F_avg_pool2d_ncnn
b = test_F_avg_pool2d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,73 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
y = x.reshape(12, 96, 128, 128)
x = F.avg_pool3d(x, kernel_size=3)
x = F.avg_pool3d(x, kernel_size=4, stride=2, padding=2)
x = F.avg_pool3d(x, kernel_size=(1,2,3), stride=1, padding=(0,1,1), ceil_mode=False, count_include_pad=True)
x = F.avg_pool3d(x, kernel_size=(3,4,5), stride=(1,2,2), padding=(1,1,2), ceil_mode=True, count_include_pad=False)
x = F.avg_pool3d(x, kernel_size=(5,4,3), stride=(2,1,1), padding=1, ceil_mode=False, count_include_pad=True)
x = F.avg_pool3d(x, kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
#x = F.avg_pool3d(x, kernel_size=(5,4,4), stride=1, padding=2, ceil_mode=False, count_include_pad=False, divisor_override=77)
y = F.avg_pool3d(y, kernel_size=3)
y = F.avg_pool3d(y, kernel_size=4, stride=2, padding=2)
y = F.avg_pool3d(y, kernel_size=(1,2,3), stride=1, padding=(0,1,1), ceil_mode=False, count_include_pad=True)
y = F.avg_pool3d(y, kernel_size=(3,4,5), stride=(1,2,2), padding=(1,1,2), ceil_mode=True, count_include_pad=False)
y = F.avg_pool3d(y, kernel_size=(5,4,3), stride=(2,1,1), padding=1, ceil_mode=False, count_include_pad=True)
y = F.avg_pool3d(y, kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
#y = F.avg_pool3d(y, kernel_size=(5,4,4), stride=1, padding=2, ceil_mode=False, count_include_pad=False, divisor_override=77)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 96, 128, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_avg_pool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_avg_pool3d.pt inputshape=[1,12,96,128,128]")
# ncnn inference
import test_F_avg_pool3d_ncnn
b = test_F_avg_pool3d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,76 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.m3 = torch.rand(16)
self.v3 = torch.rand(16)
self.w3 = nn.Parameter(torch.rand(16))
self.b3 = nn.Parameter(torch.rand(16))
self.m4 = torch.rand(2)
self.v4 = torch.rand(2)
self.w4 = nn.Parameter(torch.rand(2))
self.b4 = nn.Parameter(torch.rand(2))
self.m5 = torch.rand(3)
self.v5 = torch.rand(3)
self.w5 = nn.Parameter(torch.rand(3))
self.b5 = nn.Parameter(torch.rand(3))
def forward(self, x, y, z):
x = F.batch_norm(x, self.m3, self.v3, self.w3, self.b3)
y = F.batch_norm(y, self.m4, self.v4, self.w4, self.b4, eps=1e-3)
z = F.batch_norm(z, self.m5, self.v5, self.w5, self.b5, eps=1e-2)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 16)
y = torch.rand(1, 2, 16)
z = torch.rand(1, 3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_batch_norm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_batch_norm.pt inputshape=[1,16],[1,2,16],[1,3,12,16]")
# ncnn inference
import test_F_batch_norm_ncnn
b = test_F_batch_norm_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,69 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w2 = nn.Parameter(torch.rand(12, 6, 4))
self.b2 = nn.Parameter(torch.rand(12))
self.w3 = nn.Parameter(torch.rand(6, 4, 3))
def forward(self, x, w0, w1, b1, y):
x = F.conv1d(x, w0, None, stride=2, padding=1)
if torch.__version__ < '1.9':
x = F.conv1d(x, w1, b1, stride=1, padding=1, dilation=2, groups=2)
else:
x = F.conv1d(x, w1, b1, stride=1, padding='same', dilation=2, groups=2)
y = F.conv1d(y, self.w2, self.b2, stride=2, padding=2)
y = F.conv1d(y, self.w3, None, stride=2, padding=1, groups=3)
return x, y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 52)
w0 = torch.rand(16, 12, 3)
w1 = torch.rand(16, 8, 5)
b1 = torch.rand(16)
y = torch.rand(1, 6, 25)
a0, a1 = net(x, w0, w1, b1, y)
# export torchscript
mod = torch.jit.trace(net, (x, w0, w1, b1, y))
mod.save("test_F_conv1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_conv1d.pt inputshape=[1,12,52],[16,12,3],[16,8,5],[16],[1,6,25]")
# ncnn inference
import test_F_conv1d_ncnn
b0, b1 = test_F_conv1d_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,69 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w2 = nn.Parameter(torch.rand(12, 6, 4, 4))
self.b2 = nn.Parameter(torch.rand(12))
self.w3 = nn.Parameter(torch.rand(6, 4, 3, 3))
def forward(self, x, w0, w1, b1, y):
x = F.conv2d(x, w0, None, stride=(2,2), padding=(1,1))
if torch.__version__ < '1.9':
x = F.conv2d(x, w1, b1, stride=(1,1), padding=(1,1), dilation=(2,1), groups=2)
else:
x = F.conv2d(x, w1, b1, stride=(1,1), padding='same', dilation=(2,1), groups=2)
y = F.conv2d(y, self.w2, self.b2, stride=(2,2), padding=(2,2))
y = F.conv2d(y, self.w3, None, stride=(2,2), padding=(1,1), groups=3)
return x, y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 52, 64)
w0 = torch.rand(16, 12, 3, 3)
w1 = torch.rand(16, 8, 5, 5)
b1 = torch.rand(16)
y = torch.rand(1, 6, 32, 25)
a0, a1 = net(x, w0, w1, b1, y)
# export torchscript
mod = torch.jit.trace(net, (x, w0, w1, b1, y))
mod.save("test_F_conv2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_conv2d.pt inputshape=[1,12,52,64],[16,12,3,3],[16,8,5,5],[16],[1,6,32,25]")
# ncnn inference
import test_F_conv2d_ncnn
b0, b1 = test_F_conv2d_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,59 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w2 = nn.Parameter(torch.rand(12, 6, 4, 4, 4))
self.b2 = nn.Parameter(torch.rand(12))
self.w3 = nn.Parameter(torch.rand(6, 4, 3, 3, 3))
def forward(self, y):
y = F.conv3d(y, self.w2, self.b2, stride=(2,2,2), padding=(2,2,2))
y = F.conv3d(y, self.w3, None, stride=(2,2,2), padding=(1,1,1), groups=3)
return y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
y = torch.rand(1, 6, 12, 11, 10)
a = net(y)
# export torchscript
mod = torch.jit.trace(net, y)
mod.save("test_F_conv3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_conv3d.pt inputshape=[1,6,12,11,10]")
# ncnn inference
import test_F_conv3d_ncnn
b = test_F_conv3d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,59 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w2 = nn.Parameter(torch.rand(6, 12, 4))
self.b2 = nn.Parameter(torch.rand(12))
self.w3 = nn.Parameter(torch.rand(12, 2, 3))
def forward(self, y):
y = F.conv_transpose1d(y, self.w2, self.b2, stride=2, padding=1, output_padding=1)
y = F.conv_transpose1d(y, self.w3, None, stride=1, padding=2, dilation=2, groups=3)
return y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
y = torch.rand(1, 6, 5)
a = net(y)
# export torchscript
mod = torch.jit.trace(net, y)
mod.save("test_F_conv_transpose1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_conv_transpose1d.pt inputshape=[1,6,5]")
# ncnn inference
import test_F_conv_transpose1d_ncnn
b = test_F_conv_transpose1d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,59 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w2 = nn.Parameter(torch.rand(6, 12, 4, 4))
self.b2 = nn.Parameter(torch.rand(12))
self.w3 = nn.Parameter(torch.rand(12, 2, 3, 3))
def forward(self, y):
y = F.conv_transpose2d(y, self.w2, self.b2, stride=(2,2), padding=(1,1), output_padding=(1,1))
y = F.conv_transpose2d(y, self.w3, None, stride=(1,2), padding=(2,1), dilation=(2,1), groups=3)
return y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
y = torch.rand(1, 6, 5, 6)
a = net(y)
# export torchscript
mod = torch.jit.trace(net, y)
mod.save("test_F_conv_transpose2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_conv_transpose2d.pt inputshape=[1,6,5,6]")
# ncnn inference
import test_F_conv_transpose2d_ncnn
b = test_F_conv_transpose2d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,59 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2022 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w2 = nn.Parameter(torch.rand(6, 12, 4, 4, 4))
self.b2 = nn.Parameter(torch.rand(12))
self.w3 = nn.Parameter(torch.rand(12, 2, 3, 3, 3))
def forward(self, y):
y = F.conv_transpose3d(y, self.w2, self.b2, stride=(2,2,2), padding=(1,0,1), output_padding=(1,1,0))
y = F.conv_transpose3d(y, self.w3, None, stride=(1,1,2), padding=(2,2,1), dilation=(2,2,1), groups=3)
return y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
y = torch.rand(1, 6, 4, 5, 6)
a = net(y)
# export torchscript
mod = torch.jit.trace(net, y)
mod.save("test_F_conv_transpose3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_conv_transpose3d.pt inputshape=[1,6,4,5,6]")
# ncnn inference
import test_F_conv_transpose3d_ncnn
b = test_F_conv_transpose3d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,67 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z, w):
x = F.dropout(x, training=False)
y = F.dropout(y, training=False)
z = F.dropout(z, p=0.6, training=False)
w = F.dropout(w, p=0.1, training=False)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
w = F.relu(w)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
w = torch.rand(5, 7, 9, 11)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_F_dropout.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_dropout.pt inputshape=[16],[2,16],[3,12,16],[5,7,9,11]")
# ncnn inference
import test_F_dropout_ncnn
b = test_F_dropout_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y):
x = F.dropout2d(x, training=False)
z = F.dropout2d(y, p=0.6, training=False)
x = F.relu(x)
y = F.relu(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12, 2, 16)
y = torch.rand(3, 12, 16)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_F_dropout2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_dropout2d.pt inputshape=[12,2,16],[3,12,16]")
# ncnn inference
import test_F_dropout2d_ncnn
b = test_F_dropout2d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y):
x = F.dropout3d(x, training=False)
z = F.dropout3d(y, p=0.6, training=False)
x = F.relu(x)
y = F.relu(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12, 4, 2, 16)
y = torch.rand(3, 5, 12, 16)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_F_dropout3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_dropout3d.pt inputshape=[12,4,2,16],[3,5,12,16]")
# ncnn inference
import test_F_dropout3d_ncnn
b = test_F_dropout3d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.elu(x)
y = F.elu(y, 1.2)
z = F.elu(z, -0.6)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 16)
y = torch.rand(1, 2, 16)
z = torch.rand(1, 3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_elu.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_elu.pt inputshape=[1,16],[1,2,16],[1,3,12,16]")
# ncnn inference
import test_F_elu_ncnn
b = test_F_elu_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,56 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w1 = nn.Parameter(torch.rand(10, 128))
def forward(self, y):
y = F.embedding(y, self.w1)
return y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
y = torch.randint(10, (1, 11), dtype=torch.int)
a = net(y)
# export torchscript
mod = torch.jit.trace(net, (y))
mod.save("test_F_embedding.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_embedding.pt inputshape=[1,11]i32")
# ncnn inference
import test_F_embedding_ncnn
b = test_F_embedding_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y):
x = F.feature_alpha_dropout(x, training=False)
y = F.feature_alpha_dropout(y, p=0.6, training=False)
x = F.relu(x)
y = F.relu(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(3, 6, 12, 16)
y = torch.rand(5, 7, 9, 11)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_F_feature_alpha_dropout.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_feature_alpha_dropout.pt inputshape=[3,6,12,16],[5,7,9,11]")
# ncnn inference
import test_F_feature_alpha_dropout_ncnn
b = test_F_feature_alpha_dropout_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.gelu(x)
y = F.gelu(y)
z = F.gelu(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_gelu.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_gelu.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_gelu_ncnn
b = test_F_gelu_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w5 = nn.Parameter(torch.rand(32))
self.b5 = nn.Parameter(torch.rand(32))
def forward(self, z):
z = F.group_norm(z, 8, self.w5, self.b5, eps=1e-2)
return z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
z = torch.rand(1, 32, 12, 16)
a = net(z)
# export torchscript
mod = torch.jit.trace(net, z)
mod.save("test_F_group_norm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_group_norm.pt inputshape=[1,32,12,16]")
# ncnn inference
import test_F_group_norm_ncnn
b = test_F_group_norm_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
def hardsigmoid_forward_0(x):
return F.relu6(x + 3., True) / 6.
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.hardsigmoid(x)
y = F.hardsigmoid(y)
z = hardsigmoid_forward_0(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_hardsigmoid.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_hardsigmoid.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_hardsigmoid_ncnn
b = test_F_hardsigmoid_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,72 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
def hardswish_forward_0(x):
return x * F.hardsigmoid(x)
def hardswish_forward_1(x):
return x * F.hardtanh(x + 3, 0., 6.) / 6.
def hardswish_forward_2(x):
out = F.relu6(x + 3., True) / 6.
return out * x
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.hardswish(x)
y = hardswish_forward_0(y)
z = hardswish_forward_1(z)
z = hardswish_forward_2(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_hardswish.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_hardswish.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_hardswish_ncnn
b = test_F_hardswish_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.hardtanh(x)
y = F.hardtanh(y, -1, 1)
z = F.hardtanh(z, -0.1, 0.1)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_hardtanh.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_hardtanh.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_hardtanh_ncnn
b = test_F_hardtanh_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,97 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, w):
x = F.interpolate(x, size=16)
x = F.interpolate(x, scale_factor=2, mode='nearest')
x = F.interpolate(x, size=(20), mode='nearest')
x = F.interpolate(x, scale_factor=(4), mode='nearest')
x = F.interpolate(x, size=16, mode='linear')
x = F.interpolate(x, scale_factor=2, mode='linear')
x = F.interpolate(x, size=(24), mode='linear', align_corners=True)
x = F.interpolate(x, scale_factor=(3), mode='linear', align_corners=True)
x = F.interpolate(x, scale_factor=1.5, mode='nearest', recompute_scale_factor=True)
x = F.interpolate(x, scale_factor=1.2, mode='linear', align_corners=False, recompute_scale_factor=True)
x = F.interpolate(x, scale_factor=0.8, mode='linear', align_corners=True, recompute_scale_factor=True)
y = F.interpolate(y, size=16)
y = F.interpolate(y, scale_factor=2, mode='nearest')
y = F.interpolate(y, size=(20,20), mode='nearest')
y = F.interpolate(y, scale_factor=(4,4), mode='nearest')
y = F.interpolate(y, size=(16,24), mode='nearest')
y = F.interpolate(y, scale_factor=(2,3), mode='nearest')
y = F.interpolate(y, size=16, mode='bilinear')
y = F.interpolate(y, scale_factor=2, mode='bilinear')
y = F.interpolate(y, size=(20,20), mode='bilinear', align_corners=False)
y = F.interpolate(y, scale_factor=(4,4), mode='bilinear', align_corners=False)
y = F.interpolate(y, size=(16,24), mode='bilinear', align_corners=True)
y = F.interpolate(y, scale_factor=(2,3), mode='bilinear', align_corners=True)
y = F.interpolate(y, size=16, mode='bicubic')
y = F.interpolate(y, scale_factor=2, mode='bicubic')
y = F.interpolate(y, size=(20,20), mode='bicubic', align_corners=False)
y = F.interpolate(y, scale_factor=(4,4), mode='bicubic', align_corners=False)
y = F.interpolate(y, size=(16,24), mode='bicubic', align_corners=True)
y = F.interpolate(y, scale_factor=(2,3), mode='bicubic', align_corners=True)
y = F.interpolate(y, scale_factor=(1.6,2), mode='nearest', recompute_scale_factor=True)
y = F.interpolate(y, scale_factor=(2,1.2), mode='bilinear', align_corners=False, recompute_scale_factor=True)
y = F.interpolate(y, scale_factor=(0.5,0.4), mode='bilinear', align_corners=True, recompute_scale_factor=True)
y = F.interpolate(y, scale_factor=(0.8,0.9), mode='bicubic', align_corners=False, recompute_scale_factor=True)
y = F.interpolate(y, scale_factor=(1.1,0.5), mode='bicubic', align_corners=True, recompute_scale_factor=True)
w = F.interpolate(w, scale_factor=(2.976744,2.976744), mode='nearest', recompute_scale_factor=False)
return x, y, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 3, 32)
y = torch.rand(1, 3, 32, 32)
w = torch.rand(1, 8, 86, 86)
a = net(x, y, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, w))
mod.save("test_F_interpolate.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_interpolate.pt inputshape=[1,3,32],[1,3,32,32],[1,8,86,86]")
# ncnn inference
import test_F_interpolate_ncnn
b = test_F_interpolate_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,65 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w3 = nn.Parameter(torch.rand(24))
self.b3 = nn.Parameter(torch.rand(24))
self.w4 = nn.Parameter(torch.rand(12, 16))
self.b4 = nn.Parameter(torch.rand(12, 16))
def forward(self, x, y):
x = F.layer_norm(x, (24,), self.w3, self.b3)
y = F.layer_norm(y, (12,16), self.w4, self.b4, eps=1e-3)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12, 24)
y = torch.rand(3, 12, 16)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_F_layer_norm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_layer_norm.pt inputshape=[12,24],[3,12,16]")
# ncnn inference
import test_F_layer_norm_ncnn
b = test_F_layer_norm_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z, w):
x = F.leaky_relu(x)
y = F.leaky_relu(y, 0.1)
z = F.leaky_relu(z, -0.22)
w = F.leaky_relu(w, 0)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
w = torch.rand(5, 7, 9, 11)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_F_leaky_relu.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_leaky_relu.pt inputshape=[16],[2,16],[3,12,16],[5,7,9,11]")
# ncnn inference
import test_F_leaky_relu_ncnn
b = test_F_leaky_relu_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.local_response_norm(x, 4)
x = F.local_response_norm(x, size=4, alpha=0.001, beta=0.2, k=1.9)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 3, 12, 16)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_local_response_norm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_local_response_norm.pt inputshape=[1,3,12,16]")
# ncnn inference
import test_F_local_response_norm_ncnn
b = test_F_local_response_norm_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,74 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
y = x.reshape(12, 128)
x = F.max_pool1d(x, kernel_size=3)
x = F.max_pool1d(x, kernel_size=4, stride=2, padding=2, dilation=1)
x = F.max_pool1d(x, kernel_size=3, stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False)
x = F.max_pool1d(x, kernel_size=5, stride=2, padding=2, dilation=1, return_indices=False, ceil_mode=True)
x = F.max_pool1d(x, kernel_size=3, stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False)
x = F.max_pool1d(x, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
y = F.max_pool1d(y, kernel_size=3)
y = F.max_pool1d(y, kernel_size=4, stride=2, padding=2, dilation=1)
y = F.max_pool1d(y, kernel_size=3, stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False)
y = F.max_pool1d(y, kernel_size=5, stride=2, padding=2, dilation=1, return_indices=False, ceil_mode=True)
y = F.max_pool1d(y, kernel_size=3, stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False)
y = F.max_pool1d(y, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
return x, y
#x, indices1 = F.max_pool1d(x, kernel_size=2, padding=1, dilation=1, return_indices=True, ceil_mode=False)
#x, indices2 = F.max_pool1d(x, kernel_size=5, stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=True)
#return x, indices1, indices2
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_max_pool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_max_pool1d.pt inputshape=[1,12,128]")
# ncnn inference
import test_F_max_pool1d_ncnn
b = test_F_max_pool1d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,74 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
y = x.reshape(12, 128, 127)
x = F.max_pool2d(x, kernel_size=3)
x = F.max_pool2d(x, kernel_size=4, stride=2, padding=2, dilation=1)
x = F.max_pool2d(x, kernel_size=(1,3), stride=1, padding=(0,1), dilation=1, return_indices=False, ceil_mode=False)
x = F.max_pool2d(x, kernel_size=(4,5), stride=(1,2), padding=(1,2), dilation=1, return_indices=False, ceil_mode=True)
x = F.max_pool2d(x, kernel_size=(2,3), stride=1, padding=1, dilation=(1,1), return_indices=False, ceil_mode=False)
x = F.max_pool2d(x, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
y = F.max_pool2d(y, kernel_size=3)
y = F.max_pool2d(y, kernel_size=4, stride=2, padding=2, dilation=1)
y = F.max_pool2d(y, kernel_size=(1,3), stride=1, padding=(0,1), dilation=1, return_indices=False, ceil_mode=False)
y = F.max_pool2d(y, kernel_size=(4,5), stride=(1,2), padding=(1,2), dilation=1, return_indices=False, ceil_mode=True)
y = F.max_pool2d(y, kernel_size=(2,3), stride=1, padding=1, dilation=(1,1), return_indices=False, ceil_mode=False)
y = F.max_pool2d(y, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
return x, y
#x, indices1 = F.max_pool2d(x, kernel_size=2, padding=1, dilation=1, return_indices=True, ceil_mode=False)
#x, indices2 = F.max_pool2d(x, kernel_size=(5,4), stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=False)
#return x, indices1, indices2
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 128, 127)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_max_pool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_max_pool2d.pt inputshape=[1,12,128,127]")
# ncnn inference
import test_F_max_pool2d_ncnn
b = test_F_max_pool2d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,73 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
y = x.reshape(12, 96, 128, 128)
x = F.max_pool3d(x, kernel_size=3)
x = F.max_pool3d(x, kernel_size=4, stride=2, padding=2, dilation=1)
x = F.max_pool3d(x, kernel_size=(1,2,3), stride=1, padding=(0,0,1), dilation=1, return_indices=False, ceil_mode=False)
x = F.max_pool3d(x, kernel_size=(3,4,5), stride=(1,2,2), padding=(1,2,2), dilation=1, return_indices=False, ceil_mode=True)
x = F.max_pool3d(x, kernel_size=(2,3,3), stride=1, padding=1, dilation=(1,1,1), return_indices=False, ceil_mode=False)
x = F.max_pool3d(x, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
y = F.max_pool3d(y, kernel_size=3)
y = F.max_pool3d(y, kernel_size=4, stride=2, padding=2, dilation=1)
y = F.max_pool3d(y, kernel_size=(1,2,3), stride=1, padding=(0,0,1), dilation=1, return_indices=False, ceil_mode=False)
y = F.max_pool3d(y, kernel_size=(3,4,5), stride=(1,2,2), padding=(1,2,2), dilation=1, return_indices=False, ceil_mode=True)
y = F.max_pool3d(y, kernel_size=(2,3,3), stride=1, padding=1, dilation=(1,1,1), return_indices=False, ceil_mode=False)
y = F.max_pool3d(y, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
return x, y
#x, indices = F.max_pool3d(x, kernel_size=(5,4,4), stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=False)
#return x, indices
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 96, 128, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_max_pool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_max_pool3d.pt inputshape=[1,12,96,128,128]")
# ncnn inference
import test_F_max_pool3d_ncnn
b = test_F_max_pool3d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,67 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
def mish_forward_0(x):
return x * F.softplus(x).tanh()
def mish_forward_1(x):
return x.mul(torch.tanh(F.softplus(x)))
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.mish(x)
y = mish_forward_0(y)
z = mish_forward_1(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_mish.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_mish.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_mish_ncnn
b = test_F_mish_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.normalize(x, dim=0)
x = F.normalize(x, dim=0, eps=1e-3)
# TODO
#y = F.normalize(y, p=1, dim=1)
#y = F.normalize(y, dim=2)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12, 24, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_normalize.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_normalize.pt inputshape=[12,24,64]")
# ncnn inference
import test_F_normalize_ncnn
b = test_F_normalize_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,71 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.pad(x, (3,4), mode='constant', value=1.3)
x = F.pad(x, (2,2))
y = F.pad(y, (5,6), mode='reflect')
y = F.pad(y, (2,1), mode='replicate')
y = F.pad(y, (3,4), mode='constant', value=1.3)
y = F.pad(y, (1,1))
z = F.pad(z, (3,4,3,4), mode='reflect')
z = F.pad(z, (2,1,2,0), mode='replicate')
z = F.pad(z, (1,0,2,0), mode='constant', value=1.3)
z = F.pad(z, (3,3,3,3))
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 16)
y = torch.rand(1, 2, 16)
z = torch.rand(1, 3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_pad.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_pad.pt inputshape=[1,16],[1,2,16],[1,3,12,16]")
# ncnn inference
import test_F_pad_ncnn
b = test_F_pad_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.pixel_shuffle(x, 2)
x = F.pixel_shuffle(x, 4)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 6, 7)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_pixel_shuffle.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_pixel_shuffle.pt inputshape=[1,128,6,7]")
# ncnn inference
import test_F_pixel_shuffle_ncnn
b = test_F_pixel_shuffle_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.pixel_unshuffle(x, 4)
x = F.pixel_unshuffle(x, 2)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 3, 128, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_pixel_unshuffle.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_pixel_unshuffle.pt inputshape=[1,3,128,128]")
# ncnn inference
import test_F_pixel_unshuffle_ncnn
b = test_F_pixel_unshuffle_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,65 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.w4 = nn.Parameter(torch.rand(16))
self.w5 = nn.Parameter(torch.rand(2))
self.w6 = nn.Parameter(torch.rand(3))
def forward(self, x, y, z):
x = F.prelu(x, self.w4)
y = F.prelu(y, self.w5)
z = F.prelu(z, self.w6)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 16)
y = torch.rand(12, 2, 16)
z = torch.rand(1, 3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_prelu.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_prelu.pt inputshape=[1,16],[12,2,16],[1,3,12,16]")
# ncnn inference
import test_F_prelu_ncnn
b = test_F_prelu_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z, w):
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
w = F.relu(w)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
w = torch.rand(5, 7, 9, 11)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_F_relu.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_relu.pt inputshape=[16],[2,16],[3,12,16],[5,7,9,11]")
# ncnn inference
import test_F_relu_ncnn
b = test_F_relu_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.relu6(x)
y = F.relu6(y)
z = F.relu6(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 16)
y = torch.rand(1, 2, 16)
z = torch.rand(1, 3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_relu6.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_relu6.pt inputshape=[1,16],[1,2,16],[1,3,12,16]")
# ncnn inference
import test_F_relu6_ncnn
b = test_F_relu6_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.sigmoid(x)
y = F.sigmoid(y)
z = F.sigmoid(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_sigmoid.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_sigmoid.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_sigmoid_ncnn
b = test_F_sigmoid_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
def silu_forward_0(x):
return x * torch.sigmoid(x)
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.silu(x)
y = F.silu(y)
z = silu_forward_0(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_silu.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_silu.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_silu_ncnn
b = test_F_silu_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.softmax(x, 0)
y = F.softmax(y, 1)
z = F.softmax(z, 2)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_softmax.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_softmax.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_softmax_ncnn
b = test_F_softmax_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = F.tanh(x)
y = F.tanh(y)
z = F.tanh(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(16)
y = torch.rand(2, 16)
z = torch.rand(3, 12, 16)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_F_tanh.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_tanh.pt inputshape=[16],[2,16],[3,12,16]")
# ncnn inference
import test_F_tanh_ncnn
b = test_F_tanh_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,88 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, w):
x = F.upsample(x, size=16)
x = F.upsample(x, scale_factor=2, mode='nearest')
x = F.upsample(x, size=(20), mode='nearest')
x = F.upsample(x, scale_factor=(4), mode='nearest')
x = F.upsample(x, size=16, mode='linear')
x = F.upsample(x, scale_factor=2, mode='linear')
x = F.upsample(x, size=(24), mode='linear', align_corners=True)
x = F.upsample(x, scale_factor=(3), mode='linear', align_corners=True)
y = F.upsample(y, size=16)
y = F.upsample(y, scale_factor=2, mode='nearest')
y = F.upsample(y, size=(20,20), mode='nearest')
y = F.upsample(y, scale_factor=(4,4), mode='nearest')
y = F.upsample(y, size=(16,24), mode='nearest')
y = F.upsample(y, scale_factor=(2,3), mode='nearest')
y = F.upsample(y, size=16, mode='bilinear')
y = F.upsample(y, scale_factor=2, mode='bilinear')
y = F.upsample(y, size=(20,20), mode='bilinear', align_corners=False)
y = F.upsample(y, scale_factor=(4,4), mode='bilinear', align_corners=False)
y = F.upsample(y, size=(16,24), mode='bilinear', align_corners=True)
y = F.upsample(y, scale_factor=(2,3), mode='bilinear', align_corners=True)
y = F.upsample(y, size=16, mode='bicubic')
y = F.upsample(y, scale_factor=2, mode='bicubic')
y = F.upsample(y, size=(20,20), mode='bicubic', align_corners=False)
y = F.upsample(y, scale_factor=(4,4), mode='bicubic', align_corners=False)
y = F.upsample(y, size=(16,24), mode='bicubic', align_corners=True)
y = F.upsample(y, scale_factor=(2,3), mode='bicubic', align_corners=True)
w = F.upsample(w, scale_factor=(1.499,1.499), mode='nearest')
return x, y, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 3, 32)
y = torch.rand(1, 3, 32, 32)
w = torch.rand(1, 8, 12, 12)
a = net(x, y, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, w))
mod.save("test_F_upsample.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_upsample.pt inputshape=[1,3,32],[1,3,32,32],[1,8,12,12]")
# ncnn inference
import test_F_upsample_ncnn
b = test_F_upsample_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
x = F.upsample_bilinear(x, size=(12,12))
x = F.upsample_bilinear(x, scale_factor=2)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_F_upsample_bilinear.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_upsample_bilinear.pt inputshape=[1,12,24,64]")
# ncnn inference
import test_F_upsample_bilinear_ncnn
b = test_F_upsample_bilinear_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,61 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, w):
x = F.upsample_nearest(x, size=(12,12))
x = F.upsample_nearest(x, scale_factor=2)
w = F.upsample_nearest(w, scale_factor=(2.976744,2.976744))
return x, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
w = torch.rand(1, 8, 86, 86)
a = net(x, w)
# export torchscript
mod = torch.jit.trace(net, (x, w))
mod.save("test_F_upsample_nearest.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_F_upsample_nearest.pt inputshape=[1,12,24,64],[1,8,86,86]")
# ncnn inference
import test_F_upsample_nearest_ncnn
b = test_F_upsample_nearest_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = x.contiguous(memory_format=torch.contiguous_format)
y = y.contiguous(memory_format=torch.preserve_format)
z = z.contiguous(memory_format=torch.channels_last)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(3, 16)
y = torch.rand(5, 9, 11)
z = torch.rand(8, 5, 9, 10)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_Tensor_contiguous.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_Tensor_contiguous.pt inputshape=[3,16],[5,9,11],[8,5,9,10]")
# ncnn inference
import test_Tensor_contiguous_ncnn
b = test_Tensor_contiguous_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.equal(a0, b0):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = x.repeat(2, 3)
x = x.repeat(3, 4)
y = y.repeat(2, 1, 4)
y = y.repeat(4, 5, 1)
z = z.repeat(2, 3, 1, 5)
z = z.repeat(3, 3, 1, 1)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(3, 16)
y = torch.rand(5, 9, 11)
z = torch.rand(8, 5, 9, 10)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_Tensor_repeat.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_Tensor_repeat.pt inputshape=[3,16],[5,9,11],[8,5,9,10]")
# ncnn inference
import test_Tensor_repeat_ncnn
b = test_Tensor_repeat_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.equal(a0, b0):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,67 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = x.reshape(2, 24)
x = x.reshape(48)
y = y.reshape(11, 5, 9)
y = y.reshape(99, 5)
z = z.reshape(4, 3, 6, 10)
z = z.reshape(15, 6, 8)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(3, 16)
y = torch.rand(5, 9, 11)
z = torch.rand(8, 5, 9, 2)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_Tensor_reshape.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_Tensor_reshape.pt inputshape=[3,16],[5,9,11],[8,5,9,2]")
# ncnn inference
import test_Tensor_reshape_ncnn
b = test_Tensor_reshape_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,67 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = x[:12,1:14:1]
x = x[...,1:]
x = x[:,:x.size(1)-1]
y = y[1:,5:,3:]
y = y[:,1:13:1,:14]
y = y[:y.size(1):,:,:]
z = z[4:]
z = z[:2,:,:,2:-2:1]
z = z[:,:,z.size(2)-3:,:]
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(13, 26)
y = torch.rand(15, 19, 21)
z = torch.rand(18, 15, 19, 20)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_Tensor_slice.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_Tensor_slice.pt inputshape=[13,26],[15,19,21],[18,15,19,20]")
# ncnn inference
import test_Tensor_slice_ncnn
b = test_Tensor_slice_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,67 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, x, y, z):
x = x.reshape(2, 24)
x = x.reshape(48)
y = y.reshape(11, 5, 9)
y = y.reshape(99, 5)
z = z.reshape(4, 3, 6, 10)
z = z.reshape(15, 6, 8)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(3, 16)
y = torch.rand(5, 9, 11)
z = torch.rand(8, 5, 9, 2)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_Tensor_view.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_Tensor_view.pt inputshape=[3,16],[5,9,11],[8,5,9,2]")
# ncnn inference
import test_Tensor_view_ncnn
b = test_Tensor_view_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,45 @@
# 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 torch
import torchvision.models as models
def test():
net = models.mobilenet_v2().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 3, 224, 224)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_mobilenet_v2.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_mobilenet_v2.pt inputshape=[1,3,224,224]")
# ncnn inference
import test_mobilenet_v2_ncnn
b = test_mobilenet_v2_ncnn.test_inference()
return torch.allclose(a, b, 1e-2, 1e-2)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,45 @@
# 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 torch
import torchvision.models as models
def test():
net = models.mobilenet_v3_small().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 3, 224, 224)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_mobilenet_v3_small.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_mobilenet_v3_small.pt inputshape=[1,3,224,224]")
# ncnn inference
import test_mobilenet_v3_small_ncnn
b = test_mobilenet_v3_small_ncnn.test_inference()
return torch.allclose(a, b, 1e-2, 1e-2)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,91 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2022 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
def forward(self, a0, a1, b0, b1, c0, c1, d0, d1, e0, e1, f0, f1, g0, g1, h0, h1, i0, i1, j0, j1, k0, k1, l0, l1):
a = torch.matmul(a0, a1.transpose(-2, -1))
b = torch.matmul(b0, b1.transpose(-2, -1))
c = torch.matmul(c0, c1.transpose(-2, -1))
d = torch.matmul(d0, d1.transpose(-2, -1))
e = torch.matmul(e0, e1.transpose(-2, -1))
f = torch.matmul(f0, f1.transpose(-2, -1))
g = torch.matmul(g0, g1.transpose(-2, -1))
h = torch.matmul(h0, h1.transpose(-2, -1))
i = torch.matmul(i0, i1.transpose(-2, -1))
j = torch.matmul(j0, j1.transpose(-2, -1))
k = torch.matmul(k0, k1.transpose(-2, -1))
l = torch.matmul(l0, l1.transpose(-2, -1))
return a, b, c, d, e, f, g, h, i, j, k, l
def test():
net = Model()
net.eval()
torch.manual_seed(0)
a0 = torch.rand(14)
a1 = torch.rand(6, 14)
b0 = torch.rand(13)
b1 = torch.rand(7, 4, 13)
c0 = torch.rand(15)
c1 = torch.rand(5, 7, 9, 15)
d0 = torch.rand(23, 14)
d1 = torch.rand(25, 14)
e0 = torch.rand(4, 5)
e1 = torch.rand(10, 40, 5)
f0 = torch.rand(14, 6)
f1 = torch.rand(2, 4, 20, 6)
g0 = torch.rand(10, 23, 14)
g1 = torch.rand(5, 14)
h0 = torch.rand(7, 8, 13, 14)
h1 = torch.rand(35, 14)
i0 = torch.rand(10, 23, 14)
i1 = torch.rand(10, 5, 14)
j0 = torch.rand(10, 13, 18)
j1 = torch.rand(3, 1, 8, 18)
k0 = torch.rand(1, 5, 23, 11)
k1 = torch.rand(8, 1, 9, 11)
l0 = torch.rand(6, 9, 13, 14)
l1 = torch.rand(6, 9, 15, 14)
a = net(a0, a1, b0, b1, c0, c1, d0, d1, e0, e1, f0, f1, g0, g1, h0, h1, i0, i1, j0, j1, k0, k1, l0, l1)
# export torchscript
mod = torch.jit.trace(net, (a0, a1, b0, b1, c0, c1, d0, d1, e0, e1, f0, f1, g0, g1, h0, h1, i0, i1, j0, j1, k0, k1, l0, l1))
mod.save("test_ncnn_fuse_transpose_matmul.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_ncnn_fuse_transpose_matmul.pt inputshape=[14],[6,14],[13],[7,4,13],[15],[5,7,9,15],[23,14],[25,14],[4,5],[10,40,5],[14,6],[2,4,20,6],[10,23,14],[5,14],[7,8,13,14],[35,14],[10,23,14],[10,5,14],[10,13,18],[3,1,8,18],[1,5,23,11],[8,1,9,11],[6,9,13,14],[6,9,15,14]")
# ncnn inference
import test_ncnn_fuse_transpose_matmul_ncnn
b = test_ncnn_fuse_transpose_matmul_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AdaptiveAvgPool1d(output_size=(7))
self.pool_1 = nn.AdaptiveAvgPool1d(output_size=1)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AdaptiveAvgPool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AdaptiveAvgPool1d.pt inputshape=[1,128,13]")
# ncnn inference
import test_nn_AdaptiveAvgPool1d_ncnn
b = test_nn_AdaptiveAvgPool1d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AdaptiveAvgPool2d(output_size=(7,6))
self.pool_1 = nn.AdaptiveAvgPool2d(output_size=1)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 13, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AdaptiveAvgPool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AdaptiveAvgPool2d.pt inputshape=[1,128,13,13]")
# ncnn inference
import test_nn_AdaptiveAvgPool2d_ncnn
b = test_nn_AdaptiveAvgPool2d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AdaptiveAvgPool3d(output_size=(7,6,5))
self.pool_1 = nn.AdaptiveAvgPool3d(output_size=1)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 13, 13, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AdaptiveAvgPool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AdaptiveAvgPool3d.pt inputshape=[1,128,13,13,13]")
# ncnn inference
import test_nn_AdaptiveAvgPool3d_ncnn
b = test_nn_AdaptiveAvgPool3d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AdaptiveMaxPool1d(output_size=(7))
self.pool_1 = nn.AdaptiveMaxPool1d(output_size=1)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AdaptiveMaxPool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AdaptiveMaxPool1d.pt inputshape=[1,128,13]")
# ncnn inference
import test_nn_AdaptiveMaxPool1d_ncnn
b = test_nn_AdaptiveMaxPool1d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AdaptiveMaxPool2d(output_size=(7,6))
self.pool_1 = nn.AdaptiveMaxPool2d(output_size=1)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 13, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AdaptiveMaxPool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AdaptiveMaxPool2d.pt inputshape=[1,128,13,13]")
# ncnn inference
import test_nn_AdaptiveMaxPool2d_ncnn
b = test_nn_AdaptiveMaxPool2d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AdaptiveMaxPool3d(output_size=(7,6,5))
self.pool_1 = nn.AdaptiveMaxPool3d(output_size=1)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 128, 13, 13, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AdaptiveMaxPool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AdaptiveMaxPool3d.pt inputshape=[1,128,13,13,13]")
# ncnn inference
import test_nn_AdaptiveMaxPool3d_ncnn
b = test_nn_AdaptiveMaxPool3d_ncnn.test_inference()
b = b.reshape_as(a)
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,70 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.dropout_0 = nn.AlphaDropout()
self.dropout_1 = nn.AlphaDropout(p=0.7)
def forward(self, x, y, z, w):
x = self.dropout_0(x)
y = self.dropout_0(y)
z = self.dropout_1(z)
w = self.dropout_1(w)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
w = F.relu(w)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
w = torch.rand(12, 24, 32, 64)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_nn_AlphaDropout.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AlphaDropout.pt inputshape=[12],[12,64],[12,24,64],[12,24,32,64]")
# ncnn inference
import test_nn_AlphaDropout_ncnn
b = test_nn_AlphaDropout_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,68 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AvgPool1d(kernel_size=3)
self.pool_1 = nn.AvgPool1d(kernel_size=4, stride=2, padding=2)
self.pool_2 = nn.AvgPool1d(kernel_size=3, stride=1, padding=(0), ceil_mode=False, count_include_pad=True)
self.pool_3 = nn.AvgPool1d(kernel_size=5, stride=2, padding=(2), ceil_mode=True, count_include_pad=False)
self.pool_4 = nn.AvgPool1d(kernel_size=3, stride=2, padding=1, ceil_mode=False, count_include_pad=True)
self.pool_5 = nn.AvgPool1d(kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
self.pool_6 = nn.AvgPool1d(kernel_size=4, stride=1, padding=2, ceil_mode=False, count_include_pad=False)
def forward(self, x):
x = self.pool_0(x)
x = self.pool_1(x)
x = self.pool_2(x)
x = self.pool_3(x)
x = self.pool_4(x)
x = self.pool_5(x)
x = self.pool_6(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AvgPool1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AvgPool1d.pt inputshape=[1,12,128]")
# ncnn inference
import test_nn_AvgPool1d_ncnn
b = test_nn_AvgPool1d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,78 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AvgPool2d(kernel_size=3)
self.pool_1 = nn.AvgPool2d(kernel_size=4, stride=2, padding=2)
self.pool_2 = nn.AvgPool2d(kernel_size=(1,3), stride=1, padding=(0,1), ceil_mode=False, count_include_pad=True)
self.pool_3 = nn.AvgPool2d(kernel_size=(4,5), stride=(1,2), padding=(1,2), ceil_mode=True, count_include_pad=False)
self.pool_4 = nn.AvgPool2d(kernel_size=(5,3), stride=(2,1), padding=1, ceil_mode=False, count_include_pad=True)
self.pool_5 = nn.AvgPool2d(kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
def forward(self, x):
y = x.reshape(12, 128, 128)
x = self.pool_0(x)
x = self.pool_1(x)
x = self.pool_2(x)
x = self.pool_3(x)
x = self.pool_4(x)
x = self.pool_5(x)
y = self.pool_0(y)
y = self.pool_1(y)
y = self.pool_2(y)
y = self.pool_3(y)
y = self.pool_4(y)
y = self.pool_5(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 128, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AvgPool2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AvgPool2d.pt inputshape=[1,12,128,128]")
# ncnn inference
import test_nn_AvgPool2d_ncnn
b = test_nn_AvgPool2d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,78 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pool_0 = nn.AvgPool3d(kernel_size=3)
self.pool_1 = nn.AvgPool3d(kernel_size=4, stride=2, padding=2)
self.pool_2 = nn.AvgPool3d(kernel_size=(1,2,3), stride=1, padding=(0,1,1), ceil_mode=False, count_include_pad=True)
self.pool_3 = nn.AvgPool3d(kernel_size=(3,4,5), stride=(1,2,2), padding=(1,1,2), ceil_mode=True, count_include_pad=False)
self.pool_4 = nn.AvgPool3d(kernel_size=(5,4,3), stride=(2,1,1), padding=1, ceil_mode=False, count_include_pad=True)
self.pool_5 = nn.AvgPool3d(kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
def forward(self, x):
y = x.reshape(12, 96, 128, 128)
x = self.pool_0(x)
x = self.pool_1(x)
x = self.pool_2(x)
x = self.pool_3(x)
x = self.pool_4(x)
x = self.pool_5(x)
y = self.pool_0(y)
y = self.pool_1(y)
y = self.pool_2(y)
y = self.pool_3(y)
y = self.pool_4(y)
y = self.pool_5(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 96, 128, 128)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_AvgPool3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_AvgPool3d.pt inputshape=[1,12,96,128,128]")
# ncnn inference
import test_nn_AvgPool3d_ncnn
b = test_nn_AvgPool3d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.bn_0 = nn.BatchNorm1d(num_features=32)
self.bn_1 = nn.BatchNorm1d(num_features=32, eps=1e-1, affine=False)
self.bn_2 = nn.BatchNorm1d(num_features=11, affine=True)
def forward(self, x, y):
x = self.bn_0(x)
x = self.bn_1(x)
y = self.bn_2(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 32, 64)
y = torch.rand(1, 11, 1)
a0, a1 = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_BatchNorm1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_BatchNorm1d.pt inputshape=[1,32,64],[1,11,1]")
# ncnn inference
import test_nn_BatchNorm1d_ncnn
b0, b1 = test_nn_BatchNorm1d_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.bn_0 = nn.BatchNorm2d(num_features=32)
self.bn_1 = nn.BatchNorm2d(num_features=32, eps=1e-1, affine=False)
self.bn_2 = nn.BatchNorm2d(num_features=11, affine=True)
def forward(self, x, y):
x = self.bn_0(x)
x = self.bn_1(x)
y = self.bn_2(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 32, 12, 64)
y = torch.rand(1, 11, 1, 1)
a0, a1 = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_BatchNorm2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_BatchNorm2d.pt inputshape=[1,32,12,64],[1,11,1,1]")
# ncnn inference
import test_nn_BatchNorm2d_ncnn
b0, b1 = test_nn_BatchNorm2d_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.bn_0 = nn.BatchNorm3d(num_features=32)
self.bn_1 = nn.BatchNorm3d(num_features=32, eps=1e-1, affine=False)
self.bn_2 = nn.BatchNorm3d(num_features=11, affine=True)
def forward(self, x, y):
x = self.bn_0(x)
x = self.bn_1(x)
y = self.bn_2(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 32, 12, 5, 64)
y = torch.rand(1, 11, 3, 1, 1)
a0, a1 = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_BatchNorm3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_BatchNorm3d.pt inputshape=[1,32,12,5,64],[1,11,3,1,1]")
# ncnn inference
import test_nn_BatchNorm3d_ncnn
b0, b1 = test_nn_BatchNorm3d_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,62 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.shuffle_0 = nn.ChannelShuffle(2)
self.shuffle_1 = nn.ChannelShuffle(16)
def forward(self, x, y):
x = self.shuffle_0(x)
x = self.shuffle_1(x)
y = self.shuffle_0(y)
y = self.shuffle_1(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 64, 6, 8)
y = torch.rand(1, 96, 7, 9)
a0, a1 = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_ChannelShuffle.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ChannelShuffle.pt inputshape=[1,64,6,8],[1,96,7,9]")
# ncnn inference
import test_nn_ChannelShuffle_ncnn
b0, b1 = test_nn_ChannelShuffle_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pad_0 = nn.ConstantPad1d(2, 0.1)
self.pad_1 = nn.ConstantPad1d(padding=(3,4), value=-1)
self.pad_2 = nn.ConstantPad1d(padding=(1,0), value=123)
def forward(self, x):
x = self.pad_0(x)
x = self.pad_1(x)
x = self.pad_2(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_ConstantPad1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ConstantPad1d.pt inputshape=[1,12,13]")
# ncnn inference
import test_nn_ConstantPad1d_ncnn
b = test_nn_ConstantPad1d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pad_0 = nn.ConstantPad2d(2, 0.1)
self.pad_1 = nn.ConstantPad2d(padding=(3,4,5,6), value=-2)
self.pad_2 = nn.ConstantPad2d(padding=(1,0,2,0), value=0)
def forward(self, x):
x = self.pad_0(x)
x = self.pad_1(x)
x = self.pad_2(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 13, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_ConstantPad2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ConstantPad2d.pt inputshape=[1,12,13,13]")
# ncnn inference
import test_nn_ConstantPad2d_ncnn
b = test_nn_ConstantPad2d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.pad_0 = nn.ConstantPad3d(2, 0.3)
self.pad_1 = nn.ConstantPad3d(padding=(1,2,3,4,5,6), value=0)
self.pad_2 = nn.ConstantPad3d(padding=(1,0,2,0,0,3), value=1.1)
def forward(self, x):
x = self.pad_0(x)
x = self.pad_1(x)
x = self.pad_2(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 13, 13, 13)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_ConstantPad3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ConstantPad3d.pt inputshape=[1,12,13,13,13]")
# ncnn inference
import test_nn_ConstantPad3d_ncnn
b = test_nn_ConstantPad3d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,73 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv_0 = nn.Conv1d(in_channels=12, out_channels=16, kernel_size=3)
self.conv_1 = nn.Conv1d(in_channels=16, out_channels=20, kernel_size=2, stride=2, padding=2, dilation=1)
self.conv_2 = nn.Conv1d(in_channels=20, out_channels=24, kernel_size=3, stride=1, padding=(4), dilation=1, groups=1, bias=False)
if torch.__version__ < '1.9':
self.conv_3 = nn.Conv1d(in_channels=24, out_channels=28, kernel_size=5, stride=1, padding=0, dilation=1, groups=4, bias=True)
self.conv_4 = nn.Conv1d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, dilation=2, groups=2, bias=False, padding_mode='zeros')
else:
self.conv_3 = nn.Conv1d(in_channels=24, out_channels=28, kernel_size=5, stride=1, padding='valid', dilation=1, groups=4, bias=True)
self.conv_4 = nn.Conv1d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding='same', dilation=2, groups=2, bias=False, padding_mode='zeros')
#self.conv_5 = nn.Conv1d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, dilation=1, groups=32, bias=True, padding_mode='reflect')
#self.conv_6 = nn.Conv1d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, dilation=1, groups=1, bias=False, padding_mode='replicate')
def forward(self, x):
x = self.conv_0(x)
x = self.conv_1(x)
x = self.conv_2(x)
x = self.conv_3(x)
x = self.conv_4(x)
#x = self.conv_5(x)
#x = self.conv_6(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_Conv1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Conv1d.pt inputshape=[1,12,64]")
# ncnn inference
import test_nn_Conv1d_ncnn
b = test_nn_Conv1d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,73 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv_0 = nn.Conv2d(in_channels=12, out_channels=16, kernel_size=3)
self.conv_1 = nn.Conv2d(in_channels=16, out_channels=20, kernel_size=(2,4), stride=(2,1), padding=2, dilation=1)
self.conv_2 = nn.Conv2d(in_channels=20, out_channels=24, kernel_size=(1,3), stride=1, padding=(2,4), dilation=1, groups=1, bias=False)
if torch.__version__ < '1.9':
self.conv_3 = nn.Conv2d(in_channels=24, out_channels=28, kernel_size=(5,4), stride=1, padding=0, dilation=1, groups=4, bias=True)
self.conv_4 = nn.Conv2d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, dilation=(1,2), groups=2, bias=False, padding_mode='zeros')
else:
self.conv_3 = nn.Conv2d(in_channels=24, out_channels=28, kernel_size=(5,4), stride=1, padding='valid', dilation=1, groups=4, bias=True)
self.conv_4 = nn.Conv2d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding='same', dilation=(1,2), groups=2, bias=False, padding_mode='zeros')
#self.conv_5 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, dilation=1, groups=32, bias=True, padding_mode='reflect')
#self.conv_6 = nn.Conv2d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, dilation=1, groups=1, bias=False, padding_mode='replicate')
def forward(self, x):
x = self.conv_0(x)
x = self.conv_1(x)
x = self.conv_2(x)
x = self.conv_3(x)
x = self.conv_4(x)
#x = self.conv_5(x)
#x = self.conv_6(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 64, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_Conv2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Conv2d.pt inputshape=[1,12,64,64]")
# ncnn inference
import test_nn_Conv2d_ncnn
b = test_nn_Conv2d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,75 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv_0 = nn.Conv3d(in_channels=12, out_channels=16, kernel_size=3)
self.conv_1 = nn.Conv3d(in_channels=16, out_channels=20, kernel_size=(2,3,4), stride=(2,2,1), padding=2, dilation=1)
self.conv_2 = nn.Conv3d(in_channels=20, out_channels=24, kernel_size=(1,2,3), stride=1, padding=(2,4,1), dilation=1, groups=1, bias=False)
if torch.__version__ < '1.9':
self.conv_3 = nn.Conv3d(in_channels=24, out_channels=28, kernel_size=(5,4,3), stride=1, padding=0, dilation=1, groups=4, bias=True)
self.conv_4 = nn.Conv3d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, dilation=(1,2,2), groups=2, bias=False, padding_mode='zeros')
else:
self.conv_3 = nn.Conv3d(in_channels=24, out_channels=28, kernel_size=(5,4,3), stride=1, padding='valid', dilation=1, groups=4, bias=True)
self.conv_4 = nn.Conv3d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding='same', dilation=(1,2,2), groups=2, bias=False, padding_mode='zeros')
#self.conv_5 = nn.Conv3d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, dilation=1, groups=32, bias=True, padding_mode='reflect')
#self.conv_6 = nn.Conv3d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, dilation=1, groups=1, bias=False, padding_mode='replicate')
#self.conv_7 = nn.Conv3d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(5,6), dilation=2, groups=1, bias=True, padding_mode='circular')
def forward(self, x):
x = self.conv_0(x)
x = self.conv_1(x)
x = self.conv_2(x)
x = self.conv_3(x)
x = self.conv_4(x)
#x = self.conv_5(x)
#x = self.conv_6(x)
#x = self.conv_7(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 48, 48, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_Conv3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Conv3d.pt inputshape=[1,12,48,48,64]")
# ncnn inference
import test_nn_Conv3d_ncnn
b = test_nn_Conv3d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,71 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.deconv_0 = nn.ConvTranspose1d(in_channels=12, out_channels=16, kernel_size=3)
self.deconv_1 = nn.ConvTranspose1d(in_channels=16, out_channels=20, kernel_size=2, stride=2, padding=2, output_padding=0)
self.deconv_2 = nn.ConvTranspose1d(in_channels=20, out_channels=24, kernel_size=3, stride=1, padding=(2), output_padding=(0), dilation=1, groups=1, bias=False)
self.deconv_3 = nn.ConvTranspose1d(in_channels=24, out_channels=28, kernel_size=5, stride=2, padding=0, output_padding=(1), dilation=1, groups=4, bias=True)
self.deconv_4 = nn.ConvTranspose1d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, output_padding=0, dilation=2, groups=2, bias=False)
self.deconv_5 = nn.ConvTranspose1d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, output_padding=1, dilation=1, groups=32, bias=True)
self.deconv_6 = nn.ConvTranspose1d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, output_padding=0, dilation=1, groups=1, bias=False)
self.deconv_7 = nn.ConvTranspose1d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(6), output_padding=(1), dilation=2, groups=1, bias=True)
def forward(self, x):
x = self.deconv_0(x)
x = self.deconv_1(x)
x = self.deconv_2(x)
x = self.deconv_3(x)
x = self.deconv_4(x)
x = self.deconv_5(x)
x = self.deconv_6(x)
x = self.deconv_7(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 10)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_ConvTranspose1d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ConvTranspose1d.pt inputshape=[1,12,10]")
# ncnn inference
import test_nn_ConvTranspose1d_ncnn
b = test_nn_ConvTranspose1d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,71 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.deconv_0 = nn.ConvTranspose2d(in_channels=12, out_channels=16, kernel_size=3)
self.deconv_1 = nn.ConvTranspose2d(in_channels=16, out_channels=20, kernel_size=(2,4), stride=(2,1), padding=2, output_padding=0)
self.deconv_2 = nn.ConvTranspose2d(in_channels=20, out_channels=24, kernel_size=(1,3), stride=1, padding=(2,4), output_padding=(0,0), dilation=1, groups=1, bias=False)
self.deconv_3 = nn.ConvTranspose2d(in_channels=24, out_channels=28, kernel_size=(5,4), stride=2, padding=0, output_padding=(0,1), dilation=1, groups=4, bias=True)
self.deconv_4 = nn.ConvTranspose2d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, output_padding=0, dilation=(1,2), groups=2, bias=False)
self.deconv_5 = nn.ConvTranspose2d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, output_padding=1, dilation=1, groups=32, bias=True)
self.deconv_6 = nn.ConvTranspose2d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, output_padding=0, dilation=1, groups=1, bias=False)
self.deconv_7 = nn.ConvTranspose2d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(5,6), output_padding=(1,0), dilation=2, groups=1, bias=True)
def forward(self, x):
x = self.deconv_0(x)
x = self.deconv_1(x)
x = self.deconv_2(x)
x = self.deconv_3(x)
x = self.deconv_4(x)
x = self.deconv_5(x)
x = self.deconv_6(x)
x = self.deconv_7(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 10, 10)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_ConvTranspose2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ConvTranspose2d.pt inputshape=[1,12,10,10]")
# ncnn inference
import test_nn_ConvTranspose2d_ncnn
b = test_nn_ConvTranspose2d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,71 @@
# Tencent is pleased to support the open source community by making ncnn available.
#
# Copyright (C) 2022 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.deconv_0 = nn.ConvTranspose3d(in_channels=12, out_channels=16, kernel_size=3)
self.deconv_1 = nn.ConvTranspose3d(in_channels=16, out_channels=20, kernel_size=(2,3,4), stride=(2,2,1), padding=2, output_padding=0)
self.deconv_2 = nn.ConvTranspose3d(in_channels=20, out_channels=24, kernel_size=(1,2,3), stride=1, padding=(2,3,4), output_padding=(0,0,0), dilation=1, groups=1, bias=False)
self.deconv_3 = nn.ConvTranspose3d(in_channels=24, out_channels=28, kernel_size=(5,4,3), stride=2, padding=0, output_padding=(0,1,1), dilation=1, groups=4, bias=True)
self.deconv_4 = nn.ConvTranspose3d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, output_padding=0, dilation=(1,2,2), groups=2, bias=False)
self.deconv_5 = nn.ConvTranspose3d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, output_padding=1, dilation=1, groups=32, bias=True)
self.deconv_6 = nn.ConvTranspose3d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, output_padding=0, dilation=1, groups=1, bias=False)
self.deconv_7 = nn.ConvTranspose3d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(5,6,7), output_padding=(1,0,1), dilation=2, groups=1, bias=True)
def forward(self, x):
x = self.deconv_0(x)
x = self.deconv_1(x)
x = self.deconv_2(x)
x = self.deconv_3(x)
x = self.deconv_4(x)
x = self.deconv_5(x)
x = self.deconv_6(x)
x = self.deconv_7(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 7, 7, 10)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_ConvTranspose3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ConvTranspose3d.pt inputshape=[1,12,7,7,10]")
# ncnn inference
import test_nn_ConvTranspose3d_ncnn
b = test_nn_ConvTranspose3d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,70 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.dropout_0 = nn.Dropout()
self.dropout_1 = nn.Dropout(p=0.7)
def forward(self, x, y, z, w):
x = self.dropout_0(x)
y = self.dropout_0(y)
z = self.dropout_1(z)
w = self.dropout_1(w)
x = F.relu(x)
y = F.relu(y)
z = F.relu(z)
w = F.relu(w)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
w = torch.rand(12, 24, 32, 64)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_nn_Dropout.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Dropout.pt inputshape=[12],[12,64],[12,24,64],[12,24,32,64]")
# ncnn inference
import test_nn_Dropout_ncnn
b = test_nn_Dropout_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.dropout_0 = nn.Dropout2d()
self.dropout_1 = nn.Dropout2d(p=0.7)
def forward(self, x, y):
x = self.dropout_0(x)
y = self.dropout_1(y)
x = F.relu(x)
y = F.relu(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12, 24, 64)
y = torch.rand(3, 4, 5)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_Dropout2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Dropout2d.pt inputshape=[12,24,64],[3,4,5]")
# ncnn inference
import test_nn_Dropout2d_ncnn
b = test_nn_Dropout2d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.dropout_0 = nn.Dropout3d()
self.dropout_1 = nn.Dropout3d(p=0.7)
def forward(self, x, y):
x = self.dropout_0(x)
y = self.dropout_1(y)
x = F.relu(x)
y = F.relu(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12, 6, 8, 16)
y = torch.rand(3, 4, 5, 6)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_Dropout3d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Dropout3d.pt inputshape=[12,6,8,16],[3,4,5,6]")
# ncnn inference
import test_nn_Dropout3d_ncnn
b = test_nn_Dropout3d_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.act_0 = nn.ELU()
self.act_1 = nn.ELU(alpha=1.3)
def forward(self, x, y, z):
x = self.act_0(x)
y = self.act_0(y)
z = self.act_1(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12)
y = torch.rand(1, 12, 64)
z = torch.rand(1, 12, 24, 64)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_nn_ELU.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_ELU.pt inputshape=[1,12],[1,12,64],[1,12,24,64]")
# ncnn inference
import test_nn_ELU_ncnn
b = test_nn_ELU_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,56 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.embed_0 = nn.Embedding(embedding_dim=128, num_embeddings=10)
def forward(self, x):
x = self.embed_0(x)
return x
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.randint(10, (13,), dtype=torch.int)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_Embedding.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Embedding.pt inputshape=[13]i32")
# ncnn inference
import test_nn_Embedding_ncnn
b = test_nn_Embedding_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.act_0 = nn.GELU()
def forward(self, x, y, z):
x = self.act_0(x)
y = self.act_0(y)
z = self.act_0(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_nn_GELU.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_GELU.pt inputshape=[12],[12,64],[12,24,64]")
# ncnn inference
import test_nn_GELU_ncnn
b = test_nn_GELU_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,87 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.gru_0_0 = nn.GRU(input_size=32, hidden_size=16)
self.gru_0_1 = nn.GRU(input_size=16, hidden_size=16, num_layers=3, bias=False)
self.gru_0_2 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True)
self.gru_0_3 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True)
self.gru_0_4 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True)
self.gru_1_0 = nn.GRU(input_size=25, hidden_size=16, batch_first=True)
self.gru_1_1 = nn.GRU(input_size=16, hidden_size=16, num_layers=3, bias=False, batch_first=True)
self.gru_1_2 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True)
self.gru_1_3 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True)
self.gru_1_4 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True)
def forward(self, x, y):
x = x.permute(1, 0, 2)
x0, _ = self.gru_0_0(x)
x1, _ = self.gru_0_1(x0)
x2, h0 = self.gru_0_2(x1)
x3, h1 = self.gru_0_3(x1, h0)
x4, _ = self.gru_0_4(x1, h1)
y0, _ = self.gru_1_0(y)
y1, _ = self.gru_1_1(y0)
y2, h2 = self.gru_1_2(y1)
y3, h3 = self.gru_1_3(y1, h2)
y4, _ = self.gru_1_4(y1, h3)
x2 = x2.permute(1, 0, 2)
x3 = x3.permute(1, 0, 2)
x4 = x4.permute(1, 0, 2)
return x2, x3, x4, y2, y3, y4
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 10, 32)
y = torch.rand(1, 12, 25)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_GRU.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_GRU.pt inputshape=[1,10,32],[1,12,25]")
# ncnn inference
import test_nn_GRU_ncnn
b = test_nn_GRU_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-3, 1e-3):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,60 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.gn_0 = nn.GroupNorm(num_groups=4, num_channels=12)
self.gn_1 = nn.GroupNorm(num_groups=12, num_channels=12, eps=1e-2, affine=True)
self.gn_2 = nn.GroupNorm(num_groups=1, num_channels=12, eps=1e-4, affine=True)
def forward(self, x):
x = self.gn_0(x)
x = self.gn_1(x)
x = self.gn_2(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
a0 = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_GroupNorm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_GroupNorm.pt inputshape=[1,12,24,64]")
# ncnn inference
import test_nn_GroupNorm_ncnn
b0 = test_nn_GroupNorm_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.act_0 = nn.Hardsigmoid()
def forward(self, x, y, z):
x = self.act_0(x)
y = self.act_0(y)
z = self.act_0(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_nn_Hardsigmoid.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Hardsigmoid.pt inputshape=[12],[12,64],[12,24,64]")
# ncnn inference
import test_nn_Hardsigmoid_ncnn
b = test_nn_Hardsigmoid_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.act_0 = nn.Hardswish()
def forward(self, x, y, z):
x = self.act_0(x)
y = self.act_0(y)
z = self.act_0(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_nn_Hardswish.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Hardswish.pt inputshape=[12],[12,64],[12,24,64]")
# ncnn inference
import test_nn_Hardswish_ncnn
b = test_nn_Hardswish_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,64 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.act_0 = nn.Hardtanh()
self.act_1 = nn.Hardtanh(-0.2, 0.2)
def forward(self, x, y, z):
x = self.act_0(x)
y = self.act_0(y)
z = self.act_1(z)
return x, y, z
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
a = net(x, y, z)
# export torchscript
mod = torch.jit.trace(net, (x, y, z))
mod.save("test_nn_Hardtanh.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Hardtanh.pt inputshape=[12],[12,64],[12,24,64]")
# ncnn inference
import test_nn_Hardtanh_ncnn
b = test_nn_Hardtanh_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,58 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.in_0 = nn.InstanceNorm2d(num_features=12, affine=True)
self.in_1 = nn.InstanceNorm2d(num_features=12, eps=1e-2, affine=True)
def forward(self, x):
x = self.in_0(x)
x = self.in_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
a = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_InstanceNorm2d.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_InstanceNorm2d.pt inputshape=[1,12,24,64]")
# ncnn inference
import test_nn_InstanceNorm2d_ncnn
b = test_nn_InstanceNorm2d_ncnn.test_inference()
return torch.allclose(a, b, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,87 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.lstm_0_0 = nn.LSTM(input_size=32, hidden_size=16)
self.lstm_0_1 = nn.LSTM(input_size=16, hidden_size=16, num_layers=3, bias=False)
self.lstm_0_2 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True)
self.lstm_0_3 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True)
self.lstm_0_4 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True)
self.lstm_1_0 = nn.LSTM(input_size=25, hidden_size=16, batch_first=True)
self.lstm_1_1 = nn.LSTM(input_size=16, hidden_size=16, num_layers=3, bias=False, batch_first=True)
self.lstm_1_2 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True)
self.lstm_1_3 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True)
self.lstm_1_4 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True)
def forward(self, x, y):
x = x.permute(1, 0, 2)
x0, _ = self.lstm_0_0(x)
x1, _ = self.lstm_0_1(x0)
x2, (h0, c0) = self.lstm_0_2(x1)
x3, (h1, c1) = self.lstm_0_3(x1, (h0, c0))
x4, _ = self.lstm_0_4(x1, (h1, c1))
y0, _ = self.lstm_1_0(y)
y1, _ = self.lstm_1_1(y0)
y2, (h2, c2) = self.lstm_1_2(y1)
y3, (h3, c3) = self.lstm_1_3(y1, (h2, c2))
y4, _ = self.lstm_1_4(y1, (h3, c3))
x2 = x2.permute(1, 0, 2)
x3 = x3.permute(1, 0, 2)
x4 = x4.permute(1, 0, 2)
return x2, x3, x4, y2, y3, y4
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 10, 32)
y = torch.rand(1, 12, 25)
a = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_LSTM.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_LSTM.pt inputshape=[1,10,32],[1,12,25]")
# ncnn inference
import test_nn_LSTM_ncnn
b = test_nn_LSTM_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-3, 1e-3):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,59 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.ln_0 = nn.LayerNorm(64)
self.ln_1 = nn.LayerNorm(normalized_shape=(24,64), eps=1e-2, elementwise_affine=False)
def forward(self, x, y):
x = self.ln_0(x)
y = self.ln_1(y)
return x, y
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(24, 64)
y = torch.rand(12, 24, 64)
a0, a1 = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_LayerNorm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_LayerNorm.pt inputshape=[24,64],[12,24,64]")
# ncnn inference
import test_nn_LayerNorm_ncnn
b0, b1 = test_nn_LayerNorm_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,66 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.act_0 = nn.LeakyReLU()
self.act_1 = nn.LeakyReLU(negative_slope=-0.24)
def forward(self, x, y, z, w):
x = self.act_0(x)
y = self.act_0(y)
z = self.act_1(z)
w = self.act_1(w)
return x, y, z, w
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(12)
y = torch.rand(12, 64)
z = torch.rand(12, 24, 64)
w = torch.rand(12, 24, 32, 64)
a = net(x, y, z, w)
# export torchscript
mod = torch.jit.trace(net, (x, y, z, w))
mod.save("test_nn_LeakyReLU.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_LeakyReLU.pt inputshape=[12],[12,64],[12,24,64],[12,24,32,64]")
# ncnn inference
import test_nn_LeakyReLU_ncnn
b = test_nn_LeakyReLU_ncnn.test_inference()
for a0, b0 in zip(a, b):
if not torch.allclose(a0, b0, 1e-4, 1e-4):
return False
return True
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,63 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear_0 = nn.Linear(in_features=64, out_features=16, bias=False)
self.linear_1 = nn.Linear(in_features=16, out_features=3, bias=True)
def forward(self, x, y):
x = self.linear_0(x)
x = self.linear_1(x)
y = self.linear_0(y)
y = self.linear_1(y)
return x, y
def test():
net = Model().half().float()
net.eval()
torch.manual_seed(0)
x = torch.rand(64)
y = torch.rand(12, 64)
a0, a1 = net(x, y)
# export torchscript
mod = torch.jit.trace(net, (x, y))
mod.save("test_nn_Linear.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_Linear.pt inputshape=[64],[12,64]")
# ncnn inference
import test_nn_Linear_ncnn
b0, b1 = test_nn_Linear_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4) and torch.allclose(a1, b1, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

View File

@ -0,0 +1,58 @@
# 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 torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.ln_0 = nn.LocalResponseNorm(3)
self.ln_1 = nn.LocalResponseNorm(size=5, alpha=0.001, beta=0.8, k=0.9)
def forward(self, x):
x = self.ln_0(x)
x = self.ln_1(x)
return x
def test():
net = Model()
net.eval()
torch.manual_seed(0)
x = torch.rand(1, 12, 24, 64)
a0 = net(x)
# export torchscript
mod = torch.jit.trace(net, x)
mod.save("test_nn_LocalResponseNorm.pt")
# torchscript to pnnx
import os
os.system("../../src/pnnx test_nn_LocalResponseNorm.pt inputshape=[1,12,24,64]")
# ncnn inference
import test_nn_LocalResponseNorm_ncnn
b0 = test_nn_LocalResponseNorm_ncnn.test_inference()
return torch.allclose(a0, b0, 1e-4, 1e-4)
if __name__ == "__main__":
if test():
exit(0)
else:
exit(1)

Some files were not shown because too many files have changed in this diff Show More