Commit Graph
7 Commits
Author SHA1 Message Date
digital-garbageandClaude Sonnet 4.6 b0a2c68689 Add pre-generation conditioning ascent to value function guidance
Before each denoising run, when the value function is ready, gradient-ascend
the text conditioning until predicted reward plateaus (self-terminating: step
size = 0.5% of conditioning norm, stops when delta < 1e-3, caps at 50 steps).
No user-facing params — fully automated. Per-step wrapper still runs on top.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-30 10:38:34 +03:00
digital-garbageandClaude Sonnet 4.6 55d905e6ee Fix gradient: use torch.empty + copy_ to escape inference tensor flag
.clone() on an inference tensor inherits the inference flag even inside
inference_mode(False). torch.empty() inside that context creates a true
normal tensor; copy_() fills it from the inference tensor without flag
inheritance.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 04:38:24 +03:00
digital-garbageandClaude Sonnet 4.6 4dd8fcd674 Fix value function gradient: move to MLP device before backward pass
MLP lives on CPU, conditioning on GPU — device mismatch silently fell back
to fixed direction. Now moves conditioning to MLP device for the forward/
backward, then returns gradient to original device.

Also surface value function fallback errors instead of swallowing them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 04:35:50 +03:00
digital-garbageandClaude Sonnet 4.6 db5c766a8e Fix value function: wrap entire train_on in inference_mode(False)
Buffer tensors were created inside inference_mode — detach().cpu() doesn't
escape the inference flag. Wrapping the whole method ensures the clone and
all subsequent tensor ops produce normal autograd-tracked tensors.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 03:57:30 +03:00
digital-garbageandClaude Sonnet 4.6 c1c586b27d Fix value function: use inference_mode(False) to exit ComfyUI's exec context
torch.enable_grad() cannot override torch.inference_mode() in older PyTorch.
Add inference_mode(False) as outer wrapper in both train_on and gradient.
Also .clone() tensors from buffer/input before grad tracking to avoid
inference_mode tensor view restrictions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 03:56:00 +03:00
digital-garbageandClaude Sonnet 4.6 766a803a2c Fix value function: wrap train and gradient in torch.enable_grad()
ComfyUI execution context may have torch.no_grad() active, which kills
gradient tracking needed for training and gradient computation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 03:53:25 +03:00
digital-garbageandClaude Sonnet 4.6 44e694695e Add online value function for reward-guided sampling
value_function.py: OnlineValueFunction MLP that trains incrementally on
(conditioning, reward) pairs. Uses replay buffer of 100 samples, 20
gradient steps per update. save/load to .pt file via refinement_state_path.

Studio popup: new "Value guidance" toggle. When on, trains the value function
after every rated generation (good and bad). Falls back to no-op if conditioning
deserialization fails.

chain sampler: _load_value_function reads the trained MLP if ready (5+
samples). _build_embed_guidance_wrapper uses value function gradient
(∂reward/∂conditioning) when available, fixed liked direction as fallback.
Log reports which mode is active and sample count.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-29 03:45:18 +03:00