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

Update MaskedLMHead to support dtype=bfloat16/float16/float64 #1197

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

g-dspencer
Copy link

  • Update MaskedLMHead to support dtype=bfloat16/float16/float64.

Inspired by keras-team/keras@397ad57
i.e. using the idiom (?) of dtype=self._dtype_policy.

This is to fix #1195

I had a previous try at this where I accidentally included print statements, sorry.

Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -153,9 +153,11 @@ def build(self, inputs_shape, masked_positions_shape=None):
activation=self.intermediate_activation,
kernel_initializer=self.kernel_initializer,
bias_initializer=self.bias_initializer,
dtype=self._dtype_policy,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -36,6 +39,30 @@ def test_valid_call(self):
position_data = ops.random.randint(minval=0, maxval=10, shape=(4, 5))
model((token_data, position_data))

@parameterized.named_parameters(
("bfloat16", tf.bfloat16),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we now run our testing suite with jax/torch/tf with keras-core, we are generally just referring to these by string name, e.g. "float16" instead of tf.float16.

Does anything break if we switch to that?

@@ -119,6 +146,32 @@ def test_one_train_step(self):
loss = model.train_on_batch(x=(token_data, position_data), y=label_data)
self.assertGreater(loss, 0)

@parameterized.named_parameters(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would kill this test. Compiling a real loss function can make for slower tests, and with the parameterized testing this could slow down our suite.

@@ -153,9 +153,11 @@ def build(self, inputs_shape, masked_positions_shape=None):
activation=self.intermediate_activation,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should really have this for all our our "composite" layers in KerasNLP, right?

  • token and position embedding
  • transformer decoder
  • transformer encoder
  • cached multi head attention
  • f net encoder

Are you interested in following up for other layers? (same PR or split PRs fine!)

)
encoded_tokens = keras.Input(shape=(10, 16))
positions = keras.Input(shape=(5,), dtype="int32")
outputs = head(encoded_tokens, masked_positions=positions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need a rebase over master. This should be mask_positions now. This is causing a lot of test failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MaskedLMHead should support dtype=bfloat16
2 participants