diff --git a/paddleaudio/tests/benchmark/README.md b/paddleaudio/tests/benchmark/README.md new file mode 100644 index 00000000000..ef44544926f --- /dev/null +++ b/paddleaudio/tests/benchmark/README.md @@ -0,0 +1,45 @@ +# 1. Prepare +First, install `line_profiler` via pip. +```sh +pip install line_profiler +``` + +# 2. Run +Run the specific script for profiling. +```sh +kernprof -l features/mel_spectrogram.py +python -m line_profiler -u 1e-3 mel_spectrogram.py.lprof +``` + +Result: +```sh +Timer unit: 0.001 s + +Total time: 22.1208 s +File: features/mel_spectrogram.py +Function: test_melspect_cpu at line 13 + +Line # Hits Time Per Hit % Time Line Contents +============================================================== + 13 @profile + 14 def test_melspect_cpu(input_shape, times): + 15 1 0.1 0.1 0.0 paddle.set_device('cpu') + 16 1 234.5 234.5 1.1 x = paddle.randn(input_shape) + 17 1 85.3 85.3 0.4 feature_extractor = paddleaudio.features.MelSpectrogram(**feat_conf, dtype=x.dtype) + 18 101 0.5 0.0 0.0 for i in range(times): + 19 100 21800.5 218.0 98.6 y = feature_extractor(x) + +Total time: 4.80543 s +File: features/mel_spectrogram.py +Function: test_melspect_gpu at line 22 + +Line # Hits Time Per Hit % Time Line Contents +============================================================== + 22 @profile + 23 def test_melspect_gpu(input_shape, times): + 24 1 0.5 0.5 0.0 paddle.set_device('gpu') + 25 1 4144.8 4144.8 86.3 x = paddle.randn(input_shape) + 26 1 41.9 41.9 0.9 feature_extractor = paddleaudio.features.MelSpectrogram(**feat_conf, dtype=x.dtype) + 27 101 0.2 0.0 0.0 for i in range(times): + 28 100 618.1 6.2 12.9 y = feature_extractor(x) +``` diff --git a/paddleaudio/tests/benchmark/features/__init__.py b/paddleaudio/tests/benchmark/features/__init__.py new file mode 100644 index 00000000000..97043fd7ba6 --- /dev/null +++ b/paddleaudio/tests/benchmark/features/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. diff --git a/paddleaudio/tests/benchmark/features/log_mel_spectrogram.py b/paddleaudio/tests/benchmark/features/log_mel_spectrogram.py new file mode 100644 index 00000000000..75c21253d72 --- /dev/null +++ b/paddleaudio/tests/benchmark/features/log_mel_spectrogram.py @@ -0,0 +1,50 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 paddle + +import paddleaudio + +feat_conf = { + 'sr': 16000, + 'n_fft': 512, + 'hop_length': 128, + 'n_mels': 40, + 'f_min': 0.0, +} + + +@profile +def test_log_melspect_cpu(input_shape, times): + paddle.set_device('cpu') + x = paddle.randn(input_shape) + feature_extractor = paddleaudio.features.LogMelSpectrogram( + **feat_conf, dtype=x.dtype) + for i in range(times): + y = feature_extractor(x) + + +@profile +def test_log_melspect_gpu(input_shape, times): + paddle.set_device('gpu') + x = paddle.randn(input_shape) + feature_extractor = paddleaudio.features.LogMelSpectrogram( + **feat_conf, dtype=x.dtype) + for i in range(times): + y = feature_extractor(x) + + +input_shape = (16, 48000) # (N, T) +times = 100 +test_log_melspect_cpu(input_shape, times) +test_log_melspect_gpu(input_shape, times) diff --git a/paddleaudio/tests/benchmark/features/mel_spectrogram.py b/paddleaudio/tests/benchmark/features/mel_spectrogram.py new file mode 100644 index 00000000000..e8e7cc200a8 --- /dev/null +++ b/paddleaudio/tests/benchmark/features/mel_spectrogram.py @@ -0,0 +1,50 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 paddle + +import paddleaudio + +feat_conf = { + 'sr': 16000, + 'n_fft': 512, + 'hop_length': 128, + 'n_mels': 40, + 'f_min': 0.0, +} + + +@profile +def test_melspect_cpu(input_shape, times): + paddle.set_device('cpu') + x = paddle.randn(input_shape) + feature_extractor = paddleaudio.features.MelSpectrogram( + **feat_conf, dtype=x.dtype) + for i in range(times): + y = feature_extractor(x) + + +@profile +def test_melspect_gpu(input_shape, times): + paddle.set_device('gpu') + x = paddle.randn(input_shape) + feature_extractor = paddleaudio.features.MelSpectrogram( + **feat_conf, dtype=x.dtype) + for i in range(times): + y = feature_extractor(x) + + +input_shape = (16, 48000) # (N, T) +times = 100 +test_melspect_cpu(input_shape, times) +test_melspect_gpu(input_shape, times) diff --git a/paddleaudio/tests/benchmark/features/mfcc.py b/paddleaudio/tests/benchmark/features/mfcc.py new file mode 100644 index 00000000000..1d3fadd367e --- /dev/null +++ b/paddleaudio/tests/benchmark/features/mfcc.py @@ -0,0 +1,50 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 paddle + +import paddleaudio + +feat_conf = { + 'sr': 16000, + 'n_mfcc': 20, + 'n_fft': 512, + 'hop_length': 128, + 'n_mels': 40, + 'f_min': 0.0, + 'top_db': 80.0, +} + + +@profile +def test_mfcc_cpu(input_shape, times): + paddle.set_device('cpu') + x = paddle.randn(input_shape) + feature_extractor = paddleaudio.features.MFCC(**feat_conf, dtype=x.dtype) + for i in range(times): + y = feature_extractor(x) + + +@profile +def test_mfcc_gpu(input_shape, times): + paddle.set_device('gpu') + x = paddle.randn(input_shape) + feature_extractor = paddleaudio.features.MFCC(**feat_conf, dtype=x.dtype) + for i in range(times): + y = feature_extractor(x) + + +input_shape = (16, 48000) # (N, T) +times = 100 +test_mfcc_cpu(input_shape, times) +test_mfcc_gpu(input_shape, times)