product-recognition-demo/recognization/code
kiyo9w 3614b23a2c fix: serialize gallery inference so Metal cannot be entered twice
The server segfaulted a few seconds into every live scan that used the crop
lane. The crash report put the fault in libtorch's Metal shader library
(`MetalShaderLibrary::exec_unary_kernel` inside an unordered_map insert),
reached from `copy_cast_kernel_mps` under `clone`/`reshape`.

Reproduced deterministically: eight threads calling `embed()` on a fresh
process crash it, while the same eight threads after one warm forward are
fine. Metal's shader library is process-global and is not thread-safe while
kernels are still being compiled into it, which is exactly what the first
forward does. The detector already serializes its predict for this reason;
the gallery did not.

Two changes:

* A module-level `_MPS_INFER_LOCK` covers the whole MPS region of a forward -
  model load, `.to(device)`, the forward, and the normalize that follows it.
  Normalize dispatches its own Metal kernels, so leaving it outside the lock
  left the same first-compile race open. Module-level because the state being
  protected is the process's, not one gallery's.
* A cache hit skips the embedding forward, so the model used to be built and
  the kernels compiled by whichever classify worker asked first - up to eight
  racing into one process-wide cache. The load path now warms the backbone
  single-threaded under the existing lock, on the cache-hit branch too. The
  readiness path loads the gallery before the socket accepts, so the warmup
  is free. It is Metal-only and best-effort: a CPU gallery has no shader
  library to compile, and a cache hit is served without a usable backbone by
  contract.

The regression test records overlap rather than timing: a fake backbone that
sleeps and counts concurrent entries, run from four threads, asserting the
count never exceeds one. It fails on the previous gallery.py and needs no MPS.
2026-09-28 17:22:55 +09:00
..
web fix: restore reliable authenticated live recognition 2026-09-28 12:38:15 +09:00
__init__.py Initial commit for push folder 2026-09-18 09:13:53 +09:00
app.py fix: bound and gate the crop evidence lane 2026-09-28 17:22:52 +09:00
bench_engine.py feat: recognition engine and session tracker for multi-client serving 2026-09-24 11:52:59 +09:00
bench_identity.py feat: a multi-view embedding gallery, because one template per SKU cannot work 2026-09-24 23:43:27 +09:00
build_store_from_shelf.py fix: require explicit safe shelf enrollment paths 2026-09-28 13:41:43 +09:00
collect_shelf_images.py feat: a live path that uses the enrolled store, and a demo server a phone can reach 2026-09-25 10:51:57 +09:00
detector.py refactor: make the matcher and detector safe for concurrent clients 2026-09-24 11:43:24 +09:00
empty_shelf_detector.py Initial commit for push folder 2026-09-18 09:13:53 +09:00
engine.py perf: reuse sample dimensions and catalog digest 2026-09-28 14:06:34 +09:00
eval_external_images.py test: evaluate on 54 real store photos the pipeline has never seen 2026-09-25 02:21:00 +09:00
eval_live.py fix: record and require the walk identity backend 2026-09-28 14:42:03 +09:00
feature_matcher.py feat: recognition server API, reference stream client, and live label memory 2026-09-24 12:31:17 +09:00
finetune_identity.py fix: refuse unavailable gallery and pin head trainer 2026-09-28 14:41:57 +09:00
gallery.py fix: serialize gallery inference so Metal cannot be entered twice 2026-09-28 17:22:55 +09:00
identity.py fix: restore reliable authenticated live recognition 2026-09-28 12:38:15 +09:00
inventory.py Initial commit for push folder 2026-09-18 09:13:53 +09:00
live.py fix: bound and gate the crop evidence lane 2026-09-28 17:22:52 +09:00
observe.py feat: identity decision contract and session-local observation model 2026-09-24 11:43:41 +09:00
ocr.py feat: add bounded crop evidence and keyed gallery cache 2026-09-28 15:49:34 +09:00
pipeline.py feat: identity decision contract and session-local observation model 2026-09-24 11:43:41 +09:00
reconcile.py fix: a tag may not accuse on weak evidence, nor name a distant product 2026-09-25 01:30:03 +09:00
record.py fix: record and require the walk identity backend 2026-09-28 14:42:03 +09:00
shelf_map.py feat: a shelf map, so a facing outlives the track id that found it 2026-09-25 01:54:38 +09:00
stream_client.py fix: restore reliable authenticated live recognition 2026-09-28 12:38:15 +09:00
tag_cards.py fix: restore reliable authenticated live recognition 2026-09-28 12:38:15 +09:00
tags.py fix: bind tag projection to each frame origin 2026-09-28 14:41:49 +09:00
test_api.py fix: bound and gate the crop evidence lane 2026-09-28 17:22:52 +09:00
test_engine.py fix: refuse unavailable gallery and pin head trainer 2026-09-28 14:41:57 +09:00
test_eval_live.py fix: record and require the walk identity backend 2026-09-28 14:42:03 +09:00
test_gallery.py fix: serialize gallery inference so Metal cannot be entered twice 2026-09-28 17:22:55 +09:00
test_live.py fix: bound and gate the crop evidence lane 2026-09-28 17:22:52 +09:00
test_observe.py feat: identity decision contract and session-local observation model 2026-09-24 11:43:41 +09:00
test_ocr_tags.py fix: bind tag projection to each frame origin 2026-09-28 14:41:49 +09:00
test_real_model.py docs: API contract, README for the server and live mode, real-model test 2026-09-24 12:46:54 +09:00
test_reconcile.py fix: a tag may not accuse on weak evidence, nor name a distant product 2026-09-25 01:30:03 +09:00
test_record.py fix: record and require the walk identity backend 2026-09-28 14:42:03 +09:00
test_shelf_map.py feat: a shelf map, so a facing outlives the track id that found it 2026-09-25 01:54:38 +09:00
test_suite.py Initial commit for push folder 2026-09-18 09:13:53 +09:00
test_tag_cards.py fix: the number on the tag is the regular price, and a price must be corroborated 2026-09-25 01:29:46 +09:00
test_tag_identity.py fix: a tag may not accuse on weak evidence, nor name a distant product 2026-09-25 01:30:03 +09:00
test_tracking.py test: defend engine and tracking contracts 2026-09-24 12:43:32 +09:00
tracking.py fix: restore reliable authenticated live recognition 2026-09-28 12:38:15 +09:00
train.py Initial commit for push folder 2026-09-18 09:13:53 +09:00
transport.py fix: guard reserved sessions and bounded response state 2026-09-28 13:41:35 +09:00
yolo_adapter.py feat: identity decision contract and session-local observation model 2026-09-24 11:43:41 +09:00