Skip to main content

Lift any checkpoint into MLX - PyTorch, Safetensors, GGUF

Project description

mlx-weightlifter

Lift any checkpoint into MLX

License Python 3.10+ MLX


Universal weight loading library for MLX. Load checkpoints from any format directly into your nn.Module without framework dependencies.

Supported Formats

Format Extensions Features
GGUF .gguf Auto architecture detection, MoE expansion, built-in tokenizer
Safetensors .safetensors HuggingFace shard support, index files
PyTorch .pt, .bin, .jit Custom unpickler (no torch import), auto-cache to safetensors

Installation

pip install mlx-weightlifter

Or from source:

git clone https://github.com/MLXPorts/mlx-weightlifter.git
cd mlx-weightlifter
pip install -e .

Quick Start

Load a GGUF Model

from mlx_weightlifter import load_gguf_with_mlx

# Auto-detects architecture (qwen3moe, qwen3, gemma3, llama)
model, tokenizer = load_gguf_with_mlx("model.gguf")

# Use it
tokens = tokenizer.encode("Hello, world!")

Load Weights into nn.Module

from mlx_weightlifter import load_weights_from_safetensors
import mlx.nn as nn

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.layers = [nn.Linear(512, 512) for _ in range(12)]

model = MyModel()

# Load with automatic key mapping and dtype conversion
load_weights_from_safetensors(
    model,
    "checkpoint_dir",
    target_dtype=mx.bfloat16,
    strip_prefix="model."
)

Load PyTorch Checkpoints (No Torch Required)

from mlx_weightlifter import load_pytorch_bin

# Loads .pt/.bin files without importing torch
weights = load_pytorch_bin("pytorch_model.bin")

# Auto-caches to safetensors for faster subsequent loads

Features

GGUF Loading

  • Architecture Detection: Automatically identifies model architecture from metadata
  • Weight Mapping: Translates GGUF tensor names to mlx-lm conventions
  • MoE Expansion: Expands consolidated expert weights to per-expert tensors
  • Tokenizer Extraction: Builds BPE tokenizer directly from GGUF metadata

Supported architectures:

  • qwen3moe - Qwen3 Mixture of Experts
  • qwen3 - Qwen3 dense models
  • gemma3 - Gemma 3 models
  • llama - LLaMA-style models

nn.Module Weight Loading

from mlx_weightlifter import load_weights_from_dict, get_parameter_dict

# Extract parameters as flat dict
params = get_parameter_dict(model)
# {'layers.0.weight': array(...), 'layers.0.bias': array(...), ...}

# Load with custom key mapping
def hf_to_local(key):
    return key.replace("transformer.h.", "layers.")

load_weights_from_dict(
    model,
    weights,
    key_mapper=hf_to_local,
    target_dtype=mx.bfloat16,
    strict=False  # Allow partial loading
)

Config Inference

from mlx_weightlifter import infer_config_from_checkpoint, infer_config_from_safetensors

# Infer model dimensions from weight shapes
config = infer_config_from_safetensors("model_dir")
print(config)
# {'hidden_size': 4096, 'num_layers': 32, 'num_heads': 32, ...}

API Reference

Format Loaders

Function Description
load_gguf_with_mlx(path) Load GGUF model + tokenizer
load_pytorch_bin(path) Load PyTorch .pt/.bin file
load_jit(path) Load TorchScript .jit file
load_safetensors_weights(path) Load single safetensors file

nn.Module Loading

Function Description
load_weights_from_safetensors(module, dir) Load from HF checkpoint directory
load_weights_from_dict(module, weights) Load from in-memory dict
get_parameter_dict(module) Extract flat parameter dict
set_parameter(module, path, value) Set single parameter by path

Utilities

Function Description
resolve_dtype(name) Convert dtype string to mx.Dtype
load_config(path) Load JSON config file
compute_derived_dims(config) Compute derived dimensions

Requirements

  • Python 3.10+
  • MLX 0.10+
  • mlx-lm (optional, for GGUF model classes)

License

MIT License - see LICENSE for details.

Contributing

Contributions welcome! Please open an issue or PR on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlx_weightlifter-0.1.1.tar.gz (33.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlx_weightlifter-0.1.1-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

Details for the file mlx_weightlifter-0.1.1.tar.gz.

File metadata

  • Download URL: mlx_weightlifter-0.1.1.tar.gz
  • Upload date:
  • Size: 33.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mlx_weightlifter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2e404ad9df90a10062ee269c88e095460fef2db5ecec0804e56c43318146089b
MD5 d0ea71b2f94dc710c4399ecee08102ee
BLAKE2b-256 d4d00a6825c4160818751ae1353afec0b8c100c8071efa435945b9b2bc5e19ad

See more details on using hashes here.

File details

Details for the file mlx_weightlifter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mlx_weightlifter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e14ef743ce6ae672c8ff734aa920578a3a188dc915f930f85fd602d2d69ba72
MD5 792110f70ec013e718df24881830b396
BLAKE2b-256 3c7d9ea0f5fe70a99cadba7121711071bdbd0ba72cf31b9e794da6703c10ccd4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page