|
| 1 | +namespace AiDotNet.Serving.Batching; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Adaptive batching strategy that dynamically adjusts batch size based on latency and throughput. |
| 5 | +/// This strategy aims to maximize throughput while maintaining latency SLAs. |
| 6 | +/// </summary> |
| 7 | +public class AdaptiveBatchingStrategy : IBatchingStrategy |
| 8 | +{ |
| 9 | + private readonly int _minBatchSize; |
| 10 | + private readonly int _maxBatchSize; |
| 11 | + private readonly int _maxWaitMs; |
| 12 | + private readonly double _targetLatencyMs; |
| 13 | + private readonly double _latencyToleranceFactor; |
| 14 | + |
| 15 | + private int _currentOptimalBatchSize; |
| 16 | + private double _recentAverageLatency; |
| 17 | + private readonly object _lock = new(); |
| 18 | + |
| 19 | + // Constants for batch size adaptation |
| 20 | + private const double SmoothingFactor = 0.3; |
| 21 | + private const int BatchSizeAdjustmentStep = 5; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the AdaptiveBatchingStrategy. |
| 25 | + /// </summary> |
| 26 | + /// <param name="minBatchSize">Minimum batch size</param> |
| 27 | + /// <param name="maxBatchSize">Maximum batch size</param> |
| 28 | + /// <param name="maxWaitMs">Maximum wait time before processing</param> |
| 29 | + /// <param name="targetLatencyMs">Target latency in milliseconds</param> |
| 30 | + /// <param name="latencyToleranceFactor">Tolerance factor for latency (e.g., 2.0 means p99 should be less than 2x p50)</param> |
| 31 | + public AdaptiveBatchingStrategy( |
| 32 | + int minBatchSize, |
| 33 | + int maxBatchSize, |
| 34 | + int maxWaitMs, |
| 35 | + double targetLatencyMs, |
| 36 | + double latencyToleranceFactor = 2.0) |
| 37 | + { |
| 38 | + _minBatchSize = minBatchSize; |
| 39 | + _maxBatchSize = maxBatchSize; |
| 40 | + _maxWaitMs = maxWaitMs; |
| 41 | + _targetLatencyMs = targetLatencyMs; |
| 42 | + _latencyToleranceFactor = latencyToleranceFactor; |
| 43 | + _currentOptimalBatchSize = minBatchSize; |
| 44 | + _recentAverageLatency = targetLatencyMs; |
| 45 | + } |
| 46 | + |
| 47 | + public string Name => "Adaptive"; |
| 48 | + |
| 49 | + public bool ShouldProcessBatch(int queuedRequests, double timeInQueueMs, double averageLatencyMs, int queueDepth) |
| 50 | + { |
| 51 | + if (queuedRequests == 0) |
| 52 | + return false; |
| 53 | + |
| 54 | + // Process if we have enough requests for optimal batch size |
| 55 | + if (queuedRequests >= _currentOptimalBatchSize) |
| 56 | + return true; |
| 57 | + |
| 58 | + // Process if we're approaching max wait time |
| 59 | + if (timeInQueueMs >= _maxWaitMs) |
| 60 | + return true; |
| 61 | + |
| 62 | + // Process if queue is building up (backpressure detection) |
| 63 | + if (queueDepth > _currentOptimalBatchSize * 2) |
| 64 | + return true; |
| 65 | + |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + public int GetOptimalBatchSize(int queuedRequests, double averageLatencyMs) |
| 70 | + { |
| 71 | + lock (_lock) |
| 72 | + { |
| 73 | + return Math.Min(Math.Min(queuedRequests, _currentOptimalBatchSize), _maxBatchSize); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void UpdatePerformanceFeedback(int batchSize, double latencyMs) |
| 78 | + { |
| 79 | + lock (_lock) |
| 80 | + { |
| 81 | + // Exponential moving average of latency |
| 82 | + _recentAverageLatency = SmoothingFactor * latencyMs + (1 - SmoothingFactor) * _recentAverageLatency; |
| 83 | + |
| 84 | + // Adapt batch size based on latency |
| 85 | + if (_recentAverageLatency < _targetLatencyMs) |
| 86 | + { |
| 87 | + // Latency is good, try increasing batch size |
| 88 | + _currentOptimalBatchSize = Math.Min(_currentOptimalBatchSize + BatchSizeAdjustmentStep, _maxBatchSize); |
| 89 | + } |
| 90 | + else if (_recentAverageLatency > _targetLatencyMs * _latencyToleranceFactor) |
| 91 | + { |
| 92 | + // Latency is too high, decrease batch size |
| 93 | + _currentOptimalBatchSize = Math.Max(_currentOptimalBatchSize - BatchSizeAdjustmentStep, _minBatchSize); |
| 94 | + } |
| 95 | + // Otherwise, keep current batch size |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments