Files
Model-Optimizer/modelopt/torch/quantization/ggml/common.py
T
9e3d555aa1 [OMNIML-5899] Add IQ quantization codecs and backend (#2446)
## Summary

- add IQ1_S and IQ2_XS reference codecs and a weight-only fake-quant
backend
- register and export both formats from the quantization package
- cache compact packed weights across unchanged forwards and invalidate
on tensor or config changes
- use one Python-side IQ2_XS FP16 scale predictor for both reference and
CUDA packing
- validate packed payload metadata, normalize CUDA cache keys, and
define a shared non-finite policy

## PR split

This work is split into four focused PRs. Each PR targets `main` and
owns a disjoint file set:

1. **Kernel** — [#2448: Add CUDA kernels for IQ
packing](https://github.com/NVIDIA/Model-Optimizer/pull/2448)
2. **Quantization** — [#2446: Add IQ quantization codecs and
backend](https://github.com/NVIDIA/Model-Optimizer/pull/2446)
3. **Export** — [#2447: Export IQ checkpoints from HF and
Megatron](https://github.com/NVIDIA/Model-Optimizer/pull/2447)
4. **Recipes** — [#2449: Add IQ post-training quantization
recipes](https://github.com/NVIDIA/Model-Optimizer/pull/2449)

The required merge order is #2448, #2446, #2447, then #2449.

## Scope

This PR owns the Python codecs, backend dispatch, package registration,
license attribution, CPU codec/backend tests, and CUDA
numerical/reference-path tests. The native CUDA layer and direct
extension tests remain in #2448; export and recipes remain in their own
PRs.

## Why the codecs are separate from `qtensor`

The new `ggml/` package contains stateless reference codecs and
fake-quant backend functions. They transform ordinary tensors into
packed format payloads and reconstruct tensors for fake quantization;
they do not define persistent runtime quantized-tensor objects.

`BaseQuantizedTensor` subclasses under `qtensor/` own runtime tensor
objects and execution dispatch. Keeping the codecs separate avoids
claiming a runtime tensor contract that these formats do not yet
provide. A `qtensor` type can be added later if a runtime execution path
requires one.

## Compatibility boundary

The Python encoders intentionally use fixed-scale, unweighted searches.
They are not intended to reproduce another encoder's bytes for every
input when that encoder performs iterative scale refinement or
importance weighting. Compatibility is defined by the canonical
codebooks, 50/74-byte payload layouts, and pinned dequantization
formulas.

IQ2_XS computes the FP16 superblock scale once in the Python predictor
and passes it to the CUDA packer. This removes a duplicate
floating-point reduction and makes native/reference byte parity use the
same scale. Non-finite input elements are treated as zero during packing
in both implementations.

The unit tests construct nonzero payload fields independently and
validate metadata, signs, local scales, and global scales. The CUDA
tests compare native packed bytes with this Python reference encoder.

## Test coverage

- [IQ1_S CPU codec
tests](https://github.com/NVIDIA/Model-Optimizer/blob/e8d937081d8cd01cf8e44d43915df443b79deb17/tests/unit/torch/quantization/test_iq1_s.py)
- [IQ2_XS CPU codec
tests](https://github.com/NVIDIA/Model-Optimizer/blob/e8d937081d8cd01cf8e44d43915df443b79deb17/tests/unit/torch/quantization/test_iq2_xs.py)
- [registered backend and cache
tests](https://github.com/NVIDIA/Model-Optimizer/blob/e8d937081d8cd01cf8e44d43915df443b79deb17/tests/unit/torch/quantization/test_ggml_backend.py)
- [IQ1_S CUDA byte-parity, numerical, non-finite, zero-payload, and
fallback
tests](https://github.com/NVIDIA/Model-Optimizer/blob/e8d937081d8cd01cf8e44d43915df443b79deb17/tests/gpu/torch/quantization/test_iq1_s_cuda.py)
- [IQ2_XS CUDA byte-parity, numerical, non-finite, zero-payload,
underflow, and fallback
tests](https://github.com/NVIDIA/Model-Optimizer/blob/e8d937081d8cd01cf8e44d43915df443b79deb17/tests/gpu/torch/quantization/test_iq2_xs_cuda.py)

## Licensing

The embedded codebook data cites the pinned upstream MIT source, carries
its license notice, and uses the repository's third-party license
mechanism. Human OSRB/code-owner confirmation is still required; this PR
does not claim that approval.

## Validation

- focused lint, format, and type checks pass for all changed Python
files
- 36 focused CPU codec and backend tests pass locally
- all 20 direct-extension and CUDA integration test cases collect
locally; runtime CUDA execution remains delegated to GPU CI
- restricted-term scan passes


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
  - Added GGML quantization support for IQ1_S and IQ2_XS formats.
- Added quantization, dequantization, and fake-quantization workflows
with pass-through gradients.
  - Added CPU fallback when CUDA acceleration is unavailable.
- Added validation for packed weights, tensor shapes, formats, and
backend options.
- Added configurable chunk processing and caching for repeated
quantization.

- **Tests**
- Added comprehensive CPU and CUDA coverage for accuracy, validation,
caching, fallback behavior, and edge cases.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Hung-Yueh Chiang <hungyuehc@nvidia.com>
Signed-off-by: Chenjie Luo <chenjiel@nvidia.com>
Co-authored-by: Chenjie Luo <chenjiel@nvidia.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 06:27:58 +00:00

164 lines
6.2 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Shared validation for GGML-compatible block quantizers."""
import math
import weakref
from collections.abc import Callable
from dataclasses import dataclass
import torch
GGML_BLOCK_SIZE = 256
@dataclass
class _PackedWeightCache:
input_ref: weakref.ReferenceType
input_key: tuple[object, ...]
format_name: str
block_chunk_size: int
packed_weights: torch.Tensor
weight_shape: torch.Tensor
def _input_cache_key(inputs: torch.Tensor) -> tuple[object, ...] | None:
try:
version = inputs._version
except RuntimeError:
# Inference tensors can omit version counters, so changes cannot be detected safely.
return None
return (
inputs.data_ptr(),
tuple(inputs.shape),
tuple(inputs.stride()),
inputs.dtype,
inputs.device,
version,
)
def fake_quantize_with_cache(
inputs: torch.Tensor,
quantizer,
*,
format_name: str,
block_chunk_size: int,
quantize: Callable[..., tuple[torch.Tensor, torch.Tensor]],
dequantize: Callable[..., torch.Tensor],
) -> torch.Tensor:
"""Fake-quantize a weight while caching its compact packed representation."""
input_key = _input_cache_key(inputs)
cache = getattr(quantizer, "_quantizer_cache", None)
if (
isinstance(cache, _PackedWeightCache)
and input_key is not None
and cache.input_ref() is inputs
and cache.input_key == input_key
and cache.format_name == format_name
and cache.block_chunk_size == block_chunk_size
):
packed_weights, weight_shape = cache.packed_weights, cache.weight_shape
else:
packed_weights, weight_shape = quantize(inputs, block_chunk_size=block_chunk_size)
if input_key is not None:
quantizer._quantizer_cache = _PackedWeightCache(
input_ref=weakref.ref(inputs),
input_key=input_key,
format_name=format_name,
block_chunk_size=block_chunk_size,
packed_weights=packed_weights,
weight_shape=weight_shape,
)
else:
quantizer._quantizer_cache = None
reconstructed = dequantize(
packed_weights,
weight_shape,
dtype=inputs.dtype,
block_chunk_size=block_chunk_size,
)
return inputs + (reconstructed - inputs).detach()
def narrow_to_float32(blocks: torch.Tensor) -> torch.Tensor:
"""Narrow ``blocks`` to float32 the way the CUDA ``load_float`` helper does.
Non-finite elements become zero, and finite elements outside the float32 range saturate
instead of overflowing to infinity and then being zeroed. Sanitizing at the source precision
is what keeps the reference encoders byte-identical to the extension for float64 weights;
converting first would turn a finite 1e100 into zero on this path and into the float32
maximum on the CUDA one.
"""
finite = torch.nan_to_num(blocks, nan=0.0, posinf=0.0, neginf=0.0)
if finite.dtype == torch.float64:
# Only float64 can hold a finite value the narrowing would overflow. The float32 bounds
# do not fit in the narrower dtypes, so clamping them would raise rather than no-op.
info = torch.finfo(torch.float32)
finite = finite.clamp(info.min, info.max)
return finite.float()
def validate_weight(weight: torch.Tensor, format_name: str) -> None:
"""Validate weight metadata accepted by the current GGML block encoders."""
if weight.numel() == 0:
raise ValueError(f"{format_name} requires a non-empty weight")
if weight.dim() == 0 or weight.shape[-1] % GGML_BLOCK_SIZE:
raise ValueError(
f"{format_name} requires the last weight dimension to be divisible by "
f"{GGML_BLOCK_SIZE}, got shape {tuple(weight.shape)}"
)
if not weight.is_floating_point():
raise TypeError(f"{format_name} requires a floating-point weight, got {weight.dtype}")
def validate_block_chunk_size(block_chunk_size: int) -> None:
"""Validate the common encoder and decoder block-chunk limit."""
if isinstance(block_chunk_size, bool) or not isinstance(block_chunk_size, int):
raise TypeError("block_chunk_size must be an integer")
if block_chunk_size <= 0:
raise ValueError(f"block_chunk_size must be positive, got {block_chunk_size}")
def validate_packed_weights(
packed_weights: torch.Tensor,
weight_shape: torch.Tensor,
*,
block_bytes: int,
format_name: str,
) -> tuple[int, ...]:
"""Validate a packed payload and return its logical shape."""
if (
packed_weights.dim() == 0
or packed_weights.dtype != torch.uint8
or packed_weights.shape[-1] != block_bytes
):
raise ValueError(
f"packed_weights must be uint8 with last dimension {block_bytes}, "
f"got {packed_weights.dtype} {tuple(packed_weights.shape)}"
)
integral_dtypes = {torch.int8, torch.uint8, torch.int16, torch.int32, torch.int64}
if weight_shape.dim() != 1 or weight_shape.dtype not in integral_dtypes:
raise ValueError("weight_shape must be a one-dimensional integral tensor")
shape = tuple(int(v) for v in weight_shape.detach().cpu().tolist())
if not shape or any(dimension <= 0 for dimension in shape) or shape[-1] % GGML_BLOCK_SIZE:
raise ValueError(f"invalid {format_name} logical weight shape: {shape}")
expected_payload_values = math.prod(shape) // GGML_BLOCK_SIZE * block_bytes
if packed_weights.numel() != expected_payload_values:
raise ValueError("packed_weights size does not match weight_shape")
return shape