Serving a fine-tuned model
9 min read
From trained adapter to running service
After training you have either full tuned weights or a small adapter. Serving means putting them behind an endpoint that your application calls. There are three decisions to make, and each affects quality, speed and cost.
- Merge or keep separate. Whether the adapter is folded into the weights.
- Precision. How many bits each weight takes.
- Hardware and runtime. What serves requests and how many at a time.
Merge the adapter or keep it separate
A LoRA adapter adds a low-rank update to chosen weight matrices, as How LoRA works showed. You can load the base and the adapter separately, which lets one base serve several adapters, or merge the update into the weights. Merging removes the extra step at inference. The program below checks that the merge changes nothing it should not: the output of the base plus adapter equals the output of the merged matrix, up to rounding.
base = [[0.50, -0.20, 0.10, 0.00],
[0.30, 0.40, -0.10, 0.20],
[-0.10, 0.20, 0.60, 0.10],
[0.00, 0.10, -0.30, 0.50]]
down = [[0.2, -0.1, 0.0, 0.3], [0.1, 0.2, -0.2, 0.0]]
up = [[0.5, 0.1], [-0.3, 0.2], [0.0, 0.4], [0.2, -0.1]]
scale = 2.0
x = [1.0, -2.0, 0.5, 1.5]
def matvec(matrix, vector):
return [sum(w * v for w, v in zip(row, vector)) for row in matrix]
adapter_path = matvec(base, x)
low_rank = matvec(up, matvec(down, x))
separate = [a + scale * b for a, b in zip(adapter_path, low_rank)]
delta = [[scale * sum(up[i][k] * down[k][j] for k in range(2)) for j in range(4)] for i in range(4)]
merged_matrix = [[b + d for b, d in zip(brow, drow)] for brow, drow in zip(base, delta)]
merged = matvec(merged_matrix, x)
print("separate:", [round(v, 4) for v in separate])
print("merged: ", [round(v, 4) for v in merged])
print(f"largest difference: {max(abs(a - b) for a, b in zip(separate, merged)):.0e}")It prints:
separate: [1.72, -0.92, -0.37, 0.82]
merged: [1.72, -0.92, -0.37, 0.82]
largest difference: 2e-16The two paths agree to within about 2e-16, which is floating-point rounding, so the merge is safe in exact arithmetic. In real serving the weights are stored at limited precision, which can change a merged result slightly. Merging into a quantised base can be lossy, so check the output and merge into a full-precision copy where the library recommends it. Keep the original adapter and base. The merged copy is a build output you can recreate.
Precision and memory
Each weight takes 2 bytes at 16 bits, 1 byte at 8 bits and half a byte at 4 bits, as Quantisation and QLoRA explained. The weights are not the only memory use. Each request in flight also needs a key-value cache, which grows with the length of the conversation. The program below estimates how many 2,000-token requests fit on a 24 GB GPU at two precisions, and what that does to the cost of a request. The latency and price numbers are assumptions for the arithmetic.
params = 7_000_000_000
layers, width = 32, 4096
tokens_per_request = 2000
gpu_gb, overhead_gb = 24, 2
gpu_cost_per_hour = 1.50
seconds_per_request = 0.3 + 150 * 0.03
kv_per_token = 2 * layers * width * 2
kv_gb = kv_per_token * tokens_per_request / 1e9
print(f"KV cache: {kv_per_token:,} bytes a token, {kv_gb:.2f} GB for one request")
for name, bytes_per_weight in [("16-bit", 2), ("4-bit", 0.5)]:
weights_gb = params * bytes_per_weight / 1e9
concurrent = int((gpu_gb - overhead_gb - weights_gb) // kv_gb)
per_hour = concurrent * 3600 / seconds_per_request
cost = gpu_cost_per_hour / per_hour * 1000
print(f"{name}: weights {weights_gb:.1f} GB, {concurrent} requests at once, ${cost:.2f} per 1,000 requests")It prints:
KV cache: 524,288 bytes a token, 1.05 GB for one request
16-bit: weights 14.0 GB, 7 requests at once, $0.29 per 1,000 requests
4-bit: weights 3.5 GB, 17 requests at once, $0.12 per 1,000 requestsCutting the weights from 14 GB to 3.5 GB leaves room for 17 requests at once instead of 7, and that cuts the cost from about $0.29 to $0.12 per 1,000 requests. It may also change answers, so measure quality on the fixed evaluation set from Evaluating a fine-tuned model before you accept the saving. A serving runtime such as vLLM handles the batching and cache management for you. The numbers above show why it matters.
Before you go live
- Same prompt and tokeniser. Serve with the chat template and tokeniser used in training and evaluation. A mismatch is a common cause of poor results.
- Format checks. If the model returns JSON, validate it, as Structured output and tool calls shows.
- Latency under load. Test with realistic input and output lengths and concurrent users. Record the 95th percentile as well as the average.
- Costs beyond compute. Include idle capacity, storage, network traffic and logging.
- Rollback. Keep the previous model serving until the new one passes your checks.
- ConvertMerge or export, and choose the precision.
- Re-testRun the fixed evaluation set on the converted model.
- Load testMeasure latency and throughput at realistic lengths.
- CanarySend a small share of traffic to the new model.
- MonitorWatch quality, latency and cost, and keep the old version ready.
What should you do after merging or quantising?
The converted model loads fine.
Conversion can change answers, so measure again.
What limits concurrent requests on a GPU?
You size a server.
The cache grows with the length of each request.
Why keep the previous model deployed?
A new release is going live.
A quick rollback limits the damage of a bad release.
Your reading position is saved
0 of 36 complete
Lesson complete
Serving a fine-tuned model
Up next