Skip to content

Commit ff582bc

Browse files
Merge pull request #48115 from kaixih:fused_batch_norm_5d
PiperOrigin-RevId: 367065849 Change-Id: I0c8fe8e3fd799f6132d76aea49a4b82218720874
2 parents 0f920ac + cd9d1b5 commit ff582bc

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

‎tensorflow/python/keras/layers/normalization.py‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,25 +235,27 @@ def _raise_if_fused_cannot_be_used(self):
235235
"""Raises a ValueError if fused implementation cannot be used.
236236
237237
In addition to the checks done in this function, the input tensors rank must
238-
be 4. The input rank check can only be done once the input shape is known.
238+
be 4 or 5. The input rank check can only be done once the input shape is
239+
known.
239240
"""
240241
# Note the ValueErrors in this function are caught and not reraised in
241242
# _fused_can_be_used(). No other exception besides ValueError should be
242243
# raised here.
243244

244245
# Currently fused batch norm doesn't support renorm. It also only supports a
245-
# channel dimension on axis 1 or 3, when no virtual batch size or adjustment
246-
# is used.
246+
# channel dimension on axis 1 or 3 (rank=4) / 1 or 4 (rank5), when no
247+
# virtual batch size or adjustment is used.
247248
if self.renorm:
248249
raise ValueError('Passing both `fused=True` and `renorm=True` is '
249250
'not supported')
250251
axis = [self.axis] if isinstance(self.axis, int) else self.axis
251-
# Axis -3 is equivalent to 1, and axis -1 is equivalent to 3, because the
252-
# input rank is required to be 4 (which is checked later).
253-
# TODO(b/173253101): Once the input rank can be 5, update this check.
254-
if len(axis) > 1 or axis[0] not in (-3, -1, 1, 3):
252+
# Axis -3 is equivalent to 1, and axis -1 is equivalent to 3, when the
253+
# input rank is 4. Similarly, the valid axis is -4, -1, 1, 4 when the rank
254+
# is 5. The combination of ranks and axes will be checked later.
255+
if len(axis) > 1 or axis[0] not in (-4, -3, -1, 1, 3, 4):
255256
raise ValueError('Passing `fused=True` is only supported when axis is 1 '
256-
'or 3. Got axis %s' % (axis,))
257+
'or 3 for input rank = 4 or 1 or 4 for input rank = 5. '
258+
'Got axis %s' % (axis,))
257259
if self.virtual_batch_size is not None:
258260
raise ValueError('Passing `fused=True` is not supported when '
259261
'`virtual_batch_size` is specified.')
@@ -334,19 +336,16 @@ def build(self, input_shape):
334336
# TODO(yaozhang): if input is not 4D, reshape it to 4D and reshape the
335337
# output back to its original shape accordingly.
336338
if self._USE_V2_BEHAVIOR:
337-
# TODO(b/173253101): Using fused in the 5D case is currently disabled
338-
# due to a regression on UNet, so it is only currently only supported in
339-
# the 4D case.
340339
if self.fused is None:
341-
self.fused = ndims == 4
342-
elif self.fused and ndims != 4:
340+
self.fused = ndims in (4, 5)
341+
elif self.fused and ndims not in (4, 5):
343342
raise ValueError('Batch normalization layers with `fused=True` only '
344343
'support 4D or 5D input tensors. '
345344
'Received tensor with shape: %s' %
346345
(tuple(input_shape),))
347346
else:
348347
assert self.fused is not None
349-
self.fused = (ndims == 4 and self._fused_can_be_used())
348+
self.fused = (ndims in (4, 5) and self._fused_can_be_used())
350349
# TODO(chrisying): fused batch norm is currently not supported for
351350
# multi-axis batch norm and by extension virtual batches. In some cases,
352351
# it might be possible to use fused batch norm but would require reshaping

‎tensorflow/python/keras/layers/normalization_test.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ def test_bessels_correction(self):
255255
layer = normalization_v2.BatchNormalization(
256256
momentum=0.5, moving_variance_initializer='zeros')
257257
layer(x, training=True)
258-
self.assertFalse(layer.fused)
259-
# Since fused is not used, Bessel's correction is not used. The variance of
260-
# [0, 2] is 1 without Bessel's correction. Since the momentum is 0.5, the
261-
# variance is 1 * 0.5 == 0.5.
262-
self.assertAllEqual(self.evaluate(layer.moving_variance), [0.5])
258+
self.assertTrue(layer.fused)
259+
# Since fused is used, Bessel's correction is used. The variance of [0, 2]
260+
# is 2 with Bessel's correction. Since the momentum is 0.5, the variance is
261+
# 2 * 0.5 == 1.
262+
self.assertAllEqual(self.evaluate(layer.moving_variance), [1.])
263263

264264

265265
class BatchNormalizationV1Test(keras_parameterized.TestCase):
@@ -315,7 +315,7 @@ def test_v2_fused_attribute(self):
315315
self.assertIsNone(norm.fused)
316316
inp = keras.layers.Input(shape=(4, 4, 4, 4))
317317
norm(inp)
318-
self.assertEqual(norm.fused, False)
318+
self.assertEqual(norm.fused, True)
319319

320320
norm = normalization_v2.BatchNormalization(virtual_batch_size=2)
321321
self.assertEqual(norm.fused, False)

0 commit comments

Comments
 (0)