From dcd98332cbc9c3ac1d26596bfc0e37407b61c764 Mon Sep 17 00:00:00 2001 From: Snehaa Giridharan Date: Thu, 3 Aug 2023 12:08:46 -0400 Subject: [PATCH 1/7] Initial commit - To Decibels Includes unittest setup --- include/rppt_tensor_audio_augmentations.h | 19 + .../cpu/host_tensor_audio_augmentations.hpp | 1 + src/modules/cpu/kernel/to_decibels.hpp | 122 + .../rppt_tensor_audio_augmentations.cpp | 32 + .../HOST/audio/Tensor_host_audio.cpp | 77 + .../to_decibels_ref_237-126133-0020.txt | 35840 +++++++++++++++ .../to_decibels_ref_237-134500-0001.txt | 28960 ++++++++++++ .../to_decibels_ref_237-134500-0004.txt | 35600 ++++++++++++++ .../to_decibels_ref_2830-3979-0005.txt | 33680 ++++++++++++++ .../to_decibels_ref_2830-3980-0059.txt | 29200 ++++++++++++ .../to_decibels_ref_6829-68769-0045.txt | 34160 ++++++++++++++ .../to_decibels_ref_6829-68769-0047.txt | 37200 +++++++++++++++ .../to_decibels_ref_6829-68771-0017.txt | 38240 ++++++++++++++++ 13 files changed, 273131 insertions(+) create mode 100644 src/modules/cpu/kernel/to_decibels.hpp create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_237-126133-0020.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_237-134500-0001.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_237-134500-0004.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_2830-3979-0005.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_2830-3980-0059.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_6829-68769-0045.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_6829-68769-0047.txt create mode 100644 utilities/test_suite/REFERENCE_OUTPUTS_AUDIO/to_decibels/to_decibels_ref_6829-68771-0017.txt diff --git a/include/rppt_tensor_audio_augmentations.h b/include/rppt_tensor_audio_augmentations.h index 52fa79afb..cda48722a 100644 --- a/include/rppt_tensor_audio_augmentations.h +++ b/include/rppt_tensor_audio_augmentations.h @@ -45,6 +45,25 @@ extern "C" { RppStatus rppt_non_silent_region_detection_host(RppPtr_t srcPtr, RpptDescPtr srcDescPtr, Rpp32s *srcSize, Rpp32f *detectedIndexTensor, Rpp32f *detectionLengthTensor, Rpp32f cutOffDB, Rpp32s windowLength, Rpp32f referencePower, Rpp32s resetInterval, rppHandle_t rppHandle); +/******************** to_decibels ********************/ + +// To Decibels augmentation for magnitude buffer + +// *param[in] srcPtr source tensor memory +// *param[in] srcDescPtr source tensor descriptor +// *param[out] dstPtr destination tensor memory +// *param[in] dstDescPtr destination tensor descriptor +// *param[in] srcDims source dimensions +// *param[in] cutOffDB minimum or cut-off ratio in dB +// *param[in] multiplier factor by which the logarithm is multiplied +// *param[in] referenceMagnitude Reference magnitude if not provided maximum value of input used as reference +// *param[in] rppHandle HIP-handle for "_gpu" variants and Host-handle for "_host" variants +// *returns a RppStatus enumeration. +// *retval RPP_SUCCESS : successful completion +// *retval RPP_ERROR : Error + +RppStatus rppt_to_decibels_host(RppPtr_t srcPtr, RpptDescPtr srcDescPtr, RppPtr_t dstPtr, RpptDescPtr dstDescPtr, RpptImagePatchPtr srcDims, Rpp32f cutOffDB, Rpp32f multiplier, Rpp32f referenceMagnitude, rppHandle_t rppHandle); + #ifdef __cplusplus } #endif diff --git a/src/modules/cpu/host_tensor_audio_augmentations.hpp b/src/modules/cpu/host_tensor_audio_augmentations.hpp index dbc9f0f29..76c3193fc 100644 --- a/src/modules/cpu/host_tensor_audio_augmentations.hpp +++ b/src/modules/cpu/host_tensor_audio_augmentations.hpp @@ -21,5 +21,6 @@ THE SOFTWARE. #define HOST_TENSOR_AUDIO_AUGMENTATIONS_HPP #include "kernel/non_silent_region_detection.hpp" +#include "kernel/to_decibels.hpp" #endif // HOST_TENSOR_AUDIO_AUGMENTATIONS_HPP \ No newline at end of file diff --git a/src/modules/cpu/kernel/to_decibels.hpp b/src/modules/cpu/kernel/to_decibels.hpp new file mode 100644 index 000000000..f6c6f100e --- /dev/null +++ b/src/modules/cpu/kernel/to_decibels.hpp @@ -0,0 +1,122 @@ +/* +Copyright (c) 2019 - 2023 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "rppdefs.h" +#include + +RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, + RpptDescPtr srcDescPtr, + Rpp32f *dstPtr, + RpptDescPtr dstDescPtr, + RpptImagePatchPtr srcDims, + Rpp32f cutOffDB, + Rpp32f multiplier, + Rpp32f referenceMagnitude, + rpp::Handle& handle) +{ + Rpp32u numThreads = handle.GetNumThreads(); + bool referenceMax = (referenceMagnitude == 0.0) ? false : true; + + // Calculate the intermediate values needed for DB conversion + Rpp32f minRatio = std::pow(10, cutOffDB / multiplier); + if(minRatio == 0.0f) + minRatio = std::nextafter(0.0f, 1.0f); + + const Rpp32f log10Factor = 0.3010299956639812; //1 / std::log(10); + multiplier *= log10Factor; + + omp_set_dynamic(0); +#pragma omp parallel for num_threads(numThreads) + for(int batchCount = 0; batchCount < srcDescPtr->n; batchCount++) + { + Rpp32f *srcPtrCurrent = srcPtr + batchCount * srcDescPtr->strides.nStride; + Rpp32f *dstPtrCurrent = dstPtr + batchCount * dstDescPtr->strides.nStride; + + Rpp32u height = srcDims[batchCount].height; + Rpp32u width = srcDims[batchCount].width; + Rpp32f refMag = referenceMagnitude; + + // Compute maximum value in the input buffer + if(!referenceMax) + { + refMag = -std::numeric_limits::max(); + Rpp32f *srcPtrTemp = srcPtrCurrent; + if(width == 1) + { + refMag = std::max(refMag, *(std::max_element(srcPtrTemp, srcPtrTemp + height))); + } + else + { + for(int i = 0; i < height; i++) + { + refMag = std::max(refMag, *(std::max_element(srcPtrTemp, srcPtrTemp + width))); + srcPtrTemp += srcDescPtr->strides.hStride; + } + } + } + + // Avoid division by zero + if(refMag == 0.0f) + refMag = 1.0f; + + Rpp32f invReferenceMagnitude = 1.f / refMag; + // Interpret as 1D array + if(width == 1) + { + Rpp32s vectorIncrement = 8; + Rpp32s alignedLength = (height / 8) * 8; + for(Rpp32s vectorLoopCount = 0; vectorLoopCount < height; vectorLoopCount++) + { + *dstPtrCurrent = multiplier * std::log2(std::max(minRatio, (*srcPtrCurrent) * invReferenceMagnitude)); + srcPtrCurrent++; + dstPtrCurrent++; + } + } + else + { + Rpp32s vectorIncrement = 8; + Rpp32s alignedLength = (width / 8) * 8; + + Rpp32f *srcPtrRow, *dstPtrRow; + srcPtrRow = srcPtrCurrent; + dstPtrRow = dstPtrCurrent; + for(int i = 0; i < height; i++) + { + Rpp32f *srcPtrTemp, *dstPtrTemp; + srcPtrTemp = srcPtrRow; + dstPtrTemp = dstPtrRow; + Rpp32s vectorLoopCount = 0; + for(; vectorLoopCount < width; vectorLoopCount++) + { + *dstPtrTemp = multiplier * std::log2(std::max(minRatio, (*srcPtrTemp) * invReferenceMagnitude)); + srcPtrTemp++; + dstPtrTemp++; + } + + srcPtrRow += srcDescPtr->strides.hStride; + dstPtrRow += dstDescPtr->strides.hStride; + } + } + } + + return RPP_SUCCESS; +} diff --git a/src/modules/rppt_tensor_audio_augmentations.cpp b/src/modules/rppt_tensor_audio_augmentations.cpp index 5afaf19ac..a9e7db759 100644 --- a/src/modules/rppt_tensor_audio_augmentations.cpp +++ b/src/modules/rppt_tensor_audio_augmentations.cpp @@ -57,3 +57,35 @@ RppStatus rppt_non_silent_region_detection_host(RppPtr_t srcPtr, return RPP_SUCCESS; } + +/******************** to_decibels ********************/ + +RppStatus rppt_to_decibels_host(RppPtr_t srcPtr, + RpptDescPtr srcDescPtr, + RppPtr_t dstPtr, + RpptDescPtr dstDescPtr, + RpptImagePatchPtr srcDims, + Rpp32f cutOffDB, + Rpp32f multiplier, + Rpp32f referenceMagnitude, + rppHandle_t rppHandle) +{ + if ((srcDescPtr->dataType == RpptDataType::F32) && (dstDescPtr->dataType == RpptDataType::F32)) + { + to_decibels_host_tensor(static_cast(srcPtr), + srcDescPtr, + static_cast(dstPtr), + dstDescPtr, + srcDims, + cutOffDB, + multiplier, + referenceMagnitude, + rpp::deref(rppHandle)); + + return RPP_SUCCESS; + } + else + { + return RPP_ERROR_NOT_IMPLEMENTED; + } +} diff --git a/utilities/test_suite/HOST/audio/Tensor_host_audio.cpp b/utilities/test_suite/HOST/audio/Tensor_host_audio.cpp index eb7d2812b..e905740cb 100644 --- a/utilities/test_suite/HOST/audio/Tensor_host_audio.cpp +++ b/utilities/test_suite/HOST/audio/Tensor_host_audio.cpp @@ -91,6 +91,55 @@ void verify_non_silent_region_detection(float *detectedIndex, float *detectionLe std::cerr<<"FAILED! "<n; batchcount++) + { + string current_file_name = audioNames[batchcount]; + size_t last_index = current_file_name.find_last_of("."); + current_file_name = current_file_name.substr(0, last_index); // Remove extension from file name + string out_file = ref_path + test_case + "/" + test_case + "_ref_" + current_file_name + ".txt"; + ref_file.open(out_file, ios::in); + if(!ref_file.is_open()) + { + cerr<<"Unable to open the file specified! Please check the path of the file given as input"<strides.nStride; + Rpp32f *dstPtrRow = dstPtrCurrent; + for(int i = 0; i < dstDims[batchcount].height; i++) + { + Rpp32f *dstPtrTemp = dstPtrRow; + for(int j = 0; j < dstDims[batchcount].width; j++) + { + ref_file>>ref_val; + out_val = dstPtrTemp[j]; + bool invalid_comparision = ((out_val == 0.0f) && (ref_val != 0.0f)); + if(!invalid_comparision && abs(out_val - ref_val) < 1e-20) + matched_indices += 1; + } + dstPtrRow += dstDescPtr->strides.hStride; + } + ref_file.close(); + if(matched_indices == (dstDims[batchcount].width * dstDims[batchcount].height) && matched_indices !=0) + file_match++; + } + + std::cerr<n) + std::cerr<<"PASSED!"<n<<" outputs are matching with reference outputs"< Date: Thu, 17 Aug 2023 12:37:33 -0400 Subject: [PATCH 2/7] Cleanup --- src/modules/cpu/kernel/to_decibels.hpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/modules/cpu/kernel/to_decibels.hpp b/src/modules/cpu/kernel/to_decibels.hpp index f6c6f100e..c3d308742 100644 --- a/src/modules/cpu/kernel/to_decibels.hpp +++ b/src/modules/cpu/kernel/to_decibels.hpp @@ -34,7 +34,6 @@ RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, rpp::Handle& handle) { Rpp32u numThreads = handle.GetNumThreads(); - bool referenceMax = (referenceMagnitude == 0.0) ? false : true; // Calculate the intermediate values needed for DB conversion Rpp32f minRatio = std::pow(10, cutOffDB / multiplier); @@ -56,14 +55,12 @@ RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, Rpp32f refMag = referenceMagnitude; // Compute maximum value in the input buffer - if(!referenceMax) + if(!referenceMagnitude) { refMag = -std::numeric_limits::max(); Rpp32f *srcPtrTemp = srcPtrCurrent; if(width == 1) - { refMag = std::max(refMag, *(std::max_element(srcPtrTemp, srcPtrTemp + height))); - } else { for(int i = 0; i < height; i++) @@ -75,15 +72,13 @@ RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, } // Avoid division by zero - if(refMag == 0.0f) + if(!refMag) refMag = 1.0f; Rpp32f invReferenceMagnitude = 1.f / refMag; // Interpret as 1D array if(width == 1) { - Rpp32s vectorIncrement = 8; - Rpp32s alignedLength = (height / 8) * 8; for(Rpp32s vectorLoopCount = 0; vectorLoopCount < height; vectorLoopCount++) { *dstPtrCurrent = multiplier * std::log2(std::max(minRatio, (*srcPtrCurrent) * invReferenceMagnitude)); @@ -93,9 +88,6 @@ RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, } else { - Rpp32s vectorIncrement = 8; - Rpp32s alignedLength = (width / 8) * 8; - Rpp32f *srcPtrRow, *dstPtrRow; srcPtrRow = srcPtrCurrent; dstPtrRow = dstPtrCurrent; From 8e05043aee664a029668933bda4cbd897feb33bf Mon Sep 17 00:00:00 2001 From: Snehaa Giridharan Date: Mon, 4 Sep 2023 06:02:31 -0400 Subject: [PATCH 3/7] Additional Cleanup --- src/modules/cpu/kernel/to_decibels.hpp | 12 ++---------- src/modules/rppt_tensor_audio_augmentations.cpp | 2 ++ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/modules/cpu/kernel/to_decibels.hpp b/src/modules/cpu/kernel/to_decibels.hpp index c3d308742..4097dce86 100644 --- a/src/modules/cpu/kernel/to_decibels.hpp +++ b/src/modules/cpu/kernel/to_decibels.hpp @@ -80,11 +80,7 @@ RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, if(width == 1) { for(Rpp32s vectorLoopCount = 0; vectorLoopCount < height; vectorLoopCount++) - { - *dstPtrCurrent = multiplier * std::log2(std::max(minRatio, (*srcPtrCurrent) * invReferenceMagnitude)); - srcPtrCurrent++; - dstPtrCurrent++; - } + *dstPtrCurrent++ = multiplier * std::log2(std::max(minRatio, (*srcPtrCurrent++) * invReferenceMagnitude)); } else { @@ -98,11 +94,7 @@ RppStatus to_decibels_host_tensor(Rpp32f *srcPtr, dstPtrTemp = dstPtrRow; Rpp32s vectorLoopCount = 0; for(; vectorLoopCount < width; vectorLoopCount++) - { - *dstPtrTemp = multiplier * std::log2(std::max(minRatio, (*srcPtrTemp) * invReferenceMagnitude)); - srcPtrTemp++; - dstPtrTemp++; - } + *dstPtrTemp++ = multiplier * std::log2(std::max(minRatio, (*srcPtrTemp++) * invReferenceMagnitude)); srcPtrRow += srcDescPtr->strides.hStride; dstPtrRow += dstDescPtr->strides.hStride; diff --git a/src/modules/rppt_tensor_audio_augmentations.cpp b/src/modules/rppt_tensor_audio_augmentations.cpp index a9e7db759..b88e1097a 100644 --- a/src/modules/rppt_tensor_audio_augmentations.cpp +++ b/src/modules/rppt_tensor_audio_augmentations.cpp @@ -70,6 +70,8 @@ RppStatus rppt_to_decibels_host(RppPtr_t srcPtr, Rpp32f referenceMagnitude, rppHandle_t rppHandle) { + if (multiplier == 0) + return RPP_ERROR_ZERO_DIVISION; if ((srcDescPtr->dataType == RpptDataType::F32) && (dstDescPtr->dataType == RpptDataType::F32)) { to_decibels_host_tensor(static_cast(srcPtr), From 337ccc1fa0551ee85bc83f2d77ad37aede76d612 Mon Sep 17 00:00:00 2001 From: sampath1117 Date: Fri, 22 Sep 2023 07:11:39 +0000 Subject: [PATCH 4/7] added doxygen changes for to_decibels --- include/rppt_tensor_audio_augmentations.h | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/rppt_tensor_audio_augmentations.h b/include/rppt_tensor_audio_augmentations.h index 21603eb99..b250c5d72 100644 --- a/include/rppt_tensor_audio_augmentations.h +++ b/include/rppt_tensor_audio_augmentations.h @@ -55,22 +55,22 @@ extern "C" { */ RppStatus rppt_non_silent_region_detection_host(RppPtr_t srcPtr, RpptDescPtr srcDescPtr, Rpp32s *srcLengthTensor, Rpp32f *detectedIndexTensor, Rpp32f *detectionLengthTensor, Rpp32f cutOffDB, Rpp32s windowLength, Rpp32f referencePower, Rpp32s resetInterval, rppHandle_t rppHandle); -/******************** to_decibels ********************/ - -// To Decibels augmentation for magnitude buffer - -// *param[in] srcPtr source tensor memory -// *param[in] srcDescPtr source tensor descriptor -// *param[out] dstPtr destination tensor memory -// *param[in] dstDescPtr destination tensor descriptor -// *param[in] srcDims source dimensions -// *param[in] cutOffDB minimum or cut-off ratio in dB -// *param[in] multiplier factor by which the logarithm is multiplied -// *param[in] referenceMagnitude Reference magnitude if not provided maximum value of input used as reference -// *param[in] rppHandle HIP-handle for "_gpu" variants and Host-handle for "_host" variants -// *returns a RppStatus enumeration. -// *retval RPP_SUCCESS : successful completion -// *retval RPP_ERROR : Error +/*! \brief To Decibels augmentation HOST +* \details To Decibels augmentation that converts magnitude values to decibel values +* \param[in] srcPtr source tensor memory +* \param[in] srcDescPtr source tensor descriptor +* \param[out] dstPtr destination tensor memory +* \param[in] dstDescPtr destination tensor descriptor +* \param[in] srcDims source tensor size (tensor of batchSize * 2 values) +* \param[in] cutOffDB minimum or cut-off ratio in dB +* \param[in] multiplier factor by which the logarithm is multiplied +* \param[in] referenceMagnitude Reference magnitude if not provided maximum value of input used as reference +* \param[in] rppHandle HIP-handle for "_gpu" variants and Host-handle for "_host" variants + * \return RppStatus enum. + * \returns RPP_SUCCESS \ref RppStatus on successful completion. + * Else return RPP_ERROR + * \ingroup group_tensor_audio + */ RppStatus rppt_to_decibels_host(RppPtr_t srcPtr, RpptDescPtr srcDescPtr, RppPtr_t dstPtr, RpptDescPtr dstDescPtr, RpptImagePatchPtr srcDims, Rpp32f cutOffDB, Rpp32f multiplier, Rpp32f referenceMagnitude, rppHandle_t rppHandle); From d1b5b4167fc52197a3be59ae619b0a0d05605c41 Mon Sep 17 00:00:00 2001 From: sampath1117 Date: Fri, 22 Sep 2023 08:36:40 +0000 Subject: [PATCH 5/7] updated test suite support for to_decibels --- .../test_suite/HOST/Tensor_host_audio.cpp | 24 ++++++++++++++++- utilities/test_suite/HOST/runAudioTests.py | 26 +++++++++---------- utilities/test_suite/rpp_test_suite_audio.h | 6 +++++ utilities/test_suite/rpp_test_suite_common.h | 5 ---- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/utilities/test_suite/HOST/Tensor_host_audio.cpp b/utilities/test_suite/HOST/Tensor_host_audio.cpp index 08b398b96..d554425e3 100644 --- a/utilities/test_suite/HOST/Tensor_host_audio.cpp +++ b/utilities/test_suite/HOST/Tensor_host_audio.cpp @@ -29,7 +29,7 @@ int main(int argc, char **argv) if (argc < MIN_ARG_COUNT) { printf("\nImproper Usage! Needs all arguments!\n"); - printf("\nUsage: ./Tensor_host_audio f16 = 3 / u8->f32 = 4 / i8 = 5 / u8->i8 = 6> \n"); + printf("\nUsage: ./Tensor_host_audio f16 = 3 / u8->f32 = 4 / i8 = 5 / u8->i8 = 6> \n"); return -1; } @@ -236,6 +236,28 @@ int main(int argc, char **argv) break; } + case 1: + { + testCaseName = "to_decibels"; + Rpp32f cutOffDB = std::log(1e-20); + Rpp32f multiplier = std::log(10); + Rpp32f referenceMagnitude = 1.0f; + + for (i = 0; i < noOfAudioFiles; i++) + { + srcDims[i].height = srcLengthTensor[i]; + srcDims[i].width = 1; + } + + startWallTime = omp_get_wtime(); + startCpuTime = clock(); + if (inputBitDepth == 2) + rppt_to_decibels_host(inputf32, srcDescPtr, outputf32, dstDescPtr, srcDims, cutOffDB, multiplier, referenceMagnitude, handle); + else + missingFuncFlag = 1; + + break; + } default: { missingFuncFlag = 1; diff --git a/utilities/test_suite/HOST/runAudioTests.py b/utilities/test_suite/HOST/runAudioTests.py index a0c321304..b02eee10a 100644 --- a/utilities/test_suite/HOST/runAudioTests.py +++ b/utilities/test_suite/HOST/runAudioTests.py @@ -119,12 +119,12 @@ def run_performance_test(loggingFolder, srcPath, case, numRuns, testType, bitDep def rpp_test_suite_parser_and_validator(): parser = argparse.ArgumentParser() parser.add_argument("--input_path", type = str, default = inFilePath, help = "Path to the input folder") - parser.add_argument("--case_start", type = int, default = 0, help = "Testing range starting case # - (0:0)") - parser.add_argument("--case_end", type = int, default = 0, help = "Testing range ending case # - (0:0)") + parser.add_argument("--case_start", type = int, default = 0, help = "Testing range starting case # - (0:1)") + parser.add_argument("--case_end", type = int, default = 1, help = "Testing range ending case # - (0:1)") parser.add_argument('--test_type', type = int, default = 0, help = "Type of Test - (0 = QA tests / 1 = Performance tests)") parser.add_argument('--case_list', nargs = "+", help = "List of case numbers to test", required = False) parser.add_argument('--num_runs', type = int, default = 1, help = "Specifies the number of runs for running the performance tests") - parser.add_argument('--preserve_output', type = int, default = 1, help = "preserves the output of the program - (0 = override output / 1 = preserve output )" ) + parser.add_argument('--preserve_output', type = int, default = 1, help = "preserves the output of the program - (0 = override output / 1 = preserve output )") parser.add_argument('--batch_size', type = int, default = 1, help = "Specifies the batch size to use for running tests. Default is 1.") args = parser.parse_args() @@ -132,8 +132,8 @@ def rpp_test_suite_parser_and_validator(): validate_path(args.input_path) # validate the parameters passed by user - if ((args.case_start < 0 or args.case_start > 0) or (args.case_end < 0 or args.case_end > 0)): - print("Starting case# and Ending case# must be in the 0:0 range. Aborting!") + if ((args.case_start < 0 or args.case_start > 1) or (args.case_end < 0 or args.case_end > 1)): + print("Starting case# and Ending case# must be in the 0:1 range. Aborting!") exit(0) elif args.case_end < args.case_start: print("Ending case# must be greater than starting case#. Aborting!") @@ -141,7 +141,7 @@ def rpp_test_suite_parser_and_validator(): elif args.test_type < 0 or args.test_type > 1: print("Test Type# must be in the 0 / 1. Aborting!") exit(0) - elif args.case_list is not None and args.case_start > 0 and args.case_end < 0: + elif args.case_list is not None and args.case_start > 1 and args.case_end < 0: print("Invalid input! Please provide only 1 option between case_list, case_start and case_end") exit(0) elif args.num_runs <= 0: @@ -162,8 +162,8 @@ def rpp_test_suite_parser_and_validator(): args.case_list = [str(x) for x in args.case_list] else: for case in args.case_list: - if int(case) != 0: - print("The case# must be 0!") + if int(case) < 0 or int(case) > 1: + print("The case# must be 0-1 range!") exit(0) return args @@ -216,21 +216,21 @@ def rpp_test_suite_parser_and_validator(): if batchSize != 8: print("QA tests can only run with a batch size of 8.") exit(0) - if int(case) != 0: - print(f"Invalid case number {case}. Case number must be 0!") + if int(case) < 0 or int(case) > 1: + print(f"Invalid case number {case}. Case number must be 0-1 range!") continue run_unit_test(srcPath, case, numRuns, testType, bitDepth, batchSize, outFilePath) else: for case in caseList: - if int(case) != 0: - print(f"Invalid case number {case}. Case number must be 0!") + if int(case) < 0 or int(case) > 1: + print(f"Invalid case number {case}. Case number must be 0-1 range!") continue run_performance_test(loggingFolder, srcPath, case, numRuns, testType, bitDepth, batchSize, outFilePath) # print the results of qa tests -supportedCaseList = ['0'] +supportedCaseList = ['0', '1'] supportedCases = 0 for num in caseList: if num in supportedCaseList: diff --git a/utilities/test_suite/rpp_test_suite_audio.h b/utilities/test_suite/rpp_test_suite_audio.h index 20be30c75..401f14a80 100644 --- a/utilities/test_suite/rpp_test_suite_audio.h +++ b/utilities/test_suite/rpp_test_suite_audio.h @@ -34,6 +34,12 @@ typedef half Rpp16f; // Include this header file to use functions from libsndfile #include +std::map audioAugmentationMap = +{ + {0, "non_silent_region_detection"}, + {1, "to_decibels"}, +}; + void verify_output(Rpp32f *dstPtr, RpptDescPtr dstDescPtr, RpptImagePatchPtr dstDims, string testCase, vector audioNames, string dst) { fstream refFile; diff --git a/utilities/test_suite/rpp_test_suite_common.h b/utilities/test_suite/rpp_test_suite_common.h index 829e61455..9cbc9d934 100644 --- a/utilities/test_suite/rpp_test_suite_common.h +++ b/utilities/test_suite/rpp_test_suite_common.h @@ -75,11 +75,6 @@ std::map augmentationMap = {87, "tensor_sum"} }; -std::map audioAugmentationMap = -{ - {0, "non_silent_region_detection"}, -}; - template inline T validate_pixel_range(T pixel) { From c4138366bff453dbc7ce6f12b0cde5241f20f294 Mon Sep 17 00:00:00 2001 From: sampath1117 Date: Wed, 27 Sep 2023 11:57:24 +0000 Subject: [PATCH 6/7] merge with latest changes --- .../kernel/non_silent_region_detection.hpp | 26 ++-- .../test_suite/HOST/Tensor_host_audio.cpp | 145 +++++------------- utilities/test_suite/rpp_test_suite_audio.h | 86 ++++++++++- 3 files changed, 130 insertions(+), 127 deletions(-) diff --git a/src/modules/cpu/kernel/non_silent_region_detection.hpp b/src/modules/cpu/kernel/non_silent_region_detection.hpp index 158ccff84..734d267b8 100644 --- a/src/modules/cpu/kernel/non_silent_region_detection.hpp +++ b/src/modules/cpu/kernel/non_silent_region_detection.hpp @@ -28,14 +28,6 @@ Rpp32f getSquare(Rpp32f &value) return (value * value); } -Rpp32f getMax(Rpp32f *values, Rpp32s srcLength) -{ - Rpp32f max = values[0]; - for(int i = 1; i < srcLength; i++) - max = std::max(max, values[i]); - return max; -} - RppStatus non_silent_region_detection_host_tensor(Rpp32f *srcPtr, RpptDescPtr srcDescPtr, Rpp32s *srcLengthTensor, @@ -56,16 +48,16 @@ RppStatus non_silent_region_detection_host_tensor(Rpp32f *srcPtr, { Rpp32f *srcPtrTemp = srcPtr + batchCount * srcDescPtr->strides.nStride; Rpp32s srcLength = srcLengthTensor[batchCount]; + + // mmsBuffer length is equal to input audio length and can vary dynamically for each input in a batch + // preallocating a static buffer for entire batchsize will be too big, so allocate mmsBuffer for each sample dynamically Rpp32f *mmsBuffer = static_cast(calloc(srcLength, sizeof(Rpp32f))); bool referenceMax = (referencePower == 0.0f); // set reset interval based on the user input Rpp32s resetLength = (resetInterval == -1) ? srcLength : resetInterval; - // Calculate buffer size for mms array and allocate mms buffer - Rpp32s mmsBufferSize = srcLength; - - // Calculate moving mean square of input array and store srcPtrTemp mms buffer + // calculate moving mean square of input Rpp32f meanFactor = 1.0f / windowLength; Rpp32s windowBegin = -windowLength + 1; for (Rpp32s outPos = 0; outPos < srcLength;) @@ -84,12 +76,12 @@ RppStatus non_silent_region_detection_host_tensor(Rpp32f *srcPtr, } } - // Convert cutOff from DB to magnitude - Rpp32f base = (referenceMax) ? getMax(mmsBuffer, mmsBufferSize) : referencePower; + // convert cutoff from DB to magnitude + Rpp32f base = (referenceMax) ? *std::max_element(mmsBuffer, mmsBuffer + srcLength) : referencePower; Rpp32f cutOffMag = base * cutOff; - // Calculate begining index, length of non silent region from the mms buffer - Rpp32s endIdx = mmsBufferSize; + // calculate begining index, length of non silent region from the mms buffer + Rpp32s endIdx = srcLength; Rpp32s beginIdx = endIdx; Rpp32s detectBegin, detectEnd; for(int i = 0; i < endIdx; i++) @@ -119,7 +111,7 @@ RppStatus non_silent_region_detection_host_tensor(Rpp32f *srcPtr, detectEnd = endIdx - beginIdx + 1; } - // Extend non silent region + // extend non silent region if(detectBegin != 0 && detectEnd != 0) { Rpp32s newBegin = std::max(detectBegin - (windowLength - 1), 0); diff --git a/utilities/test_suite/HOST/Tensor_host_audio.cpp b/utilities/test_suite/HOST/Tensor_host_audio.cpp index d554425e3..06378c870 100644 --- a/utilities/test_suite/HOST/Tensor_host_audio.cpp +++ b/utilities/test_suite/HOST/Tensor_host_audio.cpp @@ -66,12 +66,12 @@ int main(int argc, char **argv) // Other initializations int missingFuncFlag = 0; - int i = 0, j = 0, fileCnt = 0; - int maxChannels = 0; + int i = 0, j = 0; + int maxSrcChannels = 0; int maxSrcWidth = 0, maxSrcHeight = 0; int maxDstWidth = 0, maxDstHeight = 0; - unsigned long long iBufferSize = 0; - unsigned long long oBufferSize = 0; + Rpp64u iBufferSize = 0; + Rpp64u oBufferSize = 0; static int noOfAudioFiles = 0; // String ops on function name @@ -81,12 +81,12 @@ int main(int argc, char **argv) string func = funcName; // Get number of audio files - vector audioNames, audioFilePath; - search_files_recursive(src, audioNames, audioFilePath, ".wav"); + vector audioNames, audioFilesPath; + search_files_recursive(src, audioNames, audioFilesPath, ".wav"); noOfAudioFiles = audioNames.size(); if (noOfAudioFiles < batchSize || ((noOfAudioFiles % batchSize) != 0)) { - replicate_last_file_to_fill_batch(audioFilePath[noOfAudioFiles - 1], audioFilePath, audioNames, audioNames[noOfAudioFiles - 1], noOfAudioFiles, batchSize); + replicate_last_file_to_fill_batch(audioFilesPath[noOfAudioFiles - 1], audioFilesPath, audioNames, audioNames[noOfAudioFiles - 1], noOfAudioFiles, batchSize); noOfAudioFiles = audioNames.size(); } @@ -99,64 +99,20 @@ int main(int argc, char **argv) // Find max audio dimensions in the input dataset maxSrcHeight = 1; maxDstHeight = 1; - for (int cnt = 0; cnt < noOfAudioFiles ; cnt++) - { - SNDFILE *infile; - SF_INFO sfinfo; - int readcount; - - // The SF_INFO struct must be initialized before using it - memset (&sfinfo, 0, sizeof (sfinfo)); - if (!(infile = sf_open (audioFilePath[cnt].c_str(), SFM_READ, &sfinfo))) - { - sf_close (infile); - continue; - } - - maxSrcWidth = std::max(maxSrcWidth, static_cast(sfinfo.frames)); - maxChannels = std::max(maxChannels, static_cast(sfinfo.channels)); - - // Close input - sf_close (infile); - } + set_audio_max_dimensions(audioFilesPath, maxSrcWidth, maxSrcChannels); maxDstWidth = maxSrcWidth; // Set numDims, offset, n/c/h/w values for src/dst - srcDescPtr->numDims = 4; - srcDescPtr->offsetInBytes = 0; - srcDescPtr->n = batchSize; - srcDescPtr->h = maxSrcHeight; - srcDescPtr->w = maxSrcWidth; - srcDescPtr->c = maxChannels; - - dstDescPtr->numDims = 4; - dstDescPtr->offsetInBytes = 0; - dstDescPtr->n = batchSize; - dstDescPtr->h = maxDstHeight; - dstDescPtr->w = maxDstWidth; - if (testCase == 3) - dstDescPtr->c = 1; - else - dstDescPtr->c = maxChannels; - - // Optionally set w stride as a multiple of 8 for src/dst - srcDescPtr->w = ((srcDescPtr->w / 8) * 8) + 8; - dstDescPtr->w = ((dstDescPtr->w / 8) * 8) + 8; - - // Set n/c/h/w strides for src/dst - srcDescPtr->strides.nStride = srcDescPtr->c * srcDescPtr->w * srcDescPtr->h; - srcDescPtr->strides.hStride = srcDescPtr->c * srcDescPtr->w; - srcDescPtr->strides.wStride = srcDescPtr->c; - srcDescPtr->strides.cStride = 1; - - dstDescPtr->strides.nStride = dstDescPtr->c * dstDescPtr->w * dstDescPtr->h; - dstDescPtr->strides.hStride = dstDescPtr->c * dstDescPtr->w; - dstDescPtr->strides.wStride = dstDescPtr->c; - dstDescPtr->strides.cStride = 1; + Rpp32u offsetInBytes = 0; + set_audio_descriptor_dims_and_strides(srcDescPtr, batchSize, maxSrcHeight, maxSrcWidth, maxSrcChannels, offsetInBytes); + int maxDstChannels = maxSrcChannels; + if(testCase == 3) + maxDstChannels = 1; + set_audio_descriptor_dims_and_strides(dstDescPtr, batchSize, maxDstHeight, maxDstWidth, maxDstChannels, offsetInBytes); // Set buffer sizes for src/dst - iBufferSize = (unsigned long long)srcDescPtr->h * (unsigned long long)srcDescPtr->w * (unsigned long long)srcDescPtr->c * (unsigned long long)srcDescPtr->n; - oBufferSize = (unsigned long long)dstDescPtr->h * (unsigned long long)dstDescPtr->w * (unsigned long long)dstDescPtr->c * (unsigned long long)dstDescPtr->n; + iBufferSize = (Rpp64u)srcDescPtr->h * (Rpp64u)srcDescPtr->w * (Rpp64u)srcDescPtr->c * (Rpp64u)srcDescPtr->n; + oBufferSize = (Rpp64u)dstDescPtr->h * (Rpp64u)dstDescPtr->w * (Rpp64u)dstDescPtr->c * (Rpp64u)dstDescPtr->n; // Initialize host buffers for input & output Rpp32f *inputf32 = (Rpp32f *)calloc(iBufferSize, sizeof(Rpp32f)); @@ -174,42 +130,10 @@ int main(int argc, char **argv) { for (int iterCount = 0; iterCount < noOfIterations; iterCount++) { - for (int cnt = 0; cnt < batchSize; cnt++) - { - Rpp32f *inputTempF32; - inputTempF32 = inputf32 + (cnt * srcDescPtr->strides.nStride); - - SNDFILE *infile; - SF_INFO sfinfo; - int readcount; - - // The SF_INFO struct must be initialized before using it - memset (&sfinfo, 0, sizeof (sfinfo)); - if (!(infile = sf_open (audioFilePath[fileCnt].c_str(), SFM_READ, &sfinfo))) - { - sf_close (infile); - continue; - } + // Read and decode audio and fill the audio dim values + if (inputBitDepth == 2) + read_audio_batch_and_fill_dims(srcDescPtr, inputf32, audioFilesPath, iterCount, srcLengthTensor, channelsTensor); - srcLengthTensor[cnt] = sfinfo.frames; - channelsTensor[cnt] = sfinfo.channels; - srcDims[cnt].width = sfinfo.frames; - dstDims[cnt].width = sfinfo.frames; - srcDims[cnt].height = 1; - dstDims[cnt].height = 1; - - int bufferLength = sfinfo.frames * sfinfo.channels; - if (inputBitDepth == 2) - { - readcount = (int) sf_read_float (infile, inputTempF32, bufferLength); - if (readcount != bufferLength) - cout << "F32 Unable to read audio file completely " << std::endl; - } - fileCnt++; - - // Close input - sf_close (infile); - } clock_t startCpuTime, endCpuTime; double startWallTime, endWallTime; switch (testCase) @@ -231,6 +155,7 @@ int main(int argc, char **argv) else missingFuncFlag = 1; + // QA mode - verify outputs with golden outputs. Below code doesn’t run for performance tests if (testType == 0) verify_non_silent_region_detection(detectedIndex, detectionLength, testCaseName, batchSize, audioNames, dst); @@ -243,10 +168,12 @@ int main(int argc, char **argv) Rpp32f multiplier = std::log(10); Rpp32f referenceMagnitude = 1.0f; - for (i = 0; i < noOfAudioFiles; i++) + for (int i = 0; i < batchSize; i++) { srcDims[i].height = srcLengthTensor[i]; srcDims[i].width = 1; + dstDims[i].height = srcDims[i].height; + dstDims[i].width = 1; } startWallTime = omp_get_wtime(); @@ -267,29 +194,31 @@ int main(int argc, char **argv) endCpuTime = clock(); endWallTime = omp_get_wtime(); - cpuTime = ((double)(endCpuTime - startCpuTime)) / CLOCKS_PER_SEC; - wallTime = endWallTime - startWallTime; if (missingFuncFlag == 1) { printf("\nThe functionality %s doesn't yet exist in RPP\n", func.c_str()); return -1; } + + cpuTime = ((double)(endCpuTime - startCpuTime)) / CLOCKS_PER_SEC; + wallTime = endWallTime - startWallTime; maxWallTime = std::max(maxWallTime, wallTime); minWallTime = std::min(minWallTime, wallTime); avgWallTime += wallTime; - cpuTime *= 1000; - wallTime *= 1000; + // QA mode - verify outputs with golden outputs. Below code doesn’t run for performance tests if (testType == 0) { - if (batchSize == 8 && testCase != 0) + /* Run only if testCase is not 0 + For testCase 0 verify_non_silent_region_detection function is used for QA testing */ + if (testCase != 0) verify_output(outputf32, dstDescPtr, dstDims, testCaseName, audioNames, dst); - cout <<"\n\n"; - cout <<"CPU Backend Clock Time: "<< cpuTime <<" ms/batch"<< endl; - cout <<"CPU Backend Wall Time: "<< wallTime <<" ms/batch"<< endl; - - // If DEBUG_MODE is set to 1 dump the outputs to csv files for debugging + /* Dump the outputs to csv files for debugging + Runs only if + 1. DEBUG_MODE is enabled + 2. Current iteration is 1st iteration + 3. Test case is not 0 */ if (DEBUG_MODE && iterCount == 0 && testCase != 0) { std::ofstream refFile; @@ -300,12 +229,10 @@ int main(int argc, char **argv) } } } - - // Reset fileIndex to 0 for next run - fileCnt = 0; } rppDestroyHost(handle); + // performance test mode if (testType == 1) { // Display measured times diff --git a/utilities/test_suite/rpp_test_suite_audio.h b/utilities/test_suite/rpp_test_suite_audio.h index 401f14a80..aa03eb46b 100644 --- a/utilities/test_suite/rpp_test_suite_audio.h +++ b/utilities/test_suite/rpp_test_suite_audio.h @@ -40,6 +40,86 @@ std::map audioAugmentationMap = {1, "to_decibels"}, }; +// sets descriptor dimensions and strides of src/dst +inline void set_audio_descriptor_dims_and_strides(RpptDescPtr descPtr, int batchSize, int maxHeight, int maxWidth, int maxChannels, int offsetInBytes) +{ + descPtr->numDims = 4; + descPtr->offsetInBytes = offsetInBytes; + descPtr->n = batchSize; + descPtr->h = maxHeight; + descPtr->w = maxWidth; + descPtr->c = maxChannels; + + // Optionally set w stride as a multiple of 8 for src/dst + descPtr->w = ((descPtr->w / 8) * 8) + 8; + descPtr->strides.nStride = descPtr->c * descPtr->w * descPtr->h; + descPtr->strides.hStride = descPtr->c * descPtr->w; + descPtr->strides.wStride = descPtr->c; + descPtr->strides.cStride = 1; +} + +// sets values of maxHeight and maxWidth +inline void set_audio_max_dimensions(vector audioFilesPath, int& maxWidth, int& maxChannels) +{ + for (const std::string& audioPath : audioFilesPath) + { + SNDFILE *infile; + SF_INFO sfinfo; + int readcount; + + // The SF_INFO struct must be initialized before using it + memset (&sfinfo, 0, sizeof (sfinfo)); + if (!(infile = sf_open (audioPath.c_str(), SFM_READ, &sfinfo))) + { + sf_close (infile); + continue; + } + + maxWidth = std::max(maxWidth, static_cast(sfinfo.frames)); + maxChannels = std::max(maxChannels, static_cast(sfinfo.channels)); + + // Close input + sf_close (infile); + } +} + +void read_audio_batch_and_fill_dims(RpptDescPtr descPtr, Rpp32f *inputf32, vector audioFilesPath, int iterCount, Rpp32s *srcLengthTensor, Rpp32s *channelsTensor) +{ + auto fileIndex = iterCount * descPtr->n; + for (int i = 0, j = fileIndex; i < descPtr->n, j < fileIndex + descPtr->n; i++, j++) + { + Rpp32f *inputTempF32; + inputTempF32 = inputf32 + (i * descPtr->strides.nStride); + + // Read and decode data + SNDFILE *infile; + SF_INFO sfinfo; + int readcount; + + // The SF_INFO struct must be initialized before using it + memset (&sfinfo, 0, sizeof (sfinfo)); + if (!(infile = sf_open (audioFilesPath[j].c_str(), SFM_READ, &sfinfo))) + { + sf_close (infile); + continue; + } + + srcLengthTensor[i] = sfinfo.frames; + channelsTensor[i] = sfinfo.channels; + + int bufferLength = sfinfo.frames * sfinfo.channels; + readcount = (int) sf_read_float (infile, inputTempF32, bufferLength); + if (readcount != bufferLength) + { + std::cout << "Unable to read audio file: "< audioNames, string dst) { fstream refFile; @@ -64,6 +144,10 @@ void verify_output(Rpp32f *dstPtr, RpptDescPtr dstDescPtr, RpptImagePatchPtr dst Rpp32f refVal, outVal; Rpp32f *dstPtrCurrent = dstPtr + batchCount * dstDescPtr->strides.nStride; Rpp32f *dstPtrRow = dstPtrCurrent; + Rpp32u hStride = dstDescPtr->strides.hStride; + if (dstDims[batchCount].width == 1) + hStride = 1; + for (int i = 0; i < dstDims[batchCount].height; i++) { Rpp32f *dstPtrTemp = dstPtrRow; @@ -75,7 +159,7 @@ void verify_output(Rpp32f *dstPtr, RpptDescPtr dstDescPtr, RpptImagePatchPtr dst if (!invalidComparision && abs(outVal - refVal) < 1e-20) matchedIndices += 1; } - dstPtrRow += dstDescPtr->strides.hStride; + dstPtrRow += hStride; } refFile.close(); if (matchedIndices == (dstDims[batchCount].width * dstDims[batchCount].height) && matchedIndices !=0) From 527ed187d6488d3582baa496294e77ef3baa522b Mon Sep 17 00:00:00 2001 From: sampath1117 Date: Wed, 27 Sep 2023 05:11:50 -0700 Subject: [PATCH 7/7] minor change --- utilities/test_suite/HOST/Tensor_host_audio.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/utilities/test_suite/HOST/Tensor_host_audio.cpp b/utilities/test_suite/HOST/Tensor_host_audio.cpp index 06378c870..0a33016d1 100644 --- a/utilities/test_suite/HOST/Tensor_host_audio.cpp +++ b/utilities/test_suite/HOST/Tensor_host_audio.cpp @@ -41,6 +41,12 @@ int main(int argc, char **argv) int batchSize = atoi(argv[6]); char *dst = argv[7]; + if (testType == 0 && batchSize != 8) + { + cout << "Error! QA Mode only runs with batchsize 8" << endl; + return -1; + } + // Set case names string funcName = audioAugmentationMap[testCase]; if (funcName.empty()) @@ -170,10 +176,8 @@ int main(int argc, char **argv) for (int i = 0; i < batchSize; i++) { - srcDims[i].height = srcLengthTensor[i]; - srcDims[i].width = 1; - dstDims[i].height = srcDims[i].height; - dstDims[i].width = 1; + srcDims[i].height = dstDims[i].height = srcLengthTensor[i]; + srcDims[i].width = dstDims[i].width = 1; } startWallTime = omp_get_wtime();