perf(multi-hmr): autocast opt-in

MULTIHMR_AUTOCAST=1 enables MPS mixed precision for the ViT-S
backbone. Tested 2026-05-13: slower than fp32 baseline (400ms vs
270ms) -- overhead cast within forward exceeds matmul savings on
M5. Off by default; FP16 .half() crashes MPS matmul accumulator,
left out entirely.

apple_vision_pose face parser short-circuits to return immediately
since pyobjc 11 cannot dereference VNFaceLandmarkRegion2D
pointsInImageOfSize_ result. Removes ObjCPointerWarning spam at
30 fps (9 regions per face).
This commit is contained in:
L'électron rare
2026-05-13 23:24:06 +02:00
parent 2c8094c06c
commit 623c47983d
2 changed files with 34 additions and 11 deletions
+2 -4
View File
@@ -557,10 +557,8 @@ class AppleVisionPoseWorker:
# API d'acces simple. Face parsing depuis Apple Vision est
# actuellement bloque ; on garde MediaPipe (CPU XNNPACK) pour
# face/hand fin tandis que Vision sert body 2D sur ANE.
n = 0; n_written = 0
if n_written > 0 and not hasattr(self, "_logged_face_write_" + region_name):
LOG.info("face: %s wrote %d points", region_name, n_written)
setattr(self, "_logged_face_write_" + region_name, True)
# Skip pour eviter le spam ObjCPointerWarning a 30 fps.
return
# faceContour
fill("faceContour", *FACE_OFFSETS["contour"])
+32 -7
View File
@@ -142,6 +142,20 @@ class MultiHMRWorker:
model = Model(**kwargs).to(torch_device)
model.load_state_dict(ckpt["model_state_dict"], strict=False)
model.eval()
# MPS mixed precision via torch.autocast : ~1.3-1.7x sur
# ViT-S backbone, casts auto vers float16 pour les matmuls
# gardant l'accumulator en float32 (necessaire MPS sinon
# "Destination NDArray and Accumulator NDArray cannot have
# different datatype" sur MPSNDArrayMatrixMultiplication).
# Disable via env MULTIHMR_AUTOCAST=0.
# autocast MPS teste 2026-05-13 : plus lent (400ms vs 270ms
# baseline) car overhead de cast dans le forward. Defaut OFF.
# Opt-in via MULTIHMR_AUTOCAST=1.
self._use_autocast = (
device == "mps"
and os.environ.get("MULTIHMR_AUTOCAST", "0") == "1")
if self._use_autocast:
LOG.info("Multi-HMR PyTorch : MPS autocast (fp16) enabled")
# torch.compile teste 2026-05-13 : plante en runtime avec
# `TypeError: torch.Size() takes an iterable of 'int' (item
# is 'FakeTensor')`. Multi-HMR a du shape-arithmetic non
@@ -230,13 +244,24 @@ class MultiHMRWorker:
t_inf_start = time.monotonic()
try:
with torch.no_grad():
humans = model(
tensor,
is_training=False,
nms_kernel_size=self.nms_kernel_size,
det_thresh=self.det_thresh,
K=K,
)
if getattr(self, "_use_autocast", False):
with torch.autocast(device_type="mps",
dtype=torch.float16):
humans = model(
tensor,
is_training=False,
nms_kernel_size=self.nms_kernel_size,
det_thresh=self.det_thresh,
K=K,
)
else:
humans = model(
tensor,
is_training=False,
nms_kernel_size=self.nms_kernel_size,
det_thresh=self.det_thresh,
K=K,
)
except Exception as e:
LOG.warning("inference failed: %s", e)
time.sleep(self.period)