Open
Conversation
Author
|
Also need this fix issue dealing with endianness of float types: thammegowda@09064c8 I ended up reformatting the code to suit my coding style. main...thammegowda:safetensors.cpp:main |
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.
Thank you for this library/code! I tried to load a safetensor file but got 0 tensors loaded. I asked an AI coding agent to find and fix the bug. Note: apart from this line of text, the rest is generated by agent -- but I've verified that the fix works.
Safetensors.hpp Bug Fix Summary
Problem
The
safetensors.hpplibrary was loading 0 tensors from safetensors files, while the Python reference implementation correctly loaded 340 tensors from the test filetmp/gemma-3-1b-it/model.safetensors.Root Cause
The bug was in the
SimpleJSONParserclass insafetensors.hpp. Specifically:Improper metadata skipping: When encountering the
__metadata__field in the JSON header, the parser used a simplistic approach:This approach failed because:
"__metadata__":{"format":"pt"}, it would stop at the first}(end of metadata object)Solution
Added a proper
skipValue()method that recursively handles:The fix involved:
skipValue()method to properly skip JSON values of any complexityparseTensorInfo()to useskipValue()for unknown fieldsparse()method to useskipValue()for the metadata objectChanges Made
Files Modified
libs/safetensors.cpp/safetensors.hpp- Fixed JSON parsertests/test_serialize.cpp- Enhanced test case with better error reportingFiles Created (for testing/verification)
tests/test_safetensors_load.py- Python reference implementationtests/debug_safetensors_header.py- Debug script to inspect safetensors headerstests/test_verify_tensors.py- Verification script for specific tensorsTest Results
Before Fix
After Fix
Verification
How to Test
Python Test
cd /home/tg/work/repos/projectXYZ python3 tests/test_safetensors_load.py tmp/gemma-3-1b-it/model.safetensorsC++ Test
cd /home/tg/work/repos/projectXYZ ./build-debug/tests/projectXYZ-tests safetensors_load tmp/gemma-3-1b-it/model.safetensorsBoth should report loading 340 tensors successfully.