home
diamond Go Premium
Data Engineering Path  ·  Data & AI

Running Open-Source LLMs Locally

+-------------------------------------------------------------------------+
|                  VRAM MEMORY REQUIREMENT FORMULA                        |
+-------------------------------------------------------------------------+
|  Memory (Bytes) = Parameters * (Bits per Parameter / 8) * 1.2 overhead  |
|                                                                         |
|  Example for 8 Billion Parameter Model:                                 |
|  - 16-bit (FP16): 8B * 2 bytes = 16 GB VRAM                             |
|  - 8-bit (INT8):  8B * 1 byte  = 8 GB VRAM                              |
|  - 4-bit (INT4):  8B * 0.5 byte = 4 GB VRAM                             |
+-------------------------------------------------------------------------+

Hardware Acceleration Options

Running open-source LLMs requires selecting the right accelerator device:

  1. NVIDIA GPUs (CUDA): The gold standard for production inference and fine-tuning. Supported natively by PyTorch, vLLM, and TensorRT-LLM.
  2. Apple Silicon (MPS & MLX): Apple unified memory allows Mac M-series chips (M1/M2/M3/M4) to allocate up to 128GB of RAM directly to the GPU without PCIe transfer bottlenecks.
  3. CPU Inference (llama.cpp): Fast AVX-512 quantized inference on standard commodity x86 servers.

Running with PyTorch on CUDA or Apple Silicon (MPS)

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "meta-llama/Meta-Llama-3-8B-Instruct"

# Detect best available accelerator device
if torch.cuda.is_available():
    device = "cuda"
    dtype = torch.bfloat16
elif torch.backends.mps.is_available():
    device = "mps"
    dtype = torch.float16
else:
    device = "cpu"
    dtype = torch.float32

print(f"Running inference on: {device} with {dtype}")

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=dtype,
    device_map="auto"
)

prompt = "Explain Apache Spark shuffle in two sentences."
inputs = tokenizer(prompt, return_tensors="pt").to(device)

with torch.no_grad():
    output = model.generate(**inputs, max_new_tokens=100)

print(tokenizer.decode(output[0], skip_special_tokens=True))
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.