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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# Copyright (c) Delanoe Pirard / Aedelon
# Licensed under the Apache License, Version 2.0
"""
Adaptive Batching Module for Depth Anything 3.

This module provides intelligent batch size selection based on available GPU memory,
model parameters, and input resolution. It maximizes throughput by dynamically
adjusting batch sizes to utilize as much GPU memory as safely possible.

Key features:
- Memory profiling for accurate estimation
- Model-specific memory coefficients
- Resolution-aware scaling
- Safety margins to prevent OOM
- Support for CUDA and MPS devices
"""
from __future__ import annotations

import gc
import math
from dataclasses import dataclass, field
from typing import Callable, Iterator, Sequence, TypeVar

import torch

from depth_anything_3.utils.logger import logger

T = TypeVar("T")


# =============================================================================
# Model Memory Profiles
# =============================================================================

@dataclass
class ModelMemoryProfile:
    """Memory profile for a specific model variant.

    Attributes:
        base_memory_mb: Fixed memory overhead (model weights, buffers)
        per_image_mb_at_504: Memory per image at 504px resolution
        activation_scale: Scaling factor for activations (quadratic with resolution)
        safety_margin: Safety margin to prevent OOM (0.0 to 1.0)
    """
    base_memory_mb: float
    per_image_mb_at_504: float
    activation_scale: float = 1.0
    safety_margin: float = 0.15  # 15% safety margin by default


# Empirically measured memory profiles for each model variant
# Values calibrated on various GPU configurations
MODEL_MEMORY_PROFILES: dict[str, ModelMemoryProfile] = {
    # Small models (ViT-S backbone)
    "da3-small": ModelMemoryProfile(
        base_memory_mb=350,
        per_image_mb_at_504=180,
        activation_scale=0.8,
    ),
    # Base models (ViT-B backbone)
    "da3-base": ModelMemoryProfile(
        base_memory_mb=800,
        per_image_mb_at_504=350,
        activation_scale=1.0,
    ),
    # Large models (ViT-L backbone)
    "da3-large": ModelMemoryProfile(
        base_memory_mb=1600,
        per_image_mb_at_504=600,
        activation_scale=1.2,
    ),
    "da3metric-large": ModelMemoryProfile(
        base_memory_mb=1700,
        per_image_mb_at_504=650,
        activation_scale=1.2,
    ),
    "da3mono-large": ModelMemoryProfile(
        base_memory_mb=1700,
        per_image_mb_at_504=650,
        activation_scale=1.2,
    ),
    # Giant models (ViT-G backbone)
    "da3-giant": ModelMemoryProfile(
        base_memory_mb=4500,
        per_image_mb_at_504=1200,
        activation_scale=1.5,
    ),
    "da3nested-giant-large": ModelMemoryProfile(
        base_memory_mb=6000,
        per_image_mb_at_504=1500,
        activation_scale=1.8,
    ),
}


# =============================================================================
# Memory Utilities
# =============================================================================

def get_available_memory_mb(device: torch.device) -> float:
    """Get available GPU memory in MB.

    Args:
        device: Target device

    Returns:
        Available memory in MB, or float('inf') for CPU
    """
    if device.type == "cuda":
        torch.cuda.synchronize(device)
        total = torch.cuda.get_device_properties(device).total_memory
        reserved = torch.cuda.memory_reserved(device)
        return (total - reserved) / (1024 * 1024)

    elif device.type == "mps":
        # MPS doesn't expose free memory directly
        # Use system memory as a rough proxy (conservative estimate)
        try:
            allocated = torch.mps.current_allocated_memory()
            # Assume 8GB available for MPS (conservative for most Apple Silicon)
            # This can be overridden via environment variable
            import os
            max_mps_memory_gb = float(os.environ.get("DA3_MPS_MAX_MEMORY_GB", "8"))
            max_mps_memory_mb = max_mps_memory_gb * 1024
            return max(0, max_mps_memory_mb - (allocated / (1024 * 1024)))
        except Exception:
            return 6000  # Conservative fallback: 6GB

    else:
        return float("inf")  # CPU has no practical limit for batching


def get_total_memory_mb(device: torch.device) -> float:
    """Get total GPU memory in MB.

    Args:
        device: Target device

    Returns:
        Total memory in MB
    """
    if device.type == "cuda":
        return torch.cuda.get_device_properties(device).total_memory / (1024 * 1024)

    elif device.type == "mps":
        import os
        return float(os.environ.get("DA3_MPS_MAX_MEMORY_GB", "8")) * 1024

    else:
        return float("inf")


