README.md 9.11 KB
Newer Older
gaclove's avatar
gaclove committed
1
2
3
4
5
6
7
8
9
10
11
# LightX2V Server

## Overview

The LightX2V server is a distributed video generation service built with FastAPI that processes image-to-video tasks using a multi-process architecture with GPU support. It implements a sophisticated task queue system with distributed inference capabilities for high-throughput video generation workloads.

## Architecture

### System Architecture

```mermaid
PengGao's avatar
PengGao committed
12
13
flowchart TB
    Client[Client] -->|Send API Request| Router[FastAPI Router]
14

PengGao's avatar
PengGao committed
15
16
17
18
    subgraph API Layer
        Router --> TaskRoutes[Task APIs]
        Router --> FileRoutes[File APIs]
        Router --> ServiceRoutes[Service Status APIs]
19

PengGao's avatar
PengGao committed
20
21
22
23
24
25
        TaskRoutes --> CreateTask["POST /v1/tasks/ - Create Task"]
        TaskRoutes --> CreateTaskForm["POST /v1/tasks/form - Form Create"]
        TaskRoutes --> ListTasks["GET /v1/tasks/ - List Tasks"]
        TaskRoutes --> GetTaskStatus["GET /v1/tasks/id/status - Get Status"]
        TaskRoutes --> GetTaskResult["GET /v1/tasks/id/result - Get Result"]
        TaskRoutes --> StopTask["DELETE /v1/tasks/id - Stop Task"]
26

PengGao's avatar
PengGao committed
27
        FileRoutes --> DownloadFile["GET /v1/files/download/path - Download File"]
28

PengGao's avatar
PengGao committed
29
30
        ServiceRoutes --> GetServiceStatus["GET /v1/service/status - Service Status"]
        ServiceRoutes --> GetServiceMetadata["GET /v1/service/metadata - Metadata"]
gaclove's avatar
gaclove committed
31
    end
32

PengGao's avatar
PengGao committed
33
34
35
36
37
38
39
40
41
42
43
    subgraph Task Management
        TaskManager[Task Manager]
        TaskQueue[Task Queue]
        TaskStatus[Task Status]
        TaskResult[Task Result]

        CreateTask --> TaskManager
        CreateTaskForm --> TaskManager
        TaskManager --> TaskQueue
        TaskManager --> TaskStatus
        TaskManager --> TaskResult
gaclove's avatar
gaclove committed
44
    end
45

PengGao's avatar
PengGao committed
46
47
48
49
50
51
52
53
54
55
56
57
    subgraph File Service
        FileService[File Service]
        DownloadImage[Download Image]
        DownloadAudio[Download Audio]
        SaveFile[Save File]
        GetOutputPath[Get Output Path]

        FileService --> DownloadImage
        FileService --> DownloadAudio
        FileService --> SaveFile
        FileService --> GetOutputPath
    end
58

PengGao's avatar
PengGao committed
59
60
61
62
    subgraph Processing Thread
        ProcessingThread[Processing Thread]
        NextTask[Get Next Task]
        ProcessTask[Process Single Task]
63

PengGao's avatar
PengGao committed
64
65
66
        ProcessingThread --> NextTask
        ProcessingThread --> ProcessTask
    end
67

PengGao's avatar
PengGao committed
68
69
70
    subgraph Video Generation Service
        VideoService[Video Service]
        GenerateVideo[Generate Video]
71

PengGao's avatar
PengGao committed
72
73
        VideoService --> GenerateVideo
    end
74

PengGao's avatar
PengGao committed
75
76
77
78
79
80
81
82
83
84
85
86
    subgraph Distributed Inference Service
        InferenceService[Distributed Inference Service]
        SubmitTask[Submit Task]
        Worker[Inference Worker Node]
        ProcessRequest[Process Request]
        RunPipeline[Run Inference Pipeline]

        InferenceService --> SubmitTask
        SubmitTask --> Worker
        Worker --> ProcessRequest
        ProcessRequest --> RunPipeline
    end
87

PengGao's avatar
PengGao committed
88
89
90
91
92
93
94
95
96
    %% ====== Connect Modules ======
    TaskQueue --> ProcessingThread
    ProcessTask --> VideoService
    GenerateVideo --> InferenceService
    GetTaskResult --> FileService
    DownloadFile --> FileService
    VideoService --> FileService
    InferenceService --> TaskManager
    TaskManager --> TaskStatus
gaclove's avatar
gaclove committed
97
98
99
100
101
102
103
104
105
106
107
108
```

