Skip to content

Commit

Permalink
Merge branch 'keras-team:master' into falcon-causallm
Browse files Browse the repository at this point in the history
  • Loading branch information
SamanehSaadat authored May 20, 2024
2 parents e1169b5 + 294304b commit 91b4ac2
Show file tree
Hide file tree
Showing 445 changed files with 7,721 additions and 2,248 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,12 @@ jobs:
pip install --no-deps -e "." --progress-bar off
- name: Lint
run: bash shell/lint.sh
- name: Check for API changes
run: |
bash shell/api_gen.sh
git status
clean=$(git status | grep "nothing to commit")
if [ -z "$clean" ]; then
echo "Please run shell/api_gen.sh to generate API."
exit 1
fi
1 change: 1 addition & 0 deletions .kokoro/github/ubuntu/gpu/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ then
fi

pip install --no-deps -e "." --progress-bar off
pip install huggingface_hub

# Run Extra Large Tests for Continuous builds
if [ "${RUN_XLARGE:-0}" == "1" ]
Expand Down
13 changes: 11 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ has cuda support.
conda create -y -n keras-nlp-cpu python=3.10
conda activate keras-nlp-cpu
pip install -r requirements.txt # install deps
python pip_build.py --install # install keras-nlp
pip install -e . # install keras-nlp

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

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

## Update Public API

Run API generation script when creating PRs that update `keras_nlp_export`
public APIs. Add the files changed in `keras_nlp/api` to the same PR.

```
./shell/api_gen.sh
```

## Testing changes

