-
Notifications
You must be signed in to change notification settings - Fork 110
Add support for CLS token pooling in text embedding #385
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
78dc2e3
add suport for cls token pooling
nyo16 32dfdc3
removing test
nyo16 3535141
simplifying Nx slice
nyo16 574128c
fixing formating issues.
nyo16 61fe5d2
documentation added to main module. Simplifying logic for check the a…
nyo16 5ce9de4
Update lib/bumblebee/text.ex
jonatanklosko 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 hidden or 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 |
---|---|---|
|
@@ -61,6 +61,18 @@ defmodule Bumblebee.Text.TextEmbedding do | |
nil -> | ||
output | ||
|
||
:cls_token_pooling -> | ||
case Nx.rank(output) do | ||
3 -> | ||
# Assuming CLS token is always at the first position | ||
Nx.take(output, 0, axis: 1) | ||
|
||
rank -> | ||
raise ArgumentError, | ||
"expected the output tensor to have rank 3 to apply :cls pooling, got: #{rank}." <> | ||
" You should either disable pooling or pick a different output using :output_attribute" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe for any pooling we expect 3, because we reduce n tokens into 1. Now that we have more types, we can move the check before the case, like this: if output_pool != nil and Nx.rank(output) != 3 do
raise ...
end We can use the message from the other clause! |
||
end | ||
|
||
:mean_pooling -> | ||
case Nx.rank(output) do | ||
3 -> | ||
|
@@ -81,7 +93,7 @@ defmodule Bumblebee.Text.TextEmbedding do | |
|
||
other -> | ||
raise ArgumentError, | ||
"expected :output_pool to be one of nil or :mean_pooling, got: #{inspect(other)}" | ||
"expected :output_pool to be one of :cls_token_pooling, :mean_pooling or nil, got: #{inspect(other)}" | ||
end | ||
|
||
output = | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.