mlx-iqk
mlx-iqk provides IQ_K codecs, byte-preserving relayouts, and Metal kernels
for MLX on Apple silicon. It supports stacked routed-expert projections and
ordinary dense projections. Codec selection, tensor allocation, and model
policy remain the responsibility of the caller.
Supported formats
| use | members |
|---|---|
| routed experts | IQ1_S_R4, IQ2_KS, IQ2_K, IQ3_K |
| dense tensors | IQ4_KS, IQ4_K, IQ5_KS, IQ5_K, IQ6_K |
For every member, the library provides:
- a CPU encoder and dequantizer based on pinned ik_llama.cpp code;
- conversion between ik wire bytes and a reduction-contiguous layout;
- a NumPy reference decoder;
- Metal decode and dequantization kernels;
- geometry and representation checks before kernel compilation.
The relayout changes where fields are stored, not their encoded values or
total byte count. mlx_iqk.format.bits_per_weight() reports the exact storage
cost for a member and row width, including row metadata.
Installation
uv add mlx-iqk
The runtime requires Apple silicon and MLX. Python 3.11 or newer is supported. The CPU codec builds its vendored C++ sources on first use if the corresponding shared library is absent.
Conversion
mlx_iqk.codec converts float32 rows to ik wire bytes. mlx_iqk.format packs
those bytes into the layout consumed by the Metal kernels.
from mlx_iqk import codec
from mlx_iqk import format as fmt
wire = codec.quantize("iq2_ks", weights, imatrix)
streams = fmt.pack("iq2_ks", wire, weights.shape[-1])
# Both checks are byte-exact.
assert fmt.relayout_row_bytes("iq2_ks", weights.shape[-1]) == wire.shape[-1]
assert (fmt.unpack("iq2_ks", streams, weights.shape[-1]) == wire).all()
imatrix is one float32 importance vector with the same length as a weight
row. Passing None selects the codec's unweighted objective; it is not
equivalent to calibrated conversion.
Routed experts
IqkSwitchLinear stores one stacked expert projection. Small token-expert
batches use a packed GEMV. Larger or explicitly sorted batches dequantize the
expert stack to fp16 and use mx.gather_mm.
import mlx.core as mx
from mlx_iqk import IqkSwitchLinear
projection = IqkSwitchLinear(
"iq2_ks",
num_experts=num_experts,
out_features=out_features,
in_features=in_features,
)
projection.load_streams({name: mx.array(value) for name, value in streams.items()})
output = projection(hidden, expert_indices)
The routed kernels currently support these reduction widths:
| members | in_features |
|---|---|
IQ2_KS, IQ2_K |
768, 2048, 2560, 4096 |
IQ3_K |
768, 2560 |
IQ1_S_R4 |
640, 2048, 2560, 4096 |
The generic packed GEMV requires out_features to be a positive multiple of
16. Dequantization accepts any positive output size.
mlx_iqk.routed also contains a specialized two-dispatch MoE executor for its
declared geometry. It fuses gate, up, and SwiGLU in the first dispatch, then
fuses down projection, route weighting, and ordered reduction in the second.
Unsupported geometries and codec tuples are rejected before compilation.
Dense tensors
Dense entry points accept either named relayout streams or one row-packed
uint8 matrix:
from mlx_iqk import dense_gemv, dense_linear
decoded = dense_gemv(member, hidden, streams, out_features, in_features)
automatic = dense_linear(member, hidden, streams, out_features, in_features)
The dense_*_packed variants operate directly on a row-packed MLX allocation.
Full and row-range dequantization are available for prefill paths. The dense
kernels support reduction widths 512, 768, 1024, 2048, 2560, 4096, 6144,
8192, and 10240, with arbitrary positive output sizes.
dense_linear uses a token-count crossover between packed GEMV and
dequantize-plus-matmul. Callers with measured scheduling policy can invoke
dense_gemv, dense_dequantized, or their packed and range variants
directly.
Architecture adapters
Model-specific geometry is isolated from the format and kernel core.
mlx_iqk.qwen provides the logical-to-stored width mapping and zero-padding
helpers required by the supported Qwen hybrid architecture. The adapter does
not select codecs or tensor allocations.
Correctness
The CPU dequantizers define the arithmetic reference. Relayout tests compare their float32 output bit for bit, and Metal dequantization tests compare fp16 output bit for bit. GEMV tests use independent decoded-weight products and format-specific tolerances where accumulation order differs.
uv sync --locked --group dev
uv run --locked --group dev pytest
The optional upstream cross-check requires an ik_llama.cpp checkout at the
revision recorded in vendor/ik_llama/UPSTREAM_REVISION:
IK_LLAMA_DIR=/path/to/ik_llama.cpp \
uv run --locked --group dev pytest tests/test_vendor_upstream.py
It rebuilds its comparator in a temporary directory and verifies quantization,
dequantization, row sizes, and relayouts against upstream libggml.
Benchmarks
bench/RESULTS.md records kernel measurements and the corresponding address
floors. Benchmark drivers validate their outputs before timing and emit JSON
for reproducibility.
Licensing
The project is MIT licensed. Vendored codec code retains the upstream MIT
terms. NOTICE and ATTRIBUTION.md identify copied and adapted work, source
revisions, symbols, and local changes.
Metadata
Release files for mlx-iqk 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mlx_iqk-0.1.3.tar.gz | 177.8 kB | With provenance | Details for mlx_iqk-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mlx_iqk-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for mlx_iqk-0.1.3-py3-none-any.whl |
Total release size: 273.8 kB
Release files / mlx_iqk-0.1.3.tar.gz
| Download URL | mlx_iqk-0.1.3.tar.gz |
|---|---|
| Size | 177.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9f93f47d02720472aeee51e6c2243ceb63ad66ed98e07428512efc005095e956
|
|
BLAKE2b-256 checksum How to use checksums |
e4bcf2b31355e9d1dab5418d7b9e9434618d1d39d92672e0353b942675b1b0d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.
Transparency logRelease files / mlx_iqk-0.1.3-py3-none-any.whl
| Download URL | mlx_iqk-0.1.3-py3-none-any.whl |
|---|---|
| Size | 96.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb2f2dae7b44e99b46643abdc1e5fffeda84b1f4ba61df25a0aebcec400d18cf
|
|
BLAKE2b-256 checksum How to use checksums |
8a5cfa68a7d1ac7737aa92eefe4b2bd83397000d29c0fb177c0c9bbb35686f6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.
Transparency logSecurity for mlx-iqk 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mlx_iqk-0.1.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.