CivArchive
    ← All articles
    Published September 15, 2024by zentrocdot

    Print Header of LoRA files

    96 views3 reactions0 comments on CivitAI1 collected
    loratool guide

    Foreword

    When I started working with LoRA files I realized that I need a tool to view the header of such a Lora file. Since this task is quite easy to solve, I did not do much research in this case, just wrote a Python program right away.

    In this article, I assume that a LoRA file is available as a .safetensors file. The structure of a .safetensors file will described briefly in the technical background section.

    Reading LoRA files

    The procedure to read the LoRA file and print the header is straightforward. First the file will be opened for reading. Next the size of the header is read, which is stored in the first 8 bytes of the file. Afterwards one can read the header based on the given header length. Then the retrieved header as bytes data is converted into JSON data. The whole thing is then printed out in a structured form.

    The Python3 script which does the job can be found on GitHub [1].

    Technical Background

    The .safetensors file is a binary file. This binary file consists of three parts in general. The first part is 8 bytes long and stores the file size of the header. Then there is the second part, which is the header of the file, consisting of a metadata section and a section with pointers to the tensors from part three. The previous mentioned tensors can be found in the third part.

    Using a hex editor one can see, that part two is in readable form, while part one and part three are unreadable binary informations.

    Outlook

    In on of the next articles I will present a small python program, which extracts and prints the meta data of a LoRA file. After that I present a Python script, which can change the metadata of a LoRA file. My last version is working well and in final testing phase.

    Finally

    Have a nice day! Have fun! Be inspired!

    Reference

    [1] https://github.com/zentrocdot/artificial-intelligence-tools/blob/main/python/print_header_v0002.py

    [2] https://github.com/huggingface/safetensors