-
Notifications
You must be signed in to change notification settings - Fork 110
Move featurizer batch part to serving computation #243
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,69 @@ defmodule Bumblebee.Featurizer do | |
@type t :: Bumblebee.Configurable.t() | ||
|
||
@doc """ | ||
Performs feature extraction on the given input. | ||
Converts the given input to a batched tensor (or a tensor container). | ||
|
||
Numerical batch processing should be moved to `c:process_batch/2` | ||
whenever possible. | ||
""" | ||
@callback process_input(t(), input :: any()) :: Nx.t() | Nx.Container.t() | ||
|
||
@doc """ | ||
Returns an input template for `c:process_batch/2`. | ||
|
||
The shape is effectively the same as the result of `c:process_input/2`, | ||
except for the batch size. | ||
""" | ||
@callback batch_template(t(), batch_size :: pos_integer()) :: Nx.t() | Nx.Container.t() | ||
|
||
@doc """ | ||
Optional batch processing stage. | ||
|
||
This is a numerical function. It receives the result of `c:process_input/2`, | ||
except the batch size may differ. | ||
|
||
When using featurizer as part of `Nx.Serving`, the batch stage can | ||
be merged with the model computation and compiled together. | ||
""" | ||
@callback process_batch(t(), input :: Nx.t() | Nx.Container.t()) :: Nx.t() | Nx.Container.t() | ||
|
||
@optional_callbacks batch_template: 2, process_batch: 2 | ||
|
||
@doc """ | ||
Converts the given input to a batched tensor (or a tensor container). | ||
""" | ||
@spec process_input(t(), any()) :: Nx.t() | Nx.Container.t() | ||
def process_input(%module{} = featurizer, input) do | ||
module.process_input(featurizer, input) | ||
end | ||
|
||
@doc """ | ||
Returns an input template for `process_batch/2`. | ||
|
||
If the featurizer does not define batch processing, `nil` is returned. | ||
""" | ||
@spec batch_template(t(), pos_integer()) :: Nx.t() | Nx.Container.t() | nil | ||
def batch_template(%module{} = featurizer, batch_size) do | ||
if function_exported?(module, :batch_template, 2) do | ||
module.batch_template(featurizer, batch_size) | ||
end | ||
end | ||
|
||
@doc """ | ||
Optional batch processing stage. | ||
|
||
This is a numerical function. It receives the result of `c:process_input/2`, | ||
except the batch size may differ. | ||
|
||
If the featurizer does not define batch processing, the input is | ||
returned as is. | ||
""" | ||
@callback apply(t(), input :: any(), defn_options :: keyword()) :: any() | ||
@spec process_batch(t(), Nx.t() | Nx.Container.t()) :: Nx.t() | Nx.Container.t() | ||
def process_batch(%module{} = featurizer, batch) do | ||
if function_exported?(module, :process_batch, 2) do | ||
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. Keep in mind this will only work if 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. Ah right, will add ensure loaded! 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. FWIW it should be loaded, because we would call |
||
module.process_batch(featurizer, batch) | ||
else | ||
batch | ||
end | ||
end | ||
end |
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 didn't deprecate, because it's very unlikely that someone implements a featurizer outside bumblebee.
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.
Beautiful. If you want to keep backwards compatibility, you could keep it as a apply and introduce apply_batch.
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 would rather check for it as fallback, I just don't think it's worth in this case.
As for naming, I didn't go with
apply_batch
because then it seems as ifapply_batch
were batched version ofapply
. I'm not sure the current naming is perfect either, but the best I come up with :)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.
Your call, just mentioning for completeness.