File size: 3,697 Bytes
bc24b9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Training Tokenizer Fix Plugin

Fixes: 'TrainingResearcher' object has no attribute 'tokenizer'

Problem: TrainingResearcher wraps BackgroundResearcher but doesn't expose
         the tokenizer attribute needed by LoRADataset during training.

Solution: Add tokenizer property that delegates to EVE's tokenizer.
"""

def register(orchestrator):
    """Register the tokenizer fix plugin."""
    
    eve = orchestrator.eve
    
    print("🔧 Training Tokenizer Fix Plugin loaded")
    
    # Find all TrainingResearchers
    training_researchers = [
        r for r in eve.researchers 
        if r.__class__.__name__ == 'TrainingResearcher'
    ]
    
    if not training_researchers:
        print("   ⚠️ No TrainingResearchers found - plugin not needed")
        return
    
    # Add tokenizer property to each TrainingResearcher
    for researcher in training_researchers:
        # Add tokenizer as property that delegates to EVE's tokenizer
        researcher.tokenizer = eve.tokenizer
        
    print(f"   ✅ Fixed tokenizer access for {len(training_researchers)} TrainingResearchers")
    
    # Also fix the LoRADataset issue by patching the class
    try:
        from torch.utils.data import Dataset
        
        # Get reference to LoRADataset class
        import sys
        if hasattr(sys.modules['__main__'], 'LoRADataset'):
            LoRADataset = sys.modules['__main__'].LoRADataset
            
            # Store original __getitem__
            original_getitem = LoRADataset.__getitem__
            
            def fixed_getitem(self, idx):
                """Fixed __getitem__ that handles both data formats safely."""
                item = self.data[idx]
                
                # Safely extract text fields
                user_text = ''
                assistant_text = ''
                
                # Try multiple possible keys
                if isinstance(item, dict):
                    # For training data format
                    user_text = item.get('input', item.get('topic', ''))
                    assistant_text = item.get('target', item.get('content', ''))
                    
                    # Handle None values
                    if user_text is None:
                        user_text = ''
                    if assistant_text is None:
                        assistant_text = ''
                    
                    # Ensure strings
                    user_text = str(user_text)
                    assistant_text = str(assistant_text)
                    
                    # Build prompt
                    text = f"User: {user_text}\nAssistant: {assistant_text}"
                else:
                    # Fallback for unexpected format
                    text = str(item)
                
                # Tokenize
                encodings = self.tokenizer(
                    text,
                    truncation=True,
                    max_length=self.max_length,
                    padding="max_length",
                    return_tensors="pt"
                )
                
                return {
                    "input_ids": encodings["input_ids"].squeeze(),
                    "attention_mask": encodings["attention_mask"].squeeze(),
                    "labels": encodings["input_ids"].squeeze()
                }
            
            # Monkey-patch the method
            LoRADataset.__getitem__ = fixed_getitem
            print("   ✅ Patched LoRADataset.__getitem__ for safer data handling")
            
    except Exception as e:
        print(f"   ⚠️ Could not patch LoRADataset: {e}")
    
    print("   🎯 Plugin ready - training should work now\n")