Create optimize_cpu.py

#6
by sugakrit6 - opened
Files changed (1) hide show
  1. optimize_cpu.py +163 -0
optimize_cpu.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ AICoverGen CPU Performance Optimizer
3
+ Place this file in the AICoverGen root directory and run it instead of main.py
4
+
5
+ Usage:
6
+ python optimize_cpu.py -i "song_link_or_path" -dir "model_folder_name" -p 0
7
+
8
+ This script automatically applies all CPU optimizations including:
9
+ - PyTorch threading optimization
10
+ - Faster processing parameters
11
+ - Intel MKL optimizations (if available)
12
+ """
13
+
14
+ import os
15
+ import sys
16
+ import argparse
17
+ import subprocess
18
+ import multiprocessing
19
+
20
+ # ========== CPU OPTIMIZATION SETTINGS ==========
21
+ def setup_cpu_optimizations():
22
+ """Apply CPU-specific optimizations before running"""
23
+
24
+ # Get optimal thread count (number of physical cores)
25
+ cpu_count = multiprocessing.cpu_count()
26
+
27
+ # Set PyTorch threading for better CPU performance
28
+ # These reduce overhead and improve throughput
29
+ os.environ['OMP_NUM_THREADS'] = str(cpu_count)
30
+ os.environ['MKL_NUM_THREADS'] = str(cpu_count)
31
+ os.environ['OPENBLAS_NUM_THREADS'] = str(cpu_count)
32
+ os.environ['VECLIB_MAXIMUM_THREADS'] = str(cpu_count)
33
+ os.environ['NUMEXPR_NUM_THREADS'] = str(cpu_count)
34
+
35
+ # Enable Intel MKL optimizations if available
36
+ try:
37
+ import intel_extension_for_pytorch as ipex
38
+ print("✓ Intel Extension for PyTorch detected - extra optimizations enabled!")
39
+ except ImportError:
40
+ print("ℹ Intel Extension not installed (optional). Install with: pip install intel-extension-for-pytorch")
41
+
42
+ print(f"✓ CPU optimizations applied (using {cpu_count} threads)")
43
+
44
+ # ========== OPTIMIZED PARAMETER DEFAULTS ==========
45
+ def get_optimized_args():
46
+ """Parse arguments with CPU-optimized defaults"""
47
+
48
+ parser = argparse.ArgumentParser(
49
+ description='AICoverGen with CPU optimizations',
50
+ formatter_class=argparse.RawDescriptionHelpFormatter
51
+ )
52
+
53
+ # Required arguments
54
+ parser.add_argument('-i', '--song-input', type=str, required=True,
55
+ help='YouTube link or local audio file path')
56
+ parser.add_argument('-dir', '--rvc-dirname', type=str, required=True,
57
+ help='Name of folder in rvc_models directory')
58
+ parser.add_argument('-p', '--pitch-change', type=int, required=True,
59
+ help='Pitch change in octaves: 1(male->female), -1(female->male), 0(no change)')
60
+
61
+ # Optimized optional arguments (defaults set for speed)
62
+ parser.add_argument('-k', '--keep-files', action='store_true',
63
+ help='Keep intermediate files (default: False for speed)')
64
+ parser.add_argument('-ir', '--index-rate', type=float, default=0.3,
65
+ help='Index rate (default: 0.3, lower=faster)')
66
+ parser.add_argument('-fr', '--filter-radius', type=int, default=0,
67
+ help='Median filtering (default: 0 for speed, normally 3)')
68
+ parser.add_argument('-rms', '--rms-mix-rate', type=float, default=0.25,
69
+ help='RMS mix rate (default: 0.25)')
70
+ parser.add_argument('-palgo', '--pitch-detection-algo', type=str, default='rmvpe',
71
+ choices=['rmvpe', 'mangio-crepe'],
72
+ help='Pitch detection (default: rmvpe - fastest)')
73
+ parser.add_argument('-hop', '--crepe-hop-length', type=int, default=128,
74
+ help='Crepe hop length (default: 128, higher=faster)')
75
+ parser.add_argument('-pro', '--protect', type=float, default=0.15,
76
+ help='Protect consonants (default: 0.15, lower=faster)')
77
+ parser.add_argument('-mv', '--main-vol', type=int, default=0,
78
+ help='Main vocals volume change in dB (default: 0)')
79
+ parser.add_argument('-bv', '--backup-vol', type=int, default=0,
80
+ help='Backup vocals volume change in dB (default: 0)')
81
+ parser.add_argument('-iv', '--inst-vol', type=int, default=0,
82
+ help='Instrumental volume change in dB (default: 0)')
83
+ parser.add_argument('-pall', '--pitch-change-all', type=int, default=0,
84
+ help='Pitch change for all audio in semitones (default: 0)')
85
+ parser.add_argument('-rsize', '--reverb-size', type=float, default=0.0,
86
+ help='Reverb room size (default: 0.0 for speed)')
87
+ parser.add_argument('-rwet', '--reverb-wetness', type=float, default=0.0,
88
+ help='Reverb wetness (default: 0.0 for speed)')
89
+ parser.add_argument('-rdry', '--reverb-dryness', type=float, default=0.8,
90
+ help='Reverb dryness (default: 0.8)')
91
+ parser.add_argument('-rdamp', '--reverb-damping', type=float, default=0.7,
92
+ help='Reverb damping (default: 0.7)')
93
+ parser.add_argument('-oformat', '--output-format', type=str, default='mp3',
94
+ choices=['mp3', 'wav'],
95
+ help='Output format (default: mp3 for smaller size)')
96
+
97
+ return parser.parse_args()
98
+
99
+ # ========== MAIN EXECUTION ==========
100
+ def main():
101
+ print("=" * 60)
102
+ print("AICoverGen CPU Performance Optimizer")
103
+ print("=" * 60)
104
+
105
+ # Apply CPU optimizations first
106
+ setup_cpu_optimizations()
107
+
108
+ # Parse arguments
109
+ args = get_optimized_args()
110
+
111
+ # Build command for main.py
112
+ cmd = [
113
+ sys.executable,
114
+ 'src/main.py',
115
+ '-i', args.song_input,
116
+ '-dir', args.rvc_dirname,
117
+ '-p', str(args.pitch_change),
118
+ '-ir', str(args.index_rate),
119
+ '-fr', str(args.filter_radius),
120
+ '-rms', str(args.rms_mix_rate),
121
+ '-palgo', args.pitch_detection_algo,
122
+ '-hop', str(args.crepe_hop_length),
123
+ '-pro', str(args.protect),
124
+ '-mv', str(args.main_vol),
125
+ '-bv', str(args.backup_vol),
126
+ '-iv', str(args.inst_vol),
127
+ '-pall', str(args.pitch_change_all),
128
+ '-rsize', str(args.reverb_size),
129
+ '-rwet', str(args.reverb_wetness),
130
+ '-rdry', str(args.reverb_dryness),
131
+ '-rdamp', str(args.reverb_damping),
132
+ '-oformat', args.output_format
133
+ ]
134
+
135
+ if args.keep_files:
136
+ cmd.append('-k')
137
+
138
+ print("\n📊 Optimized Settings Applied:")
139
+ print(f" • Threading: {os.environ['OMP_NUM_THREADS']} threads")
140
+ print(f" • Pitch Detection: {args.pitch_detection_algo} (fastest)")
141
+ print(f" • Filter Radius: {args.filter_radius} (0=fastest)")
142
+ print(f" • Protect: {args.protect} (lower=faster)")
143
+ print(f" • Reverb: Disabled for speed")
144
+ print(f" • Output Format: {args.output_format}")
145
+
146
+ print("\n🚀 Starting optimized processing...")
147
+ print("=" * 60)
148
+
149
+ # Run the main script with optimizations
150
+ try:
151
+ subprocess.run(cmd, check=True)
152
+ print("\n" + "=" * 60)
153
+ print("✅ Processing complete!")
154
+ print("=" * 60)
155
+ except subprocess.CalledProcessError as e:
156
+ print(f"\n❌ Error during processing: {e}")
157
+ sys.exit(1)
158
+ except KeyboardInterrupt:
159
+ print("\n\n⚠️ Processing interrupted by user")
160
+ sys.exit(0)
161
+
162
+ if __name__ == '__main__':
163
+ main()