feat(vram): parse IEC binary size suffixes (KiB..PiB)

ParseSizeString accepted only SI suffixes, so a "20GiB" floor was rejected
outright. Model and VRAM sizes are conventionally quoted in IEC units, and
silently reading GiB as GB would understate a floor by about 7%.

Purely additive: these inputs previously returned an unknown-suffix error.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2026-07-18 09:56:27 +00:00
parent ad79ec28fb
commit cd6f584618
2 changed files with 20 additions and 2 deletions

View File

@@ -169,8 +169,9 @@ func EstimateMultiContext(ctx context.Context, files []FileInput, contextSizes [
}
// ParseSizeString parses a human-readable size string (e.g. "500MB", "14.5 GB", "2tb")
// into bytes. Supports B, KB, MB, GB, TB, PB (case-insensitive, space optional).
// Uses SI units (1 KB = 1000 B).
// into bytes. Supports B, KB, MB, GB, TB, PB (case-insensitive, space optional)
// as SI units (1 KB = 1000 B), and the IEC suffixes KiB, MiB, GiB, TiB, PiB as
// binary units (1 KiB = 1024 B).
func ParseSizeString(s string) (uint64, error) {
s = strings.TrimSpace(s)
if s == "" {
@@ -212,6 +213,18 @@ func ParseSizeString(s string) (uint64, error) {
multiplier = 1000 * 1000 * 1000 * 1000
case "P", "PB":
multiplier = 1000 * 1000 * 1000 * 1000 * 1000
// IEC binary suffixes. Model and VRAM sizes are conventionally quoted in
// these, so treating "GiB" as "GB" would understate a floor by 7%.
case "KIB":
multiplier = 1024
case "MIB":
multiplier = 1024 * 1024
case "GIB":
multiplier = 1024 * 1024 * 1024
case "TIB":
multiplier = 1024 * 1024 * 1024 * 1024
case "PIB":
multiplier = 1024 * 1024 * 1024 * 1024 * 1024
default:
return 0, fmt.Errorf("unknown size suffix: %q", suffix)
}

View File

@@ -25,6 +25,11 @@ var _ = Describe("ParseSizeString", func() {
Entry("short suffix 100M", "100M", uint64(100_000_000)),
Entry("short suffix 2G", "2G", uint64(2_000_000_000)),
Entry("short suffix 1K", "1K", uint64(1_000)),
Entry("binary 1KiB", "1KiB", uint64(1024)),
Entry("binary 512MiB", "512MiB", uint64(512*1024*1024)),
Entry("binary 6GiB", "6GiB", uint64(6*1024*1024*1024)),
Entry("binary 2TiB", "2TiB", uint64(2*1024*1024*1024*1024)),
Entry("binary 1.5 gib lowercase with space", "1.5 gib", uint64(1.5*1024*1024*1024)),
)
DescribeTable("invalid sizes",