Claude Code for Apple MLX: LLM Inference on Apple Silicon — Claude Skills 360 Blog
Blog / AI / Claude Code for Apple MLX: LLM Inference on Apple Silicon
AI

Claude Code for Apple MLX: LLM Inference on Apple Silicon

Published: September 28, 2027
Read time: 5 min read
By: Claude Skills 360

MLX runs ML models natively on Apple Silicon with unified memory. pip install mlx mlx-lm. import mlx.core as mx, import mlx.nn as nn. Arrays: a = mx.array([1.0, 2.0, 3.0]) — lives on GPU via Metal by default. mx.zeros((4, 4)), mx.random.normal((512,)). Operations lazy by default: mx.eval(result) triggers computation. Gradient: grad_fn = mx.grad(loss_fn), grads = grad_fn(model, x, y). loss, grads = mx.value_and_grad(loss_fn)(model, x, y). Module: class MLP(nn.Module): def __init__(self): super().__init__(); self.l1=nn.Linear(784,256); self.l2=nn.Linear(256,10). def __call__(self, x): return self.l2(nn.relu(self.l1(x))). Compile: mx.compile(fn) — JIT traces and caches the computation graph. Optimizer: opt = mlx.optimizers.AdamW(learning_rate=1e-3). opt.update(model, grads). mx.eval(model.parameters(), opt.state). LLM inference: from mlx_lm import load, generate. model, tokenizer = load("mlx-community/Meta-Llama-3.1-8B-Instruct-4bit"). response = generate(model, tokenizer, prompt="Hello!", max_tokens=200, verbose=True). Fine-tuning with LoRA: mlx_lm.lora --model mlx-community/Mistral-7B-v0.1-4bit --train --data data/ --batch-size 4 --lora-layers 4. Quantize: python -m mlx_lm.convert --hf-path meta-llama/Llama-3.2-3B-Instruct --mlx-path mlx-llama3 --quantize --q-bits 4. mx.quantize(layer, bits=4) quantizes Linear layer weights. Batch inference: mlx_lm.server --model mlx-community/Llama-3.2-3B-Instruct-4bit --port 8080 serves OpenAI-compatible HTTP endpoint. Claude Code generates MLX model definitions, training loops, LoRA fine-tuning scripts, quantization pipelines, and inference servers for Apple Silicon.

CLAUDE.md for Apple MLX

## Apple MLX Stack
- Version: mlx >= 0.18, mlx-lm >= 0.18 (Apple Silicon only — M1/M2/M3/M4)
- Arrays: mx.array / mx.zeros/ones/random.normal — lazy eval, call mx.eval() to materialize
- Grad: mx.grad(fn) or mx.value_and_grad(fn) — pure functions over module trees
- Module: nn.Module subclass with __call__; mx.compile(fn) for JIT
- Optimizer: mlx.optimizers.AdamW(lr); opt.update(model, grads); mx.eval(model, opt.state)
- LLM: mlx_lm.load(model_id) → generate(model, tokenizer, prompt, max_tokens)
- Quantize: mlx_lm.convert --quantize --q-bits 4 (4-bit, ~2GB for 7B)
- LoRA: mlx_lm.lora --model path --train --data dir/ --lora-layers 4

MLX Neural Network Training

# models/mlx_train.py — MLX neural network training on Apple Silicon
from __future__ import annotations
import time
from functools import partial
from pathlib import Path
from typing import Generator

import mlx.core as mx
import mlx.nn as nn
import mlx.optimizers as optim
import numpy as np


# ── 1. Model definitions ──────────────────────────────────────────────────────

class MLP(nn.Module):
    """Multi-layer perceptron — tabular classification or regression."""

    def __init__(self, dims: list[int], dropout: float = 0.1):
        super().__init__()
        self.layers  = [nn.Linear(dims[i], dims[i+1]) for i in range(len(dims)-2)]
        self.out     = nn.Linear(dims[-2], dims[-1])
        self.dropout = nn.Dropout(p=dropout)

    def __call__(self, x: mx.array, training: bool = True) -> mx.array:
        for layer in self.layers:
            x = nn.relu(layer(x))
            if training:
                x = self.dropout(x)
        return self.out(x)


class TransformerBlock(nn.Module):
    """Single Transformer block with pre-norm."""

    def __init__(self, d_model: int, num_heads: int, mlp_ratio: int = 4):
        super().__init__()
        self.norm1 = nn.LayerNorm(d_model)
        self.norm2 = nn.LayerNorm(d_model)
        self.attn  = nn.MultiHeadAttention(d_model, num_heads)
        self.ff1   = nn.Linear(d_model, d_model * mlp_ratio)
        self.ff2   = nn.Linear(d_model * mlp_ratio, d_model)

    def __call__(self, x: mx.array, mask: mx.array | None = None) -> mx.array:
        # Self-attention with residual
        h = self.norm1(x)
        x = x + self.attn(h, h, h, mask=mask)
        # Feed-forward with residual
        h = self.norm2(x)
        x = x + self.ff2(nn.gelu(self.ff1(h)))
        return x


class LoRALinear(nn.Module):
    """
    LoRA-augmented linear layer for efficient fine-tuning.
    Adds trainable low-rank decomposition BA to frozen weights W.
    """

    def __init__(self, base_layer: nn.Linear, rank: int = 16, alpha: float = 16.0):
        super().__init__()
        in_f  = base_layer.weight.shape[1]
        out_f = base_layer.weight.shape[0]

        # Freeze base weights by NOT making them parameters
        self.weight = base_layer.weight    # Frozen
        self.bias   = base_layer.bias

        # LoRA matrices — only these are trained
        scale  = alpha / rank
        self.A = mx.random.normal((rank, in_f)) * 0.01
        self.B = mx.zeros((out_f, rank))
        self.scale = scale

    def __call__(self, x: mx.array) -> mx.array:
        base_out = x @ self.weight.T
        if self.bias is not None:
            base_out = base_out + self.bias
        lora_out = (x @ self.A.T) @ self.B.T
        return base_out + self.scale * lora_out


