Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mixed precision check in TestCase.run_layer_test: compare with output_spec dtype instead of hardcoded float16 #19297

Merged
merged 7 commits into from
Mar 14, 2024
21 changes: 18 additions & 3 deletions keras/testing/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,29 @@ def data_generator():

if run_mixed_precision_check:
layer = layer_cls(**{**init_kwargs, "dtype": "mixed_float16"})
input_spec = tree.map_structure(
lambda spec: KerasTensor(
spec.shape,
dtype=(
layer.compute_dtype
if layer.autocast
and backend.is_float_dtype(spec.dtype)
else spec.dtype
),
),
keras_tensor_inputs,
)
if isinstance(input_data, dict):
output_data = layer(**input_data, **call_kwargs)
output_spec = layer.compute_output_spec(**input_spec)
else:
output_data = layer(input_data, **call_kwargs)
for tensor in tree.flatten(output_data):
output_spec = layer.compute_output_spec(input_spec)
for tensor, spec in zip(
tree.flatten(output_data), tree.flatten(output_spec)
):
dtype = standardize_dtype(tensor.dtype)
if is_float_dtype(dtype):
self.assertEqual(dtype, "float16")
self.assertEqual(dtype, spec.dtype)
for weight in layer.weights:
dtype = standardize_dtype(weight.dtype)
if is_float_dtype(dtype):
Expand Down