Skip to content

Commit

Permalink
Remove invalid tf1 export decorators from the Keras codebase, and fix…
Browse files Browse the repository at this point in the history
… bug related to symbol deduping in pip_build.py. Also add sanity check to prevent future similar issues.

PiperOrigin-RevId: 519843284
  • Loading branch information
fchollet authored and tensorflower-gardener committed Mar 27, 2023
1 parent 1a8c6c1 commit 86e8daa
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 44 deletions.
4 changes: 0 additions & 4 deletions keras/legacy_tf_layers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@
# isort: off
from tensorflow.python.ops import variable_scope as vs
from tensorflow.python.util.tf_export import keras_export
from tensorflow.python.util.tf_export import tf_export

_KERAS_STYLE_SCOPE = False


@keras_export(
v1=["keras.__internal__.legacy.layers.experimental.keras_style_scope"]
)
@tf_export(v1=["layers.experimental.keras_style_scope"])
@tf_contextlib.contextmanager
def keras_style_scope():
"""Use Keras-style variable management.
Expand Down Expand Up @@ -113,7 +111,6 @@ def call(self, input, state):
@keras_export(
v1=["keras.__internal__.legacy.layers.experimental.set_keras_style"]
)
@tf_export(v1=["layers.experimental.set_keras_style"])
def set_keras_style():
"""Use Keras-style variable management.
Expand Down Expand Up @@ -157,7 +154,6 @@ def _is_in_keras_style_scope():


