Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[linen] Linesearch (and lbfgs) support for TrainState #4471

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions flax/training/train_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ class TrainState(struct.PyTreeNode):
tx: optax.GradientTransformation = struct.field(pytree_node=False)
opt_state: optax.OptState = struct.field(pytree_node=True)

def apply_gradients(self, *, grads, **kwargs):
def apply_gradients(self, *, grads, value=None, value_fn=None, **kwargs):
"""Updates ``step``, ``params``, ``opt_state`` and ``**kwargs`` in return value.

Note that internally this function calls ``.tx.update()`` followed by a call
to ``optax.apply_updates()`` to update ``params`` and ``opt_state``.

For ``optax.GradientTransformationExtraArgs``, the optional ``value`` and
``value_fn`` are passed to ``.tx.update()``.

Args:
grads: Gradients that have the same pytree structure as ``.params``.
value (optional): value of the objective associated with the current grads update.
value_fn (optional): function to evaluate the objective given the model.
**kwargs: Additional dataclass attributes that should be ``.replace()``-ed.

Returns:
Expand All @@ -100,9 +105,20 @@ def apply_gradients(self, *, grads, **kwargs):
grads_with_opt = grads
params_with_opt = self.params

updates, new_opt_state = self.tx.update(
grads_with_opt, self.opt_state, params_with_opt
)
if value is None or value_fn is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if either of them is not None we should pass them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll update the PR

Copy link
Author

@emiresenov emiresenov Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figure the same logic should hold for grad since e.g. scale_by_polyak also usesGradientTransformationExtraArgs and only takes value as an additional keyword argument in .update().

I moved all three optional args to an update_kwargs dictionary, adding each to .update() only if they are not None. Additionally, my former commit should not have sent the **kwargs to .update() sinceTrainState uses these for something else – I mixed up the usage from the merged PR I was referring to.

See the changes in my latest commit and let me know if there are any issues.

updates, new_opt_state = self.tx.update(
grads_with_opt, self.opt_state, params_with_opt
)
else:
updates, new_opt_state = self.tx.update(
grads, self.opt_state,
params_with_opt,
grad=grads,
value=value,
value_fn=value_fn,
**kwargs,
)

new_params_with_opt = optax.apply_updates(params_with_opt, updates)

# As implied by the OWG name, the gradients are used directly to update the
Expand Down