Skip to content

Commit

Permalink
Don't block on tensor access in postprocessing (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Sep 14, 2023
1 parent aa0fa87 commit 2a61cfc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/bumblebee/diffusion/stable_diffusion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ defmodule Bumblebee.Diffusion.StableDiffusion do
end

defp client_postprocessing({outputs, _metadata}, multi?, safety_checker?) do
# We use binary backend so we are not blocked by the serving computation
outputs = Nx.backend_transfer(outputs, Nx.BinaryBackend)

for outputs <- Bumblebee.Utils.Nx.batch_to_list(outputs) do
results =
for outputs = %{image: image} <- Bumblebee.Utils.Nx.batch_to_list(outputs) do
Expand All @@ -336,7 +339,7 @@ defmodule Bumblebee.Diffusion.StableDiffusion do

defp zeroed(tensor) do
0
|> Nx.tensor(type: Nx.type(tensor))
|> Nx.tensor(type: Nx.type(tensor), backend: Nx.BinaryBackend)
|> Nx.broadcast(Nx.shape(tensor))
end

Expand Down
4 changes: 4 additions & 0 deletions lib/bumblebee/text/question_answering.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ defmodule Bumblebee.Text.QuestionAnswering do
{batch, {all_inputs, raw_inputs, multi?}}
end)
|> Nx.Serving.client_postprocessing(fn {outputs, _metadata}, {inputs, raw_inputs, multi?} ->
# We use binary backend so we are not blocked by the serving computation
inputs = Nx.backend_transfer(inputs, Nx.BinaryBackend)
outputs = Nx.backend_transfer(outputs, Nx.BinaryBackend)

Enum.zip_with(
[raw_inputs, Utils.Nx.batch_to_list(inputs), Utils.Nx.batch_to_list(outputs)],
fn [{_question_text, context_text}, inputs, outputs] ->
Expand Down
3 changes: 3 additions & 0 deletions lib/bumblebee/text/text_embedding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ defmodule Bumblebee.Text.TextEmbedding do
{batch, multi?}
end)
|> Nx.Serving.client_postprocessing(fn {embeddings, _metadata}, multi? ->
# We use binary backend so we are not blocked by the serving computation
embeddings = Nx.backend_transfer(embeddings, Nx.BinaryBackend)

for embedding <- Bumblebee.Utils.Nx.batch_to_list(embeddings) do
%{embedding: embedding}
end
Expand Down
4 changes: 4 additions & 0 deletions lib/bumblebee/text/token_classification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ defmodule Bumblebee.Text.TokenClassification do
{batch, {all_inputs, multi?}}
end)
|> Nx.Serving.client_postprocessing(fn {scores, _metadata}, {inputs, multi?} ->
# We use binary backend so we are not blocked by the serving computation
scores = Nx.backend_transfer(scores, Nx.BinaryBackend)
inputs = Nx.backend_transfer(inputs, Nx.BinaryBackend)

Enum.zip_with(
Utils.Nx.batch_to_list(inputs),
Utils.Nx.batch_to_list(scores),
Expand Down
3 changes: 3 additions & 0 deletions lib/bumblebee/vision/image_embedding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ defmodule Bumblebee.Vision.ImageEmbedding do
{Nx.Batch.concatenate([inputs]), multi?}
end)
|> Nx.Serving.client_postprocessing(fn {embeddings, _metadata}, multi? ->
# We use binary backend so we are not blocked by the serving computation
embeddings = Nx.backend_transfer(embeddings, Nx.BinaryBackend)

for embedding <- Bumblebee.Utils.Nx.batch_to_list(embeddings) do
%{embedding: embedding}
end
Expand Down

0 comments on commit 2a61cfc

Please sign in to comment.