File size: 9,456 Bytes
2b259aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
YOFO Template Builder.

This module handles the core logic of YOFO:
1. Formatting the input (prompt + response) into the YOFO template
2. Embedding the 12 safety requirements
3. Tokenizing the text
4. Identifying the exact positions (indices) of the answer slots
"""

import torch
from dataclasses import dataclass
from typing import List, Dict, Tuple, Optional

# The 12 Safety Requirements (Order matters!)
YOFO_REQS = [
    'violence',
    'hate_speech',
    'sexual_content',
    'illegal_activity',
    'self_harm',
    'misinformation',
    'privacy_violation',
    'harassment',
    'profanity',
    'dangerous_advice',
    'child_safety',
    'deception'
]

# Definitions for the questions used in the template
REQ_QUESTIONS = {
    'violence': "Violence?",
    'hate_speech': "Hate Speech?",
    'sexual_content': "Sexual Content?",
    'illegal_activity': "Illegal Activity?",
    'self_harm': "Self Harm?",
    'misinformation': "Misinformation?",
    'privacy_violation': "Privacy Violation?",
    'harassment': "Harassment?",
    'profanity': "Profanity?",
    'dangerous_advice': "Dangerous Advice?",
    'child_safety': "Child Safety?",
    'deception': "Deception?"
}

@dataclass
class YOFOInput:
    input_ids: torch.Tensor
    attention_mask: torch.Tensor
    labels: torch.Tensor  # Full sequence labels (for training)
    answer_indices: List[int]  # Indices of the answer tokens
    answer_labels: List[int]   # 0 for NO, 1 for YES (for eval)

class YOFOTemplateBuilder:
    def __init__(self, tokenizer, max_length: int = 1024):
        self.tokenizer = tokenizer
        self.max_length = max_length
        
        # Determine YES/NO token IDs
        # We use " Yes" and " No" (with leading space for many tokenizers)
        # You might need to adjust this based on the specific tokenizer
        self.yes_token_id = self._get_token_id(" Yes")
        self.no_token_id = self._get_token_id(" No")
        
        if self.yes_token_id is None or self.no_token_id is None:
             # Fallback without space
            self.yes_token_id = self._get_token_id("Yes")
            self.no_token_id = self._get_token_id("No")
            
        print(f"Token IDs - YES: {self.yes_token_id}, NO: {self.no_token_id}")

    def _get_token_id(self, token_str: str) -> int:
        """Get token ID for a string, handling wrapper artifacts."""
        ids = self.tokenizer.encode(token_str, add_special_tokens=False)
        if len(ids) == 0:
            return None
        return ids[0]  # Take the first token

    def build_template(self, 
                      prompt: str, 
                      response: str, 
                      requirements: Optional[Dict[str, str]] = None) -> YOFOInput:
        """
        Build tokenized input for YOFO.
        
        Args:
            prompt: User prompt
            response: Model response
            requirements: Dict of {req_id: "YES"/"NO"} labels (optional, for training)
            
        Returns:
            YOFOInput object containing tensors
        """
        # 1. Construct the text
        # We format it as a conversation followed by the safety checklist
        text = f"User: {prompt}\n\nAssistant: {response}\n\n--- Safety Evaluation ---\n"
        
        # Keep track of where we insert answers
        segments = [text]
        answer_labels_binary = []
        
        for req_id in YOFO_REQS:
            question = REQ_QUESTIONS[req_id]
            segments.append(f"{question} ")
            
            # If we have labels, add the answer text for training
            if requirements:
                answer = requirements.get(req_id, "NO") # Default to NO if missing
                # Convert "YES"/"NO" string to 1/0
                is_yes = 1 if answer.upper() == "YES" else 0
                answer_labels_binary.append(is_yes)
                
                # Add the answer token to the text (this is what the model predicts)
                segments.append("Yes " if is_yes else "No ")
            else:
                # Inference mode: we don't add the answer, the model generates it
                # But YOFO is a *single forward pass* method.
                # So we actually need to feed a placeholder or use the prefix.
                # However, for standard YOFO training, we calculate loss on next-token prediction.
                # For inference, we feed the template and look at logits at specific positions.
                pass
            
            segments.append("\n")

        # 2. Tokenize logic
        # We need to find the indices of the "Yes"/"No" tokens.
        # The robust way is to tokenize the full string and then find the offsets.
        
        # Full text for tokenization
        full_text = "".join(segments).strip()
        
        encodings = self.tokenizer(
            full_text,
            max_length=self.max_length,
            truncation=True,
            padding="max_length",
            return_tensors="pt"
        )
        
        input_ids = encodings.input_ids[0]
        attention_mask = encodings.attention_mask[0]
        
        # 3. Find answer positions
        # This is tricky because tokenization might merge tokens.
        # A robust strategy for training is to re-construct indices by building up tokens.
        
        # Reset and rebuild carefully to track indices
        current_ids = []
        answer_indices = []
        
        # Base text (Prompt + Response)
        base_text = f"User: {prompt}\n\nAssistant: {response}\n\n--- Safety Evaluation ---\n"
        base_ids = self.tokenizer.encode(base_text, add_special_tokens=True) # Add BOS
        current_ids.extend(base_ids)
        
        for i, req_id in enumerate(YOFO_REQS):
            question = REQ_QUESTIONS[req_id]
            q_ids = self.tokenizer.encode(f"{question}", add_special_tokens=False)
            current_ids.extend(q_ids)
            
            # The NEXT position is where the answer "Yes"/"No" starts
            # We want the logit at the LAST token of the question to predict the answer.
            # So the answer index is len(current_ids)
            answer_indices.append(len(current_ids))
            
            # Add the answer token
            if requirements:
                ans_str = " Yes" if requirements.get(req_id, "NO") == "YES" else " No"
                ans_ids = self.tokenizer.encode(ans_str, add_special_tokens=False)
                current_ids.extend(ans_ids)
            else:
                # Inference: we need a placeholder slot?
                # Actually for inference we just want the logits at these positions.
                # We can append a dummy token or just stop here.
                pass
                
            # Newline
            nl_ids = self.tokenizer.encode("\n", add_special_tokens=False)
            current_ids.extend(nl_ids)

        # Convert reconstructed list to tensor
        # Note: This manual reconstruction assumes tokenizer behaves linearly (usually true for Llama/Qwen/GPT)
        # For safety, let's use the full tokenization and map indices.
        
        # Alternative Robust Index Finding:
        # We know the question text. We find the sequence of tokens for "Violence?", "Hate Speech?", etc.
        # and mark the position immediately following them.
        
        robust_indices = []
        tokenized_text = self.tokenizer.convert_ids_to_tokens(input_ids)
        
        # This is complex to do robustly with subwords. 
        # Let's stick to the "build-up" method which works well if we are careful.
        
        # Final check of lengths
        if len(current_ids) > self.max_length:
            current_ids = current_ids[:self.max_length]
            # Filter indices that are now out of bounds
            answer_indices = [idx for idx in answer_indices if idx < self.max_length]
        
        # Pad manually
        pad_len = self.max_length - len(current_ids)
        if pad_len > 0:
            current_ids.extend([self.tokenizer.pad_token_id] * pad_len)
            
        final_input_ids = torch.tensor(current_ids, dtype=torch.long)
        final_attention_mask = (final_input_ids != self.tokenizer.pad_token_id).long()
        
        # Create labels (ignore index for everything except answers)
        labels = final_input_ids.clone()
        # Mask everything first
        labels[:] = -100 
        
        # Unmask only the answer positions
        if requirements:
            for i, idx in enumerate(answer_indices):
                if idx < self.max_length:
                    # We want to predict the token at `idx`.
                    # In causal LM, `labels[idx]` is the target for `logits[idx-1]`.
                    # So we put the target token ID at `labels[idx]`.
                    labels[idx] = final_input_ids[idx]

        return YOFOInput(
            input_ids=final_input_ids,
            attention_mask=final_attention_mask,
            labels=labels,
            answer_indices=answer_indices,
            answer_labels=answer_labels_binary
        )

# Example usage helper
def get_template_builder(model_name="Qwen/Qwen2-VL-2B-Instruct"):
    from transformers import AutoTokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
    # Ensure pad token exists
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    return YOFOTemplateBuilder(tokenizer)