Add sampler lifecycle callbacks#14115
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR adds sampler lifecycle callback support to ComfyUI's sampling pipeline. It introduces three new callback event identifiers (ON_SAMPLER_START, ON_SAMPLER_STEP, ON_SAMPLER_END) in CallbacksMP and integrates callback dispatch into KSAMPLER.sample. KSAMPLER.sample now reads callbacks from extra_args["model_options"], invokes start callbacks with sampler metadata, calls step callbacks (and the original callback) each step with sigma and tensor-shape info, and invokes end callbacks with the final samples shape. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@comfy/samplers.py`:
- Around line 1041-1051: The sampling call using self.sampler_function(...)
should be wrapped in a try/finally so sampler_end_callbacks are always invoked
even on exceptions; move the inverse_noise_scaling and the construction of
sampler_info into the try block for the success path, and in the finally iterate
sampler_end_callbacks to emit the end event with sampler_info where
"samples_shape": tuple(samples.shape) on success but "samples_shape": None on
failure, referencing self.sampler_function,
model_wrap.inner_model.model_sampling.inverse_noise_scaling,
sampler_end_callbacks, sampler_info and k_callback so listeners aren't left
stuck when sampling throws.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: bd84356e-7c9f-4667-b968-688d78590967
📒 Files selected for processing (2)
comfy/patcher_extension.pycomfy/samplers.py
Summary
Adds three sampler lifecycle callback identifiers to
CallbacksMP:ON_SAMPLER_STARTON_SAMPLER_STEPON_SAMPLER_ENDKSAMPLER.sample()now emits these callbacks around the existing k-diffusion callback path. The callbacks receive sampling metadata such as step index, sigmas, total step count, sampler function name, and tensor shapes.Motivation
Some custom nodes need to observe sampler lifecycle information for debugging, profiling, progress tracking, or step-aware extension behavior. Today those extensions often need to wrap or replace sampler internals even when they only need metadata.
Comfy already exposes wrappers and callbacks through
patcher_extension; this change extends that existing mechanism to sampler lifecycle events without adding node UI or changing default sampling behavior.Scope
This intentionally does not expose mutable latent tensors to callbacks. The step callback includes
x_shapeanddenoised_shape, not the tensors themselves.If there is interest in a latent observer or transform callback later, that should be handled separately because it needs a more explicit contract for ordering, mutation, dtype/device/shape validation, batching, masks, determinism, and multiple callbacks.
Compatibility
Testing
python3 -m py_compile comfy/patcher_extension.py comfy/samplers.pygit diff --checkKSAMPLERsampler function verified thatON_SAMPLER_START,ON_SAMPLER_STEP, andON_SAMPLER_ENDcallbacks are emitted in order while preserving the existing callback path.