KerasNLP is tested using [PyTest](https://docs.pytest.org/en/6.2.x/).
Expand Down
115 changes: 53 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# KerasNLP: Modular NLP Workflows for Keras
# KerasNLP: Multi-framework NLP Models
[![](https://github.com/keras-team/keras-nlp/workflows/Tests/badge.svg?branch=master)](https://github.com/keras-team/keras-nlp/actions?query=workflow%3ATests+branch%3Amaster)
![Python](https://img.shields.io/badge/python-v3.9.0+-success.svg)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/keras-team/keras-nlp/issues)

KerasNLP is a natural language processing library that works natively
with TensorFlow, JAX, or PyTorch. Built on Keras 3, these models, layers,
metrics, and tokenizers can be trained and serialized in any framework and
re-used in another without costly migrations.

KerasNLP supports users through their entire development cycle. Our workflows
are built from modular components that have state-of-the-art preset weights when
used out-of-the-box and are easily customizable when more control is needed.
with TensorFlow, JAX, or PyTorch. KerasNLP provides a repository of pre-trained
models and a collection of lower-level building blocks for language modeling.
Built on Keras 3, models can be trained and serialized in any framework
and re-used in another without costly migrations.

This library is an extension of the core Keras API; all high-level modules are
[`Layers`](https://keras.io/api/layers/) or
[`Models`](https://keras.io/api/models/) that receive that same level of polish
as core Keras. If you are familiar with Keras, congratulations! You already
understand most of KerasNLP.

See our [Getting Started guide](https://keras.io/guides/keras_nlp/getting_started)
to start learning our API. We welcome [contributions](CONTRIBUTING.md).
Layers and Models that receive that same level of polish as core Keras.
If you are familiar with Keras, congratulations! You already understand most of
KerasNLP.

All models support JAX, TensorFlow, and PyTorch from a single model
definition and can be fine-tuned on GPUs and TPUs out of the box. Models can
be trained on individual accelerators with built-in PEFT techniques, or
fine-tuned at scale with model and data parallel training. See our
[Getting Started guide](https://keras.io/guides/keras_nlp/getting_started)
to start learning our API. Browse our models on
[Kaggle](https://www.kaggle.com/organizations/keras/models).
We welcome contributions.

## Quick Links

Expand All @@ -28,7 +30,7 @@ to start learning our API. We welcome [contributions](CONTRIBUTING.md).
- [Home Page](https://keras.io/keras_nlp)
- [Developer Guides](https://keras.io/guides/keras_nlp)
- [API Reference](https://keras.io/api/keras_nlp)
- [Getting Started guide](https://keras.io/guides/keras_nlp/getting_started)
- [Pre-trained Models](https://www.kaggle.com/organizations/keras/models)

### For contributors

Expand All @@ -38,53 +40,13 @@ to start learning our API. We welcome [contributions](CONTRIBUTING.md).
- [API Design Guide](API_DESIGN_GUIDE.md)
- [Call for Contributions](https://github.com/keras-team/keras-nlp/issues?q=is%3Aissue+is%3Aopen+label%3A%22contributions+welcome%22)

## Installation

KerasNLP supports both Keras 2 and Keras 3. We recommend Keras 3 for all new
users, as it enables using KerasNLP models and layers with JAX, TensorFlow and
PyTorch.

### Keras 2 Installation

To install the latest KerasNLP release with Keras 2, simply run:

```
pip install --upgrade keras-nlp
```

### Keras 3 Installation

There are currently two ways to install Keras 3 with KerasNLP. To install the
stable versions of KerasNLP and Keras 3, you should install Keras 3 **after**
installing KerasNLP. This is a temporary step while TensorFlow is pinned to
Keras 2, and will no longer be necessary after TensorFlow 2.16.

```
pip install --upgrade keras-nlp
pip install --upgrade keras>=3
```

To install the latest nightly changes for both KerasNLP and Keras, you can use
our nightly package.

```
pip install --upgrade keras-nlp-nightly
```

> [!IMPORTANT]
> Keras 3 will not function with TensorFlow 2.14 or earlier.
Read [Getting started with Keras](https://keras.io/getting_started/) for more information
on installing Keras 3 and compatibility with different frameworks.

## Quickstart

Fine-tune BERT on a small sentiment analysis task using the
[`keras_nlp.models`](https://keras.io/api/keras_nlp/models/) API:
Fine-tune BERT on IMDb movie reviews:

```python
import os
os.environ["KERAS_BACKEND"] = "tensorflow" # Or "jax" or "torch"!
os.environ["KERAS_BACKEND"] = "jax" # Or "tensorflow" or "torch"!

import keras_nlp
import tensorflow_datasets as tfds
Expand All @@ -96,8 +58,8 @@ imdb_train, imdb_test = tfds.load(
batch_size=16,
)
# Load a BERT model.
classifier = keras_nlp.models.BertClassifier.from_preset(
"bert_base_en_uncased",
classifier = keras_nlp.models.Classifier.from_preset(
"bert_base_en",
num_classes=2,
activation="softmax",
)
Expand All @@ -107,7 +69,35 @@ classifier.fit(imdb_train, validation_data=imdb_test)
classifier.predict(["What an amazing movie!", "A total waste of my time."])
```

For more in depth guides and examples, visit https://keras.io/keras_nlp/.
Try it out [in a colab](https://colab.research.google.com/gist/mattdangerw/e457e42d5ea827110c8d5cb4eb9d9a07/kerasnlp-quickstart.ipynb).
For more in depth guides and examples, visit
[keras.io/keras_nlp](https://keras.io/keras_nlp/).

## Installation

To install the latest KerasNLP release with Keras 3, simply run:

```
pip install --upgrade keras-nlp
```

To install the latest nightly changes for both KerasNLP and Keras, you can use
our nightly package.

```
pip install --upgrade keras-nlp-nightly
```

Note that currently, installing KerasNLP will always pull in TensorFlow for use
of the `tf.data` API for preprocessing. Even when pre-processing with `tf.data`,
training can still happen on any backend.

Read [Getting started with Keras](https://keras.io/getting_started/) for more
information on installing Keras 3 and compatibility with different frameworks.

> [!IMPORTANT]
> We recommend using KerasNLP with TensorFlow 2.16 or later, as TF 2.16 packages
> Keras 3 by default.
## Configuring your backend

Expand Down Expand Up @@ -145,7 +135,8 @@ KerasNLP provides access to pre-trained models via the `keras_nlp.models` API.
These pre-trained models are provided on an "as is" basis, without warranties
or conditions of any kind. The following underlying models are provided by third
parties, and subject to separate licenses:
BART, DeBERTa, DistilBERT, GPT-2, OPT, RoBERTa, Whisper, and XLM-RoBERTa.
BART, BLOOM, DeBERTa, DistilBERT, GPT-2, Llama, Mistral, OPT, RoBERTa, Whisper,
and XLM-RoBERTa.

## Citing KerasNLP

Expand Down
86 changes: 86 additions & 0 deletions api_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2023 The KerasNLP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to generate keras_nlp public API in `keras_nlp/api` directory.
Usage:
Run via `./shell/api_gen.sh`.
It generates API and formats user and generated APIs.
"""

import os
import shutil

import namex

package = "keras_nlp"


def ignore_files(_, filenames):
return [f for f in filenames if f.endswith("_test.py")]


def copy_source_to_build_directory(root_path):
# Copy sources (`keras_nlp/` directory and setup files) to build dir
build_dir = os.path.join(root_path, "tmp_build_dir")
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
os.mkdir(build_dir)
shutil.copytree(
package, os.path.join(build_dir, package), ignore=ignore_files
)
return build_dir


def export_version_string(api_init_fname):
with open(api_init_fname) as f:
contents = f.read()
with open(api_init_fname, "w") as f:
contents += "from keras_nlp.src.version_utils import __version__\n"
f.write(contents)


def build():
# Backup the `keras_nlp/__init__.py` and restore it on error in api gen.
root_path = os.path.dirname(os.path.abspath(__file__))
code_api_dir = os.path.join(root_path, package, "api")
# Create temp build dir
build_dir = copy_source_to_build_directory(root_path)
build_api_dir = os.path.join(build_dir, package, "api")
build_init_fname = os.path.join(build_dir, package, "__init__.py")
build_api_init_fname = os.path.join(build_api_dir, "__init__.py")
try:
os.chdir(build_dir)
# Generates `keras_nlp/api` directory.
if os.path.exists(build_api_dir):
shutil.rmtree(build_api_dir)
if os.path.exists(build_init_fname):
os.remove(build_init_fname)
os.makedirs(build_api_dir)
namex.generate_api_files(
"keras_nlp", code_directory="src", target_directory="api"
)
# Add __version__ to keras package
export_version_string(build_api_init_fname)
# Copy back the keras_nlp/api and keras_nlp/__init__.py from build dir
if os.path.exists(code_api_dir):
shutil.rmtree(code_api_dir)
shutil.copytree(build_api_dir, code_api_dir)
finally:
# Clean up: remove the build directory (no longer needed)
shutil.rmtree(build_dir)


if __name__ == "__main__":
build()
13 changes: 10 additions & 3 deletions keras_nlp/conftest.py → conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
import os

import pytest
import tensorflow as tf

from keras_nlp.backend import config as backend_config
from keras_nlp.backend import keras
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use `keras_nlp`, please install Tensorflow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)

from keras_nlp.src.backend import config as backend_config
from keras_nlp.src.backend import keras


def pytest_addoption(parser):
Expand Down
8 changes: 7 additions & 1 deletion examples/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@

import os

import tensorflow as tf
try:
import tensorflow as tf
except ImportError:
raise ImportError(
"To use `keras_nlp`, please install Tensorflow: `pip install tensorflow`. "
"The TensorFlow package is required for data preprocessing with any backend."
)
from google import protobuf


Expand Down
Loading

0 comments on commit 91b4ac2

Please sign in to comment.