mirror of
https://github.com/Tencent/ncnn.git
synced 2026-10-18 13:42:18 +08:00
5ea98309e3
* harden layer parameter validation Original patches and author identities: https://github.com/Tencent/ncnn/pull/6912 (beilzx) https://github.com/Tencent/ncnn/pull/6919 (FluxMind-O) https://github.com/Tencent/ncnn/pull/6928 (lxcxjxhx) https://github.com/Tencent/ncnn/pull/6948 (ktz03) https://github.com/Tencent/ncnn/pull/6950 (ktz03) https://github.com/Tencent/ncnn/pull/6971 (LI) https://github.com/Tencent/ncnn/pull/6974 (LI) https://github.com/Tencent/ncnn/pull/6977 (LI) Co-authored-by: beilzx <2069360659@qq.com> Co-authored-by: FluxMind-O <3283918317@qq.com> Co-authored-by: lxcxjxhx <lxcxjxhx@users.noreply.github.com> Co-authored-by: ktz03 <2484593937@qq.com> Co-authored-by: LI <2484593937@qq.com>
163 lines
4.8 KiB
C++
163 lines
4.8 KiB
C++
// Copyright 2024 Tencent
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
#include "testutil.h"
|
|
|
|
#include "layer_type.h"
|
|
|
|
#include <limits.h>
|
|
|
|
static int test_spectrogram(int size, int n_fft, int power, int hoplen, int winlen, int window_type, int center, int pad_type, int normalized, int onesided)
|
|
{
|
|
ncnn::Mat a = RandomMat(size);
|
|
|
|
ncnn::ParamDict pd;
|
|
pd.set(0, n_fft);
|
|
pd.set(1, power);
|
|
pd.set(2, hoplen);
|
|
pd.set(3, winlen);
|
|
pd.set(4, window_type);
|
|
pd.set(5, center);
|
|
pd.set(6, pad_type);
|
|
pd.set(7, normalized);
|
|
pd.set(8, onesided);
|
|
|
|
std::vector<ncnn::Mat> weights(0);
|
|
|
|
int ret = test_layer("Spectrogram", pd, weights, a);
|
|
if (ret != 0)
|
|
{
|
|
fprintf(stderr, "test_spectrogram failed size=%d n_fft=%d power=%d hoplen=%d winlen=%d window_type=%d center=%d pad_type=%d normalized=%d onesided=%d\n", size, n_fft, power, hoplen, winlen, window_type, center, pad_type, normalized, onesided);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int test_spectrogram_0()
|
|
{
|
|
return 0
|
|
|| test_spectrogram(17, 1, 0, 1, 1, 0, 1, 0, 0, 0)
|
|
|| test_spectrogram(39, 17, 0, 7, 15, 0, 0, 0, 1, 0)
|
|
|| test_spectrogram(128, 10, 0, 2, 7, 1, 1, 1, 1, 1)
|
|
|| test_spectrogram(255, 17, 1, 14, 17, 2, 0, 0, 0, 1)
|
|
|| test_spectrogram(124, 55, 2, 12, 55, 1, 1, 2, 2, 0);
|
|
}
|
|
|
|
#if NCNN_VALIDATION
|
|
static int test_spectrogram_load_param()
|
|
{
|
|
ncnn::ParamDict base;
|
|
base.set(0, 16);
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, base, 0) != 0)
|
|
return -1;
|
|
|
|
int ret = 0
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 0, 0, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 0, -1, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 3, 0, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 3, -1, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 3, 17, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 2, 0, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 4, 3, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 7, 3, -1);
|
|
if (ret != 0)
|
|
return ret;
|
|
|
|
{
|
|
ncnn::ParamDict pd = base;
|
|
pd.set(0, INT_MAX);
|
|
pd.set(7, 2);
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, pd, -1) != 0)
|
|
return -1;
|
|
}
|
|
|
|
for (int n_fft = 1; n_fft <= 3; n_fft++)
|
|
{
|
|
ncnn::ParamDict pd = base;
|
|
pd.set(0, n_fft);
|
|
pd.set(7, 2);
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, pd, -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, pd, 2, 1, 0))
|
|
return -1;
|
|
}
|
|
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, base, 0, 4, 0) != 0)
|
|
return -1;
|
|
|
|
if (sizeof(size_t) == 4)
|
|
{
|
|
// window allocation overflows on 32-bit platforms
|
|
const int n_ffts[] = {0x40000001, 0x40000000, 0x3fffffff, INT_MAX};
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
for (int normalized = 0; normalized <= 2; normalized++)
|
|
{
|
|
ncnn::ParamDict pd = base;
|
|
pd.set(0, n_ffts[i]);
|
|
pd.set(7, normalized);
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, pd, -1) != 0)
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int test_spectrogram_load_param_type()
|
|
{
|
|
ncnn::ParamDict base;
|
|
base.set(0, 16);
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, base, 0) != 0)
|
|
return -1;
|
|
|
|
for (int i = 0; i <= 2; i++)
|
|
{
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, base, 1, i, 0) != 0)
|
|
return -1;
|
|
}
|
|
|
|
const int invalid[] = {-1, 3, INT_MIN, INT_MAX};
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, base, 1, invalid[i], -1) != 0)
|
|
return -1;
|
|
}
|
|
|
|
for (int i = 0; i <= 2; i++)
|
|
{
|
|
if (test_layer_param(ncnn::LayerType::Spectrogram, base, 6, i, 0) != 0)
|
|
return -1;
|
|
}
|
|
|
|
// padding is unused without centering
|
|
ncnn::ParamDict uncentered = base;
|
|
uncentered.set(5, 0);
|
|
|
|
const int invalid_pad[] = {-1, 3, INT_MIN, INT_MAX};
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
int ret = 0
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, base, 6, invalid_pad[i], -1)
|
|
|| test_layer_param(ncnn::LayerType::Spectrogram, uncentered, 6, invalid_pad[i], 0);
|
|
if (ret != 0)
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#endif // NCNN_VALIDATION
|
|
|
|
int main()
|
|
{
|
|
SRAND(7767517);
|
|
|
|
return 0
|
|
|| test_spectrogram_0()
|
|
#if NCNN_VALIDATION
|
|
|| test_spectrogram_load_param()
|
|
|| test_spectrogram_load_param_type()
|
|
#endif // NCNN_VALIDATION
|
|
;
|
|
}
|