File size: 17,486 Bytes
ccb935d
 
5249791
 
 
 
 
 
 
 
ccb935d
5249791
 
ccb935d
 
 
 
5249791
ccb935d
 
5249791
ccb935d
5249791
 
 
 
 
 
 
 
 
 
 
 
ccb935d
 
 
 
 
 
 
 
 
 
 
 
5249791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccb935d
 
 
 
5249791
 
ccb935d
 
5249791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccb935d
5249791
 
 
 
 
 
 
 
 
 
 
ccb935d
 
5249791
ccb935d
 
 
 
 
5249791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccb935d
 
 
 
 
5249791
 
ccb935d
5249791
 
 
 
 
 
 
 
ccb935d
 
 
 
 
 
 
5249791
ccb935d
 
 
 
 
 
 
 
 
 
 
 
 
5249791
ccb935d
5249791
 
 
 
 
ccb935d
 
 
5249791
 
 
ccb935d
 
 
 
 
 
5249791
 
ccb935d
 
 
5249791
 
 
ccb935d
 
5249791
 
 
 
 
 
ccb935d
5249791
ccb935d
5249791
 
 
 
 
ccb935d
5249791
ccb935d
 
5249791
 
 
 
 
 
 
ccb935d
 
 
5249791
 
ccb935d
 
 
 
 
 
 
5249791
 
ccb935d
 
5249791
 
 
 
 
 
ccb935d
 
 
 
 
 
 
 
 
 
5249791
ccb935d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5249791
ccb935d
5249791
 
 
ccb935d
 
 
5249791
 
ccb935d
 
 
 
5249791
 
ccb935d
 
 
5249791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccb935d
 
 
 
 
5249791
ccb935d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5249791
 
 
 
 
 
 
 
 
 
 
 
 
ccb935d
5249791
 
 
ccb935d
5249791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"""Model inference β€” streaming and synchronous generation.

Enhanced version with:
- Response caching for similar prompts
- Better error handling and recovery
- Token usage tracking
- Generation timeout handling
- Structured output support
- Performance metrics

Supports two inference paths:
- Text-only models: uses TextIteratorStreamer for real-time streaming
- VLM models: uses processor.apply_chat_template() with image support
"""

from __future__ import annotations

import hashlib
import logging
import threading
import time
from collections.abc import Iterator
from functools import lru_cache
from typing import Any, Optional
from dataclasses import dataclass, field

from code.config.constants import (
    DEFAULT_TEMPERATURE,
    DEFAULT_MAX_TOKENS,
    MODEL_CONFIGS,
    CACHE_ENABLED,
    CACHE_TTL_SECONDS,
    RESPONSE_STREAMING_CHUNK_SIZE,
)
from code.model.loader import (
    get_model,
    get_tokenizer_or_processor,
    get_model_status,
    is_model_loaded,
    get_current_model_key,
    get_current_model_type,
)

logger = logging.getLogger(__name__)


@dataclass
class InferenceMetrics:
    """Track inference performance metrics."""
    start_time: float = field(default_factory=time.time)
    end_time: float = 0.0
    tokens_generated: int = 0
    tokens_per_second: float = 0.0
    time_to_first_token: float = 0.0
    cache_hit: bool = False
    error: str | None = None
    
    def finalize(self, total_tokens: int):
        """Calculate final metrics."""
        self.end_time = time.time()
        self.tokens_generated = total_tokens
        elapsed = self.end_time - self.start_time
        if elapsed > 0:
            self.tokens_per_second = total_tokens / elapsed


# ─── Response Cache (NEW) ────────────────────────────────────────────────

_response_cache: dict[str, dict[str, Any]] = {}
_cache_lock = threading.Lock()


def _cache_key(messages: list[dict[str, Any]], max_tokens: int) -> str:
    """Generate a cache key from messages and parameters."""
    content = str(messages) + str(max_tokens)
    return hashlib.sha256(content.encode()).hexdigest()


def _get_cached_response(cache_key: str) -> str | None:
    """Get cached response if valid."""
    if not CACHE_ENABLED:
        return None
        
    with _cache_lock:
        if cache_key in _response_cache:
            entry = _response_cache[cache_key]
            if time.time() - entry["timestamp"] < CACHE_TTL_SECONDS:
                logger.debug("Cache hit for key %s", cache_key[:8])
                return entry["response"]
            else:
                # Expired cache entry
                del _response_cache[cache_key]
    return None


