Download labs/shave_models/boolean/release.py from tablinum/RailShot-Stream: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/tablinum/RailShot-Stream/resolve/main/labs/shave_models/boolean/release.py
- Command line
-
hf download hf://tablinum/RailShot-Stream/labs/shave_models/boolean/release.py
-
curl -L -o release.py https://huggingface.co/tablinum/RailShot-Stream/resolve/main/labs/shave_models/boolean/release.py
3.2 kB
| """Transport a Gather manifest and both native 2020.3 blobs losslessly.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import struct | |
| import zlib | |
| from blob_layout import compact_gather_blob, inspect_blob | |
| MAGIC = b"M8B1" | |
| HEADER = struct.Struct("<4s3I") | |
| TARGETS = {"ncs1": ("ncs1-native.blob", "VPU_MYRIAD_2450"), | |
| "ncs2": ("ncs2-2020.3.blob", "VPU_MYRIAD_2480")} | |
| def _validate(manifest, blobs): | |
| if not isinstance(manifest, dict) or manifest.get("variant") != "gather": | |
| raise ValueError("Release requires a Gather manifest") | |
| inputs = manifest.get("input_shapes") | |
| shape = inputs.get("features") if isinstance(inputs, dict) else None | |
| if not isinstance(shape, list) or len(shape) != 1 or type(shape[0]) is not int or shape[0] < 1: | |
| raise ValueError("Release has invalid Gather geometry") | |
| for stick, blob in blobs.items(): | |
| count = manifest.get("fp16_coefficient_bytes") if stick == "ncs1" else None | |
| layout = inspect_blob(blob, compact_coefficients=count) | |
| if (layout["input_bytes"] != shape[0] * 4 or layout["output_bytes"] != shape[0] * 2 | |
| or layout["stage_count"] != 1 or layout["sole_stage"]["type"] != 100): | |
| raise ValueError("Native Gather blob does not match the release geometry") | |
| def pack_bundle(directory, compact_ncs1=False): | |
| manifest = (directory / "model.json").read_bytes() | |
| blobs = {stick: (directory / filename).read_bytes() | |
| for stick, (filename, _) in TARGETS.items()} | |
| model = json.loads(manifest) | |
| _validate(model, blobs) | |
| if compact_ncs1: | |
| blobs["ncs1"] = compact_gather_blob(blobs["ncs1"], model.get("fp16_coefficient_bytes")) | |
| manifest = json.dumps(model, ensure_ascii=False, separators=(",", ":")).encode("utf8") | |
| fields = (manifest, blobs["ncs1"], blobs["ncs2"]) | |
| return zlib.compress(HEADER.pack(MAGIC, *(len(field) for field in fields)) + b"".join(fields), 9) | |
| def read_release(path): | |
| try: | |
| data = zlib.decompress(path.read_bytes()) | |
| except zlib.error as exc: | |
| raise ValueError("Unreadable MagicEightBall release") from exc | |
| if len(data) < HEADER.size: | |
| raise ValueError("Missing MagicEightBall release header") | |
| magic, manifest_size, ncs1_size, ncs2_size = HEADER.unpack_from(data) | |
| boundary = HEADER.size + manifest_size | |
| split = boundary + ncs1_size | |
| if magic != MAGIC or split + ncs2_size != len(data): | |
| raise ValueError("Invalid MagicEightBall release layout") | |
| manifest = json.loads(data[HEADER.size:boundary]) | |
| blobs = {"ncs1": data[boundary:split], "ncs2": data[split:]} | |
| _validate(manifest, blobs) | |
| return manifest, blobs | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("bundle", type=Path) | |
| parser.add_argument("output", type=Path) | |
| parser.add_argument("--compact-ncs1", action="store_true", help="Remove verified NCS1 terminal zero padding") | |
| args = parser.parse_args() | |
| archive = pack_bundle(args.bundle, args.compact_ncs1) | |
| args.output.write_bytes(archive) | |
| print("%d bytes" % len(archive)) | |