AI Hero Club

Efficient LLM Inference · Lesson 4

Marked Complete by You0 / 8

Saved in this browser only. Marking a lesson complete does not mean the project has passed verification.

Lesson Outline 9 subsections
Course Contents 8 lessons
Full course plan.
Efficient LLM Inference / Learning Plan ↗

LESSON 04 / 8Suggested study 120 minutes

Continuous Batching: State, Timelines and Falsifiable Simulations

When a short request arrives before a long one finishes, how does continuous batching change waiting and completion times?

3 Questions for This Lesson
  • Retroactive FIFO discrete dispatch and check for compliance
  • Calculated completion, waiting and utilization of static versus successive batches
  • Identifying boundaries between simulation clocks and real hardware, first words, target
Course Project ZIP ↓

The Simulated Object and Fixed Assumptions

The first three chapters have already given a forward calculation and spatial model and now include multiple arrival requests. The project in this chapter is a discrete scheduler, which does not execute Transformer forwards. Each request has a unique integer number id, arrival, and the required number of service words tokens; the length of each moment is one tock, and all active requests are for each to complete a service word. S. The word service is known as a limited workload, without prefilling, networking, sampling stop or variable calculation time, and therefore the tick is an abstract unit, not a millisecond.

This simplification allows us to isolate a mechanism: when will the empty slots be handed over to the next request after the same short and medium requests have been completed? The static FIFO retains the boundary of the group until the entire group requests are fully completed, the closing slots are empty, etc.; successive FIFO recovers the complete at the beginning of the next kick, adding the waiting person in the order of arrival and numbering. Both strategies use exactly the same input and unit service assumptions. If the continuous strategy is better performed, it can only be attributed to the difference in the use of slots in the model and cannot be relied upon to assert that the same percentage acceleration is required for successive batch processing of real equipment.

Traceable State and a Single Transition

The status consists of waiting queues, active requests and the remaining service volume, records completed and current hours that have not yet been accepted. Each Tick first handles the recovery of the previous tock, receives incoming and available requests, and then reduces each to the current operator by one and the remaining amount becomes the current tock plus one at the time of completion of the zero log. If the system does not run and the next request has not yet arrived, the clock can jump to the next time; this free space is still contained in the observation area between zero and final completion. Sorting is done only in pre-arranged fields and cannot be a secret priority for short tasks for the sake of good results.

The safety variable for scheduleing is that a number cannot be accompanied by two active copies; the request cannot start without arrival; the number of active is no more than slots; the remaining amount is not negative; each request has a final completion record. The job will always require the sum of the running slots in all Ticks equal to the sum of all requested hyphens. The model will not be cancelled or failed, so the maintenance equation should be accurately established. If future entries are cancelled, the service, cancellation and residual will have to be recorded separately, and the same completion determination cannot be maintained.

W=∑igi=∑t∣Rt∣,U=WS TendW=\sum_i g_i=\sum_t |R_t|,\quad U=\frac{W}{S\,T_{end}}
g For each requested service word, R is the pool of the right-runers and the utilization factor is calculated for the entire area from zero to final completion.

Worked Example 1: Four Requests and Two Slots

The requests arrive at zero-time, with service levels of one, four, two, and three. The static strategy runs zero and one: the first is complete, while the next three are only one until the end of the full four. Group two and three are completed from four, two in six, and three in seven. The total workload consists of ten service words, which occupy seven tocks and two slots, with a utilization rate of seven. 10/(2×7)=5/710/(2\times7)=5/7. The three empty slots left by the short request are observable waste in this model.

After the first tip of the continuous strategy, zero is released, request two for access at one point; service two times three and request three times. Request one in four and request three in six. Completion time is one, four, three and six, total six-tick. 10/12=5/610/12=5/6 From seven to six for every ten to ten, the ratio is divided by six instead of the percentage point of the difference in utilization. The waiting time is zero, zero, one, three, counting from the beginning.

Run in the Project DirectoryProject Directory Run: Checks to move to pick events and ultimately to keep constant, without reading any hardware speed.
from lab import schedule
jobs=[{'id':i,'arrival':0,'tokens':n} for i,n in enumerate([1,4,2,3])]
a=schedule(jobs,2,False);b=schedule(jobs,2,True)
assert a['makespan_ticks']==7 and b['makespan_ticks']==6
assert a['busy_slots']==b['busy_slots']==10
assert [j['finish'] for j in b['completed']]==[1,4,3,6]
print(a['utilization'],b['utilization'])

Worked Example 2: A Policy Change May Bring No Gain

If both requests arrive at three minutes, service is four and service is two slots, the two are completed from three to seven. Static is exactly the same as a continuous strategy, because there is no pre-release slot available for recovery. At zero-observed rate of eight divided by fourteen, if the first request is made one; each has a clear denominator and cannot choose the larger one without the mark. This negative result is evidence of an excessive conclusion by “It's gotta be faster with successive batches.” and checks whether the clock jumps wrongly erases the initial idleness.

