Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR2 - Audio - To decibels HOST Tensor #150

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/rppt_tensor_audio_augmentations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
114 changes: 114 additions & 0 deletions src/modules/cpu/kernel/to_decibels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user gives multiplier as 0, is there a check?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure added this check

if(minRatio == 0.0f)
minRatio = std::nextafter(0.0f, 1.0f);

const Rpp32f log10Factor = 0.3010299956639812; //1 / std::log(10);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize as #define in rpp_cpu_common

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initializing that as #define even at the start of this file lead to output mismatches, thus left it as such

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snehaa8 Then instead of #define, lets just move the const Rpp32f as-is to rpp_cpu_common

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));
srcPtrCurrent++;
dstPtrCurrent++;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use post increment ++ at L84, and make loop single line removing braces

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
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));
srcPtrTemp++;
dstPtrTemp++;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here on increments

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

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

return RPP_SUCCESS;
}
32 changes: 32 additions & 0 deletions src/modules/rppt_tensor_audio_augmentations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rpp32f*>(srcPtr),
srcDescPtr,
static_cast<Rpp32f*>(dstPtr),
dstDescPtr,
srcDims,
cutOffDB,
multiplier,
referenceMagnitude,
rpp::deref(rppHandle));

return RPP_SUCCESS;
}
else
{
return RPP_ERROR_NOT_IMPLEMENTED;
}
}
77 changes: 77 additions & 0 deletions utilities/test_suite/HOST/audio/Tensor_host_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,55 @@ void verify_non_silent_region_detection(float *detectedIndex, float *detectionLe
std::cerr<<"FAILED! "<<file_match<<"/"<<bs<<" outputs are matching with reference outputs"<<std::endl;
}

void verify_output(Rpp32f *dstPtr, RpptDescPtr dstDescPtr, RpptImagePatchPtr dstDims, string test_case, char audioNames[][1000])
{
fstream ref_file;
string ref_path = get_current_dir_name();
string pattern = "HOST/audio/build";
remove_substring(ref_path, pattern);
ref_path = ref_path + "REFERENCE_OUTPUTS_AUDIO/";
int file_match = 0;
for (int batchcount = 0; batchcount < dstDescPtr->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"<<endl;
break;
}
int matched_indices = 0;
Rpp32f ref_val, out_val;
Rpp32f *dstPtrCurrent = dstPtr + batchcount * dstDescPtr->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<<std::endl<<"Results for Test case: "<<test_case<<std::endl;
if(file_match == dstDescPtr->n)
std::cerr<<"PASSED!"<<std::endl;
else
std::cerr<<"FAILED! "<<file_match<<"/"<<dstDescPtr->n<<" outputs are matching with reference outputs"<<std::endl;
}

int main(int argc, char **argv)
{
// Handle inputs
Expand All @@ -114,6 +163,9 @@ int main(int argc, char **argv)
case 0:
strcpy(funcName, "non_silent_region_detection");
break;
case 1:
strcpy(funcName, "to_decibels");
break;
default:
strcpy(funcName, "testCase");
break;
Expand Down Expand Up @@ -336,6 +388,31 @@ int main(int argc, char **argv)
verify_non_silent_region_detection(detectedIndex, detectionLength, testCaseName, noOfAudioFiles, audioNames);
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;

verify_output(outputf32, dstDescPtr, dstDims, testCaseName, audioNames);
break;
}
default:
{
missingFuncFlag = 1;
Expand Down
Loading