File size: 9,300 Bytes
ad7badd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script to validate the multi-LoRA logic without requiring PyTorch dependencies
"""
import sys
import os

# Add the current directory to the Python path
sys.path.insert(0, '/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning')

def test_lora_config():
    """Test LoRA configuration system"""
    print("Testing LoRA configuration system...")
    
    # Read the app.py file and extract the LORA_CONFIG
    with open('/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning/app.py', 'r') as f:
        content = f.read()
    
    # Check if LORA_CONFIG is defined
    if 'LORA_CONFIG = {' not in content:
        print("❌ LORA_CONFIG not found in app.py")
        return False
    
    # Check for required LoRA entries
    required_loras = [
        "None",
        "InStyle (Style Transfer)",
        "InScene (In-Scene Editing)",
        "Face Segmentation",
        "Object Remover"
    ]
    
    for lora_name in required_loras:
        if f'"{lora_name}"' not in content:
            print(f"❌ Missing LoRA: {lora_name}")
            return False
        print(f"βœ… Found LoRA: {lora_name}")
    
    print("βœ… LoRA configuration test passed!")
    return True

def test_lora_manager_structure():
    """Test LoRA manager class structure"""
    print("\nTesting LoRA manager class structure...")
    
    try:
        # Read the lora_manager.py file
        with open('/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning/lora_manager.py', 'r') as f:
            content = f.read()
        
        # Check for required methods
        required_methods = [
            'def __init__',
            'def register_lora',
            'def configure_lora',
            'def load_lora',
            'def unload_lora',
            'def fuse_lora',
            'def get_lora_ui_config'
        ]
        
        for method in required_methods:
            if method not in content:
                print(f"❌ Missing method: {method}")
                return False
            print(f"βœ… Found method: {method}")
        
        # Check for required attributes
        required_attributes = [
            'self.lora_registry',
            'self.lora_configurations',
            'self.current_lora'
        ]
        
        for attr in required_attributes:
            if attr not in content:
                print(f"❌ Missing attribute: {attr}")
                return False
            print(f"βœ… Found attribute: {attr}")
        
        print("βœ… LoRA manager structure test passed!")
        return True
        
    except Exception as e:
        print(f"❌ LoRA manager test failed: {e}")
        return False

def test_ui_functions():
    """Test UI-related function existence"""
    print("\nTesting UI functions...")
    
    try:
        # Read the app.py file
        with open('/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning/app.py', 'r') as f:
            content = f.read()
        
        # Check for required UI functions
        required_functions = [
            'def on_lora_change(',
            'def infer(',
            'def load_and_fuse_lora('
        ]
        
        for func in required_functions:
            if func not in content:
                print(f"❌ Missing function: {func}")
                return False
            print(f"βœ… Found function: {func}")
        
        # Check for Gradio components
        required_components = [
            'gr.Dropdown',
            'gr.Image',
            'gr.Textbox',
            'gr.Button',
            'gr.Accordion'
        ]
        
        for component in required_components:
            if component not in content:
                print(f"❌ Missing component: {component}")
                return False
            print(f"βœ… Found component: {component}")
        
        print("βœ… UI functions test passed!")
        return True
        
    except Exception as e:
        print(f"❌ UI function test failed: {e}")
        return False

def test_dynamic_ui_logic():
    """Test the dynamic UI visibility logic"""
    print("\nTesting dynamic UI visibility logic...")
    
    try:
        # Read the app.py file
        with open('/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning/app.py', 'r') as f:
            content = f.read()
        
        # Check for style vs edit logic
        if 'config["type"] == "style"' not in content:
            print("❌ Missing style vs edit type checking")
            return False
        print("βœ… Found style vs edit type checking")
        
        # Check for visibility logic
        if 'visible=not is_style_lora' not in content and 'visible=is_style_lora' not in content:
            print("❌ Missing visibility logic for components")
            return False
        print("βœ… Found visibility logic for components")
        
        # Check for prompt template handling
        if 'config["prompt_template"]' not in content:
            print("❌ Missing prompt template handling")
            return False
        print("βœ… Found prompt template handling")
        
        print("βœ… Dynamic UI logic test passed!")
        return True
        
    except Exception as e:
        print(f"❌ Dynamic UI logic test failed: {e}")
        return False

def test_lora_fusion_methods():
    """Test LoRA fusion method implementations"""
    print("\nTesting LoRA fusion methods...")
    
    try:
        # Read the app.py file
        with open('/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning/app.py', 'r') as f:
            content = f.read()
        
        # Check for fusion methods
        required_methods = [
            'load_lora_weights',
            'fuse_lora',
            'unfuse_lora'
        ]
        
        for method in required_methods:
            if method not in content:
                print(f"❌ Missing fusion method: {method}")
                return False
            print(f"βœ… Found fusion method: {method}")
        
        # Check for manual fusion implementation
        if 'fuse_lora_manual' not in content:
            print("❌ Missing manual fusion function")
            return False
        print("βœ… Found manual fusion function")
        
        # Check for different fusion methods support
        if 'config["method"] == "standard"' not in content or 'config["method"] == "manual_fuse"' not in content:
            print("❌ Missing support for different fusion methods")
            return False
        print("βœ… Found support for different fusion methods")
        
        print("βœ… LoRA fusion methods test passed!")
        return True
        
    except Exception as e:
        print(f"❌ LoRA fusion methods test failed: {e}")
        return False

def test_memory_management():
    """Test memory management features"""
    print("\nTesting memory management...")
    
    try:
        # Read the app.py file
        with open('/config/workspace/hf/Qwen-Image-Edit-2509-Turbo-Lightning/app.py', 'r') as f:
            content = f.read()
        
        # Check for garbage collection
        required_cleanups = [
            'gc.collect()',
            'torch.cuda.empty_cache()'
        ]
        
        for cleanup in required_cleanups:
            if cleanup not in content:
                print(f"⚠️  Missing cleanup: {cleanup}")
            else:
                print(f"βœ… Found cleanup: {cleanup}")
        
        # Check for state reset
        if 'load_state_dict' not in content:
            print("⚠️  Missing state reset logic")
        else:
            print("βœ… Found state reset logic")
        
        print("βœ… Memory management test passed!")
        return True
        
    except Exception as e:
        print(f"❌ Memory management test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("=" * 60)
    print("Multi-LoRA Implementation Logic Validation")
    print("=" * 60)
    
    tests = [
        test_lora_config,
        test_lora_manager_structure,
        test_ui_functions,
        test_dynamic_ui_logic,
        test_lora_fusion_methods,
        test_memory_management
    ]
    
    passed = 0
    failed = 0
    
    for test in tests:
        try:
            if test():
                passed += 1
            else:
                failed += 1
        except Exception as e:
            print(f"❌ {test.__name__} failed with exception: {e}")
            failed += 1
    
    print("\n" + "=" * 60)
    print(f"Test Results: {passed} passed, {failed} failed")
    print("=" * 60)
    
    if failed == 0:
        print("πŸŽ‰ All tests passed! Multi-LoRA implementation logic is correct.")
        print("\nKey Features Verified:")
        print("βœ… Multi-LoRA configuration system")
        print("βœ… LoRA manager with all required methods")
        print("βœ… Dynamic UI component visibility")
        print("βœ… Support for different LoRA types (style vs edit)")
        print("βœ… Multiple fusion methods (standard and manual)")
        print("βœ… Memory management and cleanup")
        return True
    else:
        print("⚠️ Some tests failed. Please check the implementation.")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)