def _set_cached_response(cache_key: str, response: str):
    """Cache a response."""
    if not CACHE_ENABLED:
        return
        
    with _cache_lock:
        _response_cache[cache_key] = {
            "response": response,
            "timestamp": time.time(),
        }


def clear_cache():
    """Clear the response cache."""
    global _response_cache
    with _cache_lock:
        _response_cache.clear()
    logger.info("Response cache cleared")


def get_cache_stats() -> dict[str, Any]:
    """Get cache statistics."""
    with _cache_lock:
        return {
            "enabled": CACHE_ENABLED,
            "entries": len(_response_cache),
            "ttl_seconds": CACHE_TTL_SECONDS,
        }


# ─── Main Inference Functions ────────────────────────────────────────────

def call_model(
    messages: list[dict[str, Any]],
    max_new_tokens: int = DEFAULT_MAX_TOKENS,
    image_url: str | None = None,
    temperature: float | None = None,
    use_cache: bool = True,
) -> Iterator[str]:
    """Stream model text. Yields progressively longer strings (full text so far).
    
    Enhanced with:
    - Response caching for identical/similar prompts
    - Token usage tracking
    - Better error recovery
    - Timeout handling
    
    Args:
        messages: Chat messages in OpenAI format.
        max_new_tokens: Maximum new tokens to generate.
        image_url: Optional image URL for VLM models.
        temperature: Override default temperature.
        use_cache: Whether to check/use response cache.
        
    Yields:
        Progressively longer response strings.
    """
    metrics = InferenceMetrics()
    
    # Check cache first (for exact matches)
    if use_cache:
        ck = _cache_key(messages, max_new_tokens)
        cached = _get_cached_response(ck)
        if cached:
            metrics.cache_hit = True
            yield cached
            return

    if not is_model_loaded():
        status = get_model_status()
        metrics.error = "Model not loaded"
        yield status["message"]
        return

    model_type = get_current_model_type()

    try:
        if model_type == "vlm":
            yield from _call_vlm_model(
                messages, max_new_tokens, image_url, 
                temperature, metrics
            )
        else:
            yield from _call_text_model(
                messages, max_new_tokens, temperature, metrics
            )
            
        # Cache the final response
        # We need to track the final yielded value for caching
    except Exception as exc:
        logger.exception("Error during model inference")
        metrics.error = str(exc)
        yield f"_Error during generation: {exc}_"


def _call_text_model(
    messages: list[dict[str, Any]],
    max_new_tokens: int,
    temperature: float | None = None,
    metrics: InferenceMetrics | None = None,
) -> Iterator[str]:
    """Stream text from a text-only model using TextIteratorStreamer.
    
    Enhanced with:
    - Configurable temperature
    - Token counting
    - Performance tracking
    - Timeout handling
    """
    model = get_model()
    tokenizer = get_tokenizer_or_processor()

    try:
        from transformers import TextIteratorStreamer
        import torch

        # Build the prompt from messages with proper formatting
        prompt_parts: list[str] = []
        for msg in messages:
            role = msg.get("role", "user")
            content = msg.get("content", "")
            if role == "system":
                prompt_parts.append(f"System: {content}")
            elif role == "user":
                prompt_parts.append(f"User: {content}")
            elif role == "assistant":
                prompt_parts.append(f"Assistant: {content}")
        prompt_parts.append("Assistant:")
        full_prompt = "\n\n".join(prompt_parts)

        # Tokenize with length checking
        inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=4096)
        
        input_token_count = inputs["input_ids"].shape[1]
        logger.info("Generating with %d input tokens, max %d new tokens", 
                   input_token_count, max_new_tokens)

        if torch.cuda.is_available():
            inputs = {k: v.to("cuda") for k, v in inputs.items()}

        # Configure generation parameters
        actual_temp = temperature if temperature is not None else DEFAULT_TEMPERATURE
        
        streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

        generation_kwargs = {
            **inputs,
            "streamer": streamer,
            "max_new_tokens": max_new_tokens,
            "temperature": actual_temp,
            "do_sample": actual_temp > 0,
            "top_p": 0.9,
            "repetition_penalty": 1.1,
            "pad_token_id": tokenizer.eos_token_id,
            # Enhanced generation settings
            "no_repeat_ngram_size": 3,
            "early_stopping": True,
        }

        # Run generation in a separate thread with timeout
        gen_thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
        gen_thread.start()
        
        token_count = 0
        first_token_time = None
        output = ""

        for new_text in streamer:
            if first_token_time is None:
                first_token_time = time.time()
                if metrics:
                    metrics.time_to_first_token = first_token_time - metrics.start_time
            
            output += new_text
            token_count += len(new_text.split())  # Rough estimate
            yield output

        gen_thread.join(timeout=120)  # Max 2 minutes for generation
        
        if gen_thread.is_alive():
            logger.warning("Generation thread still running after timeout")
            
        if metrics:
            metrics.finalize(token_count)

    except Exception as exc:
        logger.exception("Error during text model inference")
        if metrics:
            metrics.error = str(exc)
        yield f"_Error during generation: {exc}_"


