Skip to content

Preserve partial worker models on failure and stop completion - #1295

Open
ale18V wants to merge 8 commits into
feat/training-worker-page-client-alternativefrom
fix/worker-model-recovery
Open

ale18V wants to merge 8 commits into
feat/training-worker-page-client-alternativefrom
fix/worker-model-recovery

Conversation

@ale18V

@ale18V ale18V commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

What & why

Stacked on #1287 and targeting its branch. Stop now interrupts network waits after startup without disposing the partial model, so a client waiting for participants or aggregation can stop and save its progress. Terminal messages return the model before connection cleanup, and the page waits separately for cleanup before terminating the worker.

Validation: unit/integration tests, five Chrome browser checks, lint, formatting, TypeScript checks, and the production build pass. A real-server check with two LUS browser clients and 96 images per client completed three collaborative rounds; after stopping the first client and leaving the second waiting alone for 20 seconds, they stopped in 0.83 s and 0.09 s, and both models saved. This ran on Chrome 154/Linux with software WebGL.

Technical plan

Reuse the client's existing abort mechanism once startup has handed the model to the trainer. Keep explicit success and error sends, returning the encoded model before closing connections. A stop acknowledgment cancels the response watchdog, while a separate cleanup acknowledgment allows the page to wait for shutdown after receiving the model.

Deviations from plan

None.

Todo

Remaining review suggestions are tracked in the review threads.

@ale18V
ale18V added this pull request to stack #1296 November 18, 2026 10:08
@ale18V
ale18V requested a review from JulienVig November 18, 2026 10:09
@ale18V ale18V changed the title Preserve partial worker models on failure and stop completion Simplify worker dataset inputs and preserve partial training models Nov 18, 2026
@ale18V
ale18V force-pushed the fix/worker-model-recovery branch from ccc7b55 to e241d11 Compare November 18, 2026 10:45
@ale18V ale18V changed the title Simplify worker dataset inputs and preserve partial training models Preserve partial worker models on failure and stop completion Nov 18, 2026

@JulienVig JulienVig left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few suggestions but not blocking

const roundStatus = ref<[RoundStatus, string]>();
const training = ref(false);
let worker: Worker | undefined;
let finishing = false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using 3 state variables to track the training state: finishing, worker and stopTimeout. To simplify things and avoid mistakes we could use a single phase variable:

interface Session {
  worker: Worker;
  closePeerTransport?: () => void;
}

type Phase =
  | { kind: "idle" }
  | { kind: "training"; session: Session }
  | { kind: "stopping"; session: Session; timeout: ReturnType<typeof setTimeout> }
  | { kind: "finishing"; session: Session };

const phase = shallowRef<Phase>({ kind: "idle" });
const isTraining = computed(() => phase.value.kind !== "idle");

These types would allow more robust transitions and avoid the different state variables from getting out of sync.
In the UI this even allows us to replace "Stop training" by "Stopping..." after clicking on the button

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #1295. Replaced separate lifecycle flags with phase, so Stop now disables the button and displays “Stopping…” while waiting for the worker.

stopTimeout = undefined;
try {
if (message.model !== undefined) {
const model = await modelDecode(message.model);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the worker training reports an error and an inval model, this line will throw and hide the original worker error. We could report any worker error before anything else

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #1295. Reported the worker error before decoding the partial model, so decoding failures no longer hide the original error.

@JulienVig

JulienVig commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

@ale18V I just tried to train a LUS model in the UI with two clients. After a few rounds, clicking stop training in the first tab stops the training immediately but then stopping the second tab takes around 30s. I also tried it on the current main branch and the second tab stops instantly. Could you look into what introduces this lag? If this is intentional then we should definitly add some UX feedback after clicking stop

@ale18V

ale18V commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed in #1295. Now Stop interrupts participant waits and preserves the model. In the two-tab LUS check, the second client stopped in 92 ms and both models saved.

@ale18V

ale18V commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hi Julien, regarding the benchmark: I compared page responsiveness on current main, the other WebWorker PR approach (old #1264), and the current worker implementation with a local transfer optimization.

I used two browser tabs, a local Disco server, the LUS model and 96 images per participant, over three decentralized training rounds. I used Chrome's PerformanceObserver to record tasks blocking the page's main thread for more than 50 ms, from Start until just before Stop.

Values below are Tab 1 | Tab 2:

Version Longest task exceeding 50 ms Number of those tasks
Main: training and aggregation on page 1,234 ms | 1,358 ms 6 | 7
Earlier PageClient version: training in worker, aggregation on page 248 ms | 248 ms 1 | 1
Current worker version, including 1adfd1ec: training and aggregation in worker None detected | None detected 0 | 0

I also ran another test about the latency of sending messages through the WebRTC bridge: I send byte buffers through the production WebRTC code and echoed them back, directly vs through the worker bridge.
The benchmark tested payloads from 1 KiB to 64 MiB.

Turns out the code could be optimized more: the bridge unnecessarily copied every received message in webapp/src/components/training/peer_transport.ts:

const data = Uint8Array.from(message.data); // Allocate and copy all bytes
port.postMessage({ ...message, data }, [data.buffer]); // Transfer the copy

Commit 1adfd1ec — perf(workers): transfer owned peer buffers without copying instead transfers the original buffer when we own its entire allocation. Buffers that cannot safely be handed over retain the copying path.

For one 64 MiB receive, chunk reassembly took 26.5 ms, the unnecessary copy 29.4 ms, and transfer 0.5 ms. Reassembly also exists on main; our bridge added the second full-message copy. This also matches the ~60ms staggering that was reported in PR #1297.

The repeated transport results were:

Message size Direct control Bridge before fix Bridge after fix
1 MiB 2.3 ± 0.8 ms 3.0 ± 1.7 ms 4.2 ± 1.9 ms
16 MiB 8.3 ± 0.4 ms 14.1 ± 1.5 ms 7.9 ± 0.5 ms
64 MiB 31.5 ± 1.3 ms 56.8 ± 1.9 ms 27.6 ± 1.2 ms

Times were measured through a 5 ms timer measured callback lateness;

In conclusion, the current version of the PR removes page freezing, as during training there is no task that freezes the main thread for longer than 50ms.
Also the transport bridge does not introduce a significant overhead, as we pass buffers through transfering object ownership between the threads

Let startup finish before applying Stop, then reuse the client's existing abort mechanism to interrupt participant, peer and aggregation waits without disposing the model.

Return the model before closing connections and acknowledge cleanup separately. Cover network cancellation, deferred startup stopping, slow cleanup and saving partial models through the app.
Allow transports to consume fresh encoded messages and transfer owned receive buffers directly. Preserve copying for borrowed, sliced and pooled buffers, and remove the redundant Buffer wrapper after message encoding.

Verify ownership and byte integrity through real MessageChannels. In the focused 64 MiB WebRTC benchmark, median worst page lag fell from 57.8 ms to 27.0 ms and observed Long Tasks fell from 21 to zero across three runs; two LUS clients also trained, stopped and saved over WebRTC.
@ale18V
ale18V force-pushed the fix/worker-model-recovery branch from 6f301c2 to 4df22f5 Compare November 25, 2026 11:23

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants