Skip to content

[REFACTOR] Refactor LingBot stateful generation and session lifecycle#8

Merged
lzx1413 merged 9 commits into
mainfrom
refactor_lingbot
Jul 14, 2026
Merged

[REFACTOR] Refactor LingBot stateful generation and session lifecycle#8
lzx1413 merged 9 commits into
mainfrom
refactor_lingbot

Conversation

@lzx1413

@lzx1413 lzx1413 commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR refactors LingBot-World-Fast stateful chunk generation around explicit generation sessions and worker-local runtime state.

It replaces implicitly pipeline-owned state with externally owned generation sessions and cache handles. Long-lived KV, scheduler, timestep, RNG, and VAE streaming decode state now remains in worker-local cache registries instead of being passed between chunk requests.

The refactor also introduces deterministic session cleanup, transactional poison/release semantics, action-driven single-chunk generation, and stronger isolation between concurrent or sequential sessions.

Four-GPU offline and interactive WebRTC examples, assets, tests, and deployment documentation are included to validate and demonstrate the refactored architecture.

Motivation

LingBot generates video incrementally, with every chunk depending on mutable state produced by previous chunks. The previous implementation coupled this state to pipeline and worker execution details, making ownership, failure recovery, and session isolation difficult to reason
about.

In particular, the runtime needed clearer guarantees for:

  • ownership of long-lived generation state
  • session isolation across KV and VAE decode caches
  • deterministic worker and cache cleanup
  • safe behavior after partial generation failures
  • action-driven generation without materializing all controls in advance
  • multi-GPU offline and interactive streaming execution

This refactor establishes explicit session and cache lifecycle boundaries. Callers own generation sessions, workers own device-local state, and cache handles connect the two without transferring large mutable objects between requests.

Type of Change

  • Bug fix (session isolation and failure cleanup)
  • New feature (action-driven chunk API and interactive examples)
  • Breaking change
  • Performance improvement
  • Code refactoring
  • Documentation update
  • Other

Changes Made

Stateful generation architecture

  • Refactor LingBot chunk generation around externally owned generation sessions.
  • Expose an action-driven single-chunk generation API.
  • Keep KV, scheduler, timestep, and RNG state in worker-local cache registries.
  • Address worker-local state through lightweight cache handles.
  • Avoid transferring long-lived mutable generation state between chunk requests.
  • Defer external control materialization until each chunk is generated.

Session and cache lifecycle

  • Add explicit cache creation, lookup, poison, release, and shutdown semantics.
  • Make failed state unusable so partially updated caches cannot be reused.
  • Ensure worker shutdown deterministically releases session-owned resources.
  • Harden session creation and cleanup against partial initialization failures.
  • Prevent stale sessions from affecting later generation requests.

VAE streaming isolation

  • Isolate VAE streaming decode state per generation session.
  • Prevent decode state from leaking between concurrent or sequential sessions.
  • Add transactional poison and release behavior for decode failures.
  • Release VAE state as part of generation-session cleanup.

Model and parallel execution

  • Update the LingBot DiT, denoising pipeline, attention configuration, and runtime integration.
  • Support H100 Sage Attention and Ulysses sequence parallelism.
  • Enable FSDP for the four-GPU streaming configuration.
  • Align camera control, intrinsics, sampler settings, and prompt defaults with the reference H100 workflow.

Examples and interactive streaming

  • Add the four-GPU H100 offline example and associated input assets.
  • Update the four-GPU stream service example.
  • Add bidirectional WebRTC camera control.
  • Support translation through WASD/arrow keys and rotation through IJKL.
  • Convert keyboard input into accumulated camera poses.
  • Hold the latest generated frame while the user is idle.
  • Support the tested 20-second, 321-frame streaming configuration.
  • Add local, VS Code Remote SSH, and standard OpenSSH browser workflows.
  • Document TURN-over-TCP configuration and coturn deployment.

Regression coverage

  • Add model and attention regression tests.
  • Add parallelism and runtime-baseline coverage.
  • Add action-loop and camera-control tests.
  • Add generation-session and cache-lifecycle tests.
  • Add worker shutdown and failure-cleanup tests.
  • Add VAE decode-state isolation coverage.
  • Add offline and streaming example configuration tests.

lzx1413 added 8 commits July 13, 2026 15:10
Main changes:
- add the four-GPU LingBot offline and streaming examples, assets, configuration, and documentation
- expose an action-driven single-chunk API with externally owned generation sessions
- keep KV, scheduler, timestep, and RNG state in worker-local cache registries addressed by cache handles
- isolate VAE streaming decode state per session and add transactional poison/release semantics
- add LingBot model, attention, parallelism, runtime, service-loop, and cache lifecycle regression coverage

Verification:
- python -m pytest tests/unit/pipelines/lingbot_world_fast tests/unit/models/test_lingbot_world_fast_dit.py -q
- python -m ruff check telefuser/models/wan_video_vae.py telefuser/pipelines/lingbot_world_fast examples/lingbot/lingbot_world_fast_image_to_video_h100.py tests/unit/pipelines/lingbot_world_fast
- python -m ruff format --check telefuser/models/wan_video_vae.py telefuser/pipelines/lingbot_world_fast examples/lingbot/lingbot_world_fast_image_to_video_h100.py tests/unit/pipelines/lingbot_world_fast
- four-GPU H100 81-frame smoke test with byte-identical Stage 0/1/2 output
Add explicit ParallelWorker lifecycle and group failure semantics for rank errors and timeouts. Reject reuse after failure or closure, terminate all ranks on unrecoverable errors, and propagate deterministic shutdown through the LingBot pipeline, service, and offline example.

Add lifecycle coverage for timeout and remote-rank failures, idempotent close behavior, and pipeline/service close delegation.

Verification: python -m pytest tests/unit/worker tests/unit/pipelines/lingbot_world_fast tests/unit/models/test_lingbot_world_fast_dit.py -q (54 passed, 3 xfailed); ruff check; 4-GPU H100 regression with byte-identical Stage 0 output and no residual worker processes.
Move action-to-control conversion out of generation sessions and make each pipeline call consume one explicit control tensor or deferred factory. Initialize the runtime on the first call, materializing the first control after VAE/noise preparation and before KV-cache initialization to preserve the baseline execution order. Migrate the offline example and online service, and add lifecycle/control alignment coverage.\n\nVerified with:\n- python -m ruff check telefuser/pipelines/lingbot_world_fast/control.py telefuser/pipelines/lingbot_world_fast/session.py telefuser/pipelines/lingbot_world_fast/pipeline.py telefuser/pipelines/lingbot_world_fast/service.py examples/lingbot/lingbot_world_fast_image_to_video_h100.py tests/unit/pipelines/lingbot_world_fast/test_pipeline_call.py tests/unit/pipelines/lingbot_world_fast/test_service_action_loop.py tests/unit/pipelines/lingbot_world_fast/test_control_alignment.py\n- python -m pytest tests/unit/worker tests/unit/pipelines/lingbot_world_fast tests/unit/models/test_lingbot_world_fast_dit.py -q\n- 4-GPU LingBot example; output SHA-256 matched work_dirs/lingbot_world_fast_stage0_baseline.mp4
Reject incomplete frame and control windows instead of truncating them, and validate control mode, tensor shape, dtype, and device before generation.

Simplify LingBot initialization by removing the unused module manager parameter, clean obsolete configuration fields, and keep serialization in the service layer.

Use TF_MODEL_ZOO_PATH in both examples and synchronize the English and Chinese streaming documentation.

Verification:
- python -m pytest tests/unit/pipelines/lingbot_world_fast tests/unit/models/test_lingbot_world_fast_dit.py -q
- python -m ruff check telefuser/pipelines/lingbot_world_fast examples/lingbot tests/unit/pipelines/lingbot_world_fast
- python -m ruff format --check telefuser/pipelines/lingbot_world_fast examples/lingbot tests/unit/pipelines/lingbot_world_fast
- 4-GPU LingBot offline smoke test; output SHA-256 matched Stage 0 and Stage 3 baselines
Remove the unneeded 240p LingBot resolution preset from the offline and streaming examples.

Update English and Chinese streaming guidance to recommend reducing frame_num for lower-memory smoke tests.

Verification:
- python -m ruff check examples/lingbot
- python -m ruff format --check examples/lingbot
- python -m pytest tests/unit/pipelines/lingbot_world_fast/test_stream_example.py -q
Validate session configuration before creation, retain the normalized control context, and reliably drain terminal service messages. Release decoder and KV-cache state when sessions finish, avoid per-call cache clears for streaming generation, and remove unused LingBot runtime abstractions.\n\nVerification:\n- python -m ruff check telefuser/core/base_pipeline.py telefuser/pipelines/lingbot_world_fast tests/unit/pipelines/lingbot_world_fast examples/lingbot\n- python -m ruff format --check telefuser/core/base_pipeline.py telefuser/pipelines/lingbot_world_fast tests/unit/pipelines/lingbot_world_fast examples/lingbot\n- python -m pytest tests/unit/pipelines/lingbot_world_fast tests/unit/models/test_lingbot_world_fast_dit.py -q\n- 4-GPU offline LingBot H100 image-to-video run
- match LingBot WASD/IJKL camera integration, fixed intrinsics, and cross-chunk pose continuity
- hold the last WebRTC frame while idle and add source-compatible browser controls
- pass stream GPU count from --gpu-num into get_service and document H100 usage
- add control, service, CLI, and stream example coverage

Verification:
- .venv/bin/python -m pytest tests/unit/pipelines/lingbot_world_fast tests/unit/service --ignore=tests/unit/service/livekit -q (192 passed)
- .venv/bin/python -m ruff check on changed Python files
- git diff --check
- GPU 2,3 real-model WebRTC E2E at 832x480, 81 frames, 7 chunks
- enable four-worker FSDP streaming and enforce the tested 20-second session limit
- add duration-based WebRTC controls, separate translation and rotation pads, and H100-aligned defaults
- document local, VS Code, and OpenSSH access with TURN-over-TCP setup in English and Chinese
- cover duration validation, FSDP configuration, and demo defaults with unit tests

Verification:
- .venv/bin/python -m pytest -q tests/unit/pipelines/lingbot_world_fast/test_service_action_loop.py tests/unit/pipelines/lingbot_world_fast/test_stream_example.py
- git diff --cached --check
- four-H100 321-frame WebRTC E2E generation (27/27 chunks)
@lzx1413 lzx1413 changed the title feat(lingbot): refactor chunk sessions and cache lifecycle [REFACTOR] Refactor LingBot stateful generation and session lifecycle Jul 14, 2026
@lzx1413 lzx1413 requested review from Kaimary and Copilot July 14, 2026 05:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the LingBot-World-Fast pipeline to use externally owned, action-driven generation sessions with explicit chunk transactions, while moving worker-local state (KV caches, scheduler/timestep selection, RNG state) into cache registries addressed by handles. It also extends the stream server lifecycle to pass a gpu_num argument into stream pipeline factories, and adds substantial unit/regression coverage plus updated examples/docs for multi-GPU streaming.

Changes:

  • Introduce externally owned generation sessions + single-chunk API with transactional poison/release semantics; move persistent denoise state into worker-local cache registries keyed by cache handles.
  • Add --gpu-num plumbed through stream-serve → service container → StreamPipelineServiceget_service(gpu_num=...) (while keeping factories without gpu_num supported).
  • Add LingBot multi-GPU offline + streaming examples, WebRTC demo/UI updates, and broad unit tests for lifecycle, control alignment, caching, and attention backends.

Reviewed changes

Copilot reviewed 36 out of 40 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/unit/worker/test_parallel_worker_lifecycle.py Adds lifecycle/timeout/failure/close idempotency coverage for ParallelWorker.
tests/unit/service/test_service_routes.py Extends CLI/service tests to validate --gpu-num plumbing into stream server/service startup.
tests/unit/pipelines/lingbot_world_fast/test_stream_example.py Verifies stream example defaults and gpu_num wiring into service/pipeline.
tests/unit/pipelines/lingbot_world_fast/test_session_cache.py Tests cache-handle registry semantics and session-scoped VAE decode state.
tests/unit/pipelines/lingbot_world_fast/test_service_action_loop.py Adds tests for action-driven session loop, directional controls, and session lifecycle.
tests/unit/pipelines/lingbot_world_fast/test_runtime_baseline.py Adds runtime/session baseline tests (chunking, reproducibility, cleanup on failure).
tests/unit/pipelines/lingbot_world_fast/test_pipeline_call.py Validates single-chunk API transaction semantics, poisoning, release, and concurrency rejection.
tests/unit/pipelines/lingbot_world_fast/test_parallelism.py Tests Ulysses + optional FSDP wiring in denoising stage.
tests/unit/pipelines/lingbot_world_fast/test_control_alignment.py Migrates/expands control alignment tests to new control builder + truncation APIs.
tests/unit/ops/test_attention_backends.py Adds coverage that Sage attention import prefers TeleFuser kernel module.
tests/unit/models/test_lingbot_world_fast_dit.py Adds coverage for unified attention usage + config propagation in LingBot DiT blocks.
telefuser/worker/parallel_worker.py Adds failure/closed state, idempotent close, and robust wait/terminate semantics.
telefuser/service/main.py Plumbs gpu_num into stream server initialization.
telefuser/service/core/stream_pipeline_service.py Adds gpu_num support by introspecting get_service factory signature.
telefuser/service/core/container.py Threads gpu_num through container stream initialization.
telefuser/pipelines/lingbot_world_fast/session.py Introduces session/chunk request/result/status models and session-owned decode state.
telefuser/pipelines/lingbot_world_fast/service.py Refactors streaming service loop to external session + action-driven chunk execution; adds duration limits and control integration changes.
telefuser/pipelines/lingbot_world_fast/pipeline.py Implements externally owned session lifecycle, cache-handle init/release, and denoise via worker cache registry.
telefuser/pipelines/lingbot_world_fast/denoising.py Converts denoising into a BaseStage with cache registry and initialize_cache/denoise_and_update_cache APIs.
telefuser/pipelines/lingbot_world_fast/control.py Refactors control building to explicit context/builder + deferred materialization and truncation helpers.
telefuser/pipelines/lingbot_world_fast/init.py Updates public exports to new session and chunk request/result types.
telefuser/ops/attention/backends.py Adjusts Sage attention import priority order.
telefuser/models/wan_video_vae.py Adds session-owned streaming decode state for incremental VAE decoding.
telefuser/models/lingbot_world_fast_dit.py Switches to unified attention backend + adds Ulysses sequence-parallel hooks.
telefuser/entrypoints/cli/main.py Adds --gpu-num/-g CLI option and passes it to run_stream_server.
telefuser/core/base_pipeline.py Adds opt-out flag to skip post-call GPU memory clearing for stateful pipelines.
README.md Updates LingBot WebRTC demo quickstart and networking/TURN guidance.
examples/stream_server/webrtc_bidirectional_demo.py Updates UI/controls, duration-to-frame mapping, defaults, and uses threaded HTTP server.
examples/lingbot/stream_lingbot_world_fast.py Adds a get_service(gpu_num=...) factory and multi-GPU pipeline configuration.
examples/lingbot/README.md Adds detailed LingBot offline + streaming docs, tested limits, and troubleshooting.
examples/lingbot/lingbot_world_fast_image_to_video_h100.py Adds offline 1/4-GPU H100 example using new session/chunk API and control source.
examples/example_config.yaml Adds LingBot offline example entry.
docs/zh/stream_server.md Documents --gpu-num and expands LingBot streaming/TURN guidance (ZH).
docs/zh/service.md Updates service doc snippet referencing LingBot streaming configuration (ZH).
docs/en/stream_server.md Documents --gpu-num and expands LingBot streaming/TURN guidance (EN).
docs/en/service.md Updates service doc snippet referencing LingBot streaming configuration (EN).
.gitignore Adjusts ignore rules to include LingBot example assets in-repo.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/lingbot/lingbot_world_fast_image_to_video_h100.py Outdated
Comment thread telefuser/service/core/stream_pipeline_service.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lzx1413 lzx1413 merged commit 9aafb26 into main Jul 14, 2026
5 checks passed
@lzx1413 lzx1413 deleted the refactor_lingbot branch July 14, 2026 05:46
@lzx1413 lzx1413 mentioned this pull request Jul 14, 2026
19 tasks
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