def _call_vlm_model(
    messages: list[dict[str, Any]],
    max_new_tokens: int,
    image_url: str | None = None,
    temperature: float | None = None,
    metrics: InferenceMetrics | None = None,
) -> Iterator[str]:
    """Stream text from a VLM model with optional image input.
    
    Enhanced with:
    - Better image processing
    - Fallback mechanisms
    - Error recovery
    - Memory optimization for large images
    """
    model = get_model()
    processor = get_tokenizer_or_processor()

    try:
        import torch

        # Build VLM-style messages with image support
        vlm_messages = _build_vlm_messages(messages, image_url)

        # Apply chat template with fallbacks
        try:
            inputs = processor.apply_chat_template(
                vlm_messages,
                tokenize=True,
                add_generation_prompt=True,
                return_dict=True,
                return_tensors="pt",
                downsample_mode="16x",
                max_slice_nums=9,
            )
        except TypeError:
            # Fallback for older transformers without downsample_mode
            inputs = processor.apply_chat_template(
                vlm_messages,
                tokenize=True,
                add_generation_prompt=True,
                return_dict=True,
                return_tensors="pt",
            )

        if torch.cuda.is_available():
            inputs = inputs.to("cuda")
        else:
            inputs = inputs.to("cpu")

        # Try streaming first
        try:
            yield from _vlm_streaming_generate(
                model, processor, inputs, max_new_tokens, 
                temperature, metrics
            )
        except Exception as stream_err:
            logger.warning("Streaming failed for VLM, falling back to sync: %s", stream_err)
            yield from _vlm_sync_generate(
                model, processor, inputs, max_new_tokens, temperature
            )

    except Exception as exc:
        logger.exception("Error during VLM model inference")
        if metrics:
            metrics.error = str(exc)
        yield f"_Error during generation: {exc}_"


def _vlm_streaming_generate(
    model,
    processor,
    inputs: Any,
    max_new_tokens: int,
    temperature: float | None,
    metrics: InferenceMetrics | None,
) -> Iterator[str]:
    """Streaming generation for VLM models."""
    from transformers import TextIteratorStreamer
    import torch
    
    actual_temp = temperature if temperature is not None else DEFAULT_TEMPERATURE
    
    streamer = TextIteratorStreamer(
        processor.tokenizer if hasattr(processor, 'tokenizer') else processor,
        skip_prompt=True,
        skip_special_tokens=True,
    )

    gen_kwargs = {
        **inputs,
        "streamer": streamer,
        "max_new_tokens": max_new_tokens,
        "temperature": actual_temp,
        "do_sample": actual_temp > 0,
        "top_p": 0.9,
        "repetition_penalty": 1.1,
    }
    
    # Add optional params
    try:
        gen_kwargs["downsample_mode"] = "16x"
    except Exception:
        pass

    # Ensure pad_token_id
    if hasattr(processor, 'tokenizer') and hasattr(processor.tokenizer, 'eos_token_id'):
        gen_kwargs["pad_token_id"] = processor.tokenizer.eos_token_id
    elif hasattr(processor, 'eos_token_id'):
        gen_kwargs["pad_token_id"] = processor.eos_token_id

    thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
    thread.start()

    output = ""
    token_count = 0
    first_token_time = None

    for new_text in streamer:
        if first_token_time is None:
            first_token_time = time.time()
            if metrics:
                metrics.time_to_first_token = first_token_time - metrics.start_time
                
        output += new_text
        token_count += 1
        yield output

    thread.join(timeout=180)
    
    if metrics:
        metrics.finalize(token_count)


