feat(weights): add exact weight footprint from safetensors header#64
Merged
Conversation
Add `weight_footprint_bytes(model_dir) -> Option<u64>` to `mlxcel-core::weights` that returns the byte-accurate weight size before any tensors are loaded. Resolution order: 1. `metadata.total_size` from `model.safetensors.index.json` (sharded models — already parsed by `parse_shard_index`, now also extracts the discarded field) 2. Safetensors binary header of a single `model.safetensors` — reads 8-byte LE header-length prefix plus the JSON header object, sums dtype × shape-product per tensor entry without touching tensor data 3. Returns `None` when neither is available; callers fall back to analytical estimate `parse_shard_index` is unchanged in return type; new `parse_shard_index_with_total_size` exposes the extended result via the `ShardIndexResult` type alias (added to silence clippy::type_complexity). Wire exact footprint into `quant_advisor.rs`: - `QuantAdvice` gains `exact_weight_bytes: Option<u64>` - `advise_quantization` calls `weight_footprint_bytes` and converts exact bytes to a billions-of-parameters estimate (bytes / 2 / 1e9, FP16 reference), which supersedes the analytical config.json estimate when present - `print_quant_advice` shows the exact GiB/MiB figure and source tag when available; analytical estimate is shown as a reference note New unit tests: 9 in `weights::tests` (sharded index with/without total_size, single-file binary header, scalar tensor, dtype itemsize table, missing case) and 4 in `quant_advisor::tests` (exact_weight_bytes field, index wiring, format_bytes helpers).
This was referenced May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds byte-accurate weight-size accounting from safetensors metadata so
--recommend-quantreports the exact model footprint before any tensor data is loaded, with the analytical config.json estimate as a fallback.What changed
src/lib/mlxcel-core/src/weights.rs: addedweight_footprint_bytes(model_dir) -> Option<u64>(public),parse_shard_index_with_total_size(public) that exposes the previously discardedmetadata.total_sizefield,parse_shard_index_inner(private shared implementation),extract_shards_and_total_size(private),read_safetensors_header_bytes(private),safetensors_dtype_itemsize(private), and theShardIndexResulttype alias. The originalparse_shard_indexandextract_shards_from_index_jsonare unchanged in public behavior.src/execution/quant_advisor.rs:QuantAdvicegainsexact_weight_bytes: Option<u64>;advise_quantizationcallsweight_footprint_bytesand converts exact bytes to a billions-of-parameters signal (takes precedence over the analytical estimate);print_quant_adviceshows exact GiB/MiB with source tag and shows the analytical estimate as a reference note. Newformat_byteshelper. Importsmlxcel_core::weights::weight_footprint_bytes.Test plan
cargo test -p mlxcel-core --lib weights::tests— all 22 tests pass (9 new: sharded index with/without total_size, single-file binary header, scalar tensor, dtype itemsize, missing case)cargo test -p mlxcel --lib execution::quant_advisor::tests— all 11 tests pass (4 new: exact_weight_bytes field, index wiring, format_bytes helpers)cargo clippy -p mlxcel-core --lib --tests -- -D warnings— cleancargo clippy -p mlxcel --lib --tests -- -D warnings— cleanCloses #53