PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
# Copyright (c) 2025 Intel Corporation
#
# 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.
import json
import os
from collections.abc import Iterable
from functools import partial
import torch
from safetensors import safe_open
from torch import Tensor
from auto_round.utils import LazyImport, download_or_get_path
from auto_round.utils.path_safety import resolve_within_directory
gguf = LazyImport("gguf")
_GRANITE_MOE_ARCHITECTURES = {
"GraniteMoeForCausalLM",
"GraniteMoeSharedForCausalLM",
"GraniteMoeHybridForCausalLM",
}
_GRANITE_AGGREGATED_EXPERT_TENSORS = {
"down_proj": gguf.MODEL_TENSOR.FFN_DOWN_EXP,
"gate_proj": gguf.MODEL_TENSOR.FFN_GATE_EXP,
"up_proj": gguf.MODEL_TENSOR.FFN_UP_EXP,
}
def handle_special_model(cls, model_architecture):
if model_architecture == "GptOssForCausalLM":
cls.generate_extra_tensors = partial(gptoss_generate_extra_tensors, cls)
cls.modify_tensors = partial(gptoss_modify_tensors, cls)
elif model_architecture in _GRANITE_MOE_ARCHITECTURES:
cls.modify_tensors = partial(granite_moe_modify_tensors, cls, cls.modify_tensors)
return cls
def granite_moe_modify_tensors(cls, original_modify_tensors, data_torch, name, bid):
"""Handle AutoRound's aggregated Granite MoE views before llama.cpp conversion."""
expert_projection = next(
(
projection
for projection in _GRANITE_AGGREGATED_EXPERT_TENSORS
if name.endswith(
(f"block_sparse_moe.experts.{projection}", f"block_sparse_moe.experts.{projection}.weight")
)
),
None,
)
if expert_projection is not None:
if data_torch.ndim != 3:
raise ValueError(
f"Aggregated Granite expert tensor {name!r} must be three-dimensional, "
f"got shape {tuple(data_torch.shape)}"
)
tensor_type = _GRANITE_AGGREGATED_EXPERT_TENSORS[expert_projection]
new_name = cls.format_tensor_name(tensor_type, bid)
yield new_name, data_torch
return
if name.endswith(("block_sparse_moe.experts.gate_up_proj", "block_sparse_moe.experts.gate_up_proj.weight")):
if data_torch.ndim != 3:
raise ValueError(
f"Aggregated Granite expert tensor {name!r} must be three-dimensional, "
f"got shape {tuple(data_torch.shape)}"
)
ffn_dim = cls.hparams["intermediate_size"]
if data_torch.shape[-2] != 2 * ffn_dim:
raise ValueError(
f"Aggregated Granite gate/up tensor {name!r} must have size {2 * ffn_dim} in dimension -2, "
f"got shape {tuple(data_torch.shape)}"
)
gate, up = data_torch.split(ffn_dim, dim=-2)
yield cls.format_tensor_name(gguf.MODEL_TENSOR.FFN_GATE_EXP, bid), gate
yield cls.format_tensor_name(gguf.MODEL_TENSOR.FFN_UP_EXP, bid), up
return
yield from original_modify_tensors(data_torch, name, bid)
def get_tensor_from_file(dir_path, tensor_name):
if not os.path.isdir(dir_path):
dir_path = download_or_get_path(dir_path)
INDEX_FILE = "model.safetensors.index.json"
# get filename
if INDEX_FILE in os.listdir(dir_path):
with open(os.path.join(dir_path, INDEX_FILE)) as f:
tensor_index = json.load(f)
filename = tensor_index["weight_map"][tensor_name]
else:
filename = "model.safetensors"
# get tensor
f = safe_open(str(resolve_within_directory(dir_path, filename, origin=INDEX_FILE)), framework="pt")
return f.get_tensor(tensor_name)
GPTOSS_RELOAD = True
def gptoss_generate_extra_tensors(cls) -> Iterable[tuple[str, Tensor]]:
blocks0: Tensor = torch.zeros(1)
blocks1: Tensor = torch.zeros(1)
found_mxfp4_tensors = False
# we assume that tensors are loaded in the correct order
def repack(name, data_torch, blocks0, blocks1):
if "mlp.experts.down_proj_blocks" in name:
blocks0 = data_torch
elif "mlp.experts.down_proj_scales" in name:
new_name = cls.map_tensor_name(name.replace("_scales", ".weight"))
for t in cls.gguf_writer.tensors:
if new_name in t.keys():
return blocks0, blocks1
cls.repack_mxfp4(new_name, blocks0, data_torch)
found_mxfp4_tensors = True
elif "mlp.experts.gate_up_proj_blocks" in name:
blocks0, blocks1 = data_torch[:, ::2, :, :], data_torch[:, 1::2, :, :]
elif "mlp.experts.gate_up_proj_scales" in name:
scales0, scales1 = data_torch[:, ::2, :], data_torch[:, 1::2, :]
new_name_gate = cls.map_tensor_name(name.replace("gate_up_proj_scales", "gate_proj.weight"))
new_name_up = cls.map_tensor_name(name.replace("gate_up_proj_scales", "up_proj.weight"))
for t in cls.gguf_writer.tensors:
if new_name_gate in t.keys() or new_name_up in t.keys():
return blocks0, blocks1
cls.repack_mxfp4(new_name_gate, blocks0, scales0)
cls.repack_mxfp4(new_name_up, blocks1, scales1)
found_mxfp4_tensors = True
return blocks0, blocks1
for name, data_torch in cls.get_tensors():
if GPTOSS_RELOAD and name.endswith(("mlp.experts.down_proj", "mlp.experts.gate_up_proj")):
block_name = name + "_blocks"
block_data_torch = get_tensor_from_file(cls.model.name_or_path, block_name)
blocks0, blocks1 = repack(block_name, block_data_torch, blocks0, blocks1)
scale_name = name + "_scales"
scale_data_torch = get_tensor_from_file(cls.model.name_or_path, scale_name)
blocks0, blocks1 = repack(scale_name, scale_data_torch, blocks0, blocks1)
else:
blocks0, blocks1 = repack(name, data_torch, blocks0, blocks1)
# convert to bf16 model, foud_mxfp4_tensors is False
# if not found_mxfp4_tensors:
# raise ValueError("No MXFP4 tensors found in the model. Please make sure you are using MXFP4 model.")
return []
def gptoss_modify_tensors(cls, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
del bid # unused
if "sinks" in name:
name += ".weight"
# correct naming for down_proj
if "down_proj" in name:
if name.endswith("_bias"):
name = name.replace("down_proj_bias", "down_proj.bias")
else:
if GPTOSS_RELOAD:
return []
if data_torch.dtype != torch.uint8:
return [(cls.map_tensor_name(name + ".weight"), data_torch)]
return []
# split the gate_up into gate and up
if "gate_up_proj" in name:
if name.endswith("_bias"):
name_up = name.replace("gate_up_proj_bias", "up_proj.bias")
name_gate = name.replace("gate_up_proj_bias", "gate_proj.bias")
gate_proj_bias, up_proj_bias = data_torch[..., ::2], data_torch[..., 1::2]
return [(cls.map_tensor_name(name_gate), gate_proj_bias), (cls.map_tensor_name(name_up), up_proj_bias)]
else:
if GPTOSS_RELOAD:
return []
if data_torch.dtype != torch.uint8:
new_name_gate = cls.map_tensor_name(name.replace("gate_up_proj", "gate_proj.weight"))
new_name_up = cls.map_tensor_name(name.replace("gate_up_proj", "up_proj.weight"))
gate_data, up_data = data_torch[:, ::2, :], data_torch[:, 1::2, :]
# gate_data = gate_data.reshape(-1, 2880, 2880)
# up_data = up_data.reshape(-1, 2880, 2880)
return [(new_name_gate, gate_data), (new_name_up, up_data)]
return []
return [(cls.map_tensor_name(name), data_torch)]