# ResCapsGuard — implementation notes Capsule-based sibling of Res2TCNGuard (same ETASR 2024 paper, Borodin et al.). Wrapped for the Speech Anti-Spoofing Arena on 2026-06-02. ## Network & checkpoint - `_net.py` is **verbatim** from the upstream notebook (`github.com/lab260ru/ResCapsGuard/new_capsules_changed_sinc.ipynb`), cells 6/8/10/13/15/17/19: `SincConv`, `Res_block`, `Encoder`, `ChanelWiseStats`+`View`, `PrimaryCapsules`, `RoutingMechanism`, `CapsuleNet`. No class bodies were edited. - Checkpoint `new_capsules_changed_sinc_layer.pth` (OrderedDict state_dict, 931 keys). - **Construction args confirmed by `strict=True` load + state_dict shape introspection:** `CapsuleNet(num_class=2, gpu_id=<0|-1>, d_args={"nb_samp":64600,"first_conv":128, "filts":[70,[1,32],[32,32],[32,64],[64,64]]}, num_capsules=30, num_iterations=2)`. Evidence: `fea_ext.capsules.0..29` ⇒ num_capsules=30; `routing_stats.route_weights` shape `(2, 30, 4, 8)` ⇒ num_output_capsules=2, num_input_capsules=30, data_out=4, data_in=8. - **Parameters: 1,606,664 (1.607 M).** - **Score direction:** `forward(x, random=False, dropout=0)` returns `(z, class_)`; the wrapper returns `class_[:, 1]` (index 1 = bona fide, higher = more bona fide) — matches the arena convention and the notebook's `produce_evaluation_file`. - **Determinism:** inference uses `random=False, dropout=0` (no routing noise, no dropout). Verified by a test that two `score_batch` calls are bit-identical. ## Device handling (gpu_id) `RoutingMechanism` places helper tensors via a legacy `gpu_id` arg (`.cuda(self.gpu_id)`). The wrapper sets `gpu_id = 0` when CUDA is visible (single pinned device ⇒ ordinal 0), else `-1`. **No `_net.py` patch was needed** — with `gpu_id=0` the `.cuda(0)` guards fire correctly and there is no CPU/GPU mismatch. (A minimal `device=priors.device` patch was the fallback plan but proved unnecessary.) The batch=1 path (the `RoutingMechanism` `squeeze()`/`unsqueeze_` branch) is exercised by a unit test. ## GPU selection gotcha (IMPORTANT) PyTorch defaults to **fastest-first** device ordering, which does NOT match `nvidia-smi`. On this box the two RTX 4070 Ti SUPER cards are nvidia-smi indices 1 and 3, but under torch's default ordering ordinal 3 is a 5060 Ti. Always run with **both**: ``` CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 PYTHONPATH=. speech-spoof-bench run ... ``` Gate used before every run: assert `torch.cuda.get_device_name(0)` contains "4070 Ti SUPER". `PYTHONPATH=.` is also required — `speech-spoof-bench` is a console-script entry point, so CWD is NOT on `sys.path` and `--model-module rescapsguard:...` would otherwise `ModuleNotFoundError`. (Both pitfalls were folded back into the official `docs/developing/new-model.md`.) ## Performance sweep (2026-06-02, RTX 4070 Ti SUPER, dummy 64600-sample inputs, median of 5) | batch_size | median (ms) | utt/s | |-----------:|------------:|-------:| | 1 | 29.225 | 34.22 | | 2 | 31.302 | 63.89 | | 4 | 31.579 | 126.67 | | 8 | 45.280 | 176.68 | | 16 | 89.054 | 179.67 | | 32 | 173.073 | 184.89 | | 64 | 349.076 | 183.34 | **Chosen batch_size = 32** (peak 184.89 utt/s; bs=64 within ~0.8 %, so the smaller was kept to save GPU memory). End-to-end run throughput was ~140 utt/s (decode-bound), so the canonical `speech-spoof-bench run` is used — no custom parallel runner (same conclusion as Res2TCNGuard). ## Arena results (deterministic first-64600-sample window, no random crop) | Dataset | EER % | Trials | Skipped | Notes | |---|---:|---:|---:|---| | ASVspoof2019_LA | 1.86 | 71,237 | 0 | in-domain (paper 2.25 % with random crop) | | ASVspoof2021_LA | 18.70 | 181,566 | 0 | cross-dataset generalization | | ASVspoof2021_DF | 17.00 | 611,829 | 0 | cross-dataset (sibling Res2TCNGuard: 17.02 %) | | CD-ADD | 54.55 | 20,786 | 0 | out-of-domain (modern neural-TTS); does not generalize | | InTheWild | 55.92 | 31,779 | 0 | out-of-domain (real deepfakes); does not generalize | `n_skipped = 0` on all five. Every submission's EER reproduced exactly (`reproduce --scoring --no-local`, Δ 0.0e+00) at the manifest-pinned dataset revision. ## Deviation from the paper The paper trains/evaluates with a **random** 64,600-sample crop; the Arena uses a **deterministic first-64,600-sample window** for exact reproducibility. This is why ASVspoof2019_LA reproduces at 1.86 % rather than the paper's 2.25 % (the sibling Res2TCNGuard showed the same effect: paper 1.49 % → reproduced 1.50 %). ## Submission / arena gotcha (reproduction block) The Arena **hides** any submission whose `reproduction:` block is empty. The documented flow has the submitter open with `reproduction: {}` and the maintainer fill it at merge — but merging without filling it leaves the system off the board and the dynamic tier/rank badges "unranked". For PRs #2–#5 the reproduction block was **pre-filled** (`match: scoring`, reproduced via `reproduce --scoring --no-local`) so a merge is one-click and badges appear immediately. (PR #1 was filled with a follow-up commit to `main` after the fact.) See the project memory `reference-arena-reproduction-gate`.