Skip to content

Is it possible to look into a safetensors file and grab its metadata? #275

Closed Answered by unphased
unphased asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks. I went the underengineered route.

sd_model_discriminator.sh

#!/bin/bash

FILE=$1
METADATA_DUMP_SCRIPT=~/util/safetensormetadump

COUNT_1024=$($METADATA_DUMP_SCRIPT $FILE | grep '^\s*1024' | wc -l)

if (( $COUNT_1024 > 50 )); then
    echo "guess v2: $FILE"
else
    echo "guess v1.5: $FILE"
fi

safetensormetadump:

#!/bin/bash

# Specify the file
FILE="$1"

# Extract the first 8 bytes and convert them to a decimal integer
HEADER_LENGTH=$(dd "if=$FILE" bs=1 count=8 2>/dev/null | od -An -vtu8)

# Extract the metadata, starting from the 9th byte
dd "if=$FILE" bs=1 skip=8 "count=$HEADER_LENGTH" 2>/dev/null | jq
  • Extracts the metadata json from the safetensor
  • sends it to jq to prettify it

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@unphased
Comment options

Answer selected by unphased
@julien-c
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants