Spaces:
Paused
Paused
Zhen Ye Claude Opus 4.6 commited on
Commit ·
4c36e1e
1
Parent(s): 63684e4
fix: add runtime fallback for torch.compile inductor/triton failures
Browse filestorch.compile wrapping succeeds immediately but actual compilation
happens lazily on first forward pass. Now catches runtime errors on
the first propagate_in_video yield, reverts all compiled forwards
to eager originals, and retries the propagation transparently.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
388 unmodified lines3893903913923933943953 unmodified lines399400401402403404405406407408409410411412413414415416417418177 unmodified lines596597598599600601602603604388 unmodified lines"""Compile SAM2 sub-modules with torch.compile (max-autotune).Compiles 5 components matching Facebook's official VOS recipe.Falls back silently to eager mode on any compilation error."""vp = self._video_predictorcomponents = [3 unmodified lines("sam_prompt_encoder", dict(mode="max-autotune", fullgraph=True, dynamic=False)),("sam_mask_decoder", dict(mode="max-autotune", fullgraph=True, dynamic=False)),]compiled = []for attr, kwargs in components:module = getattr(vp, attr, None)if module is None:continuetry:module.forward = torch.compile(module.forward, **kwargs)compiled.append(attr)except Exception as e:logging.warning("torch.compile failed for %s: %s", attr, e)if compiled:logging.info("torch.compile applied to: %s", ", ".join(compiled))else:logging.info("torch.compile not available, using eager mode.")# -- Single-frame interface (Segmenter.predict) -------------------------177 unmodified linesclass_names_list: List[str] = []cursor = 0for out_frame_idx, out_obj_ids, out_mask_logits in self._video_predictor.propagate_in_video(inference_state, max_frame_num_to_track=step, start_frame_idx=start_idx,):bool_masks = (out_mask_logits[:, 0] > 0.0) # (N, H, W) GPU asyncn = bool_masks.shape[0]388 unmodified lines3893903913923933943953963973 unmodified lines401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435177 unmodified lines613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644388 unmodified lines"""Compile SAM2 sub-modules with torch.compile (max-autotune).Compiles 5 components matching Facebook's official VOS recipe.torch.compile wraps succeed immediately; actual Triton/inductorcompilation happens lazily on first forward pass. We storeoriginal forwards so propagate_segment can fall back on error."""vp = self._video_predictorcomponents = [3 unmodified lines("sam_prompt_encoder", dict(mode="max-autotune", fullgraph=True, dynamic=False)),("sam_mask_decoder", dict(mode="max-autotune", fullgraph=True, dynamic=False)),]self._original_forwards: Dict[str, Any] = {}compiled = []for attr, kwargs in components:module = getattr(vp, attr, None)if module is None:continuetry:self._original_forwards[attr] = module.forwardmodule.forward = torch.compile(module.forward, **kwargs)compiled.append(attr)except Exception as e:logging.warning("torch.compile wrapping failed for %s: %s", attr, e)if compiled:logging.info("torch.compile applied to: %s", ", ".join(compiled))self._torch_compiled = Trueelse:logging.info("torch.compile not available, using eager mode.")self._torch_compiled = Falsedef _revert_torch_compile(self):"""Revert compiled forwards back to eager originals."""vp = self._video_predictorfor attr, orig_fwd in self._original_forwards.items():module = getattr(vp, attr, None)if module is not None:module.forward = orig_fwdself._original_forwards.clear()self._torch_compiled = Falselogging.warning("Reverted torch.compile — falling back to eager mode.")# -- Single-frame interface (Segmenter.predict) -------------------------177 unmodified linesclass_names_list: List[str] = []cursor = 0# Wrap generator to catch torch.compile runtime failures on first frame.# If inductor/triton fails, revert to eager and restart propagation._generator = self._video_predictor.propagate_in_video(inference_state, max_frame_num_to_track=step, start_frame_idx=start_idx,)if getattr(self, '_torch_compiled', False) and not getattr(self, '_compile_verified', False):try:_first = next(_generator)except Exception as e:logging.warning("torch.compile runtime error, reverting to eager: %s", e)self._revert_torch_compile()# Re-init propagation with eager forwardsself._video_predictor.reset_state(inference_state)for obj_id, obj_info in mask_dict.labels.items():self._video_predictor.add_new_mask(inference_state, start_idx, obj_id, obj_info.mask,)_generator = self._video_predictor.propagate_in_video(inference_state, max_frame_num_to_track=step, start_frame_idx=start_idx,)_first = next(_generator)import itertoolsself._compile_verified = True_generator = itertools.chain([_first], _generator)for out_frame_idx, out_obj_ids, out_mask_logits in _generator:bool_masks = (out_mask_logits[:, 0] > 0.0) # (N, H, W) GPU asyncn = bool_masks.shape[0]
models/segmenters/grounded_sam2.py+44-4