Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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
46 changes: 40 additions & 6 deletions projects/miopen/src/kernels/MIOpenBatchNormFwdTrainSpatial.cl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ MIOpenBatchNormFwdTrainSpatial(const __global _FLOAT* __restrict in,
gcn_reduce2(&mean, &variance, (_FLOAT_ACCUM)INHW, lcl_data_x, lcl_data_y, lid);
#endif

variance = mad(-mean, mean, variance);
if(MIO_BN_NHW == 1)
{
variance = 0;
}
else
{
variance = mad(-mean, mean, variance);
}
if(variance < 0)
{
variance = 0;
Expand Down Expand Up @@ -353,7 +360,14 @@ MIOpenBatchNormFwdTrainSpatial(const __global _FLOAT* __restrict in,
#endif

// REDUCTION COMPLETE ---------------------------
variance = mad(-mean, mean, variance);
if(MIO_BN_NHW == 1)
{
variance = 0;
}
else
{
variance = mad(-mean, mean, variance);
}
if(variance < 0)
{
variance = 0;
Expand Down Expand Up @@ -629,7 +643,14 @@ MIOpenBatchNormFwdTrainSpatialFinalMeanVariance(
gcn_reduce2(&mean, &variance, INHW, lcl_data_x, lcl_data_y, ylid + zlid * ygrp_sz);
#endif

variance = mad(-mean, mean, variance);
if(MIO_BN_NHW == 1)
{
variance = 0;
}
else
{
variance = mad(-mean, mean, variance);
}
variance = max(variance, (_FLOAT_PREC_C)0.);
invVariance = rsqrt(variance + (_FLOAT_PREC_C)epsilon);

Expand Down Expand Up @@ -842,8 +863,14 @@ MIOpenBatchNormFwdTrainSpatial(const __global _FLOAT* __restrict in,
local _FLOAT_ACCUM lcl_data_y[MIO_BN_LDSGCN_SIZE];
gcn_reduce2(&mean, &variance, (_FLOAT_ACCUM)INHW, lcl_data_x, lcl_data_y, lid);
#endif

variance = mad(-mean, mean, variance);
if(MIO_BN_NHW == 1)
{
variance = 0;
}
else
{
variance = mad(-mean, mean, variance);
}
if(variance < 0)
{
variance = 0;
Expand Down Expand Up @@ -965,7 +992,14 @@ __kernel void MIOpenBatchNormFwdTrainSpatial(const __global _FLOAT* __restrict i
gcn_reduce2(&mean, &variance, (_FLOAT_ACCUM)INHW, lcl_data_x, lcl_data_y, lid);
#endif

variance = mad(-mean, mean, variance);
if(MIO_BN_NHW == 1)
{
variance = 0;
}
else
{
variance = mad(-mean, mean, variance);
}
variance = variance > 0. ? variance : 0.;
invVariance = rsqrt(variance + (_FLOAT_PREC)epsilon);
pvscale = lcl_scale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,14 @@ struct MIOpenBatchNormFwdTrainSpatialHIPImpl<1, FpType, FpPrecType, FpAccumType>
}

// REDUCTION COMPLETE ---------------------------
variance = fma(-mean, mean, variance);
if(mio_bn_config::nhw == 1)
Comment thread
EnricoDeg marked this conversation as resolved.
Outdated
{
variance = FpPrecType{0};
}
else
{
variance = fma(-mean, mean, variance);
}
if(variance < FpPrecType{0})
{
variance = FpPrecType{0};
Expand Down
9 changes: 4 additions & 5 deletions projects/miopen/src/kernels/batchnorm_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@
#if defined(__AMDGCN__) && \
!((defined(MIO_BN_GFX103X) && MIO_BN_GFX103X) || \
(defined(MIO_BN_GFX110X) && MIO_BN_GFX110X) || \
(defined(MIO_BN_GFX120X) && MIO_BN_GFX120X) || \
(defined(MIO_BN_GFX115X) && MIO_BN_GFX115X))
(defined(MIO_BN_GFX120X) && MIO_BN_GFX120X) || (defined(MIO_BN_GFX115X) && MIO_BN_GFX115X))
#define MIOPEN_USE_AMDGCN 1
#else
#define MIOPEN_USE_AMDGCN 0
Expand Down Expand Up @@ -559,7 +558,7 @@ static inline void running_stash(global _FLOAT_PREC_C* resultRunningMean,
mean, (_FLOAT_ACCUM)expAvgFactor, pvt_newRunMean); // newMean*factor + tmp
const _FLOAT_ACCUM_C adjust =
(_FLOAT_ACCUM_C)((MIO_BN_NHW == 1)
? variance
? (_FLOAT_ACCUM_C)0.0
: variance * ((_FLOAT_ACCUM)MIO_BN_NHW /
((_FLOAT_ACCUM)MIO_BN_NHW - (_FLOAT_ACCUM)1.0)));
resultRunningVariance[channel] =
Expand All @@ -585,7 +584,7 @@ static inline void running_stash_pa(global _FLOAT_PREC* resultRunningMean,
(_FLOAT_PREC)expAvgFactor,
pvt_newRunMean); // newMean*factor + tmp

const _FLOAT_PREC adjust = (MIO_BN_N == 1) ? variance : variance * (N / (N - 1.0));
const _FLOAT_PREC adjust = (MIO_BN_N == 1) ? (_FLOAT_PREC)0.0 : variance * (N / (N - 1.0));
resultRunningVariance[index] =
(1 - (_FLOAT_PREC)expAvgFactor) * *(resultRunningVariance + index) +
(_FLOAT_PREC)expAvgFactor * adjust;
Expand All @@ -607,7 +606,7 @@ static inline void running_stash_dyn(global _FLOAT_PREC* resultRunningMean,
resultRunningMean[channel] =
(_FLOAT_PREC)mad(mean, (_FLOAT_ACCUM)expAvgFactor, pvt_newRunMean); // newMean*factor + tmp
const _FLOAT_ACCUM adjust =
(_FLOAT_ACCUM)((inhw == 1) ? variance : variance * (1. / (1. - inhw)));
(_FLOAT_ACCUM)((inhw == 1) ? (_FLOAT_ACCUM)0.0 : variance * (1. / (1. - inhw)));
resultRunningVariance[channel] =
(_FLOAT_PREC)((1 - (_FLOAT_ACCUM)expAvgFactor) *
(_FLOAT_ACCUM)(*(resultRunningVariance + channel)) +
Expand Down
Loading