# =============================================================================
# Adaptive Batch Size Calculator
# =============================================================================

@dataclass
class AdaptiveBatchConfig:
    """Configuration for adaptive batching.

    Attributes:
        min_batch_size: Minimum batch size (default: 1)
        max_batch_size: Maximum batch size cap (default: 64)
        target_memory_utilization: Target GPU memory usage (0.0 to 1.0)
        enable_profiling: Enable runtime memory profiling for calibration
        profile_warmup_batches: Number of warmup batches before profiling
    """
    min_batch_size: int = 1
    max_batch_size: int = 64
    target_memory_utilization: float = 0.85  # Use 85% of available memory
    enable_profiling: bool = True
    profile_warmup_batches: int = 2


class AdaptiveBatchSizeCalculator:
    """
    Calculates optimal batch sizes based on available GPU memory.

    This class provides intelligent batch size selection that:
    1. Estimates memory requirements based on model and resolution
    2. Measures actual memory usage during runtime (optional profiling)
    3. Adjusts batch sizes dynamically to maximize throughput

    Example:
        >>> from depth_anything_3.utils.adaptive_batching import AdaptiveBatchSizeCalculator
        >>> calc = AdaptiveBatchSizeCalculator(
        ...     model_name="da3-large",
        ...     device=torch.device("cuda"),
        ... )
        >>> # Get optimal batch size for 100 images at 518px
        >>> batch_size = calc.compute_optimal_batch_size(
        ...     num_images=100,
        ...     process_res=518,
        ... )
        >>> print(f"Optimal batch size: {batch_size}")
    """

    def __init__(
        self,
        model_name: str,
        device: torch.device,
        config: AdaptiveBatchConfig | None = None,
    ):
        """Initialize the adaptive batch size calculator.

        Args:
            model_name: Name of the DA3 model variant
            device: Target device for inference
            config: Optional configuration overrides
        """
        self.model_name = model_name
        self.device = device
        self.config = config or AdaptiveBatchConfig()

        # Get memory profile for this model
        self.profile = MODEL_MEMORY_PROFILES.get(
            model_name,
            # Fallback to large model profile for unknown models
            MODEL_MEMORY_PROFILES["da3-large"]
        )

        # Runtime calibration data
        self._measured_per_image_mb: float | None = None
        self._profiling_complete: bool = False
        self._batch_count: int = 0

    def compute_optimal_batch_size(
        self,
        num_images: int,
        process_res: int = 504,
        reserved_memory_mb: float = 0,
    ) -> int:
        """Compute optimal batch size for given workload.

        Args:
            num_images: Total number of images to process
            process_res: Processing resolution
            reserved_memory_mb: Additional memory to reserve (e.g., for other operations)

        Returns:
            Optimal batch size
        """
        # Get available memory
        available_mb = get_available_memory_mb(self.device)

        if available_mb == float("inf"):
            # CPU: return reasonable batch size based on image count
            return min(num_images, self.config.max_batch_size)

        # Apply target utilization and reserve
        usable_mb = (available_mb * self.config.target_memory_utilization) - reserved_memory_mb

        # Subtract base model memory
        usable_mb -= self.profile.base_memory_mb

        if usable_mb <= 0:
            logger.warn(
                f"Insufficient memory for model. "
                f"Available: {available_mb:.0f} MB, "
                f"Model base: {self.profile.base_memory_mb:.0f} MB"
            )
            return self.config.min_batch_size

        # Calculate per-image memory requirement
        per_image_mb = self._estimate_per_image_memory(process_res)

        # Apply safety margin
        per_image_mb *= (1 + self.profile.safety_margin)

        # Calculate optimal batch size
        optimal_batch = int(usable_mb / per_image_mb)

        # Clamp to configured bounds
        optimal_batch = max(self.config.min_batch_size, optimal_batch)
        optimal_batch = min(self.config.max_batch_size, optimal_batch)
        optimal_batch = min(num_images, optimal_batch)

        logger.debug(
            f"Adaptive batch: {optimal_batch} "
            f"(available: {available_mb:.0f} MB, "
            f"per_image: {per_image_mb:.0f} MB @ {process_res}px)"
        )

        return optimal_batch

    def _estimate_per_image_memory(self, process_res: int) -> float:
        """Estimate memory per image at given resolution.

        Memory scales approximately quadratically with resolution.

        Args:
            process_res: Processing resolution

        Returns:
            Estimated memory per image in MB
        """
        # Use measured value if available from profiling
        if self._measured_per_image_mb is not None and self._profiling_complete:
            base_per_image = self._measured_per_image_mb
        else:
            base_per_image = self.profile.per_image_mb_at_504

        # Scale quadratically with resolution
        resolution_scale = (process_res / 504) ** 2

        # Apply model-specific activation scale
        return base_per_image * resolution_scale * self.profile.activation_scale

    def update_from_profiling(self, batch_size: int, memory_used_mb: float, process_res: int) -> None:
        """Update memory estimates from actual profiling data.

        Called after inference to calibrate memory estimates.

        Args:
            batch_size: Batch size used
            memory_used_mb: Actual memory consumed
            process_res: Resolution used
        """
        if not self.config.enable_profiling:
            return

        self._batch_count += 1

        if self._batch_count <= self.config.profile_warmup_batches:
            # Skip warmup batches (memory not stable)
            return

        # Calculate per-image memory at reference resolution (504)
        resolution_scale = (process_res / 504) ** 2
        memory_per_image = (memory_used_mb - self.profile.base_memory_mb) / batch_size
        memory_at_504 = memory_per_image / resolution_scale / self.profile.activation_scale

        # Exponential moving average for stability
        alpha = 0.3
        if self._measured_per_image_mb is None:
            self._measured_per_image_mb = memory_at_504
        else:
            self._measured_per_image_mb = (
                alpha * memory_at_504 +
                (1 - alpha) * self._measured_per_image_mb
            )

        self._profiling_complete = True

        logger.debug(
            f"Profiling update: measured {memory_at_504:.0f} MB/img @ 504px "
            f"(running avg: {self._measured_per_image_mb:.0f} MB)"
        )

    def get_memory_estimate(self, batch_size: int, process_res: int) -> float:
        """Get estimated total memory for a batch.

        Args:
            batch_size: Batch size
            process_res: Processing resolution

        Returns:
            Estimated memory in MB
        """
        per_image = self._estimate_per_image_memory(process_res)
        return self.profile.base_memory_mb + (batch_size * per_image)


