You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ea53d85
Browse filesBrowse the repository at this point in the historyBrowse files
Add an ordered progress recorder that needs no callback (#14)
* Add a clearable progress callback and a silent progress sink
Upstream keeps the progress callback in an unsynchronized global that the
loading or generating thread reads on every step, so a caller cannot tell when
a replaced callback has stopped running. A Dart NativeCallable invoked after
its VM shut down aborts with "GetFfiCallbackMetadata called after shutdown".
src/sd_dart_wrapper.h adds two exports, compiled into the same library by a
top-level CMakeLists.txt that wraps the upstream project:
- sd_dart_set_progress_callback(callback, data) registers one forwarder with
upstream and routes progress to `callback`; NULL discards progress without
printing progress bars.
- sd_dart_clear_progress_callback(void*) stops routing to that callback and
takes one pointer, so it can be a Dart NativeFinalizer callback.
The callback is swapped behind a lock held while it runs: when either function
returns, the replaced callback is not running on another thread and is never
called again.
The export allowlist, shipped headers, XCFramework module map and SwiftPM
consumer probe cover the new header.
Refs #10
* Keep the upstream build directory short for Windows
Building upstream under build/<target>/third_party/stable-diffusion.cpp put
the nested Vulkan shader generator's compiler checks past the 260-character
Windows path limit, so windows-x64-vulkan failed to configure it.
Refs #10
* Move the Vulkan shader generator build to a short path
The previous commit was not enough for windows-x64-vulkan: MSBuild's file
tracker failed on a 260-character tlog path in the generator's nested
configure (FTK1011). The same path is 257 characters when upstream is the
top-level project, so any extra directory level breaks it.
An inherited EP_PREFIX puts ggml's ExternalProject under build/<target>/ep,
which shortens that path to 213 characters.
Refs #10
* Record progress natively instead of calling back
An independent audit of the callback design found that it cannot be made safe
for a Dart callback:
- sd_dart_clear_progress_callback held a mutex across the callback and took
the same mutex. As a NativeFinalizer it runs at isolate shutdown while the
thread is not at a safepoint; a NativeCallable.listener called on the
reporting thread blocks on a pending safepoint operation. The finalizer
waits for the mutex, its holder waits for the garbage collection, and the
collection waits for the finalizer: the process never exits.
- When the callback belongs to a background isolate and the main isolate
dies, the VM stops accepting callbacks before that isolate's finalizer
runs, so the original abort comes back.
Both reproduced. The library now takes no callback at all:
- sd_dart_progress_enable() registers a recorder with upstream, once. It
only stores the report in atomics, and silences the stdout progress bars,
loads included.
- sd_dart_progress_read() copies the latest whole report, with a sequence
number, into a caller-provided struct. It takes no lock and never waits
for the reporting thread, so it is a Dart leaf call.
sd_dart_set_progress_callback and sd_dart_clear_progress_callback are gone.
Nothing has to be undone when the caller goes away, and the recorder has no
destructor, so it stays valid while exit() runs.
tests/test_progress_stress.py drives a Dart VM that dies while a worker
isolate reports progress from a native call, with allocating isolates, for
three owner layouts. The two replaced designs are kept there as controls.
CI runs it on Linux and macOS.
Refs #10
* Check DLL exports and smoke-test macOS in CI
validate_artifacts.py returned before the export check on Windows, so
nothing compared a DLL's export table with the allowlist. It now reads the
export name table itself, which needs no Windows SDK tool.
macos-arm64 gets the smoke step the Linux and Windows targets have, so the
progress recorder is checked against the built macOS library too.
Refs #10
* Keep a history of progress reports
A second audit found that recording only the latest report loses progress
for every batch of more than one image. stable-diffusion.cpp samples all
images before it decodes any, so one image's steps/steps report is followed
within a millisecond by the next image's 0/steps. A caller that polls sees
only the latter, and llamadart's tracker, which counts images by the former,
never advances the image index or emits its decoding event. Reproduced with
SD-Turbo (4 steps x 3 images, Metal) and SDXS (1 step x 2, CPU) at a 50 ms
poll.
The recorder is now a ring of 4096 reports, and sd_dart_progress_read()
returns them in order:
size_t sd_dart_progress_read(uint64_t after, sd_dart_progress_t* reports,
size_t capacity, uint64_t* latest);
- It copies the reports with a sequence greater than `after`, oldest first,
and reports the newest sequence through `latest`.
- The 4095 most recent reports are kept. The fastest source measured,
convert, makes 47 reports in 50 ms. A caller that falls further behind
gets the newest ones and sees the jump in sequence.
- A report is accepted only if the published sequence shows that its slot
cannot have been rewritten during the copy. Readers still take no lock and
never wait for a reporter; a read copies at most capacity + 4 reports.
sd_dart_progress_enable() now returns only once the recorder is registered,
also when another thread is the one registering it.
The Dart VM harness checks every report it reads against its sequence, has
a configuration in which no report may be missing, and gains
progress_images.dart: a stand-in emits upstream's report pattern for a batch
and a polling consumer must see all of it, while a control that looks only
at the newest report must not.
Refs #10
* Pin the Dart SDK of the dart vm jobs
The harness's control expects the aborts of this VM when native code calls
a Dart callback during shutdown. An unpinned SDK could turn the job red for
a VM change that is not a defect here.
Refs #10
| `sd_dart_progress_enable` | Records progress in the library instead of printing progress bars to stdout, for every later call in the process, model loads included. Idempotent; when it returns the recorder is registered. |
77
+
| `sd_dart_progress_read` | Copies the reports with a sequence greater than `after` into `reports`, oldest first, at most `capacity`, and returns how many. `latest`, if not `NULL`, receives the sequence of the newest report (0 if none); a `capacity` of 0 only asks for that. |
78
+
79
+
Upstream's `sd_set_progress_callback` calls back on the thread that is loading
80
+
or generating, for as long as that call runs. A callback into a managed
81
+
runtime cannot be made safe there. A Dart `NativeCallable` is called after
82
+
its isolate or the VM has shut down, which aborts the process, and a wrapper
83
+
that waits for a call in flight before dropping the callback can deadlock VM
84
+
shutdown. So the library takes no callback: it records the reports itself,
85
+
and the caller reads them in order whenever it likes.
86
+
87
+
What is guaranteed:
88
+
89
+
- **Every report, in order.** The sequences of one call's reports are
90
+
consecutive, and each report is whole: the three values of one upstream
91
+
call, never a mix of two.
92
+
- **History.** The library keeps the 4095 most recent reports. A caller that
93
+
reads before more than that were recorded since its last read misses none.
94
+
The fastest sources measured on an M4 Max are `convert` (746 reports a
95
+
second, 47 in 50 ms) and a tiled VAE decode with 64-pixel tiles (195 a
96
+
second, 10 in 50 ms); sampling a batch of one-step images reports 16 times
97
+
a second. So a 50 ms poll uses about 1% of the history, and a caller may
98
+
stall for more than five seconds before a report is lost.
99
+
- **Overflow is visible.** If more than 4095 reports were recorded since
100
+
`after`, the older ones are gone and the first report returned is not
101
+
`after + 1`.
102
+
- **Never blocks.** `sd_dart_progress_read` takes no lock, makes no system
103
+
call, never waits for a reporting thread and copies at most `capacity` + 4
104
+
reports; a reporting thread never waits for a reader. It is safe as a Dart
105
+
leaf call. A call may return fewer reports than there are; the caller has
106
+
them all once the last sequence it holds equals `latest`.
107
+
- **Nothing to undo.** No call is needed when the caller goes away, and the
108
+
state is never destroyed, so reporting and reading stay valid while
109
+
`exit()` runs.
110
+
111
+
What is not:
112
+
113
+
- **Which call a report belongs to.** Reports are process-wide, as upstream's
114
+
callback is. Every context's loads and generations go into the one
115
+
sequence, so two contexts working at once see each other's reports, load
116
+
reports included, and each takes up part of the other's history.
117
+
- **A registration that survives `sd_set_progress_callback`.** The first
118
+
`sd_dart_progress_enable` call registers the recorder there. That function
119
+
is not synchronized, so make the call before another thread starts a load
120
+
or generation. A later `sd_set_progress_callback` call replaces the
121
+
recorder, `latest` stops advancing, and `sd_dart_progress_enable` does not
122
+
register it again. A caller that wants a C callback uses upstream's
123
+
function instead of this API.
124
+
- **A shorter exit.** A process whose caller died still waits for a native
125
+
call that is in flight: the Dart VM, for one, exits only after the isolate
126
+
inside `new_sd_ctx` or `generate_image` returns.
127
+
128
+
To follow one call, ask for `latest` before starting it, read from there on a
129
+
timer, and read once more when the call has returned:
130
+
131
+
```c
132
+
uint64_t after;
133
+
sd_dart_progress_read(0, NULL, 0, &after);
134
+
/* start the call on another thread; then, on each tick and once at the end: */
0 commit comments