Skip to content

Commit

Permalink
Merge branch 'master' into gh-action-dep-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
sampathweb authored Mar 14, 2023
2 parents 1ff4a33 + 4c94a0d commit a9e7aea
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ jobs:
- name: Test with pytest
run: |
pytest --cov=keras_nlp --cov-report xml:coverage.xml
- name: Run integration tests
run: |
python pip_build.py --install && cd integration_tests && pytest .
3 changes: 3 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ jobs:
- name: Test with pytest
run: |
pytest --cov=keras_nlp --cov-report xml:coverage.xml
- name: Run integration tests
run: |
python pip_build.py --install && cd integration_tests && pytest .
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ recommend using `conda` to install tensorflow dependencies (such as CUDA), and
`pip` to install python packages from PyPI. The exact method will depend on your
OS.

**Note**: Please be careful not to use the `tensorflow` pre-packaged with conda,
which is incompatible with `tensorflow-text` on PyPi, and follow the
instructions below.

### Linux (recommended)

To setup a complete environment with TensorFlow, a local install of keras-nlp,
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ are trying to explain to the reader.
This will continue to be a key investment area for the library. If you have an
idea for a guide or example, please open an issue to discuss.

By the end of 2022, most new NLP examples on keras.io should be use
By the end of 2022, most new NLP examples on keras.io should use
KerasNLP library.

## Citation bar
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 22 additions & 4 deletions keras_nlp/layers/transformer_layer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
from absl import logging


def _check_masks_shapes(inputs, padding_mask, attention_mask):
mask = padding_mask
if hasattr(inputs, "_keras_mask") and mask is None:
mask = inputs._keras_mask
if mask is not None:
if mask._rank() != 2:
raise ValueError(
"`padding_mask` should have shape "
"(batch_size, target_length). "
f"Received shape `{mask.shape}`."
)
if attention_mask is not None:
if attention_mask._rank() != 3:
raise ValueError(
"`attention_mask` should have shape "
"(batch_size, target_length, source_length). "
f"Received shape `{mask.shape}`."
)


def compute_causal_mask(batch_size, input_length, output_length, cache_index=0):
"""Compute a causal attention mask for a transformer decoder.
Expand Down Expand Up @@ -60,6 +80,7 @@ def merge_padding_and_attention_mask(
A merged 2D mask or None. If only `padding_mask` is provided, the
returned mask is padding_mask with one additional axis.
"""
_check_masks_shapes(inputs, padding_mask, attention_mask)
mask = padding_mask
if hasattr(inputs, "_keras_mask"):
if mask is None:
Expand All @@ -80,8 +101,5 @@ def merge_padding_and_attention_mask(
if mask is None:
return attention_mask
else:
return tf.minimum(
mask[:, tf.newaxis, :],
attention_mask,
)
return tf.minimum(mask, attention_mask)
return mask
27 changes: 26 additions & 1 deletion keras_nlp/layers/transformer_layer_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_compute_causal_mask(self):

def test_merge_padding_and_attention_mask(self):
padding_mask = tf.convert_to_tensor([[1, 1, 0]])
attention_mask = tf.convert_to_tensor([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
attention_mask = tf.convert_to_tensor(
[[[0, 0, 1], [0, 1, 0], [1, 0, 0]]]
)
inputs = tf.random.uniform(shape=[1, 3, 2])
merged_mask = utils.merge_padding_and_attention_mask(
inputs,
Expand All @@ -34,3 +36,26 @@ def test_merge_padding_and_attention_mask(self):
self.assertTrue(
(merged_mask.numpy() == [[0, 0, 0], [0, 1, 0], [1, 0, 0]]).all()
)

def test_bad_mask_shapes(self):
with self.assertRaises(ValueError):
padding_mask = tf.convert_to_tensor([[[1, 1, 0], [1, 0, 0]]])
attention_mask = tf.convert_to_tensor(
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]
)
inputs = tf.random.uniform(shape=[1, 3, 2])
utils.merge_padding_and_attention_mask(
inputs,
padding_mask,
attention_mask,
)

with self.assertRaises(ValueError):
padding_mask = tf.convert_to_tensor([[1, 1, 0]])
attention_mask = tf.convert_to_tensor([[0, 0, 1], [1, 0, 0]])
inputs = tf.random.uniform(shape=[1, 3, 2])
utils.merge_padding_and_attention_mask(
inputs,
padding_mask,
attention_mask,
)
3 changes: 1 addition & 2 deletions keras_nlp/models/backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

from tensorflow import keras

from keras_nlp.api_export import keras_nlp_export
from keras_nlp.utils.python_utils import classproperty
from keras_nlp.utils.python_utils import format_docstring


@keras_nlp_export("keras_nlp.models.Backbone")
@keras.utils.register_keras_serializable(package="keras_nlp")
class Backbone(keras.Model):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
3 changes: 1 addition & 2 deletions keras_nlp/models/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

from tensorflow import keras

from keras_nlp.api_export import keras_nlp_export
from keras_nlp.utils.python_utils import classproperty
from keras_nlp.utils.python_utils import format_docstring


@keras_nlp_export("keras_nlp.models.Preprocessor")
@keras.utils.register_keras_serializable(package="keras_nlp")
class Preprocessor(keras.layers.Layer):
"""Base class for model preprocessors."""

Expand Down
3 changes: 1 addition & 2 deletions keras_nlp/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

from tensorflow import keras

from keras_nlp.api_export import keras_nlp_export
from keras_nlp.utils.pipeline_model import PipelineModel
from keras_nlp.utils.python_utils import classproperty
from keras_nlp.utils.python_utils import format_docstring


@keras_nlp_export("keras_nlp.models.Task")
@keras.utils.register_keras_serializable(package="keras_nlp")
class Task(PipelineModel):
"""Base class for Task models."""

Expand Down

0 comments on commit a9e7aea

Please sign in to comment.