Current section
Files
Jump to
Current section
Files
lib/bumblebee/text/text_classification.ex
defmodule Bumblebee.Text.TextClassification do
@moduledoc false
alias Bumblebee.Shared
def text_classification(model_info, tokenizer, opts \\ []) do
%{model: model, params: params, spec: spec} = model_info
Shared.validate_architecture!(spec, :for_sequence_classification)
opts = Keyword.validate!(opts, [:compile, top_k: 5, defn_options: []])
top_k = opts[:top_k]
compile = opts[:compile]
defn_options = opts[:defn_options]
batch_size = compile[:batch_size]
sequence_length = compile[:sequence_length]
if compile != nil and (batch_size == nil or sequence_length == nil) do
raise ArgumentError,
"expected :compile to be a keyword list specifying :batch_size and :sequence_length, got: #{inspect(compile)}"
end
{_init_fun, predict_fun} = Axon.build(model)
scores_fun = fn params, input ->
outputs = predict_fun.(params, input)
Axon.Activations.softmax(outputs.logits)
end
Nx.Serving.new(
fn defn_options ->
scores_fun =
Shared.compile_or_jit(scores_fun, defn_options, compile != nil, fn ->
inputs = %{
"input_ids" => Nx.template({batch_size, sequence_length}, :u32),
"attention_mask" => Nx.template({batch_size, sequence_length}, :u32)
}
[params, inputs]
end)
fn inputs ->
inputs = Shared.maybe_pad(inputs, batch_size)
scores_fun.(params, inputs)
end
end,
defn_options
)
|> Nx.Serving.process_options(batch_size: batch_size)
|> Nx.Serving.client_preprocessing(fn input ->
{texts, multi?} = Shared.validate_serving_input!(input, &Shared.validate_string/1)
inputs =
Bumblebee.apply_tokenizer(tokenizer, texts,
length: sequence_length,
return_token_type_ids: false
)
{Nx.Batch.concatenate([inputs]), multi?}
end)
|> Nx.Serving.client_postprocessing(fn scores, _metadata, multi? ->
for scores <- Bumblebee.Utils.Nx.batch_to_list(scores) do
k = min(top_k, Nx.size(scores))
{top_scores, top_indices} = Nx.top_k(scores, k: k)
predictions =
Enum.zip_with(
Nx.to_flat_list(top_scores),
Nx.to_flat_list(top_indices),
fn score, idx ->
label = spec.id_to_label[idx] || "LABEL_#{idx}"
%{score: score, label: label}
end
)
%{predictions: predictions}
end
|> Shared.normalize_output(multi?)
end)
end
end