The other easily missing boundary is the request to arrive at the tip border. The current agreement first recovers and admits, so that a request to reach the point equals the current tip can be joined immediately; a person who arrives before the last kick starts cannot enter backwards. If the time is dispersed from a real event, the method should be specified in advance, otherwise the time of arrival down will be reduced by artificial wait. This item uses a direct integer tick, so there is no error in coarseding the number of real seconds. The tests were accompanied by a check of late arrival, the same length and all starting moments before arrival.

Defining Waiting, Completion, and Fairness

Waiting for definition request i as wi=si−aiw_i=s_i-a_i ♪ The time to pass ♪ ri=fi−air_i=f_i-a_i, of which a, s, f services and completions are arrived, started; where f no longer indicates the initial character and avoids confusion with chapter I time symbols. This model has one since each running tock serves a word and does not take up empty spaces. ri=wi+gir_i=w_i+g_i. The completion time average does not replace the maximum waiting, and a request that is queued for a long time will be diluted. FIFO defines only the absorption order and does not automatically give any load with a limited waiting guarantee.

If the average arrival exceeds the services that the system can provide over the long term, the queue will increase; increasing the use of slots does not remove this constant constraint. It is also important to note that the same tick or multiple slots do not indicate the word characters for individual requests in parallel, as they depend on previous output for their own return. An increase in batt may improve the reuse of rights in real models and may increase the time taken per round; this simulation assumes that the wheel length does not change with the battling, so that it specializes in testing for the best quality of hardware.

The full output length is known to facilitate offline simulations, and real services tend to know only the upper limit when it actually occurs EOS Not sure. FIFO The continuous strategy does not require advance knowledge of the total length of admission, which releases the slots only after the event has been completed; but our simulator produces the completed event with predefined integer services. If the length of the study is predicted or given priority for short requests, the prediction error, maximum protection and unknown completion status should be added to the model, and compared to the baseline without future information, the real length of the forecast cannot be used to achieve an unattainable result.

Reintroducing Prefill and Space Constraints into the Real System

A true sequence of batch processing is not only decoding completed, but also the prefilling of a new hint. The long hint may take up a large round of calculations, increasing the interval between the requested output; the block prefills open and staggered the long hint, but will introduce movement selection and cache allocation. To evaluate such systems, the state also needs to record each request in prefill or decoding, the length of the handled tip, KV page, model and location identity, and the word-mechanical budget allowed for the wheel. These mechanisms were not realized in the experiment, with successive batch processing of official documents used to check concepts and project accomplishments covering only smaller discrete models.

The byte access to Chapter III should be met in conjunction with the slot access to this chapter. Both requests should not be accepted if KV is not budgeted for if the slots are empty; rather, sufficient memory does not represent sufficient computing capacity. The real system may also support the recounting or transfer of the cache, and additional service costs will need to be recorded and identity restored at this time. The final report will separate the simulator output from CPU in advance of time, and will not be delayed by an average millisecond forgery end to the end.

Standalone ProgramStandard library stand-alone event book, separated from scheduler.
jobs=[(0,0,1),(1,0,4),(2,0,2),(3,0,3)]
# An independent hand-written event ledger for the continuous case.
running=[[0,1],[1,2],[1,2],[1,3],[3],[3]]
work=sum(len(row) for row in running)
counts={i:sum(i in row for row in running) for i,_,_ in jobs}
assert work==10
assert counts=={i:n for i,_,n in jobs}
assert max(map(len,running))==2
print(work/(2*len(running)))

The first type of verification is event re-execution: check the actual number of service, start and completion times per number on a tick basis, rather than just the total length. The second category is deformation and limitation: the single request is consistent; the slots are larger than the number of requests and there is no return revenue when they arrive; the entire group of long requests does not create short-request holes. If these characteristics fail, the state transfer should be repaired before optimization is discussed.

Exercises and Project Contributions

TRY IT YOURSELF

Basis: Constantity of events

Two slots, three simultaneous arrival requests with services of 1, 3, 1 and what are the running and completion moments of FIFO successively?

Hint 1
A table is drawn in the order of arrival and access, and one unit per slot of service is completed.
Hint 2
Who will add the pick to the first short request after completion? Record the time of completion as requested, and calculate the utilization factor against the total slott.
Compare with the Solution
Tick0 runs 0, 1, zero on 1; Tick1, 1, 2, two on 2; and Tick2, only one on 3. The completion point is numbered 1, 3, 2, total number 5, utilization rate 5/6.

TRY IT YOURSELF

Step: Indicators are not synonymous

What is the average waiting and average transit time for a continuous strategy?

Hint 1
Waiting for the arrival of the front service, the flow is completed.
Hint 2
Calculated on request and checked average flow = average waiting + average service, without the final cross-counting of mean values.
Compare with the Solution
Waiting for a split of zero. 0 、 1 、 3, average 1. Flow times 1, 4 、 3 、 6 mean 3. 5. The average service is 2. 5 Satisfied. 3.5=1+2.5. The static wait is zero. 0 、 4 、 4, mean 2 cannot be extrapolated from the range of the final completion.

