|
| 1 | +# continuous_batching.vibee |
| 2 | +# Continuous Batching for high-throughput LLM serving |
| 3 | +# Orca/vLLM style iteration-level scheduling |
| 4 | + |
| 5 | +name: continuous_batching |
| 6 | +version: "1.0.0" |
| 7 | +language: zig |
| 8 | +module: continuous_batching |
| 9 | + |
| 10 | +types: |
| 11 | + Request: |
| 12 | + description: "Inference request from client" |
| 13 | + fields: |
| 14 | + id: Int # Unique request ID |
| 15 | + prompt_tokens: List<Int> # Input token IDs |
| 16 | + max_tokens: Int # Maximum tokens to generate |
| 17 | + temperature: Float # Sampling temperature |
| 18 | + priority: Int # Request priority (higher = more urgent) |
| 19 | + created_at: Timestamp # Request creation time |
| 20 | + status: RequestStatus # Current status |
| 21 | + |
| 22 | + RequestStatus: |
| 23 | + description: "Status of a request" |
| 24 | + values: |
| 25 | + - QUEUED # Waiting in queue |
| 26 | + - PREFILL # Processing prompt |
| 27 | + - GENERATING # Generating tokens |
| 28 | + - COMPLETED # Finished generation |
| 29 | + - CANCELLED # Cancelled by client |
| 30 | + |
| 31 | + SchedulerConfig: |
| 32 | + description: "Configuration for continuous batching scheduler" |
| 33 | + fields: |
| 34 | + max_batch_size: Int # Maximum sequences in batch |
| 35 | + max_tokens_per_iter: Int # Token budget per iteration |
| 36 | + preemption_enabled: Bool # Allow preemption |
| 37 | + priority_decay: Float # Priority decay for waiting requests |
| 38 | + |
| 39 | + BatchSlot: |
| 40 | + description: "Slot in the running batch" |
| 41 | + fields: |
| 42 | + request_id: Int # Associated request |
| 43 | + seq_idx: Int # Sequence index in batch |
| 44 | + tokens_generated: Int # Tokens generated so far |
| 45 | + is_prefill: Bool # In prefill phase |
| 46 | + |
| 47 | +behaviors: |
| 48 | + - name: submit_request |
| 49 | + given: request queue, new request |
| 50 | + when: client submits inference request |
| 51 | + then: adds request to queue with priority |
| 52 | + |
| 53 | + - name: schedule_iteration |
| 54 | + given: running batch, request queue, token budget |
| 55 | + when: starting new iteration |
| 56 | + then: returns batch configuration for this iteration |
| 57 | + |
| 58 | + - name: process_iteration |
| 59 | + given: model, batch configuration |
| 60 | + when: running one iteration |
| 61 | + then: processes all sequences, returns generated tokens |
| 62 | + |
| 63 | + - name: handle_completion |
| 64 | + given: completed sequence, request queue |
| 65 | + when: sequence finishes generation |
| 66 | + then: removes from batch, adds new request if available |
| 67 | + |
| 68 | + - name: preempt_sequence |
| 69 | + given: running sequence, higher priority request |
| 70 | + when: preemption needed |
| 71 | + then: pauses sequence, saves state, schedules new request |
| 72 | + |
| 73 | +# Architecture: |
| 74 | +# |
| 75 | +# ┌─────────────────────────────────────────────────────────────┐ |
| 76 | +# │ CONTINUOUS BATCHING SCHEDULER │ |
| 77 | +# ├─────────────────────────────────────────────────────────────┤ |
| 78 | +# │ │ |
| 79 | +# │ REQUEST QUEUE (Priority Heap) │ |
| 80 | +# │ ┌─────┬─────┬─────┬─────┬─────┐ │ |
| 81 | +# │ │ R5 │ R3 │ R7 │ R1 │ R9 │ (sorted by priority) │ |
| 82 | +# │ └──┬──┴──┬──┴──┬──┴─────┴─────┘ │ |
| 83 | +# │ │ │ │ │ |
| 84 | +# │ ▼ ▼ ▼ │ |
| 85 | +# │ RUNNING BATCH (max_batch_size slots) │ |
| 86 | +# │ ┌─────┬─────┬─────┬─────┐ │ |
| 87 | +# │ │ S0 │ S1 │ S2 │ S3 │ (active sequences) │ |
| 88 | +# │ │ R5 │ R3 │ R7 │ --- │ (--- = empty slot) │ |
| 89 | +# │ └──┬──┴──┬──┴──┬──┴─────┘ │ |
| 90 | +# │ │ │ │ │ |
| 91 | +# │ ▼ ▼ ▼ │ |
| 92 | +# │ ┌─────────────────────────────────────────┐ │ |
| 93 | +# │ │ MODEL FORWARD PASS │ │ |
| 94 | +# │ │ (process all active sequences) │ │ |
| 95 | +# │ └─────────────────────────────────────────┘ │ |
| 96 | +# │ │ |
| 97 | +# │ ITERATION LOOP: │ |
| 98 | +# │ 1. Check for completed sequences → free slots │ |
| 99 | +# │ 2. Fill empty slots from queue │ |
| 100 | +# │ 3. Run forward pass for all active sequences │ |
| 101 | +# │ 4. Sample next tokens │ |
| 102 | +# │ 5. Check stopping conditions │ |
| 103 | +# │ 6. Repeat │ |
| 104 | +# │ │ |
| 105 | +# └─────────────────────────────────────────────────────────────┘ |
| 106 | +# |
| 107 | +# Throughput Improvement: |
| 108 | +# Static batching: Wait for slowest sequence |
| 109 | +# Continuous batching: Fill slots immediately |
| 110 | +# |
| 111 | +# Example (batch_size=4, requests with varying lengths): |
| 112 | +# Static: [====][====][==][======] → 6 iterations wasted |
| 113 | +# Continuous: [====][====][==][======] |
| 114 | +# [ ][ ][++][ ] → new requests fill gaps |
| 115 | +# |
| 116 | +# Throughput gain: 30-50% typical, up to 3x under high load |
0 commit comments