# =============================================================================
# Batch Iterator
# =============================================================================

@dataclass
class BatchInfo:
    """Information about a batch for processing.

    Attributes:
        batch_idx: Index of this batch (0-indexed)
        start_idx: Start index in original sequence
        end_idx: End index in original sequence (exclusive)
        items: Items in this batch
        batch_size: Size of this batch
        is_last: Whether this is the last batch
    """
    batch_idx: int
    start_idx: int
    end_idx: int
    items: list
    batch_size: int = field(init=False)
    is_last: bool = False

    def __post_init__(self):
        self.batch_size = len(self.items)


def adaptive_batch_iterator(
    items: Sequence[T],
    calculator: AdaptiveBatchSizeCalculator,
    process_res: int = 504,
    reserved_memory_mb: float = 0,
) -> Iterator[BatchInfo]:
    """
    Iterate over items with adaptive batch sizes.

    This iterator dynamically adjusts batch sizes based on available memory,
    potentially increasing throughput compared to fixed batch sizes.

    Args:
        items: Sequence of items to batch
        calculator: Adaptive batch size calculator
        process_res: Processing resolution
        reserved_memory_mb: Additional memory to reserve

    Yields:
        BatchInfo objects containing batch data and metadata

    Example:
        >>> calc = AdaptiveBatchSizeCalculator("da3-large", device)
        >>> for batch_info in adaptive_batch_iterator(images, calc, process_res=518):
        ...     result = model.inference(batch_info.items, process_res=518)
        ...     # Process result...
    """
    total = len(items)
    idx = 0
    batch_idx = 0

    while idx < total:
        remaining = total - idx

        # Compute optimal batch size for remaining items
        batch_size = calculator.compute_optimal_batch_size(
            num_images=remaining,
            process_res=process_res,
            reserved_memory_mb=reserved_memory_mb,
        )

        end_idx = min(idx + batch_size, total)
        batch_items = list(items[idx:end_idx])

        yield BatchInfo(
            batch_idx=batch_idx,
            start_idx=idx,
            end_idx=end_idx,
            items=batch_items,
            is_last=(end_idx >= total),
        )

        idx = end_idx
        batch_idx += 1


# =============================================================================
# High-Level API
# =============================================================================

def process_with_adaptive_batching(
    items: Sequence[T],
    process_fn: Callable[[list[T]], list],
    model_name: str,
    device: torch.device,
    process_res: int = 504,
    config: AdaptiveBatchConfig | None = None,
    progress_callback: Callable[[int, int], None] | None = None,
) -> list:
    """
    Process items with adaptive batching for optimal GPU utilization.

    This function handles the complete workflow of:
    1. Computing optimal batch sizes
    2. Processing batches
    3. Collecting and returning results
    4. Memory cleanup between batches

    Args:
        items: Sequence of items to process
        process_fn: Function to process a batch of items
        model_name: Name of the DA3 model
        device: Target device
        process_res: Processing resolution
        config: Optional batching configuration
        progress_callback: Optional callback(processed, total) for progress updates

    Returns:
        List of all results concatenated

    Example:
        >>> def inference_fn(batch):
        ...     return model.inference(batch, process_res=518)
        >>>
        >>> results = process_with_adaptive_batching(
        ...     items=image_paths,
        ...     process_fn=inference_fn,
        ...     model_name="da3-large",
        ...     device=torch.device("cuda"),
        ...     process_res=518,
        ... )
    """
    calculator = AdaptiveBatchSizeCalculator(
        model_name=model_name,
        device=device,
        config=config,
    )

    all_results = []
    total = len(items)

    for batch_info in adaptive_batch_iterator(items, calculator, process_res):
        # Process batch
        results = process_fn(batch_info.items)
        all_results.extend(results if isinstance(results, list) else [results])

        # Progress callback
        if progress_callback:
            progress_callback(batch_info.end_idx, total)

        # Memory cleanup between batches (except last)
        if not batch_info.is_last:
            gc.collect()
            if device.type == "cuda":
                torch.cuda.empty_cache()
            elif device.type == "mps":
                torch.mps.empty_cache()

        # Optional: profile memory usage for calibration
        if calculator.config.enable_profiling and device.type == "cuda":
            memory_used = torch.cuda.max_memory_allocated(device) / (1024 * 1024)
            calculator.update_from_profiling(
                batch_size=batch_info.batch_size,
                memory_used_mb=memory_used,
                process_res=process_res,
            )
            torch.cuda.reset_peak_memory_stats(device)

    return all_results


# =============================================================================
# Utility Functions
# =============================================================================

def estimate_max_batch_size(
    model_name: str,
    device: torch.device,
    process_res: int = 504,
    target_utilization: float = 0.85,
) -> int:
    """
    Estimate maximum batch size for a given model and resolution.

    Quick utility function for one-off batch size estimation.

    Args:
        model_name: Name of the DA3 model
        device: Target device
        process_res: Processing resolution
        target_utilization: Target memory utilization (0.0 to 1.0)

    Returns:
        Estimated maximum batch size

    Example:
        >>> max_batch = estimate_max_batch_size("da3-large", torch.device("cuda"), 518)
        >>> print(f"Max batch size at 518px: {max_batch}")
    """
    config = AdaptiveBatchConfig(target_memory_utilization=target_utilization)
    calculator = AdaptiveBatchSizeCalculator(model_name, device, config)

    # Return estimate for a large number of images
    return calculator.compute_optimal_batch_size(num_images=1000, process_res=process_res)


def log_batch_plan(
    num_images: int,
    model_name: str,
    device: torch.device,
    process_res: int = 504,
) -> None:
    """
    Log the planned batching strategy for a workload.

    Useful for debugging and understanding how images will be batched.

    Args:
        num_images: Number of images to process
        model_name: Name of the DA3 model
        device: Target device
        process_res: Processing resolution
    """
    calculator = AdaptiveBatchSizeCalculator(model_name, device)

    total_memory = get_total_memory_mb(device)
    available_memory = get_available_memory_mb(device)
    batch_size = calculator.compute_optimal_batch_size(num_images, process_res)
    num_batches = math.ceil(num_images / batch_size)
    memory_per_batch = calculator.get_memory_estimate(batch_size, process_res)

    logger.info(
        f"Batch Plan for {model_name}:\n"
        f"  Images: {num_images} @ {process_res}px\n"
        f"  Device: {device} ({total_memory:.0f} MB total, {available_memory:.0f} MB available)\n"
        f"  Batch Size: {batch_size}\n"
        f"  Num Batches: {num_batches}\n"
        f"  Est. Memory/Batch: {memory_per_batch:.0f} MB"
    )