Running a 27B LLM Locally on a Mac: Quantization and Memory Setup Guide

    This guide covers how to run a 27B language model on a Mac. It explains how to choose a quantization level, estimate memory use and set a context length by RAM size, and deploy the model with MLX, llama.cpp, or Ollama. It also covers how to verify performance and the limits of local models.

    Bottom Line

    On a 32GB Mac, the 6-bit quantized version of a 27B model can run, but memory headroom is small. Start with a context length of 8K to 16K. Macs with 64GB or more can extend the context to 64K or longer. These figures come from memory formula estimates. Check actual usage on your own machine.

    1. Why Choose a 27B Model

    7B and 13B models are fast, but they often make mistakes when handling multi-file code or recalling detailed parameters. Larger models are more capable, but their weights take up a lot of memory. A 27B model sits between the two. It suits everyday coding, debugging error messages, and understanding screenshots on a single machine. It still trails cloud flagship models, especially in complex architecture design and niche domain knowledge.

    2. Choosing a Quantization Level

    Quantization compresses model weights from high-precision formats such as FP16 into low-bit integers. You can roughly estimate the memory needed for weights with this formula:

    Parameters (B) × bits ÷ 8 = weight size (GB)

    For a 27B model:

    • 8-bit: about 27GB. Output quality is closest to the original model, but a 32GB machine has almost no memory headroom.
    • 6-bit: about 20GB. For common coding and text tasks, the difference from 8-bit is usually small. It is a popular choice for machines with 32GB or more.
    • 4-bit: about 13.5GB. Memory pressure is lowest, but math and long chains of reasoning are more prone to errors.

    Actual files are usually somewhat larger than the formula predicts, because they include metadata, embedding layers, and quantization parameters. Whether the quality difference is noticeable depends on the task. Test the model with your own code and questions before choosing a precision.

    3. How Memory Relates to the Context Window

    The longer the context, the more memory the KV cache uses. The size of the KV cache depends on the number of model layers, the number of KV heads, the dimension of each head, the context length, and the data type of the cache. Weight size alone does not tell you whether the model will run smoothly. You also need to account for context length.

    The table below shows reference values for a 6-bit model:

    Machine Memory Weight Size Suggested Context Notes
    32GB About 20GB 8K to 16K Tight headroom. May slow down with a browser and IDE open at the same time.
    48GB About 20GB 32K Comfortable headroom.
    64GB or more About 20GB 64K or longer Can fit larger code snippets. Verify with testing.

    macOS allows the GPU to use only a portion of unified memory by default. The exact share varies by model and macOS version. Beyond that limit, the system experiences memory pressure and responses slow noticeably.

    You can quantize the KV cache to q8_0 to reduce its memory use. The trade-off is that output may change slightly, so weigh that against your task.

    4. Where the KV Cache Should Live

    The KV cache normally stays in memory. Some suggest offloading it to an SSD for better speed. This approach does not hold up. SSD read and write speeds are far lower than unified memory bandwidth, and the model accesses the cache repeatedly to generate each token. Latency increases noticeably as a result.

    When memory runs low, macOS swaps some data out to the SSD. This is a fallback that only activates under memory pressure. It prevents programs from crashing outright, but responses slow down. The better approach is to limit the context length or quantize the KV cache.

    5. Three Ways to Deploy

    Option 1: MLX

    MLX is a machine learning framework from Apple, designed for Apple Silicon with native Metal acceleration. To install it and generate text, run:

    1. pip install mlx-lm
    2. mlx_lm.generate --model <model repository or local path> --prompt "Analyze the cause of this error: ..." --max-tokens 512

    To call the model through an API, start a server compatible with the OpenAI format:

    1. mlx_lm.server --model <model repository or local path> --port 8080

    Option 2: llama.cpp

    llama.cpp uses models in GGUF format and suits users who need fine-grained control over parameters. The following command uses a local GGUF file as an example:

    1. brew install llama.cpp
    2. llama-server -m ./models/your-27b-Q6_K.gguf -c 32768 -ngl 99 --cache-type-k q8_0 --cache-type-v q8_0 --port 8080

    In this command, -c sets the context length, and -ngl 99 offloads as many layers as possible to the GPU. In some versions, quantizing the V cache also requires Flash Attention to be enabled. If the server fails to start, run llama-server --help to check the correct parameter syntax for your version.

    Option 3: Ollama

    Ollama is the simplest option and suits users who prefer not to tune parameters. Set the environment variables first, then start the service and the model:

    1. export OLLAMA_CONTEXT_LENGTH=32768
    2. export OLLAMA_KV_CACHE_TYPE=q8_0
    3. ollama serve

    In a separate terminal, run:

    1. ollama run <your imported 27B model name>

    After importing the model, run ollama show <model name> to confirm its parameter count and quantization format. Make sure it is the 27B 6-bit version.

    6. How to Verify the Deployment

    1. Before running the model, open Activity Monitor and note the current memory pressure and memory used.
    2. Ask a question using real long code or a long document. Observe the time to first token and the number of tokens generated per second.
    3. If the memory pressure graph turns yellow or red, reduce the context length or switch to a lower-bit version.
    4. If you plan to ask questions with screenshots, first confirm that the model you downloaded includes a vision encoder. Text-only versions cannot read images.

    Performance varies by machine and software version. Rely on your own test results.

    7. Scope and Risks

    Local models suit everyday coding, error analysis, code snippet refactoring, offline reference, and work involving sensitive code. For complex architecture design, niche domain questions, and detailed reasoning over very long documents, cloud models are still the better choice.

    Keep the following in mind:

    • On a 32GB machine, large context windows may cause stuttering. In severe cases, the system may force-quit some apps. Save your work before running the model.
    • Download model files only from official or trusted repositories, and verify their hash values.
    • Update inference frameworks regularly to get security fixes and compatibility improvements.