check
Browse filesFiles changed (6)
- README.md+1-1
- __pycache__/convert_diffusion_to_gguf.cpython-310.pycAdded
- app.pyAdded+101
- convert_diffusion_to_gguf.pyAdded+362
- custom_quants.pyAdded+1802
- requirements.txtAdded+5
12345---title: Diffusers To Ggufemoji: 💻colorFrom: bluecolorTo: purple12345---title: Diffusers To GGUFemoji: 💻colorFrom: bluecolorTo: purple
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import gradio as grfrom convert_diffusion_to_gguf import SUPPORTED_ARCHS, qconfig_map, convertfrom huggingface_hub import create_repo, upload_filefrom argparse import Namespacefrom pathlib import Path
def upload(args, outfile): url = "" if args.host_repo_id and args.hf_token: repo_id = create_repo(args.host_repo_id, repo_type="model", exist_ok=True, token=args.hf_token).repo_id info = upload_file(repo_id=repo_id, path_in_repo=str(outfile), path_or_fileobj=str(outfile), token=args.token) url = info.commit_url print(f"Uploaded to {url}")
return url
def go_gguf(model_repo_id, subfolder, arch, outtype, outfile_name, bigendian, verbose, host_repo_id, hf_token): args = Namespace( model=model_repo_id, subfolder=subfolder, arch=arch, outtype=outtype, outfile=Path(outfile_name), bigendian=bigendian, verbose=verbose, host_repo_id=host_repo_id, hf_token=hf_token, ) convert(args) upload(args)
with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("<h1><center>GGUF Converter for Diffusers format model checkpoints</center></h1>") gr.Markdown( "Convert `diffusers` format model checkpoints from the Hub to GGUF format and optionally upload them back. Based on [this repo](https://github.com/ngxson/diffusion-to-gguf)." )
with gr.Row(): with gr.Column(scale=1): gr.Markdown("### 📥 Input Model") model_repo_id = gr.Textbox(label="Model Repo ID", placeholder="e.g., Qwen/Qwen-Image") subfolder = gr.Textbox(label="Subfolder (Optional)", placeholder="e.g., transformer")
gr.Markdown("### ⚙️ Conversion Settings") arch = gr.Dropdown(choices=SUPPORTED_ARCHS, label="Architecture") outtype = gr.Dropdown(choices=list(qconfig_map.keys()), label="Quantization Type", value="F16") outfile_name = gr.Textbox(label="Output Filename", value="{ftype}.gguf")
with gr.Accordion("Advanced Settings", open=False): bigendian = gr.Checkbox(label="Use Big Endian") verbose = gr.Checkbox(label="Verbose Logging", value=True)
gr.Markdown("### 📤 Upload to Hub (Optional)") host_repo_id = gr.Textbox(label="Your Hub Repo ID", placeholder="e.g., YourUsername/My-GGUFs") hf_token = gr.Textbox(label="Hugging Face Token", type="password", placeholder="hf_...")
convert_btn = gr.Button("Convert & Upload", variant="primary")
with gr.Column(scale=2): gr.Markdown("### 🚀 Result") url_output = gr.Markdown()
gr.Examples( examples=[ [ "black-forest-labs/FLUX.1-schnell", "transformer", "flux", "Q4_0", "flux-schnell-q4.gguf", False, False, "YourUsername/MyGGUFs", "hf_...", ], [ "Qwen/Qwen-Image", "transformer", "flux", "Q8_0", "qwen-q4.gguf", False, False, "YourUsername/MyGGUFs", "hf_...", ], ], inputs=[model_repo_id, subfolder, arch, outtype, outfile_name, bigendian, verbose, host_repo_id, hf_token], )
convert_btn.click( fn=lambda x: x, inputs=[model_repo_id, subfolder, arch, outtype, outfile_name, bigendian, verbose, host_repo_id, hf_token], outputs=[url_output], )
if __name__ == "__main__": demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362#!/usr/bin/env python3# -*- coding: utf-8 -*-
from __future__ import annotations
import loggingimport argparseimport jsonimport safetensors.torchimport osimport sysfrom pathlib import Pathfrom typing import Any, ContextManager, castfrom torch import Tensor
import numpy as npimport torchimport gguf
# TODO: add more:SUPPORTED_ARCHS = ["flux", "sd3", "ltxv", "hyvid", "wan", "hidream", "qwen"]
logger = logging.getLogger(__name__)
class QuantConfig: ftype: gguf.LlamaFileType qtype: gguf.GGMLQuantizationType
def __init__(self, ftype: gguf.LlamaFileType, qtype: gguf.GGMLQuantizationType): self.ftype = ftype self.qtype = qtype
qconfig_map: dict[str, QuantConfig] = { "F16": QuantConfig(gguf.LlamaFileType.MOSTLY_F16, gguf.GGMLQuantizationType.F16), "BF16": QuantConfig(gguf.LlamaFileType.MOSTLY_BF16, gguf.GGMLQuantizationType.BF16), "Q8_0": QuantConfig(gguf.LlamaFileType.MOSTLY_Q8_0, gguf.GGMLQuantizationType.Q8_0), "Q6_K": QuantConfig(gguf.LlamaFileType.MOSTLY_Q6_K, gguf.GGMLQuantizationType.Q6_K), "Q5_K_S": QuantConfig(gguf.LlamaFileType.MOSTLY_Q5_K_S, gguf.GGMLQuantizationType.Q5_K), "Q5_1": QuantConfig(gguf.LlamaFileType.MOSTLY_Q5_1, gguf.GGMLQuantizationType.Q5_1), "Q5_0": QuantConfig(gguf.LlamaFileType.MOSTLY_Q5_0, gguf.GGMLQuantizationType.Q5_0), "Q4_K_S": QuantConfig(gguf.LlamaFileType.MOSTLY_Q4_K_S, gguf.GGMLQuantizationType.Q4_K), "Q4_1": QuantConfig(gguf.LlamaFileType.MOSTLY_Q4_1, gguf.GGMLQuantizationType.Q4_1), "Q4_0": QuantConfig(gguf.LlamaFileType.MOSTLY_Q4_0, gguf.GGMLQuantizationType.Q4_0), "Q3_K_S": QuantConfig(gguf.LlamaFileType.MOSTLY_Q3_K_S, gguf.GGMLQuantizationType.Q3_K), # "Q2_S": QuantConfig(gguf.LlamaFileType.MOSTLY_Q2_K, gguf.GGMLQuantizationType.Q2_K), # not yet supported in python}
# tree of lazy tensorsclass LazyTorchTensor(gguf.LazyBase): _tensor_type = torch.Tensor # to keep the type-checker happy dtype: torch.dtype shape: torch.Size
# only used when converting a torch.Tensor to a np.ndarray _dtype_map: dict[torch.dtype, type] = { torch.float16: np.float16, torch.float32: np.float32, }
# used for safetensors slices # ref: https://github.com/huggingface/safetensors/blob/079781fd0dc455ba0fe851e2b4507c33d0c0d407/bindings/python/src/lib.rs#L1046 # TODO: uncomment U64, U32, and U16, ref: https://github.com/pytorch/pytorch/issues/58734 _dtype_str_map: dict[str, torch.dtype] = { "F64": torch.float64, "F32": torch.float32, "BF16": torch.bfloat16, "F16": torch.float16, # "U64": torch.uint64, "I64": torch.int64, # "U32": torch.uint32, "I32": torch.int32, # "U16": torch.uint16, "I16": torch.int16, "U8": torch.uint8, "I8": torch.int8, "BOOL": torch.bool, "F8_E4M3": torch.float8_e4m3fn, "F8_E5M2": torch.float8_e5m2, }
def numpy(self) -> gguf.LazyNumpyTensor: dtype = self._dtype_map[self.dtype] return gguf.LazyNumpyTensor( meta=gguf.LazyNumpyTensor.meta_with_dtype_and_shape(dtype, self.shape), args=(self,), func=(lambda s: s.numpy()), )
@classmethod def meta_with_dtype_and_shape(cls, dtype: torch.dtype, shape: tuple[int, ...]) -> Tensor: return torch.empty(size=shape, dtype=dtype, device="meta")
@classmethod def from_safetensors_slice(cls, st_slice: Any) -> Tensor: dtype = cls._dtype_str_map[st_slice.get_dtype()] shape: tuple[int, ...] = tuple(st_slice.get_shape()) lazy = cls(meta=cls.meta_with_dtype_and_shape(dtype, shape), args=(st_slice,), func=lambda s: s[:]) return cast(torch.Tensor, lazy)
@classmethod def __torch_function__(cls, func, types, args=(), kwargs=None): del types # unused
if kwargs is None: kwargs = {}
if func is torch.Tensor.numpy: return args[0].numpy()
return cls._wrap_fn(func)(*args, **kwargs)
class Converter: path_safetensors: Path endianess: gguf.GGUFEndian outtype: QuantConfig outfile: Path gguf_writer: gguf.GGUFWriter
def __init__( self, arch: str, path_safetensors: Path, endianess: gguf.GGUFEndian, outtype: QuantConfig, outfile: Path, subfolder: str = None, repo_id: str = None, is_diffusers: bool = False, ): self.path_safetensors = path_safetensors self.endianess = endianess self.outtype = outtype self.outfile = outfile
self.gguf_writer = gguf.GGUFWriter(path=None, arch=arch, endianess=self.endianess) self.gguf_writer.add_file_type(self.outtype.ftype) self.gguf_writer.add_type("diffusion") # for HF hub to detect the type correctly if repo_id: self.gguf_writer.add_string("repo_id", repo_id) if subfolder: self.gguf_writer.add_string("subfolder", subfolder) if is_diffusers: self.gguf_writer.add_bool("is_diffusers", True)
# load tensors and process from safetensors import safe_open
ctx = cast(ContextManager[Any], safe_open(path_safetensors, framework="pt", device="cpu")) with ctx as model_part: for name in model_part.keys(): data = model_part.get_slice(name) data = LazyTorchTensor.from_safetensors_slice(data) self.process_tensor(name, data)
def process_tensor(self, name: str, data_torch: LazyTorchTensor) -> None: is_1d = len(data_torch.shape) == 1 current_dtype = data_torch.dtype target_dtype = gguf.GGMLQuantizationType.F32 if is_1d else self.outtype.qtype
if data_torch.dtype not in (torch.float16, torch.float32): data_torch = data_torch.to(torch.float32)
data = data_torch.numpy()
if current_dtype != target_dtype: from custom_quants import quantize as custom_quantize, QuantError
try: data = custom_quantize(data, target_dtype) except QuantError as e: logger.warning("%s, %s", e, "falling back to F16") target_dtype = gguf.GGMLQuantizationType.F16 data = custom_quantize(data, target_dtype)
# reverse shape to make it similar to the internal ggml dimension order shape = gguf.quant_shape_from_byte_shape(data.shape, target_dtype) if data.dtype == np.uint8 else data.shape shape_str = f"{{{', '.join(str(n) for n in reversed(shape))}}}" logger.info(f"{f'%-32s' % f'{name},'} {current_dtype} --> {target_dtype.name}, shape = {shape_str}")
# add tensor to gguf self.gguf_writer.add_tensor(name, data, raw_dtype=target_dtype)
def write(self) -> None: self.gguf_writer.write_header_to_file(path=self.outfile) self.gguf_writer.write_kv_data_to_file() self.gguf_writer.write_tensors_to_file(progress=True) self.gguf_writer.close()
# https://github.com/bghira/SimpleTuner/blob/cea2457ab063f6dedb9e697830ae68a96be90641/helpers/training/save_hooks.py#L64def _merge_sharded_checkpoints(folder: Path): with open(folder / "diffusion_pytorch_model.safetensors.index.json", "r") as f: ckpt_metadata = json.load(f) weight_map = ckpt_metadata.get("weight_map", None) if weight_map is None: raise KeyError("'weight_map' key not found in the shard index file.")
# Collect all unique safetensors files from weight_map files_to_load = set(weight_map.values()) merged_state_dict = {}
# Load tensors from each unique file for file_name in files_to_load: part_file_path = folder / file_name if not os.path.exists(part_file_path): raise FileNotFoundError(f"Part file {file_name} not found.")
with safetensors.safe_open(part_file_path, framework="pt", device="cpu") as f: for tensor_key in f.keys(): if tensor_key in weight_map: merged_state_dict[tensor_key] = f.get_tensor(tensor_key)
return merged_state_dict
def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description="Convert a flux model to GGUF") parser.add_argument( "--outfile", type=Path, default=Path("model-{ftype}.gguf"), help="path to write to; default: 'model-{ftype}.gguf' ; note: {ftype} will be replaced by the outtype", ) parser.add_argument( "--outtype", type=str, choices=qconfig_map.keys(), default="F16", help="output quantization scheme", ) parser.add_argument( "--arch", type=str, choices=SUPPORTED_ARCHS, help="output model architecture", ) parser.add_argument( "--bigendian", action="store_true", help="model is executed on big endian machine", ) parser.add_argument( "model", type=Path, help="directory containing safetensors model file", nargs="?", ) parser.add_argument("--cache_dir", type=Path, help="Directory to store the intermediate files when needed.") parser.add_argument( "--subfolder", type=Path, default=None, help="Subfolder on the HF Hub to load checkpoints from." ) parser.add_argument( "--verbose", action="store_true", help="increase output verbosity", )
args = parser.parse_args() if args.model is None: parser.error("the following arguments are required: model") if args.arch is None: parser.error("the following arguments are required: --arch") if args.arch not in SUPPORTED_ARCHS: parser.error(f"Unsupported architecture: {args.arch}. Supported architectures: {', '.join(SUPPORTED_ARCHS)}") return args
def convert(args): if args.verbose: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO)
if not args.model.is_dir() and not args.model.is_file(): if not len(str(args.model).split("/")) == 2: logging.error(f"Model path {args.model} does not exist.") sys.exit(1)
is_diffusers = False repo_id = None merged_state_dict = None if args.model.is_dir(): logging.info("Supplied a directory.") files = list(args.model.glob("*.safetensors")) n = len(files) if n == 0: logging.error("No safetensors files found.") sys.exit(1) if n == 1: logging.info(f"Assinging {files[0]} to `args.model`") args.model = files[0] if n > 1: assert args.model / "diffusion_pytorch_model.safetensors.index.json" in list(args.model.glob("*.*")) assert args.cache_dir merged_state_dict = _merge_sharded_checkpoints(args.model) filepath = args.cache_dir / "merged_state_dict.safetensors" safetensors.torch.save_file(merged_state_dict, filepath) logging.info(f"Serialized merged state dict to {filepath}") args.model = Path(filepath)
elif len(str(args.model).split("/")) == 2: from huggingface_hub import snapshot_download
logging.info("Hub repo ID detected.") allow_patterns = f"{args.subfolder}/*.*" if args.subfolder else None local_dir = snapshot_download(repo_id=str(args.model), local_dir=args.cache_dir, allow_patterns=allow_patterns) repo_id = str(args.model) local_dir = Path(local_dir) local_dir = local_dir / args.subfolder if args.subfolder else local_dir merged_state_dict = _merge_sharded_checkpoints(local_dir) filepath = ( args.cache_dir / "merged_state_dict.safetensors" if args.cache_dir else "merged_state_dict.safetensors" ) safetensors.torch.save_file(merged_state_dict, filepath) logging.info(f"Serialized merged state dict to {filepath}") args.model = Path(filepath) is_diffusers = True
if merged_state_dict is not None: os.remove(filepath) logging.info(f"Removed the intermediate {filepath}.")
if args.model.suffix != ".safetensors": logging.error(f"Model path {args.model} is not a safetensors file.") sys.exit(1)
if args.outfile.suffix != ".gguf": logging.error("Output file must have .gguf extension.") sys.exit(1)
qconfig = qconfig_map[args.outtype] outfile = Path(str(args.outfile).format(ftype=args.outtype.upper()))
logger.info(f"Converting model in {args.model} to {outfile} with quantization {args.outtype}") converter = Converter( arch=args.arch, path_safetensors=args.model, endianess=gguf.GGUFEndian.BIG if args.bigendian else gguf.GGUFEndian.LITTLE, outtype=qconfig, outfile=outfile, repo_id=repo_id, subfolder=str(args.subfolder) if args.subfolder else None, is_diffusers=is_diffusers, ) converter.write() logger.info( f"Conversion complete. Output written to {outfile}, architecture: {args.arch}, quantization: {qconfig.qtype.name}" )
def main() -> None: args = parse_args() convert(args)
if __name__ == "__main__": main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802from __future__ import annotationsfrom abc import ABC, abstractmethodfrom typing import Any, Callable, Sequencefrom math import log2, ceil
from numpy.typing import DTypeLike
from gguf.constants import GGML_QUANT_SIZES, GGMLQuantizationType, QK_Kfrom gguf.lazy import LazyNumpyTensor
import numpy as np
def quant_shape_to_byte_shape(shape: Sequence[int], quant_type: GGMLQuantizationType) -> tuple[int, ...]: block_size, type_size = GGML_QUANT_SIZES[quant_type] if shape[-1] % block_size != 0: raise ValueError( f"Quantized tensor row size ({shape[-1]}) is not a multiple of {quant_type.name} block size ({block_size})" ) return (*shape[:-1], shape[-1] // block_size * type_size)
def quant_shape_from_byte_shape(shape: Sequence[int], quant_type: GGMLQuantizationType) -> tuple[int, ...]: block_size, type_size = GGML_QUANT_SIZES[quant_type] if shape[-1] % type_size != 0: raise ValueError( f"Quantized tensor bytes per row ({shape[-1]}) is not a multiple of {quant_type.name} type size ({type_size})" ) return (*shape[:-1], shape[-1] // type_size * block_size)
# This is faster than np.vectorize and np.apply_along_axis because it works on more than one row at a timedef _apply_over_grouped_rows( func: Callable[[np.ndarray], np.ndarray], arr: np.ndarray, otype: DTypeLike, oshape: tuple[int, ...]) -> np.ndarray: rows = arr.reshape((-1, arr.shape[-1])) osize = 1 for dim in oshape: osize *= dim out = np.empty(shape=osize, dtype=otype) # compute over groups of 16 rows (arbitrary, but seems good for performance) n_groups = (rows.shape[0] // 16) or 1 np.concatenate([func(group).ravel() for group in np.array_split(rows, n_groups)], axis=0, out=out) return out.reshape(oshape)
# round away from zero# ref: https://stackoverflow.com/a/59143326/22827863def np_roundf(n: np.ndarray) -> np.ndarray: a = abs(n) floored = np.floor(a) b = floored + np.floor(2 * (a - floored)) return np.sign(n) * b
class QuantError(Exception): ...
_type_traits: dict[GGMLQuantizationType, type[__Quant]] = {}
def quantize(data: np.ndarray, qtype: GGMLQuantizationType) -> np.ndarray: if qtype == GGMLQuantizationType.F32: return data.astype(np.float32, copy=False) elif qtype == GGMLQuantizationType.F16: return data.astype(np.float16, copy=False) elif (q := _type_traits.get(qtype)) is not None: return q.quantize(data) else: raise NotImplementedError(f"Quantization for {qtype.name} is not yet implemented")
def dequantize(data: np.ndarray, qtype: GGMLQuantizationType) -> np.ndarray: if qtype == GGMLQuantizationType.F32: return data.view(np.float32) elif qtype == GGMLQuantizationType.F16: return data.view(np.float16).astype(np.float32) elif (q := _type_traits.get(qtype)) is not None: return q.dequantize(data) else: raise NotImplementedError(f"Dequantization for {qtype.name} is not yet implemented")
class __Quant(ABC): qtype: GGMLQuantizationType block_size: int type_size: int
grid: np.ndarray[Any, np.dtype[np.float32]] | None = None grid_shape: tuple[int, int] = (0, 0) grid_map: tuple[int | float, ...] = () grid_hex: bytes | None = None
def __init__(self): return TypeError("Quant conversion classes can't have instances")
def __init_subclass__(cls, qtype: GGMLQuantizationType) -> None: cls.qtype = qtype cls.block_size, cls.type_size = GGML_QUANT_SIZES[qtype] cls.__quantize_lazy = LazyNumpyTensor._wrap_fn( cls.__quantize_array, meta_noop=(np.uint8, cls.__shape_to_bytes) ) cls.__dequantize_lazy = LazyNumpyTensor._wrap_fn( cls.__dequantize_array, meta_noop=(np.float32, cls.__shape_from_bytes) ) assert qtype not in _type_traits _type_traits[qtype] = cls
@classmethod def init_grid(cls): if cls.grid is not None or cls.grid_hex is None: return
bits_per_elem = ceil(log2(len(cls.grid_map))) assert bits_per_elem != 0, cls.qtype.name elems_per_byte = 8 // bits_per_elem
grid = np.frombuffer(cls.grid_hex, dtype=np.uint8) # decode hexadecimal chars from grid grid = grid.reshape((-1, 2)) grid = (np.where(grid > 0x40, grid + 9, grid) & 0x0F) << np.array([4, 0], dtype=np.uint8).reshape((1, 2)) grid = grid[..., 0] | grid[..., 1] # unpack the grid values grid = grid.reshape((-1, 1)) >> np.array( [i for i in range(0, 8, 8 // elems_per_byte)], dtype=np.uint8 ).reshape((1, elems_per_byte)) grid = (grid & ((1 << bits_per_elem) - 1)).reshape((-1, 1)) grid_map = np.array(cls.grid_map, dtype=np.float32).reshape((1, -1)) grid = np.take_along_axis(grid_map, grid, axis=-1) cls.grid = grid.reshape((1, 1, *cls.grid_shape))
@classmethod @abstractmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: raise NotImplementedError
@classmethod @abstractmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: raise NotImplementedError
@classmethod def quantize_rows(cls, rows: np.ndarray) -> np.ndarray: rows = rows.astype(np.float32, copy=False) shape = rows.shape n_blocks = rows.size // cls.block_size blocks = rows.reshape((n_blocks, cls.block_size)) blocks = cls.quantize_blocks(blocks) assert blocks.dtype == np.uint8 assert blocks.shape[-1] == cls.type_size return blocks.reshape(cls.__shape_to_bytes(shape))
@classmethod def dequantize_rows(cls, rows: np.ndarray) -> np.ndarray: rows = rows.view(np.uint8) shape = rows.shape n_blocks = rows.size // cls.type_size blocks = rows.reshape((n_blocks, cls.type_size)) blocks = cls.dequantize_blocks(blocks) assert blocks.dtype == np.float32 assert blocks.shape[-1] == cls.block_size return blocks.reshape(cls.__shape_from_bytes(shape))
@classmethod def __shape_to_bytes(cls, shape: Sequence[int]): return quant_shape_to_byte_shape(shape, cls.qtype)
@classmethod def __shape_from_bytes(cls, shape: Sequence[int]): return quant_shape_from_byte_shape(shape, cls.qtype)
@classmethod def __quantize_array(cls, array: np.ndarray) -> np.ndarray: return _apply_over_grouped_rows( cls.quantize_rows, arr=array, otype=np.uint8, oshape=cls.__shape_to_bytes(array.shape) )
@classmethod def __dequantize_array(cls, array: np.ndarray) -> np.ndarray: cls.init_grid() return _apply_over_grouped_rows( cls.dequantize_rows, arr=array, otype=np.float32, oshape=cls.__shape_from_bytes(array.shape) )
@classmethod def __quantize_lazy(cls, lazy_tensor: LazyNumpyTensor, /) -> Any: pass
@classmethod def __dequantize_lazy(cls, lazy_tensor: LazyNumpyTensor, /) -> Any: pass
@classmethod def can_quantize(cls, tensor: np.ndarray | LazyNumpyTensor) -> bool: return tensor.shape[-1] % cls.block_size == 0
@classmethod def quantize(cls, tensor: np.ndarray | LazyNumpyTensor) -> np.ndarray: if not cls.can_quantize(tensor): raise QuantError(f"Can't quantize tensor with shape {tensor.shape} to {cls.qtype.name}") if isinstance(tensor, LazyNumpyTensor): return cls.__quantize_lazy(tensor) else: return cls.__quantize_array(tensor)
@classmethod def dequantize(cls, tensor: np.ndarray | LazyNumpyTensor) -> np.ndarray: if isinstance(tensor, LazyNumpyTensor): return cls.__dequantize_lazy(tensor) else: return cls.__dequantize_array(tensor)
class BF16(__Quant, qtype=GGMLQuantizationType.BF16): @classmethod # same as ggml_compute_fp32_to_bf16 in ggml-impl.h def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n = blocks.view(np.uint32) # force nan to quiet n = np.where((n & 0x7FFFFFFF) > 0x7F800000, (n & np.uint32(0xFFFF0000)) | np.uint32(64 << 16), n) # round to nearest even n = (np.uint64(n) + (0x7FFF + ((n >> 16) & 1))) >> 16 return n.astype(np.uint16).view(np.uint8)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: return (blocks.view(np.int16).astype(np.int32) << 16).view(np.float32)
class Q4_0(__Quant, qtype=GGMLQuantizationType.Q4_0): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
imax = abs(blocks).argmax(axis=-1, keepdims=True) max = np.take_along_axis(blocks, imax, axis=-1)
d = max / -8 with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) # FIXME: Q4_0's reference rounding is cursed and depends on FMA qs = ( np.trunc((np.float64(blocks) * np.float64(id)) + np.float64(8.5), dtype=np.float32) .astype(np.uint8) .clip(0, 15) )
qs = qs.reshape((n_blocks, 2, cls.block_size // 2)) qs = qs[..., 0, :] | (qs[..., 1, :] << np.uint8(4))
d = d.astype(np.float16).view(np.uint8)
return np.concatenate([d, qs], axis=-1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, qs = np.hsplit(blocks, [2])
d = d.view(np.float16).astype(np.float32)
qs = qs.reshape((n_blocks, -1, 1, cls.block_size // 2)) >> np.array([0, 4], dtype=np.uint8).reshape( (1, 1, 2, 1) ) qs = (qs & np.uint8(0x0F)).reshape((n_blocks, -1)).astype(np.int8) - np.int8(8)
return d * qs.astype(np.float32)
class Q4_1(__Quant, qtype=GGMLQuantizationType.Q4_1): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
max = blocks.max(axis=-1, keepdims=True) min = blocks.min(axis=-1, keepdims=True)
d = (max - min) / 15 with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) qs = np.trunc((blocks - min) * id + np.float32(0.5), dtype=np.float32).astype(np.uint8).clip(0, 15)
qs = qs.reshape((n_blocks, 2, cls.block_size // 2)) qs = qs[..., 0, :] | (qs[..., 1, :] << np.uint8(4))
d = d.astype(np.float16).view(np.uint8) m = min.astype(np.float16).view(np.uint8)
return np.concatenate([d, m, qs], axis=-1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) m, qs = np.hsplit(rest, [2])
d = d.view(np.float16).astype(np.float32) m = m.view(np.float16).astype(np.float32)
qs = qs.reshape((n_blocks, -1, 1, cls.block_size // 2)) >> np.array([0, 4], dtype=np.uint8).reshape( (1, 1, 2, 1) ) qs = (qs & np.uint8(0x0F)).reshape((n_blocks, -1)).astype(np.float32)
return (d * qs) + m
class Q5_0(__Quant, qtype=GGMLQuantizationType.Q5_0): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
imax = abs(blocks).argmax(axis=-1, keepdims=True) max = np.take_along_axis(blocks, imax, axis=-1)
d = max / -16 with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) # FIXME: Q5_0's reference rounding is cursed and depends on FMA q = ( np.trunc((np.float64(blocks) * np.float64(id)) + np.float64(16.5), dtype=np.float32) .astype(np.uint8) .clip(0, 31) )
qs = q.reshape((n_blocks, 2, cls.block_size // 2)) qs = (qs[..., 0, :] & np.uint8(0x0F)) | (qs[..., 1, :] << np.uint8(4))
qh = np.packbits(q.reshape((n_blocks, 1, 32)) >> np.uint8(4), axis=-1, bitorder="little").reshape(n_blocks, 4)
d = d.astype(np.float16).view(np.uint8)
return np.concatenate([d, qh, qs], axis=-1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) qh, qs = np.hsplit(rest, [4])
d = d.view(np.float16).astype(np.float32) qh = qh.view(np.uint32)
qh = qh.reshape((n_blocks, 1)) >> np.array([i for i in range(32)], dtype=np.uint32).reshape((1, 32)) ql = qs.reshape((n_blocks, -1, 1, cls.block_size // 2)) >> np.array([0, 4], dtype=np.uint8).reshape( (1, 1, 2, 1) ) qh = (qh & np.uint32(0x01)).astype(np.uint8) ql = (ql & np.uint8(0x0F)).reshape((n_blocks, -1))
qs = (ql | (qh << np.uint8(4))).astype(np.int8) - np.int8(16)
return d * qs.astype(np.float32)
class Q5_1(__Quant, qtype=GGMLQuantizationType.Q5_1): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
max = blocks.max(axis=-1, keepdims=True) min = blocks.min(axis=-1, keepdims=True)
d = (max - min) / 31 with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) q = np.trunc((blocks - min) * id + np.float32(0.5), dtype=np.float32).astype(np.uint8).clip(0, 31)
qs = q.reshape((n_blocks, 2, cls.block_size // 2)) qs = (qs[..., 0, :] & np.uint8(0x0F)) | (qs[..., 1, :] << np.uint8(4))
qh = np.packbits(q.reshape((n_blocks, 1, 32)) >> np.uint8(4), axis=-1, bitorder="little").reshape(n_blocks, 4)
d = d.astype(np.float16).view(np.uint8) m = min.astype(np.float16).view(np.uint8)
return np.concatenate([d, m, qh, qs], axis=-1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) m, rest = np.hsplit(rest, [2]) qh, qs = np.hsplit(rest, [4])
d = d.view(np.float16).astype(np.float32) m = m.view(np.float16).astype(np.float32) qh = qh.view(np.uint32)
qh = qh.reshape((n_blocks, 1)) >> np.array([i for i in range(32)], dtype=np.uint32).reshape((1, 32)) ql = qs.reshape((n_blocks, -1, 1, cls.block_size // 2)) >> np.array([0, 4], dtype=np.uint8).reshape( (1, 1, 2, 1) ) qh = (qh & np.uint32(0x01)).astype(np.uint8) ql = (ql & np.uint8(0x0F)).reshape((n_blocks, -1))
qs = (ql | (qh << np.uint8(4))).astype(np.float32)
return (d * qs) + m
class Q8_0(__Quant, qtype=GGMLQuantizationType.Q8_0): @classmethod # Implementation of Q8_0 with bit-exact same results as reference implementation in ggml-quants.c def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: d = abs(blocks).max(axis=1, keepdims=True) / 127 with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) qs = np_roundf(blocks * id)
# (n_blocks, 2) d = d.astype(np.float16).view(np.uint8) # (n_blocks, block_size) qs = qs.astype(np.int8).view(np.uint8)
return np.concatenate([d, qs], axis=1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: d, x = np.split(blocks, [2], axis=1) d = d.view(np.float16).astype(np.float32) x = x.view(np.int8).astype(np.float32)
return x * d
class Q2_K(__Quant, qtype=GGMLQuantizationType.Q2_K): @classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
scales, rest = np.hsplit(blocks, [QK_K // 16]) qs, rest = np.hsplit(rest, [QK_K // 4]) d, dmin = np.hsplit(rest, [2])
d = d.view(np.float16).astype(np.float32) dmin = dmin.view(np.float16).astype(np.float32)
# (n_blocks, 16, 1) dl = (d * (scales & 0xF).astype(np.float32)).reshape((n_blocks, QK_K // 16, 1)) ml = (dmin * (scales >> 4).astype(np.float32)).reshape((n_blocks, QK_K // 16, 1))
shift = np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 1, 4, 1))
qs = (qs.reshape((n_blocks, -1, 1, 32)) >> shift) & np.uint8(3)
qs = qs.reshape((n_blocks, QK_K // 16, 16)).astype(np.float32)
qs = dl * qs - ml
return qs.reshape((n_blocks, -1))
class Q3_K(__Quant, qtype=GGMLQuantizationType.Q3_K): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: """ Quantizes a numpy array of floats into Q3_K format. Vectorized implementation of the C++ reference code. """ n_blocks = blocks.shape[0] sub_blocks = blocks.reshape((n_blocks, 16, 16))
# --- Vectorized make_qx_quants logic for per-sub-block scales --- nmax_data = 4 # Quantization range for data: [-4, 3]
flat_sub_blocks = sub_blocks.reshape(-1, 16) weights_data = flat_sub_blocks * flat_sub_blocks # rmse_type=1 uses w=x*x
# Find max absolute values for each sub-block abs_sub_blocks = np.abs(flat_sub_blocks) max_indices = np.argmax(abs_sub_blocks, axis=-1, keepdims=True) max_vals = np.take_along_axis(flat_sub_blocks, max_indices, axis=-1)
# Iteratively find the best scale for each sub-block with np.errstate(divide="ignore", invalid="ignore"): initial_iscale = np.where(max_vals == 0, 0, -nmax_data / max_vals)
# Initial calculation (is=0) l = np_roundf(flat_sub_blocks * initial_iscale).clip(-nmax_data, nmax_data - 1) sumlx = np.sum(weights_data * flat_sub_blocks * l, axis=-1) suml2 = np.sum(weights_data * l * l, axis=-1)
with np.errstate(divide="ignore", invalid="ignore"): current_scales = np.divide(sumlx, suml2, out=np.zeros_like(sumlx), where=suml2 != 0)
best_scores = current_scales * sumlx best_scales = current_scales.copy()
# Iterative search over potential iscale adjustments for is_ in range(-9, 10): if is_ == 0: continue with np.errstate(divide="ignore", invalid="ignore"): iscale_try = -(nmax_data + 0.1 * is_) / max_vals iscale_try[max_vals == 0] = 0
l_try = np_roundf(flat_sub_blocks * iscale_try).clip(-nmax_data, nmax_data - 1) sumlx_try = np.sum(weights_data * flat_sub_blocks * l_try, axis=-1) suml2_try = np.sum(weights_data * l_try * l_try, axis=-1)
improvement_mask = (suml2_try > 0) & (sumlx_try * sumlx_try * suml2 > best_scores * suml2_try) if np.any(improvement_mask): with np.errstate(divide="ignore", invalid="ignore"): scales_try = np.divide(sumlx_try, suml2_try, out=np.zeros_like(sumlx_try), where=suml2_try != 0) best_scores[improvement_mask] = (scales_try * sumlx_try)[improvement_mask] best_scales[improvement_mask] = scales_try[improvement_mask] # Update suml2 for the next comparison in the loop suml2[improvement_mask] = suml2_try[improvement_mask]
scales = best_scales.reshape(n_blocks, 16)
# --- Vectorized logic to quantize the scales themselves --- nmax_scales = 32 # Quantization range for scales: [-32, 31] abs_scales = np.abs(scales) max_scale_indices = np.argmax(abs_scales, axis=-1, keepdims=True) max_scale_vals = np.take_along_axis(scales, max_scale_indices, axis=-1)
with np.errstate(divide="ignore", invalid="ignore"): iscale_s = np.where(max_scale_vals == 0, 0, -nmax_scales / max_scale_vals)
l_s = np_roundf(scales * iscale_s).clip(-nmax_scales, nmax_scales - 1) d_val = np.divide( np.sum(scales * l_s, axis=-1, keepdims=True), np.sum(l_s * l_s, axis=-1, keepdims=True), out=np.zeros((n_blocks, 1)), where=np.sum(l_s * l_s, axis=-1, keepdims=True) != 0, )
# Pack the 6-bit quantized scales into 12 bytes l = (l_s + 32).astype(np.uint8) scales_packed = np.zeros((n_blocks, 12), dtype=np.uint8) l_low = l & 0x0F l_high = (l >> 4) & 0x03 scales_packed[:, 0:8] = l_low[:, 0:8] | (l_low[:, 8:16] << 4) l_high_reshaped = l_high.reshape(n_blocks, 4, 4).transpose(0, 2, 1) packed_high_bits = ( l_high_reshaped[:, :, 0] | (l_high_reshaped[:, :, 1] << 2) | (l_high_reshaped[:, :, 2] << 4) | (l_high_reshaped[:, :, 3] << 6) ) scales_packed[:, 8:12] = packed_high_bits d = d_val.astype(np.float16).view(np.uint8)
# --- Re-quantize data with final scales and pack --- sc_dequant = (l.astype(np.int8) - 32).astype(np.float32) d_eff = (d_val * sc_dequant).reshape(n_blocks, 16, 1)
with np.errstate(divide="ignore", invalid="ignore"): l_data_float = np.divide(sub_blocks, d_eff, out=np.zeros_like(sub_blocks), where=d_eff != 0)
l_data = (np.clip(np_roundf(l_data_float), -4, 3) + 4).astype(np.uint8) l_data = l_data.reshape(n_blocks, 256)
# hmask stores the 3rd bit hmask_values = (l_data > 3).reshape(n_blocks, 8, 32).transpose(0, 2, 1) hmask = np.packbits(hmask_values, axis=-1, bitorder="little").reshape(n_blocks, -1)
# qs stores the lower 2 bits l_data[l_data > 3] -= 4 l_data_low = (l_data & 0x03).reshape(n_blocks, 2, 4, 32) qs_parts = ( l_data_low[:, :, 0, :] | (l_data_low[:, :, 1, :] << 2) | (l_data_low[:, :, 2, :] << 4) | (l_data_low[:, :, 3, :] << 6) ) qs = qs_parts.reshape(n_blocks, 64)
return np.concatenate([hmask, qs, scales_packed, d], axis=1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
hmask, rest = np.hsplit(blocks, [QK_K // 8]) qs, rest = np.hsplit(rest, [QK_K // 4]) scales, d = np.hsplit(rest, [12])
d = d.view(np.float16).astype(np.float32)
# The scales are packed at 6-bit each in this pattern: # 0: IIIIAAAA # 1: JJJJBBBB # 2: KKKKCCCC # 3: LLLLDDDD # 4: MMMMEEEE # 5: NNNNFFFF # 6: OOOOGGGG # 7: PPPPHHHH # 8: MMIIEEAA # 9: NNJJFFBB # 10: OOKKGGCC # 11: PPLLHHDD lscales, hscales = np.hsplit(scales, [8]) lscales = lscales.reshape((n_blocks, 1, 8)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 2, 1)) lscales = lscales.reshape((n_blocks, 16)) hscales = hscales.reshape((n_blocks, 1, 4)) >> np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 4, 1)) hscales = hscales.reshape((n_blocks, 16)) scales = (lscales & np.uint8(0x0F)) | ((hscales & np.uint8(0x03)) << np.uint8(4)) scales = (scales.astype(np.int8) - np.int8(32)).astype(np.float32)
dl = (d * scales).reshape((n_blocks, 16, 1))
ql = qs.reshape((n_blocks, -1, 1, 32)) >> np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 1, 4, 1)) qh = hmask.reshape(n_blocks, -1, 1, 32) >> np.array([i for i in range(8)], dtype=np.uint8).reshape( (1, 1, 8, 1) ) ql = ql.reshape((n_blocks, 16, QK_K // 16)) & np.uint8(3) qh = qh.reshape((n_blocks, 16, QK_K // 16)) & np.uint8(1) qh = qh ^ np.uint8(1) # strangely, the offset is zero when the bitmask is 1 q = (ql.astype(np.int8) - (qh << np.uint8(2)).astype(np.int8)).astype(np.float32)
return (dl * q).reshape((n_blocks, QK_K))
class Q4_K(__Quant, qtype=GGMLQuantizationType.Q4_K): K_SCALE_SIZE = 12 QK_K = QK_K # Block size
@classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: """ Quantizes a numpy array of floats into Q4_K format. Vectorized implementation inspired by the C++ reference code. """ if blocks.shape[-1] % cls.QK_K != 0: raise ValueError( f"The last dimension of the input array must be a multiple of {cls.QK_K}, but got {blocks.shape[-1]}" )
n_blocks = blocks.size // cls.QK_K sub_blocks = blocks.reshape((n_blocks, 8, 32))
# --- Vectorized make_qkx2_quants logic --- nmax = 15 rmin = -1.0 rdelta = 0.1 nstep = 20
# Calculate weights for all sub-blocks sum_x2 = np.sum(sub_blocks * sub_blocks, axis=-1, keepdims=True) # Use np.maximum to avoid sqrt of negative number due to float precision av_x = np.sqrt(np.maximum(0, sum_x2 / 32.0)) weights = av_x + np.abs(sub_blocks) sum_w = np.sum(weights, axis=-1, keepdims=True) sum_x = np.sum(weights * sub_blocks, axis=-1, keepdims=True)
# Initial guess for scales and mins min_v = np.min(sub_blocks, axis=-1, keepdims=True) max_v = np.max(sub_blocks, axis=-1, keepdims=True) min_v[min_v > 0] = 0.0
max_minus_min = max_v - min_v
# Handle cases where all values in a sub-block are the same is_flat = max_minus_min < 1e-8 max_minus_min[is_flat] = 1.0 # Avoid division by zero
with np.errstate(divide="ignore"): iscale = nmax / max_minus_min scale = 1.0 / iscale scale[is_flat] = 0.0
l_current = np_roundf(iscale * (sub_blocks - min_v)).clip(0, nmax).astype(np.uint8) diff = scale * l_current + min_v - sub_blocks best_mse = np.sum(weights * (diff * diff), axis=-1)
scale_best = scale.squeeze(-1) min_best = min_v.squeeze(-1)
# Iterative search loop over all sub-blocks at once for is_ in range(nstep + 1): with np.errstate(divide="ignore"): current_iscale = (rmin + rdelta * is_ + nmax) / max_minus_min current_iscale[is_flat] = 0.0
l_aux = np_roundf(current_iscale * (sub_blocks - min_v)).clip(0, nmax).astype(np.uint8)
w_l = weights * l_aux sum_l = np.sum(w_l, axis=-1, keepdims=True) sum_l2 = np.sum(w_l * l_aux, axis=-1, keepdims=True) sum_xl = np.sum(w_l * sub_blocks, axis=-1, keepdims=True)
D = sum_w * sum_l2 - sum_l * sum_l
valid_D_mask = D > 0 # Use np.where for safe division, filling invalid entries with 0 this_scale = np.divide((sum_w * sum_xl - sum_x * sum_l), D, out=np.zeros_like(D), where=valid_D_mask) this_min = np.divide((sum_l2 * sum_x - sum_l * sum_xl), D, out=np.zeros_like(D), where=valid_D_mask)
# Handle case where candidate min > 0 min_gt_zero_mask = valid_D_mask & (this_min > 0) if np.any(min_gt_zero_mask): recalc_scale = np.divide(sum_xl, sum_l2, out=np.zeros_like(sum_xl), where=sum_l2 > 0) this_scale = np.where(min_gt_zero_mask, recalc_scale, this_scale) this_min = np.where(min_gt_zero_mask, 0.0, this_min)
# Calculate current MSE diff = this_scale * l_aux + this_min - sub_blocks current_mse = np.sum(weights * (diff * diff), axis=-1)
# Update best values where MSE has improved improvement_mask = valid_D_mask.squeeze(-1) & (current_mse < best_mse) if np.any(improvement_mask): best_mse[improvement_mask] = current_mse[improvement_mask] scale_best[improvement_mask] = this_scale.squeeze(-1)[improvement_mask] min_best[improvement_mask] = this_min.squeeze(-1)[improvement_mask]
scales_all = scale_best mins_all = -min_best # --- End of vectorized search ---
# Find block-level d and dmin max_scale_per_block = np.max(scales_all, axis=1, keepdims=True) max_min_per_block = np.max(mins_all, axis=1, keepdims=True)
# Quantize and pack scales and mins with np.errstate(divide="ignore", invalid="ignore"): inv_scale = np.where(max_scale_per_block == 0, 0, 63.0 / max_scale_per_block) inv_min = np.where(max_min_per_block == 0, 0, 63.0 / max_min_per_block)
ls = np.clip(np_roundf(scales_all * inv_scale), 0, 63).astype(np.uint8) lm = np.clip(np_roundf(mins_all * inv_min), 0, 63).astype(np.uint8)
scales_packed = np.zeros((n_blocks, cls.K_SCALE_SIZE), dtype=np.uint8) scales_packed[:, 0:4] = ls[:, 0:4] & 0x3F scales_packed[:, 4:8] = lm[:, 0:4] & 0x3F scales_packed[:, 8:12] = (ls[:, 4:8] & 0x0F) | ((lm[:, 4:8] & 0x0F) << 4) scales_packed[:, 0:4] |= (ls[:, 4:8] >> 4) << 6 scales_packed[:, 4:8] |= (lm[:, 4:8] >> 4) << 6
# Store block-level d and dmin with np.errstate(divide="ignore", invalid="ignore"): d_val = np.where(max_scale_per_block == 0, 0, max_scale_per_block / 63.0) dmin_val = np.where(max_min_per_block == 0, 0, max_min_per_block / 63.0)
d = d_val.reshape(n_blocks, 1).astype(np.float16).view(np.uint8) dmin = dmin_val.reshape(n_blocks, 1).astype(np.float16).view(np.uint8)
# Re-quantize the actual data d_eff = (d_val * ls.astype(np.float32)).reshape(n_blocks, 8, 1) m_eff = (dmin_val * lm.astype(np.float32)).reshape(n_blocks, 8, 1)
with np.errstate(divide="ignore", invalid="ignore"): L_float = np.divide(sub_blocks + m_eff, d_eff, out=np.zeros_like(sub_blocks), where=d_eff != 0)
L = np.clip(np_roundf(L_float), 0, 15).astype(np.uint8)
# Pack the 4-bit quantized data L_reshaped = L.reshape((n_blocks, cls.QK_K // 64, 2, 32)) L_low = L_reshaped[:, :, 0, :].reshape(n_blocks, -1) L_high = L_reshaped[:, :, 1, :].reshape(n_blocks, -1) qs = L_low | (L_high << 4)
# Assemble and return the final block return np.concatenate([d, dmin, scales_packed, qs], axis=1)
@staticmethod def get_scale_min(scales: np.ndarray) -> tuple[np.ndarray, np.ndarray]: n_blocks = scales.shape[0] s = scales.view(np.uint8).reshape(n_blocks, Q4_K.K_SCALE_SIZE)
sc = np.zeros((n_blocks, 8), dtype=np.uint8) m = np.zeros((n_blocks, 8), dtype=np.uint8)
sc[:, 0:4] = s[:, 0:4] & 0x3F m[:, 0:4] = s[:, 4:8] & 0x3F
sc[:, 4:8] = (s[:, 8:12] & 0x0F) | ((s[:, 0:4] >> 6) << 4) m[:, 4:8] = (s[:, 8:12] >> 4) | ((s[:, 4:8] >> 6) << 4)
return sc, m
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) dmin, rest = np.hsplit(rest, [2]) scales, qs = np.hsplit(rest, [cls.K_SCALE_SIZE])
d = d.view(np.float16).astype(np.float32) dmin = dmin.view(np.float16).astype(np.float32)
sc, m = cls.get_scale_min(scales)
d_eff = (d * sc.astype(np.float32)).reshape((n_blocks, 8, 1)) dm_eff = (dmin * m.astype(np.float32)).reshape((n_blocks, 8, 1))
# Unpack 4-bit values and arrange back into sub-blocks qs_reshaped = qs.reshape(n_blocks, QK_K // 64, 32) qs_unpacked = np.empty((n_blocks, 8, 32), dtype=np.float32) qs_unpacked[:, [0, 2, 4, 6], :] = qs_reshaped & 0x0F qs_unpacked[:, [1, 3, 5, 7], :] = qs_reshaped >> 4
return (d_eff * qs_unpacked - dm_eff).reshape((n_blocks, QK_K))
class Q5_K(__Quant, qtype=GGMLQuantizationType.Q5_K): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: """ Quantizes a numpy array of floats into Q5_K format. Vectorized implementation of the C++ reference code. """ if blocks.shape[-1] % QK_K != 0: raise ValueError( f"The last dimension of the input array must be a multiple of {QK_K}, but got {blocks.shape[-1]}" )
n_blocks = blocks.size // QK_K sub_blocks = blocks.reshape((n_blocks, 8, 32))
# --- Vectorized make_qkx3_quants logic for 5 bits --- nmax = 31 nstep = 36 rmin = -0.9 rdelta = 0.05
# Calculate weights for all sub-blocks sum_x2 = np.sum(sub_blocks * sub_blocks, axis=-1, keepdims=True) av_x = np.sqrt(np.maximum(0, 2 * sum_x2 / QK_K)) # sigma calculation from C++ weights = av_x + np.abs(sub_blocks) sum_w = np.sum(weights, axis=-1, keepdims=True) sum_x = np.sum(weights * sub_blocks, axis=-1, keepdims=True)
min_v = np.min(sub_blocks, axis=-1, keepdims=True) max_v = np.max(sub_blocks, axis=-1, keepdims=True) min_v[min_v > 0] = 0.0
max_minus_min = max_v - min_v is_flat = max_minus_min < 1e-8 max_minus_min[is_flat] = 1.0
# Initial mse for comparison with np.errstate(divide="ignore"): iscale_initial = nmax / max_minus_min scale_initial = 1.0 / iscale_initial scale_initial[is_flat] = 0.0 l_initial = np_roundf(iscale_initial * (sub_blocks - min_v)).clip(0, nmax).astype(np.uint8) diff = scale_initial * l_initial + min_v - sub_blocks best_mse = np.sum(weights * (diff * diff), axis=-1)
scale_best = scale_initial.squeeze(-1) min_best = min_v.squeeze(-1)
# Iterative search for is_ in range(nstep + 1): with np.errstate(divide="ignore"): current_iscale = (rmin + rdelta * is_ + nmax) / max_minus_min current_iscale[is_flat] = 0.0
l_aux = np_roundf(current_iscale * (sub_blocks - min_v)).clip(0, nmax).astype(np.uint8) w_l = weights * l_aux sum_l = np.sum(w_l, axis=-1, keepdims=True) sum_l2 = np.sum(w_l * l_aux, axis=-1, keepdims=True) sum_xl = np.sum(w_l * sub_blocks, axis=-1, keepdims=True)
D = sum_w * sum_l2 - sum_l * sum_l valid_D_mask = D > 0 this_scale = np.divide((sum_w * sum_xl - sum_x * sum_l), D, out=np.zeros_like(D), where=valid_D_mask) this_min = np.divide((sum_l2 * sum_x - sum_l * sum_xl), D, out=np.zeros_like(D), where=valid_D_mask)
min_gt_zero_mask = valid_D_mask & (this_min > 0) if np.any(min_gt_zero_mask): recalc_scale = np.divide(sum_xl, sum_l2, out=np.zeros_like(sum_xl), where=sum_l2 > 0) this_scale = np.where(min_gt_zero_mask, recalc_scale, this_scale) this_min = np.where(min_gt_zero_mask, 0.0, this_min)
diff = this_scale * l_aux + this_min - sub_blocks current_mse = np.sum(weights * (diff * diff), axis=-1) improvement_mask = valid_D_mask.squeeze(-1) & (current_mse < best_mse) if np.any(improvement_mask): best_mse[improvement_mask] = current_mse[improvement_mask] scale_best[improvement_mask] = this_scale.squeeze(-1)[improvement_mask] min_best[improvement_mask] = this_min.squeeze(-1)[improvement_mask]
scales_all = scale_best mins_all = -min_best
# --- Quantize and pack scales/mins (identical to Q4_K) --- max_scale_per_block = np.max(scales_all, axis=1, keepdims=True) max_min_per_block = np.max(mins_all, axis=1, keepdims=True) with np.errstate(divide="ignore", invalid="ignore"): inv_scale = np.where(max_scale_per_block == 0, 0, 63.0 / max_scale_per_block) inv_min = np.where(max_min_per_block == 0, 0, 63.0 / max_min_per_block) ls = np.clip(np_roundf(scales_all * inv_scale), 0, 63).astype(np.uint8) lm = np.clip(np_roundf(mins_all * inv_min), 0, 63).astype(np.uint8)
scales_packed = np.zeros((n_blocks, Q4_K.K_SCALE_SIZE), dtype=np.uint8) scales_packed[:, 0:4] = ls[:, 0:4] & 0x3F scales_packed[:, 4:8] = lm[:, 0:4] & 0x3F scales_packed[:, 8:12] = (ls[:, 4:8] & 0x0F) | ((lm[:, 4:8] & 0x0F) << 4) scales_packed[:, 0:4] |= (ls[:, 4:8] >> 4) << 6 scales_packed[:, 4:8] |= (lm[:, 4:8] >> 4) << 6
# --- Store block-level d and dmin (identical to Q4_K) --- with np.errstate(divide="ignore", invalid="ignore"): d_val = np.where(max_scale_per_block == 0, 0, max_scale_per_block / 63.0) dmin_val = np.where(max_min_per_block == 0, 0, max_min_per_block / 63.0) d = d_val.reshape(n_blocks, 1).astype(np.float16).view(np.uint8) dmin = dmin_val.reshape(n_blocks, 1).astype(np.float16).view(np.uint8)
# --- Re-quantize the actual data to 5 bits --- d_eff = (d_val * ls.astype(np.float32)).reshape(n_blocks, 8, 1) m_eff = (dmin_val * lm.astype(np.float32)).reshape(n_blocks, 8, 1) with np.errstate(divide="ignore", invalid="ignore"): L_float = np.divide(sub_blocks + m_eff, d_eff, out=np.zeros_like(sub_blocks), where=d_eff != 0) L = np.clip(np_roundf(L_float), 0, 31).astype(np.uint8)
# --- Pack the 5-bit quantized data into qh and qs --- # qh (high bits) h = (L > 15).astype(np.uint8) h_reshaped = h.reshape(n_blocks, 8, 32).transpose(0, 2, 1) bit_shifts = 2 ** np.arange(8, dtype=np.uint8).reshape(1, 1, 8) qh = np.sum(h_reshaped * bit_shifts, axis=-1).astype(np.uint8)
# qs (low bits) L[L > 15] -= 16 l_reshaped = L.reshape(n_blocks, 8, 32) part1 = l_reshaped[:, ::2, :].reshape(n_blocks, -1) part2 = l_reshaped[:, 1::2, :].reshape(n_blocks, -1) qs = part1 | (part2 << 4)
return np.concatenate([d, dmin, scales_packed, qh, qs], axis=1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) dmin, rest = np.hsplit(rest, [2]) scales, rest = np.hsplit(rest, [Q4_K.K_SCALE_SIZE]) qh, qs = np.hsplit(rest, [QK_K // 8])
d = d.view(np.float16).astype(np.float32) dmin = dmin.view(np.float16).astype(np.float32)
sc, m = Q4_K.get_scale_min(scales)
d_eff = (d * sc.astype(np.float32)).reshape((n_blocks, -1, 1)) dm_eff = (dmin * m.astype(np.float32)).reshape((n_blocks, -1, 1))
# Unpack high bits (qh) bit_shifts = 2 ** np.arange(8, dtype=np.uint8).reshape(1, 1, 8) qh_unpacked = (qh[:, :, np.newaxis] & bit_shifts) != 0 qh_unpacked = qh_unpacked.transpose(0, 2, 1).reshape(n_blocks, -1, 32)
# Unpack low bits (qs) ql_unpacked = np.empty((n_blocks, 8, 32), dtype=np.uint8) qs_reshaped = qs.reshape(n_blocks, 4, 32) ql_unpacked[:, ::2, :] = qs_reshaped & 0x0F ql_unpacked[:, 1::2, :] = qs_reshaped >> 4
# Combine high and low bits and dequantize q = (ql_unpacked + (qh_unpacked * 16)).astype(np.float32) return (d_eff * q - dm_eff).reshape((n_blocks, QK_K))
class Q6_K(__Quant, qtype=GGMLQuantizationType.Q6_K): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: """ Quantizes a numpy array of floats into Q6_K format. Vectorized implementation of the C++ reference code. """ n_blocks = blocks.shape[0] # Reshape for sub-block processing sub_blocks = blocks.reshape(n_blocks * 16, 16)
# --- Vectorized `make_qx_quants` for all sub-blocks to find initial scales --- nmax_data = 32 # For Q6_K, data range is [-32, 31]
# Weights are x*x for the reference implementation weights_data = sub_blocks * sub_blocks
# Find max absolute values for each sub-block to determine the initial scale abs_sub_blocks = np.abs(sub_blocks) max_indices = np.argmax(abs_sub_blocks, axis=-1, keepdims=True) max_vals = np.take_along_axis(sub_blocks, max_indices, axis=-1)
is_zero_mask = np.abs(max_vals) < 1e-15
with np.errstate(divide="ignore", invalid="ignore"): initial_iscale = np.where(is_zero_mask, 0, -nmax_data / max_vals)
# Use np.round for round-half-to-even, matching C's nearest_int l = np.round(sub_blocks * initial_iscale).clip(-nmax_data, nmax_data - 1) sumlx = np.sum(weights_data * sub_blocks * l, axis=-1) suml2 = np.sum(weights_data * l * l, axis=-1)
with np.errstate(divide="ignore", invalid="ignore"): scales_cand = np.divide(sumlx, suml2, out=np.zeros_like(sumlx), where=suml2 != 0) best_scores = scales_cand * sumlx best_l = l.copy()
# Iterative search over potential iscale adjustments for is_ in range(-9, 10): if is_ == 0: continue with np.errstate(divide="ignore", invalid="ignore"): iscale_try = np.where(is_zero_mask, 0, -(nmax_data + 0.1 * is_) / max_vals)
l_try = np.round(sub_blocks * iscale_try).clip(-nmax_data, nmax_data - 1) sumlx_try = np.sum(weights_data * sub_blocks * l_try, axis=-1) suml2_try = np.sum(weights_data * l_try * l_try, axis=-1)
improvement_mask = (suml2_try > 0) & (sumlx_try * sumlx_try * suml2 > best_scores * suml2_try) if np.any(improvement_mask): with np.errstate(divide="ignore", invalid="ignore"): new_best_scores = np.divide(sumlx_try * sumlx_try, suml2_try, where=suml2_try > 0) best_scores[improvement_mask] = new_best_scores[improvement_mask] best_l[improvement_mask] = l_try[improvement_mask] suml2[improvement_mask] = suml2_try[improvement_mask]
# Recompute final best scales from the best quants (best_l) sumlx_final = np.sum(weights_data * sub_blocks * best_l, axis=-1) suml2_final = np.sum(weights_data * best_l * best_l, axis=-1) with np.errstate(divide="ignore", invalid="ignore"): scales = np.divide(sumlx_final, suml2_final, out=np.zeros_like(sumlx_final), where=suml2_final != 0)
scales[np.all(sub_blocks == 0, axis=-1)] = 0.0 scales = scales.reshape(n_blocks, 16)
# --- Quantize the scales themselves --- abs_scales = np.abs(scales) max_abs_scale_indices = np.argmax(abs_scales, axis=-1, keepdims=True) max_scale_vals = np.take_along_axis(scales, max_abs_scale_indices, axis=-1)
with np.errstate(divide="ignore", invalid="ignore"): is_zero_mask = np.abs(max_scale_vals) < 1e-15 iscale_s = np.where(is_zero_mask, 0, -128.0 / max_scale_vals) d_val = np.where(is_zero_mask, 0, max_scale_vals / -128.0)
quantized_scales = np.round(scales * iscale_s).clip(-128, 127).astype(np.int8) d = d_val.astype(np.float16).view(np.uint8)
# --- Re-quantize original data with final scales --- d_sub = d_val * quantized_scales.astype(np.float32) d_sub_reshaped = d_sub.reshape(n_blocks, 16, 1)
sub_blocks_reshaped = blocks.reshape(n_blocks, 16, 16) with np.errstate(divide="ignore", invalid="ignore"): l_float = np.divide( sub_blocks_reshaped, d_sub_reshaped, out=np.zeros_like(sub_blocks_reshaped), where=d_sub_reshaped != 0 )
l_final = np.round(l_float).clip(-32, 31).astype(np.int8) L = (l_final + 32).astype(np.uint8).reshape(n_blocks, 256)
# --- Pack the 6-bit quantized data --- L_reshaped = L.reshape(n_blocks, 2, 4, 32) L_low = L_reshaped & 0xF L_high = L_reshaped >> 4
# Pack lower 4 bits into ql ql = np.empty((n_blocks, 128), dtype=np.uint8) ql[:, 0:32] = L_low[:, 0, 0, :] | (L_low[:, 0, 2, :] << 4) ql[:, 32:64] = L_low[:, 0, 1, :] | (L_low[:, 0, 3, :] << 4) ql[:, 64:96] = L_low[:, 1, 0, :] | (L_low[:, 1, 2, :] << 4) ql[:, 96:128] = L_low[:, 1, 1, :] | (L_low[:, 1, 3, :] << 4)
# Pack higher 2 bits into qh qh_packed = ( L_high[:, :, 0, :] | (L_high[:, :, 1, :] << 2) | (L_high[:, :, 2, :] << 4) | (L_high[:, :, 3, :] << 6) ) qh = qh_packed.reshape(n_blocks, -1)
# Final assembly: view scales as uint8 before concatenating return np.concatenate([ql, qh, quantized_scales.view(np.uint8), d], axis=1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
ql, rest = np.hsplit(blocks, [QK_K // 2]) qh, rest = np.hsplit(rest, [QK_K // 4]) scales, d = np.hsplit(rest, [QK_K // 16])
scales = scales.view(np.int8).astype(np.float32) d = d.view(np.float16).astype(np.float32) d = (d * scales).reshape((n_blocks, QK_K // 16, 1))
ql = ql.reshape((n_blocks, -1, 1, 64)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2, 1)) ql = (ql & np.uint8(0x0F)).reshape((n_blocks, -1, 32)) qh = qh.reshape((n_blocks, -1, 1, 32)) >> np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 1, 4, 1)) qh = (qh & np.uint8(0x03)).reshape((n_blocks, -1, 32)) q = (ql | (qh << np.uint8(4))).astype(np.int8) - np.int8(32) q = q.reshape((n_blocks, QK_K // 16, -1)).astype(np.float32)
return (d * q).reshape((n_blocks, QK_K))
class TQ1_0(__Quant, qtype=GGMLQuantizationType.TQ1_0): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d = abs(blocks).max(axis=-1, keepdims=True) with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) qs = np_roundf(blocks * id) qs = (qs.astype(np.int8) + np.int8(1)).astype(np.uint8)
qs0, qs1, qh = qs[..., : (32 * 5)], qs[..., (32 * 5) : (48 * 5)], qs[..., (48 * 5) :] qs0 = qs0.reshape((n_blocks, -1, 5, 32)) * np.array([81, 27, 9, 3, 1], dtype=np.uint8).reshape((1, 1, 5, 1)) qs0 = np.sum(qs0, axis=-2).reshape((n_blocks, -1)) qs1 = qs1.reshape((n_blocks, -1, 5, 16)) * np.array([81, 27, 9, 3, 1], dtype=np.uint8).reshape((1, 1, 5, 1)) qs1 = np.sum(qs1, axis=-2).reshape((n_blocks, -1)) qh = qh.reshape((n_blocks, -1, 4, 4)) * np.array([81, 27, 9, 3], dtype=np.uint8).reshape((1, 1, 4, 1)) qh = np.sum(qh, axis=-2).reshape((n_blocks, -1)) qs = np.concatenate([qs0, qs1, qh], axis=-1) qs = (qs.astype(np.uint16) * 256 + (243 - 1)) // 243
qs = qs.astype(np.uint8) d = d.astype(np.float16).view(np.uint8)
return np.concatenate([qs, d], axis=-1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
qs, rest = np.hsplit(blocks, [(QK_K - 4 * QK_K // 64) // 5]) qh, d = np.hsplit(rest, [QK_K // 64])
d = d.view(np.float16).astype(np.float32)
qs0, qs1 = qs[..., :32], qs[..., 32:] qs0 = qs0.reshape((n_blocks, -1, 1, 32)) * np.array([1, 3, 9, 27, 81], dtype=np.uint8).reshape((1, 1, 5, 1)) qs0 = qs0.reshape((n_blocks, -1)) qs1 = qs1.reshape((n_blocks, -1, 1, 16)) * np.array([1, 3, 9, 27, 81], dtype=np.uint8).reshape((1, 1, 5, 1)) qs1 = qs1.reshape((n_blocks, -1)) qh = qh.reshape((n_blocks, -1, 1, 4)) * np.array([1, 3, 9, 27], dtype=np.uint8).reshape((1, 1, 4, 1)) qh = qh.reshape((n_blocks, -1)) qs = np.concatenate([qs0, qs1, qh], axis=-1) qs = ((qs.astype(np.uint16) * 3) >> 8).astype(np.int8) - np.int8(1)
return d * qs.astype(np.float32)
class TQ2_0(__Quant, qtype=GGMLQuantizationType.TQ2_0): @classmethod def quantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d = abs(blocks).max(axis=-1, keepdims=True) with np.errstate(divide="ignore"): id = np.where(d == 0, 0, 1 / d) qs = np_roundf(blocks * id) qs = (qs.astype(np.int8) + np.int8(1)).astype(np.uint8)
qs = qs.reshape((n_blocks, -1, 4, 32)) << np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 1, 4, 1)) qs = qs[..., 0, :] | qs[..., 1, :] | qs[..., 2, :] | qs[..., 3, :] qs = qs.reshape((n_blocks, -1))
d = d.astype(np.float16).view(np.uint8)
return np.concatenate([qs, d], axis=-1)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
qs, d = np.hsplit(blocks, [QK_K // 4])
d = d.view(np.float16).astype(np.float32)
qs = qs.reshape((n_blocks, -1, 1, 32)) >> np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 1, 4, 1)) qs = (qs & 0x03).reshape((n_blocks, -1)).astype(np.int8) - np.int8(1)
return d * qs.astype(np.float32)
class IQ2_XXS(__Quant, qtype=GGMLQuantizationType.IQ2_XXS): ksigns: bytes = ( b"\x00\x81\x82\x03\x84\x05\x06\x87\x88\x09\x0a\x8b\x0c\x8d\x8e\x0f" b"\x90\x11\x12\x93\x14\x95\x96\x17\x18\x99\x9a\x1b\x9c\x1d\x1e\x9f" b"\xa0\x21\x22\xa3\x24\xa5\xa6\x27\x28\xa9\xaa\x2b\xac\x2d\x2e\xaf" b"\x30\xb1\xb2\x33\xb4\x35\x36\xb7\xb8\x39\x3a\xbb\x3c\xbd\xbe\x3f" b"\xc0\x41\x42\xc3\x44\xc5\xc6\x47\x48\xc9\xca\x4b\xcc\x4d\x4e\xcf" b"\x50\xd1\xd2\x53\xd4\x55\x56\xd7\xd8\x59\x5a\xdb\x5c\xdd\xde\x5f" b"\x60\xe1\xe2\x63\xe4\x65\x66\xe7\xe8\x69\x6a\xeb\x6c\xed\xee\x6f" b"\xf0\x71\x72\xf3\x74\xf5\xf6\x77\x78\xf9\xfa\x7b\xfc\x7d\x7e\xff" )
# iq2xxs_grid, but with each byte of the original packed in 2 bits, # by mapping 0x08 to 0, 0x19 to 1, and 0x2b to 2. grid_shape = (256, 8) grid_map = (0x08, 0x19, 0x2B) grid_hex = ( b"00000200050008000a00110014002000220028002a0041004400500058006100" b"6400800082008a00a20001010401100115014001840198010002020222028202" b"010404041004210424044004420448046004810484049004a404000502050805" b"200546056905800591050906100640068406a406000805080808140828084108" b"440850085208880804094009020a140a01100410101021104010601084109010" b"951000110811201150115a118011241245120014081420142514491480141815" b"6215001616160118041810184018811800190519a019511a002002200a204420" b"6120802082202921482100220222012404241024402456240025412564259026" b"082820289428442a014004401040184021402440404048405640604081408440" b"9040004120416141804185410142104248425642684200440844204480449944" b"124524450046014804481048404845480049584961498249454a904a00500850" b"1150195020508050885004514251a4519152905492540a550156545600581158" b"195864584059085a046010604060686000615561186260620064056410651265" b"84654268008002800a8041808280048118814081118201840484108415844084" b"608400854685948509864086608602880489118a0490109024904090a1901691" b"8091459200942294449451958198209902a050a085a009a100a218a450a804a9" )
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, qs = np.hsplit(blocks, [2])
d = d.view(np.float16).astype(np.float32)
qs = qs.view(np.uint32).reshape(n_blocks, -1, 2)
db = d * (np.float32(0.5) + (qs[..., 1] >> 28).astype(np.float32)) * np.float32(0.25) db = db.reshape((n_blocks, -1, 1, 1))
# get the sign indices and unpack the bits signs = qs[..., 1].reshape((n_blocks, -1, 1)) >> np.array([0, 7, 14, 21], dtype=np.uint32).reshape((1, 1, 4)) ksigns = np.frombuffer(cls.ksigns, dtype=np.uint8).reshape((1, 1, 1, 128)) signs = (signs & np.uint32(0x7F)).reshape((n_blocks, -1, 4, 1)) signs = np.take_along_axis(ksigns, signs, axis=-1) signs = signs.reshape((n_blocks, -1, 4, 1)) >> np.array([i for i in range(8)], dtype=np.uint8).reshape( (1, 1, 1, 8) ) signs = signs & np.uint8(0x01) signs = np.where(signs == 0, np.float32(1), np.float32(-1)) signs = signs.reshape((n_blocks, -1, 4, 8))
assert cls.grid is not None grid = np.take_along_axis(cls.grid, qs[..., 0].copy().view(np.uint8).reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 4, 8))
return (db * grid * signs).reshape((n_blocks, -1))
class IQ2_XS(__Quant, qtype=GGMLQuantizationType.IQ2_XS): # iq2xs_grid, but with each byte of the original packed in 2 bits, # by mapping 0x08 to 0, 0x19 to 1, and 0x2b to 2. grid_shape = (512, 8) grid_map = (0x08, 0x19, 0x2B) grid_hex = ( b"00000200050008000a0011001400160019002000220025002800410044004600" b"49005000520055005800610064008000820085008800910094009900a0000101" b"04010601090110011201150118011a0121012401400142014501480151015401" b"6001680181018401900100020202050208021102140220024102440250025502" b"80028a0201040404060409041004120415041804210424044004420445044804" b"5104540456046004810484049004000502050505080511051405200541054405" b"500561058005010604061006260640064206840600080208050808080a081108" b"14082008250841084408500858088008a008aa08010904091009400981098909" b"000a200a280a960aa00a01100410061009101010121015101810211024104010" b"4210451048105110541060106a10811084109010001102110511081111111411" b"2011411144115011801194119611011204120612101240126012001402140514" b"0814111414142014411444144914501464148014011504151015401500161416" b"49160118041810181218401854188618001905196619511aa91a002002200520" b"08200a201120142020204120442050208020a020012104211021402148216521" b"002222228022a82201240424102429244024002541255225992501261a26a626" b"002808280a28202855288828a22868299029082a202a822a882a8a2a01400440" b"0640094010401240154018402140244040404240454048404a40514054406040" b"6540814084409040004102410541084111411441204141414441504180418541" b"a241014204421042124229424042004402440544084411441444194420444144" b"4444504480449444014504451045244540459a4500460a464446504601480448" b"1048404845485448624800491149444950496949044a00500250055008501150" b"145020502850415044505050805001510451105115514051425100524452aa52" b"0154045410542154405460548154a154005508558055885521566856a1560058" b"14584158505899581a5940594259855a0160046010604060546062608660a960" b"006124624a62926200641664106540654565a46501686a682569066a546a626a" b"00800280058008801180148020802a8041804480508080808280a880aa800181" b"0481068110814081518159810082208280828282a082a8820184048410841284" b"158440846084898400854485a58518866a860088088825885a8880888288a888" b"0689228a808a888a968aa88a0190049010904090569084900091229164915692" b"89920094059444945094589429959095929541965198a6984999159a609a00a0" b"02a008a00aa020a02aa0a0a051a159a1a6a100a202a208a22aa280a2a0a240a4" b"95a465a698a60aa820a822a828a8a0a8a8a804a984a986a928aa2aaa91aaaaaa" )
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) qs, scales = np.hsplit(rest, [2 * QK_K // 8])
d = d.view(np.float16).astype(np.float32) qs = qs.view(np.uint16)
scales = scales.reshape((n_blocks, -1, 1)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2)) scales = (scales & 0x0F).reshape((n_blocks, -1)) db = d * (np.float32(0.5) + scales) * np.float32(0.25) db = db.reshape((n_blocks, -1, 1, 1))
# get the sign indices and unpack the bits signs = np.frombuffer(IQ2_XXS.ksigns, dtype=np.uint8).reshape(1, 1, 128) signs = np.take_along_axis(signs, (qs >> 9).reshape((n_blocks, -1, 1)), axis=-1) signs = signs.reshape((n_blocks, -1, 1)) >> np.array([i for i in range(8)], dtype=np.uint8).reshape((1, 1, 8)) signs = signs & np.uint8(0x01) signs = np.where(signs == 0, np.float32(1), np.float32(-1)) signs = signs.reshape((n_blocks, -1, 2, 8))
assert cls.grid is not None grid = np.take_along_axis(cls.grid, (qs & np.uint16(511)).reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 2, 8))
return (db * grid * signs).reshape((n_blocks, -1))
class IQ2_S(__Quant, qtype=GGMLQuantizationType.IQ2_S): # iq2s_grid, but with each byte of the original packed in 2 bits, # by mapping 0x08 to 0, 0x19 to 1, and 0x2b to 2. grid_shape = (1024, 8) grid_map = (0x08, 0x19, 0x2B) grid_hex = ( b"00000200050008000a0011001400160019002000220025002800410044004600" b"490050005200550058006100640066006900800082008500880091009400a000" b"a500aa0001010401060109011001120115011801210124014001420145014801" b"510154015601590160016501680181018401900192019501a101a40100020202" b"050208021102140220022a02410244024602490250025502800285028a029402" b"a202010404040604090410041204150418042104240426042904400442044504" b"48044a0451045404560459046004620465048104840486048904900495049804" b"a104a40400050205050508050a05110514051605190520052505280541054405" b"46054905500552055505580561056405800582058505880591059405a0050106" b"0406060609061006150640064506480651065406600681068406900600080208" b"050808081108140816081908200825082a084108440846084908500852085508" b"580861086408800885089408aa08010904091009120915091809210940094509" b"480951095409600981099009000a110a140a220a280a2a0a500a990a01100410" b"0610091010101210151018102110241026104010421045104810511054105610" b"59106010621065106810811084108610901095109810a110a410001102110511" b"08110a1111111411161119112011221125112811411144114611491150115211" b"5511581161116411801182118511881191119411011204120912101215122112" b"2412401245125112541281128412901200140214051408141114141416141914" b"2014251428144114441446144914501452145514581461146414801482148514" b"881491149414a014011504150615091510151215151518152115241540154215" b"4515481551155415601581158415901500160516081611161416201641164416" b"50168016aa160118041806180918101815181818211840184218451848185118" b"541860188118841800190219051908191119141920194119441950196919a219" b"041a101a401a561a00200220052008201120142016201920202025202a204120" b"4420502052205520642080208a209420aa200121042110211221152121214021" b"4221452151215421602181218421902100220a22222228222a22442250228822" b"8a22a82201240424062409241024152418242124242440244224452448245124" b"5424602481248424902400250525082511251425202541254425502566258025" b"0126042610264026592600280528112814284128442850288a28aa2801290429" b"102995290a2a222a642a882a8a2a014004400640094010401240154018401a40" b"21402440264040404240454048404a4051405440564059406040624065408140" b"8440904095409840a140a4400041024105410841114114411641194120412241" b"2541414144414641494150415241554158416141644180418241854188419141" b"9441a04101420442104212421542184224424042454248425142544260428142" b"844200440244054408440a441144144416441944204422442544284441444444" b"46444944504452445544584461446444804482448544884491449444a0440145" b"0445064509451045124515451845214524454045424545454845514554456045" b"6a4581458445904500460246054608461146144620464146444650468046a546" b"0148044809481048124815481848214824484048424845484848514854486048" b"84489048004902490549084911491449204941494449504980499649014a044a" b"104a404a00500250055008501150145016501950205022502550285041504450" b"4650495050505250555058506150645080508250855088509150945001510451" b"0651095110511251155118512151245140514251455148515151545160518151" b"8451905100520552085211521452205241524452505269528052015404540654" b"0954105412541554185421542454405442544554485451545454605481548454" b"9054005502550555085511551455205541554455505580550156045610562656" b"405600580258055808581158145820584158445850585a588058015904591059" b"4059005a195a855aa85a01600460066010601260156018602160246040604560" b"4860516054606060846090600061026105610861116114612061416144615061" b"806199610462106240625662a162006405640864116414642064416444645064" b"806401650465106540654a656865926500669466016804681068656898680069" b"2a69426aa16a0080028005800880118014801980208025804180448050805280" b"5580588061808080858091809480018104810981108112811581188121812481" b"408142814581488151815481818184819081a981008205820a82118214824182" b"4482508201840484068409841084128415841884218440844284458448845184" b"5484608481848484908400850285058508851185148520854185448550858085" b"8a85018604861086298640860088058811881488418844885088a28801890489" b"40896589228a588a5a8a828aa28a019004900990109012901590189024904090" b"4290459048905190549060908190849090900091059111911491419144915091" b"5a910192049210924092a6920094029405940894119414942094419444945094" b"8094969401950495109540959895a19500964696649601980498109826984098" b"a998009949995299909a00a005a00aa014a022a02aa041a044a050a0a2a0aaa0" b"40a165a102a20aa222a228a22aa282a288a28aa2a8a201a404a410a440a489a4" b"a4a400a519a551a60aa828a8a2a854a986a908aa0aaa20aa22aa28aa88aaaaaa" )
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) qs, rest = np.hsplit(rest, [QK_K // 8]) signs, rest = np.hsplit(rest, [QK_K // 8]) qh, scales = np.hsplit(rest, [QK_K // 32])
d = d.view(np.float16).astype(np.float32)
scales = scales.reshape((n_blocks, -1, 1)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2)) scales = (scales & 0x0F).reshape((n_blocks, -1)) db = d * (np.float32(0.5) + scales) * np.float32(0.25) db = db.reshape((n_blocks, -1, 1, 1))
# unpack the sign bits signs = signs.reshape((n_blocks, -1, 1)) >> np.array([i for i in range(8)], dtype=np.uint8).reshape((1, 1, 8)) signs = signs & np.uint8(0x01) signs = np.where(signs == 0, np.float32(1), np.float32(-1)) signs = signs.reshape((n_blocks, -1, 2, 8))
qh = qh.reshape((n_blocks, -1, 1)) >> np.array([0, 2, 4, 6], dtype=np.uint8).reshape((1, 1, 4)) qs = qs.astype(np.uint16) | ((qh & 0x03).astype(np.uint16) << 8).reshape((n_blocks, -1))
assert cls.grid is not None grid = np.take_along_axis(cls.grid, qs.reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 2, 8))
return (db * grid * signs).reshape((n_blocks, -1))
class IQ3_XXS(__Quant, qtype=GGMLQuantizationType.IQ3_XXS): grid_shape = (256, 4) grid_map = (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x34, 0x3E) grid_hex = ( b"0000020004001100130017002000220031004200730075000101030110011201" b"2101250130013201410154017001000202020402110220022202310233023702" b"5102570275020103070310031203250370031304370444045704730475040105" b"0705320552053506640610071407160743076107011003101010121021102310" b"3010321034104710501000110211111120112211011203121012121221123012" b"7212001302132013311346136613011405145014201524154615711505162217" b"4017002002201120132020202220262031204220012103210521102112212121" b"3021632167217021002202221122172220222222372240225522012310231423" b"7023742335245324032527254125742501270327162745270130103012302130" b"2330503065307230003102312031313144314631013203321032253252327232" b"1133333330344734723400350635223555351436363663363337603704401740" b"3540374053405740744120423742404260426642074345430444514464442545" b"4345704505471047124730471250415070500051065126515551145232527252" b"0253535310542354275472540255315550562457425724604460466064602161" b"6161176264623063366344640565526533660367216703700570077010703270" b"5270267140711272457252720073157333736073217441740075027524753076" )
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) qs, scales = np.hsplit(rest, [QK_K // 4])
d = d.view(np.float16).astype(np.float32) scales = scales.view(np.uint32)
db = d * (np.float32(0.5) + (scales >> 28).astype(np.float32)) * np.float32(0.5) db = db.reshape((n_blocks, -1, 1, 1))
# get the sign indices and unpack the bits signs = scales.reshape((n_blocks, -1, 1)) >> np.array([0, 7, 14, 21], dtype=np.uint32).reshape((1, 1, 4)) ksigns = np.frombuffer(IQ2_XXS.ksigns, dtype=np.uint8).reshape((1, 1, 1, 128)) signs = (signs & np.uint32(0x7F)).reshape((n_blocks, -1, 4, 1)) signs = np.take_along_axis(ksigns, signs, axis=-1) signs = signs.reshape((n_blocks, -1, 4, 1)) >> np.array([i for i in range(8)], dtype=np.uint8).reshape( (1, 1, 1, 8) ) signs = signs & np.uint8(0x01) signs = np.where(signs == 0, np.float32(1), np.float32(-1)) signs = signs.reshape((n_blocks, -1, 4, 8))
assert cls.grid is not None grid = np.take_along_axis(cls.grid, qs.reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 4, 8))
return (db * grid * signs).reshape((n_blocks, -1))
class IQ3_S(__Quant, qtype=GGMLQuantizationType.IQ3_S): grid_shape = (512, 4) grid_map = (0x01, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x0F) grid_hex = ( b"0000010002000500070010001100120014001600200021002500330040004200" b"4500470051005300600062007100740077000001010102010401100111011501" b"2001230127013101350144016101650172010002010205020702100213021602" b"2102250230023402420245024702510253027002730203031103150320032203" b"3103330336034403500352036703710375030004130417042104240432044004" b"4304510470040205040520052205260533054105450547056605730506061106" b"1306310652067106000702070407200722072607330750075407001001100210" b"0410101011101310151017102010221031103410361054105610611072100011" b"0111031106111011141121113011331141115011521170117611001212121512" b"1712201224123212401243125512601272120113041307131013131321132713" b"3013341341136213701303140514121414143114331442144614501454140115" b"1015131521153015321551152016241627164416461601170317101712172117" b"3517411762177017002001200320052007201020122014201620212023202720" b"3020322041204320452050205220672070207320752000210221102113211721" b"2221252131213421422151210122042207222122232230223722412253225722" b"7122742200230223052311232223242331233323422350236623012407242024" b"2324322435244124722475240425112522253725402553257025002602260726" b"2126552661260527112726273027432750270230113013301530173022303130" b"3330353042304430473051306330713001310331053114312131233140316031" b"7231763100321232203232323432503201331033143321332333273330334133" b"4333473355337333033411341634223431345234603464340135103512352535" b"3235443556357335163641360137033720372237353700400440124020402440" b"2740324041405040704002410741114113412241304135414341514155410142" b"0342104215422142334240425742624270420443114313432043224331433543" b"0044024424443744404471440545074521456245134634466046104715473047" b"4347514702501050145022504050445047505250665074500151035105511251" b"2151325172510052115223523052365253520253075310532753445351536553" b"7353015404542054325446541255265551555355425602570457225711601360" b"1560316033606060006120612761646112623462426255626262706200631463" b"2163406325644364626400650365346560650566406611671367007004700770" b"2070227036704070547062700271117124714371457101720472107216722172" b"3072517202733273357353730174057413742074507422754275027631760077" )
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) qs, rest = np.hsplit(rest, [QK_K // 4]) qh, rest = np.hsplit(rest, [QK_K // 32]) signs, scales = np.hsplit(rest, [QK_K // 8])
d = d.view(np.float16).astype(np.float32)
scales = scales.reshape((n_blocks, -1, 1)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2)) scales = (scales & 0x0F).reshape((n_blocks, -1)) db = d * (1 + 2 * scales) db = db.reshape((n_blocks, -1, 1, 1))
# unpack the sign bits signs = signs.reshape((n_blocks, -1, 1)) >> np.array([i for i in range(8)], dtype=np.uint8).reshape((1, 1, 8)) signs = signs & np.uint8(0x01) signs = np.where(signs == 0, np.float32(1), np.float32(-1)) signs = signs.reshape((n_blocks, -1, 4, 8))
qh = qh.reshape((n_blocks, -1, 1)) >> np.array([i for i in range(8)], dtype=np.uint8) qh = (qh & 0x01).astype(np.uint16).reshape((n_blocks, -1)) qs = qs.astype(np.uint16) | (qh << 8)
assert cls.grid is not None grid = np.take_along_axis(cls.grid, qs.reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 4, 8))
return (db * grid * signs).reshape((n_blocks, -1))
class IQ1_S(__Quant, qtype=GGMLQuantizationType.IQ1_S): # iq1s_grid, with each byte packed into 2 bits # -1, 0, 1 <=> 0, 1, 2 grid_shape = (2048, 8) grid_map = (-1, 0, 1) grid_hex = ( b"00000200050008000a00110015002000220028002a0045005100540056006500" b"8000820088008a009500a000a200a800aa000401050111011401160119011a01" b"2501410146014901520155015a0161016401660168018501910194019601a501" b"0002020208020a0215022002220228022a024502510259026402690280028202" b"88028a02910295029902a002a202a802aa021104140416042504410449045504" b"5a046404650491049904a5040105040505050605150518051a05290540054505" b"4a0550055105540555055605590560056205650568056a058105910595059805" b"9a05a105a405a505a605a9051406190641064406500652065506580660066106" b"6606690685069106940699060008020808080a0815082008220828082a084508" b"5108560865088008820888088a089508a008a208a808aa080509110914091909" b"2409250941095009510955096109640969099109940996099909a509000a020a" b"080a0a0a150a200a220a280a2a0a450a510a590a610a650a800a820a850a880a" b"8a0a950aa00aa20aa80aaa0a1010111014101910241025104110441050105510" b"58106110641065106910911094109610a110a510011104110611091110111211" b"1511181121112411291145114a11501151115211541155115611591160116511" b"841192119511a111a41111121412161225124012461249125212551258125a12" b"641266128512911294129612a512011406140914141415141814191421142614" b"41144514461448144a1451145414551456145914621465146814841489149014" b"94149514981499149a14a114a414a514a914021505150a151115141515151615" b"191520152215251528152a154115441545154615511552155415551556155915" b"5a1561156415651566156915801582158415851588158a159015911594159515" b"961599159a15a015a215a51501160416051606161516161618161a1621162616" b"401642164416451648164a165116551656165816591661166416651668166916" b"6a1686168a1692169516a416a916111816182518411844184618491850185518" b"58185a1860186118641866186918851891189418a5181019121915191a192119" b"25194219441945194819511954195519561959195a19601965196a1989199119" b"921995199819a119a619a919091a161a241a261a441a461a491a501a521a551a" b"581a611a661a691a851a911a961a9a1a0020022008200a201520202022202520" b"28202a20452051205920612065208020822088208a209520a020a220a520a820" b"aa2005211121142119212521422144214921552158215a216121642165216621" b"8521902196219921a521012208220a22112215222022222228222a2245225122" b"562259226522812288228a2291229522a022a222a822aa220524142416241924" b"252444244524462449245224552458245a2466248524912494249924a124a524" b"0925152521252925402545254825512554255525592562256525682589259025" b"9425952598259a25a125a425a625a92505261026122619262526412649265526" b"6026612669268426862690269a260028022808280a2815282028222828282a28" b"45285128542865288028822888288a28a028a228a828aa280929112914291929" b"2529462949295229552961296429662969298529902996299929a429a529002a" b"022a082a0a2a202a222a282a2a2a452a512a562a592a652a802a822a882a8a2a" b"952aa02aa22aa82aaa2a054011401640254049405240554058405a4061406440" b"664094409940a140a6400041014104410641094112411541164118411a412141" b"26412941454148414a41514154415541564159415a41654168416a4181418441" b"8641904192419541a041a141a241054211421442164225424142524255425a42" b"6442694289429442a5420144154419442944454448444a445144544455445644" b"61446244654468446a44814486448944904492449544a044a144a94401450245" b"05450a4511451445154516451945204525452a45414544454545464549455045" b"5145544555455645584559456145644565456645694582458445854588459145" b"94459545964599459a45a545a845aa450146054609461446154618461a462146" b"2446294640464246454648465046514652465546564659466246654668468146" b"85468a4694469546a146a446a6460548114815481a4825484248494850485548" b"5848614864486648694885489148944896489948a5480149054906490a491049" b"144915491849214924492649404945494a495149524954495549564959496049" b"6249654966496a49864989499249954996499849a149a449a649a949164a444a" b"464a494a554a584a5a4a644a694a944aa54a0150045005500650095012501550" b"1a50215024502950405045504850515054505550565059506550685086508950" b"95509850a050a150a650a9500551085109510a51115114511551165118511951" b"20512551265128512a5141514451455146514951505151515251545155515651" b"585159515a51615164516551665169518251855191519451955196519951a051" b"a551aa5101520652125215521a5221522452425245524a525152545255525652" b"595262526552855290529252955299529a52a452045405541154145415541654" b"185419542154255428542a54415444544554465449544a545054515454545554" b"5654585459545a54615462546454655466546954805488548a54915494549554" b"96549954a154a454a554aa540155025504550555065509551055115512551455" b"1555165519551a55215524552555265529554055415542554455455546554855" b"4955505551555255545555555655585559555a55605561556455655566556855" b"69556a5581558455855589558a559055915594559555965598559955a155a455" b"a555a655a9550056015602560456065608560956115614561556185619562056" b"2156225624562556265628562956415645564656485649564a56505651565256" b"545655565656585659565a566156645665566956825685568656885689568a56" b"915695569a56a256a556a656a856a95604580558065809581058155818582158" b"2a58455848584a58515854585558565858585958605862586458655882588958" b"9058925895589858a158a9580159025905590a59115914591559165919592559" b"41594459455946594959505951595259545955595659585959595a5961596459" b"655966596959815985598959915994599559965998599959a559045a085a155a" b"1a5a205a255a265a295a455a485a495a515a555a565a585a595a625a655a685a" b"6a5a815a8a5a925a955a965a985a9a5aa15a0560146016601960256044605060" b"5560566058605a60616064606660696081609660a56001610461066109611261" b"15612161226126612961456149615161556156615961656166616a6184618a61" b"92619561a161a661a96111621662196240624162466255625662586260628562" b"91629662a56211641264156416641a6421642664296440644264456448644a64" b"516454645564566459645a646064626465648464856489649064926494649564" b"966498649a64a164a464a964056508650a651165156516651965446545654665" b"496550655165546555655665596561656465656566656965866589658a659165" b"9565966599659a65a265a565a665a86502660966156620662666286629664066" b"456648664a66516654665566566658665a666066656668668066826685668a66" b"9466966698669966a066a466a666aa661668196825684168526855685a686168" b"6968856891689868a66801690469106915692169246926692969406941694569" b"4669486951695469556956695969606965696a69826984698a699569a169a469" b"a569a969116a166a186a416a446a496a506a556a586a5a6a646a656a696a866a" b"946a986a9a6aa66a0080028008800a802080228028802a804580508051805480" b"5680598065808080828088808a809580a080a280a880aa800581118114811681" b"1981258141814481498150815281558156815881598164816681698185818981" b"948196819981a5810082028208820a8215822082228228822a82518254825982" b"65828082828288828a829582a082a282a882aa82148419844184448451845584" b"5a846184648469849484998401850985128515851a8526852985408541854585" b"4885518554855585568559855a856585668568856a8581858485868589859085" b"928595859885a68511861686198625864186448649864a865086558659865a86" b"618666866a86858691869a86a4860088028808880a8815882088228828882a88" b"41884588518854885988658869888088828888888a889588a088a288a888aa88" b"05890689118914891689258941894489468949895089528955895a8961896489" b"858996899989a589008a028a088a0a8a158a208a228a288a2a8a458a518a548a" b"568a808a828a888a8a8a958aa08aa28aa88aaa8a059011901690189019902590" b"419046904990559058905a9069906a9085909190949096909990a59001910491" b"069109911091159118911a912191249126912991409145915091519154915591" b"569159916291659184918691929195919891a191a491a691a991059211921492" b"19922592449246924992509252925592589266926992859294929692a9920194" b"04940694109415941894269440944a9451945494559456945894599460946194" b"62946594849486949294949495949894a194a9940095059508950a9510951195" b"14951595169519952195259529952a9541954495459546954995509551955295" b"549555955695589559955a956195649565956695699581958595889591959295" b"94959595969599959a95a095a295a595a895aa95019604961096159619962096" b"2696299645964896499651965296559656965996659668968296849689968a96" b"929694969596a496a696a9960598169819982598419846985098529855985698" b"5a98649865988598919896989998a59804990699099910991299159918991a99" b"209921992499269940994299459948994a995199549955995699599962996599" b"66996a99819984999099929995999a99a199a699059a159a259a449a469a499a" b"509a559a589a619a859a919a949a959a969a00a002a008a00aa015a020a022a0" b"28a02aa045a051a054a056a059a080a082a088a08aa095a0a0a0a2a0a8a0aaa0" b"05a109a111a114a116a119a11aa146a149a151a155a158a15aa161a164a185a1" b"90a192a196a199a102a208a20aa210a219a222a228a22aa245a251a256a259a2" b"65a280a282a288a28aa295a2a0a2a2a2a8a2aaa219a425a441a444a450a454a4" b"55a458a45aa461a465a466a468a469a485a406a509a510a512a515a518a526a5" b"29a542a545a551a554a555a556a559a565a56aa581a584a585a586a589a592a5" b"95a598a505a611a616a61aa621a625a644a646a64aa652a655a656a658a660a6" b"62a686a690a695a696a699a6a1a6a4a6a6a600a802a808a80aa820a822a828a8" b"2aa851a854a856a859a880a882a888a88aa895a8a0a8a2a8a8a8aaa805a914a9" b"19a921a925a941a950a955a95aa961a966a969a990a996a900aa02aa08aa0aaa" b"20aa22aa28aa2aaa51aa54aa56aa80aa82aa88aa8aaa95aaa0aaa2aaa8aaaaaa" )
delta = np.float32(0.125)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) qs, qh = np.hsplit(rest, [QK_K // 8])
d = d.view(np.float16).astype(np.float32) qh = qh.view(np.uint16)
dl = d * (2 * ((qh >> 12) & 7) + 1) dl = dl.reshape((n_blocks, -1, 1, 1)) delta = np.where((qh & np.uint16(0x8000)) == 0, cls.delta, -cls.delta) delta = delta.reshape((n_blocks, -1, 1, 1))
qh = qh.reshape((n_blocks, -1, 1)) >> np.array([0, 3, 6, 9], dtype=np.uint16).reshape((1, 1, 4)) qs = qs.astype(np.uint16) | ((qh & 7) << 8).reshape((n_blocks, -1))
assert cls.grid is not None grid = np.take_along_axis(cls.grid, qs.reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 4, 8))
return (dl * (grid + delta)).reshape((n_blocks, -1))
class IQ1_M(__Quant, qtype=GGMLQuantizationType.IQ1_M): grid_shape = IQ1_S.grid_shape grid_map = IQ1_S.grid_map grid_hex = IQ1_S.grid_hex
delta = IQ1_S.delta
# Okay *this* type is weird. It's the only one which stores the f16 scales in multiple parts. @classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
qs, rest = np.hsplit(blocks, [QK_K // 8]) qh, scales = np.hsplit(rest, [QK_K // 16])
# The f16 scale is packed across multiple bytes scales = scales.view(np.uint16) d = (scales.reshape((n_blocks, 4)) & np.uint16(0xF000)) >> np.array([12, 8, 4, 0], dtype=np.uint16).reshape( (1, 4) ) d = d[..., 0] | d[..., 1] | d[..., 2] | d[..., 3] d = d.view(np.float16).astype(np.float32).reshape((n_blocks, 1))
scales = scales.reshape(n_blocks, -1, 1) >> np.array([0, 3, 6, 9], dtype=np.uint16).reshape((1, 1, 4)) scales = (scales & 0x07).reshape((n_blocks, -1)) dl = d * (2 * scales + 1) dl = dl.reshape((n_blocks, -1, 2, 1, 1))
qh = qh.reshape((n_blocks, -1, 1)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2)) qs = qs.astype(np.uint16) | ((qh & 0x07).astype(np.uint16) << 8).reshape((n_blocks, -1))
delta = np.where(qh & 0x08 == 0, cls.delta, -cls.delta) delta = delta.reshape((n_blocks, -1, 2, 2, 1))
assert cls.grid is not None grid = np.take_along_axis(cls.grid, qs.reshape((n_blocks, -1, 1, 1)), axis=-2) grid = grid.reshape((n_blocks, -1, 2, 2, 8))
return (dl * (grid + delta)).reshape((n_blocks, -1))
class IQ4_NL(__Quant, qtype=GGMLQuantizationType.IQ4_NL): kvalues = (-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113)
@classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, qs = np.hsplit(blocks, [2])
d = d.view(np.float16).astype(np.float32)
qs = qs.reshape((n_blocks, -1, 1, cls.block_size // 2)) >> np.array([0, 4], dtype=np.uint8).reshape( (1, 1, 2, 1) )
qs = (qs & np.uint8(0x0F)).reshape((n_blocks, -1, 1))
kvalues = np.array(cls.kvalues, dtype=np.int8).reshape(1, 1, 16) qs = np.take_along_axis(kvalues, qs, axis=-1).astype(np.float32).reshape((n_blocks, -1))
return d * qs
class IQ4_XS(__Quant, qtype=GGMLQuantizationType.IQ4_XS): @classmethod def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray: n_blocks = blocks.shape[0]
d, rest = np.hsplit(blocks, [2]) scales_h, rest = np.hsplit(rest, [2]) scales_l, qs = np.hsplit(rest, [QK_K // 64])
d = d.view(np.float16).astype(np.float32) scales_h = scales_h.view(np.uint16)
scales_l = scales_l.reshape((n_blocks, -1, 1)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2)) scales_h = scales_h.reshape((n_blocks, 1, -1)) >> np.array( [2 * i for i in range(QK_K // 32)], dtype=np.uint16 ).reshape((1, -1, 1)) scales_l = scales_l.reshape((n_blocks, -1)) & np.uint8(0x0F) scales_h = scales_h.reshape((n_blocks, -1)).astype(np.uint8) & np.uint8(0x03)
scales = (scales_l | (scales_h << np.uint8(4))).astype(np.int8) - np.int8(32) dl = (d * scales.astype(np.float32)).reshape((n_blocks, -1, 1))
qs = qs.reshape((n_blocks, -1, 1, 16)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 2, 1)) qs = qs.reshape((n_blocks, -1, 32, 1)) & np.uint8(0x0F)
kvalues = np.array(IQ4_NL.kvalues, dtype=np.int8).reshape((1, 1, 1, -1)) qs = np.take_along_axis(kvalues, qs, axis=-1).astype(np.float32).reshape((n_blocks, -1, 32))
return (dl * qs).reshape((n_blocks, -1))12345torchnumpysafetensorshuggingface-hubggufREADME.md+1-1
__pycache__/convert_diffusion_to_gguf.cpython-310.pycAdded
app.pyAdded+101
convert_diffusion_to_gguf.pyAdded+362
custom_quants.pyAdded+1802
requirements.txtAdded+5