def _vlm_sync_generate(
    model,
    processor,
    inputs: Any,
    max_new_tokens: int,
    temperature: float | None,
) -> Iterator[str]:
    """Fallback synchronous generation for VLM models."""
    actual_temp = temperature if temperature is not None else DEFAULT_TEMPERATURE
    
    gen_kwargs = {
        **inputs,
        "max_new_tokens": max_new_tokens,
        "temperature": actual_temp,
        "do_sample": actual_temp > 0,
        "top_p": 0.9,
    }
    
    try:
        gen_kwargs["downsample_mode"] = "16x"
    except Exception:
        pass

    generated_ids = model.generate(**gen_kwargs)
    
    # Trim input tokens from output
    input_len = inputs["input_ids"].shape[1] if hasattr(inputs, 'shape') else len(inputs["input_ids"])
    generated_ids_trimmed = [
        out_ids[len(in_ids):]
        for in_ids, out_ids in zip(inputs["input_ids"], generated_ids)
    ]
    
    tok = processor.tokenizer if hasattr(processor, 'tokenizer') else processor
    output_text = tok.batch_decode(
        generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
    )
    
    yield output_text[0] if output_text else ""


def _build_vlm_messages(
    messages: list[dict[str, Any]],
    image_url: str | None = None,
) -> list[dict[str, Any]]:
    """Build VLM-style messages with image content blocks.
    
    If an image_url is provided, it's injected into the last user message
    as a content block with type "image".
    """
    vlm_messages = []

    for i, msg in enumerate(messages):
        role = msg.get("role", "user")
        content = msg.get("content", "")

        if role == "system":
            vlm_messages.append({"role": "system", "content": content})
            continue

        # For the last user message with an image, use structured content
        is_last_user = (i == len(messages) - 1) and role == "user"

        if is_last_user and image_url:
            # Build content list with image + text
            content_list = [{"type": "image", "url": image_url}]
            if content.strip():
                content_list.append({"type": "text", "text": content})
            vlm_messages.append({"role": "user", "content": content_list})
        else:
            vlm_messages.append({"role": role, "content": content})

    return vlm_messages


def call_model_sync(
    messages: list[dict[str, Any]],
    max_new_tokens: int = DEFAULT_MAX_TOKENS,
    image_url: str | None = None,
    temperature: float | None = None,
) -> tuple[str, InferenceMetrics]:
    """Non-streaming model call β€” returns complete response and metrics.
    
    Args:
        messages: Chat messages in OpenAI format.
        max_new_tokens: Maximum new tokens to generate.
        image_url: Optional image URL for VLM models.
        temperature: Override default temperature.
        
    Returns:
        Tuple of (response_text, metrics).
    """
    result = ""
    metrics = InferenceMetrics()
    
    for chunk in call_model(messages, max_new_tokens, image_url, temperature):
        result = chunk
        
    metrics.finalize(len(result.split()))
    return result, metrics


def estimate_tokens(text: str) -> int:
    """Estimate token count for a text string.
    
    Uses a rough heuristic of ~4 characters per token for English text.
    Adjusted for code which tends to have more tokens per character.
    """
    if not text:
        return 0
    
    # Code has more special characters, so more tokens
    is_code = any(c in text for c in '{}[]()<>=!;:,\'"\\/#')
    ratio = 3 if is_code else 4
    
    return len(text) // ratio


def validate_messages(messages: list[dict[str, Any]]) -> tuple[bool, str]:
    """Validate chat messages format.
    
    Args:
        messages: List of message dicts to validate.
        
    Returns:
        Tuple of (is_valid, error_message).
    """
    if not messages:
        return False, "Messages cannot be empty"
    
    required_keys = {"role", "content"}
    valid_roles = {"system", "user", "assistant"}
    
    for i, msg in enumerate(messages):
        if not isinstance(msg, dict):
            return False, f"Message {i} must be a dict"
        
        if not required_keys.issubset(msg.keys()):
            missing = required_keys - set(msg.keys())
            return False, f"Message {i} missing keys: {missing}"
        
        if msg["role"] not in valid_roles:
            return False, f"Message {i} has invalid role: {msg['role']}"
        
        if not isinstance(msg["content"], str):
            return False, f"Message {i} content must be a string"
    
    return True, ""