diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:03:39 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:13:28 +0200 |
| commit | 64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch) | |
| tree | 85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /tools/embed.py | |
| download | pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip | |
Init
Diffstat (limited to 'tools/embed.py')
| -rw-r--r-- | tools/embed.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/embed.py b/tools/embed.py new file mode 100644 index 0000000..bc4157e --- /dev/null +++ b/tools/embed.py @@ -0,0 +1,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)) |
