Skip to content
Open
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
1 change: 1 addition & 0 deletions dnn-providers/hip-kernel-provider/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(
${KERNEL_GENERATED_SOURCES}
engines/plans/BatchnormPlanBuilder.cpp
engines/plans/BatchnormFwdInferencePlan.cpp
engines/plans/BatchnormFwdInferenceWithVariancePlan.cpp
engines/plans/BatchnormApplicabilityChecks.cpp
)
target_compile_definitions(
Expand Down
71 changes: 71 additions & 0 deletions dnn-providers/hip-kernel-provider/src/HipKernelUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,77 @@
namespace hip_kernel_provider::hip_kernel_utils
{

ActivationParams parseActivation(const hipdnn_data_sdk::data_objects::PointwiseAttributes& attrs)
Copy link
Contributor

Choose a reason for hiding this comment

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

missing tests on this

{
using PM = hipdnn_data_sdk::data_objects::PointwiseMode;

switch(attrs.operation())
{
case PM::RELU_FWD:
case PM::RELU_BWD:
{
if(attrs.relu_lower_clip() && attrs.relu_upper_clip())
{
return ActivationParams{ActivationMode::CLAMP,
static_cast<double>(*attrs.relu_lower_clip()),
static_cast<double>(*attrs.relu_upper_clip()),
0.0};
}
if(attrs.relu_upper_clip())
{
return ActivationParams{ActivationMode::CLIPPED_RELU,
static_cast<double>(*attrs.relu_upper_clip()),
0.0,
0.0};
}
if(attrs.relu_lower_clip_slope())
{
return ActivationParams{ActivationMode::LEAKY_RELU,
static_cast<double>(*attrs.relu_lower_clip_slope()),
0.0,
0.0};
}
if(attrs.relu_lower_clip().has_value() && attrs.relu_lower_clip().value() != 0.f)
{
throw hipdnn_plugin_sdk::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"Standard relu with a non-zero lower_clip is not supported");
}
return ActivationParams{ActivationMode::RELU, 0.0, 0.0, 0.0};
}
case PM::SIGMOID_FWD:
case PM::SIGMOID_BWD:
return ActivationParams{ActivationMode::LOGISTIC, 0.0, 0.0, 0.0};
case PM::TANH_FWD:
case PM::TANH_BWD:
return ActivationParams{ActivationMode::TANH, 1.0, 1.0, 0.0};
case PM::ELU_FWD:
case PM::ELU_BWD:
{
double alpha = attrs.elu_alpha() ? static_cast<double>(*attrs.elu_alpha()) : 1.0;
return ActivationParams{ActivationMode::ELU, alpha, 0.0, 0.0};
}
case PM::SOFTPLUS_FWD:
case PM::SOFTPLUS_BWD:
if(attrs.softplus_beta())
{
if(static_cast<double>(*attrs.softplus_beta()) != 1.0)
{
throw hipdnn_plugin_sdk::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"Softplus only supports beta = 1.0");
}
}
return ActivationParams{ActivationMode::SOFTRELU, 0.0, 0.0, 0.0};
case PM::ABS:
return ActivationParams{ActivationMode::ABS, 0.0, 0.0, 0.0};
case PM::IDENTITY:
return ActivationParams{ActivationMode::PASTHRU, 0.0, 0.0, 0.0};
default:
throw hipdnn_plugin_sdk::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"Unsupported activation operation");
}
}

hipdnnPluginDeviceBuffer_t findDeviceBuffer(int64_t uid,
const hipdnnPluginDeviceBuffer_t* deviceBuffers,
uint32_t numDeviceBuffers)
Expand Down
26 changes: 26 additions & 0 deletions dnn-providers/hip-kernel-provider/src/HipKernelUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,38 @@

#include <unordered_map>

#include <hipdnn_data_sdk/data_objects/pointwise_attributes_generated.h>
#include <hipdnn_data_sdk/data_objects/tensor_attributes_generated.h>
#include <hipdnn_plugin_sdk/PluginApiDataTypes.h>

