Skip to content

Add support for positions array in keras_nlp.layers.RotaryEmbedding layer #1571

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

Merged
merged 4 commits into from
Apr 11, 2024

Conversation

tirthasheshpatel
Copy link
Contributor

Adds support for a positions array in our RotaryEmbedding layer. This is useful when non-arange positions arrays are required like the one used in this paper.

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! couple tiny comments

tensor = ops.cast(positions, dtype="float32")
else:
seq_len = ops.shape(inputs)[sequence_axis]
tensor = ops.arange(seq_len, dtype="float32") + start_index
Copy link
Member

Choose a reason for hiding this comment

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

Let's move this branch into it's own sub function?

That might make it easier to try overriding.

if positions is None:
    positions = self._compute_positions(inputs, start_index=0)
else:
    positions = ops.cast(positions, dtype="float32")

Copy link
Contributor Author

@tirthasheshpatel tirthasheshpatel Apr 11, 2024

Choose a reason for hiding this comment

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

Since we are renaming the tensor var to positions, we can simply remove the else statement. Did that in the latest commit, let me know if it looks good!

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 actually keep the self._compute_positions over-ridable method for a somewhat hacky reason. Since we don't have great "layer wrappinig" or generation mutation functionality right now, this allows for a patch on the layer itself that would control this computation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@mattdangerw mattdangerw merged commit ab8d951 into keras-team:master Apr 11, 2024
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.

2 participants