@@ -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
0 commit comments