Spaces:
Running
Running
perf: optimize CPU inference via thread limiting and inference mode usage
Browse files
app.py
CHANGED
|
@@ -21,7 +21,14 @@ from df import config
|
|
| 21 |
from df.enhance import enhance, init_df, load_audio, save_audio
|
| 22 |
from df.io import resample
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
model, df, _ = init_df("./DeepFilterNet2", config_allow_defaults=True)
|
| 26 |
model = model.to(device=device).eval()
|
| 27 |
|
|
@@ -184,7 +191,8 @@ def demo_fn(
|
|
| 184 |
|
| 185 |
logger.info("Start denoising audio")
|
| 186 |
# Call enhance with attenuation limit (atten_lim_db) to prevent over-silencing & artifacts
|
| 187 |
-
|
|
|
|
| 188 |
logger.info("Denoising finished")
|
| 189 |
|
| 190 |
# Dry/wet blending: mix original 'sample' back into 'enhanced' to restore voice texture
|
|
|
|
| 21 |
from df.enhance import enhance, init_df, load_audio, save_audio
|
| 22 |
from df.io import resample
|
| 23 |
|
| 24 |
+
# Optimize PyTorch CPU execution for faster inference
|
| 25 |
+
if torch.cuda.is_available():
|
| 26 |
+
device = torch.device("cuda")
|
| 27 |
+
else:
|
| 28 |
+
device = torch.device("cpu")
|
| 29 |
+
# Limit intra-op thread count to avoid scheduling overhead on multi-core environments
|
| 30 |
+
torch.set_num_threads(4)
|
| 31 |
+
|
| 32 |
model, df, _ = init_df("./DeepFilterNet2", config_allow_defaults=True)
|
| 33 |
model = model.to(device=device).eval()
|
| 34 |
|
|
|
|
| 191 |
|
| 192 |
logger.info("Start denoising audio")
|
| 193 |
# Call enhance with attenuation limit (atten_lim_db) to prevent over-silencing & artifacts
|
| 194 |
+
with torch.inference_mode():
|
| 195 |
+
enhanced = enhance(model, df, sample, atten_lim_db=atten_lim_db)
|
| 196 |
logger.info("Denoising finished")
|
| 197 |
|
| 198 |
# Dry/wet blending: mix original 'sample' back into 'enhanced' to restore voice texture
|