# ── 2. Loss functions ─────────────────────────────────────────────────────────

def cross_entropy_loss(model: nn.Module, x: mx.array, y: mx.array) -> mx.array:
    logits = model(x, training=True)
    return nn.losses.cross_entropy(logits, y, reduction="mean")


def mse_loss(model: nn.Module, x: mx.array, y: mx.array) -> mx.array:
    return mx.mean((model(x, training=True) - y) ** 2)


# ── 3. Training step (compiled for speed) ────────────────────────────────────

def make_train_step(loss_fn):
    """Factory — creates a JIT-compiled training step for a given loss."""

    @partial(mx.compile, inputs=nn.Module.trainable_parameters, outputs=nn.Module.trainable_parameters)
    def train_step(model: nn.Module, optimizer: optim.Optimizer, x: mx.array, y: mx.array):
        loss, grads = mx.value_and_grad(loss_fn)(model, x, y)
        optimizer.update(model, grads)
        return loss

    return train_step


# ── 4. Data generator ─────────────────────────────────────────────────────────

def numpy_to_mx_batches(
    X: np.ndarray,
    y: np.ndarray,
    batch_size: int = 256,
    shuffle: bool = True,
) -> Generator[tuple[mx.array, mx.array], None, None]:
    n = len(X)
    idx = np.random.permutation(n) if shuffle else np.arange(n)
    for start in range(0, n, batch_size):
        b = idx[start : start + batch_size]
        yield mx.array(X[b]), mx.array(y[b])


# ── 5. Full training loop ─────────────────────────────────────────────────────

def train(
    in_features:  int   = 10,
    out_features: int   = 2,
    hidden_dim:   int   = 128,
    lr:           float = 1e-3,
    epochs:       int   = 20,
    batch_size:   int   = 256,
):
    # Synthetic data
    rng    = np.random.default_rng(42)
    X_np   = rng.standard_normal((5000, in_features)).astype(np.float32)
    y_np   = rng.integers(0, out_features, size=5000)

    # Model + optimizer
    model = MLP([in_features, hidden_dim, hidden_dim, out_features])
    mx.eval(model.parameters())   # Materialize lazy params

    optimizer  = optim.AdamW(learning_rate=lr, weight_decay=1e-4)
    train_step = make_train_step(cross_entropy_loss)

    for epoch in range(epochs):
        t0   = time.perf_counter()
        losses: list[float] = []

        for x_b, y_b in numpy_to_mx_batches(X_np, y_np, batch_size):
            loss = train_step(model, optimizer, x_b, y_b)
            mx.eval(loss)          # Force evaluation to get the scalar value
            losses.append(loss.item())

        elapsed = (time.perf_counter() - t0) * 1e3
        print(f"Epoch {epoch+1:3d} | loss={np.mean(losses):.4f} | {elapsed:.0f}ms")

    return model


# ── 6. LLM inference with mlx-lm ─────────────────────────────────────────────

def run_llm_inference(
    model_id:   str = "mlx-community/Meta-Llama-3.1-8B-Instruct-4bit",
    prompt:     str = "Explain gradient descent in one paragraph.",
    max_tokens: int = 300,
) -> str:
    """Load quantized LLM and generate text on Apple Silicon."""
    from mlx_lm import load, generate

    print(f"Loading {model_id}...")
    model, tokenizer = load(model_id)

    response = generate(
        model,
        tokenizer,
        prompt=prompt,
        max_tokens=max_tokens,
        temp=0.7,
        verbose=False,
    )
    return response


# ── 7. Save and load model weights ───────────────────────────────────────────

def save_model(model: nn.Module, path: str = "model.npz") -> None:
    """Save model weights as NumPy .npz file."""
    weights = dict(mx.utils.tree_flatten(model.parameters()))
    np.savez(path, **{k: np.array(v) for k, v in weights.items()})
    print(f"Model saved: {path}")


def load_model(model: nn.Module, path: str = "model.npz") -> nn.Module:
    """Load weights back into model."""
    data     = np.load(path)
    weights  = mx.utils.tree_unflatten(list(data.items()))
    model.load_weights(list(mx.utils.tree_flatten(weights)))
    mx.eval(model.parameters())
    return model


if __name__ == "__main__":
    print("Training MLP on Apple Silicon...")
    model = train()

    print("\nRunning LLM inference...")
    response = run_llm_inference()
    print(f"Response: {response[:200]}...")

For the PyTorch MPS backend alternative when needing access to PyTorch’s full ecosystem (TRL, DeepSpeed, Flash Attention) while running on Apple Silicon — torch.device("mps") moves tensors to the M-chip GPU while MLX’s unified memory model avoids explicit copies between CPU and GPU entirely, and MLX’s LoRA fine-tuning via mlx_lm.lora is specifically optimized for Apple Silicon memory bandwidth in ways that PyTorch MPS cannot match. For the GGUF/Ollama alternative when wanting a completely no-code local LLM setup — Ollama downloads and runs GGUF-quantized models with a single command while MLX provides a Python API that allows customization of generation, evaluation pipelines, and fine-tuning that isn’t possible through Ollama’s black-box serving layer. The Claude Skills 360 bundle includes Apple MLX skill sets covering nn.Module definitions, training loops, LoRA fine-tuning, LLM inference with mlx-lm, quantization, and weight serialization. Start with the free tier to try Apple Silicon ML code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free