Skip to content

Commit e4ae9e9

Browse files
justinchubyCopilot
andcommitted
Order shard progress bars deterministically
Derive each tqdm row from the one-based shard number embedded in the external-data filename, so concurrent callbacks cannot reorder the displayed bars. Cover shard-two-first callback delivery while retaining the onnx_ir 1.0 single-bar fallback. Co-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent 5da048e commit e4ae9e9

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎src/mobius/_model_package.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,9 @@ def _make_progress_callback():
271271
272272
Newer ``onnx_ir`` versions may invoke callbacks concurrently and out of
273273
index order. Count invocations instead of tracking ``metadata.index`` and
274-
serialize all progress-bar mutations. This remains compatible with
275-
``onnx_ir`` 1.0, where callbacks are invoked serially.
274+
derive each bar's position from its shard filename so rendering order is
275+
deterministic. Serialize all progress-bar mutations. This remains compatible
276+
with ``onnx_ir`` 1.0, where callbacks are invoked serially.
276277
"""
277278
lock = threading.Lock()
278279
bars: dict[str, tqdm.tqdm] = {}
@@ -288,10 +289,16 @@ def callback(tensor: ir.TensorProtocol, metadata: ir.external_data.CallbackInfo)
288289
if shard_total is not None
289290
else "Saving external data"
290291
)
292+
position = 0
293+
if shard_total is not None:
294+
shard_prefix, separator, _ = metadata.filename.rpartition("-of-")
295+
shard_number = shard_prefix.rpartition("-")[2]
296+
if separator and shard_number.isdigit():
297+
position = int(shard_number) - 1
291298
pbar = tqdm.tqdm(
292299
total=shard_total if shard_total is not None else metadata.total,
293300
desc=description,
294-
position=len(bars),
301+
position=position,
295302
leave=True,
296303
)
297304
bars[key] = pbar

‎src/mobius/_model_package_test.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def set_postfix_str(self, value):
146146
def close(self):
147147
self.closed = True
148148

149-
def test_creates_one_progress_bar_per_shard(self, monkeypatch):
149+
def test_orders_progress_bars_by_shard_number(self, monkeypatch):
150150
bars = []
151151

152152
def make_bar(**kwargs):
@@ -156,7 +156,10 @@ def make_bar(**kwargs):
156156

157157
monkeypatch.setattr("mobius._model_package.tqdm.tqdm", make_bar)
158158
callback = _make_progress_callback()
159-
for filename in ("model-00001-of-00002.data", "model-00002-of-00002.data"):
159+
for filename in (
160+
"model-00002-of-00002.onnx.data",
161+
"model-00001-of-00002.onnx.data",
162+
):
160163
for shard_index in reversed(range(2)):
161164
callback(
162165
self._Tensor(),
@@ -171,10 +174,10 @@ def make_bar(**kwargs):
171174
)
172175

173176
assert len(bars) == 2
174-
assert [bar.position for bar in bars] == [0, 1]
177+
assert [bar.position for bar in bars] == [1, 0]
175178
assert all(bar.total == 2 and bar.n == 2 and bar.closed for bar in bars)
176-
assert "model-00001-of-00002.data" in bars[0].desc
177-
assert "model-00002-of-00002.data" in bars[1].desc
179+
assert "model-00002-of-00002.onnx.data" in bars[0].desc
180+
assert "model-00001-of-00002.onnx.data" in bars[1].desc
178181

179182
def test_falls_back_to_one_bar_with_onnx_ir_1_0(self, monkeypatch):
180183
bars = []
@@ -200,6 +203,7 @@ def make_bar(**kwargs):
200203
assert len(bars) == 1
201204
assert bars[0].total == 4
202205
assert bars[0].n == 4
206+
assert bars[0].position == 0
203207
assert bars[0].closed
204208

205209
def test_is_thread_safe(self, monkeypatch):

0 commit comments

Comments
 (0)