## Task Processing Flow

```mermaid
sequenceDiagram
    participant C as Client
    participant API as API Server
    participant TM as TaskManager
    participant PT as Processing Thread
    participant VS as VideoService
    participant FS as FileService
PengGao's avatar
PengGao committed
109
110
111
    participant DIS as DistributedInferenceService
    participant TIW0 as TorchrunInferenceWorker<br/>(Rank 0)
    participant TIW1 as TorchrunInferenceWorker<br/>(Rank 1..N)
112

gaclove's avatar
gaclove committed
113
114
115
116
117
118
    C->>API: POST /v1/tasks<br/>(Create Task)
    API->>TM: create_task()
    TM->>TM: Generate task_id
    TM->>TM: Add to queue<br/>(status: PENDING)
    API->>PT: ensure_processing_thread()
    API-->>C: TaskResponse<br/>(task_id, status: pending)
119

gaclove's avatar
gaclove committed
120
121
122
    Note over PT: Processing Loop
    PT->>TM: get_next_pending_task()
    TM-->>PT: task_id
123

gaclove's avatar
gaclove committed
124
125
    PT->>TM: acquire_processing_lock()
    PT->>TM: start_task()<br/>(status: PROCESSING)
126

gaclove's avatar
gaclove committed
127
    PT->>VS: generate_video_with_stop_event()
128

gaclove's avatar
gaclove committed
129
130
131
132
133
134
135
    alt Image is URL
        VS->>FS: download_image()
        FS->>FS: HTTP download<br/>with retry
        FS-->>VS: image_path
    else Image is Base64
        VS->>FS: save_base64_image()
        FS-->>VS: image_path
PengGao's avatar
PengGao committed
136
137
138
139
140
141
142
143
144
145
146
147
148
    else Image is local path
        VS->>VS: use existing path
    end

    alt Audio is URL
        VS->>FS: download_audio()
        FS->>FS: HTTP download<br/>with retry
        FS-->>VS: audio_path
    else Audio is Base64
        VS->>FS: save_base64_audio()
        FS-->>VS: audio_path
    else Audio is local path
        VS->>VS: use existing path
gaclove's avatar
gaclove committed
149
    end
150

PengGao's avatar
PengGao committed
151
152
    VS->>DIS: submit_task_async(task_data)
    DIS->>TIW0: process_request(task_data)
153

PengGao's avatar
PengGao committed
154
155
156
    Note over TIW0,TIW1: Torchrun-based Distributed Processing
    TIW0->>TIW0: Check if processing
    TIW0->>TIW0: Set processing = True
157

PengGao's avatar
PengGao committed
158
159
160
161
162
163
164
165
166
    alt Multi-GPU Mode (world_size > 1)
        TIW0->>TIW1: broadcast_task_data()<br/>(via DistributedManager)
        Note over TIW1: worker_loop() listens for broadcasts
        TIW1->>TIW1: Receive task_data
    end

    par Parallel Inference across all ranks
        TIW0->>TIW0: runner.set_inputs(task_data)
        TIW0->>TIW0: runner.run_pipeline()
gaclove's avatar
gaclove committed
167
    and
PengGao's avatar
PengGao committed
168
169
170
171
172
173
174
175
176
        Note over TIW1: If world_size > 1
        TIW1->>TIW1: runner.set_inputs(task_data)
        TIW1->>TIW1: runner.run_pipeline()
    end

    Note over TIW0,TIW1: Synchronization
    alt Multi-GPU Mode
        TIW0->>TIW1: barrier() for sync
        TIW1->>TIW0: barrier() response
gaclove's avatar
gaclove committed
177
    end
178

PengGao's avatar
PengGao committed
179
180
181
    TIW0->>TIW0: Set processing = False
    TIW0->>DIS: Return result (only rank 0)
    TIW1->>TIW1: Return None (non-rank 0)
182

PengGao's avatar
PengGao committed
183
    DIS-->>VS: TaskResponse
gaclove's avatar
gaclove committed
184
    VS-->>PT: TaskResponse
185

gaclove's avatar
gaclove committed
186
187
    PT->>TM: complete_task()<br/>(status: COMPLETED)
    PT->>TM: release_processing_lock()
188

gaclove's avatar
gaclove committed
189
190
191
192
193
    Note over C: Client Polling
    C->>API: GET /v1/tasks/{task_id}/status
    API->>TM: get_task_status()
    TM-->>API: status info
    API-->>C: Task Status
194

gaclove's avatar
gaclove committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    C->>API: GET /v1/tasks/{task_id}/result
    API->>TM: get_task_status()
    API->>FS: stream_file_response()
    FS-->>API: Video Stream
    API-->>C: Video File
```

