Zero Labs
Industry News / AI

From Cloud APIs to Running Fine-Tuned AI Models on Your Own Hardware

Source · Towards AI By Gaurangi

From the publisher’s feed, collected in the ZeroLabs newsroom.

What if I tell you, that $500 monthly API bill is optional. So is the “We need a GPU server to run this model”.The engineers who know about quantisation and LoRA are quietly running the same models at a tenth of the cost, on hardware they already own. The ones who don’t are signing cloud invoices.I was in the second group until recently and now am documenting my learnings and findings.Learn these two things and your relationship with AI infrastructure changes permanently. Your costs drop. Your latency drops. Your dependence on a cloud provider’s uptime, pricing changes, and data policies is out of question.Part-1 was about how SLMs work under the hood. Part 2 is where we go hands-on.We’re going to shrink models, customise them all on your own hardware.Quantisation — The very first thing to understand before running any SLMConsider a scenario where you are storing a list of numbers. Each number is precise to be 15 decimal places. Now imagine someone asks you if you actually need 15 decimal places here, or would 2 places be fine?This question builds the entire background behind quantisation.Any model — at its core, is a collection of billions of numbers known as weights. Each weight is a floating number which by default, is stored in 32 or 16 bit format. That precision is great for training in cases where tiny differences in values matters to a great extent. But for purpose like generating text, most of this precision is wasted. The model doesn’t know the difference between 0.318847291 versus 0.319 .Quantisation is the process of deliberately reducing the precision to save memory and speed up the inference. Instead of a 32-bit weight, an INT8 or INT4 is used.The model weights themselves occupy roughly 14GB in FP16, 7GB in INT8 and 3.5GB in INT4. This is what creates the difference between “needs a cloud GPU” and “runs on your Mac”.But hold on, there is a catch. Compression always comes with a cost. Rounding numbers leads to information loss which further means slightly worse outputs. This loss is much smaller than most people expect.INT8 loses almost nothing on most tasks. INT4 loses a bit more, but for majority of real-world use cases ie. summarisation, classification, QnA and code generation, the difference is also tending to zero to a human reader. This percision-quality trade off is worth it for half the hardware requirements.Visual — How Quantisation WorksPlay with the widget below to see this better. You can visualise what happens to a real weight value as you quantise it down. Drag the weight value to something large (around 2) and see how INT4 spikes. Then drag it to zero and see the error. Most weights in trained model lie around zero, which is exactly where INT4 is the most accurate.Quantisation Precision ExplorerIf the weight value is large (eg. 0.318), INT4 rounding is evident (error: 0.0510). Quantisation hurts more at extreme values. INT8 keeps error at 0.0043. Thus, a better trade-off for quality-sensitive tasks. It would still save 75% memory than 32-bit format.Which format you’ll actually encounterUnderstanding INT4 and INT8 is in theory. In practise, when one downloads a quantised model, you’ll see format nomenclatures like GGUF and AWQ .GPT-Generated Unified Format (GGUF) is a container. It can compared to mp4 for video. It is not the compression algo, it is the standardised packaging that wraps the quantised weights, tokeniser and everything else that the model needs as a single file.phi-3-mini-4k-instruct-q4_K_M.gguf ; here the q4 denotes INT4quantisation. K_M refers to the specific quantisation scheme variant.Activation-Aware Weight Quantisation (AWQ), unlike GGUF is about the method itself. In AWQ, not all weights are equally important, a small fraction of them have a disproportionate impact on model quality. Standard INT4 quantisation rounds all the weights the same way. AWQ identifies critical weights first, and protects them. This results in INT4Models that behave pretty much like their 16-bit variant.Quick Cheat SheetRunning locally on CPU or Mac :- look for GGUF filesRunning on a GPU or production serving :- Look for AWQ (or GPTQ is a reliable fallback)Best quality and have the memory :- Use the original FP16 ModelLoRA — How to teach an old SLM new tricks without redoing itA 7B model has 7 billion numbers that define how it behaves. Full fine-tuning means updating all of them during training. For a 7B model in 16-bit precision, just holding the weights take around 14GB. The gradients and optimiser are roughly 3–4x that ie. 50–60GB memory at the least. This ain’t consumer GPU. This is a server.Low-Rank Adaption (LoRA) a 2021 paper by Microsoft changed this completely.AnalogyThink about a musician who wants to learn a new style. He doesn’t need to unlearn everything and starts fresh — he just needs to adapt a relatively a small set of adjustments.Base Knowledge remains frozen. Only the difference gets trained. That’s LoRA. The pre-trained model weights stay completely frozen. You don’t touch them. Instead, you inject a small set of new matrices alongside them. When the model runs, the frozen weights and the small trained matrices are combined, and the result behaves like a model that was specifically trained for your task.The mathsEvery weight matrix has a shape. a weight martix W might be 4096x4096 ie. 16 million params in one matrix.LoRA says instead of updating W directly, represent the change a product of two much smaller matrices. Instead of a 4096x4096 matrices, you use a 4096 x r and r x 4096 matrix, where r is a rank. Rank is a small number typically between 4 and 64. 8 is the sweet spot used in most published LoRA fine-tunes.At r = 8, two matrices together would have :- 4096*8 + 8*4096 = 65536 parameters which is around 0.4% of the original 16M params.LoRA reduces training requirements enough that many fine-tuning workloads move from multi-GPU servers to a single high-end consumer GPU.Play with the widget below. Drag the rank slider and watch exactly what happens to parameter count and memory.LoRA Rank VisualiserThe key thing to notice in the widget is how W is frozen. Matrices A & B are small and the one thing getting updated. At rank 8 on a 1024-dimensional model across 16 layers, you go from fine-tuning 16M parameters to 262K. The W matrix doesn’t change at all — the model just learns to apply a consistent delta on top of it.What does a LoRA fine-tune actually look like in code?from transformers import AutoModelForCausalLMmodel = AutoModelForCausalLM.from_pretrained( "microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)from peft import LoraConfig, get_peft_modelconfig = LoraConfig( r=8, lora_alpha=16, target_modules=[ "qkv_proj", "o_proj", "gate_up_proj", "down_proj" ], lora_dropout=0.05, bias="none", task_type="CAUSAL_LM")model = get_peft_model(model, config)model.print_trainable_parameters()#trainable params: 12,582,912 || all params: 3,833,662,464 || trainable%: 0.3282216970888755That last line is the one worth sitting with. 0.3% of the model’s parameters are being trained. The other 99.7% are frozen. And the result, on a domain-specific task, is a model that behaves as if it were trained specifically for that task from the start.How to choose rank?There is no hard and fast rule to choose rank. For format or style adaptation which includes teaching the model to always respond in a specific structure, tone, or language, rank 4 to 8 is typically sufficient. For domain knowledge tasks ie. medical, legal, a specific codebase, rank 16 to 32 gives the model more capacity to store new associations. Going above rank 64 rarely adds quality and starts to erode the memory benefits that made LoRA worth using in the first place.The practical advice; Start at rank 8. If your evaluation metrics plateau and the model clearly isn’t capturing domain-specific patterns, double it and retrain. Rank selection is an empirical question ; The widget above builds the intuition, but your dataset is the final judge.Small NoteIn practice, these two ideas are often combined through QLoRA, where a quantised base model remains frozen while LoRA adapters are trained on top. This dramatically reduces memory requirements while retaining most of the benefits of fine-tuning.You now own the full stack!! Almost.Six months ago, running a fine-tuned language model on own hardware felt like something that only research labs did.Now you know that a 7B model in INT4 fits in 3.5GB. But there’s one piece missing.Quantisation made the model small enough to run. LoRA made it yours. Neither of them solves the hardest problem in production AI: how do you give a model knowledge it was never trained on, without retraining it at all? Your internal documents. Your codebase. Your company’s knowledge base from the last five years. A model — no matter how well fine-tuned — has a knowledge cutoff and a context window.That’s what Part 3 is about. We’ll build the full pipeline from scratch: embeddings, vector search, context injection, and a working local RAG system using Ollama and a model you already know how to run and customise.If you found this useful, a clap helps more engineers find it. And if Part 1 — the architecture tricks behind SWA and GQA — is still in your reading list, that’s the place to start. Engage in the comments for any discussions. Thank you for your time!From Cloud APIs to Running Fine-Tuned AI Models on Your Own Hardware was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.