Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(adapters): read bfloat16 adapters in adapters check and adapters diff (#2058) (#2063)

Younes Berianea1921ee36ba3
diff --git a/changelog.d/0.76.0/2063.fixed.md b/changelog.d/0.76.0/2063.fixed.md
new file mode 100644
index 00000000..6aa31a77
--- /dev/null
+++ b/changelog.d/0.76.0/2063.fixed.md
@@ -0,0 +1 @@
+- **`soup adapters check` and `soup adapters diff` no longer crash on a bfloat16 adapter.** Both read the weights through `safe_open(framework="numpy")`, which cannot return bfloat16 (numpy has no such dtype), so an adapter saved from a bf16 run failed with `data type 'bfloat16' not understood`, or with "Corrupt or unreadable safetensors weights" in `check`. They now share one reader that parses the header with `read_header` and widens bfloat16 to float32 exactly (it is the top 16 bits of a float32); the other dtypes adapters store are read as before, a dtype numpy cannot hold (FP8, and the U16/U32/U64 tags `read_header` does not map) is refused as an unsupported dtype naming the tensor, and the size cap, tensor-count cap and symlink checks are unchanged. (#2063)
diff --git a/src/soup_cli/utils/adapter_check.py b/src/soup_cli/utils/adapter_check.py
index ce58a2ba..9eb98bea 100644
--- a/src/soup_cli/utils/adapter_check.py
+++ b/src/soup_cli/utils/adapter_check.py
@@ -20,12 +20,11 @@ from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Optional, Tuple
-from soup_cli.utils.adapter_diff import _adapter_weights_path, _require_str
+from soup_cli.utils.adapter_diff import _adapter_weights_path, _read_safetensors_numpy
from soup_cli.utils.paths import enforce_under_cwd_and_no_symlink, is_under_cwd
from soup_cli.utils.terminal import strip_control
INNER_NAMESPACE_MARKER = ".inner."
-_MAX_LAYERS = 10_000
#: The largest adapter weights file ``soup adapters check`` will read: 2 GiB. The check
#: loads every tensor into memory and accumulates in float64, so the resident size is a
@@ -99,16 +98,9 @@ def _read_config(adapter_dir: Path) -> dict[str, Any]:
def _load_safetensors_numpy(path: Path) -> dict[str, Any]:
- """Lazy-load adapter tensors using safetensors numpy framework."""
- try:
- from safetensors import safe_open
- except ImportError as exc:
- raise RuntimeError(
- "safetensors package required; pip install safetensors"
- ) from exc
-
+ """Load adapter tensors as numpy arrays (bfloat16 widened to float32)."""
# Size first, from the filesystem and before the file is opened: the tensor-count
- # cap below does not bound memory, one 100 GB tensor is a single key.
+ # cap does not bound memory, one 100 GB tensor is a single key.
try:
size = os.stat(str(path)).st_size
except OSError as exc:
@@ -119,23 +111,14 @@ def _load_safetensors_numpy(path: Path) -> dict[str, Any]:
f"{MAX_ADAPTER_BYTES:,} bytes ({MAX_ADAPTER_BYTES // 1024**3} GiB) "
"because it loads every tensor into memory"
)
-
- result: dict[str, Any] = {}
try:
- with safe_open(str(path), framework="numpy") as f:
- keys = list(f.keys())
- if len(keys) > _MAX_LAYERS:
- raise ValueError(f"adapter has >{_MAX_LAYERS} tensors")
- for key in keys:
- _require_str(key, "tensor name")
- result[key] = f.get_tensor(key)
+ return _read_safetensors_numpy(path)
except (ValueError, RuntimeError):
raise
except Exception as exc:
raise ValueError(
f"Corrupt or unreadable safetensors weights in {path.name}: {exc}"
) from exc
- return result
def _split_lora_role(name: str) -> Optional[Tuple[str, str]]:
diff --git a/src/soup_cli/utils/adapter_diff.py b/src/soup_cli/utils/adapter_diff.py
index 2dc73547..4b10ad4b 100644
--- a/src/soup_cli/utils/adapter_diff.py
+++ b/src/soup_cli/utils/adapter_diff.py
@@ -177,24 +177,81 @@ def _validate_top_k(top_k: object) -> int:
return top_k
-def _load_safetensors(path: Path) -> Mapping[str, Any]:
- """Lazy-load adapter_model.safetensors via the ``safetensors`` package."""
+#: Safetensors dtype tag -> little-endian numpy dtype, for everything except BF16.
+_NUMPY_DTYPES = {
+ "float16": "<f2",
+ "float32": "<f4",
+ "float64": "<f8",
+ "int8": "i1",
+ "int16": "<i2",
+ "int32": "<i4",
+ "int64": "<i8",
+ "uint8": "u1",
+ "bool": "?",
+}
+
+
+def _read_safetensors_numpy(path: Path) -> dict[str, Any]:
+ """Read every tensor of a safetensors file as a numpy array, BF16 included.
+
+ ``safe_open(framework="numpy")`` cannot return bfloat16 (numpy has no such dtype), so
+ an adapter trained in bf16, which is the default dtype of a ``torch_dtype: bfloat16``
+ base, failed with ``data type 'bfloat16' not understood`` (#2058). The header is parsed
+ by ``read_header`` (no mapping, offsets and sizes validated) and the payload read
+ directly; a bfloat16 tensor is widened to float32 exactly, since it is the top 16 bits
+ of a float32.
+
+ The arrays are read-only views of the bytes read (``np.frombuffer``); copy one before
+ changing it in place.
+
+ Raises ``ValueError`` for more than ``_MAX_LAYERS`` tensors (before any payload is
+ read), for a header ``read_header`` refuses, and for a dtype numpy cannot hold (the
+ FP8 spellings ``read_header`` accepts), each naming the file once.
+ """
+ import numpy as np
+
+ from soup_cli.utils.safetensors_reader import read_header
+
try:
- from safetensors import safe_open
- except ImportError as exc:
- raise RuntimeError(
- "safetensors package required; pip install safetensors"
- ) from exc
+ entries = read_header(str(path))
+ except (ValueError, OSError) as exc:
+ # read_header leads its messages with the path; the file is named once, here.
+ reason = str(exc).removeprefix(f"{path}: ").replace(str(path), path.name)
+ lead = "Unsupported" if "unsupported dtype" in reason else "Corrupt or unreadable"
+ raise ValueError(f"{lead} safetensors weights in {path.name}: {reason}") from exc
+ if len(entries) > _MAX_LAYERS:
+ raise ValueError(f"adapter has >{_MAX_LAYERS} tensors")
result: dict[str, Any] = {}
- with safe_open(str(path), framework="numpy") as f:
- for key in f.keys():
- _require_str(key, "tensor name")
- result[key] = f.get_tensor(key)
- if len(result) > _MAX_LAYERS:
- raise ValueError(f"adapter has >{_MAX_LAYERS} tensors")
+ with open(str(path), "rb") as handle:
+ for name, entry in entries.items():
+ _require_str(name, "tensor name")
+ numpy_dtype = _NUMPY_DTYPES.get(entry.dtype)
+ if numpy_dtype is None and entry.dtype != "bfloat16":
+ raise ValueError(
+ f"Unsupported safetensors weights in {path.name}: tensor {name!r} "
+ f"has unsupported dtype {entry.dtype!r}; numpy has no such dtype"
+ )
+ handle.seek(entry.start)
+ raw = handle.read(entry.nbytes)
+ if len(raw) != entry.nbytes:
+ raise ValueError(
+ f"Corrupt or unreadable safetensors weights in {path.name}: "
+ f"tensor {name!r} is truncated"
+ )
+ if entry.dtype == "bfloat16":
+ bits = np.frombuffer(raw, dtype="<u2").astype(np.uint32) << 16
+ array = bits.view(np.float32)
+ else:
+ array = np.frombuffer(raw, dtype=numpy_dtype)
+ result[name] = array.reshape(entry.shape)
return result
+def _load_safetensors(path: Path) -> Mapping[str, Any]:
+ """Load adapter_model.safetensors as numpy arrays (bfloat16 widened to float32)."""
+ return _read_safetensors_numpy(path)
+
+
def _adapter_weights_path(adapter_dir: Path) -> Path:
"""Return the safetensors path inside an adapter dir, raising if missing.
diff --git a/tests/test_issue1721_adapters_check.py b/tests/test_issue1721_adapters_check.py
index f384ba15..42e2be50 100644
--- a/tests/test_issue1721_adapters_check.py
+++ b/tests/test_issue1721_adapters_check.py
@@ -21,6 +21,7 @@ from __future__ import annotations
import json
import math
+import re
from pathlib import Path
import numpy as np
@@ -28,6 +29,7 @@ import pytest
from typer.testing import CliRunner
from soup_cli.cli import app as soup_app
+from soup_cli.utils import adapter_diff as adapter_diff_module
from soup_cli.utils.adapter_check import (
AdapterCheckReport,
check_adapter,
@@ -853,3 +855,259 @@ def test_every_rendered_name_is_stripped(
monkeypatch.chdir(tmp_path)
res = runner.invoke(soup_app, ["adapters", "check", "no\x1b[2Jdir"])
assert res.exit_code == 1 and chr(27) not in res.output
+
+
+# ---------- bfloat16 adapters (#2058) ----------
+
+
+def _bf16_bits(arr: np.ndarray) -> np.ndarray:
+ """Round float32 to bfloat16 (round-to-nearest-even), as raw uint16 bits."""
+ bits = np.ascontiguousarray(arr, dtype=np.float32).view(np.uint32).astype(np.uint64)
+ bits = bits + 0x7FFF + ((bits >> 16) & 1)
+ return (bits >> 16).astype(np.uint16)
+
+
+def _bf16_to_f32(bits: np.ndarray) -> np.ndarray:
+ return (bits.astype(np.uint32) << 16).view(np.float32)
+
+
+def _write_raw_adapter(dir_path: Path, tensors: dict[str, tuple[str, tuple, bytes]]) -> None:
+ """Write a safetensors adapter by hand: ``{name: (dtype tag, shape, payload bytes)}``."""
+ header: dict = {}
+ payload = b""
+ for name, (tag, shape, raw) in tensors.items():
+ header[name] = {
+ "dtype": tag,
+ "shape": list(shape),
+ "data_offsets": [len(payload), len(payload) + len(raw)],
+ }
+ payload += raw
+ hdr = json.dumps(header).encode("utf-8")
+ hdr += b" " * (-len(hdr) % 8)
+ dir_path.mkdir(parents=True, exist_ok=True)
+ (dir_path / "adapter_model.safetensors").write_bytes(
+ len(hdr).to_bytes(8, "little") + hdr + payload
+ )
+ cfg = {
+ "peft_type": "LORA", "r": 8, "lora_alpha": 16.0,
+ "target_modules": ["q_proj", "v_proj"],
+ }
+ (dir_path / "adapter_config.json").write_text(json.dumps(cfg), encoding="utf-8")
+
+
+def _write_bf16_adapter(dir_path: Path, weights: dict[str, np.ndarray]) -> None:
+ """Write a bfloat16 safetensors adapter by hand (header + uint16 payload), no torch."""
+ _write_raw_adapter(
+ dir_path,
+ {name: ("BF16", arr.shape, _bf16_bits(arr).tobytes()) for name, arr in weights.items()},
+ )
+
+
+def _bf16_weights(b_value: float) -> tuple[dict, dict]:
+ rng = np.random.default_rng(0)
+ a = rng.standard_normal((8, 16)).astype(np.float32)
+ b = b_value * rng.standard_normal((16, 8)).astype(np.float32)
+ exact = {Q_LORA_A: _bf16_to_f32(_bf16_bits(a)), Q_LORA_B: _bf16_to_f32(_bf16_bits(b))}
+ return {Q_LORA_A: a, Q_LORA_B: b}, exact
+
+
+def test_bf16_adapter_check_matches_float32(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
+ pytest.importorskip("safetensors")
+ monkeypatch.chdir(tmp_path)
+ weights, exact = _bf16_weights(0.5)
+ _write_bf16_adapter(tmp_path / "bf16", weights)
+ _write_synthetic_adapter(tmp_path / "f32", exact)
+
+ got = check_adapter("bf16")
+ want = check_adapter("f32")
+ assert got.verdict == "alive"
+ assert got.total_frobenius == pytest.approx(want.total_frobenius, rel=1e-9)
+ assert got.live_fraction == want.live_fraction
+
+ res = runner.invoke(soup_app, ["adapters", "check", "bf16"])
+ assert res.exit_code == 0, (res.output, repr(res.exception))
+ assert res.stdout.strip().splitlines()[-1] == "alive"
+
+
+def test_bf16_zero_lora_b_reports_inactive(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
+ pytest.importorskip("safetensors")
+ monkeypatch.chdir(tmp_path)
+ weights, _ = _bf16_weights(0.0)
+ _write_bf16_adapter(tmp_path / "bf16_zero", weights)
+
+ report = check_adapter("bf16_zero")
+ assert report.verdict == "inactive"
+ assert report.verdict_line == "inactive: all lora_B layers are zero (1/1)"
+ res = runner.invoke(soup_app, ["adapters", "check", "bf16_zero"])
+ assert res.exit_code == 2, (res.output, repr(res.exception))
+
+
+def test_bf16_adapter_diff_matches_float32(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
+ pytest.importorskip("safetensors")
+ from soup_cli.utils.adapter_diff import compute_adapter_diff
+
+ monkeypatch.chdir(tmp_path)
+ w1, e1 = _bf16_weights(0.5)
+ w2, e2 = _bf16_weights(0.9)
+ _write_bf16_adapter(tmp_path / "bf_a", w1)
+ _write_bf16_adapter(tmp_path / "bf_b", w2)
+ _write_synthetic_adapter(tmp_path / "f_a", e1)
+ _write_synthetic_adapter(tmp_path / "f_b", e2)
+
+ got = compute_adapter_diff("bf_a", "bf_b")
+ want = compute_adapter_diff("f_a", "f_b")
+ assert got.shared_layers == want.shared_layers == 2
+ assert [d.frobenius for d in got.per_layer] == pytest.approx(
+ [d.frobenius for d in want.per_layer], rel=1e-9
+ )
+ assert got.effective_rank_a == pytest.approx(want.effective_rank_a, rel=1e-9)
+
+
[email protected]("dtype", sorted(adapter_diff_module._NUMPY_DTYPES))
[email protected]("shape", [(3, 5), (), (0, 4)], ids=["matrix", "scalar", "empty"])
+def test_adapter_load_matches_safe_open_numpy(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, dtype: str, shape: tuple
+):
+ """Every dtype the reader maps comes back as ``safe_open(framework="numpy")`` gives it."""
+ pytest.importorskip("safetensors")
+ from safetensors import safe_open
+ from safetensors.numpy import save_file
+
+ from soup_cli.utils.adapter_diff import load_adapter_weights
+
+ monkeypatch.chdir(tmp_path)
+ rng = np.random.default_rng(1)
+ values = np.asarray(rng.standard_normal(shape) * 100)
+ if dtype == "bool":
+ array = np.asarray(values > 0)
+ elif np.issubdtype(np.dtype(dtype), np.unsignedinteger):
+ array = np.asarray(np.abs(values).astype(dtype))
+ else:
+ array = np.asarray(values.astype(dtype))
+ (tmp_path / "typed").mkdir()
+ target = tmp_path / "typed" / "adapter_model.safetensors"
+ save_file({Q_LORA_A: array}, str(target))
+
+ with safe_open(str(target), framework="numpy") as f:
+ want = f.get_tensor(Q_LORA_A)
+ got = load_adapter_weights("typed")[Q_LORA_A]
+ assert got.dtype == want.dtype, (got.dtype, want.dtype)
+ assert got.shape == want.shape, (got.shape, want.shape)
+ assert np.array_equal(got, want), (got, want)
+
+
+def test_bf16_adapter_widens_exactly(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
+ pytest.importorskip("safetensors")
+ from soup_cli.utils.adapter_diff import load_adapter_weights
+
+ monkeypatch.chdir(tmp_path)
+ weights, exact = _bf16_weights(0.5)
+ _write_bf16_adapter(tmp_path / "bf16", weights)
+
+ loaded = load_adapter_weights("bf16")
+ for name, want in exact.items():
+ assert loaded[name].dtype == np.float32
+ assert loaded[name].shape == want.shape
+ assert np.array_equal(loaded[name], want)
+
+
+def test_bf16_truncated_payload_is_a_named_refusal(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ pytest.importorskip("safetensors")
+ monkeypatch.chdir(tmp_path)
+ weights, _ = _bf16_weights(0.5)
+ _write_bf16_adapter(tmp_path / "bf16", weights)
+ target = tmp_path / "bf16" / "adapter_model.safetensors"
+ target.write_bytes(target.read_bytes()[:-4])
+
+ with pytest.raises(ValueError, match="past the end of the file") as excinfo:
+ check_adapter("bf16")
+ assert str(excinfo.value).startswith(
+ "Corrupt or unreadable safetensors weights in adapter_model.safetensors: tensor "
+ ), str(excinfo.value)
+ assert str(excinfo.value).count("adapter_model.safetensors") == 1, str(excinfo.value)
+ res = runner.invoke(soup_app, ["adapters", "check", "bf16"])
+ assert res.exit_code == 1, (res.output, repr(res.exception))
+
+
+def test_bf16_adapter_over_the_tensor_cap_is_refused(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ pytest.importorskip("safetensors")
+ import soup_cli.utils.adapter_diff as adapter_diff
+
+ monkeypatch.chdir(tmp_path)
+ weights, _ = _bf16_weights(0.5)
+ _write_bf16_adapter(tmp_path / "bf16", weights)
+ monkeypatch.setattr(adapter_diff, "_MAX_LAYERS", 1)
+
+ with pytest.raises(ValueError, match="tensors"):
+ check_adapter("bf16")
+ with pytest.raises(ValueError, match="tensors"):
+ adapter_diff.load_adapter_weights("bf16")
+
+
+def test_payload_shorter_than_its_header_is_a_named_refusal(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """The file shrinking after ``read_header`` checked it is refused, not reshaped."""
+ pytest.importorskip("safetensors")
+ import soup_cli.utils.adapter_diff as adapter_diff
+ from soup_cli.utils import safetensors_reader
+
+ monkeypatch.chdir(tmp_path)
+ weights, _ = _bf16_weights(0.5)
+ _write_bf16_adapter(tmp_path / "bf16", weights)
+ target = tmp_path / "bf16" / "adapter_model.safetensors"
+ real_read_header = safetensors_reader.read_header
+
+ def read_then_truncate(path: str):
+ entries = real_read_header(path)
+ target.write_bytes(target.read_bytes()[:-4])
+ return entries
+
+ monkeypatch.setattr(safetensors_reader, "read_header", read_then_truncate)
+ with pytest.raises(ValueError, match=f"tensor {Q_LORA_B!r} is truncated"):
+ adapter_diff.load_adapter_weights("bf16")
+
+
[email protected]("tag", ["F8_E4M3", "F8_E5M2"])
+def test_fp8_adapter_is_refused_with_tensor_and_dtype(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, tag: str
+):
+ """``read_header`` knows the FP8 spellings, the numpy reader does not (#2061)."""
+ pytest.importorskip("safetensors")
+ from soup_cli.utils import safetensors_reader
+ from soup_cli.utils.adapter_diff import load_adapter_weights
+
+ monkeypatch.chdir(tmp_path)
+ _write_raw_adapter(tmp_path / "fp8", {Q_LORA_A: (tag, (4, 2), bytes(8))})
+ dtype = safetensors_reader._DTYPES[tag]
+ want = f"tensor {Q_LORA_A!r} has unsupported dtype {dtype!r}"
+
+ with pytest.raises(ValueError, match=re.escape(want)):
+ check_adapter("fp8")
+ with pytest.raises(ValueError, match=re.escape(want)):
+ load_adapter_weights("fp8")
+ res = runner.invoke(soup_app, ["adapters", "check", "fp8"])
+ assert res.exit_code == 1, (res.output, repr(res.exception))
+ assert "KeyError" not in res.output, res.output
+
+
+def test_unmapped_header_dtype_is_unsupported_not_corrupt(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """A dtype ``read_header`` refuses (U16) is reported as unsupported, file named once."""
+ pytest.importorskip("safetensors")
+ monkeypatch.chdir(tmp_path)
+ _write_raw_adapter(tmp_path / "u16", {Q_LORA_A: ("U16", (4, 2), bytes(16))})
+
+ with pytest.raises(ValueError) as excinfo:
+ check_adapter("u16")
+ message = str(excinfo.value)
+ assert message.startswith(
+ "Unsupported safetensors weights in adapter_model.safetensors: tensor "
+ ), message
+ assert "Corrupt" not in message, message
+ assert message.count("adapter_model.safetensors") == 1, message