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
23 changes: 22 additions & 1 deletion keras/src/layers/normalization/group_normalization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import warnings

import ml_dtypes

from keras.src import backend
from keras.src import constraints
from keras.src import initializers
Expand Down Expand Up @@ -93,6 +97,22 @@ def __init__(
self.beta_constraint = constraints.get(beta_constraint)
self.gamma_constraint = constraints.get(gamma_constraint)

compute_dtype = backend.standardize_dtype(self.compute_dtype)
try:
finfo = ml_dtypes.finfo(compute_dtype)
if self.epsilon != 0 and self.epsilon < float(finfo.eps):
warnings.warn(
"The configured `epsilon` is smaller than what can be "
f"represented in the layer compute dtype "
f"({compute_dtype}); "
"it may be rounded to 0 under autocast. Consider "
"increasing `epsilon` or setting `autocast=False` "
"for this layer.",
stacklevel=2,
)
except Exception:
pass

def build(self, input_shape):
dim = input_shape[self.axis]

Expand Down Expand Up @@ -151,7 +171,8 @@ def call(self, inputs):
normalized_inputs = self._apply_normalization(
reshaped_inputs, inputs.shape
)
return ops.reshape(normalized_inputs, ops.shape(inputs))
outputs = ops.reshape(normalized_inputs, ops.shape(inputs))
return ops.cast(outputs, self.compute_dtype)

def _reshape_into_groups(self, inputs):
input_shape = ops.shape(inputs)
Expand Down
26 changes: 26 additions & 0 deletions keras/src/layers/normalization/group_normalization_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import warnings

import numpy as np
import pytest

from keras.src import constraints
from keras.src import layers
Expand Down Expand Up @@ -175,3 +178,26 @@ def test_broadcasting_2d_channels_first(self):
),
atol=1e-3,
)

def test_warns_when_epsilon_too_small_for_compute_dtype(self):
with pytest.warns(UserWarning, match="epsilon"):
layers.GroupNormalization(
groups=2,
axis=-1,
scale=False,
center=False,
epsilon=1e-4,
dtype="mixed_bfloat16",
)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
layers.GroupNormalization(
groups=2,
axis=-1,
scale=False,
center=False,
epsilon=1e-3,
dtype="mixed_bfloat16",
)
self.assertFalse(any("epsilon" in str(x.message) for x in w))
Comment on lines +193 to +203
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What's the difference between this test and the previous one line 212?

Loading