aboutsummaryrefslogtreecommitdiff
path: root/tools/embed.py
blob: bc4157ea925f0e3af0219863db0a22906108ff6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Turns the weight blob into a C source file so a binary carries no data file.

Deliberately depends on nothing but the standard library: this runs inside the
container build, where the checkpoint, PyTorch and pyannote are not present and
should not have to be. Regenerating the blob itself needs all three and is a
separate step.

    python3 tools/embed.py pyseg_weights.bin src/weights_blob.c
"""

import sys

HEADER = """/* Generated by tools/embed.py from {name}. Do not edit.
 *
 * The packed weight blob, so a binary needs no data file at runtime.
 * pyseg_create_from_file reads the identical bytes from disk instead. */

#include <stddef.h>

__attribute__((aligned(64)))
const unsigned char pyseg_weights_blob[] = {{
"""


def main(argv: list[str]) -> int:
    if len(argv) != 3:
        print(__doc__.strip(), file=sys.stderr)
        return 2

    with open(argv[1], "rb") as f:
        data = f.read()

    step = 24
    with open(argv[2], "w") as out:
        out.write(HEADER.format(name=argv[1]))
        for i in range(0, len(data), step):
            out.write("".join(f"{b:d}," for b in data[i : i + step]))
            out.write("\n")
        out.write("};\n")
        out.write(f"const size_t pyseg_weights_blob_len = {len(data)};\n")
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))