Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

fix(rife): an executor's answer damaged on the way, anywhere, is a RIFE that fails in the reading's words; each member is read whole, its CRC-32 checked, before its array

Soohong Kimb210e00ef600
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b40b149..0081d4d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,9 +7,9 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the
**Upgrading from 2.45.2: Defaults stay unchanged, cache keys update once, reports show the backend, install checks model hashes, and schemas are unchanged.** With `SPRITE_GEN_RIFE_BACKEND` unset (the default), RIFE is the `rife-ncnn-vulkan` binary as before. Its frames are the same bytes as 2.45.2. Measured: the binary's frames and sixteen-pass readings through the changed engine equal the old engine's, byte for byte. A `--between-cache` directory kept by 2.45.2 is made again once. The key now names the backend (`backend: "ncnn"`). The engine's `rife.py` changed, and its sha256 is part of the key anyway. Reports take `interpolator.kind` from what made the frames: `rife-ncnn-vulkan` as before for the binary, or `rife-torch` for the new backend. The overlapped wrap's record counts `binary_calls` for the binary as before, or `runs` for the torch backend. `SPRITE_GEN_RIFE_EXECUTOR` (new) is unset by default, and RIFE runs in this process. `sprite-gen rife install` now checks the model's two files (`rife-v4.6/flownet.param`, `flownet.bin`) against pinned SHA-256 after unpacking, for either backend. A model that differs installs nothing. The line a missing RIFE is reported with (`install` in records, warnings) is `sprite-gen rife install` for the binary. Where the torch backend is chosen, that line is: `pip install "sprite-gen[torch]" && sprite-gen rife install --backend torch`. No command-line flags of `video-loop` / `video-cycle-align` / `video-set` change. No restoration schemas change: `source-restoration-v5`, repair report `schema_version` 4, comparison `schema_version` 6.
- **New: RIFE's torch backend runs rife-v4.6 in PyTorch for compute GPUs without Vulkan.** `SPRITE_GEN_RIFE_BACKEND=torch` runs the same rife-v4.6 in PyTorch (`sprite_gen/video/rife_torch.py`). It serves a GPU the binary cannot run on: rife-ncnn-vulkan needs Vulkan, and a GPU offered for compute alone (CUDA with no Vulkan driver) has none. The network is rife-v4.6's IFNet written again from Practical-RIFE's `IFNet_HDv3` (MIT, notice in NOTICE). Its weights are read out of the binary's own model files (SHA-256 pinned, the binary's float16 weights), with no other download. One run does what one call of the binary does with `-x -z`. The pair is turned eight ways and run both ways in time, with flows averaged at each of four levels. The loop follows rife-ncnn-vulkan's `RIFE::process_v4` (MIT, notice in NOTICE). `SPRITE_GEN_RIFE_DEVICE` names `cuda` (default) or `cpu`. `SPRITE_GEN_RIFE_TTA` means the same for both backends. The default augmentation stays `-x -z`. To install: `pip install "sprite-gen[torch]"` (new extra: `torch>=2.13.0,<3`). Then run: `sprite-gen rife install --backend torch` (new flag; defaults to `SPRITE_GEN_RIFE_BACKEND`). This unpacks the model, and makes a check frame through PyTorch on `SPRITE_GEN_RIFE_DEVICE`. It reads the model only, so a Linux ARM machine the binary does not run on is not refused. A CPU-only host installs PyTorch's CPU build first: `pip install torch==2.13.0 --index-url https://download.pytorch.org/whl/cpu` (192 MB, against the CUDA build's 527 MB plus its CUDA libraries). Nothing falls back. With torch chosen the binary is never looked for or run. `cuda` where PyTorch finds no CUDA is refused naming why (a CPU-only PyTorch, or a CUDA that finds no GPU) and does not run on the CPU. `mps` (Apple's GPU) is refused (no lane measures it; on a Mac the binary runs on the GPU already). A backend or device named wrong is refused. A device that fails as the network is put on it or as a frame runs (out of its memory, its driver or CUDA failing) is a RIFE that fails, in PyTorch's own words: the loop fails with its record, and the install's check frame fails. PyTorch not installed, or no model where the backend looks (`SPRITE_GEN_RIFE_MODEL`, else the install root), is RIFE not installed, as the binary missing is. Under `--repair auto` the loop is cut as filmed with the warning, and its record names the torch install line. On CUDA each frame runs with cuDNN deterministic, its benchmark off and convolutions in IEEE float32 (no TF32). The same frame twice is the same bytes on one GPU, driver and PyTorch. What made a frame is the RIFE's identity: backend, model bytes loaded, network code, PyTorch, on CUDA the GPU, driver, CUDA, cuDNN and those settings; on the CPU its instruction set. Because of this identity, `--between-cache` never hands one backend's or GPU's frame to another. Measured on synthetic walkers: Seven walker pairs (clean, outline lost, ghost, torn, a cross-fade to look at) get the same verdicts from the binary on an Apple GPU, the binary on Mesa llvmpipe and the torch backend on a CPU. The torch frame lies 0.005 to 0.052 of 255 on average from the binary's Apple GPU frame. The binary's own llvmpipe frame lies 0.007 to 0.25. A 960 x 960 frame of a synthetic walker with `-x -z`: On an Apple M4 Max CPU (12 threads), 3.3 to 3.6 s; the binary on that Mac's GPU takes 1.27 to 1.37 s. In a Modal container of two CPUs (the app's job size), 22.6 to 28.5 s on two threads; the binary on Mesa llvmpipe in the same container takes 127.7 to 131.5 s. The torch backend's frames are not the binary's bytes, which is one reason the binary stays the default. [loop-repair](docs/loop-repair.md) section 1
-- **New: a host with no GPU sends the torch backend's frames to a GPU elsewhere (`SPRITE_GEN_RIFE_EXECUTOR`).** The host names a send of its own, `SPRITE_GEN_RIFE_EXECUTOR=module:attr`: a function from a request's bytes to its answer's. It reaches `sprite_gen.video.rife_torch.serve` on the GPU's machine, the same sprite-gen with PyTorch and the model installed there. The engine knows nothing of what carries the bytes, and the host needs no PyTorch. A frame is one call (its two runs made there), and a read is one call (its two runs and sixteen passes, the passes reduced there to how far they disagree). A RIFE asks the executor what it is once, as it is made (the probe, which takes the cold start), so a `video-loop` makes at most 53 calls. The RIFE holds that answer for its life, and every request carries it. An executor that is not it any more (another GPU, driver or image) makes nothing, and the RIFE fails naming what differs (`gpu: Tesla T4 -> NVIDIA L4`). An answer from another identity is not handed on. So one RIFE never hands on frames of two GPUs, and a frame taken from `--between-cache` is the frame it would make; the next RIFE asks again and keys its frames anew. Refused: an executor named with the binary's backend or with `SPRITE_GEN_RIFE_DEVICE` (the device is the executor's), a send that cannot be imported, and an executor whose `rife.py` or protocol is another (run the same sprite-gen on both sides). A send that raises, an answer that does not read and the executor's own failure are each a RIFE that fails, in its words; nothing is asked again or run elsewhere. The host names one GPU type for the executor (no fallback list) and does not deploy it anew while a run goes on. Requests and answers are a compressed NumPy archive, read with `allow_pickle=False`. Reports name `executor` and the executor's `device` and `gpu`; the overlapped wrap counts `runs` and `executor_calls`. [loop-repair](docs/loop-repair.md) section 1
+- **New: a host with no GPU sends the torch backend's frames to a GPU elsewhere (`SPRITE_GEN_RIFE_EXECUTOR`).** The host names a send of its own, `SPRITE_GEN_RIFE_EXECUTOR=module:attr`: a function from a request's bytes to its answer's. It reaches `sprite_gen.video.rife_torch.serve` on the GPU's machine, the same sprite-gen with PyTorch and the model installed there. The engine knows nothing of what carries the bytes, and the host needs no PyTorch. A frame is one call (its two runs made there), and a read is one call (its two runs and sixteen passes, the passes reduced there to how far they disagree). A RIFE asks the executor what it is once, as it is made (the probe, which takes the cold start), so a `video-loop` makes at most 53 calls. The RIFE holds that answer for its life, and every request carries it. An executor that is not it any more (another GPU, driver or image) makes nothing, and the RIFE fails naming what differs (`gpu: Tesla T4 -> NVIDIA L4`). An answer from another identity is not handed on. So one RIFE never hands on frames of two GPUs, and a frame taken from `--between-cache` is the frame it would make; the next RIFE asks again and keys its frames anew. Refused: an executor named with the binary's backend or with `SPRITE_GEN_RIFE_DEVICE` (the device is the executor's), a send that cannot be imported, and an executor whose `rife.py` or protocol is another (run the same sprite-gen on both sides). A send that raises, an answer that does not read (not the format, damaged or cut short on the way, or not the frame's size) and the executor's own failure are each a RIFE that fails, in its words; nothing is asked again or run elsewhere. The host names one GPU type for the executor (no fallback list) and does not deploy it anew while a run goes on. Requests and answers are a compressed NumPy archive, read with `allow_pickle=False`, each member whole so its CRC-32 is checked before its array is read: an answer damaged on the way hands on no other pixels. Reports name `executor` and the executor's `device` and `gpu`; the overlapped wrap counts `runs` and `executor_calls`. [loop-repair](docs/loop-repair.md) section 1
- **Lane: `scripts/linux_lane.sh` tests PyTorch 2.13.0 CPU after the suite.** After the suite, PyTorch 2.13.0 CPU is installed into the same environment. It runs `sprite-gen rife install --backend torch`'s check frame. It runs every test marked `torch_rife` with `SPRITE_GEN_RIFE_DEVICE=cpu`, with none skipped. `--rife-only` runs the same. `--rife-unmeasured` lets `torch_rife` tests skip as it lets `real_rife` ones. CUDA is not measured by this lane.
-- **Tests**: `tests/video/test_rife_torch.py` (the eight ways undo themselves and are the single passes' eight turns; flows of a textured patch run alone each of the sixteen ways and turned back point alike, where a wrong sign, a transpose's x/y, or a backward swap puts one elsewhere; `-z` and `-x` give the same frame the other way in time and flipped; the install check frame's disc lands half way; the model loads strict and other bytes are refused by SHA-256; the same frame twice is the same bytes and the identity names what makes it; seven recorded synthetic walker pairs in `tests/fixtures/rife-walkers` are judged as the binary judged them with no binary, the frame within twice the binary's own GPU-to-GPU gap, and alike to this machine's binary where both run; a device that fails as the network is put on it or as a frame runs (out of its memory, its driver, CUDA not starting, a CUDA call deferred to its start) is a RIFE that fails in PyTorch's words, a TypeError there is raised as it is, a loop whose device fails as the network is put on it fails with its record, and the install's check frame fails; without PyTorch: torch chosen and not installed is RIFE not installed and the binary is never run, a loop's record names it, a backend or device named wrong is refused before anything is looked for, cuda where there is none is refused, a model not the pinned one is refused; with a runner stood in: reports name rife-torch and count runs, the loop and the alignment name the torch backend, and a frame kept by one runner is not taken for another); `tests/video/test_rife_executor.py` (a frame is one call and a read one call, and the RIFE probes once as it is made, their bytes the same device's in this process with this host's augmentation; every request names the engine code and the identity held; the host runs without PyTorch; the identity is the executor's as the RIFE was made; a T4 frame kept is the RIFE's own after the executor turns L4, which it then refuses to make anew, and the next RIFE misses and keeps the L4 frame under its own key; an executor whose GPU, driver, network code, weights, PyTorch, cuDNN or determinism changed makes nothing, the field named; an answer from another identity is neither handed on nor kept; another container of the same identity runs on, and the next RIFE takes every frame with its probe alone; a T4 and L4 pool is refused at the first L4 request; a frame made while this host's augmentation changed is not kept; other engine code or another protocol is refused before anything loads; a request that does not read, or is pickled, is answered so; a send, an answer or an executor that fails is a RIFE that fails, in its words; an executor named with the binary, with a device, or not there is refused and the binary never runs; a loop names the executor; marked `torch_rife`: `rife_torch.serve` makes the frame and the read of the backend in this process, loaded once); `tests/video/test_rife_install.py` (a zip whose model is not the pinned one installs nothing for both backends; `--backend torch` installs on a machine the binary does not run on and checks its frame through torch); `tests/release/test_lane_rife_collection.py` (a test RIFE decides carries `real_rife` or `torch_rife`); new pytest mark `torch_rife` (skipped where PyTorch, the model or the named device is missing).
+- **Tests**: `tests/video/test_rife_torch.py` (the eight ways undo themselves and are the single passes' eight turns; flows of a textured patch run alone each of the sixteen ways and turned back point alike, where a wrong sign, a transpose's x/y, or a backward swap puts one elsewhere; `-z` and `-x` give the same frame the other way in time and flipped; the install check frame's disc lands half way; the model loads strict and other bytes are refused by SHA-256; the same frame twice is the same bytes and the identity names what makes it; seven recorded synthetic walker pairs in `tests/fixtures/rife-walkers` are judged as the binary judged them with no binary, the frame within twice the binary's own GPU-to-GPU gap, and alike to this machine's binary where both run; a device that fails as the network is put on it or as a frame runs (out of its memory, its driver, CUDA not starting, a CUDA call deferred to its start) is a RIFE that fails in PyTorch's words, a TypeError there is raised as it is, a loop whose device fails as the network is put on it fails with its record, and the install's check frame fails; without PyTorch: torch chosen and not installed is RIFE not installed and the binary is never run, a loop's record names it, a backend or device named wrong is refused before anything is looked for, cuda where there is none is refused, a model not the pinned one is refused; with a runner stood in: reports name rife-torch and count runs, the loop and the alignment name the torch backend, and a frame kept by one runner is not taken for another); `tests/video/test_rife_executor.py` (a frame is one call and a read one call, and the RIFE probes once as it is made, their bytes the same device's in this process with this host's augmentation; every request names the engine code and the identity held; the host runs without PyTorch; the identity is the executor's as the RIFE was made; a T4 frame kept is the RIFE's own after the executor turns L4, which it then refuses to make anew, and the next RIFE misses and keeps the L4 frame under its own key; an executor whose GPU, driver, network code, weights, PyTorch, cuDNN or determinism changed makes nothing, the field named; an answer from another identity is neither handed on nor kept; another container of the same identity runs on, and the next RIFE takes every frame with its probe alone; a T4 and L4 pool is refused at the first L4 request; a frame made while this host's augmentation changed is not kept; other engine code or another protocol is refused before anything loads; a request that does not read, is pickled or was damaged on the way is answered so, in the reading's words; a send, an answer or an executor that fails is a RIFE that fails, in its words; an answer damaged in a member's local header, a compressed block, an array's header or its own bytes, or its end record, or cut short, is a RIFE that fails in the words of what reading it raised, which is its cause, and undamaged is the frame made here; each byte of the probe's, a frame's and a read's answer damaged in turn fails the RIFE or reads as it was made, nothing else raised and no other pixels handed on; a reading that cannot read what `pack` writes raises as it is, a fault of the code; an answer that is not bytes does not read; an executor named with the binary, with a device, or not there is refused and the binary never runs; a loop names the executor, and a loop whose executor's answer was damaged on the way fails with its record (`failed`, in the decompressor's words, the gait fallback's frames removed); marked `torch_rife`: `rife_torch.serve` makes the frame and the read of the backend in this process, loaded once); `tests/video/test_rife_install.py` (a zip whose model is not the pinned one installs nothing for both backends; `--backend torch` installs on a machine the binary does not run on and checks its frame through torch); `tests/release/test_lane_rife_collection.py` (a test RIFE decides carries `real_rife` or `torch_rife`); new pytest mark `torch_rife` (skipped where PyTorch, the model or the named device is missing).
## v2.45.2 - The new `video-loop --overlap-wrap off` turns the overlapped wrap off on its own; the motion anchor's fine search is faster and gives the same values
diff --git a/docs/loop-repair.md b/docs/loop-repair.md
index db0ce6b..4e93e78 100644
--- a/docs/loop-repair.md
+++ b/docs/loop-repair.md
@@ -120,18 +120,21 @@ The host keeps to three things:
executor of other code is refused as the RIFE is made.
- Do not deploy the executor anew while a loop or an alignment runs: that run fails, by why.
-A send that raises, an answer that does not read, or the executor's own failure (its model not
-installed, its GPU out of memory) is a RIFE that fails, in its words. Nothing is asked again, and
-nothing runs here or on another device in its place.
+A send that raises, an answer that does not read (not the format, damaged or cut short on the
+way, or not the frame's size), or the executor's own failure (its model not installed, its GPU out
+of memory) is a RIFE that fails, in its words. Nothing is asked again, and nothing runs here or on
+another device in its place.
A request and an answer are each one compressed NumPy archive (`np.savez_compressed`, read with
`allow_pickle=False`). Its `header` is JSON: `protocol` (1) and `code` (the `rife.py` SHA-256),
then `probe`, or `expect`, `t`, `modes` and `read`. Its images are RIFE's input as `rife.inputs`
makes it: the colour (H × W × 3) and the coverage (H × W) of the two frames, uint8. The answer
holds the identity, and the made colour and coverage (uint8) and, for a read, the disagreement
-(float32); or an error (`protocol`, `code`, `identity` or `failed`, with what it names). Reports
-name the executor: `interpolator` reads `kind` `rife-torch`, `executor`, and the executor's
-`device` and `gpu`; the overlapped wrap counts `runs` and `executor_calls`.
+(float32); or an error (`protocol`, `code`, `identity` or `failed`, with what it names). Each
+member of the archive is read whole, so its CRC-32 is checked before its array is read: an answer
+damaged on the way does not read, and hands on no other pixels. Reports name the executor:
+`interpolator` reads `kind` `rife-torch`, `executor`, and the executor's `device` and `gpu`; the
+overlapped wrap counts `runs` and `executor_calls`.
### Install — `sprite-gen rife install`
diff --git a/sprite_gen/video/rife.py b/sprite_gen/video/rife.py
index 8023f61..d92fe65 100644
--- a/sprite_gen/video/rife.py
+++ b/sprite_gen/video/rife.py
@@ -750,6 +750,12 @@ class Binary:
raise RifeUnavailable(f"{self.binary.name}: the binary or model it runs from could not be read: {exc}") from exc
+class Unreadable(ValueError):
+ """Bytes that are not a request or an answer between a `RemoteRunner` and its executor (`unpack`), or a request or an
+ answer that is not what it is to be (`_asked`, `RemoteRunner`): the message names why, and where reading the bytes
+ raised, what it raised is the cause."""
+
+
def pack(header: dict, arrays: dict[str, np.ndarray] | None = None) -> bytes:
"""A request or an answer between a `RemoteRunner` and its executor as bytes: `header` as UTF-8 JSON and `arrays`, in one
compressed NumPy archive."""
@@ -759,24 +765,54 @@ def pack(header: dict, arrays: dict[str, np.ndarray] | None = None) -> bytes:
def unpack(data: bytes) -> tuple[dict, dict[str, np.ndarray]]:
- """`pack` read back: (header, arrays). Anything else is a ValueError naming why — pickled objects among them, which are
- never loaded."""
+ """`pack` read back: (header, arrays). Anything else is `Unreadable`, naming why — and whatever reading the bytes raises
+ on the way is that: a member cut short, a block that does not decompress, a CRC-32 that does not match, a header that
+ does not parse, pickled objects (which are never loaded); what it raised is named and kept as the cause. Only where
+ `pack`'s own bytes do not read either is it the reading that fails, not the bytes: a fault of the code, raised as it
+ is."""
if not isinstance(data, (bytes, bytearray, memoryview)):
- raise ValueError(f"{type(data).__name__}, not bytes")
+ raise Unreadable(f"{type(data).__name__}, not bytes")
try:
- archive = np.load(io.BytesIO(bytes(data)), allow_pickle=False)
- if not isinstance(archive, np.lib.npyio.NpzFile):
- raise ValueError("an array, not an archive of them")
- with archive:
- arrays = {name: archive[name] for name in archive.files}
- header = json.loads(arrays.pop("header").tobytes().decode("utf-8"))
- except (OSError, ValueError, EOFError, KeyError, zipfile.BadZipFile) as exc:
- raise ValueError(f"{type(exc).__name__}: {exc}") from exc
+ return _unpacked(bytes(data))
+ except Unreadable:
+ raise
+ except Exception as exc:
+ if not _reads_pack():
+ raise
+ kind = type(exc)
+ named = kind.__qualname__ if kind.__module__ == "builtins" else f"{kind.__module__}.{kind.__qualname__}"
+ raise Unreadable(f"{named}: {exc}" if str(exc) else named) from exc
+
+
+def _unpacked(data: bytes) -> tuple[dict, dict[str, np.ndarray]]:
+ """`unpack`'s reading: the bytes opened as a zip archive, each member read whole — so its CRC-32 is checked — and only
+ then read as a NumPy array, pickled objects refused; the header as JSON. NumPy's own archive reader stops where a
+ member's header says its array ends: a header damaged to a shorter length gives the array read from the wrong place,
+ its CRC-32 never reached."""
+ with zipfile.ZipFile(io.BytesIO(data)) as archive:
+ members = [(info.filename, archive.read(info)) for info in archive.infolist()]
+ arrays = {}
+ for name, member in members:
+ if not name.endswith(".npy"):
+ raise Unreadable(f"{name} is not an array")
+ arrays[name.removesuffix(".npy")] = np.lib.format.read_array(io.BytesIO(member), allow_pickle=False)
+ if "header" not in arrays:
+ raise Unreadable("it has no header")
+ header = json.loads(arrays.pop("header").tobytes().decode("utf-8"))
if not isinstance(header, dict):
- raise ValueError("its header is not an object")
+ raise Unreadable("its header is not an object")
return header, arrays
+def _reads_pack() -> bool:
+ """Whether `unpack`'s reading reads what `pack` writes."""
+ try:
+ _unpacked(pack({}))
+ except Exception:
+ return False
+ return True
+
+
def _differs(held: dict, other: object) -> str:
"""Where another identity is not the one held, field by field (`gpu: Tesla T4 -> NVIDIA L4`)."""
other = other if isinstance(other, dict) else {}
@@ -785,23 +821,23 @@ def _differs(held: dict, other: object) -> str:
def _asked(header: dict, arrays: dict[str, np.ndarray]) -> tuple[list[np.ndarray], list[np.ndarray], float, tuple[str, ...], bool]:
"""A frame's or a read's request, as `RemoteRunner` sends it: (colour, coverage, t, the augmentation, whether the passes
- are read), or a ValueError naming what is not so."""
+ are read), or `Unreadable` naming what is not so."""
t, modes, read = header.get("t"), header.get("modes"), header.get("read")
if isinstance(t, bool) or not isinstance(t, (int, float)):
- raise ValueError(f"t is {t!r}, not a number")
+ raise Unreadable(f"t is {t!r}, not a number")
if (not isinstance(modes, list) or any(not isinstance(m, str) or m not in TTA_MODES for m in modes)
or len(set(modes)) < len(modes)):
- raise ValueError(f"the augmentation is {modes!r}, not a list of {' or '.join(TTA_MODES)}, each once")
+ raise Unreadable(f"the augmentation is {modes!r}, not a list of {' or '.join(TTA_MODES)}, each once")
if not isinstance(read, bool):
- raise ValueError(f"read is {read!r}, not true or false")
+ raise Unreadable(f"read is {read!r}, not true or false")
cover = arrays.get("cover_a")
if cover is None or cover.ndim != 2:
- raise ValueError("cover_a is not an image of one channel")
+ raise Unreadable("cover_a is not an image of one channel")
h, w = cover.shape
for name, shape in (("colour_a", (h, w, 3)), ("colour_b", (h, w, 3)), ("cover_a", (h, w)), ("cover_b", (h, w))):
got = arrays.get(name)
if got is None or got.shape != shape or got.dtype != np.uint8:
- raise ValueError(f"{name} is {'missing' if got is None else f'{got.shape} {got.dtype}'}, not {shape} uint8")
+ raise Unreadable(f"{name} is {'missing' if got is None else f'{got.shape} {got.dtype}'}, not {shape} uint8")
return [arrays["colour_a"], arrays["colour_b"]], [arrays["cover_a"], arrays["cover_b"]], float(t), tuple(modes), read
@@ -815,7 +851,7 @@ def answer(request: bytes, runner: Callable[[], object]) -> bytes:
answered in its words; anything else is raised, a fault of the code."""
try:
header, arrays = unpack(request)
- except ValueError as exc:
+ except Unreadable as exc:
return pack({"error": "failed", "message": f"the request does not read: {exc}"})
if header.get("protocol") != PROTOCOL:
return pack({"error": "protocol", "protocol": PROTOCOL})
@@ -832,7 +868,7 @@ def answer(request: bytes, runner: Callable[[], object]) -> bytes:
return pack({"error": "identity", "identity": own})
try:
colour, cover, t, modes, read = _asked(header, arrays)
- except ValueError as exc:
+ except Unreadable as exc:
return pack({"error": "failed", "message": f"the request does not read: {exc}"})
def run(x: np.ndarray, y: np.ndarray, at: float, named: tuple[str, ...] | None) -> np.ndarray:
@@ -859,17 +895,14 @@ class RemoteRunner:
carries it (`expect`): an executor that is not it any more makes nothing and says what differs, and an answer from
another is not handed on — so every frame handed on, and every frame a `Cache` keeps under its identity, is that
executor's. An executor whose protocol or rife.py is another is refused as it is made. Nothing is asked again or run
- elsewhere: a send that fails, an answer that does not read and the executor's own failure are each a RIFE that fails
- (`RifeUnavailable`). Counts its calls of the send (`calls`)."""
+ elsewhere: a send that fails, an answer that does not read — however it does not (`_read`) — and the executor's own
+ failure are each a RIFE that fails (`RifeUnavailable`). Counts its calls of the send (`calls`)."""
def __init__(self, send: Callable[[bytes], bytes], name: str):
self.send, self.name = send, name
self.calls = 0
self.remote: dict = {}
- header, _ = self._sent({"probe": True}, {})
- if not isinstance(header.get("identity"), dict):
- raise RifeUnavailable(f"{name}: the executor's answer does not read: it names no identity")
- self.remote = header["identity"]
+ self.remote = self._sent({"probe": True}, {}, {})[0]
def frame(self, colour: list[np.ndarray], cover: list[np.ndarray], t: float) -> tuple[np.ndarray, np.ndarray]:
made = self._made(colour, cover, t, read=False)
@@ -891,23 +924,16 @@ class RemoteRunner:
augmentation this host asks for now (`tta()`) and whether the passes are read; the answer is that identity's, at
the frame's size, or it is not handed on."""
h, w = cover[0].shape
- header, arrays = self._sent({"expect": self.remote, "t": t, "modes": list(tta()), "read": read},
- {"colour_a": colour[0], "colour_b": colour[1], "cover_a": cover[0], "cover_b": cover[1]})
- if header.get("identity") != self.remote:
- raise RifeUnavailable(f"{self.name}: the executor answered as another than this RIFE runs as "
- f"({_differs(self.remote, header.get('identity'))}); its frame is not handed on")
want = {"rgb": ((h, w, 3), np.uint8), "alpha": ((h, w), np.uint8), **({"apart": ((h, w), np.float32)} if read else {})}
- for name, (shape, dtype) in want.items():
- got = arrays.get(name)
- if got is None or got.shape != shape or got.dtype != dtype:
- found = "missing" if got is None else f"{got.shape} {got.dtype}"
- raise RifeUnavailable(f"{self.name}: the executor's answer does not read: {name} is {found}, not {shape} "
- f"{np.dtype(dtype)}")
- return arrays
-
- def _sent(self, header: dict, arrays: dict[str, np.ndarray]) -> tuple[dict, dict[str, np.ndarray]]:
- """One call of the send, this engine's protocol and code (CODE_SHA256) heading the request: the answer, read, or
- the refusal or failure it names (`RifeUnavailable`)."""
+ return self._sent({"expect": self.remote, "t": t, "modes": list(tta()), "read": read},
+ {"colour_a": colour[0], "colour_b": colour[1], "cover_a": cover[0], "cover_b": cover[1]}, want)[1]
+
+ def _sent(self, header: dict, arrays: dict[str, np.ndarray],
+ want: dict[str, tuple[tuple[int, ...], type]]) -> tuple[dict, dict[str, np.ndarray]]:
+ """One call of the send, this engine's protocol and code (CODE_SHA256) heading the request, and its answer read
+ (`_read`: the identity it names and its arrays, `want`'s at their shapes and dtypes). An answer that does not read,
+ however it does not, is a RIFE that fails here, naming why in the reading's words, what reading it raised the
+ cause."""
request = pack({"protocol": PROTOCOL, "code": CODE_SHA256, **header}, arrays)
self.calls += 1
try:
@@ -915,9 +941,16 @@ class RemoteRunner:
except Exception as exc: # what carries the bytes is the host's, failing in its own words
raise RifeUnavailable(f"{self.name}: sending to the executor failed: {exc}") from exc
try:
- answered, got = unpack(data)
- except ValueError as exc:
- raise RifeUnavailable(f"{self.name}: the executor's answer does not read: {exc}") from exc
+ return self._read(data, header.get("expect"), want)
+ except Unreadable as exc:
+ raise RifeUnavailable(f"{self.name}: the executor's answer does not read: {exc}") from (exc.__cause__ or exc)
+
+ def _read(self, data: bytes, expect: dict | None,
+ want: dict[str, tuple[tuple[int, ...], type]]) -> tuple[dict, dict[str, np.ndarray]]:
+ """The executor's answer read whole (`unpack`): the identity it names — the one the request `expect`s, where it
+ names one — and its arrays, each `want` names there at its shape and dtype. One that is not so is `Unreadable`;
+ the refusal or the failure it names, and an answer from another identity, are each a RIFE that fails."""
+ answered, got = unpack(data)
error = answered.get("error")
if error == "protocol":
raise RifeUnavailable(f"{self.name}: the executor speaks protocol {answered.get('protocol')}, this one {PROTOCOL}: "
@@ -931,7 +964,18 @@ class RemoteRunner:
"runs as it is")
if error is not None:
raise RifeUnavailable(f"{self.name}: the executor failed: {answered.get('message', error)}")
- return answered, got
+ identity = answered.get("identity")
+ if not isinstance(identity, dict):
+ raise Unreadable("it names no identity")
+ if expect is not None and identity != expect:
+ raise RifeUnavailable(f"{self.name}: the executor answered as another than this RIFE runs as "
+ f"({_differs(expect, identity)}); its frame is not handed on")
+ for name, (shape, dtype) in want.items():
+ found = got.get(name)
+ if found is None or found.shape != shape or found.dtype != dtype:
+ raise Unreadable(f"{name} is {'missing' if found is None else f'{found.shape} {found.dtype}'}, not {shape} "
+ f"{np.dtype(dtype)}")
+ return identity, got
def torch_runner(model: Path | None = None) -> object: