-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add bulk transformations functionality #49
Merged
jonatanklosko
merged 4 commits into
elixir-nx:main
from
Virviil:encoding_transformations
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Tokenizers.Encoding.Transformation do | ||
@moduledoc """ | ||
Module containing handy functions to build the transformations list. | ||
|
||
This list is aplied to an encoding using `Tokenizers.Encoding.transform/2`. | ||
""" | ||
|
||
@type t :: [ | ||
{:pad, {non_neg_integer(), Tokenizers.Encoding.padding_opts()}}, | ||
{:truncate, {non_neg_integer(), Tokenizers.Encoding.truncation_opts()}}, | ||
{:set_sequence_id, non_neg_integer()} | ||
] | ||
|
||
@doc """ | ||
Generates the padding transformation. | ||
|
||
Check `Tokenizers.Encoding.pad/3` for more information. | ||
""" | ||
@spec pad(non_neg_integer(), Tokenizers.Encoding.padding_opts()) :: | ||
{:pad, {non_neg_integer(), Tokenizers.Encoding.padding_opts()}} | ||
def pad(target_length, opts \\ []) do | ||
{:pad, {target_length, opts}} | ||
end | ||
|
||
@doc """ | ||
Generates the truncation transformation. | ||
|
||
Check `Tokenizers.Encoding.truncate/3` for more information. | ||
""" | ||
@spec truncate(non_neg_integer(), Tokenizers.Encoding.truncation_opts()) :: | ||
{:truncate, {non_neg_integer(), Tokenizers.Encoding.truncation_opts()}} | ||
def truncate(max_length, opts \\ []) do | ||
{:truncate, {max_length, opts}} | ||
end | ||
|
||
@doc """ | ||
Generates the set_sequence_id transformation. | ||
|
||
Check `Tokenizers.Encoding.set_sequence_id/2` for more information. | ||
""" | ||
@spec set_sequence_id(non_neg_integer()) :: | ||
{:set_sequence_id, non_neg_integer()} | ||
def set_sequence_id(id) do | ||
{:set_sequence_id, id} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved option types directly to the relevant functions, so we no longer have that type. We can bring it back, but it's also fine to just say
keyword()
since we point to the user toEncoding.pad/3
anyway :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonatanklosko I'm not sure that having opts inside function spec and not as a type is good idea (at least here). Using
keyword()
removes the ElixirLS ability to autosuggest, while having full list of opts in every function is a duplication and can lead to inconsistency, when in the future commits one place will be updated and other - no. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I wasn't aware ElixirLS does that, I don't have strong opinion, but that's a fair argument. So in this case we can bring the type back to share it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is how it looks:
It take it from spec, so
keyword()
is less expressive.I'll take a look how to return types at least for these functions