File size: 12,687 Bytes
da76488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Example demonstrating special token usage in chain-of-thought reasoning.

This script shows how to:
1. Generate reasoning chains with special tokens
2. Parse structured reasoning from generated text
3. Use special tokens for PRM evaluation
"""

import torch
from typing import List, Dict, Tuple
from src.reasoning.step_data import ReasoningStep, ReasoningChain, StepType, SPECIAL_TOKENS
from src.reasoning.prm import ProcessRewardModel


class SpecialTokenParser:
    """Parse generated text with special tokens into structured ReasoningChain."""
    
    def __init__(self, tokenizer):
        self.tokenizer = tokenizer
        
        # Get token IDs
        self.token_ids = {
            name: tokenizer.convert_tokens_to_ids(name)
            for name in SPECIAL_TOKENS
        }
    
    def parse_reasoning_chain(self, generated_text: str, image_path: str, prompt: str) -> ReasoningChain:
        """
        Parse generated text with special tokens into a ReasoningChain.
        
        Args:
            generated_text: Generated text containing special tokens
            image_path: Path to the input image
            prompt: Original prompt
            
        Returns:
            ReasoningChain with parsed steps
        """
        steps = []
        
        # Split by reasoning_start and reasoning_end
        if "<|reasoning_start|>" not in generated_text or "<|reasoning_end|>" not in generated_text:
            # No structured reasoning found
            return ReasoningChain(
                chain_id="parsed_0",
                image_path=image_path,
                prompt=prompt,
                steps=[],
                final_answer=generated_text,
                is_correct=False
            )
        
        # Extract reasoning section
        reasoning_start = generated_text.find("<|reasoning_start|>") + len("<|reasoning_start|>")
        reasoning_end = generated_text.find("<|reasoning_end|>")
        reasoning_text = generated_text[reasoning_start:reasoning_end]
        
        # Extract answer
        answer_start = generated_text.find("<|answer_start|>") + len("<|answer_start|>")
        answer_end = generated_text.find("<|answer_end|>")
        final_answer = generated_text[answer_start:answer_end] if answer_start > 0 and answer_end > 0 else ""
        
        # Parse individual steps
        step_texts = reasoning_text.split("<|step_start|>")
        
        for i, step_text in enumerate(step_texts):
            if not step_text.strip():
                continue
            
            step = self._parse_step(step_text, i)
            if step:
                steps.append(step)
        
        return ReasoningChain(
            chain_id=f"parsed_{hash(generated_text) % 10000}",
            image_path=image_path,
            prompt=prompt,
            steps=steps,
            final_answer=final_answer.strip(),
            is_correct=False  # Will be evaluated later
        )
    
    def _parse_step(self, step_text: str, step_id: int) -> ReasoningStep:
        """Parse a single step from text."""
        try:
            # Extract step type
            step_type = StepType.INFERENCE  # default
            if "<|step_type|>" in step_text:
                type_start = step_text.find("<|step_type|>") + len("<|step_type|>")
                type_end = step_text.find("<|", type_start)
                step_type_str = step_text[type_start:type_end].strip()
                try:
                    step_type = StepType(step_type_str)
                except ValueError:
                    pass
            
            # Extract dependencies
            dependencies = []
            if "<|depends_on|>" in step_text:
                dep_start = step_text.find("<|depends_on|>") + len("<|depends_on|>")
                dep_end = step_text.find("<|", dep_start)
                deps_str = step_text[dep_start:dep_end].strip()
                dependencies = [int(d) for d in deps_str.split(",") if d.strip().isdigit()]
            
            # Extract description
            description = ""
            if "<|description_start|>" in step_text:
                desc_start = step_text.find("<|description_start|>") + len("<|description_start|>")
                desc_end = step_text.find("<|description_end|>")
                description = step_text[desc_start:desc_end].strip()
            
            # Extract confidence
            confidence = 0.5  # default
            if "<|confidence_start|>" in step_text:
                conf_start = step_text.find("<|confidence_start|>") + len("<|confidence_start|>")
                conf_end = step_text.find("<|confidence_end|>")
                try:
                    confidence = float(step_text[conf_start:conf_end].strip())
                except ValueError:
                    pass
            
            return ReasoningStep(
                step_id=step_id,
                step_type=step_type,
                description=description,
                confidence=confidence,
                dependencies=dependencies
            )
        
        except Exception as e:
            print(f"Error parsing step: {e}")
            return None


class SpecialTokenGenerator:
    """Generate text with special tokens for structured reasoning."""
    
    def __init__(self, model, tokenizer, prm: ProcessRewardModel = None):
        self.model = model
        self.tokenizer = tokenizer
        self.prm = prm
        
        # Get special token IDs
        self.reasoning_start_id = tokenizer.convert_tokens_to_ids("<|reasoning_start|>")
        self.reasoning_end_id = tokenizer.convert_tokens_to_ids("<|reasoning_end|>")
        self.step_start_id = tokenizer.convert_tokens_to_ids("<|step_start|>")
        self.step_end_id = tokenizer.convert_tokens_to_ids("<|step_end|>")
        self.ki_id = tokenizer.convert_tokens_to_ids("ки")  # PRM separator
    
    def generate_with_structure(
        self,
        prompt: str,
        image_features: torch.Tensor,
        max_steps: int = 5,
        temperature: float = 0.7,
    ) -> Tuple[str, List[float]]:
        """
        Generate reasoning chain with enforced structure and PRM evaluation.
        
        Args:
            prompt: Input prompt
            image_features: Visual features
            max_steps: Maximum reasoning steps
            temperature: Sampling temperature
            
        Returns:
            (generated_text, step_rewards)
        """
        # Tokenize prompt
        input_ids = self.tokenizer.encode(prompt, return_tensors='pt')
        
        # Start with reasoning token
        reasoning_start = torch.tensor([[self.reasoning_start_id]])
        current_ids = torch.cat([input_ids, reasoning_start], dim=1)
        
        step_rewards = []
        generated_text_parts = [prompt, " <|reasoning_start|>"]
        
        # Generate steps
        for step_num in range(max_steps):
            # Generate step with enforced structure
            step_text, step_reward = self._generate_step(
                current_ids,
                image_features,
                step_num,
                temperature
            )
            
            generated_text_parts.append(step_text)
            step_rewards.append(step_reward)
            
            # Update current_ids
            step_ids = self.tokenizer.encode(step_text, add_special_tokens=False, return_tensors='pt')
            current_ids = torch.cat([current_ids, step_ids], dim=1)
            
            # Check if reasoning should end (model generates low confidence)
            if step_reward < 0.3:
                break
        
        # End reasoning
        generated_text_parts.append(" <|reasoning_end|>")
        
        # Generate final answer
        reasoning_end = torch.tensor([[self.reasoning_end_id]])
        current_ids = torch.cat([current_ids, reasoning_end], dim=1)
        
        answer_text = self._generate_answer(current_ids, temperature)
        generated_text_parts.append(answer_text)
        
        return "".join(generated_text_parts), step_rewards
    
    def _generate_step(
        self,
        current_ids: torch.Tensor,
        image_features: torch.Tensor,
        step_num: int,
        temperature: float,
    ) -> Tuple[str, float]:
        """Generate a single reasoning step with structure."""
        # Add step_start token
        step_start = torch.tensor([[self.step_start_id]])
        step_ids = torch.cat([current_ids, step_start], dim=1)
        
        parts = [" <|step_start|>"]
        
        # Generate step type
        parts.append(" <|step_type|>")
        type_text = self._generate_until_token(step_ids, "<|", max_new_tokens=10, temperature=temperature)
        parts.append(type_text)
        
        # Generate description
        parts.append(" <|description_start|>")
        desc_text = self._generate_until_token(step_ids, "<|description_end|>", max_new_tokens=100, temperature=temperature)
        parts.append(desc_text)
        parts.append("<|description_end|>")
        
        # Generate confidence
        parts.append(" <|confidence_start|>")
        conf_text = self._generate_until_token(step_ids, "<|confidence_end|>", max_new_tokens=5, temperature=temperature)
        parts.append(conf_text)
        parts.append("<|confidence_end|>")
        
        # Add PRM separator
        parts.append(" ки")
        
        # Evaluate with PRM if available
        step_reward = 0.5
        if self.prm:
            step_text = "".join(parts)
            # TODO: Implement PRM evaluation
            # step_reward = self.prm.evaluate_step(...)
        
        parts.append(" <|step_end|>")
        
        return "".join(parts), step_reward
    
    def _generate_until_token(
        self,
        input_ids: torch.Tensor,
        stop_token: str,
        max_new_tokens: int,
        temperature: float,
    ) -> str:
        """Generate text until a specific token appears."""
        # Simplified - in practice, use proper stopping criteria
        outputs = self.model.generate(
            input_ids,
            max_new_tokens=max_new_tokens,
            temperature=temperature,
            do_sample=True,
        )
        
        generated_text = self.tokenizer.decode(outputs[0][input_ids.size(1):], skip_special_tokens=False)
        
        # Stop at token
        if stop_token in generated_text:
            generated_text = generated_text[:generated_text.find(stop_token)]
        
        return generated_text
    
    def _generate_answer(self, current_ids: torch.Tensor, temperature: float) -> str:
        """Generate final answer."""
        parts = [" <|answer_start|>"]
        
        # Generate answer text
        outputs = self.model.generate(
            current_ids,
            max_new_tokens=100,
            temperature=temperature,
        )
        
        answer_text = self.tokenizer.decode(outputs[0][current_ids.size(1):], skip_special_tokens=False)
        
        # Stop at answer_end
        if "<|answer_end|>" in answer_text:
            answer_text = answer_text[:answer_text.find("<|answer_end|>")]
        
        parts.append(answer_text)
        parts.append("<|answer_end|>")
        
        return "".join(parts)


def example_usage():
    """Example demonstrating special token usage."""
    from transformers import AutoTokenizer, AutoModel
    
    # Load model with special tokens
    model_path = "path/to/model_with_special_tokens"
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
    
    # Initialize generator
    generator = SpecialTokenGenerator(model, tokenizer)
    
    # Generate structured reasoning
    prompt = "How many red objects are in the image?"
    image_features = torch.randn(1, 768)  # Placeholder
    
    generated_text, step_rewards = generator.generate_with_structure(
        prompt=prompt,
        image_features=image_features,
        max_steps=5,
        temperature=0.7,
    )
    
    print("Generated reasoning:")
    print(generated_text)
    print(f"\nStep rewards: {step_rewards}")
    
    # Parse back into structured format
    parser = SpecialTokenParser(tokenizer)
    chain = parser.parse_reasoning_chain(
        generated_text=generated_text,
        image_path="example.jpg",
        prompt=prompt,
    )
    
    print(f"\nParsed {len(chain.steps)} steps:")
    for step in chain.steps:
        print(f"  Step {step.step_id} ({step.step_type.value}): {step.description}")
    print(f"Final answer: {chain.final_answer}")


if __name__ == "__main__":
    example_usage()