Files
sgl-project--sglang/python/sglang/srt/layers/quantization/mlx.py
T
wehub-resource-sync 94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:16 +08:00

125 lines
4.7 KiB
Python

"""Marker config and auto-detect hook for MLX backend quantization presets.
The MLX backend (``python/sglang/srt/hardware_backend/mlx/``) performs its own
quantization at model load time via :func:`mlx_lm.utils.quantize_model`. The
standard PyTorch ``QuantizationConfig`` machinery is never invoked on that
path.
This module serves two purposes:
1. Registry registration. Listing ``mlx_q4`` and ``mlx_q8`` in
``QUANTIZATION_METHODS`` lets :meth:`ModelConfig._verify_quantization`
recognize them as known methods without backend-specific exceptions in
the generic config code.
2. Auto-detection for mlx-community HF repos.
:meth:`override_quantization_method` claims ``config.json`` blocks of
the form ``{"group_size": <int>, "bits": <int>}`` with no
``quant_method`` key and resolves them to the matching preset.
Already-quantized mlx-community repos load on Apple Silicon without the
user passing ``--quantization`` on the CLI. Resolves #25119.
The PyTorch path constructors (``from_config``, ``get_quant_method``) raise
``NotImplementedError`` with a clear pointer to ``SGLANG_USE_MLX=1``, since
this class is not a real PyTorch quantization implementation.
"""
from __future__ import annotations
from typing import Any, Dict, List, Optional
import torch
from sglang.srt.layers.quantization.base_config import (
QuantizationConfig,
QuantizeMethodBase,
)
class MlxQuantizationConfig(QuantizationConfig):
"""Marker config for MLX backend on-the-fly quantization presets.
Not a real quantization config — the MLX backend handles quantization
itself. Any standard-PyTorch-path method that touches this class raises
a helpful error pointing the user at ``SGLANG_USE_MLX=1``.
"""
_ERR = (
"MLX on-the-fly quantization (--quantization mlx_q4 / mlx_q8) is "
"handled by the MLX backend at model-load time via mlx_lm.utils."
"quantize_model, not by this QuantizationConfig class. If you "
"reached this error, SGLANG_USE_MLX=1 is likely not set."
)
def __init__(self, preset: str):
super().__init__()
self.preset = preset
@classmethod
def get_name(cls) -> str:
return "mlx"
@classmethod
def get_supported_act_dtypes(cls) -> List[torch.dtype]:
return []
@classmethod
def get_min_capability(cls) -> int:
# Capability check is for NVIDIA SM versions; not meaningful for MLX.
return 0
@classmethod
def get_config_filenames(cls) -> List[str]:
return []
@classmethod
def from_config(cls, config: Dict[str, Any]) -> MlxQuantizationConfig:
raise NotImplementedError(cls._ERR)
@classmethod
def override_quantization_method(cls, hf_quant_cfg, user_quant) -> Optional[str]:
"""Auto-detect mlx-community-shape quantization configs.
mlx-community models ship ``config.json`` with::
"quantization_config": {"group_size": <int>, "bits": <int>}
No ``quant_method`` key, no other identifying field. Without this
override, :meth:`ModelConfig._verify_quantization` cannot match the
shape to any registered method and raises ``Unknown quantization
method`` (see #25119). Match it here and return the preset whose
bit-width agrees, so pre-quantized HF repos load on Apple Silicon
without the user having to pass ``--quantization`` on the CLI.
Returns ``None`` for any input that does not look like a bare MLX
preset: non-dict, dict with an explicit ``quant_method``, missing
keys, non-integer values, or unsupported bit-width. Also defers to
any explicit ``--quantization`` CLI choice (``user_quant``) per the
registry contract: CLI selection takes priority over auto-detect.
"""
if user_quant is not None:
# User passed --quantization explicitly; respect that choice
# regardless of the HF config shape. Matches the moe_wna16 /
# modelopt convention.
return None
if not isinstance(hf_quant_cfg, dict):
return None
if "quant_method" in hf_quant_cfg:
# Configs that declare a quant_method belong to whichever method
# registers under that name; do not hijack them.
return None
bits = hf_quant_cfg.get("bits")
group_size = hf_quant_cfg.get("group_size")
if not isinstance(bits, int) or not isinstance(group_size, int):
return None
if bits == 4:
return "mlx_q4"
if bits == 8:
return "mlx_q8"
return None
def get_quant_method(
self, layer: torch.nn.Module, prefix: str
) -> Optional[QuantizeMethodBase]:
raise NotImplementedError(self._ERR)