Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access header with python #576

Open
justinchuby opened this issue Feb 23, 2025 · 1 comment
Open

How to access header with python #576

justinchuby opened this issue Feb 23, 2025 · 1 comment

Comments

@justinchuby
Copy link

Is there a way to access the header in Python to know the offsets of each tensor data?

@justinchuby justinchuby changed the title Access header with python How to access header with python Feb 23, 2025
@justinchuby
Copy link
Author

I am now using this snippet:

def read_safetensors_header(file: BytesIO) -> dict[str, dict[str, Any]]:
    """Read the header of a safetensors file.

    Args:
        file: The safetensors file to read.

    Returns:
        The header of the safetensors file.
    """
    header_size = struct.unpack_from("i", file.read(8))[0]
    header = file.read(header_size)
    return json.loads(header.decode("utf-8"))

Please let me know if there are better ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant