2023-11-11 06:04:50 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-03 21:36:41 +02:00
|
|
|
import logging
|
2023-11-11 06:04:50 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import struct
|
|
|
|
import tempfile
|
2024-06-09 04:34:29 +02:00
|
|
|
from dataclasses import dataclass
|
2023-11-11 06:04:50 +01:00
|
|
|
from enum import Enum, auto
|
2024-07-18 12:40:15 +02:00
|
|
|
from math import prod
|
2024-06-24 11:42:03 +02:00
|
|
|
from pathlib import Path
|
2023-11-11 06:04:50 +01:00
|
|
|
from io import BufferedWriter
|
2024-05-11 17:06:26 +02:00
|
|
|
from typing import IO, Any, Sequence, Mapping
|
2024-04-18 13:49:01 +02:00
|
|
|
from string import ascii_letters, digits
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from .constants import (
|
|
|
|
GGUF_DEFAULT_ALIGNMENT,
|
|
|
|
GGUF_MAGIC,
|
|
|
|
GGUF_VERSION,
|
|
|
|
GGMLQuantizationType,
|
|
|
|
GGUFEndian,
|
|
|
|
GGUFValueType,
|
|
|
|
Keys,
|
|
|
|
RopeScalingType,
|
2024-02-15 18:21:49 +01:00
|
|
|
PoolingType,
|
2023-11-11 06:04:50 +01:00
|
|
|
TokenType,
|
|
|
|
)
|
|
|
|
|
2024-05-25 03:11:48 +02:00
|
|
|
from .quants import quant_shape_from_byte_shape
|
|
|
|
|
2024-05-03 21:36:41 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
SHARD_NAME_FORMAT = "{:s}-{:05d}-of-{:05d}.gguf"
|
|
|
|
|
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
@dataclass
|
|
|
|
class TensorInfo:
|
|
|
|
shape: Sequence[int]
|
|
|
|
dtype: GGMLQuantizationType
|
|
|
|
nbytes: int
|
|
|
|
tensor: np.ndarray[Any, Any] | None = None
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class GGUFValue:
|
|
|
|
value: Any
|
|
|
|
type: GGUFValueType
|
|
|
|
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
class WriterState(Enum):
|
2024-06-09 04:34:29 +02:00
|
|
|
NO_FILE = auto()
|
2023-11-11 06:04:50 +01:00
|
|
|
EMPTY = auto()
|
|
|
|
HEADER = auto()
|
|
|
|
KV_DATA = auto()
|
|
|
|
TI_DATA = auto()
|
2024-06-09 04:34:29 +02:00
|
|
|
WEIGHTS = auto()
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GGUFWriter:
|
2024-06-24 11:42:03 +02:00
|
|
|
fout: list[BufferedWriter] | None
|
|
|
|
path: Path | None
|
2023-11-11 06:04:50 +01:00
|
|
|
temp_file: tempfile.SpooledTemporaryFile[bytes] | None
|
2024-06-24 11:42:03 +02:00
|
|
|
tensors: list[dict[str, TensorInfo]]
|
|
|
|
kv_data: list[dict[str, GGUFValue]]
|
2024-06-09 04:34:29 +02:00
|
|
|
state: WriterState
|
2023-11-11 06:04:50 +01:00
|
|
|
_simple_value_packing = {
|
|
|
|
GGUFValueType.UINT8: "B",
|
|
|
|
GGUFValueType.INT8: "b",
|
|
|
|
GGUFValueType.UINT16: "H",
|
|
|
|
GGUFValueType.INT16: "h",
|
|
|
|
GGUFValueType.UINT32: "I",
|
|
|
|
GGUFValueType.INT32: "i",
|
|
|
|
GGUFValueType.FLOAT32: "f",
|
|
|
|
GGUFValueType.UINT64: "Q",
|
|
|
|
GGUFValueType.INT64: "q",
|
|
|
|
GGUFValueType.FLOAT64: "d",
|
|
|
|
GGUFValueType.BOOL: "?",
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(
|
2024-06-24 11:42:03 +02:00
|
|
|
self, path: os.PathLike[str] | str | None, arch: str, use_temp_file: bool = False, endianess: GGUFEndian = GGUFEndian.LITTLE,
|
|
|
|
split_max_tensors: int = 0, split_max_size: int = 0, dry_run: bool = False, small_first_shard: bool = False
|
2023-11-11 06:04:50 +01:00
|
|
|
):
|
2024-06-09 04:34:29 +02:00
|
|
|
self.fout = None
|
2024-06-24 11:42:03 +02:00
|
|
|
self.path = Path(path) if path else None
|
2023-11-11 06:04:50 +01:00
|
|
|
self.arch = arch
|
|
|
|
self.endianess = endianess
|
|
|
|
self.data_alignment = GGUF_DEFAULT_ALIGNMENT
|
|
|
|
self.use_temp_file = use_temp_file
|
|
|
|
self.temp_file = None
|
2024-06-24 11:42:03 +02:00
|
|
|
self.tensors = [{}]
|
|
|
|
self.kv_data = [{}]
|
|
|
|
self.split_max_tensors = split_max_tensors
|
|
|
|
self.split_max_size = split_max_size
|
|
|
|
self.dry_run = dry_run
|
|
|
|
self.small_first_shard = small_first_shard
|
2024-05-03 21:36:41 +02:00
|
|
|
logger.info("gguf: This GGUF file is for {0} Endian only".format(
|
2023-11-11 06:04:50 +01:00
|
|
|
"Big" if self.endianess == GGUFEndian.BIG else "Little",
|
|
|
|
))
|
2024-06-09 04:34:29 +02:00
|
|
|
self.state = WriterState.NO_FILE
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
if self.small_first_shard:
|
|
|
|
self.tensors.append({})
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
self.add_architecture()
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def get_total_parameter_count(self) -> tuple[int, int, int, int]:
|
|
|
|
total_params = 0
|
|
|
|
shared_params = 0
|
|
|
|
expert_params = 0
|
|
|
|
|
|
|
|
expert_sum = 0
|
|
|
|
n_expert_tensors = 0
|
|
|
|
|
|
|
|
last_lora_a: tuple[str, TensorInfo] | None = None
|
|
|
|
|
|
|
|
for tensors in self.tensors:
|
|
|
|
for name, info in tensors.items():
|
|
|
|
|
|
|
|
shape = info.shape
|
|
|
|
|
|
|
|
if name.endswith(".lora_a"):
|
|
|
|
last_lora_a = (name, info)
|
|
|
|
continue
|
|
|
|
elif name.endswith(".lora_b"):
|
|
|
|
if last_lora_a is None or last_lora_a[0] != name[:-1] + "a":
|
|
|
|
# Bail when the LoRA pair can't be found trivially
|
|
|
|
logger.warning("can't measure LoRA size correctly, tensor order is unusual")
|
|
|
|
return 0, 0, 0, 0
|
|
|
|
else:
|
|
|
|
shape = (*shape[:-1], last_lora_a[1].shape[-1])
|
|
|
|
|
|
|
|
size = prod(shape)
|
|
|
|
|
|
|
|
if "_exps." in name:
|
|
|
|
expert_params += (size // shape[-3])
|
|
|
|
expert_sum += shape[-3]
|
|
|
|
n_expert_tensors += 1
|
|
|
|
else:
|
|
|
|
shared_params += size
|
|
|
|
|
|
|
|
total_params += size
|
|
|
|
|
|
|
|
# Hopefully this should work even for variable-expert-count models
|
|
|
|
expert_count = (expert_sum // n_expert_tensors) if n_expert_tensors > 0 else 0
|
|
|
|
|
|
|
|
# Negate the total to signal it's likely not exact
|
|
|
|
if last_lora_a is not None:
|
|
|
|
total_params = -total_params
|
|
|
|
|
|
|
|
# NOTE: keep the output in the same order as accepted by 'size_label' in gguf-py/gguf/utility.py
|
|
|
|
return total_params, shared_params, expert_params, expert_count
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
def format_shard_names(self, path: Path) -> list[Path]:
|
|
|
|
if len(self.tensors) == 1:
|
|
|
|
return [path]
|
|
|
|
return [path.with_name(SHARD_NAME_FORMAT.format(path.stem, i + 1, len(self.tensors))) for i in range(len(self.tensors))]
|
|
|
|
|
|
|
|
def open_output_file(self, path: Path | None = None) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
if self.state is WriterState.EMPTY and self.fout is not None and (path is None or path == self.path):
|
|
|
|
# allow calling this multiple times as long as the path is the same
|
|
|
|
return
|
2024-07-18 12:40:15 +02:00
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
if self.state is not WriterState.NO_FILE:
|
|
|
|
raise ValueError(f'Expected output file to be not yet opened, got {self.state}')
|
|
|
|
|
|
|
|
if path is not None:
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
if self.path is not None:
|
2024-06-24 11:42:03 +02:00
|
|
|
filenames = self.print_plan()
|
|
|
|
self.fout = [open(filename, "wb") for filename in filenames]
|
2024-06-09 04:34:29 +02:00
|
|
|
self.state = WriterState.EMPTY
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
def print_plan(self) -> list[Path]:
|
|
|
|
logger.info("Writing the following files:")
|
|
|
|
assert self.path is not None
|
|
|
|
filenames = self.format_shard_names(self.path)
|
|
|
|
assert len(filenames) == len(self.tensors)
|
|
|
|
for name, tensors in zip(filenames, self.tensors):
|
|
|
|
logger.info(f"{name}: n_tensors = {len(tensors)}, total_size = {GGUFWriter.format_n_bytes_to_str(sum(ti.nbytes for ti in tensors.values()))}")
|
|
|
|
|
|
|
|
if self.dry_run:
|
|
|
|
logger.info("Dry run, not writing files")
|
2024-07-18 12:40:15 +02:00
|
|
|
for name in filenames:
|
|
|
|
print(name) # noqa: NP100
|
2024-06-24 11:42:03 +02:00
|
|
|
exit()
|
|
|
|
|
|
|
|
return filenames
|
|
|
|
|
|
|
|
def add_shard_kv_data(self) -> None:
|
|
|
|
if len(self.tensors) == 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
total_tensors = sum(len(t) for t in self.tensors)
|
|
|
|
assert self.fout is not None
|
|
|
|
total_splits = len(self.fout)
|
|
|
|
self.kv_data.extend({} for _ in range(len(self.kv_data), total_splits))
|
|
|
|
for i, kv_data in enumerate(self.kv_data):
|
|
|
|
kv_data[Keys.Split.LLM_KV_SPLIT_NO] = GGUFValue(i, GGUFValueType.UINT16)
|
|
|
|
kv_data[Keys.Split.LLM_KV_SPLIT_COUNT] = GGUFValue(total_splits, GGUFValueType.UINT16)
|
|
|
|
kv_data[Keys.Split.LLM_KV_SPLIT_TENSORS_COUNT] = GGUFValue(total_tensors, GGUFValueType.INT32)
|
|
|
|
|
|
|
|
def write_header_to_file(self, path: Path | None = None) -> None:
|
|
|
|
if len(self.tensors) == 1 and (self.split_max_tensors != 0 or self.split_max_size != 0):
|
|
|
|
logger.warning("Model fails split requirements, not splitting")
|
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
self.open_output_file(path)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
if self.state is not WriterState.EMPTY:
|
|
|
|
raise ValueError(f'Expected output file to be empty, got {self.state}')
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
assert self.fout is not None
|
|
|
|
assert len(self.fout) == len(self.tensors)
|
|
|
|
assert len(self.kv_data) == 1
|
|
|
|
|
|
|
|
self.add_shard_kv_data()
|
|
|
|
|
|
|
|
for fout, tensors, kv_data in zip(self.fout, self.tensors, self.kv_data):
|
|
|
|
fout.write(self._pack("<I", GGUF_MAGIC, skip_pack_prefix = True))
|
|
|
|
fout.write(self._pack("I", GGUF_VERSION))
|
|
|
|
fout.write(self._pack("Q", len(tensors)))
|
|
|
|
fout.write(self._pack("Q", len(kv_data)))
|
|
|
|
fout.flush()
|
2023-11-11 06:04:50 +01:00
|
|
|
self.state = WriterState.HEADER
|
|
|
|
|
|
|
|
def write_kv_data_to_file(self) -> None:
|
|
|
|
if self.state is not WriterState.HEADER:
|
|
|
|
raise ValueError(f'Expected output file to contain the header, got {self.state}')
|
2024-06-09 04:34:29 +02:00
|
|
|
assert self.fout is not None
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
for fout, kv_data in zip(self.fout, self.kv_data):
|
|
|
|
kv_bytes = bytearray()
|
2024-06-09 04:34:29 +02:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
for key, val in kv_data.items():
|
|
|
|
kv_bytes += self._pack_val(key, GGUFValueType.STRING, add_vtype=False)
|
|
|
|
kv_bytes += self._pack_val(val.value, val.type, add_vtype=True)
|
|
|
|
|
|
|
|
fout.write(kv_bytes)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
self.flush()
|
|
|
|
self.state = WriterState.KV_DATA
|
|
|
|
|
|
|
|
def write_ti_data_to_file(self) -> None:
|
|
|
|
if self.state is not WriterState.KV_DATA:
|
|
|
|
raise ValueError(f'Expected output file to contain KV data, got {self.state}')
|
2024-06-09 04:34:29 +02:00
|
|
|
assert self.fout is not None
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
for fout, tensors in zip(self.fout, self.tensors):
|
|
|
|
ti_data = bytearray()
|
|
|
|
offset_tensor = 0
|
|
|
|
|
|
|
|
for name, ti in tensors.items():
|
|
|
|
ti_data += self._pack_val(name, GGUFValueType.STRING, add_vtype=False)
|
|
|
|
n_dims = len(ti.shape)
|
|
|
|
ti_data += self._pack("I", n_dims)
|
|
|
|
for j in range(n_dims):
|
|
|
|
ti_data += self._pack("Q", ti.shape[n_dims - 1 - j])
|
|
|
|
ti_data += self._pack("I", ti.dtype)
|
|
|
|
ti_data += self._pack("Q", offset_tensor)
|
|
|
|
offset_tensor += GGUFWriter.ggml_pad(ti.nbytes, self.data_alignment)
|
|
|
|
|
|
|
|
fout.write(ti_data)
|
|
|
|
fout.flush()
|
2023-11-11 06:04:50 +01:00
|
|
|
self.state = WriterState.TI_DATA
|
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
def add_key_value(self, key: str, val: Any, vtype: GGUFValueType) -> None:
|
2024-06-24 11:42:03 +02:00
|
|
|
if any(key in kv_data for kv_data in self.kv_data):
|
2024-06-09 04:34:29 +02:00
|
|
|
raise ValueError(f'Duplicated key name {key!r}')
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
self.kv_data[0][key] = GGUFValue(value=val, type=vtype)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_uint8(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key,val, GGUFValueType.UINT8)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_int8(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.INT8)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_uint16(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.UINT16)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_int16(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.INT16)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_uint32(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.UINT32)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_int32(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.INT32)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_float32(self, key: str, val: float) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.FLOAT32)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_uint64(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.UINT64)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_int64(self, key: str, val: int) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.INT64)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_float64(self, key: str, val: float) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.FLOAT64)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_bool(self, key: str, val: bool) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.BOOL)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_string(self, key: str, val: str) -> None:
|
|
|
|
if not val:
|
|
|
|
return
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.STRING)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_array(self, key: str, val: Sequence[Any]) -> None:
|
2024-07-30 16:57:03 +02:00
|
|
|
if len(val) == 0:
|
|
|
|
return
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_key_value(key, val, GGUFValueType.ARRAY)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def ggml_pad(x: int, n: int) -> int:
|
|
|
|
return ((x + n - 1) // n) * n
|
|
|
|
|
|
|
|
def add_tensor_info(
|
2024-05-13 20:10:51 +02:00
|
|
|
self, name: str, tensor_shape: Sequence[int], tensor_dtype: np.dtype,
|
2023-11-11 06:04:50 +01:00
|
|
|
tensor_nbytes: int, raw_dtype: GGMLQuantizationType | None = None,
|
|
|
|
) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
if self.state is not WriterState.NO_FILE:
|
|
|
|
raise ValueError(f'Expected output file to be not yet opened, got {self.state}')
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
if any(name in tensors for tensors in self.tensors):
|
2024-06-09 04:34:29 +02:00
|
|
|
raise ValueError(f'Duplicated tensor name {name!r}')
|
2024-04-28 17:36:18 +02:00
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
if raw_dtype is None:
|
gguf : add support for I64 and F64 arrays (#6062)
* gguf : add support for I64 and F64 arrays
GGML currently does not support I64 or F64 arrays and they are not often
used in machine learning, however if in the future the need arises, it
would be nice to add them now, so that the types are next to the other
types I8, I16, I32 in the enums, and it also reserves their type number.
Furthermore, with this addition the GGUF format becomes very usable for
most computational applications of NumPy (being compatible with the most
common NumPy dtypes: i8, i16, i32, i64, f32, f64), providing a faster,
and more versatile alternative to the `npz` format, and a simpler
alternative to the `hdf5` format.
The change in this PR seems small, not significantly increasing the
maintenance burden. I tested this from Python using GGUFWriter/Reader
and `gguf-dump`, as well as from C, everything seems to work.
* Fix compiler warnings
2024-03-15 09:46:51 +01:00
|
|
|
if tensor_dtype == np.float16:
|
2024-03-14 11:40:14 +01:00
|
|
|
dtype = GGMLQuantizationType.F16
|
gguf : add support for I64 and F64 arrays (#6062)
* gguf : add support for I64 and F64 arrays
GGML currently does not support I64 or F64 arrays and they are not often
used in machine learning, however if in the future the need arises, it
would be nice to add them now, so that the types are next to the other
types I8, I16, I32 in the enums, and it also reserves their type number.
Furthermore, with this addition the GGUF format becomes very usable for
most computational applications of NumPy (being compatible with the most
common NumPy dtypes: i8, i16, i32, i64, f32, f64), providing a faster,
and more versatile alternative to the `npz` format, and a simpler
alternative to the `hdf5` format.
The change in this PR seems small, not significantly increasing the
maintenance burden. I tested this from Python using GGUFWriter/Reader
and `gguf-dump`, as well as from C, everything seems to work.
* Fix compiler warnings
2024-03-15 09:46:51 +01:00
|
|
|
elif tensor_dtype == np.float32:
|
|
|
|
dtype = GGMLQuantizationType.F32
|
|
|
|
elif tensor_dtype == np.float64:
|
|
|
|
dtype = GGMLQuantizationType.F64
|
2024-03-14 11:40:14 +01:00
|
|
|
elif tensor_dtype == np.int8:
|
|
|
|
dtype = GGMLQuantizationType.I8
|
|
|
|
elif tensor_dtype == np.int16:
|
|
|
|
dtype = GGMLQuantizationType.I16
|
|
|
|
elif tensor_dtype == np.int32:
|
|
|
|
dtype = GGMLQuantizationType.I32
|
gguf : add support for I64 and F64 arrays (#6062)
* gguf : add support for I64 and F64 arrays
GGML currently does not support I64 or F64 arrays and they are not often
used in machine learning, however if in the future the need arises, it
would be nice to add them now, so that the types are next to the other
types I8, I16, I32 in the enums, and it also reserves their type number.
Furthermore, with this addition the GGUF format becomes very usable for
most computational applications of NumPy (being compatible with the most
common NumPy dtypes: i8, i16, i32, i64, f32, f64), providing a faster,
and more versatile alternative to the `npz` format, and a simpler
alternative to the `hdf5` format.
The change in this PR seems small, not significantly increasing the
maintenance burden. I tested this from Python using GGUFWriter/Reader
and `gguf-dump`, as well as from C, everything seems to work.
* Fix compiler warnings
2024-03-15 09:46:51 +01:00
|
|
|
elif tensor_dtype == np.int64:
|
|
|
|
dtype = GGMLQuantizationType.I64
|
2024-03-14 11:40:14 +01:00
|
|
|
else:
|
gguf : add support for I64 and F64 arrays (#6062)
* gguf : add support for I64 and F64 arrays
GGML currently does not support I64 or F64 arrays and they are not often
used in machine learning, however if in the future the need arises, it
would be nice to add them now, so that the types are next to the other
types I8, I16, I32 in the enums, and it also reserves their type number.
Furthermore, with this addition the GGUF format becomes very usable for
most computational applications of NumPy (being compatible with the most
common NumPy dtypes: i8, i16, i32, i64, f32, f64), providing a faster,
and more versatile alternative to the `npz` format, and a simpler
alternative to the `hdf5` format.
The change in this PR seems small, not significantly increasing the
maintenance burden. I tested this from Python using GGUFWriter/Reader
and `gguf-dump`, as well as from C, everything seems to work.
* Fix compiler warnings
2024-03-15 09:46:51 +01:00
|
|
|
raise ValueError("Only F16, F32, F64, I8, I16, I32, I64 tensors are supported for now")
|
2023-11-11 06:04:50 +01:00
|
|
|
else:
|
|
|
|
dtype = raw_dtype
|
2024-05-13 20:10:51 +02:00
|
|
|
if tensor_dtype == np.uint8:
|
2024-05-25 03:11:48 +02:00
|
|
|
tensor_shape = quant_shape_from_byte_shape(tensor_shape, raw_dtype)
|
2024-06-09 04:34:29 +02:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
# make sure there is at least one tensor before splitting
|
|
|
|
if len(self.tensors[-1]) > 0:
|
|
|
|
if ( # split when over tensor limit
|
|
|
|
self.split_max_tensors != 0
|
|
|
|
and len(self.tensors[-1]) >= self.split_max_tensors
|
|
|
|
) or ( # split when over size limit
|
|
|
|
self.split_max_size != 0
|
|
|
|
and sum(ti.nbytes for ti in self.tensors[-1].values()) + tensor_nbytes > self.split_max_size
|
|
|
|
):
|
|
|
|
self.tensors.append({})
|
|
|
|
|
|
|
|
self.tensors[-1][name] = TensorInfo(shape=tensor_shape, dtype=dtype, nbytes=tensor_nbytes)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_tensor(
|
2024-05-11 17:06:26 +02:00
|
|
|
self, name: str, tensor: np.ndarray[Any, Any], raw_shape: Sequence[int] | None = None,
|
2023-11-11 06:04:50 +01:00
|
|
|
raw_dtype: GGMLQuantizationType | None = None,
|
|
|
|
) -> None:
|
|
|
|
if self.endianess == GGUFEndian.BIG:
|
|
|
|
tensor.byteswap(inplace=True)
|
|
|
|
if self.use_temp_file and self.temp_file is None:
|
2023-11-20 11:35:47 +01:00
|
|
|
fp = tempfile.SpooledTemporaryFile(mode="w+b", max_size=256 * 1024 * 1024)
|
2023-11-11 06:04:50 +01:00
|
|
|
fp.seek(0)
|
|
|
|
self.temp_file = fp
|
|
|
|
|
|
|
|
shape: Sequence[int] = raw_shape if raw_shape is not None else tensor.shape
|
2024-06-09 04:34:29 +02:00
|
|
|
self.add_tensor_info(name, shape, tensor.dtype, tensor.nbytes, raw_dtype=raw_dtype)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
if self.temp_file is None:
|
2024-06-24 11:42:03 +02:00
|
|
|
self.tensors[-1][name].tensor = tensor
|
2023-11-11 06:04:50 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
tensor.tofile(self.temp_file)
|
|
|
|
self.write_padding(self.temp_file, tensor.nbytes)
|
|
|
|
|
|
|
|
def write_padding(self, fp: IO[bytes], n: int, align: int | None = None) -> None:
|
|
|
|
pad = GGUFWriter.ggml_pad(n, align if align is not None else self.data_alignment) - n
|
|
|
|
if pad != 0:
|
|
|
|
fp.write(bytes([0] * pad))
|
|
|
|
|
2024-05-11 17:06:26 +02:00
|
|
|
def write_tensor_data(self, tensor: np.ndarray[Any, Any]) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
if self.state is not WriterState.TI_DATA and self.state is not WriterState.WEIGHTS:
|
|
|
|
raise ValueError(f'Expected output file to contain tensor info or weights, got {self.state}')
|
|
|
|
assert self.fout is not None
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
if self.endianess == GGUFEndian.BIG:
|
|
|
|
tensor.byteswap(inplace=True)
|
2024-06-24 11:42:03 +02:00
|
|
|
|
|
|
|
file_id = -1
|
|
|
|
for i, tensors in enumerate(self.tensors):
|
|
|
|
if len(tensors) > 0:
|
|
|
|
file_id = i
|
|
|
|
break
|
|
|
|
|
|
|
|
fout = self.fout[file_id]
|
|
|
|
|
|
|
|
# pop the first tensor info
|
|
|
|
# TODO: cleaner way to get the first key
|
|
|
|
first_tensor_name = [name for name, _ in zip(self.tensors[file_id].keys(), range(1))][0]
|
|
|
|
ti = self.tensors[file_id].pop(first_tensor_name)
|
|
|
|
assert ti.nbytes == tensor.nbytes
|
|
|
|
|
|
|
|
self.write_padding(fout, fout.tell())
|
|
|
|
tensor.tofile(fout)
|
|
|
|
self.write_padding(fout, tensor.nbytes)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
self.state = WriterState.WEIGHTS
|
|
|
|
|
2024-05-09 00:16:38 +02:00
|
|
|
def write_tensors_to_file(self, *, progress: bool = False) -> None:
|
2023-11-11 06:04:50 +01:00
|
|
|
self.write_ti_data_to_file()
|
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
assert self.fout is not None
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
for fout in self.fout:
|
|
|
|
self.write_padding(fout, fout.tell())
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
if self.temp_file is None:
|
2024-06-24 11:42:03 +02:00
|
|
|
shard_bar = None
|
2024-06-09 04:34:29 +02:00
|
|
|
bar = None
|
2024-05-09 00:16:38 +02:00
|
|
|
|
|
|
|
if progress:
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
total_bytes = sum(ti.nbytes for t in self.tensors for ti in t.values())
|
2024-05-09 00:16:38 +02:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
if len(self.fout) > 1:
|
|
|
|
shard_bar = tqdm(desc=f"Shard (0/{len(self.fout)})", total=None, unit="byte", unit_scale=True)
|
2024-05-09 00:16:38 +02:00
|
|
|
bar = tqdm(desc="Writing", total=total_bytes, unit="byte", unit_scale=True)
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
for i, (fout, tensors) in enumerate(zip(self.fout, self.tensors)):
|
|
|
|
if shard_bar is not None:
|
|
|
|
shard_bar.set_description(f"Shard ({i + 1}/{len(self.fout)})")
|
|
|
|
total = sum(ti.nbytes for ti in tensors.values())
|
|
|
|
shard_bar.reset(total=(total if total > 0 else None))
|
|
|
|
|
|
|
|
# relying on the fact that Python dicts preserve insertion order (since 3.7)
|
|
|
|
for ti in tensors.values():
|
|
|
|
assert ti.tensor is not None # can only iterate once over the tensors
|
|
|
|
assert ti.tensor.nbytes == ti.nbytes
|
|
|
|
ti.tensor.tofile(fout)
|
|
|
|
if shard_bar is not None:
|
|
|
|
shard_bar.update(ti.nbytes)
|
|
|
|
if bar is not None:
|
|
|
|
bar.update(ti.nbytes)
|
|
|
|
self.write_padding(fout, ti.nbytes)
|
|
|
|
ti.tensor = None
|
2024-06-09 04:34:29 +02:00
|
|
|
else:
|
|
|
|
self.temp_file.seek(0)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
shutil.copyfileobj(self.temp_file, self.fout[0 if not self.small_first_shard else 1])
|
2024-06-09 04:34:29 +02:00
|
|
|
self.flush()
|
|
|
|
self.temp_file.close()
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
self.state = WriterState.WEIGHTS
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def flush(self) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
assert self.fout is not None
|
2024-06-24 11:42:03 +02:00
|
|
|
for fout in self.fout:
|
|
|
|
fout.flush()
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def close(self) -> None:
|
2024-06-09 04:34:29 +02:00
|
|
|
if self.fout is not None:
|
2024-06-24 11:42:03 +02:00
|
|
|
for fout in self.fout:
|
|
|
|
fout.close()
|
2024-06-09 04:34:29 +02:00
|
|
|
self.fout = None
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-15 20:50:47 +02:00
|
|
|
def add_type(self, type_name: str) -> None:
|
|
|
|
self.add_string(Keys.General.TYPE, type_name)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_architecture(self) -> None:
|
|
|
|
self.add_string(Keys.General.ARCHITECTURE, self.arch)
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_quantization_version(self, quantization_version: int) -> None:
|
|
|
|
self.add_uint32(Keys.General.QUANTIZATION_VERSION, quantization_version)
|
|
|
|
|
|
|
|
def add_custom_alignment(self, alignment: int) -> None:
|
|
|
|
self.data_alignment = alignment
|
|
|
|
self.add_uint32(Keys.General.ALIGNMENT, alignment)
|
|
|
|
|
|
|
|
def add_file_type(self, ftype: int) -> None:
|
|
|
|
self.add_uint32(Keys.General.FILE_TYPE, ftype)
|
|
|
|
|
|
|
|
def add_name(self, name: str) -> None:
|
|
|
|
self.add_string(Keys.General.NAME, name)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_author(self, author: str) -> None:
|
|
|
|
self.add_string(Keys.General.AUTHOR, author)
|
|
|
|
|
2024-04-05 20:41:38 +02:00
|
|
|
def add_version(self, version: str) -> None:
|
|
|
|
self.add_string(Keys.General.VERSION, version)
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_organization(self, organization: str) -> None:
|
|
|
|
self.add_string(Keys.General.ORGANIZATION, organization)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_finetune(self, finetune: str) -> None:
|
|
|
|
self.add_string(Keys.General.FINETUNE, finetune)
|
|
|
|
|
|
|
|
def add_basename(self, basename: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASENAME, basename)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
|
|
|
def add_description(self, description: str) -> None:
|
|
|
|
self.add_string(Keys.General.DESCRIPTION, description)
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_quantized_by(self, quantized: str) -> None:
|
|
|
|
self.add_string(Keys.General.QUANTIZED_BY, quantized)
|
|
|
|
|
|
|
|
def add_size_label(self, size_label: str) -> None:
|
|
|
|
self.add_string(Keys.General.SIZE_LABEL, size_label)
|
|
|
|
|
|
|
|
def add_license(self, license: str) -> None:
|
|
|
|
self.add_string(Keys.General.LICENSE, license)
|
|
|
|
|
|
|
|
def add_license_name(self, license: str) -> None:
|
|
|
|
self.add_string(Keys.General.LICENSE_NAME, license)
|
|
|
|
|
|
|
|
def add_license_link(self, license: str) -> None:
|
|
|
|
self.add_string(Keys.General.LICENSE_LINK, license)
|
|
|
|
|
|
|
|
def add_url(self, url: str) -> None:
|
|
|
|
self.add_string(Keys.General.URL, url)
|
|
|
|
|
|
|
|
def add_doi(self, doi: str) -> None:
|
|
|
|
self.add_string(Keys.General.DOI, doi)
|
|
|
|
|
|
|
|
def add_uuid(self, uuid: str) -> None:
|
|
|
|
self.add_string(Keys.General.UUID, uuid)
|
|
|
|
|
|
|
|
def add_repo_url(self, repo_url: str) -> None:
|
|
|
|
self.add_string(Keys.General.REPO_URL, repo_url)
|
2024-04-05 20:41:38 +02:00
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_source_url(self, url: str) -> None:
|
|
|
|
self.add_string(Keys.General.SOURCE_URL, url)
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_source_doi(self, doi: str) -> None:
|
|
|
|
self.add_string(Keys.General.SOURCE_DOI, doi)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_source_uuid(self, uuid: str) -> None:
|
|
|
|
self.add_string(Keys.General.SOURCE_UUID, uuid)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_source_repo_url(self, repo_url: str) -> None:
|
|
|
|
self.add_string(Keys.General.SOURCE_REPO_URL, repo_url)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_base_model_count(self, source_count: int) -> None:
|
|
|
|
self.add_uint32(Keys.General.BASE_MODEL_COUNT, source_count)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_base_model_name(self, source_id: int, name: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_NAME.format(id=source_id), name)
|
|
|
|
|
|
|
|
def add_base_model_author(self, source_id: int, author: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_AUTHOR.format(id=source_id), author)
|
|
|
|
|
|
|
|
def add_base_model_version(self, source_id: int, version: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_VERSION.format(id=source_id), version)
|
|
|
|
|
|
|
|
def add_base_model_organization(self, source_id: int, organization: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_ORGANIZATION.format(id=source_id), organization)
|
|
|
|
|
2024-11-13 11:10:38 +01:00
|
|
|
def add_base_model_description(self, source_id: int, description: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_DESCRIPTION.format(id=source_id), description)
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_base_model_url(self, source_id: int, url: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_URL.format(id=source_id), url)
|
|
|
|
|
|
|
|
def add_base_model_doi(self, source_id: int, doi: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_DOI.format(id=source_id), doi)
|
|
|
|
|
|
|
|
def add_base_model_uuid(self, source_id: int, uuid: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_UUID.format(id=source_id), uuid)
|
|
|
|
|
|
|
|
def add_base_model_repo_url(self, source_id: int, repo_url: str) -> None:
|
|
|
|
self.add_string(Keys.General.BASE_MODEL_REPO_URL.format(id=source_id), repo_url)
|
|
|
|
|
2024-11-13 11:10:38 +01:00
|
|
|
def add_dataset_count(self, source_count: int) -> None:
|
|
|
|
self.add_uint32(Keys.General.DATASET_COUNT, source_count)
|
|
|
|
|
|
|
|
def add_dataset_name(self, source_id: int, name: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_NAME.format(id=source_id), name)
|
|
|
|
|
|
|
|
def add_dataset_author(self, source_id: int, author: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_AUTHOR.format(id=source_id), author)
|
|
|
|
|
|
|
|
def add_dataset_version(self, source_id: int, version: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_VERSION.format(id=source_id), version)
|
|
|
|
|
|
|
|
def add_dataset_organization(self, source_id: int, organization: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_ORGANIZATION.format(id=source_id), organization)
|
|
|
|
|
|
|
|
def add_dataset_description(self, source_id: int, description: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_DESCRIPTION.format(id=source_id), description)
|
|
|
|
|
|
|
|
def add_dataset_url(self, source_id: int, url: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_URL.format(id=source_id), url)
|
|
|
|
|
|
|
|
def add_dataset_doi(self, source_id: int, doi: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_DOI.format(id=source_id), doi)
|
|
|
|
|
|
|
|
def add_dataset_uuid(self, source_id: int, uuid: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_UUID.format(id=source_id), uuid)
|
|
|
|
|
|
|
|
def add_dataset_repo_url(self, source_id: int, repo_url: str) -> None:
|
|
|
|
self.add_string(Keys.General.DATASET_REPO_URL.format(id=source_id), repo_url)
|
|
|
|
|
2024-07-18 12:40:15 +02:00
|
|
|
def add_tags(self, tags: Sequence[str]) -> None:
|
|
|
|
self.add_array(Keys.General.TAGS, tags)
|
|
|
|
|
|
|
|
def add_languages(self, languages: Sequence[str]) -> None:
|
|
|
|
self.add_array(Keys.General.LANGUAGES, languages)
|
|
|
|
|
|
|
|
def add_tensor_data_layout(self, layout: str) -> None:
|
|
|
|
self.add_string(Keys.LLM.TENSOR_DATA_LAYOUT.format(arch=self.arch), layout)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-03-14 17:21:56 +01:00
|
|
|
def add_vocab_size(self, size: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.VOCAB_SIZE.format(arch=self.arch), size)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_context_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.CONTEXT_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_embedding_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.EMBEDDING_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
2024-12-18 18:27:21 +01:00
|
|
|
def add_features_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.FEATURES_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_posnet_embedding_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.PosNet.EMBEDDING_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_posnet_block_count(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.PosNet.BLOCK_COUNT.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_convnext_embedding_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.ConvNext.EMBEDDING_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_convnext_block_count(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.ConvNext.BLOCK_COUNT.format(arch=self.arch), length)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_block_count(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.BLOCK_COUNT.format(arch=self.arch), length)
|
|
|
|
|
Add support for DeepseekV2ForCausalLM (#7519)
* common : increase max number of experts to 160
* common : add tensors ATTN_Q_A, ATTN_Q_A_NORM, ATTN_Q_B, ATTN_KV_A_MQA, ATTN_KV_A_NORM, ATTN_KV_B needed by DeepSeek-V2 MLA (multi-head latent attention) architecture
* common : add model header parameters: leading_dense_block_count, expert_feed_forward_length, expert_shared_count, expert_weights_scale, attention.q_lora_rank, attention.kv_lora_rank, rope.scaling.yarn_log_multiplier
* convert-hf : add model conversion support for DeepseekV2ForCausalLM
* llama : add model types for DeepSeek-V2 and DeepSeek-V2-Lite models
* llama : add two new llm_build_moe_ffn() arguments: scale_w (whether to scale weights of selected MoE experts) and w_scale (numerical value of the scaling factor)
* llama : add inference support for LLM_ARCH_DEEPSEEK2
---------
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
2024-05-28 17:07:05 +02:00
|
|
|
def add_leading_dense_block_count(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.LEADING_DENSE_BLOCK_COUNT.format(arch=self.arch), length)
|
|
|
|
|
2024-07-04 19:14:21 +02:00
|
|
|
def add_feed_forward_length(self, length: int | Sequence[int]) -> None:
|
|
|
|
if isinstance(length, int):
|
|
|
|
self.add_uint32(Keys.LLM.FEED_FORWARD_LENGTH.format(arch=self.arch), length)
|
|
|
|
else:
|
|
|
|
self.add_array(Keys.LLM.FEED_FORWARD_LENGTH.format(arch=self.arch), length)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
Add support for DeepseekV2ForCausalLM (#7519)
* common : increase max number of experts to 160
* common : add tensors ATTN_Q_A, ATTN_Q_A_NORM, ATTN_Q_B, ATTN_KV_A_MQA, ATTN_KV_A_NORM, ATTN_KV_B needed by DeepSeek-V2 MLA (multi-head latent attention) architecture
* common : add model header parameters: leading_dense_block_count, expert_feed_forward_length, expert_shared_count, expert_weights_scale, attention.q_lora_rank, attention.kv_lora_rank, rope.scaling.yarn_log_multiplier
* convert-hf : add model conversion support for DeepseekV2ForCausalLM
* llama : add model types for DeepSeek-V2 and DeepSeek-V2-Lite models
* llama : add two new llm_build_moe_ffn() arguments: scale_w (whether to scale weights of selected MoE experts) and w_scale (numerical value of the scaling factor)
* llama : add inference support for LLM_ARCH_DEEPSEEK2
---------
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
2024-05-28 17:07:05 +02:00
|
|
|
def add_expert_feed_forward_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.EXPERT_FEED_FORWARD_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
2024-06-17 21:08:46 +02:00
|
|
|
def add_expert_shared_feed_forward_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.EXPERT_SHARED_FEED_FORWARD_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_parallel_residual(self, use: bool) -> None:
|
|
|
|
self.add_bool(Keys.LLM.USE_PARALLEL_RESIDUAL.format(arch=self.arch), use)
|
|
|
|
|
2024-06-24 07:06:05 +02:00
|
|
|
def add_decoder_start_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.DECODER_START_TOKEN_ID.format(arch=self.arch), id)
|
|
|
|
|
2024-07-04 19:14:21 +02:00
|
|
|
def add_head_count(self, count: int | Sequence[int]) -> None:
|
|
|
|
if isinstance(count, int):
|
|
|
|
self.add_uint32(Keys.Attention.HEAD_COUNT.format(arch=self.arch), count)
|
|
|
|
else:
|
|
|
|
self.add_array(Keys.Attention.HEAD_COUNT.format(arch=self.arch), count)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-07-04 19:14:21 +02:00
|
|
|
def add_head_count_kv(self, count: int | Sequence[int]) -> None:
|
|
|
|
if isinstance(count, int):
|
|
|
|
self.add_uint32(Keys.Attention.HEAD_COUNT_KV.format(arch=self.arch), count)
|
|
|
|
else:
|
|
|
|
self.add_array(Keys.Attention.HEAD_COUNT_KV.format(arch=self.arch), count)
|
2023-11-11 06:04:50 +01:00
|
|
|
|
2024-01-02 12:51:28 +01:00
|
|
|
def add_key_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.KEY_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_value_length(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.VALUE_LENGTH.format(arch=self.arch), length)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_max_alibi_bias(self, bias: float) -> None:
|
|
|
|
self.add_float32(Keys.Attention.MAX_ALIBI_BIAS.format(arch=self.arch), bias)
|
|
|
|
|
|
|
|
def add_clamp_kqv(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Attention.CLAMP_KQV.format(arch=self.arch), value)
|
|
|
|
|
2024-03-15 21:41:22 +01:00
|
|
|
def add_logit_scale(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.LLM.LOGIT_SCALE.format(arch=self.arch), value)
|
|
|
|
|
2024-06-30 05:44:08 +02:00
|
|
|
def add_attn_logit_softcapping(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.LLM.ATTN_LOGIT_SOFTCAPPING.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_final_logit_softcapping(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.LLM.FINAL_LOGIT_SOFTCAPPING.format(arch=self.arch), value)
|
|
|
|
|
2023-12-13 13:04:25 +01:00
|
|
|
def add_expert_count(self, count: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.EXPERT_COUNT.format(arch=self.arch), count)
|
|
|
|
|
|
|
|
def add_expert_used_count(self, count: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.EXPERT_USED_COUNT.format(arch=self.arch), count)
|
|
|
|
|
Add support for DeepseekV2ForCausalLM (#7519)
* common : increase max number of experts to 160
* common : add tensors ATTN_Q_A, ATTN_Q_A_NORM, ATTN_Q_B, ATTN_KV_A_MQA, ATTN_KV_A_NORM, ATTN_KV_B needed by DeepSeek-V2 MLA (multi-head latent attention) architecture
* common : add model header parameters: leading_dense_block_count, expert_feed_forward_length, expert_shared_count, expert_weights_scale, attention.q_lora_rank, attention.kv_lora_rank, rope.scaling.yarn_log_multiplier
* convert-hf : add model conversion support for DeepseekV2ForCausalLM
* llama : add model types for DeepSeek-V2 and DeepSeek-V2-Lite models
* llama : add two new llm_build_moe_ffn() arguments: scale_w (whether to scale weights of selected MoE experts) and w_scale (numerical value of the scaling factor)
* llama : add inference support for LLM_ARCH_DEEPSEEK2
---------
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
2024-05-28 17:07:05 +02:00
|
|
|
def add_expert_shared_count(self, count: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.EXPERT_SHARED_COUNT.format(arch=self.arch), count)
|
|
|
|
|
|
|
|
def add_expert_weights_scale(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.LLM.EXPERT_WEIGHTS_SCALE.format(arch=self.arch), value)
|
|
|
|
|
2024-09-28 14:08:43 +02:00
|
|
|
def add_swin_norm(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.LLM.SWIN_NORM.format(arch=self.arch), value)
|
|
|
|
|
2024-09-01 16:38:17 +02:00
|
|
|
def add_rescale_every_n_layers(self, count: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.RESCALE_EVERY_N_LAYERS.format(arch=self.arch), count)
|
|
|
|
|
|
|
|
def add_time_mix_extra_dim(self, dim: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.TIME_MIX_EXTRA_DIM.format(arch=self.arch), dim)
|
|
|
|
|
|
|
|
def add_time_decay_extra_dim(self, dim: int) -> None:
|
|
|
|
self.add_uint32(Keys.LLM.TIME_DECAY_EXTRA_DIM.format(arch=self.arch), dim)
|
|
|
|
|
2024-09-17 08:44:58 +02:00
|
|
|
def add_residual_scale(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.LLM.RESIDUAL_SCALE.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_embedding_scale(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.LLM.EMBEDDING_SCALE.format(arch=self.arch), value)
|
|
|
|
|
2024-09-01 16:38:17 +02:00
|
|
|
def add_wkv_head_size(self, size: int) -> None:
|
|
|
|
self.add_uint32(Keys.WKV.HEAD_SIZE.format(arch=self.arch), size)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_layer_norm_eps(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Attention.LAYERNORM_EPS.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_layer_norm_rms_eps(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Attention.LAYERNORM_RMS_EPS.format(arch=self.arch), value)
|
|
|
|
|
2024-12-18 18:27:21 +01:00
|
|
|
def add_group_norm_eps(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Attention.GROUPNORM_EPS.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_group_norm_groups(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.GROUPNORM_GROUPS.format(arch=self.arch), value)
|
|
|
|
|
2024-02-11 17:21:38 +01:00
|
|
|
def add_causal_attention(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.Attention.CAUSAL.format(arch=self.arch), value)
|
2024-02-13 13:06:58 +01:00
|
|
|
|
Add support for DeepseekV2ForCausalLM (#7519)
* common : increase max number of experts to 160
* common : add tensors ATTN_Q_A, ATTN_Q_A_NORM, ATTN_Q_B, ATTN_KV_A_MQA, ATTN_KV_A_NORM, ATTN_KV_B needed by DeepSeek-V2 MLA (multi-head latent attention) architecture
* common : add model header parameters: leading_dense_block_count, expert_feed_forward_length, expert_shared_count, expert_weights_scale, attention.q_lora_rank, attention.kv_lora_rank, rope.scaling.yarn_log_multiplier
* convert-hf : add model conversion support for DeepseekV2ForCausalLM
* llama : add model types for DeepSeek-V2 and DeepSeek-V2-Lite models
* llama : add two new llm_build_moe_ffn() arguments: scale_w (whether to scale weights of selected MoE experts) and w_scale (numerical value of the scaling factor)
* llama : add inference support for LLM_ARCH_DEEPSEEK2
---------
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
2024-05-28 17:07:05 +02:00
|
|
|
def add_q_lora_rank(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.Q_LORA_RANK.format(arch=self.arch), length)
|
|
|
|
|
|
|
|
def add_kv_lora_rank(self, length: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.KV_LORA_RANK.format(arch=self.arch), length)
|
|
|
|
|
2024-06-24 07:06:05 +02:00
|
|
|
def add_relative_attn_buckets_count(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.REL_BUCKETS_COUNT.format(arch=self.arch), value)
|
|
|
|
|
2024-07-01 18:48:34 +02:00
|
|
|
def add_sliding_window(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.Attention.SLIDING_WINDOW.format(arch=self.arch), value)
|
|
|
|
|
2024-09-17 08:44:58 +02:00
|
|
|
def add_attention_scale(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Attention.SCALE.format(arch=self.arch), value)
|
|
|
|
|
2024-02-15 18:21:49 +01:00
|
|
|
def add_pooling_type(self, value: PoolingType) -> None:
|
2024-03-02 18:21:47 +01:00
|
|
|
self.add_uint32(Keys.LLM.POOLING_TYPE.format(arch=self.arch), value.value)
|
2024-02-11 17:21:38 +01:00
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_rope_dimension_count(self, count: int) -> None:
|
|
|
|
self.add_uint32(Keys.Rope.DIMENSION_COUNT.format(arch=self.arch), count)
|
|
|
|
|
llama : add Qwen2VL support + multimodal RoPE (#10361)
* Barebone Qwen2VL LLM convertor
* Add Qwen2VL cli entrypoint
* [WIP] add qwen2vl arch
* Verify m-rope output
* Add vl-rope/2d-rope support for qwen2vl ViT
* update qwen2vl cli tool
* update 5D tensor op workaround
* [WIP] qwen2vl vision model
* make batch and clip utils compatible with qwen2vl
* [WIP] create inference workflow, gguf convert script but fix
* correcting vision-rope behavior, add the missing last layer back to ViT
* add arg parser to qwen2vl_surgery
* replace variable size array with vector
* cuda-gdb cmake preset
* add fp32 mrope, vision rope kernel
* add fp16 support for qwen2vl and m-rope
* add `GGML_ROPE_TYPE_MROPE`, `GGML_ROPE_TYPE_VISION`
* fix rope op mode switching, out dated func args
* update `llama_hparams`
* update to keep up stream changes
* resolve linter, test errors
* add makefile entry, update speical image padding token
* add mrope unit test, fix few compiler warnings
* rename `mrope` related function, params
* minor updates on debug util, bug fixs
* add `m-rope` testcase to `test-backend-ops`
* Apply suggestions from code review
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
* fix traililng whitespce
* store `llama_hparams.rope_sections` with fixed size array
* update position id tensor size check in GGML_OP_ROPE
* minor updates
* update `ggml_backend_*_supports_op` of unsupported backends
* remote old `rope_section` compare operator
---------
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
2024-12-14 13:43:46 +01:00
|
|
|
def add_rope_dimension_sections(self, dims: Sequence[int]) -> None:
|
|
|
|
self.add_array(Keys.Rope.DIMENSION_SECTIONS.format(arch=self.arch), dims)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_rope_freq_base(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Rope.FREQ_BASE.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_rope_scaling_type(self, value: RopeScalingType) -> None:
|
|
|
|
self.add_string(Keys.Rope.SCALING_TYPE.format(arch=self.arch), value.value)
|
|
|
|
|
|
|
|
def add_rope_scaling_factor(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Rope.SCALING_FACTOR.format(arch=self.arch), value)
|
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
def add_rope_scaling_attn_factors(self, value: float) -> None:
|
2024-05-21 22:28:32 +02:00
|
|
|
self.add_float32(Keys.Rope.SCALING_ATTN_FACTOR.format(arch=self.arch), value)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_rope_scaling_orig_ctx_len(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.Rope.SCALING_ORIG_CTX_LEN.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_rope_scaling_finetuned(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.Rope.SCALING_FINETUNED.format(arch=self.arch), value)
|
|
|
|
|
Add support for DeepseekV2ForCausalLM (#7519)
* common : increase max number of experts to 160
* common : add tensors ATTN_Q_A, ATTN_Q_A_NORM, ATTN_Q_B, ATTN_KV_A_MQA, ATTN_KV_A_NORM, ATTN_KV_B needed by DeepSeek-V2 MLA (multi-head latent attention) architecture
* common : add model header parameters: leading_dense_block_count, expert_feed_forward_length, expert_shared_count, expert_weights_scale, attention.q_lora_rank, attention.kv_lora_rank, rope.scaling.yarn_log_multiplier
* convert-hf : add model conversion support for DeepseekV2ForCausalLM
* llama : add model types for DeepSeek-V2 and DeepSeek-V2-Lite models
* llama : add two new llm_build_moe_ffn() arguments: scale_w (whether to scale weights of selected MoE experts) and w_scale (numerical value of the scaling factor)
* llama : add inference support for LLM_ARCH_DEEPSEEK2
---------
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
2024-05-28 17:07:05 +02:00
|
|
|
def add_rope_scaling_yarn_log_mul(self, value: float) -> None:
|
|
|
|
self.add_float32(Keys.Rope.SCALING_YARN_LOG_MUL.format(arch=self.arch), value)
|
|
|
|
|
llama : support Mamba Selective State Space Models (#5328)
* mamba : begin working on support for Mamba SSM
* mamba : begin figuring out how to (ab)use the kv cache for Mamba
* mamba : recurrent inference almost works, but incoherent
* mamba : recurrent inference WORKS!!!
* convert : optionally use d_conv and d_state from config.json for Mamba
* mamba : refactor recurrent conv, resulting in 20% perf increase
It's still slower than I'd like, but I did not really optimize `ggml_exp` yet.
I also refactored `ggml_exp` to work with tensors with more than 2 dimensions.
* ggml : parallelize ggml_exp
This results in 8% faster token generation for Mamba-130M.
* mamba : simplify the conv step with a self-overlapping view
Turns out the conv_state can be made smaller by one column.
Note that this breaks existing GGUFs of Mamba,
because the key_value_length field is tied to the conv_state size.
Convolution with a self-overlapping view is cool!
And it's much simpler than what I initially thought would be necessary
to make the convolution step work with more than 1 token at a time.
Next step is to make the SSM step work on batches of tokens too,
and thus I need to figure out a way to make a parallel selective scan
which will keep the ssm_state small and won't make it bigger
by a factor of (n_layer * batch_size).
* llama : fix Mamba KV self size wrongly displaying as f16 instead of f32
Relatedly, I also tried to see if other types than f32 worked for the states,
but they don't, because of the operators used.
It's probably better anyway to keep lots of precision there,
since the states are small anyway.
* mamba : fix self-overlapping view depth stride
* mamba : handle batches of more than 1 token
This means running Mamba no longer crashes when using the default settings!
And probably also slightly faster prompt processing.
Both batched and non-batched processing yield the same output.
Previously, the state was not cleared when starting a sequence.
Next step is to make the KV cache API work as expected for Mamba models.
* ggml: add ggml_ssm_scan to help with parallel selective scan
If the selective scan was implemented without a custom operator,
there would be waaay too many nodes in the graph. For example,
for Mamba-130M, with a batch size of 512 (the default),
a naive selective scan could add at least 24*512=12288 nodes,
which is more than LLAMA_MAX_NODES (8192),
and that's only for the smallest Mamba model.
So it's much cleaner with a custom operator.
Not sure about the name, though.
* ggml : in ggml_ssm_scan, merge multiple rows in the same vec operation
This will help with performance on CPU if ggml_vec_mul_f32
and ggml_vec_add_f32 are ever optimized with SIMD.
* mamba : very basic quantization support
Mostly works, but there is currently no difference
between the variants of a k-quant (e.g. Q4_K_S and Q4_K_M are the same).
Most of the SSM-specific weights can be kept in f32 without affecting
the size that much, since they are relatively small.
(the linear projection weights are responsible for most of Mamba's size)
Too much quantization seems to make the state degrade quite fast, and
the model begins to output gibberish.
It seems to affect bigger models to a lesser extent than small models,
but I'm not sure by how much.
Experimentation will be needed to figure out which weights are more important
for the _M (and _L?) variants of k-quants for Mamba.
* convert : fix wrong name for layer norm weight of offical Mamba models
I was using Q-bert/Mamba-* models before, which have a slighlty different
naming scheme for the weights.
(they start with "model.layers" instead of "backbone.layers")
* mamba : fuse more steps of the SSM scan in the ggml_ssm_scan operator
This increases performance on CPU by around 30% for prompt processing,
and by around 20% for text generation.
However, it also makes the ggml_exp and ggml_soft_plus operators unused.
Whether or not they should be kept will be decided later.
* convert : for Mamba, also consider the "MambaLMHeadModel" arch name
It's the name of the class of the official implementation,
though they don't use it (yet) in the "architectures" field of config.json
* mamba : fix vocab size problems with official models
The perplexity was waaaay to high for models with a non-round vocab size.
Not sure why, but it needed to be fixed in the metadata.
Note that this breaks existing GGUF-converted Mamba models,
but **only if** the vocab size was not already rounded.
* ggml : remove ggml_exp and ggml_soft_plus
They did not exist anyway outside of this branch,
and since ggml_ssm_scan fused operations together, they are unused.
It's always possible to bring them back if needed.
* mamba : remove some useless comments
No code change.
* convert : fix flake8 linter errors
* mamba : apply suggestions from code review
* mamba : remove unecessary branch for row-wise ssm_state and C multiplication
It was previously done to avoid permuting when only one token is processed
at a time (like when generating text), but permuting is cheap,
and dynamically changing the compute graph is not future-proof.
* ggml : in ggml_ssm_scan, use more appropriate asserts
* ggml : rename the destination pointer in ggml_compute_forward_ssm_scan_f32
* mamba : multiple sequences, but one at a time
This is a step towards making this Mamba implementation usable
with the server example (the way the system prompt is kept when clearing
the client slots will need to be changed before this can work, though).
The KV cache size for this kind of model is tied to the maximum number
of sequences kept at any single time.
For now, this number is obtained from n_parallel (plus one,
to have an extra sequence to dedicate to the system prompt),
but there might be a better way to do this which won't also
make the main example use 2 cells even if only 1 is really used.
(for this specific case, --parallel 0 helps)
Simultaneous sequence processing will probably require changes to
ggml_ssm_scan, and possibly a new operator for the conv step.
* mamba : support llama_kv_cache_seq_cp
This (mis)uses the logic around K shifts, because tokens in a state
can't be shifted anyway, and because inp_K_shift has the right shape and type.
Using ggml_get_rows is a nice way to do copies, but copy chains can't work.
Fortunately, copy chains don't really seem to be used in the examples.
Each KV cell is dedicated to the sequence ID corresponding to its own index.
* mamba : use a state mask
It's cleaner than the previous heuristic of
checking for the pos of the first token in the batch.
inp_KQ_mask could not be re-used for this, because it has the wrong shape
and because it seems more suited to the next step of
simultaneous sequence processing (helping with the problem of
remembering which token belongs to which sequence(s)/state(s)).
* llama : replace the usage of n_ctx with kv_self.size in many places
* mamba : use n_tokens directly instead of n_tok
* mamba : in comments, properly refer to KV cells instead of slots
* mamba : reduce memory usage of ggml_ssm_scan
From 290.37 MiB to 140.68 MiB of CPU compute buffer size
with Mamba 3B with a batch size of 512.
The result tensor of ggml_ssm_scan was previously a big part
of the CPU compute buffer size. To make it smaller,
it does not contain the intermediate ssm states anymore.
Both y and the last ssm state are combined in the result tensor,
because it seems only a single tensor can be returned by an operator
with the way the graph is built.
* mamba : simultaneous sequence processing
A batch can now contain tokens from multiple sequences.
This is necessary for at least the parallel example, the server example,
and the HellaSwag test in the perplexity example.
However, for this to be useful, uses of llama_kv_cache_seq_rm/cp
will need to be changed to work on whole sequences.
* ggml : add ggml_ssm_conv as a new operator for the conv step of Mamba
This operator makes it possible to use and update the correct states
for each token of the batch in the same way as ggml_ssm_scan.
Other solutions which use existing operators would need loops which would
add too many nodes to the graph (at least the ones I thought of).
Using this operator further reduces the size of the CPU compute buffer
from 140.68 MiB to 103.20 MiB with Mamba 3B with a batch size of 512.
And (at least on CPU), it's a bit faster than before.
Note that "ggml_ssm_conv" is probably not the most appropriate name,
and it could be changed if a better one is found.
* llama : add inp_s_seq as a new input tensor
The most convenient implementation to select the correct state (for Mamba)
for each token is to directly get the correct index from a tensor.
This is why inp_s_seq is storing int32_t and not floats.
The other, less convenient way to select the correct state would be
to have inp_KQ_mask contain 1.0f for each state used by a token
and 0.0f otherwise. This complicates quickly fetching the first used
state of a token, and is also less efficient because a whole row
of the mask would always need to be read for each token.
Using indexes makes it easy to stop searching when there are
no more sequences for a token, and the first sequence assigned
is always very quickly available (it's the first element of each row).
* mamba : support llama_kv_cache_seq_cp copy chains
* mamba : support shifting and dividing the kv cache pos
* mamba : make the server and parallel examples work with whole sequences
A seq_id is dedicated to the system prompt in both cases.
* llama : make llama_kv_cache_seq_rm return whether it succeeded or not
* mamba : dedicate an input tensor for state copy indices
This is cleaner and makes it easier to adapt when/if token positions
(and by extension, inp_K_shift) are no longer integers.
* mamba : adapt perplexity, batched, and batched-bench examples
* perplexity : limit the max number of sequences
This adapts to what the loaded model can provide.
* llama : add llama_n_max_seq to get the upper limit for seq_ids
Used by the perplexity example.
* batched : pass n_parallel to the model's context params
This should have been there already, but it wasn't.
* batched-bench : reserve sequences to support Mamba
* batched-bench : fix tokens being put in wrong sequences
Generation quality isn't what's measured in there anyway,
but at least using the correct sequences avoids using non-consecutive
token positions.
* mamba : stop abusing attention metadata
This breaks existing converted-to-GGUF Mamba models,
but will allow supporting mixed architectures like MambaFormer
without needing to break Mamba models.
This will also allow changing the size of Mamba's states
without having to reconvert models in the future.
(e.g. using something else than d_conv - 1 columns for the conv_states
will not require breaking existing converted Mamba models again)
* gguf-py : add new KV metadata key-value pairs for Mamba
* llama : add new metadata key-value pairs for Mamba
* llama : guard against divisions by zero when n_head is 0
* mamba : rename "unlimited" KV cache property to "recurrent"
* mamba : more correctly update the "used" field of the KV cache
* ggml : in ggml_ssm_scan, use a threshold for soft_plus
This is how the official Mamba implementation does it,
and it's also what torch.nn.Softplus does.
* convert : for Mamba, fallback to internal NeoX tokenizer
The resulting models are exactly the same
as if the tokenizer.json and tokenizer_config.json of GPT-NeoX were there.
* mamba : support state saving and restoring
* ggml : implicitly pass src tensors through dst for Mamba-related ops
* mamba : clarify some comments
* server : fix cache_tokens not getting correctly resized
Otherwise, when the "we have to evaluate at least 1 token" special case
was triggered, an extra token was kept in cache_tokens even if it was
removed from the KV cache.
For Mamba, this caused useless prompt reprocessing when the previous
request triggered the above case.
* convert-hf : support new metadata keys for Mamba
For the models available at
https://huggingface.co/collections/state-spaces/transformers-compatible-mamba-65e7b40ab87e5297e45ae406
* mamba : rename metadata to be more similar to transformers library
This breaks existing converted-to-GGUF models,
but the metadata names are more "standard".
* mamba : support mamba-*-hf models
These models share their token_embd.weight with their output.weight
* mamba : add missing spaces
This is purely a formatting change.
* convert-hf : omit output.weight when identical with token_embd.weight
Only for Mamba for now, but it might be relevant for other models eventually.
Most Mamba models actually share these two tensors, albeit implicitly.
* readme : add Mamba to supported models, and add recent API changes
* mamba : move state_seq and state_mask views outside layer loop
A few tensors were also missing `struct` in front of `ggml_tensor`.
2024-03-08 23:31:00 +01:00
|
|
|
def add_ssm_conv_kernel(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.SSM.CONV_KERNEL.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_ssm_inner_size(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.SSM.INNER_SIZE.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_ssm_state_size(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.SSM.STATE_SIZE.format(arch=self.arch), value)
|
|
|
|
|
|
|
|
def add_ssm_time_step_rank(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.SSM.TIME_STEP_RANK.format(arch=self.arch), value)
|
|
|
|
|
2024-08-21 10:06:36 +02:00
|
|
|
def add_ssm_dt_b_c_rms(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.SSM.DT_B_C_RMS.format(arch=self.arch), value)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_tokenizer_model(self, model: str) -> None:
|
|
|
|
self.add_string(Keys.Tokenizer.MODEL, model)
|
|
|
|
|
2024-04-29 15:58:41 +02:00
|
|
|
def add_tokenizer_pre(self, pre: str) -> None:
|
|
|
|
self.add_string(Keys.Tokenizer.PRE, pre)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_token_list(self, tokens: Sequence[str] | Sequence[bytes] | Sequence[bytearray]) -> None:
|
|
|
|
self.add_array(Keys.Tokenizer.LIST, tokens)
|
|
|
|
|
|
|
|
def add_token_merges(self, merges: Sequence[str] | Sequence[bytes] | Sequence[bytearray]) -> None:
|
|
|
|
self.add_array(Keys.Tokenizer.MERGES, merges)
|
|
|
|
|
|
|
|
def add_token_types(self, types: Sequence[TokenType] | Sequence[int]) -> None:
|
|
|
|
self.add_array(Keys.Tokenizer.TOKEN_TYPE, types)
|
|
|
|
|
2024-02-11 17:21:38 +01:00
|
|
|
def add_token_type_count(self, value: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.TOKEN_TYPE_COUNT, value)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_token_scores(self, scores: Sequence[float]) -> None:
|
|
|
|
self.add_array(Keys.Tokenizer.SCORES, scores)
|
|
|
|
|
|
|
|
def add_bos_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.BOS_ID, id)
|
|
|
|
|
|
|
|
def add_eos_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.EOS_ID, id)
|
|
|
|
|
|
|
|
def add_unk_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.UNK_ID, id)
|
|
|
|
|
|
|
|
def add_sep_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.SEP_ID, id)
|
|
|
|
|
|
|
|
def add_pad_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.PAD_ID, id)
|
|
|
|
|
2024-02-15 14:14:37 +01:00
|
|
|
def add_cls_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.CLS_ID, id)
|
|
|
|
|
|
|
|
def add_mask_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.MASK_ID, id)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def add_add_bos_token(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.Tokenizer.ADD_BOS, value)
|
|
|
|
|
|
|
|
def add_add_eos_token(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.Tokenizer.ADD_EOS, value)
|
|
|
|
|
2024-02-01 10:19:51 +01:00
|
|
|
def add_add_space_prefix(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.Tokenizer.ADD_PREFIX, value)
|
|
|
|
|
2024-06-24 07:06:05 +02:00
|
|
|
def add_remove_extra_whitespaces(self, value: bool) -> None:
|
|
|
|
self.add_bool(Keys.Tokenizer.REMOVE_EXTRA_WS, value)
|
|
|
|
|
|
|
|
def add_precompiled_charsmap(self, charsmap: Sequence[bytes]) -> None:
|
|
|
|
self.add_array(Keys.Tokenizer.PRECOMPILED_CHARSMAP, charsmap)
|
|
|
|
|
2024-04-18 13:49:01 +02:00
|
|
|
def add_chat_template(self, value: str | Sequence[Mapping[str, str]]) -> None:
|
2024-05-09 00:16:38 +02:00
|
|
|
if not isinstance(value, str):
|
2024-04-18 13:49:01 +02:00
|
|
|
template_default = None
|
|
|
|
template_names = set()
|
|
|
|
|
|
|
|
for choice in value:
|
|
|
|
name = choice.get('name', '')
|
|
|
|
template = choice.get('template')
|
|
|
|
|
|
|
|
# Allowing non-alphanumerical characters in template name is probably not a good idea, so filter it
|
|
|
|
name = ''.join((c if c in ascii_letters + digits else '_' for c in name))
|
|
|
|
|
|
|
|
if name and template is not None:
|
|
|
|
if name == 'default':
|
|
|
|
template_default = template
|
|
|
|
else:
|
|
|
|
template_names.add(name)
|
|
|
|
self.add_string(Keys.Tokenizer.CHAT_TEMPLATE_N.format(name=name), template)
|
|
|
|
|
|
|
|
if template_names:
|
|
|
|
self.add_array(Keys.Tokenizer.CHAT_TEMPLATES, list(template_names))
|
|
|
|
|
|
|
|
if template_default is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
value = template_default
|
|
|
|
|
2023-11-19 11:10:52 +01:00
|
|
|
self.add_string(Keys.Tokenizer.CHAT_TEMPLATE, value)
|
|
|
|
|
2024-04-16 08:13:13 +02:00
|
|
|
def add_eot_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.EOT_ID, id)
|
|
|
|
|
2024-08-05 09:38:01 +02:00
|
|
|
def add_eom_token_id(self, id: int) -> None:
|
|
|
|
self.add_uint32(Keys.Tokenizer.EOM_ID, id)
|
|
|
|
|
2023-11-11 06:04:50 +01:00
|
|
|
def _pack(self, fmt: str, value: Any, skip_pack_prefix: bool = False) -> bytes:
|
|
|
|
pack_prefix = ''
|
|
|
|
if not skip_pack_prefix:
|
|
|
|
pack_prefix = '<' if self.endianess == GGUFEndian.LITTLE else '>'
|
|
|
|
return struct.pack(f'{pack_prefix}{fmt}', value)
|
|
|
|
|
2024-06-09 04:34:29 +02:00
|
|
|
def _pack_val(self, val: Any, vtype: GGUFValueType, add_vtype: bool) -> bytes:
|
|
|
|
kv_data = bytearray()
|
|
|
|
|
|
|
|
if add_vtype:
|
|
|
|
kv_data += self._pack("I", vtype)
|
|
|
|
|
|
|
|
pack_fmt = self._simple_value_packing.get(vtype)
|
|
|
|
if pack_fmt is not None:
|
|
|
|
kv_data += self._pack(pack_fmt, val, skip_pack_prefix = vtype == GGUFValueType.BOOL)
|
|
|
|
elif vtype == GGUFValueType.STRING:
|
|
|
|
encoded_val = val.encode("utf-8") if isinstance(val, str) else val
|
|
|
|
kv_data += self._pack("Q", len(encoded_val))
|
|
|
|
kv_data += encoded_val
|
2024-07-30 16:57:03 +02:00
|
|
|
elif vtype == GGUFValueType.ARRAY:
|
|
|
|
|
|
|
|
if not isinstance(val, Sequence):
|
|
|
|
raise ValueError("Invalid GGUF metadata array, expecting sequence")
|
|
|
|
|
|
|
|
if len(val) == 0:
|
|
|
|
raise ValueError("Invalid GGUF metadata array. Empty array")
|
|
|
|
|
2024-06-24 07:06:05 +02:00
|
|
|
if isinstance(val, bytes):
|
|
|
|
ltype = GGUFValueType.UINT8
|
|
|
|
else:
|
|
|
|
ltype = GGUFValueType.get_type(val[0])
|
|
|
|
if not all(GGUFValueType.get_type(i) is ltype for i in val[1:]):
|
|
|
|
raise ValueError("All items in a GGUF array should be of the same type")
|
2024-06-09 04:34:29 +02:00
|
|
|
kv_data += self._pack("I", ltype)
|
|
|
|
kv_data += self._pack("Q", len(val))
|
|
|
|
for item in val:
|
|
|
|
kv_data += self._pack_val(item, ltype, add_vtype=False)
|
|
|
|
else:
|
|
|
|
raise ValueError("Invalid GGUF metadata value type or value")
|
|
|
|
|
|
|
|
return kv_data
|
|
|
|
|
2024-06-24 11:42:03 +02:00
|
|
|
@staticmethod
|
|
|
|
def format_n_bytes_to_str(num: int) -> str:
|
|
|
|
if num == 0:
|
|
|
|
return "negligible - metadata only"
|
|
|
|
fnum = float(num)
|
|
|
|
for unit in ("", "K", "M", "G"):
|
|
|
|
if abs(fnum) < 1000.0:
|
|
|
|
return f"{fnum:3.1f}{unit}"
|
|
|
|
fnum /= 1000.0
|
|
|
|
return f"{fnum:.1f}T - over 1TB, split recommended"
|