aboutsummaryrefslogtreecommitdiff
path: root/tools/embed.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/embed.py')
-rw-r--r--tools/embed.py45
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))