Skip to content

Warn and skip optimizer step for parameters with no gradients - #213

Open
imar6teen wants to merge 1 commit into
zou-group:mainfrom
imar6teen:fix/optimizer-step-no-gradient-warning
Open

imar6teen wants to merge 1 commit into
zou-group:mainfrom
imar6teen:fix/optimizer-step-no-gradient-warning

Conversation

@imar6teen

Copy link
Copy Markdown

Summary

Fixes #100

Previously, calling optimizer.step() on a parameter with no gradients (or only empty gradient text) silently sent an empty <CONTEXT></CONTEXT> prompt to the optimizer LLM and overwrote parameter.value.

Changes

  • Added Optimizer._has_gradient(parameter) in textgrad/optimizer/optimizer.py to check whether a parameter has non-empty gradients, emitting a UserWarning and logging a warning via logger.warning if not.
  • Updated TextualGradientDescent.step() and TextualGradientDescentwithMomentum.step() to skip updating parameters without gradients.

Verification

Verified across the following cases:

  • Calling step() before backward() emits a UserWarning, skips the engine call, and preserves the parameter value.
  • Calling step() after zero_grad() behaves the same.
  • With mixed parameters (one with gradients, one without), only the parameter with gradients is updated while the ungraded parameter warns and is skipped.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optimizer.step() does not check if the parameter has gradient

1 participant