Skip to content

Commit 516bce1

Browse files
authored
API Generation (#1608)
* API Gen Code * Generated API * Generate API * Update API Gen by removing the init string * Exclude Sentence piece tokenizer for Keras 2 * Exclude docstring test from Keras 2
1 parent a5da750 commit 516bce1

File tree

403 files changed

+2446
-1774
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

403 files changed

+2446
-1774
lines changed

.github/workflows/actions.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ jobs:
104104
pip install --no-deps -e "." --progress-bar off
105105
- name: Lint
106106
run: bash shell/lint.sh
107+
- name: Check for API changes
108+
run: |
109+
bash shell/api_gen.sh
110+
git status
111+
clean=$(git status | grep "nothing to commit")
112+
if [ -z "$clean" ]; then
113+
echo "Please run shell/api_gen.sh to generate API."
114+
exit 1
115+
fi

CONTRIBUTING.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ has cuda support.
117117
conda create -y -n keras-nlp-cpu python=3.10
118118
conda activate keras-nlp-cpu
119119
pip install -r requirements.txt # install deps
120-
python pip_build.py --install # install keras-nlp
120+
pip install -e . # install keras-nlp
121121

122122
for backend in "jax" "torch" "tensorflow"; do
123123
conda create -y -n keras-nlp-${backend} python=3.10
124124
conda activate keras-nlp-${backend}
125125
pip install -r requirements-${backend}-cuda.txt # install deps
126-
python pip_build.py --install # install keras-nlp
126+
pip install -e . # install keras-nlp
127127
done
128128
```
129129

@@ -158,6 +158,15 @@ This is automatically done if you clone using git inside WSL.
158158
Note that will not support Windows Shell/PowerShell for any scripts in this
159159
repository.
160160

161+
## Update Public API
162+
163+
Run API generation script when creating PRs that update `keras_nlp_export`
164+
public APIs. Add the files changed in `keras_nlp/api` to the same PR.
165+
166+
```
167+
./shell/api_gen.sh
168+
```
169+
161170
## Testing changes
162171

163172
KerasNLP is tested using [PyTest](https://docs.pytest.org/en/6.2.x/).

api_gen.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright 2023 The KerasNLP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Script to generate keras_nlp public API in `keras_nlp/api` directory.
15+
16+
Usage:
17+
18+
Run via `./shell/api_gen.sh`.
19+
It generates API and formats user and generated APIs.
20+
"""
21+
22+
import os
23+
import shutil
24+
25+
import namex
26+
27+
package = "keras_nlp"
28+
29+
30+
def ignore_files(_, filenames):
31+
return [f for f in filenames if f.endswith("_test.py")]
32+
33+
34+
def copy_source_to_build_directory(root_path):
35+
# Copy sources (`keras_nlp/` directory and setup files) to build dir
36+
build_dir = os.path.join(root_path, "tmp_build_dir")
37+
if os.path.exists(build_dir):
38+
shutil.rmtree(build_dir)
39+
os.mkdir(build_dir)
40+
shutil.copytree(
41+
package, os.path.join(build_dir, package), ignore=ignore_files
42+
)
43+
return build_dir
44+
45+
46+
def export_version_string(api_init_fname):
47+
with open(api_init_fname) as f:
48+
contents = f.read()
49+
with open(api_init_fname, "w") as f:
50+
contents += "from keras_nlp.src.version_utils import __version__\n"
51+
f.write(contents)
52+
53+
54+
def build():
55+
# Backup the `keras_nlp/__init__.py` and restore it on error in api gen.
56+
root_path = os.path.dirname(os.path.abspath(__file__))
57+
code_api_dir = os.path.join(root_path, package, "api")
58+
# Create temp build dir
59+
build_dir = copy_source_to_build_directory(root_path)
60+
build_api_dir = os.path.join(build_dir, package, "api")
61+
build_init_fname = os.path.join(build_dir, package, "__init__.py")
62+
build_api_init_fname = os.path.join(build_api_dir, "__init__.py")
63+
try:
64+
os.chdir(build_dir)
65+
# Generates `keras_nlp/api` directory.
66+
if os.path.exists(build_api_dir):
67+
shutil.rmtree(build_api_dir)
68+
if os.path.exists(build_init_fname):
69+
os.remove(build_init_fname)
70+
os.makedirs(build_api_dir)
71+
namex.generate_api_files(
72+
"keras_nlp", code_directory="src", target_directory="api"
73+
)
74+
# Add __version__ to keras package
75+
export_version_string(build_api_init_fname)
76+
# Copy back the keras_nlp/api and keras_nlp/__init__.py from build dir
77+
if os.path.exists(code_api_dir):
78+
shutil.rmtree(code_api_dir)
79+
shutil.copytree(build_api_dir, code_api_dir)
80+
finally:
81+
# Clean up: remove the build directory (no longer needed)
82+
shutil.rmtree(build_dir)
83+
84+
85+
if __name__ == "__main__":
86+
build()

keras_nlp/conftest.py renamed to conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"The TensorFlow package is required for data preprocessing with any backend."
2525
)
2626

27-
from keras_nlp.backend import config as backend_config
28-
from keras_nlp.backend import keras
27+
from keras_nlp.src.backend import config as backend_config
28+
from keras_nlp.src.backend import keras
2929

3030

3131
def pytest_addoption(parser):

keras_nlp/__init__.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 The KerasNLP Authors
1+
# Copyright 2023 The KerasNLP Authors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -11,21 +11,36 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""DO NOT EDIT.
1415
15-
# sentencepiece is segfaulting on tf-nightly if tensorflow is imported first.
16-
# This is a temporary fix to restore our nightly testing to green, while we look
17-
# for a longer term solution.
18-
try:
19-
import sentencepiece
20-
except ImportError:
21-
pass
22-
23-
from keras_nlp import layers
24-
from keras_nlp import metrics
25-
from keras_nlp import models
26-
from keras_nlp import samplers
27-
from keras_nlp import tokenizers
28-
from keras_nlp import utils
29-
from keras_nlp.utils.preset_utils import upload_preset
30-
from keras_nlp.version_utils import __version__
31-
from keras_nlp.version_utils import version
16+
This file was autogenerated. Do not edit it by hand,
17+
since your modifications would be overwritten.
18+
"""
19+
20+
import os
21+
22+
# Import everything from /api/ into keras.
23+
from keras_nlp.api import * # noqa: F403
24+
from keras_nlp.api import __version__ # Import * ignores names start with "_".
25+
26+
# Add everything in /api/ to the module search path.
27+
__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405
28+
29+
# Don't pollute namespace.
30+
del os
31+
32+
33+
# Never autocomplete `.src` or `.api` on an imported keras object.
34+
def __dir__():
35+
keys = dict.fromkeys((globals().keys()))
36+
keys.pop("src")
37+
keys.pop("api")
38+
return list(keys)
39+
40+
41+
# Don't import `.src` or `.api` during `from keras import *`.
42+
__all__ = [
43+
name
44+
for name in globals().keys()
45+
if not (name.startswith("_") or name in ("src", "api"))
46+
]

keras_nlp/api/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2023 The KerasNLP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""DO NOT EDIT.
15+
16+
This file was autogenerated. Do not edit it by hand,
17+
since your modifications would be overwritten.
18+
"""
19+
20+
from keras_nlp.api import layers
21+
from keras_nlp.api import metrics
22+
from keras_nlp.api import models
23+
from keras_nlp.api import samplers
24+
from keras_nlp.api import tokenizers
25+
from keras_nlp.src.utils.preset_utils import upload_preset
26+
from keras_nlp.src.version_utils import __version__
27+
from keras_nlp.src.version_utils import version

keras_nlp/api/layers/__init__.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2023 The KerasNLP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""DO NOT EDIT.
15+
16+
This file was autogenerated. Do not edit it by hand,
17+
since your modifications would be overwritten.
18+
"""
19+
20+
from keras_nlp.src.layers.modeling.alibi_bias import AlibiBias
21+
from keras_nlp.src.layers.modeling.cached_multi_head_attention import (
22+
CachedMultiHeadAttention,
23+
)
24+
from keras_nlp.src.layers.modeling.f_net_encoder import FNetEncoder
25+
from keras_nlp.src.layers.modeling.masked_lm_head import MaskedLMHead
26+
from keras_nlp.src.layers.modeling.position_embedding import PositionEmbedding
27+
from keras_nlp.src.layers.modeling.reversible_embedding import (
28+
ReversibleEmbedding,
29+
)
30+
from keras_nlp.src.layers.modeling.rotary_embedding import RotaryEmbedding
31+
from keras_nlp.src.layers.modeling.sine_position_encoding import (
32+
SinePositionEncoding,
33+
)
34+
from keras_nlp.src.layers.modeling.token_and_position_embedding import (
35+
TokenAndPositionEmbedding,
36+
)
37+
from keras_nlp.src.layers.modeling.transformer_decoder import TransformerDecoder
38+
from keras_nlp.src.layers.modeling.transformer_encoder import TransformerEncoder
39+
from keras_nlp.src.layers.preprocessing.masked_lm_mask_generator import (
40+
MaskedLMMaskGenerator,
41+
)
42+
from keras_nlp.src.layers.preprocessing.multi_segment_packer import (
43+
MultiSegmentPacker,
44+
)
45+
from keras_nlp.src.layers.preprocessing.random_deletion import RandomDeletion
46+
from keras_nlp.src.layers.preprocessing.random_swap import RandomSwap
47+
from keras_nlp.src.layers.preprocessing.start_end_packer import StartEndPacker

keras_nlp/api/metrics/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2023 The KerasNLP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""DO NOT EDIT.
15+
16+
This file was autogenerated. Do not edit it by hand,
17+
since your modifications would be overwritten.
18+
"""
19+
20+
from keras_nlp.src.metrics.bleu import Bleu
21+
from keras_nlp.src.metrics.edit_distance import EditDistance
22+
from keras_nlp.src.metrics.perplexity import Perplexity
23+
from keras_nlp.src.metrics.rouge_l import RougeL
24+
from keras_nlp.src.metrics.rouge_n import RougeN

0 commit comments

Comments
 (0)