-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-python-incremental.py
More file actions
182 lines (142 loc) · 6.96 KB
/
Copy pathtest-python-incremental.py
File metadata and controls
182 lines (142 loc) · 6.96 KB
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
#!/usr/bin/env python3
"""
Test script to demonstrate Python incremental training functionality
Shows how the system skips already processed PRs and generates .pkl files
"""
import os
import sys
import time
import json
from datetime import datetime
from codeowners_ml_system import CodeownersMLPredictor
def test_incremental_training():
"""Test the incremental training functionality"""
print("🐍 Testing Python Incremental Training (.pkl generation)")
print("=" * 60)
# Configuration
REPO_OWNER = "tenstorrent"
REPO_NAME = "tt-metal"
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
if not GITHUB_TOKEN:
print("❌ Please set GITHUB_TOKEN environment variable")
print(" export GITHUB_TOKEN='your_token_here'")
return
# Test parameters
TEST_PR_COUNT = 20 # Small count for testing
print(f"📊 Testing with {TEST_PR_COUNT} PRs from {REPO_OWNER}/{REPO_NAME}")
print()
# Initialize predictor
predictor = CodeownersMLPredictor()
# Test 1: Fresh training
print("🧪 Test 1: Fresh Training (should process all PRs)")
print("-" * 50)
# Clear any existing training log
if os.path.exists('python_training_log.json'):
os.remove('python_training_log.json')
print("🗑️ Cleared existing training log")
start_time = time.time()
try:
# First training run - should process all PRs
print(f"⏰ Starting first training run at {datetime.now().strftime('%H:%M:%S')}")
# Note: Using months=1 to limit data for testing
summary1 = predictor.train(REPO_OWNER, REPO_NAME, GITHUB_TOKEN, months=1)
first_run_time = time.time() - start_time
print(f"⏰ First run completed in {first_run_time:.1f}s")
# Save the model
model_path = 'test_codeowners_model.pkl'
predictor.save_model(model_path)
print(f"💾 Model saved to {model_path}")
# Display summary
print("\n📊 First Run Summary:")
print(f" - Trained groups: {summary1['trained_groups']}")
print(f" - Total samples: {summary1['total_samples']}")
print(f" - Training date: {summary1['training_date']}")
# Test 2: Incremental training (should skip all PRs)
print("\n🧪 Test 2: Incremental Training (should skip all PRs)")
print("-" * 50)
# Create new predictor instance to simulate fresh start
predictor2 = CodeownersMLPredictor()
start_time2 = time.time()
print(f"⏰ Starting second training run at {datetime.now().strftime('%H:%M:%S')}")
# Second training run - should skip all PRs
summary2 = predictor2.train(REPO_OWNER, REPO_NAME, GITHUB_TOKEN, months=1)
second_run_time = time.time() - start_time2
print(f"⏰ Second run completed in {second_run_time:.1f}s")
# Display summary
print("\n📊 Second Run Summary:")
print(f" - Trained groups: {summary2['trained_groups']}")
print(f" - Total samples: {summary2['total_samples']}")
print(f" - Message: {summary2.get('message', 'N/A')}")
# Test 3: Clear training log and partial retrain
print("\n🧪 Test 3: Clear Log and Retrain (should process all PRs again)")
print("-" * 50)
# Clear training log
if os.path.exists('python_training_log.json'):
os.remove('python_training_log.json')
print("🗑️ Cleared training log")
# Create new predictor instance
predictor3 = CodeownersMLPredictor()
start_time3 = time.time()
print(f"⏰ Starting third training run at {datetime.now().strftime('%H:%M:%S')}")
# Third training run - should process all PRs again
summary3 = predictor3.train(REPO_OWNER, REPO_NAME, GITHUB_TOKEN, months=1)
third_run_time = time.time() - start_time3
print(f"⏰ Third run completed in {third_run_time:.1f}s")
# Display summary
print("\n📊 Third Run Summary:")
print(f" - Trained groups: {summary3['trained_groups']}")
print(f" - Total samples: {summary3['total_samples']}")
print(f" - Training date: {summary3['training_date']}")
# Final comparison
print("\n🎯 Performance Comparison:")
print("=" * 50)
print(f"🚀 First run (all PRs): {first_run_time:.1f}s")
print(f"⚡ Second run (skip): {second_run_time:.1f}s")
print(f"🔄 Third run (all PRs): {third_run_time:.1f}s")
speed_improvement = ((first_run_time - second_run_time) / first_run_time) * 100
print(f"📈 Speed improvement: {speed_improvement:.1f}%")
# Test model prediction
if os.path.exists(model_path):
print("\n🔮 Testing Model Prediction:")
print("-" * 30)
# Load the model
test_predictor = CodeownersMLPredictor()
test_predictor.load_model(model_path)
# Test files
test_files = [
"tt_metal/hw/inc/tensix.h",
"tests/ttnn/python_api_testing/sweep_tests/test_configs/ci_sweep_config.yaml",
"docs/source/ttnn/ttnn/usage.rst"
]
predictions = test_predictor.predict_approvers(test_files, top_k=3)
print(f"🎯 Predictions for {len(test_files)} files:")
for i, pred in enumerate(predictions[:3], 1):
print(f" {i}. {pred['approver']}: {pred['confidence']:.1f}%")
print("\n✅ Incremental Training Test Completed Successfully!")
print("🎉 Key Features Demonstrated:")
print(" - ✅ Tracks processed PRs in python_training_log.json")
print(" - ✅ Skips already processed PRs (massive speed improvement)")
print(" - ✅ Generates real ML models (.pkl files)")
print(" - ✅ Preserves training state between runs")
print(" - ✅ Can be reset by clearing training log")
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()
finally:
# Cleanup test files
cleanup_files = ['test_codeowners_model.pkl', 'python_training_log.json']
for file in cleanup_files:
if os.path.exists(file):
os.remove(file)
print(f"🗑️ Cleaned up {file}")
if __name__ == "__main__":
print("🔧 Make sure you have GITHUB_TOKEN set:")
print(" export GITHUB_TOKEN='your_token_here'")
print()
if len(sys.argv) > 1 and sys.argv[1] == '--run':
test_incremental_training()
else:
print("📋 This script will test Python incremental training functionality")
print(" It will make 3 training runs to demonstrate PR skipping")
print(" Run with: python test-python-incremental.py --run")