TRY IT YOURSELF

Challenge: Identifying invalid line conclusions

A report will simulate 6tick by one millisecond request, stating that six milliseconds are completed on the four request lines. Identify missing items and give a verifiable next level model.

Hint 1
What kind of real implementation cost model does an abstract dick need to replace a wall clock with milliseconds?
Hint 2
The column batch/length-related forwards, prefixes, queues, network and space access explain why the single request round cannot be directly multiplied by multiple requests.
Compare with the Solution
A single request is not the same length as a multiple request, and the prefill, queue, network, cache budget and stoppage are missing. The next level should actually execute the forward direction of the specified battling and length, record each active request and actual time, and prefill, decoding, and space access incident logs; before that, only six toick's abstract dispatch span can be reported.

This chapter item increment is a list of schedule and saved tick-to-do operators. It provides replayable evidence of the scheduler mechanism and does not disguise itself as a network service. Chapter V goes into quantification: unlike the Cache Equivalence and Schedule Order, it quantifys the active change in value functions, so that the error budget and quality evaluation are introduced and then measured under fixed conditions in Chapter VII. CPU Time.

Validate the Integer Domain Before Discussing Termination

Schedule first check slots is a positive integer; each job must contain id, rrival, tokens, type just int, excluding Python's bool, rrival 0 、tokens≥ 1, id only. Decimals, nan, infinity, strings, missing fields are given to ValueError immediately. tokens=1.5 Enter the original cycle, the remaining amount by 1 Decrement will pass. 0.5 、− 0.5 And never equals zero, and the completion condition never triggers. Legally limited integer input, leaving the total job. W Runs a non-empty tock to at least finish a word, so that you can use the word W Strictly drop by at least one; free time jumps directly to the next limited duty, taking on at least one mission. W Starts with a limited non-negative integer number, so the number of ticks does not exceed the total numeric weight, and each task is ultimately completed only once. This termination argument relies on the input field and does not simply demonstrate the integer number of examples to allow other values to enter.

BUILD THE MECHANISM

Successive batch processing with cessation assurance

FIFO schedule of an integer tock. id/arrival/tokens is still used for the same project task record. Download this full course and run on the project root directory; edit only learner/starter.py, with a construction framework provided by the project.

Input and Output Accurate integer excludes bool; id only, arrival>=0, tokens and slots>=1.
Maximum number of services per pick; completion time is service tick+1; free jumps to the next arrival.

Run in the Project DirectoryStudy tasks to be completed
# schedule
def schedule(jobs,slots):
    raise NotImplementedError('schedule')
Hint 1

The four order of access to services is first listed.

Hint 2

Each working tick reduces the remaining balance of the positive integer and does not allow 1.5 to cross zero.

Hint 3

Finisher ' s empty slots are refilled at the next tick; do not convert simulator tick to milliseconds.

Independent inspection .venv/bin/python -m learner.check --stage schedule --output runs/learner-schedule.json

Migration test Recalculation of the time and length distribution of arrival to predict which requests are deferred; reconciliation of total service levels with no requirement for continuous movement faster for all inputs.

Reference Implementation and Explanation
Run in the Project DirectoryReference achieved
# schedule
def schedule(jobs,slots):
 if type(slots)is not int or slots<1:raise ValueError('positive integer slots')
 rows=[];seen=set()
 for job in jobs:
  if set(job)!={'id','arrival','tokens'}or any(type(job[k])is not int for k in job):raise ValueError('exact integer id/arrival/tokens')
  if job['arrival']<0 or job['tokens']<1 or job['id']in seen:raise ValueError('invalid/duplicate job')
  seen.add(job['id']);rows.append(dict(job))
 waiting=sorted(rows,key=lambda j:(j['arrival'],j['id']));active=[];time=0;finished={};trace=[]
 while waiting or active:
  if not active and waiting:time=max(time,waiting[0]['arrival'])
  while waiting and waiting[0]['arrival']<=time and len(active)<slots:active.append(waiting.pop(0))
  served=[j['id']for j in active];trace.append({'tick':time,'served':served})
  for job in active:job['tokens']-=1
  for job in active:
   if job['tokens']==0:finished[job['id']]=time+1
  active=[j for j in active if job_remaining(j)>0];time+=1
 return {'finished':finished,'trace':trace,'makespan':time}

CHECK YOUR UNDERSTANDING

Check Your Understanding

Answer first, then open the explanation to check your reasoning. These questions help reveal gaps in understanding; independent implementation and transfer require separate evaluation.

1. What does this simulation say?

Show ExplanationTick is the discrete service unit defined in the model.
2. When's the successive FIFO filling the empty space?

Show ExplanationRecover those who have completed and accept those who have reached the waiting stage.
What should 3. Busy_slots equal to?

Show ExplanationEach running slot contributes a service word character to a precise adherence.
4. When long enough to reach the slots at the same time?

Show ExplanationWithout early release and the empty space that can be filled back, there will be no benefit from this mechanism.

Manual completion records your progress; it does not certify your project.