Skip to content

Commit

Permalink
Merge pull request #150 from snehaa8/sn/to_decibels
Browse files Browse the repository at this point in the history
PR2 - Audio - To decibels HOST Tensor
  • Loading branch information
r-abishek committed Nov 14, 2023
2 parents 6b2add5 + 7753fda commit 072cc1e
Show file tree
Hide file tree
Showing 15 changed files with 273,104 additions and 14 deletions.
19 changes: 19 additions & 0 deletions include/rppt_tensor_audio_augmentations.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ 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);

/*! \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 <tt> RppStatus enum</tt>.
* \returns RPP_SUCCESS <tt>\ref RppStatus</tt> 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);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions src/modules/cpu/host_tensor_audio_augmentations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
106 changes: 106 additions & 0 deletions src/modules/cpu/kernel/to_decibels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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 <omp.h>

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();

// 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(!referenceMagnitude)
{
refMag = -std::numeric_limits<Rpp32f>::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)
refMag = 1.0f;

Rpp32f invReferenceMagnitude = 1.f / refMag;
// Interpret as 1D array
if(width == 1)
{
for(Rpp32s vectorLoopCount = 0; vectorLoopCount < height; vectorLoopCount++)
*dstPtrCurrent++ = multiplier * std::log2(std::max(minRatio, (*srcPtrCurrent++) * invReferenceMagnitude));
}
else
{
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));

srcPtrRow += srcDescPtr->strides.hStride;
dstPtrRow += dstDescPtr->strides.hStride;
}
}
}

return RPP_SUCCESS;
}
34 changes: 34 additions & 0 deletions src/modules/rppt_tensor_audio_augmentations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,37 @@ 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 (multiplier == 0)
return RPP_ERROR_ZERO_DIVISION;
if ((srcDescPtr->dataType == RpptDataType::F32) && (dstDescPtr->dataType == RpptDataType::F32))
{
to_decibels_host_tensor(static_cast<Rpp32f*>(srcPtr),
srcDescPtr,
static_cast<Rpp32f*>(dstPtr),
dstDescPtr,
srcDims,
cutOffDB,
multiplier,
referenceMagnitude,
rpp::deref(rppHandle));

return RPP_SUCCESS;
}
else
{
return RPP_ERROR_NOT_IMPLEMENTED;
}
}
45 changes: 45 additions & 0 deletions utilities/test_suite/HOST/Tensor_host_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,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 (int i = 0; i < batchSize; i++)
{
srcDims[i].height = dstDims[i].height = srcLengthTensor[i];
srcDims[i].width = dstDims[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;
Expand All @@ -173,6 +195,29 @@ int main(int argc, char **argv)
maxWallTime = std::max(maxWallTime, wallTime);
minWallTime = std::min(minWallTime, wallTime);
avgWallTime += wallTime;

// QA mode - verify outputs with golden outputs. Below code doesn’t run for performance tests
if (testType == 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);

/* 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;
refFile.open(func + ".csv");
for (int i = 0; i < oBufferSize; i++)
refFile << *(outputf32 + i) << "\n";
refFile.close();
}
}
}
}
rppDestroyHost(handle);
Expand Down
26 changes: 13 additions & 13 deletions utilities/test_suite/HOST/runAudioTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,30 @@ def run_performance_test(loggingFolder, srcPath, case, numRuns, testType, batchS
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('--qa_mode', type = int, default = 0, help = "Run with qa_mode? Output audio data from tests will be compared with golden outputs - (0 / 1)", required = False)
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()

# check if the folder exists
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!")
exit(0)
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.qa_mode < 0 or args.qa_mode > 1:
Expand All @@ -166,8 +166,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

Expand Down Expand Up @@ -226,21 +226,21 @@ def rpp_test_suite_parser_and_validator():
exit(0)

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_unit_test(srcPath, case, numRuns, testType, 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, batchSize, outFilePath)

# print the results of qa tests
supportedCaseList = ['0']
supportedCaseList = ['0', '1']
nonQACaseList = [] # Add cases present in supportedCaseList, but without QA support

if testType == 0:
Expand Down
Loading

0 comments on commit 072cc1e

Please sign in to comment.