namespace hip_kernel_provider::hip_kernel_utils
{

enum class ActivationMode : int
{
PASTHRU = 0,
LOGISTIC = 1, // sigmoid
TANH = 2,
RELU = 3,
SOFTRELU = 4, // softplus
ABS = 5,
POWER = 6,
CLIPPED_RELU = 7,
LEAKY_RELU = 8,
ELU = 9,
CLAMP = 10
};

struct ActivationParams
{
ActivationMode mode;
double alpha;
double beta;
double gamma;
};

ActivationParams parseActivation(const hipdnn_data_sdk::data_objects::PointwiseAttributes& attrs);

hipdnnPluginDeviceBuffer_t findDeviceBuffer(int64_t uid,
const hipdnnPluginDeviceBuffer_t* deviceBuffers,
uint32_t numDeviceBuffers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,105 @@ void checkBatchnormInferenceTensorConfigSupported(
ioTensorIds, affineTensorIds, statTensorIds, {}, tensorMap, false);
}

void checkBatchnormInferenceVarianceExtTensorConfigSupported(
const hipdnn_data_sdk::data_objects::BatchnormInferenceAttributesVarianceExt& bnInfAttr,
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap)
{
std::vector<int64_t> ioTensorIds = {bnInfAttr.x_tensor_uid(), bnInfAttr.y_tensor_uid()};
std::vector<int64_t> affineTensorIds
= {bnInfAttr.scale_tensor_uid(), bnInfAttr.bias_tensor_uid()};
std::vector<int64_t> statTensorIds
= {bnInfAttr.mean_tensor_uid(), bnInfAttr.variance_tensor_uid()};

checkBatchnormTensorConfigSupported(
ioTensorIds, affineTensorIds, statTensorIds, {}, tensorMap, false);
}

void checkBatchnormInferenceActivationTensorConfigSupported(
const hipdnn_data_sdk::data_objects::BatchnormInferenceAttributes& bnInfAttr,
const hipdnn_data_sdk::data_objects::PointwiseAttributes& actAttr,
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap)
{
checkBatchnormFwdActivationModeSupported(actAttr);

std::vector<int64_t> ioTensorIds = {bnInfAttr.x_tensor_uid(), actAttr.out_0_tensor_uid()};
std::vector<int64_t> affineTensorIds
= {bnInfAttr.scale_tensor_uid(), bnInfAttr.bias_tensor_uid()};
std::vector<int64_t> statTensorIds
= {bnInfAttr.mean_tensor_uid(), bnInfAttr.inv_variance_tensor_uid()};
std::vector<int64_t> intermediateTensorIds
= {bnInfAttr.y_tensor_uid(), actAttr.in_0_tensor_uid()};

checkBatchnormTensorConfigSupported(
ioTensorIds, affineTensorIds, statTensorIds, intermediateTensorIds, tensorMap, false);
}

void checkBatchnormInferenceVarianceExtActivationTensorConfigSupported(
const hipdnn_data_sdk::data_objects::BatchnormInferenceAttributesVarianceExt& bnInfAttr,
const hipdnn_data_sdk::data_objects::PointwiseAttributes& actAttr,
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap)
{
checkBatchnormFwdActivationModeSupported(actAttr);

std::vector<int64_t> ioTensorIds = {bnInfAttr.x_tensor_uid(), actAttr.out_0_tensor_uid()};
std::vector<int64_t> affineTensorIds
= {bnInfAttr.scale_tensor_uid(), bnInfAttr.bias_tensor_uid()};
std::vector<int64_t> statTensorIds
= {bnInfAttr.mean_tensor_uid(), bnInfAttr.variance_tensor_uid()};
std::vector<int64_t> intermediateTensorIds
= {bnInfAttr.y_tensor_uid(), actAttr.in_0_tensor_uid()};

checkBatchnormTensorConfigSupported(
ioTensorIds, affineTensorIds, statTensorIds, intermediateTensorIds, tensorMap, false);
}

// --- Activation Mode Validators ---

namespace
{

void checkBatchnormActivationModeSupported(
const hipdnn_data_sdk::data_objects::PointwiseAttributes& activAttr, bool isBwd)
{
// hip-kernel-provider batchnorm supports: PASSTHRU, RELU, CLIPPEDREU, CLAMP (no Leaky ReLU)

if(activAttr.operation() == hipdnn_data_sdk::data_objects::PointwiseMode::IDENTITY)
{
return;
}

if(activAttr.operation()
== (isBwd ? hipdnn_data_sdk::data_objects::PointwiseMode::RELU_BWD
: hipdnn_data_sdk::data_objects::PointwiseMode::RELU_FWD))
{
if(!activAttr.relu_lower_clip_slope())
{
return;
}
throw hipdnn_plugin_sdk::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"Batchnorm fused activation does not support Leaky ReLU.");
}

throw hipdnn_plugin_sdk::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM, "Unsupported activation mode for batchnorm fusion.");
}

} // namespace

void checkBatchnormFwdActivationModeSupported(
const hipdnn_data_sdk::data_objects::PointwiseAttributes& activAttr)
{
checkBatchnormActivationModeSupported(activAttr, false);
}

void checkBatchnormBwdActivationModeSupported(
const hipdnn_data_sdk::data_objects::PointwiseAttributes& activAttr)
{
checkBatchnormActivationModeSupported(activAttr, true);
}

} // namespace hip_kernel_provider
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <vector>

#include <hipdnn_data_sdk/data_objects/batchnorm_inference_attributes_generated.h>
#include <hipdnn_data_sdk/data_objects/batchnorm_inference_attributes_variance_ext_generated.h>
#include <hipdnn_data_sdk/data_objects/pointwise_attributes_generated.h>
#include <hipdnn_data_sdk/data_objects/tensor_attributes_generated.h>

namespace hip_kernel_provider
Expand Down Expand Up @@ -105,6 +107,29 @@ void checkBatchnormInferenceTensorConfigSupported(
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap);

void checkBatchnormInferenceVarianceExtTensorConfigSupported(
const hipdnn_data_sdk::data_objects::BatchnormInferenceAttributesVarianceExt& bnInfAttr,
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap);

void checkBatchnormInferenceActivationTensorConfigSupported(
const hipdnn_data_sdk::data_objects::BatchnormInferenceAttributes& bnInfAttr,
const hipdnn_data_sdk::data_objects::PointwiseAttributes& actAttr,
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap);

void checkBatchnormInferenceVarianceExtActivationTensorConfigSupported(
const hipdnn_data_sdk::data_objects::BatchnormInferenceAttributesVarianceExt& bnInfAttr,
const hipdnn_data_sdk::data_objects::PointwiseAttributes& actAttr,
const std::unordered_map<int64_t, const hipdnn_data_sdk::data_objects::TensorAttributes*>&
tensorMap);

void checkBatchnormFwdActivationModeSupported(
const hipdnn_data_sdk::data_objects::PointwiseAttributes& activAttr);

void checkBatchnormBwdActivationModeSupported(
const hipdnn_data_sdk::data_objects::PointwiseAttributes& activAttr);

// --- Batchnorm Type Configuration ---

// hip-kernel-provider batchnorm requirements (based on underlying kernel constraints):
Expand Down
Loading
Loading