File size: 10,645 Bytes
40fd629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb616f
 
 
 
40fd629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb616f
 
 
 
 
 
 
40fd629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb616f
 
 
 
40fd629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb616f
 
 
 
 
 
 
 
40fd629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb616f
 
 
 
 
40fd629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for the unified model card system
Verifies template processing, variable substitution, and conditional sections
"""

import os
import sys
import tempfile
import shutil
from pathlib import Path

# Add the project root to the path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from scripts.model_tonic.generate_model_card import ModelCardGenerator

def test_basic_model_card():
    """Test basic model card generation without quantized models"""
    print("πŸ§ͺ Testing basic model card generation...")
    
    # Create test variables
    variables = {
        "model_name": "Test SmolLM3 Model",
        "model_description": "A test fine-tuned SmolLM3 model",
        "repo_name": "test-user/test-model",
        "base_model": "HuggingFaceTB/SmolLM3-3B",
        "dataset_name": "OpenHermes-FR",
        "training_config_type": "H100 Lightweight",
        "trainer_type": "SFTTrainer",
        "batch_size": "8",
        "gradient_accumulation_steps": "16",
        "learning_rate": "5e-6",
        "max_epochs": "3",
        "max_seq_length": "2048",
        "hardware_info": "GPU (H100)",
        "experiment_name": "test-experiment",
        "trackio_url": "https://trackio.space/test",
        "dataset_repo": "test/trackio-experiments",
        "dataset_size": "~80K samples",
        "dataset_format": "Chat format",
        "author_name": "Test User",
        "model_name_slug": "test_smollm3_model",
        "quantized_models": False,
        "dataset_sample_size": "800000",
        "training_loss": "2.1",
        "validation_loss": "2.3",
        "perplexity": "9.8"
    }
    
    try:
        # Create generator
        generator = ModelCardGenerator()
        
        # Generate model card
        content = generator.generate_model_card(variables)
        
        # Check that content was generated
        assert content is not None
        assert len(content) > 0
        
        # Check that basic sections are present
        assert "Test SmolLM3 Model" in content
        assert "test-user/test-model" in content
        assert "HuggingFaceTB/SmolLM3-3B" in content
        
        # Check that quantized sections are NOT present
        assert "Quantized Models" not in content
        assert "int8" not in content
        assert "int4" not in content
        
        # Check that metadata fields are present
        assert "library_name: transformers" in content
        assert "pipeline_tag: text-generation" in content
        assert "base_model: HuggingFaceTB/SmolLM3-3B" in content
        assert "datasets:" in content
        assert "model-index:" in content
        
        print("βœ… Basic model card generation test passed")
        return True
        
    except Exception as e:
        print(f"❌ Basic model card generation test failed: {e}")
        return False

def test_quantized_model_card():
    """Test model card generation with quantized models"""
    print("πŸ§ͺ Testing quantized model card generation...")
    
    # Create test variables with quantized models
    variables = {
        "model_name": "Test SmolLM3 Model with Quantization",
        "model_description": "A test fine-tuned SmolLM3 model with quantized versions",
        "repo_name": "test-user/test-model",
        "base_model": "HuggingFaceTB/SmolLM3-3B",
        "dataset_name": "OpenHermes-FR",
        "training_config_type": "H100 Lightweight",
        "trainer_type": "SFTTrainer",
        "batch_size": "8",
        "gradient_accumulation_steps": "16",
        "learning_rate": "5e-6",
        "max_epochs": "3",
        "max_seq_length": "2048",
        "hardware_info": "GPU (H100)",
        "experiment_name": "test-experiment",
        "trackio_url": "https://trackio.space/test",
        "dataset_repo": "test/trackio-experiments",
        "dataset_size": "~80K samples",
        "dataset_format": "Chat format",
        "author_name": "Test User",
        "model_name_slug": "test_smollm3_model",
        "quantized_models": True,
        "dataset_sample_size": "800000",
        "training_loss": "2.1",
        "validation_loss": "2.3",
        "perplexity": "9.8"
    }
    
    try:
        # Create generator
        generator = ModelCardGenerator()
        
        # Generate model card
        content = generator.generate_model_card(variables)
        
        # Check that content was generated
        assert content is not None
        assert len(content) > 0
        
        # Check that basic sections are present
        assert "Test SmolLM3 Model with Quantization" in content
        assert "test-user/test-model" in content
        
        # Check that quantized sections ARE present
        assert "Quantized Models" in content
        assert "int8" in content
        assert "int4" in content
        assert "test-user/test-model/int8" in content
        assert "test-user/test-model/int4" in content
        
        # Check that metadata fields are present
        assert "library_name: transformers" in content
        assert "pipeline_tag: text-generation" in content
        assert "base_model: HuggingFaceTB/SmolLM3-3B" in content
        assert "datasets:" in content
        assert "model-index:" in content
        assert "quantization_types:" in content
        
        print("βœ… Quantized model card generation test passed")
        return True
        
    except Exception as e:
        print(f"❌ Quantized model card generation test failed: {e}")
        return False

def test_template_processing():
    """Test template processing and variable substitution"""
    print("πŸ§ͺ Testing template processing...")
    
    try:
        # Create generator
        generator = ModelCardGenerator()
        
        # Test variable substitution
        test_variables = {
            "model_name": "Test Model",
            "repo_name": "test/repo",
            "quantized_models": True
        }
        
        # Generate content
        content = generator.generate_model_card(test_variables)
        
        # Check variable substitution
        assert "Test Model" in content
        assert "test/repo" in content
        
        # Check conditional processing
        assert "Quantized Models" in content
        
        print("βœ… Template processing test passed")
        return True
        
    except Exception as e:
        print(f"❌ Template processing test failed: {e}")
        return False

def test_file_saving():
    """Test saving generated model cards to files"""
    print("πŸ§ͺ Testing file saving...")
    
    try:
        # Create temporary directory
        with tempfile.TemporaryDirectory() as temp_dir:
            output_path = os.path.join(temp_dir, "test_readme.md")
            
            # Create generator
            generator = ModelCardGenerator()
            
            # Test variables
            variables = {
                "model_name": "Test Model",
                "model_description": "Test description",
                "repo_name": "test/repo",
                "base_model": "HuggingFaceTB/SmolLM3-3B",
                "dataset_name": "Test Dataset",
                "training_config_type": "Test Config",
                "trainer_type": "SFTTrainer",
                "batch_size": "8",
                "gradient_accumulation_steps": "16",
                "learning_rate": "5e-6",
                "max_epochs": "3",
                "max_seq_length": "2048",
                "hardware_info": "GPU",
                "experiment_name": "test-exp",
                "trackio_url": "https://trackio.space/test",
                "dataset_repo": "test/dataset",
                "dataset_size": "1K samples",
                "dataset_format": "Chat format",
                "author_name": "Test User",
                "model_name_slug": "test_model",
                        "quantized_models": False,
        "dataset_sample_size": None,
        "training_loss": "N/A",
        "validation_loss": "N/A",
        "perplexity": "N/A"
            }
            
            # Generate and save
            content = generator.generate_model_card(variables)
            success = generator.save_model_card(content, output_path)
            
            # Check that file was created
            assert success
            assert os.path.exists(output_path)
            
            # Check file content
            with open(output_path, 'r', encoding='utf-8') as f:
                saved_content = f.read()
            
            assert "Test Model" in saved_content
            assert "test/repo" in saved_content
            
            print("βœ… File saving test passed")
            return True
            
    except Exception as e:
        print(f"❌ File saving test failed: {e}")
        return False

def test_error_handling():
    """Test error handling for missing template and invalid variables"""
    print("πŸ§ͺ Testing error handling...")
    
    try:
        # Test with non-existent template
        try:
            generator = ModelCardGenerator("non_existent_template.md")
            content = generator.generate_model_card({})
            assert False, "Should have raised FileNotFoundError"
        except FileNotFoundError:
            print("βœ… Correctly handled missing template")
        
        # Test with minimal variables
        generator = ModelCardGenerator()
        content = generator.generate_model_card({})
        
        # Should still generate some content
        assert content is not None
        assert len(content) > 0
        
        print("βœ… Error handling test passed")
        return True
        
    except Exception as e:
        print(f"❌ Error handling test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("πŸš€ Starting unified model card system tests...")
    print("=" * 50)
    
    tests = [
        test_basic_model_card,
        test_quantized_model_card,
        test_template_processing,
        test_file_saving,
        test_error_handling
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"❌ Test {test.__name__} failed with exception: {e}")
    
    print("=" * 50)
    print(f"πŸ“Š Test Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("πŸŽ‰ All tests passed! Unified model card system is working correctly.")
        return 0
    else:
        print("⚠️ Some tests failed. Please check the implementation.")
        return 1

if __name__ == "__main__":
    exit(main())