Foreword
Following my activities around Pickel Tensor and Safetensors file, I am presenting her, how the Tensors in a Safetensors file can be printed into the terminal window.
Read Tensors from Safetensors File
To read the Tensors in a .safetensors file like a model is simple as it is. With some line of Python code this can be realised. The file to read is e.g. defined as model.safetensors. This has to be changed by the user.
#!/usr/bin/python3
# Import all required modules.
from safetensors import safe_open
# Set the filename.
FN="model.safetensors"
# Create an empty tensor.
tensors = {}
# Read tensor by tensor from file.
with safe_open(FN, framework="pt", device=0) as fn:
for key in fn.keys():
# Get the value to the given key.
tensors[key] = fn.get_tensor(key)
# Print the given key.
print(key)
# Print the value related to the key.
print(tensors[key])
One will see, running the script, that there is a key as well as a value to this key. The value is the Tensor we are talking about.
Open the file of interest for reading. Simply loop over the keys and print key as well as value. That it is.
Finally
Have a nice day. Have fun. Be inspired!