-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes_zmq_optimized.py
More file actions
574 lines (498 loc) · 21 KB
/
nodes_zmq_optimized.py
File metadata and controls
574 lines (498 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
221
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
"""
ComfyUI Remote GPU Encoding Nodes (Optimized ZMQ Version)
远程 GPU 编码节点(ZMQ 优化版本)
特性:
- 零拷贝传输
- tqdm 专业进度条
- 流式和批量传输
- 会话管理
"""
import torch
import numpy as np
import time
import uuid
from typing import Dict, Any, Tuple
try:
import zmq
from tqdm import tqdm
HAS_ZMQ = True
except ImportError:
HAS_ZMQ = False
from .protocol import (
MessageType,
PixelFormat,
SessionFlags,
VideoHeader,
AudioHeader,
VIDEO_HEADER_SIZE,
AUDIO_HEADER_SIZE,
)
from .logger import Logger, LogLevel, configure_logging, LOGO_PREFIX
from .utils import NetworkUtils, SessionStorage, ConnectionManager, parse_audio
configure_logging(level=LogLevel.INFO)
class RemoteGPUEncoderOptimized:
"""
远程 GPU 编码器(ZMQ 优化版本)
特性:
- 零拷贝传输
- tqdm 专业进度条
- 流式和批量传输
- 会话管理
- 连接复用
"""
_active_sessions: Dict[str, Dict] = {}
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": (
"IMAGE",
{"tooltip": "Video frames to encode (BHWC format)"},
),
"encoder_address": (
"STRING",
{
"default": "tcp://10.10.0.1:5555",
"tooltip": "Remote encoder address (tcp://host:port)",
},
),
"output_path": (
"STRING",
{
"default": "/tmp/output.mp4",
"tooltip": "Output video path on encoder server",
},
),
"fps": (
"INT",
{
"default": 30,
"min": 1,
"max": 120,
"tooltip": "Video frame rate",
},
),
},
"optional": {
"audio": ("AUDIO", {"tooltip": "Optional audio track"}),
"session_mode": (
["auto", "start", "continue", "end"],
{
"default": "auto",
"tooltip": "auto: single batch | start/continue/end: multi-batch",
},
),
"total_frames": (
"INT",
{
"default": 0,
"min": 0,
"tooltip": "Total frames hint (0 = auto from batch)",
},
),
"check_network": (
"BOOLEAN",
{
"default": True,
"tooltip": "Check network connectivity before sending",
},
),
"show_progress": (
"BOOLEAN",
{"default": True, "tooltip": "Show tqdm progress bar"},
),
"transport_mode": (
["stream", "batch", "auto"],
{
"default": "auto",
"tooltip": "stream: zero-copy streaming | batch: optimized batching | auto: auto-select",
},
),
"batch_size": (
"INT",
{
"default": 10,
"min": 1,
"max": 100,
"tooltip": "Batch size for batch mode",
},
),
},
}
RETURN_TYPES = ("STRING", "STRING", "INT", "FLOAT", "FLOAT")
RETURN_NAMES = ("report", "session_id", "frames_sent", "fps_actual", "data_mb")
FUNCTION = "encode"
CATEGORY = "Remote GPU Encoding"
OUTPUT_NODE = True
def encode(
self,
images: torch.Tensor,
encoder_address: str,
output_path: str,
fps: int = 30,
audio: Any = None,
session_mode: str = "auto",
total_frames: int = 0,
check_network: bool = True,
show_progress: bool = True,
transport_mode: str = "auto",
batch_size: int = 10,
) -> Tuple[str, str, int, float, float]:
log = Logger("Encoder")
if not HAS_ZMQ:
error_msg = "ERROR: pyzmq not installed. Run: pip install pyzmq"
log.error(error_msg)
return (error_msg, "", 0, 0.0, 0.0)
# 解析图像
if len(images.shape) == 4:
num_frames, h, w, c = images.shape
else:
num_frames, h, w, c = 1, *images.shape
images = images.unsqueeze(0)
if total_frames <= 0:
total_frames = num_frames
should_start = session_mode in ("auto", "start")
should_end = session_mode in ("auto", "end")
# 解析音频
audio_info = parse_audio(audio)
has_audio = audio_info["has_audio"]
# 网络检查
if check_network:
log.info(f"Checking network: {encoder_address}")
is_valid, msg = NetworkUtils.validate_endpoint(
encoder_address, check_network=True
)
if not is_valid:
log.warning(f"Network warning: {msg}")
# 获取连接
try:
socket = ConnectionManager.get_socket(encoder_address, check_network=False)
except Exception as e:
error_msg = f"ERROR: Connection failed - {e}"
log.error(error_msg)
return (error_msg, "", 0, 0.0, 0.0)
# 会话管理
if should_start:
session_id = uuid.uuid4().bytes
self._active_sessions[encoder_address] = {
"id": session_id,
"start_time": time.time(),
"frames_sent": 0,
"bytes_sent": 0,
"audio_bytes": 0,
}
else:
session_data = self._active_sessions.get(encoder_address)
if session_data:
session_id = session_data["id"]
else:
session_id = uuid.uuid4().bytes
self._active_sessions[encoder_address] = {
"id": session_id,
"start_time": time.time(),
"frames_sent": 0,
"bytes_sent": 0,
"audio_bytes": 0,
}
session = self._active_sessions[encoder_address]
session_hex = session_id.hex()[:16]
# ========== SESSION_START ==========
if should_start:
log.header(f"Remote GPU Encoding Session (ZMQ Optimized)")
log.kv("Session", session_hex)
log.kv("Encoder", encoder_address)
log.kv("Output", output_path)
log.kv("Resolution", f"{w}×{h}")
log.kv("Frames", f"{num_frames} (total: {total_frames})")
log.kv("FPS", fps)
log.kv("Audio", f"{audio_info['duration']:.2f}s" if has_audio else "None")
log.separator()
flags = SessionFlags.HAS_AUDIO if has_audio else SessionFlags.NONE
header = VideoHeader(
msg_type=MessageType.SESSION_START,
flags=flags,
pixel_format=PixelFormat.RGB24,
width=w,
height=h,
channels=c,
total_frames=total_frames,
fps=fps,
session_id=session_id,
output_path=output_path,
)
socket.send(header.pack(), zmq.NOBLOCK)
ConnectionManager.update_stats(encoder_address, VIDEO_HEADER_SIZE)
log.success("Session started")
time.sleep(0.2)
# ========== AUDIO_DATA ==========
if has_audio and should_start and audio_info["data"]:
audio_header = AudioHeader(
audio_format=audio_info["format"],
channels=audio_info["channels"],
sample_rate=audio_info["sample_rate"],
num_samples=audio_info["samples"],
data_len=len(audio_info["data"]),
session_id=session_id,
)
socket.send_multipart(
[audio_header.pack(), audio_info["data"]], zmq.NOBLOCK
)
session["audio_bytes"] = len(audio_info["data"])
ConnectionManager.update_stats(encoder_address, len(audio_info["data"]))
log.success(f"Audio sent: {len(audio_info['data']) / 1024:.1f}KB")
time.sleep(0.1)
# ========== FRAME_DATA ==========
log.info(f"Sending {num_frames} frames...")
log.info(f"Transport mode: {transport_mode}")
send_start = time.time()
total_bytes = 0
# 自动选择传输模式
if transport_mode == "auto":
if num_frames > 50:
transport_mode = "batch"
log.info(f"Auto-selected: batch mode ({num_frames} frames)")
else:
transport_mode = "stream"
log.info(f"Auto-selected: stream mode ({num_frames} frames)")
# GPU → CPU (唯一拷贝)
images_np = (images.cpu().numpy() * 255).astype(np.uint8)
# ========== 流式传输(零拷贝)==========
if transport_mode == "stream":
frame_size = w * h * c
with tqdm(
total=num_frames,
desc="ZMQ Stream",
unit="frame",
disable=not show_progress,
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]",
) as pbar:
for i in range(num_frames):
# 零拷贝:直接使用 numpy 数组
frame_np = images_np[i]
session["frames_sent"] += 1
session["bytes_sent"] += frame_np.nbytes
total_bytes += frame_np.nbytes
header = VideoHeader(
msg_type=MessageType.FRAME_DATA,
pixel_format=PixelFormat.RGB24,
width=w,
height=h,
channels=c,
data_len=frame_np.nbytes,
frame_num=session["frames_sent"],
session_id=session_id,
total_frames=total_frames,
fps=fps,
output_path=output_path,
)
# 零拷贝:send_multipart + copy=False
socket.send_multipart([header.pack(), frame_np], flags=zmq.NOBLOCK)
ConnectionManager.update_stats(encoder_address, frame_np.nbytes)
# 更新进度条
elapsed_total = time.time() - session["start_time"]
current_fps = (
session["frames_sent"] / elapsed_total
if elapsed_total > 0
else 0
)
mb = session["bytes_sent"] / (1024 * 1024)
gbps = (mb * 8) / elapsed_total / 1000 if elapsed_total > 0 else 0
pbar.update(1)
pbar.set_postfix_str(
f"{current_fps:.1f} fps | {mb:.1f} MB | {gbps:.2f} Gbps"
)
# ========== 批量传输(优化)==========
elif transport_mode == "batch":
# 预分配批量缓冲区
frame_size = w * h * c
buffer_size = frame_size * batch_size
batch_buffer = np.zeros(buffer_size, dtype=np.uint8)
buffer_offset = 0
with tqdm(
total=num_frames,
desc="ZMQ Batch",
unit="frame",
disable=not show_progress,
bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]",
) as pbar:
for i in range(0, num_frames, batch_size):
batch_end = min(i + batch_size, num_frames)
current_batch_size = batch_end - i
# 使用预分配缓冲区
batch_buffer[: current_batch_size * frame_size] = images_np[
i:batch_end
].flatten()
start_frame = session["frames_sent"] + 1
header = VideoHeader(
msg_type=MessageType.BATCH_FRAMES,
pixel_format=PixelFormat.RGB24,
width=w,
height=h,
channels=c,
data_len=current_batch_size * frame_size,
frame_num=start_frame,
session_id=session_id,
total_frames=total_frames,
fps=fps,
output_path=output_path,
)
# 零拷贝:使用预分配缓冲区
batch_data = batch_buffer[: current_batch_size * frame_size]
socket.send_multipart(
[header.pack(), batch_data], flags=zmq.NOBLOCK
)
session["frames_sent"] += current_batch_size
session["bytes_sent"] += len(batch_data)
total_bytes += len(batch_data)
ConnectionManager.update_stats(encoder_address, len(batch_data))
# 更新进度条
elapsed_total = time.time() - session["start_time"]
current_fps = (
session["frames_sent"] / elapsed_total
if elapsed_total > 0
else 0
)
mb = session["bytes_sent"] / (1024 * 1024)
gbps = (mb * 8) / elapsed_total / 1000 if elapsed_total > 0 else 0
pbar.update(current_batch_size)
pbar.set_postfix_str(
f"{current_fps:.1f} fps | {mb:.1f} MB | {gbps:.2f} Gbps | {current_batch_size}f/batch"
)
# ========== SESSION_END ==========
if should_end:
header = VideoHeader(
msg_type=MessageType.SESSION_END,
width=w,
height=h,
channels=c,
frame_num=session["frames_sent"],
session_id=session_id,
total_frames=total_frames,
fps=fps,
output_path=output_path,
)
socket.send(header.pack(), zmq.NOBLOCK)
ConnectionManager.update_stats(encoder_address, VIDEO_HEADER_SIZE)
if encoder_address in self._active_sessions:
del self._active_sessions[encoder_address]
log.success("Session completed")
# ========== 统计 ==========
total_time = time.time() - session["start_time"]
frames_sent = session["frames_sent"]
send_time = time.time() - send_start
fps_actual = num_frames / send_time if send_time > 0 else 0
data_mb = session["bytes_sent"] / (1024 * 1024)
audio_mb = session.get("audio_bytes", 0) / (1024 * 1024)
total_mb = data_mb + audio_mb
throughput_gbps = (total_mb * 8) / total_time / 1000 if total_time > 0 else 0
status = "COMPLETED" if should_end else "IN PROGRESS"
report = f"""
┌─────────────────────────────────────────────────────────────────────┐
│ REMOTE GPU ENCODING REPORT │
│ (ZMQ Optimized Zero-Copy) │
├─────────────────────────────────────────────────────────────────────┤
│ Session: {session_hex:<54}│
│ Encoder: {encoder_address:<54}│
│ Output: {output_path:<54}│
├─────────────────────────────────────────────────────────────────────┤
│ VIDEO │
│ Resolution: {w}×{h:<51}│
│ Frames: {frames_sent}/{total_frames:<52}│
│ Speed: {fps_actual:.1f} fps{"":<49}│
├─────────────────────────────────────────────────────────────────────┤
│ AUDIO │
│ Included: {str(has_audio):<54}│
│ Size: {audio_mb:.2f} MB{"":<50}│
├─────────────────────────────────────────────────────────────────────┤
│ TRANSFER │
│ Mode: {transport_mode.upper():<50}│
│ Time: {total_time:.2f}s{"":<51}│
│ Data: {total_mb:.2f} MB{"":<50}│
│ Bandwidth: {throughput_gbps:.2f} Gbps{"":<47}│
├─────────────────────────────────────────────────────────────────────┤
│ Status: {status:<54}│
└─────────────────────────────────────────────────────────────────────┘
"""
log.separator()
log.success(
f"Transfer complete: {num_frames} frames | "
f"{fps_actual:.1f} fps | {throughput_gbps:.2f} Gbps"
)
return (
report.strip(),
session_hex,
frames_sent,
round(fps_actual, 2),
round(data_mb, 2),
)
@classmethod
def IS_CHANGED(cls, **kwargs):
return float("nan")
class RemoteEncoderConnectionOptimized:
"""远程编码器连接管理(优化版本)"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"action": (
["status", "release", "release_all", "test"],
{"default": "status", "tooltip": "Connection management action"},
),
},
"optional": {
"encoder_address": (
"STRING",
{
"default": "tcp://10.10.0.1:5555",
"tooltip": "Encoder address for release/test",
},
),
"trigger": ("*", {"tooltip": "Trigger input (any type)"}),
},
}
RETURN_TYPES = ("STRING", "BOOLEAN")
RETURN_NAMES = ("status", "success")
FUNCTION = "execute"
CATEGORY = "Remote GPU Encoding"
OUTPUT_NODE = True
def execute(
self, action: str, encoder_address: str = "", trigger: Any = None
) -> Tuple[str, bool]:
log = Logger("Connection")
if action == "release":
if not encoder_address:
return ("ERROR: No address specified", False)
ConnectionManager.release(encoder_address)
return (f"Released: {encoder_address}", True)
elif action == "release_all":
ConnectionManager.release_all()
return ("All connections released", True)
elif action == "test":
if not encoder_address:
return ("ERROR: No address specified", False)
log.info(f"Testing connection: {encoder_address}")
is_valid, msg = NetworkUtils.validate_endpoint(
encoder_address, check_network=True
)
if is_valid:
log.success(f"Connection test passed: {msg}")
return (f"OK: {msg}", True)
else:
log.warning(f"Connection test failed: {msg}")
return (f"FAILED: {msg}", False)
else:
return (ConnectionManager.get_status_string(), True)
@classmethod
def IS_CHANGED(cls, **kwargs):
return float("nan")
# ============================================================================
# 节点注册
# ============================================================================
NODE_CLASS_MAPPINGS = {
"RemoteGPUEncoderOptimized": RemoteGPUEncoderOptimized,
"RemoteEncoderConnectionOptimized": RemoteEncoderConnectionOptimized,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"RemoteGPUEncoderOptimized": "Remote GPU Encoder (ZMQ Optimized)",
"RemoteEncoderConnectionOptimized": "Encoder Connection",
}