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

[Enhancement] Add float datatype support to rocAucScore #3074

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ namespace data_management
{
namespace internal
{
DAAL_EXPORT double rocAucScore(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction);
// Default template to double for backwards compatability in daal4py (<2025.5)
template <typename DataType = double>
DAAL_EXPORT DataType rocAucScore(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction);
} // namespace internal
} // namespace data_management
} // namespace daal
Expand Down
35 changes: 20 additions & 15 deletions cpp/daal/src/data_management/roc_auc_score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ using namespace daal::internal;
using namespace daal::services;
using namespace daal::services::internal;

template <daal::CpuType cpu>
services::Status rocAucScoreImpl(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction, double & score)
template <typename DataType, daal::CpuType cpu>
services::Status rocAucScoreImpl(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction, DataType & score)
{
services::Status s;
SafeStatus safeStat;
const size_t nElements = truePrediction->getNumberOfRows();
TArrayScalable<IdxValType<double>, cpu> predict(nElements);
TArrayScalable<IdxValType<DataType>, cpu> predict(nElements);
DAAL_CHECK_MALLOC(predict.get());

const size_t blockSizeDefault = 256;
const size_t nBlocks = nElements / blockSizeDefault + !!(nElements % blockSizeDefault);

ReadColumns<double, cpu> testPredictionBlock(testPrediction.get(), 0, 0, nElements);
ReadColumns<DataType, cpu> testPredictionBlock(testPrediction.get(), 0, 0, nElements);
DAAL_CHECK_BLOCK_STATUS(testPredictionBlock);
const double * const testPredictionPtr = testPredictionBlock.get();
const DataType * const testPredictionPtr = testPredictionBlock.get();

daal::threader_for(nBlocks, nBlocks, [&](const size_t iBlock) {
const size_t blockBegin = iBlock * blockSizeDefault;
Expand All @@ -63,12 +63,12 @@ services::Status rocAucScoreImpl(const NumericTablePtr & truePrediction, const N
}
});

daal::parallel_sort<double>(predict.get(), predict.get() + nElements);
daal::parallel_sort<DataType>(predict.get(), predict.get() + nElements);

size_t rank = 1;
size_t elementsInBlock = 1;
size_t i = 0;
TArray<double, cpu> predictedRank(nElements);
TArray<DataType, cpu> predictedRank(nElements);
DAAL_CHECK_MALLOC(predictedRank.get());
while (i < nElements)
{
Expand All @@ -84,14 +84,14 @@ services::Status rocAucScoreImpl(const NumericTablePtr & truePrediction, const N
for (size_t j = 0; j < elementsInBlock; ++j)
{
const size_t idx = predict[i + j].index;
predictedRank[idx] = static_cast<double>(rank) + ((static_cast<double>(elementsInBlock) - double(1.0)) * double(0.5));
predictedRank[idx] = static_cast<DataType>(rank) + ((static_cast<DataType>(elementsInBlock) - DataType(1.0)) * DataType(0.5));
}
rank += elementsInBlock;
i += elementsInBlock;
}

double nPos = double(0);
double filteredRankSum = double(0);
DataType nPos = DataType(0);
DataType filteredRankSum = DataType(0);
for (size_t iBlock = 0; iBlock < nBlocks; ++iBlock)
{
const size_t blockBegin = iBlock * blockSizeDefault;
Expand All @@ -108,21 +108,26 @@ services::Status rocAucScoreImpl(const NumericTablePtr & truePrediction, const N
}
}
}
const double nNeg = static_cast<double>(nElements) - nPos;
score = (filteredRankSum - (nPos * (nPos + double(1.0)) * double(0.5))) / (nPos * nNeg);
const DataType nNeg = static_cast<DataType>(nElements) - nPos;
score = (filteredRankSum - (nPos * (nPos + DataType(1.0)) * DataType(0.5))) / (nPos * nNeg);
return s;
}

DAAL_EXPORT double rocAucScore(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction)
template <typename DataType>
DAAL_EXPORT DataType rocAucScore(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction)
{
double score = double(0);
#define DAAL_ROC_AUC_SCORE(cpuId, ...) rocAucScoreImpl<cpuId>(__VA_ARGS__);
DataType score = DataType(0);
#define DAAL_ROC_AUC_SCORE(cpuId, ...) rocAucScoreImpl<DataType, cpuId>(__VA_ARGS__);

DAAL_DISPATCH_FUNCTION_BY_CPU_SAFE(DAAL_ROC_AUC_SCORE, truePrediction, testPrediction, score);

#undef DAAL_ROC_AUC_SCORE
return score;
}

template DAAL_EXPORT float rocAucScore<float>(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction);
template DAAL_EXPORT double rocAucScore<double>(const NumericTablePtr & truePrediction, const NumericTablePtr & testPrediction);

} // namespace internal
} // namespace data_management
} // namespace daal
Loading