Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Batchnorm training mode support in a minimal build
  • Loading branch information
baijumeswani committed Aug 10, 2023
commit 20ff81f00ba0e5f100e50d0225f73e346ea32eed
14 changes: 5 additions & 9 deletions onnxruntime/core/providers/cpu/nn/batch_norm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@

namespace onnxruntime {

#if !defined(ORT_MINIMAL_BUILD)
#define BATCHNORM_INCLUDE_TRAINING_SUPPORT
#endif

template <typename T>
class BatchNorm : public OpKernel {
public:
Expand All @@ -51,7 +47,7 @@ class BatchNorm : public OpKernel {
}

if (is_train_) {
#if defined(BATCHNORM_INCLUDE_TRAINING_SUPPORT)
#ifdef ENABLE_TRAINING_CORE
momentum_ = op_kernel_info.GetAttrOrDefault<float>("momentum", 0.9f);
ORT_ENFORCE(is_spatial_, "Training mode only supports spatial BN");
#else
Expand Down Expand Up @@ -88,7 +84,7 @@ class BatchNorm : public OpKernel {
// calculate sample_size (including all channels)
size_t sample_size_incl_all_channels = sample_size * C;

#if defined(BATCHNORM_INCLUDE_TRAINING_SUPPORT)
#ifdef ENABLE_TRAINING_CORE
AllocatorPtr alloc;
ORT_RETURN_IF_ERROR(p_op_kernel_context->GetTempSpaceAllocator(&alloc));

Expand All @@ -115,7 +111,7 @@ class BatchNorm : public OpKernel {
ConstEigenVectorArrayMap<T> scale_arr(scale->Data<T>(), is_spatial_ ? C : sample_size_incl_all_channels);
ConstEigenVectorArrayMap<T> bias_arr(B->Data<T>(), is_spatial_ ? C : sample_size_incl_all_channels);

#if defined(BATCHNORM_INCLUDE_TRAINING_SUPPORT)
#ifdef ENABLE_TRAINING_CORE
// Note that we only support spatial BN for training
if (is_train_) {
EigenVectorArrayMap<T> saved_mean_arr(saved_mean->MutableData<T>(), C);
Expand Down Expand Up @@ -166,7 +162,7 @@ class BatchNorm : public OpKernel {
ConstEigenVectorArrayMap<T> var_arr(var->Data<T>(), is_spatial_ ? C : sample_size_incl_all_channels);
inv_std = (var_arr + epsilon_).sqrt().inverse();
} else {
#if defined(BATCHNORM_INCLUDE_TRAINING_SUPPORT)
#ifdef ENABLE_TRAINING_CORE
EigenVectorArrayMap<T> saved_inv_std_arr(saved_inv_std->MutableData<T>(), C);
saved_inv_std_arr = (saved_inv_std_arr + epsilon_).inverse().sqrt();
inv_std = saved_inv_std_arr;
Expand All @@ -175,7 +171,7 @@ class BatchNorm : public OpKernel {

// If we're training, do batch normalization based on computation from this batch
ConstEigenVectorArrayMap<T> mean_arr(
#if defined(BATCHNORM_INCLUDE_TRAINING_SUPPORT)
#ifdef ENABLE_TRAINING_CORE
!is_train_ ? mean->Data<T>() : saved_mean->Data<T>(),
#else
mean->Data<T>(),
Expand Down
5 changes: 2 additions & 3 deletions onnxruntime/test/providers/cpu/nn/batch_norm_op_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

#include "core/framework/tensor.h"
#include "core/providers/cpu/nn/batch_norm.h" // for BATCHNORM_INCLUDE_TRAINING_SUPPORT
#include "core/session/inference_session.h"
#include "test/common/dnnl_op_test_utils.h"
#include "test/providers/provider_test_utils.h"
Expand Down Expand Up @@ -847,7 +846,7 @@ TEST(BatchNormTest, BatchNorm2d_bfloat16) {
#endif // USE_DNNL

// TODO fix flaky test for CUDA
#ifdef BATCHNORM_INCLUDE_TRAINING_SUPPORT
#ifdef ENABLE_TRAINING_CORE
TEST(BatchNormTest, ForwardTrainingTestWithSavedOutputsOpset9) {
// TODO: Unskip when fixed #41968513
if (DefaultDmlExecutionProvider().get() != nullptr) {
Expand Down Expand Up @@ -937,7 +936,7 @@ TEST(BatchNormTest, ForwardTrainingTestOpset15) {
{kCudaExecutionProvider, kRocmExecutionProvider,
kTensorrtExecutionProvider, kOpenVINOExecutionProvider, kDnnlExecutionProvider});
}
#endif // BATCHNORM_INCLUDE_TRAINING_SUPPORT
#endif // ENABLE_TRAINING_CORE

} // namespace test
} // namespace onnxruntime