Skip to content

Commit bbd4d83

Browse files
feat: Add :progress_bar_step config option (#441)
1 parent 1625644 commit bbd4d83

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ First, if the repository is clearly a fine-tuned version of another model, you c
129129

130130
Otherwise, the Transformers library includes conversion rules to load a "slow tokenizer" and convert it to a corresponding "fast tokenizer", which is possible in most cases. You can generate the `tokenizer.json` file using [this tool](https://jonatanklosko-bumblebee-tools.hf.space/apps/tokenizer-generator). Once successful, you can follow the steps to submit a PR adding `tokenizer.json` to the model repository. Note that you do not have to wait for the PR to be merged, instead you can copy commit SHA from the PR and load the tokenizer with `Bumblebee.load_tokenizer({:hf, "model-repo", revision: "..."})`.
131131

132+
## Global configuration
133+
134+
You can configure the progress bar displayed during model downloads:
135+
136+
```elixir
137+
# Update every 10% instead of every 1%
138+
config :bumblebee, :progress_bar_step, 10
139+
140+
# Disable progress bar entirely
141+
config :bumblebee, :progress_bar_enabled, false
142+
```
143+
132144
<!-- Docs -->
133145

134146
## Contributing

lib/bumblebee/utils.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,15 @@ defmodule Bumblebee.Utils do
88
def progress_bar_enabled?() do
99
Application.get_env(:bumblebee, :progress_bar_enabled, true)
1010
end
11+
12+
@doc """
13+
Returns the progress bar update step in percent.
14+
15+
Progress updates only when crossing step boundaries (e.g., every 1%).
16+
Defaults to `1`. Set to `nil` for updates on every chunk.
17+
"""
18+
@spec progress_bar_step :: non_neg_integer() | nil
19+
def progress_bar_step() do
20+
Application.get_env(:bumblebee, :progress_bar_step, 1)
21+
end
1122
end

lib/bumblebee/utils/http.ex

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ defmodule Bumblebee.Utils.HTTP do
4949
opts = [stream: :self, sync: false, receiver: receiver]
5050

5151
{:ok, request_id} = :httpc.request(:get, request, http_opts, opts, :bumblebee)
52-
download_loop(%{request_id: request_id, file: file, total_size: nil, size: nil})
52+
53+
download_loop(%{
54+
request_id: request_id,
55+
file: file,
56+
total_size: nil,
57+
size: nil
58+
})
5359
after
5460
File.close(file)
5561
end
@@ -88,12 +94,7 @@ defmodule Bumblebee.Utils.HTTP do
8894
:ok ->
8995
part_size = byte_size(body_part)
9096
state = update_in(state.size, &(&1 + part_size))
91-
92-
if Bumblebee.Utils.progress_bar_enabled?() &&
93-
state.total_size && part_size != state.total_size do
94-
ProgressBar.render(state.size, state.total_size, suffix: :bytes)
95-
end
96-
97+
state = maybe_render_progress(state, part_size)
9798
download_loop(state)
9899

99100
{:error, error} ->
@@ -106,6 +107,27 @@ defmodule Bumblebee.Utils.HTTP do
106107
:ok
107108
end
108109

110+
defp maybe_render_progress(state, part_size)
111+
when is_nil(state.total_size) or part_size == state.total_size do
112+
state
113+
end
114+
115+
defp maybe_render_progress(state, part_size) do
116+
if Bumblebee.Utils.progress_bar_enabled?() do
117+
step = Bumblebee.Utils.progress_bar_step()
118+
percent = trunc(state.size / state.total_size * 100)
119+
last_percent = trunc((state.size - part_size) / state.total_size * 100)
120+
step_bucket = if step, do: div(percent, step), else: percent
121+
last_step_bucket = if step, do: div(last_percent, step), else: last_percent
122+
123+
if step_bucket > last_step_bucket or percent == 100 do
124+
ProgressBar.render(state.size, state.total_size, suffix: :bytes)
125+
end
126+
end
127+
128+
state
129+
end
130+
109131
defp total_size(headers) do
110132
case List.keyfind(headers, ~c"content-length", 0) do
111133
{_, content_length} ->

0 commit comments

Comments
 (0)