perf: reduce prompt relay and audio ui overhead
This commit is contained in:
@@ -1,8 +1,16 @@
|
|||||||
import folder_paths
|
import folder_paths
|
||||||
|
import numpy as np
|
||||||
import os
|
import os
|
||||||
import torch
|
import torch
|
||||||
import av
|
import av
|
||||||
|
|
||||||
|
DEFAULT_SAMPLE_RATE = 44100
|
||||||
|
DEFAULT_SILENCE_SECONDS = 1
|
||||||
|
|
||||||
|
|
||||||
|
def build_silence(sample_rate: int = DEFAULT_SAMPLE_RATE, seconds: int = DEFAULT_SILENCE_SECONDS, channels: int = 2) -> torch.Tensor:
|
||||||
|
return torch.zeros((channels, int(sample_rate * seconds)), dtype=torch.float32)
|
||||||
|
|
||||||
def f32_pcm(wav: torch.Tensor) -> torch.Tensor:
|
def f32_pcm(wav: torch.Tensor) -> torch.Tensor:
|
||||||
"""Convert audio to float 32 bits PCM format."""
|
"""Convert audio to float 32 bits PCM format."""
|
||||||
if wav.dtype.is_floating_point:
|
if wav.dtype.is_floating_point:
|
||||||
@@ -23,18 +31,17 @@ def load_audio_file(filepath: str) -> tuple[torch.Tensor, int]:
|
|||||||
sr = stream.codec_context.sample_rate
|
sr = stream.codec_context.sample_rate
|
||||||
n_channels = stream.channels
|
n_channels = stream.channels
|
||||||
|
|
||||||
frames = []
|
frame_arrays = []
|
||||||
for frame in af.decode(streams=stream.index):
|
for frame in af.decode(streams=stream.index):
|
||||||
buf = torch.from_numpy(frame.to_ndarray())
|
array = frame.to_ndarray()
|
||||||
if buf.shape[0] != n_channels:
|
if array.shape[0] != n_channels:
|
||||||
buf = buf.view(-1, n_channels).t()
|
array = array.reshape(-1, n_channels).T
|
||||||
|
frame_arrays.append(array)
|
||||||
|
|
||||||
frames.append(buf)
|
if not frame_arrays:
|
||||||
|
|
||||||
if not frames:
|
|
||||||
raise ValueError("No audio frames decoded.")
|
raise ValueError("No audio frames decoded.")
|
||||||
|
|
||||||
wav = torch.cat(frames, dim=1)
|
wav = torch.from_numpy(np.concatenate(frame_arrays, axis=1))
|
||||||
wav = f32_pcm(wav)
|
wav = f32_pcm(wav)
|
||||||
return wav, sr
|
return wav, sr
|
||||||
|
|
||||||
@@ -99,17 +106,16 @@ class LoadAudioUI:
|
|||||||
missing_info = audio if audio != "none" else "None selected"
|
missing_info = audio if audio != "none" else "None selected"
|
||||||
print(f"!!! [LoadAudioUI] Warning: Audio file '{missing_info}' not found. Outputting 1 second of silence.")
|
print(f"!!! [LoadAudioUI] Warning: Audio file '{missing_info}' not found. Outputting 1 second of silence.")
|
||||||
|
|
||||||
sample_rate = 44100
|
sample_rate = DEFAULT_SAMPLE_RATE
|
||||||
# 1 second of silence (stereo) -> shape [channels, time]
|
waveform = build_silence(sample_rate)
|
||||||
waveform = torch.zeros((2, 44100))
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
waveform, sample_rate = load_audio_file(audio_path)
|
waveform, sample_rate = load_audio_file(audio_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# If decoding fails for any reason, fallback to silence rather than crashing the workflow
|
# If decoding fails for any reason, fallback to silence rather than crashing the workflow
|
||||||
print(f"!!! [LoadAudioUI] Error decoding {audio}: {e}. Falling back to silence.")
|
print(f"!!! [LoadAudioUI] Error decoding {audio}: {e}. Falling back to silence.")
|
||||||
sample_rate = 44100
|
sample_rate = DEFAULT_SAMPLE_RATE
|
||||||
waveform = torch.zeros((2, 44100))
|
waveform = build_silence(sample_rate)
|
||||||
|
|
||||||
# Convert seconds to frames
|
# Convert seconds to frames
|
||||||
start_frame = int(start_time * sample_rate)
|
start_frame = int(start_time * sample_rate)
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import types
|
import types
|
||||||
|
|
||||||
import comfy.ldm.modules.attention
|
import comfy.ldm.modules.attention
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
_DEBUG_PROMPT_RELAY = os.environ.get("PROMPT_RELAY_DEBUG", "").strip().lower() in {"1", "true", "yes", "on"}
|
||||||
|
_DEBUG_LOG_PATH = os.path.join(os.path.dirname(__file__), "debug_prompt_relay.log")
|
||||||
|
|
||||||
|
|
||||||
def _masked_attention(q, k, v, heads, mask, transformer_options={}, **kwargs):
|
def _masked_attention(q, k, v, heads, mask, transformer_options={}, **kwargs):
|
||||||
@@ -73,10 +76,10 @@ def _make_masked_override(prev_override):
|
|||||||
|
|
||||||
|
|
||||||
def debug_log(msg):
|
def debug_log(msg):
|
||||||
|
if not _DEBUG_PROMPT_RELAY:
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
import os
|
with open(_DEBUG_LOG_PATH, "a", encoding="utf-8") as f:
|
||||||
log_path = os.path.join(os.path.dirname(__file__), "debug_prompt_relay.log")
|
|
||||||
with open(log_path, "a", encoding="utf-8") as f:
|
|
||||||
f.write(msg + "\n")
|
f.write(msg + "\n")
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user