Skip to content

Embedding Layer #205

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 15 commits into from
Mar 5, 2025
Merged

Embedding Layer #205

merged 15 commits into from
Mar 5, 2025

Conversation

OneAdder
Copy link
Collaborator

@OneAdder OneAdder commented Feb 20, 2025

Input Embeddings Lookup Table (Trainable

Core

In Natural Language Processing input data is often encoded as indices of tokens in a vocabulary. Those indices are converted into vectors using weights which are trainable.
In theory, similar behaviour can be achieved by spreading input data by the desired size of vectors and then put through input2d and linear2d layers. However, it is very inefficient as we'll have to do matmul each time instead of simply getting an element by indices.
So, I created the layer that does it efficiently. It does not have a gradient as it is intended as an input layer. But it is trainable (get_params, get_gradients and set_params).

Positional

Apart from this core functionality, I also added positional encoding (output vectors + positions assigned to sin/cos waves). This will be needed for transformers.

Python Reference

Here: torch.nn.Embedding and a custom function for positional encoding.

@OneAdder OneAdder marked this pull request as draft February 22, 2025 12:00
@OneAdder OneAdder marked this pull request as ready for review February 23, 2025 10:51
@OneAdder
Copy link
Collaborator Author

@jvdp1 @milancurcic Ready for review

Copy link
Collaborator

@jvdp1 jvdp1 left a comment

Choose a reason for hiding this comment

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

LGTM. Below are some minor suggestions.

@OneAdder OneAdder mentioned this pull request Feb 24, 2025
@OneAdder
Copy link
Collaborator Author

@milancurcic Your opinion?

@milancurcic
Copy link
Member

Thanks for the nudge. Will try to finish review and merge either today or Monday. Thank you for all the hard work!

@milancurcic
Copy link
Member

@OneAdder Can you add the Embedding layer entry to the table of layers in the README? I assume that based on it being provided in nf that it's meant to be consumed by users directly, rather than only internally by other layers.

@OneAdder
Copy link
Collaborator Author

OneAdder commented Mar 4, 2025

@milancurcic Readme updated. Thank you for resolving conflicts!

Copy link
Member

@milancurcic milancurcic left a comment

Choose a reason for hiding this comment

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

Thank you!

@milancurcic milancurcic merged commit e628d1e into modern-fortran:main Mar 5, 2025
4 checks passed
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.

3 participants