Skip to content

En/review eg/losse val grad#39

Merged
eduardolneto merged 5 commits into
eg/analysisfrom
en/review_eg/losse_val_grad
Jul 7, 2026
Merged

En/review eg/losse val grad#39
eduardolneto merged 5 commits into
eg/analysisfrom
en/review_eg/losse_val_grad

Conversation

@eduardolneto

Copy link
Copy Markdown
Contributor

Modifying losses.py to allow custom_losses to have a val_and_grad function instead of only a grad function. This is necessary for the augmented lagragian modifications in the parallel pull request, since in lbfgsb the val_and_grad option is more efficient and the only viable option. Changes on losses.py.

…ction instead of only a grad function. This is necessary for the augmented lagragian modifications in the parallel pull request, since in lbfgsb the val_and_grad option is more efficient and the only viable option.
@eduardolneto eduardolneto changed the base branch from main to eg/analysis June 27, 2026 14:29

@EstevaoMGomes EstevaoMGomes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great changes!

Comment thread essos/losses.py
Comment thread essos/losses.py

@rogeriojorge rogeriojorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly 3 minor stuff:

  1. losses.py:111, custom_loss.call_pytree():
    dofs_to_pytree now returns a named dict, but call_pytree() still does self.fun(*dofs_pytree, **self.kwargs). If dofs_pytree is a dict, this passes the keys as positional args. I think this should either be:

args = tuple(dofs_pytree[name] for name in self.args_names)
return self.fun(*args, **self.kwargs)

or, if the custom loss functions are expected to accept keyword args:

return self.fun(**dofs_pytree, **self.kwargs)

  1. losses.py:132-136, custom_loss.grad_pytree()

same things. It currently does *dofs_pytree, which will be wrong if dofs_pytree is the new named dict. I would build

args = tuple(dofs_pytree[name] for name in self.args_names)

and differentiate with those positional args, then put the gradients back into buffer.

  1. losses.py:80, _ensure_unravelers

I think "or" is actually the right condition here. Using "and" would only rebuild when all of them are None, which could leave _dofs_to_args or _dofs_to_pytree unset. But not sure.


A small test for custom_loss.value_and_grad() with two dependencies, and dofs_to_pytree() followed by call_pytree() / grad_pytree() would probably catch the dict-vs-tuple issue above.

@eduardolneto

Copy link
Copy Markdown
Contributor Author

I think I addressed all the comments for this PR.

@rogeriojorge

Copy link
Copy Markdown
Member

Can you just add one assertion for value_and_grad() itself in test_custom_loss_named_unraveler()?
Otherwise, it's good for me to merge

@rogeriojorge rogeriojorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you just add one assertion for value_and_grad() itself in test_custom_loss_named_unraveler()?
Otherwise, it's good for me to merge

@eduardolneto

Copy link
Copy Markdown
Contributor Author

Added assertion for val_and_grad

@EstevaoMGomes EstevaoMGomes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

After fixing the test, this PR is ready to be merged.

Comment thread tests/test_multiobjectives.py
@eduardolneto eduardolneto merged commit c20dd00 into eg/analysis Jul 7, 2026
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.

3 participants