@keras_export(v1=["keras.__internal__.legacy.layers.Layer"])
@tf_export(v1=["layers.Layer"])
class Layer(base_layer.Layer):
"""Base layer class.
Expand Down
15 changes: 0 additions & 15 deletions keras/legacy_tf_layers/convolutional.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@

# isort: off
from tensorflow.python.util.tf_export import keras_export
from tensorflow.python.util.tf_export import tf_export


@keras_export(v1=["keras.__internal__.legacy.layers.Conv1D"])
@tf_export(v1=["layers.Conv1D"])
class Conv1D(keras_layers.Conv1D, base.Layer):
"""1D convolution layer (e.g. temporal convolution).
Expand Down Expand Up @@ -158,7 +156,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.conv1d"])
@tf_export(v1=["layers.conv1d"])
def conv1d(
inputs,
filters,
Expand Down Expand Up @@ -306,7 +303,6 @@ def conv1d(


@keras_export(v1=["keras.__internal__.legacy.layers.Conv2D"])
@tf_export(v1=["layers.Conv2D"])
class Conv2D(keras_layers.Conv2D, base.Layer):
"""2D convolution layer (e.g. spatial convolution over images).
Expand Down Expand Up @@ -441,7 +437,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.conv2d"])
@tf_export(v1=["layers.conv2d"])
def conv2d(
inputs,
filters,
Expand Down Expand Up @@ -596,7 +591,6 @@ def conv2d(


@keras_export(v1=["keras.__internal__.legacy.layers.Conv3D"])
@tf_export(v1=["layers.Conv3D"])
class Conv3D(keras_layers.Conv3D, base.Layer):
"""3D convolution layer (e.g. spatial convolution over volumes).
Expand Down Expand Up @@ -732,7 +726,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.conv3d"])
@tf_export(v1=["layers.conv3d"])
def conv3d(
inputs,
filters,
Expand Down Expand Up @@ -888,7 +881,6 @@ def conv3d(


@keras_export(v1=["keras.__internal__.legacy.layers.SeparableConv1D"])
@tf_export(v1=["layers.SeparableConv1D"])
class SeparableConv1D(keras_layers.SeparableConv1D, base.Layer):
"""Depthwise separable 1D convolution.
Expand Down Expand Up @@ -1037,7 +1029,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.SeparableConv2D"])
@tf_export(v1=["layers.SeparableConv2D"])
class SeparableConv2D(keras_layers.SeparableConv2D, base.Layer):
"""Depthwise separable 2D convolution.
Expand Down Expand Up @@ -1190,7 +1181,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.separable_conv1d"])
@tf_export(v1=["layers.separable_conv1d"])
def separable_conv1d(
inputs,
filters,
Expand Down Expand Up @@ -1358,7 +1348,6 @@ def separable_conv1d(


@keras_export(v1=["keras.__internal__.legacy.layers.separable_conv2d"])
@tf_export(v1=["layers.separable_conv2d"])
def separable_conv2d(
inputs,
filters,
Expand Down Expand Up @@ -1530,7 +1519,6 @@ def separable_conv2d(


@keras_export(v1=["keras.__internal__.legacy.layers.Conv2DTranspose"])
@tf_export(v1=["layers.Conv2DTranspose"])
class Conv2DTranspose(keras_layers.Conv2DTranspose, base.Layer):
"""Transposed 2D convolution layer (sometimes called 2D Deconvolution).
Expand Down Expand Up @@ -1654,7 +1642,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.conv2d_transpose"])
@tf_export(v1=["layers.conv2d_transpose"])
def conv2d_transpose(
inputs,
filters,
Expand Down Expand Up @@ -1798,7 +1785,6 @@ def conv2d_transpose(


@keras_export(v1=["keras.__internal__.legacy.layers.Conv3DTranspose"])
@tf_export(v1=["layers.Conv3DTranspose"])
class Conv3DTranspose(keras_layers.Conv3DTranspose, base.Layer):
"""Transposed 3D convolution layer (sometimes called 3D Deconvolution).
Expand Down Expand Up @@ -1918,7 +1904,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.conv3d_transpose"])
@tf_export(v1=["layers.conv3d_transpose"])
def conv3d_transpose(
inputs,
filters,
Expand Down
7 changes: 0 additions & 7 deletions keras/legacy_tf_layers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@

# isort: off
from tensorflow.python.util.tf_export import keras_export
from tensorflow.python.util.tf_export import tf_export


@keras_export(v1=["keras.__internal__.legacy.layers.Dense"])
@tf_export(v1=["layers.Dense"])
class Dense(keras_layers.Dense, base.Layer):
"""Densely-connected layer class.
Expand Down Expand Up @@ -153,7 +151,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.dense"])
@tf_export(v1=["layers.dense"])
def dense(
inputs,
units,
Expand Down Expand Up @@ -275,7 +272,6 @@ def dense(


@keras_export(v1=["keras.__internal__.legacy.layers.Dropout"])
@tf_export(v1=["layers.Dropout"])
class Dropout(keras_layers.Dropout, base.Layer):
"""Applies Dropout to the input.
Expand Down Expand Up @@ -348,7 +344,6 @@ def call(self, inputs, training=False):


@keras_export(v1=["keras.__internal__.legacy.layers.dropout"])
@tf_export(v1=["layers.dropout"])
def dropout(
inputs, rate=0.5, noise_shape=None, seed=None, training=False, name=None
):
Expand Down Expand Up @@ -428,7 +423,6 @@ def dropout(


@keras_export(v1=["keras.__internal__.legacy.layers.Flatten"])
@tf_export(v1=["layers.Flatten"])
class Flatten(keras_layers.Flatten, base.Layer):
"""Flattens an input tensor while preserving the batch axis (axis 0).
Expand Down Expand Up @@ -485,7 +479,6 @@ class Flatten(keras_layers.Flatten, base.Layer):


@keras_export(v1=["keras.__internal__.legacy.layers.flatten"])
@tf_export(v1=["layers.flatten"])
def flatten(inputs, name=None, data_format="channels_last"):
"""Flattens an input tensor while preserving the batch axis (axis 0).
Expand Down
13 changes: 0 additions & 13 deletions keras/legacy_tf_layers/pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@

# isort: off
from tensorflow.python.util.tf_export import keras_export
from tensorflow.python.util.tf_export import tf_export


@keras_export(v1=["keras.__internal__.legacy.layers.AveragePooling1D"])
@tf_export(v1=["layers.AveragePooling1D"])
class AveragePooling1D(keras_layers.AveragePooling1D, base.Layer):
"""Average Pooling layer for 1D inputs.
Expand Down Expand Up @@ -101,7 +99,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.average_pooling1d"])
@tf_export(v1=["layers.average_pooling1d"])
def average_pooling1d(
inputs,
pool_size,
Expand Down Expand Up @@ -186,7 +183,6 @@ def average_pooling1d(


@keras_export(v1=["keras.__internal__.legacy.layers.MaxPooling1D"])
@tf_export(v1=["layers.MaxPooling1D"])
class MaxPooling1D(keras_layers.MaxPooling1D, base.Layer):
"""Max Pooling layer for 1D inputs.
Expand Down Expand Up @@ -258,7 +254,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.max_pooling1d"])
@tf_export(v1=["layers.max_pooling1d"])
def max_pooling1d(
inputs,
pool_size,
Expand Down Expand Up @@ -343,7 +338,6 @@ def max_pooling1d(


@keras_export(v1=["keras.__internal__.legacy.layers.AveragePooling2D"])
@tf_export(v1=["layers.AveragePooling2D"])
class AveragePooling2D(keras_layers.AveragePooling2D, base.Layer):
"""Average pooling layer for 2D inputs (e.g. images).
Expand Down Expand Up @@ -419,7 +413,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.average_pooling2d"])
@tf_export(v1=["layers.average_pooling2d"])
def average_pooling2d(
inputs,
pool_size,
Expand Down Expand Up @@ -508,7 +501,6 @@ def average_pooling2d(


@keras_export(v1=["keras.__internal__.legacy.layers.MaxPooling2D"])
@tf_export(v1=["layers.MaxPooling2D"])
class MaxPooling2D(keras_layers.MaxPooling2D, base.Layer):
"""Max pooling layer for 2D inputs (e.g. images).
Expand Down Expand Up @@ -584,7 +576,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.max_pooling2d"])
@tf_export(v1=["layers.max_pooling2d"])
def max_pooling2d(
inputs,
pool_size,
Expand Down Expand Up @@ -673,7 +664,6 @@ def max_pooling2d(


@keras_export(v1=["keras.__internal__.legacy.layers.AveragePooling3D"])
@tf_export(v1=["layers.AveragePooling3D"])
class AveragePooling3D(keras_layers.AveragePooling3D, base.Layer):
"""Average pooling layer for 3D inputs (e.g. volumes).
Expand Down Expand Up @@ -751,7 +741,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.average_pooling3d"])
@tf_export(v1=["layers.average_pooling3d"])
def average_pooling3d(
inputs,
pool_size,
Expand Down Expand Up @@ -842,7 +831,6 @@ def average_pooling3d(


@keras_export(v1=["keras.__internal__.legacy.layers.MaxPooling3D"])
@tf_export(v1=["layers.MaxPooling3D"])
class MaxPooling3D(keras_layers.MaxPooling3D, base.Layer):
"""Max pooling layer for 3D inputs (e.g. volumes).
Expand Down Expand Up @@ -920,7 +908,6 @@ def __init__(


@keras_export(v1=["keras.__internal__.legacy.layers.max_pooling3d"])
@tf_export(v1=["layers.max_pooling3d"])
def max_pooling3d(
inputs,
pool_size,
Expand Down
20 changes: 15 additions & 5 deletions pip_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,26 @@ def generate_keras_api_files(package_directory, src_directory):
f"[!] Could not inspect symbol '{name}' from {module}."
)
continue
# If the symbol is a subclass of a non-registered symbol, skip it.
# If the symbol is a non-registered subclass of
# a registered symbol, skip it.
skip = False

def has_same_metadata(a, b):
if (
hasattr(a, "_keras_api_names")
and hasattr(b, "_keras_api_names")
and a._keras_api_names == b._keras_api_names
and a._keras_api_names_v1 == b._keras_api_names_v1
):
return True
return False

try:
classes = inspect.getmro(symbol)
if len(classes) >= 2:
parents = classes[1:]
for p in parents:
if (
hasattr(p, "_keras_api_names")
and p._keras_api_names == symbol._keras_api_names
):
if has_same_metadata(p, symbol):
skip = True
except AttributeError:
# getmro will error out on a non-class
Expand Down Expand Up @@ -424,6 +433,7 @@ def test_wheel(wheel_path, expected_version, requirements_path):
f"pip3 install -r {requirements_path}\n"
f"pip3 install {wheel_path} --force-reinstall\n"
f"python3 -c 'import keras;{checks};print(keras.__version__)'\n"
f"python3 -c 'import tensorflow as tf;tf.compat.v1.layers.Dense'\n"
)
try:
# Check version is correct
Expand Down

0 comments on commit 86e8daa

Please sign in to comment.