Python中如何读取大模型.gguf后缀文件的方法指南
1、首先安装gguf包
pip install gguf
2、运行以下代码,把路径改成自己的gguf文件路径即可。
import logging
import sys
from pathlib import Path
from gguf.gguf_reader import GGUFReader
logger = logging.getLogger("reader")
def read_gguf_file(gguf_file_path):
reader = GGUFReader(gguf_file_path)
print("Key-Value Pairs:")
max_key_length = max(len(key) for key in reader.fields.keys())
for key, field in reader.fields.items():
value = field.parts[field.data[0]]
print(f"{key:{max_key_length}} : {value}")
print("----")
print("Tensors:")
tensor_info_format = "{:<30} | Shape: {:<15} | Size: {:<12} | Quantization: {}"
print(tensor_info_format.format("Tensor Name", "Shape", "Size", "Quantization"))
print("-" * 80)
for tensor in reader.tensors:
shape_str = "x".join(map(str, tensor.shape))
size_str = str(tensor.n_elements)
quantization_str = tensor.tensor_type.name
print(tensor_info_format.format(tensor.name, shape_str, size_str, quantization_str))
if __name__ == '__main__':
gguf_file_path = "/path/to/your/gguf/file"#改成自己的gguf文件路径
read_gguf_file(gguf_file_path)
作者:孙奶奶爱吃溜溜梅