## Task States

```mermaid
stateDiagram-v2
    [*] --> PENDING: create_task()
    PENDING --> PROCESSING: start_task()
    PROCESSING --> COMPLETED: complete_task()
    PROCESSING --> FAILED: fail_task()
    PENDING --> CANCELLED: cancel_task()
    PROCESSING --> CANCELLED: cancel_task()
    COMPLETED --> [*]
    FAILED --> [*]
    CANCELLED --> [*]
```

## Configuration

### Environment Variables

gaclove's avatar
gaclove committed
221
see `lightx2v/server/config.py`
gaclove's avatar
gaclove committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

### Command Line Arguments

```bash
python -m lightx2v.server.main \
    --model_path /path/to/model \
    --model_cls wan2.1_distill \
    --task i2v \
    --host 0.0.0.0 \
    --port 8000 \
    --config_json /path/to/xxx_config.json
```

```bash
python -m lightx2v.server.main \
    --model_path /path/to/model \
    --model_cls wan2.1_distill \
    --task i2v \
    --host 0.0.0.0 \
    --port 8000 \
    --config_json /path/to/xxx_dist_config.json \
    --nproc_per_node 2
```

## Key Features

### 1. Distributed Processing

- **Multi-process architecture** for GPU parallelization
- **Master-worker pattern** with rank 0 as coordinator
- **PyTorch distributed** backend (NCCL for GPU, Gloo for CPU)
- **Automatic GPU allocation** across processes
- **Task broadcasting** with chunked pickle serialization

### 2. Task Queue Management

- **Thread-safe** task queue with locks
- **Sequential processing** with single processing thread
- **Configurable queue limits** with overflow protection
- **Task prioritization** (FIFO)
- **Automatic cleanup** of old completed tasks
- **Cancellation support** for pending and running tasks

### 3. File Management

- **Multiple input formats**: URL, base64, file upload
- **HTTP downloads** with exponential backoff retry
- **Streaming responses** for large video files
- **Cache management** with automatic cleanup
- **File validation** and format detection

## Performance Considerations

1. **Single Task Processing**: Tasks are processed sequentially to manage GPU memory effectively
2. **Multi-GPU Support**: Distributes inference across available GPUs for parallelization
3. **Connection Pooling**: Reuses HTTP connections to reduce overhead
4. **Streaming Responses**: Large files are streamed to avoid memory issues
5. **Queue Management**: Automatic task cleanup prevents memory leaks
6. **Process Isolation**: Distributed workers run in separate processes for stability

## Monitoring and Debugging

### Logging

The server uses `loguru` for structured logging. Logs include:

- Request/response details
- Task lifecycle events
- Worker process status
- Error traces with context

### Health Checks

- `/v1/service/status` - Overall service health
- `/v1/tasks/queue/status` - Queue status and processing state
- Process monitoring via system tools (htop, nvidia-smi)

### Common Issues

1. **GPU Out of Memory**: Reduce `nproc_per_node` or adjust model batch size
2. **Task Timeout**: Increase `LIGHTX2V_TASK_TIMEOUT` for longer videos
3. **Queue Full**: Increase `LIGHTX2V_MAX_QUEUE_SIZE` or add rate limiting

## Security Considerations

1. **Input Validation**: All inputs validated with Pydantic schemas

## License

See the main project LICENSE file for licensing information.